generated from cloudnative-pg/cnpg-template
-
Notifications
You must be signed in to change notification settings - Fork 45
feat: add support for DefaultAzureCredential authentication mechanism #681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
armru
wants to merge
3
commits into
main
Choose a base branch
from
dev/default-azure-cred
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,227 @@ | ||
| /* | ||
| Copyright © contributors to CloudNativePG, established as | ||
| CloudNativePG a Series of LF Projects, LLC. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package specs | ||
|
|
||
| import ( | ||
| barmanapi "github.com/cloudnative-pg/barman-cloud/pkg/api" | ||
| machineryapi "github.com/cloudnative-pg/machinery/pkg/api" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| ) | ||
|
|
||
| var _ = Describe("CollectSecretNamesFromCredentials", func() { | ||
| Context("when collecting secrets from AWS credentials", func() { | ||
| It("should return secret names from S3 credentials", func() { | ||
| credentials := &barmanapi.BarmanCredentials{ | ||
| AWS: &barmanapi.S3Credentials{ | ||
| AccessKeyIDReference: &machineryapi.SecretKeySelector{ | ||
| LocalObjectReference: machineryapi.LocalObjectReference{ | ||
| Name: "aws-secret", | ||
| }, | ||
| Key: "access-key-id", | ||
| }, | ||
| SecretAccessKeyReference: &machineryapi.SecretKeySelector{ | ||
| LocalObjectReference: machineryapi.LocalObjectReference{ | ||
| Name: "aws-secret", | ||
| }, | ||
| Key: "secret-access-key", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| secrets := CollectSecretNamesFromCredentials(credentials) | ||
| Expect(secrets).To(ContainElement("aws-secret")) | ||
| }) | ||
|
|
||
| It("should handle nil AWS credentials", func() { | ||
| credentials := &barmanapi.BarmanCredentials{} | ||
|
|
||
| secrets := CollectSecretNamesFromCredentials(credentials) | ||
| Expect(secrets).To(BeEmpty()) | ||
| }) | ||
| }) | ||
|
|
||
| Context("when collecting secrets from Azure credentials", func() { | ||
| It("should return secret names when using explicit credentials", func() { | ||
| credentials := &barmanapi.BarmanCredentials{ | ||
| Azure: &barmanapi.AzureCredentials{ | ||
| ConnectionString: &machineryapi.SecretKeySelector{ | ||
| LocalObjectReference: machineryapi.LocalObjectReference{ | ||
| Name: "azure-secret", | ||
| }, | ||
| Key: "connection-string", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| secrets := CollectSecretNamesFromCredentials(credentials) | ||
| Expect(secrets).To(ContainElement("azure-secret")) | ||
| }) | ||
|
|
||
| It("should return empty list when using UseDefaultAzureCredentials", func() { | ||
| credentials := &barmanapi.BarmanCredentials{ | ||
| Azure: &barmanapi.AzureCredentials{ | ||
| UseDefaultAzureCredentials: true, | ||
| ConnectionString: &machineryapi.SecretKeySelector{ | ||
| LocalObjectReference: machineryapi.LocalObjectReference{ | ||
| Name: "azure-secret", | ||
| }, | ||
| Key: "connection-string", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| secrets := CollectSecretNamesFromCredentials(credentials) | ||
| Expect(secrets).To(BeEmpty()) | ||
| }) | ||
|
|
||
| It("should return empty list when using InheritFromAzureAD", func() { | ||
| credentials := &barmanapi.BarmanCredentials{ | ||
| Azure: &barmanapi.AzureCredentials{ | ||
| InheritFromAzureAD: true, | ||
| }, | ||
| } | ||
|
|
||
| secrets := CollectSecretNamesFromCredentials(credentials) | ||
| Expect(secrets).To(BeEmpty()) | ||
| }) | ||
|
|
||
| It("should return secret names for storage account and key", func() { | ||
| credentials := &barmanapi.BarmanCredentials{ | ||
| Azure: &barmanapi.AzureCredentials{ | ||
| StorageAccount: &machineryapi.SecretKeySelector{ | ||
| LocalObjectReference: machineryapi.LocalObjectReference{ | ||
| Name: "azure-storage", | ||
| }, | ||
| Key: "account-name", | ||
| }, | ||
| StorageKey: &machineryapi.SecretKeySelector{ | ||
| LocalObjectReference: machineryapi.LocalObjectReference{ | ||
| Name: "azure-storage", | ||
| }, | ||
| Key: "account-key", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| secrets := CollectSecretNamesFromCredentials(credentials) | ||
| Expect(secrets).To(ContainElement("azure-storage")) | ||
| }) | ||
| }) | ||
|
|
||
| Context("when collecting secrets from Google credentials", func() { | ||
| It("should return secret names from Google credentials", func() { | ||
| credentials := &barmanapi.BarmanCredentials{ | ||
| Google: &barmanapi.GoogleCredentials{ | ||
| ApplicationCredentials: &machineryapi.SecretKeySelector{ | ||
| LocalObjectReference: machineryapi.LocalObjectReference{ | ||
| Name: "google-secret", | ||
| }, | ||
| Key: "credentials.json", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| secrets := CollectSecretNamesFromCredentials(credentials) | ||
| Expect(secrets).To(ContainElement("google-secret")) | ||
| }) | ||
| }) | ||
|
|
||
| Context("when collecting secrets from multiple cloud providers", func() { | ||
| It("should return secret names from all providers", func() { | ||
| credentials := &barmanapi.BarmanCredentials{ | ||
| AWS: &barmanapi.S3Credentials{ | ||
| AccessKeyIDReference: &machineryapi.SecretKeySelector{ | ||
| LocalObjectReference: machineryapi.LocalObjectReference{ | ||
| Name: "aws-secret", | ||
| }, | ||
| Key: "access-key-id", | ||
| }, | ||
| }, | ||
| Azure: &barmanapi.AzureCredentials{ | ||
| ConnectionString: &machineryapi.SecretKeySelector{ | ||
| LocalObjectReference: machineryapi.LocalObjectReference{ | ||
| Name: "azure-secret", | ||
| }, | ||
| Key: "connection-string", | ||
| }, | ||
| }, | ||
| Google: &barmanapi.GoogleCredentials{ | ||
| ApplicationCredentials: &machineryapi.SecretKeySelector{ | ||
| LocalObjectReference: machineryapi.LocalObjectReference{ | ||
| Name: "google-secret", | ||
| }, | ||
| Key: "credentials.json", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| secrets := CollectSecretNamesFromCredentials(credentials) | ||
| Expect(secrets).To(ContainElements("aws-secret", "azure-secret", "google-secret")) | ||
| }) | ||
|
|
||
| It("should skip Azure secrets when using UseDefaultAzureCredentials with other providers", func() { | ||
| credentials := &barmanapi.BarmanCredentials{ | ||
| AWS: &barmanapi.S3Credentials{ | ||
| AccessKeyIDReference: &machineryapi.SecretKeySelector{ | ||
| LocalObjectReference: machineryapi.LocalObjectReference{ | ||
| Name: "aws-secret", | ||
| }, | ||
| Key: "access-key-id", | ||
| }, | ||
| }, | ||
| Azure: &barmanapi.AzureCredentials{ | ||
| UseDefaultAzureCredentials: true, | ||
| ConnectionString: &machineryapi.SecretKeySelector{ | ||
| LocalObjectReference: machineryapi.LocalObjectReference{ | ||
| Name: "azure-secret", | ||
| }, | ||
| Key: "connection-string", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| secrets := CollectSecretNamesFromCredentials(credentials) | ||
| Expect(secrets).To(ContainElement("aws-secret")) | ||
| Expect(secrets).NotTo(ContainElement("azure-secret")) | ||
| }) | ||
| }) | ||
|
|
||
| Context("when handling nil references", func() { | ||
| It("should skip nil secret references", func() { | ||
| credentials := &barmanapi.BarmanCredentials{ | ||
| AWS: &barmanapi.S3Credentials{ | ||
| AccessKeyIDReference: &machineryapi.SecretKeySelector{ | ||
| LocalObjectReference: machineryapi.LocalObjectReference{ | ||
| Name: "aws-secret", | ||
| }, | ||
| Key: "access-key-id", | ||
| }, | ||
| SecretAccessKeyReference: nil, | ||
| }, | ||
| } | ||
|
|
||
| secrets := CollectSecretNamesFromCredentials(credentials) | ||
| Expect(secrets).To(ContainElement("aws-secret")) | ||
| Expect(len(secrets)).To(Equal(1)) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| Copyright © contributors to CloudNativePG, established as | ||
| CloudNativePG a Series of LF Projects, LLC. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package specs | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| ) | ||
|
|
||
| func TestSpecs(t *testing.T) { | ||
| RegisterFailHandler(Fail) | ||
| RunSpecs(t, "Specs Suite") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -252,6 +252,33 @@ spec: | |
| [...] | ||
| ``` | ||
|
|
||
| ### Default Azure Credentials | ||
|
|
||
| The `useDefaultAzureCredentials` option enables the default Azure credentials | ||
| flow, which uses [`DefaultAzureCredential`](https://learn.microsoft.com/en-us/python/api/azure-identity/azure.identity.defaultazurecredential) | ||
| to automatically discover and use available credentials in the following order: | ||
|
|
||
| 1. **Environment Variables** — `AZURE_CLIENT_ID`, `AZURE_CLIENT_SECRET`, and `AZURE_TENANT_ID` for Service Principal authentication | ||
| 2. **Managed Identity** — Uses the managed identity assigned to the pod | ||
| 3. **Azure CLI** — Uses credentials from the Azure CLI if available | ||
| 4. **Azure PowerShell** — Uses credentials from Azure PowerShell if available | ||
|
|
||
| This is particularly useful when running on Azure Kubernetes Service (AKS) with | ||
| [Workload Identity](https://learn.microsoft.com/en-us/azure/aks/workload-identity-overview): | ||
|
Comment on lines
+266
to
+267
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if this assessment is totally precise. I think Azure Default Credentials are primarily useful to delegate to the SDK the auth mechanism inferring it from the environment, so that different auth methods can be used in different environments seamlessly. |
||
|
|
||
| ```yaml | ||
| apiVersion: barmancloud.cnpg.io/v1 | ||
| kind: ObjectStore | ||
| metadata: | ||
| name: azure-store | ||
| spec: | ||
| configuration: | ||
| destinationPath: "<destination path here>" | ||
| azureCredentials: | ||
| useDefaultAzureCredentials: true | ||
| [...] | ||
| ``` | ||
|
|
||
| ### Access Key, SAS Token, or Connection String | ||
|
|
||
| Store credentials in a Kubernetes secret: | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we include also
barmanCredentials.Azure.InheritFromAzureADhere?