Skip to content

Commit 42684f9

Browse files
committed
implement proper azure keyvault error handling
Signed-off-by: Markus Blaschke <mblaschke82@gmail.com>
1 parent 1e6081e commit 42684f9

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

cloudprovider/azure.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,18 @@ func (m *CloudProviderAzure) FetchToken() (token *bootstraptoken.BootstrapToken)
7676

7777
log.Infof("fetching newest token from Azure KeyVault \"%s\" secret \"%s\"", vaultName, secretName)
7878
secret, err := m.keyvaultClient.GetSecret(m.ctx, vaultUrl, secretName, "")
79-
// ignore if not found as "non error"
80-
if !secret.IsHTTPStatus(404) && err != nil {
79+
switch m.getInnerErrorCodeFromAutorestError(err) {
80+
case "SecretDisabled":
81+
// disabled secret, continue as there would be no token
82+
log.Warn("current secret is disabled, assuming non existing token")
83+
err = nil
84+
break;
85+
case "ForbiddenByPolicy":
86+
// access is forbidden
87+
log.Error("unable to access Azure KeyVault, please check access")
88+
log.Panic(err)
89+
default:
90+
// not handled error
8191
log.Panic(err)
8292
}
8393

@@ -95,13 +105,6 @@ func (m *CloudProviderAzure) FetchToken() (token *bootstraptoken.BootstrapToken)
95105
}
96106
}
97107

98-
if token != nil {
99-
contextLogger := log.WithFields(log.Fields{"token": token.Id()})
100-
contextLogger.Infof("found cloud token with id \"%s\" and expiration %s", token.Id(), token.ExpirationString())
101-
} else {
102-
log.Infof("no cloud token found")
103-
}
104-
105108
return
106109
}
107110

@@ -136,3 +139,16 @@ func (m *CloudProviderAzure) StoreToken(token *bootstraptoken.BootstrapToken) {
136139
}
137140
}
138141
}
142+
143+
func (m *CloudProviderAzure) getInnerErrorCodeFromAutorestError(err error) (code interface{}) {
144+
if autorestError, ok := err.(autorest.DetailedError); ok {
145+
if azureRequestError, ok := autorestError.Original.(*azure.RequestError); ok {
146+
if azureRequestError.ServiceError != nil {
147+
if errorCode, exists := azureRequestError.ServiceError.InnerError["code"]; exists {
148+
code = errorCode
149+
}
150+
}
151+
}
152+
}
153+
return
154+
}

manager/manager.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ func (m *KubeBootstrapTokenManager) Start() {
158158
func (m *KubeBootstrapTokenManager) syncRun() error {
159159
if token := m.cloudProvider.FetchToken(); token != nil {
160160
contextLogger := log.WithFields(log.Fields{"token": token.Id()})
161+
contextLogger.Infof("found cloud token with id \"%s\" and expiration %s", token.Id(), token.ExpirationString())
161162
if m.checkTokenRenewal(token) {
162163
contextLogger.Infof("token is not valid or going to expire, starting renewal of token")
163164
if err := m.createNewToken(); err != nil {

0 commit comments

Comments
 (0)