Skip to content

Commit 1a6b98d

Browse files
litaocdlarmru
andauthored
feat: ability to defaultAzureCredential for azure-blob-storage (#64)
This patch adds support for Azure AD-based authentication using the DefaultAzureCredential mechanism. Closes #59 Signed-off-by: Tao Li <tao.li@enterprisedb.com> Signed-off-by: Armando Ruocco <armando.ruocco@enterprisedb.com> Co-authored-by: Armando Ruocco <armando.ruocco@enterprisedb.com>
1 parent 018944b commit 1a6b98d

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

pkg/command/commandbuilder.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ func appendCloudProviderOptions(
111111
"--cloud-provider",
112112
"azure-blob-storage")
113113

114+
if useDefaultAzureCredentials(ctx) {
115+
break
116+
}
117+
114118
if !credentials.Azure.InheritFromAzureAD {
115119
break
116120
}
@@ -143,3 +147,26 @@ func appendCloudProviderOptions(
143147

144148
return options, nil
145149
}
150+
151+
type contextKey string
152+
153+
// contextKeyUseDefaultAzureCredentials contains a bool indicating if the default azure credentials should be used
154+
const contextKeyUseDefaultAzureCredentials contextKey = "useDefaultAzureCredentials"
155+
156+
func useDefaultAzureCredentials(ctx context.Context) bool {
157+
v := ctx.Value(contextKeyUseDefaultAzureCredentials)
158+
if v == nil {
159+
return false
160+
}
161+
result, ok := v.(bool)
162+
if !ok {
163+
return false
164+
}
165+
return result
166+
}
167+
168+
// ContextWithDefaultAzureCredentials create a context that contains the contextKeyUseDefaultAzureCredentials flag.
169+
// When set to true barman-cloud will use the default Azure credentials.
170+
func ContextWithDefaultAzureCredentials(ctx context.Context, enabled bool) context.Context {
171+
return context.WithValue(ctx, contextKeyUseDefaultAzureCredentials, enabled)
172+
}

pkg/command/commandbuilder_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package command
1818

1919
import (
20+
"context"
2021
"strings"
2122

2223
barmanApi "github.com/cloudnative-pg/barman-cloud/pkg/api"
@@ -57,3 +58,24 @@ var _ = Describe("barmanCloudWalRestoreOptions", func() {
5758
))
5859
})
5960
})
61+
62+
var _ = Describe("useDefaultAzureCredentials", func() {
63+
It("should be false by default", func(ctx SpecContext) {
64+
Expect(useDefaultAzureCredentials(ctx)).To(BeFalse())
65+
})
66+
67+
It("should be false if ctx contains an invalid value", func(ctx SpecContext) {
68+
newCtx := context.WithValue(ctx, contextKeyUseDefaultAzureCredentials, "invalidValue")
69+
Expect(useDefaultAzureCredentials(newCtx)).To(BeFalse())
70+
})
71+
72+
It("should be false if ctx contains false value", func(ctx SpecContext) {
73+
newCtx := context.WithValue(ctx, contextKeyUseDefaultAzureCredentials, false)
74+
Expect(useDefaultAzureCredentials(newCtx)).To(BeFalse())
75+
})
76+
77+
It("should be true only if ctx contains true value", func(ctx SpecContext) {
78+
newCtx := context.WithValue(ctx, contextKeyUseDefaultAzureCredentials, true)
79+
Expect(useDefaultAzureCredentials(newCtx)).To(BeTrue())
80+
})
81+
})

0 commit comments

Comments
 (0)