@@ -34,6 +34,14 @@ func (m *CloudProviderAzure) Init(ctx context.Context, opts config.Opts) {
3434 m .opts = opts
3535 m .log = log .WithField ("cloudprovider" , "azure" )
3636
37+ if m .opts .CloudProvider .Azure .KeyVaultName == nil || * m .opts .CloudProvider .Azure .KeyVaultName == "" {
38+ m .log .Panic ("no Azure KeyVault name specified" )
39+ }
40+
41+ if m .opts .CloudProvider .Azure .KeyVaultSecretName == nil || * m .opts .CloudProvider .Azure .KeyVaultSecretName == "" {
42+ m .log .Panic ("no Azure KeyVault secret name specified" )
43+ }
44+
3745 if m .opts .CloudProvider .Config != nil {
3846 os .Setenv ("AZURE_AUTH_LOCATION" , * m .opts .CloudProvider .Config )
3947 }
@@ -65,47 +73,45 @@ func (m *CloudProviderAzure) Init(ctx context.Context, opts config.Opts) {
6573}
6674
6775func (m * CloudProviderAzure ) FetchToken () (token * bootstraptoken.BootstrapToken ) {
68- if m .opts .CloudProvider .Azure .KeyVaultName != nil && m .opts .CloudProvider .Azure .KeyVaultSecretName != nil {
69- vaultName := * m .opts .CloudProvider .Azure .KeyVaultName
70- secretName := * m .opts .CloudProvider .Azure .KeyVaultSecretName
71- vaultUrl := fmt .Sprintf (
72- "https://%s.%s" ,
73- vaultName ,
74- m .environment .KeyVaultDNSSuffix ,
75- )
76-
77- log .Infof ("fetching newest token from Azure KeyVault \" %s\" secret \" %s\" " , vaultName , secretName )
78- secret , err := m .keyvaultClient .GetSecret (m .ctx , vaultUrl , secretName , "" )
79- if err != nil {
80- switch m .getInnerErrorCodeFromAutorestError (err ) {
81- case "SecretNotFound" :
82- // no secret found, need to create new token
83- log .Warn ("no secret found, assuming non existing token" )
84- break
85- case "SecretDisabled" :
86- // disabled secret, continue as there would be no token
87- log .Warn ("current secret is disabled, assuming non existing token" )
88- break
89- case "ForbiddenByPolicy" :
90- // access is forbidden
91- log .Error ("unable to access Azure KeyVault, please check access" )
92- log .Panic (err )
93- default :
94- // not handled error
95- log .Panic (err )
96- }
76+ vaultName := * m .opts .CloudProvider .Azure .KeyVaultName
77+ secretName := * m .opts .CloudProvider .Azure .KeyVaultSecretName
78+ vaultUrl := fmt .Sprintf (
79+ "https://%s.%s" ,
80+ vaultName ,
81+ m .environment .KeyVaultDNSSuffix ,
82+ )
83+
84+ log .Infof ("fetching newest token from Azure KeyVault \" %s\" secret \" %s\" " , vaultName , secretName )
85+ secret , err := m .keyvaultClient .GetSecret (m .ctx , vaultUrl , secretName , "" )
86+ if err != nil {
87+ switch m .getInnerErrorCodeFromAutorestError (err ) {
88+ case "SecretNotFound" :
89+ // no secret found, need to create new token
90+ log .Warn ("no secret found, assuming non existing token" )
91+ break
92+ case "SecretDisabled" :
93+ // disabled secret, continue as there would be no token
94+ log .Warn ("current secret is disabled, assuming non existing token" )
95+ break
96+ case "ForbiddenByPolicy" :
97+ // access is forbidden
98+ log .Error ("unable to access Azure KeyVault, please check access" )
99+ log .Panic (err )
100+ default :
101+ // not handled error
102+ log .Panic (err )
97103 }
104+ }
98105
99- if secret .Value != nil {
100- token = bootstraptoken .ParseFromString (* secret .Value )
101- if token != nil {
102- if secret .Attributes .Created != nil {
103- token .SetCreationUnixTime (* secret .Attributes .Created )
104- }
106+ if secret .Value != nil {
107+ token = bootstraptoken .ParseFromString (* secret .Value )
108+ if token != nil {
109+ if secret .Attributes .Created != nil {
110+ token .SetCreationUnixTime (* secret .Attributes .Created )
111+ }
105112
106- if secret .Attributes .Expires != nil {
107- token .SetExpirationUnixTime (* secret .Attributes .Expires )
108- }
113+ if secret .Attributes .Expires != nil {
114+ token .SetExpirationUnixTime (* secret .Attributes .Expires )
109115 }
110116 }
111117 }
@@ -115,33 +121,31 @@ func (m *CloudProviderAzure) FetchToken() (token *bootstraptoken.BootstrapToken)
115121
116122func (m * CloudProviderAzure ) StoreToken (token * bootstraptoken.BootstrapToken ) {
117123 contextLogger := m .log .WithFields (log.Fields {"token" : token .Id ()})
118- if m .opts .CloudProvider .Azure .KeyVaultName != nil && m .opts .CloudProvider .Azure .KeyVaultSecretName != nil {
119- vaultName := * m .opts .CloudProvider .Azure .KeyVaultName
120- secretName := * m .opts .CloudProvider .Azure .KeyVaultSecretName
121- vaultUrl := fmt .Sprintf (
122- "https://%s.%s" ,
123- vaultName ,
124- m .environment .KeyVaultDNSSuffix ,
125- )
126-
127- contextLogger .Infof ("storing token to Azure KeyVault \" %s\" secret \" %s\" with expiration %s" , vaultName , secretName , token .ExpirationString ())
128-
129- secretParameters := keyvault.SecretSetParameters {
130- Value : stringPtr (token .FullToken ()),
131- Tags : map [string ]* string {
132- "managed-by" : stringPtr ("kube-bootstrap-token-manager" ),
133- "token" : stringPtr (token .Id ()),
134- },
135- ContentType : stringPtr ("kube-bootstrap-token" ),
136- SecretAttributes : & keyvault.SecretAttributes {
137- NotBefore : token .CreationUnixTime (),
138- Expires : token .ExpirationUnixTime (),
139- },
140- }
141- _ , err := m .keyvaultClient .SetSecret (m .ctx , vaultUrl , secretName , secretParameters )
142- if err != nil {
143- log .Panic (err )
144- }
124+ vaultName := * m .opts .CloudProvider .Azure .KeyVaultName
125+ secretName := * m .opts .CloudProvider .Azure .KeyVaultSecretName
126+ vaultUrl := fmt .Sprintf (
127+ "https://%s.%s" ,
128+ vaultName ,
129+ m .environment .KeyVaultDNSSuffix ,
130+ )
131+
132+ contextLogger .Infof ("storing token to Azure KeyVault \" %s\" secret \" %s\" with expiration %s" , vaultName , secretName , token .ExpirationString ())
133+
134+ secretParameters := keyvault.SecretSetParameters {
135+ Value : stringPtr (token .FullToken ()),
136+ Tags : map [string ]* string {
137+ "managed-by" : stringPtr ("kube-bootstrap-token-manager" ),
138+ "token" : stringPtr (token .Id ()),
139+ },
140+ ContentType : stringPtr ("kube-bootstrap-token" ),
141+ SecretAttributes : & keyvault.SecretAttributes {
142+ NotBefore : token .CreationUnixTime (),
143+ Expires : token .ExpirationUnixTime (),
144+ },
145+ }
146+ _ , err := m .keyvaultClient .SetSecret (m .ctx , vaultUrl , secretName , secretParameters )
147+ if err != nil {
148+ log .Panic (err )
145149 }
146150}
147151
0 commit comments