Skip to content

Commit ff95ef2

Browse files
craig[bot]dt
andcommitted
Merge #158682
158682: cloud/azure: include env vars in client cache check r=dt a=dt The tests of the azure client expect changes in environment variables to be reflected in subsequent uses of the client. However, the client was being cached without considering the env vars -- as they typically do not change during a process' lifetime outside of tests, violating the assumptions of these tests. Now the env var values are factored into the cache key. Release note: none. Epic: none. Co-authored-by: David Taylor <[email protected]>
2 parents 34eb62b + ac66527 commit ff95ef2

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

pkg/cloud/azure/azure_file_credential.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ func NewAzureFileCredential(
6262
return c, nil
6363
}
6464

65+
func credFileEnv() string {
66+
return envutil.EnvOrDefaultString("COCKROACH_AZURE_APPLICATION_CREDENTIALS_FILE", "")
67+
}
68+
6569
// NewDefaultAzureCredentialWithFile creates a credential chain that's simply
6670
// a FileCredential prepended to the chain in DefaultAzureCredential.
6771
func NewDefaultAzureCredentialWithFile(
@@ -74,7 +78,7 @@ func NewDefaultAzureCredentialWithFile(
7478
var credentials []azcore.TokenCredential
7579
var credErrors []string
7680

77-
credentialFileName := envutil.EnvOrDefaultString("COCKROACH_AZURE_APPLICATION_CREDENTIALS_FILE", "")
81+
credentialFileName := credFileEnv()
7882
fileCredentials, err := NewAzureFileCredential(credentialFileName, &FileCredentialOptions{ClientOptions: options.ClientOptions, testingKnobs: options.testingKnobs})
7983
if err != nil {
8084
credErrors = append(credErrors, errors.Wrap(err, "file credential").Error())

pkg/cloud/azure/azure_file_credentials_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,7 @@ func TestAzureFileCredential(t *testing.T) {
211211

212212
t.Run("invalid-on-reload", func(t *testing.T) {
213213
credFile := path.Join(tmpDir, "reload-on-error-invalid-on-reload")
214-
cleanup := envutil.TestSetEnv(t, "COCKROACH_AZURE_APPLICATION_CREDENTIALS_FILE", credFile)
215-
defer cleanup()
214+
defer envutil.TestSetEnv(t, "COCKROACH_AZURE_APPLICATION_CREDENTIALS_FILE", credFile)()
216215

217216
require.NoError(t, writeAzureCredentialsFile(credFile, cfg.tenantID, cfg.clientID, cfg.clientSecret))
218217

pkg/cloud/azure/azure_storage.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
2929
"github.com/cockroachdb/cockroach/pkg/settings"
3030
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
31+
"github.com/cockroachdb/cockroach/pkg/util/envutil"
3132
"github.com/cockroachdb/cockroach/pkg/util/ioctx"
3233
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
3334
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
@@ -222,10 +223,16 @@ type azureStorage struct {
222223
settings *cluster.Settings
223224
}
224225

226+
type azCacheKey struct {
227+
conf cloudpb.ExternalStorage_Azure
228+
envFile string
229+
envID string
230+
}
231+
225232
var azClientCache struct {
226233
syncutil.Mutex
227234
// TODO(dt): make this an >1 item cache e.g. add a FIFO ring.
228-
key cloudpb.ExternalStorage_Azure
235+
key azCacheKey
229236
set time.Time
230237
client *service.Client
231238
}
@@ -274,14 +281,19 @@ func makeAzureStorage(
274281

275282
var azClient *service.Client
276283

277-
clientConf := *conf
278-
clientConf.Prefix = "" // Prefix is not part of the client identity.
284+
clientID, _ := envutil.ExternalEnvString("AZURE_CLIENT_ID", 0)
285+
cacheKey := azCacheKey{
286+
conf: *conf,
287+
envFile: credFileEnv(),
288+
envID: clientID,
289+
}
290+
cacheKey.conf.Prefix = "" // Prefix is not part of the client identity.
279291

280292
if reuseSession.Get(&args.Settings.SV) {
281293
func() {
282294
azClientCache.Lock()
283295
defer azClientCache.Unlock()
284-
if cached := azClientCache.client; cached != nil && azClientCache.key == clientConf && timeutil.Since(azClientCache.set) < 2*time.Minute {
296+
if cached := azClientCache.client; cached != nil && azClientCache.key == cacheKey && timeutil.Since(azClientCache.set) < 2*time.Minute {
285297
azClient = cached
286298
}
287299
}()
@@ -342,7 +354,7 @@ func makeAzureStorage(
342354
if reuseSession.Get(&args.Settings.SV) {
343355
azClientCache.Lock()
344356
defer azClientCache.Unlock()
345-
azClientCache.key = clientConf
357+
azClientCache.key = cacheKey
346358
azClientCache.client = azClient
347359
azClientCache.set = timeutil.Now()
348360
}

0 commit comments

Comments
 (0)