Skip to content

Commit 0a2807b

Browse files
authored
Merge pull request #1545 from LiZhenCheng9527/refactor-kmeshctl-secret
refactor kmeshctl secret command
2 parents 8b968ec + 97d2ee9 commit 0a2807b

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

ctl/secret/secret.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3131

3232
"kmesh.net/kmesh/ctl/utils"
33-
"kmesh.net/kmesh/pkg/controller/encryption/ipsec"
33+
"kmesh.net/kmesh/pkg/controller/encryption"
3434
"kmesh.net/kmesh/pkg/kube"
3535
"kmesh.net/kmesh/pkg/logger"
3636
)
@@ -118,7 +118,7 @@ func createKubeClientOrExit() kube.CLIClient {
118118
}
119119

120120
func CreateOrUpdateSecret(cmd *cobra.Command, args []string) {
121-
var ipSecKey, ipSecKeyOld ipsec.IpSecKey
121+
var ipSecKey, ipSecKeyOld encryption.IpSecKey
122122
var err error
123123

124124
ipSecKey.AeadKeyName = AeadAlgoName
@@ -215,7 +215,7 @@ func GetSecret() {
215215
}
216216

217217
// Parse the IPsec data
218-
var ipSecKey ipsec.IpSecKey
218+
var ipSecKey encryption.IpSecKey
219219
if err := json.Unmarshal(secret.Data["ipSec"], &ipSecKey); err != nil {
220220
log.Errorf("failed to unmarshal secret data: %v", err)
221221
os.Exit(1)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright The Kmesh Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package encryption
18+
19+
type IpSecKey struct {
20+
Spi int `json:"spi"`
21+
AeadKeyName string `json:"aeadKeyName"`
22+
AeadKey []byte `json:"aeadKey"`
23+
Length int `json:"length"`
24+
}

pkg/controller/encryption/ipsec/ipsec_handler.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,24 @@ import (
3232
"istio.io/istio/pkg/filewatcher"
3333

3434
"kmesh.net/kmesh/pkg/constants"
35+
"kmesh.net/kmesh/pkg/controller/encryption"
3536
"kmesh.net/kmesh/pkg/kube/apis/kmeshnodeinfo/v1alpha1"
3637
)
3738

3839
const (
3940
IpSecKeyFile = "./kmesh-ipsec/ipSec"
4041
)
4142

42-
type IpSecKey struct {
43-
Spi int `json:"spi"`
44-
AeadKeyName string `json:"aeadKeyName"`
45-
AeadKey []byte `json:"aeadKey"`
46-
Length int `json:"length"`
47-
}
48-
4943
type IpSecHandler struct {
5044
Spi int
5145
mutex sync.RWMutex
5246
watcher filewatcher.FileWatcher
53-
historyIpSecKey map[int]IpSecKey
47+
historyIpSecKey map[int]encryption.IpSecKey
5448
}
5549

5650
func NewIpSecHandler() *IpSecHandler {
5751
return &IpSecHandler{
58-
historyIpSecKey: make(map[int]IpSecKey),
52+
historyIpSecKey: make(map[int]encryption.IpSecKey),
5953
}
6054
}
6155

@@ -76,7 +70,7 @@ func (is *IpSecHandler) LoadIPSecKeyFromFile(filePath string) error {
7670
func (is *IpSecHandler) loadIPSecKeyFromIO(file *os.File) error {
7771
reader := bufio.NewReader(file)
7872
decoder := json.NewDecoder(reader)
79-
var key IpSecKey
73+
var key encryption.IpSecKey
8074
if err := decoder.Decode(&key); err != nil {
8175
return fmt.Errorf("ipsec config file decoder error, %v, please use Kmesh tool generate ipsec secret key", err)
8276
}
@@ -231,7 +225,7 @@ func (is *IpSecHandler) createXfrmRuleIngress(rawRemoteIP, rawLocalNicIP, remote
231225
* ip xfrm state add src {localNicIP} dst {remoteNicIP} proto esp spi {remoteSpi} mode tunnel reqid 1 {aead-algo} {aead-key} {aead-key-length}
232226
* ip xfrm policy add src 0.0.0.0/0 dst {remoteCIDR} dir out tmpl src {localNicIP} dst {remoteNicIP} proto esp spi {remoteSpi} reqid 1 mode tunnel mark 0x{remoteNodeID}00e0
233227
*/
234-
func (is *IpSecHandler) createXfrmRuleEgress(rawLocalNicIP, rawRemoteIP, localBootID, remoteBootID string, ipsecKey IpSecKey, podCIDRs []string) error {
228+
func (is *IpSecHandler) createXfrmRuleEgress(rawLocalNicIP, rawRemoteIP, localBootID, remoteBootID string, ipsecKey encryption.IpSecKey, podCIDRs []string) error {
235229
src := net.ParseIP(rawLocalNicIP)
236230
if src == nil {
237231
return fmt.Errorf("failed to parser ip in inserting xfrm rule, input: %v", rawLocalNicIP)
@@ -267,7 +261,7 @@ func (is *IpSecHandler) createXfrmRuleEgress(rawLocalNicIP, rawRemoteIP, localBo
267261
return nil
268262
}
269263

270-
func (is *IpSecHandler) createStateRule(src net.IP, dst net.IP, key []byte, ipsecKey IpSecKey, ingress bool) error {
264+
func (is *IpSecHandler) createStateRule(src net.IP, dst net.IP, key []byte, ipsecKey encryption.IpSecKey, ingress bool) error {
271265
state := &netlink.XfrmState{
272266
Src: src,
273267
Dst: dst,

0 commit comments

Comments
 (0)