Skip to content

Commit 8e3fa9b

Browse files
committed
azure: Cache storage client by both account name and tenant ID
We ran into this when a customer changed their storage account name. Fragment listings for their journal were using the wrong storage client, and returning errors complaining about resolving the hostname of the previous storage account, even through the fragment spec correctly specified new storage account name.
1 parent dc2b9ab commit 8e3fa9b

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

broker/fragment/store_azure.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ func parseAzureEndpoint(endpoint *url.URL) (cfg azureStoreConfig, err error) {
274274
return cfg, nil
275275
}
276276

277-
func (a *azureBackend) getAzureServiceClient(endpoint *url.URL) (client *service.Client, err error) {
277+
func (a *azureBackend) getAzureStorageClient(endpoint *url.URL) (client *service.Client, err error) {
278278
var cfg azureStoreConfig
279279

280280
if cfg, err = parseAzureEndpoint(endpoint); err != nil {
@@ -286,16 +286,22 @@ func (a *azureBackend) getAzureServiceClient(endpoint *url.URL) (client *service
286286
var accountKey = os.Getenv("AZURE_ACCOUNT_KEY")
287287

288288
a.mu.Lock()
289-
client, ok := a.clients[accountName]
289+
client, ok := a.clients[accountName+cfg.storageAccountName]
290290
a.mu.Unlock()
291291

292292
if ok {
293293
log.WithFields(log.Fields{
294294
"storageAccountName": accountName,
295-
}).Info("Re-using cached azure:// service client")
295+
"containerName": cfg.containerName,
296+
}).Info("Re-using cached azure:// storage account client")
296297
return client, nil
297298
}
298299

300+
log.WithFields(log.Fields{
301+
"storageAccountName": accountName,
302+
"containerName": cfg.containerName,
303+
}).Info("Building new azure:// storage account client")
304+
299305
sharedKeyCred, err := service.NewSharedKeyCredential(accountName, accountKey)
300306
if err != nil {
301307
return nil, err
@@ -306,7 +312,7 @@ func (a *azureBackend) getAzureServiceClient(endpoint *url.URL) (client *service
306312
}
307313

308314
a.mu.Lock()
309-
a.clients[accountName] = serviceClient
315+
a.clients[accountName+cfg.storageAccountName] = serviceClient
310316
a.mu.Unlock()
311317
return serviceClient, nil
312318
} else if endpoint.Scheme == "azure-ad" {
@@ -316,16 +322,24 @@ func (a *azureBackend) getAzureServiceClient(endpoint *url.URL) (client *service
316322
var clientSecret = os.Getenv("AZURE_CLIENT_SECRET")
317323

318324
a.mu.Lock()
319-
client, ok := a.clients[cfg.accountTenantID]
325+
client, ok := a.clients[cfg.accountTenantID+cfg.storageAccountName]
320326
a.mu.Unlock()
321327

322328
if ok {
323329
log.WithFields(log.Fields{
324-
"accountTenantId": cfg.accountTenantID,
325-
}).Info("Re-using cached azure-ad:// service client")
330+
"accountTenantId": cfg.accountTenantID,
331+
"storageAccountName": cfg.storageAccountName,
332+
"containerName": cfg.containerName,
333+
}).Info("Re-using cached azure-ad:// storage account client")
326334
return client, nil
327335
}
328336

337+
log.WithFields(log.Fields{
338+
"accountTenantId": cfg.accountTenantID,
339+
"storageAccountName": cfg.storageAccountName,
340+
"containerName": cfg.containerName,
341+
}).Info("Building new azure-ad:// storage account client")
342+
329343
identityCreds, err := azidentity.NewClientSecretCredential(
330344
cfg.accountTenantID,
331345
clientId,
@@ -345,7 +359,7 @@ func (a *azureBackend) getAzureServiceClient(endpoint *url.URL) (client *service
345359
}
346360

347361
a.mu.Lock()
348-
a.clients[cfg.accountTenantID] = serviceClient
362+
a.clients[cfg.accountTenantID+cfg.storageAccountName] = serviceClient
349363
a.mu.Unlock()
350364

351365
return serviceClient, nil
@@ -455,12 +469,12 @@ func (a *azureBackend) getUserDelegationCredential(endpoint *url.URL) (*service.
455469
Expiry: to.Ptr(expTime.UTC().Format(sas.TimeFormat)),
456470
}
457471

458-
var serviceClient, err = a.getAzureServiceClient(endpoint)
472+
var storageClient, err = a.getAzureStorageClient(endpoint)
459473
if err != nil {
460474
return nil, err
461475
}
462476

463-
cred, err := serviceClient.GetUserDelegationCredential(context.Background(), info, nil)
477+
cred, err := storageClient.GetUserDelegationCredential(context.Background(), info, nil)
464478
if err != nil {
465479
return nil, err
466480
}

0 commit comments

Comments
 (0)