Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Non-public cloud support for Azure Key Vault. #2827

Merged
merged 3 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
- **General:** Introduce new GCP Storage Scaler ([#2628](https://github.com/kedacore/keda/issues/2628))
- **General:** Introduce ARM-based container image for KEDA ([#2263](https://github.com/kedacore/keda/issues/2263)|[#2262](https://github.com/kedacore/keda/issues/2262))
- **General:** Provide support for authentication via Azure Key Vault ([#900](https://github.com/kedacore/keda/issues/900))
- **General:** Provide support for non-public clouds for Azure Key Vault ([#2733](https://github.com/kedacore/keda/issues/2733))

### Improvements

Expand Down
10 changes: 10 additions & 0 deletions apis/keda/v1alpha1/triggerauthentication_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ type AzureKeyVault struct {
VaultURI string `json:"vaultUri"`
Credentials *AzureKeyVaultCredentials `json:"credentials"`
Secrets []AzureKeyVaultSecret `json:"secrets"`
// +optional
Cloud *AzureKeyVaultCloudInfo `json:"cloud"`
}

type AzureKeyVaultCredentials struct {
Expand Down Expand Up @@ -211,6 +213,14 @@ type AzureKeyVaultSecret struct {
Version string `json:"version,omitempty"`
}

type AzureKeyVaultCloudInfo struct {
Type string `json:"type"`
// +optional
KeyVaultResourceURL string `json:"keyVaultResourceURL"`
// +optional
ActiveDirectoryEndpoint string `json:"activeDirectoryEndpoint"`
}

func init() {
SchemeBuilder.Register(&ClusterTriggerAuthentication{}, &ClusterTriggerAuthenticationList{})
SchemeBuilder.Register(&TriggerAuthentication{}, &TriggerAuthenticationList{})
Expand Down
20 changes: 20 additions & 0 deletions apis/keda/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions config/crd/bases/keda.sh_clustertriggerauthentications.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ spec:
description: AzureKeyVault is used to authenticate using Azure Key
Vault
properties:
cloud:
properties:
activeDirectoryEndpoint:
type: string
keyVaultResourceURL:
type: string
type:
type: string
required:
- type
type: object
credentials:
properties:
clientId:
Expand Down
11 changes: 11 additions & 0 deletions config/crd/bases/keda.sh_triggerauthentications.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ spec:
description: AzureKeyVault is used to authenticate using Azure Key
Vault
properties:
cloud:
properties:
activeDirectoryEndpoint:
type: string
keyVaultResourceURL:
type: string
type:
type: string
required:
- type
type: object
credentials:
properties:
clientId:
Expand Down
41 changes: 36 additions & 5 deletions pkg/scaling/resolver/azure_keyvault_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ package resolver

import (
"context"
"fmt"
"strings"

"github.com/Azure/azure-sdk-for-go/services/keyvault/v7.0/keyvault"
az "github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/go-autorest/autorest/azure/auth"
"github.com/go-logr/logr"
"sigs.k8s.io/controller-runtime/pkg/client"

kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
)

const (
azureKeyVaultResource = "https://vault.azure.net"
"github.com/kedacore/keda/v2/pkg/scalers/azure"
)

type AzureKeyVaultHandler struct {
Expand All @@ -51,7 +51,13 @@ func (vh *AzureKeyVaultHandler) Initialize(ctx context.Context, client client.Cl
clientSecret := resolveAuthSecret(ctx, client, logger, clientSecretName, triggerNamespace, clientSecretKey)

clientCredentialsConfig := auth.NewClientCredentialsConfig(clientID, clientSecret, tenantID)
clientCredentialsConfig.Resource = azureKeyVaultResource

keyvaultResourceURL, activeDirectoryEndpoint, err := vh.getPropertiesForCloud()
if err != nil {
return err
}
clientCredentialsConfig.Resource = keyvaultResourceURL
clientCredentialsConfig.AADEndpoint = activeDirectoryEndpoint

authorizer, err := clientCredentialsConfig.Authorizer()
if err != nil {
Expand All @@ -74,3 +80,28 @@ func (vh *AzureKeyVaultHandler) Read(ctx context.Context, secretName string, ver

return *result.Value, nil
}

func (vh *AzureKeyVaultHandler) getPropertiesForCloud() (string, string, error) {
cloud := vh.vault.Cloud

if cloud == nil {
return az.PublicCloud.ResourceIdentifiers.KeyVault, az.PublicCloud.ActiveDirectoryEndpoint, nil
}

if strings.EqualFold(cloud.Type, azure.PrivateCloud) {
if cloud.KeyVaultResourceURL == "" || cloud.ActiveDirectoryEndpoint == "" {
err := fmt.Errorf("properties keyVaultResourceURL and activeDirectoryEndpoint must be provided for cloud %s",
azure.PrivateCloud)
return "", "", err
}

return cloud.KeyVaultResourceURL, cloud.ActiveDirectoryEndpoint, nil
}

env, err := az.EnvironmentFromName(cloud.Type)
if err != nil {
return "", "", err
}

return env.ResourceIdentifiers.KeyVault, env.ActiveDirectoryEndpoint, nil
}
135 changes: 135 additions & 0 deletions pkg/scaling/resolver/azure_keyvault_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
Copyright 2022 The KEDA Authors

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.
*/

package resolver

import (
"testing"

az "github.com/Azure/go-autorest/autorest/azure"

kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
)

const (
testResourceURL = "testResourceURL"
testActiveDirectoryEndpoint = "testActiveDirectoryEndpoint"
)

type testData struct {
name string
isError bool
vault kedav1alpha1.AzureKeyVault
expectedKVResourceURL string
expectedADEndpoint string
}

var testDataset = []testData{
{
name: "known Azure cloud",
isError: false,
vault: kedav1alpha1.AzureKeyVault{
Cloud: &kedav1alpha1.AzureKeyVaultCloudInfo{
Type: "azurePublicCloud",
},
},
expectedKVResourceURL: az.PublicCloud.ResourceIdentifiers.KeyVault,
expectedADEndpoint: az.PublicCloud.ActiveDirectoryEndpoint,
},
{
name: "private cloud",
isError: false,
vault: kedav1alpha1.AzureKeyVault{
Cloud: &kedav1alpha1.AzureKeyVaultCloudInfo{
Type: "private",
KeyVaultResourceURL: testResourceURL,
ActiveDirectoryEndpoint: testActiveDirectoryEndpoint,
},
},
expectedKVResourceURL: testResourceURL,
expectedADEndpoint: testActiveDirectoryEndpoint,
},
{
name: "nil cloud info",
isError: false,
vault: kedav1alpha1.AzureKeyVault{
Cloud: nil,
},
expectedKVResourceURL: az.PublicCloud.ResourceIdentifiers.KeyVault,
expectedADEndpoint: az.PublicCloud.ActiveDirectoryEndpoint,
},
{
name: "invalid cloud",
isError: true,
vault: kedav1alpha1.AzureKeyVault{
Cloud: &kedav1alpha1.AzureKeyVaultCloudInfo{
Type: "invalid cloud",
},
},
expectedKVResourceURL: "",
expectedADEndpoint: "",
},
{
name: "private cloud missing keyvault resource URL",
isError: true,
vault: kedav1alpha1.AzureKeyVault{
Cloud: &kedav1alpha1.AzureKeyVaultCloudInfo{
Type: "private",
ActiveDirectoryEndpoint: testActiveDirectoryEndpoint,
},
},
expectedKVResourceURL: "",
expectedADEndpoint: "",
},
{
name: "private cloud missing active directory endpoint",
isError: true,
vault: kedav1alpha1.AzureKeyVault{
Cloud: &kedav1alpha1.AzureKeyVaultCloudInfo{
Type: "private",
KeyVaultResourceURL: testResourceURL,
},
},
expectedKVResourceURL: "",
expectedADEndpoint: "",
},
}

func TestGetPropertiesForCloud(t *testing.T) {
for _, testData := range testDataset {
vh := NewAzureKeyVaultHandler(&testData.vault)

kvResourceURL, adEndpoint, err := vh.getPropertiesForCloud()

if err != nil && !testData.isError {
t.Fatalf("test %s: expected success but got error - %s", testData.name, err)
}

if err == nil && testData.isError {
t.Fatalf("test %s: expected error but got success, testData - %+v", testData.name, testData)
}

if kvResourceURL != testData.expectedKVResourceURL {
t.Errorf("test %s: keyvault resource URl does not match. expected - %s, got - %s",
testData.name, testData.expectedKVResourceURL, kvResourceURL)
}

if adEndpoint != testData.expectedADEndpoint {
t.Errorf("test %s: active directory endpoint does not match. expected - %s, got - %s",
testData.name, testData.expectedADEndpoint, adEndpoint)
}
}
}