Skip to content

Commit 02915f4

Browse files
committed
feat(auth): add support for DefaultAzureCredential
Signed-off-by: Gabriele Fedi <gabriele.fedi@enterprisedb.com>
1 parent 316828c commit 02915f4

File tree

7 files changed

+44
-2
lines changed

7 files changed

+44
-2
lines changed

internal/cnpgi/common/common.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@ SPDX-License-Identifier: Apache-2.0
2020
package common
2121

2222
import (
23+
"context"
2324
"fmt"
2425
"path"
2526
"strings"
2627

2728
barmanapi "github.com/cloudnative-pg/barman-cloud/pkg/api"
29+
"github.com/cloudnative-pg/barman-cloud/pkg/command"
2830

31+
barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1"
2932
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/metadata"
33+
pluginmetadata "github.com/cloudnative-pg/plugin-barman-cloud/pkg/metadata"
3034
)
3135

3236
// TODO: refactor.
@@ -97,3 +101,14 @@ func MergeEnv(env []string, incomingEnv []string) []string {
97101
func BuildCertificateFilePath(objectStoreName string) string {
98102
return path.Join(metadata.BarmanCertificatesPath, objectStoreName, metadata.BarmanCertificatesFileName)
99103
}
104+
105+
// ContextWithProviderOptions enriches the context with cloud service provider specific options
106+
// based on the ObjectStore resource
107+
func ContextWithProviderOptions(ctx context.Context, objectStore barmancloudv1.ObjectStore) context.Context {
108+
if objectStore.GetAnnotations()[pluginmetadata.UseDefaultAzureCredentialsAnnotationName] ==
109+
pluginmetadata.UseDefaultAzureCredentialsTrueValue {
110+
return command.ContextWithDefaultAzureCredentials(ctx, true)
111+
}
112+
113+
return ctx
114+
}

internal/cnpgi/common/wal.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ func (w WALServiceImplementation) Archive(
127127
return nil, err
128128
}
129129

130+
ctx = ContextWithProviderOptions(ctx, objectStore)
131+
130132
envArchive, err := barmanCredentials.EnvSetCloudCredentialsAndCertificates(
131133
ctx,
132134
w.Client,

internal/cnpgi/instance/backup.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ func (b BackupServiceImplementation) Backup(
8787
return nil, err
8888
}
8989

90+
ctx = common.ContextWithProviderOptions(ctx, objectStore)
91+
9092
if err := fileutils.EnsureDirectoryExists(postgres.BackupTemporaryDirectory); err != nil {
9193
contextLogger.Error(err, "Cannot create backup temporary directory", "err", err)
9294
return nil, err

internal/cnpgi/instance/retention.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ func (c *CatalogMaintenanceRunnable) cycle(ctx context.Context) (time.Duration,
9393
return 0, err
9494
}
9595

96+
ctx = common.ContextWithProviderOptions(ctx, barmanObjectStore)
97+
9698
if err := c.maintenance(ctx, &cluster, &barmanObjectStore); err != nil {
9799
return 0, err
98100
}

internal/cnpgi/restore/restore.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ func (impl JobHookImpl) Restore(
9898
}
9999

100100
var recoveryObjectStore barmancloudv1.ObjectStore
101-
if err := impl.Client.Get(ctx, configuration.GetRecoveryBarmanObjectKey(), &recoveryObjectStore); err != nil {
101+
if err := impl.Client.Get(ctx,
102+
configuration.GetRecoveryBarmanObjectKey(),
103+
&recoveryObjectStore); err != nil {
102104
return nil, err
103105
}
104106

@@ -109,7 +111,7 @@ func (impl JobHookImpl) Restore(
109111
}
110112

111113
if err := impl.checkBackupDestination(
112-
ctx,
114+
common.ContextWithProviderOptions(ctx, targetObjectStore),
113115
configuration.Cluster,
114116
&targetObjectStore.Spec.Configuration,
115117
targetObjectStore.Name,
@@ -118,6 +120,8 @@ func (impl JobHookImpl) Restore(
118120
}
119121
}
120122

123+
ctx = common.ContextWithProviderOptions(ctx, recoveryObjectStore)
124+
121125
// Detect the backup to recover
122126
backup, env, err := loadBackupObjectFromExternalCluster(
123127
ctx,

pkg/metadata/doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package metadata provides metadata utilities for the Barman Cloud plugin
2+
package metadata

pkg/metadata/labels_annotations.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package metadata
2+
3+
// MetadataNamespace is the namespace used for the Barman Cloud plugin metadata
4+
const MetadataNamespace = "barmancloud.cnpg.io"
5+
6+
const (
7+
// UseDefaultAzureCredentialsAnnotationName is an annotation that can be set
8+
// on an ObjectStore resource to enable the use DefaultAzureCredentials
9+
// to authenticate to Azure. This is meant to be used with inheritFromAzureAD enabled.
10+
UseDefaultAzureCredentialsAnnotationName = MetadataNamespace + "/useDefaultAzureCredentials"
11+
12+
// UseDefaultAzureCredentialsTrueValue is the value for the annotation
13+
// barmancloud.cnpg.io/useDefaultAzureCredentials to enable the use of DefaultAzureCredentials
14+
UseDefaultAzureCredentialsTrueValue = "true"
15+
)

0 commit comments

Comments
 (0)