1
1
package credential
2
2
3
3
import (
4
- "context"
5
4
"crypto/aes"
6
5
"crypto/cipher"
7
6
"encoding/base64"
@@ -11,14 +10,8 @@ import (
11
10
"time"
12
11
13
12
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
14
- corev1 "k8s.io/api/core/v1"
15
- "k8s.io/apimachinery/pkg/api/errors"
16
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17
- "k8s.io/client-go/util/retry"
18
13
19
- "github.com/AliyunContainerService/terway/pkg/backoff"
20
14
"github.com/AliyunContainerService/terway/pkg/utils"
21
- "github.com/AliyunContainerService/terway/pkg/utils/k8sclient"
22
15
)
23
16
24
17
type EncryptedCredentialInfo struct {
@@ -30,65 +23,37 @@ type EncryptedCredentialInfo struct {
30
23
}
31
24
32
25
type EncryptedCredentialProvider struct {
33
- credentialPath string
34
- secretNamespace string
35
- secretName string
26
+ credentialPath string
36
27
}
37
28
38
- // NewEncryptedCredentialProvider get token from file or secret . default filepath /var/addon/token-config
39
- func NewEncryptedCredentialProvider (credentialPath , secretNamespace , secretName string ) * EncryptedCredentialProvider {
40
- return & EncryptedCredentialProvider {credentialPath : credentialPath , secretNamespace : secretNamespace , secretName : secretName }
29
+ // NewEncryptedCredentialProvider get token from file. default filepath /var/addon/token-config
30
+ func NewEncryptedCredentialProvider (credentialPath string ) * EncryptedCredentialProvider {
31
+ return & EncryptedCredentialProvider {credentialPath : credentialPath }
41
32
}
42
33
43
34
func (e * EncryptedCredentialProvider ) Resolve () (* Credential , error ) {
44
- if e .credentialPath == "" && e . secretNamespace == "" && e . secretName == "" {
35
+ if e .credentialPath == "" {
45
36
return nil , nil
46
37
}
47
38
var encodeTokenCfg []byte
48
39
var err error
49
40
var akInfo EncryptedCredentialInfo
50
41
51
- if e .credentialPath != "" {
52
- log .Info ("resolve encrypted credential" , "path" , e .credentialPath )
53
- if utils .IsWindowsOS () {
54
- // NB(thxCode): since os.Stat has not worked as expected,
55
- // we use os.Lstat instead of os.Stat here,
56
- // ref to https://github.com/microsoft/Windows-Containers/issues/97#issuecomment-887713195.
57
- _ , err = os .Lstat (e .credentialPath )
58
- } else {
59
- _ , err = os .Stat (e .credentialPath )
60
- }
61
- if err != nil {
62
- return nil , fmt .Errorf ("failed to read config %s, err: %w" , e .credentialPath , err )
63
- }
64
- encodeTokenCfg , err = os .ReadFile (e .credentialPath )
65
- if err != nil {
66
- return nil , fmt .Errorf ("failed to read token config, err: %w" , err )
67
- }
42
+ log .Info ("resolve encrypted credential" , "path" , e .credentialPath )
43
+ if utils .IsWindowsOS () {
44
+ // NB(thxCode): since os.Stat has not worked as expected,
45
+ // we use os.Lstat instead of os.Stat here,
46
+ // ref to https://github.com/microsoft/Windows-Containers/issues/97#issuecomment-887713195.
47
+ _ , err = os .Lstat (e .credentialPath )
68
48
} else {
69
- log .Info (fmt .Sprintf ("resolve secret %s/%s" , e .secretNamespace , e .secretName ))
70
-
71
- var secret * corev1.Secret
72
- err = retry .OnError (backoff .Backoff (backoff .WaitStsTokenReady ), func (err error ) bool {
73
- if errors .IsNotFound (err ) || errors .IsTooManyRequests (err ) {
74
- return true
75
- }
76
- return false
77
- }, func () error {
78
- secret , err = k8sclient .K8sClient .CoreV1 ().Secrets (e .secretNamespace ).Get (context .Background (), e .secretName , metav1.GetOptions {})
79
- if err != nil {
80
- return err
81
- }
82
- return nil
83
- })
84
- if err != nil {
85
- return nil , err
86
- }
87
- var ok bool
88
- encodeTokenCfg , ok = secret .Data ["addon.token.config" ]
89
- if ! ok {
90
- return nil , fmt .Errorf ("token is not found in addon.network.token" )
91
- }
49
+ _ , err = os .Stat (e .credentialPath )
50
+ }
51
+ if err != nil {
52
+ return nil , fmt .Errorf ("failed to read config %s, err: %w" , e .credentialPath , err )
53
+ }
54
+ encodeTokenCfg , err = os .ReadFile (e .credentialPath )
55
+ if err != nil {
56
+ return nil , fmt .Errorf ("failed to read token config, err: %w" , err )
92
57
}
93
58
94
59
err = json .Unmarshal (encodeTokenCfg , & akInfo )
0 commit comments