Skip to content
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

Add support to export eventhub keys #3882

Merged
merged 11 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
14 changes: 7 additions & 7 deletions docs/hugo/content/reference/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,13 @@ To install the CRDs for these resources, your ASO configuration must include `ev

These resource(s) are available for use in the current release of ASO. Different versions of a given resource reflect different versions of the Azure ARM API.

| Resource | ARM Version | CRD Version | Supported From | Sample |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------|---------------|----------------|-----------------------------------------------------------------------------------------------------------------------------------------------------|
| [Namespace](https://azure.github.io/azure-service-operator/reference/eventhub/v1api20211101/#eventhub.azure.com/v1api20211101.Namespace) | 2021-11-01 | v1api20211101 | v2.0.0 | [View](https://github.com/Azure/azure-service-operator/tree/main/v2/samples/eventhub/v1api/v1api20211101_namespace.yaml) |
| [NamespacesAuthorizationRule](https://azure.github.io/azure-service-operator/reference/eventhub/v1api20211101/#eventhub.azure.com/v1api20211101.NamespacesAuthorizationRule) | 2021-11-01 | v1api20211101 | v2.0.0 | [View](https://github.com/Azure/azure-service-operator/tree/main/v2/samples/eventhub/v1api/v1api20211101_namespacesauthorizationrule.yaml) |
| [NamespacesEventhub](https://azure.github.io/azure-service-operator/reference/eventhub/v1api20211101/#eventhub.azure.com/v1api20211101.NamespacesEventhub) | 2021-11-01 | v1api20211101 | v2.0.0 | [View](https://github.com/Azure/azure-service-operator/tree/main/v2/samples/eventhub/v1api/v1api20211101_namespaceseventhub.yaml) |
| [NamespacesEventhubsAuthorizationRule](https://azure.github.io/azure-service-operator/reference/eventhub/v1api20211101/#eventhub.azure.com/v1api20211101.NamespacesEventhubsAuthorizationRule) | 2021-11-01 | v1api20211101 | v2.0.0 | [View](https://github.com/Azure/azure-service-operator/tree/main/v2/samples/eventhub/v1api/v1api20211101_namespaceseventhubsauthorizationrule.yaml) |
| [NamespacesEventhubsConsumerGroup](https://azure.github.io/azure-service-operator/reference/eventhub/v1api20211101/#eventhub.azure.com/v1api20211101.NamespacesEventhubsConsumerGroup) | 2021-11-01 | v1api20211101 | v2.0.0 | [View](https://github.com/Azure/azure-service-operator/tree/main/v2/samples/eventhub/v1api/v1api20211101_namespaceseventhubsconsumergroup.yaml) |
| Resource | ARM Version | CRD Version | Supported From | Sample |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------|---------------|----------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Namespace](https://azure.github.io/azure-service-operator/reference/eventhub/v1api20211101/#eventhub.azure.com/v1api20211101.Namespace) | 2021-11-01 | v1api20211101 | v2.0.0 | [View](https://github.com/Azure/azure-service-operator/tree/main/v2/samples/eventhub/v1api20211101/v1api20211101_namespace.yaml) |
| [NamespacesAuthorizationRule](https://azure.github.io/azure-service-operator/reference/eventhub/v1api20211101/#eventhub.azure.com/v1api20211101.NamespacesAuthorizationRule) | 2021-11-01 | v1api20211101 | v2.0.0 | [View](https://github.com/Azure/azure-service-operator/tree/main/v2/samples/eventhub/v1api20211101/v1api20211101_namespacesauthorizationrule.yaml) |
| [NamespacesEventhub](https://azure.github.io/azure-service-operator/reference/eventhub/v1api20211101/#eventhub.azure.com/v1api20211101.NamespacesEventhub) | 2021-11-01 | v1api20211101 | v2.0.0 | [View](https://github.com/Azure/azure-service-operator/tree/main/v2/samples/eventhub/v1api20211101/v1api20211101_namespaceseventhub.yaml) |
| [NamespacesEventhubsAuthorizationRule](https://azure.github.io/azure-service-operator/reference/eventhub/v1api20211101/#eventhub.azure.com/v1api20211101.NamespacesEventhubsAuthorizationRule) | 2021-11-01 | v1api20211101 | v2.0.0 | [View](https://github.com/Azure/azure-service-operator/tree/main/v2/samples/eventhub/v1api20211101/v1api20211101_namespaceseventhubsauthorizationrule.yaml) |
| [NamespacesEventhubsConsumerGroup](https://azure.github.io/azure-service-operator/reference/eventhub/v1api20211101/#eventhub.azure.com/v1api20211101.NamespacesEventhubsConsumerGroup) | 2021-11-01 | v1api20211101 | v2.0.0 | [View](https://github.com/Azure/azure-service-operator/tree/main/v2/samples/eventhub/v1api20211101/v1api20211101_namespaceseventhubsconsumergroup.yaml) |

## Insights

Expand Down
14 changes: 7 additions & 7 deletions docs/hugo/content/reference/eventhub/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ To install the CRDs for these resources, your ASO configuration must include `ev

These resource(s) are available for use in the current release of ASO. Different versions of a given resource reflect different versions of the Azure ARM API.

| Resource | ARM Version | CRD Version | Supported From | Sample |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------|---------------|----------------|-----------------------------------------------------------------------------------------------------------------------------------------------------|
| [Namespace](https://azure.github.io/azure-service-operator/reference/eventhub/v1api20211101/#eventhub.azure.com/v1api20211101.Namespace) | 2021-11-01 | v1api20211101 | v2.0.0 | [View](https://github.com/Azure/azure-service-operator/tree/main/v2/samples/eventhub/v1api/v1api20211101_namespace.yaml) |
| [NamespacesAuthorizationRule](https://azure.github.io/azure-service-operator/reference/eventhub/v1api20211101/#eventhub.azure.com/v1api20211101.NamespacesAuthorizationRule) | 2021-11-01 | v1api20211101 | v2.0.0 | [View](https://github.com/Azure/azure-service-operator/tree/main/v2/samples/eventhub/v1api/v1api20211101_namespacesauthorizationrule.yaml) |
| [NamespacesEventhub](https://azure.github.io/azure-service-operator/reference/eventhub/v1api20211101/#eventhub.azure.com/v1api20211101.NamespacesEventhub) | 2021-11-01 | v1api20211101 | v2.0.0 | [View](https://github.com/Azure/azure-service-operator/tree/main/v2/samples/eventhub/v1api/v1api20211101_namespaceseventhub.yaml) |
| [NamespacesEventhubsAuthorizationRule](https://azure.github.io/azure-service-operator/reference/eventhub/v1api20211101/#eventhub.azure.com/v1api20211101.NamespacesEventhubsAuthorizationRule) | 2021-11-01 | v1api20211101 | v2.0.0 | [View](https://github.com/Azure/azure-service-operator/tree/main/v2/samples/eventhub/v1api/v1api20211101_namespaceseventhubsauthorizationrule.yaml) |
| [NamespacesEventhubsConsumerGroup](https://azure.github.io/azure-service-operator/reference/eventhub/v1api20211101/#eventhub.azure.com/v1api20211101.NamespacesEventhubsConsumerGroup) | 2021-11-01 | v1api20211101 | v2.0.0 | [View](https://github.com/Azure/azure-service-operator/tree/main/v2/samples/eventhub/v1api/v1api20211101_namespaceseventhubsconsumergroup.yaml) |
| Resource | ARM Version | CRD Version | Supported From | Sample |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------|---------------|----------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Namespace](https://azure.github.io/azure-service-operator/reference/eventhub/v1api20211101/#eventhub.azure.com/v1api20211101.Namespace) | 2021-11-01 | v1api20211101 | v2.0.0 | [View](https://github.com/Azure/azure-service-operator/tree/main/v2/samples/eventhub/v1api20211101/v1api20211101_namespace.yaml) |
| [NamespacesAuthorizationRule](https://azure.github.io/azure-service-operator/reference/eventhub/v1api20211101/#eventhub.azure.com/v1api20211101.NamespacesAuthorizationRule) | 2021-11-01 | v1api20211101 | v2.0.0 | [View](https://github.com/Azure/azure-service-operator/tree/main/v2/samples/eventhub/v1api20211101/v1api20211101_namespacesauthorizationrule.yaml) |
| [NamespacesEventhub](https://azure.github.io/azure-service-operator/reference/eventhub/v1api20211101/#eventhub.azure.com/v1api20211101.NamespacesEventhub) | 2021-11-01 | v1api20211101 | v2.0.0 | [View](https://github.com/Azure/azure-service-operator/tree/main/v2/samples/eventhub/v1api20211101/v1api20211101_namespaceseventhub.yaml) |
| [NamespacesEventhubsAuthorizationRule](https://azure.github.io/azure-service-operator/reference/eventhub/v1api20211101/#eventhub.azure.com/v1api20211101.NamespacesEventhubsAuthorizationRule) | 2021-11-01 | v1api20211101 | v2.0.0 | [View](https://github.com/Azure/azure-service-operator/tree/main/v2/samples/eventhub/v1api20211101/v1api20211101_namespaceseventhubsauthorizationrule.yaml) |
| [NamespacesEventhubsConsumerGroup](https://azure.github.io/azure-service-operator/reference/eventhub/v1api20211101/#eventhub.azure.com/v1api20211101.NamespacesEventhubsConsumerGroup) | 2021-11-01 | v1api20211101 | v2.0.0 | [View](https://github.com/Azure/azure-service-operator/tree/main/v2/samples/eventhub/v1api20211101/v1api20211101_namespaceseventhubsconsumergroup.yaml) |

115 changes: 115 additions & 0 deletions v2/api/eventhub/customizations/namespace_extension.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/

package customizations

import (
"context"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/eventhub/armeventhub"
"github.com/go-logr/logr"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/conversion"

"github.com/Azure/azure-service-operator/v2/api/eventhub/v1api20211101/storage"
"github.com/Azure/azure-service-operator/v2/internal/genericarmclient"
. "github.com/Azure/azure-service-operator/v2/internal/logging"
"github.com/Azure/azure-service-operator/v2/pkg/genruntime"
"github.com/Azure/azure-service-operator/v2/pkg/genruntime/secrets"
)

var _ genruntime.KubernetesExporter = &NamespaceExtension{}

func (ext *NamespaceExtension) ExportKubernetesResources(
ctx context.Context,
obj genruntime.MetaObject,
armClient *genericarmclient.GenericClient,
log logr.Logger,
) ([]client.Object, error) {
// This has to be the current hub storage version. It will need to be updated
// if the hub storage version changes.
typedObj, ok := obj.(*storage.Namespace)
if !ok {
return nil, errors.Errorf("cannot run on unknown resource type %T, expected *eventhub.Namespace", obj)
}

// Type assert that we are the hub type. This will fail to compile if
// the hub type has been changed but this extension has not
var _ conversion.Hub = typedObj

hasSecrets := namespaceSecretsSpecified(typedObj)
if !hasSecrets {
log.V(Debug).Info("No secrets retrieval to perform as operatorSpec is empty")
return nil, nil
}

id, err := genruntime.GetAndParseResourceID(typedObj)
if err != nil {
return nil, err
}

// Only bother calling ListKeys if there are secrets to retrieve
var res armeventhub.NamespacesClientListKeysResponse
if hasSecrets {
subscription := id.SubscriptionID
// Using armClient.ClientOptions() here ensures we share the same HTTP connection, so this is not opening a new
// connection each time through
var confClient *armeventhub.NamespacesClient
confClient, err = armeventhub.NewNamespacesClient(subscription, armClient.Creds(), armClient.ClientOptions())
if err != nil {
return nil, errors.Wrapf(err, "failed to create new NamespaceClient")
}

// RootManageSharedAccessKey is the default auth rule for namespace.
// See https://learn.microsoft.com/en-us/azure/event-hubs/event-hubs-get-connection-string
res, err = confClient.ListKeys(ctx, id.ResourceGroupName, typedObj.AzureName(), "RootManageSharedAccessKey", nil)
if err != nil {
return nil, errors.Wrapf(err, "failed to retreive response")
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: unneeded newline

}

secretSlice, err := namespaceSecretsToWrite(typedObj, res.AccessKeys)
if err != nil {
return nil, err
}

return secrets.SliceToClientObjectSlice(secretSlice), nil
}

func namespaceSecretsSpecified(obj *storage.Namespace) bool {
if obj.Spec.OperatorSpec == nil || obj.Spec.OperatorSpec.Secrets == nil {
return false
}

secrets := obj.Spec.OperatorSpec.Secrets

if secrets.PrimaryKey != nil ||
secrets.SecondaryKey != nil ||
secrets.PrimaryConnectionString != nil ||
secrets.SecondaryConnectionString != nil {
return true
}

return false
}

func namespaceSecretsToWrite(obj *storage.Namespace, keys armeventhub.AccessKeys) ([]*v1.Secret, error) {
operatorSpecSecrets := obj.Spec.OperatorSpec.Secrets
if operatorSpecSecrets == nil {
return nil, errors.Errorf("unexpected nil operatorspec")
}

collector := secrets.NewCollector(obj.Namespace)

collector.AddValue(operatorSpecSecrets.PrimaryKey, *keys.PrimaryKey)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use to.Value rather than direct deref here to hedge against panics.

Suggested change
collector.AddValue(operatorSpecSecrets.PrimaryKey, *keys.PrimaryKey)
collector.AddValue(operatorSpecSecrets.PrimaryKey, to.Value(keys.PrimaryKey))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same below

collector.AddValue(operatorSpecSecrets.SecondaryKey, *keys.SecondaryKey)
collector.AddValue(operatorSpecSecrets.PrimaryConnectionString, *keys.PrimaryConnectionString)
collector.AddValue(operatorSpecSecrets.SecondaryConnectionString, *keys.SecondaryConnectionString)

return collector.Values()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/

package customizations

import (
"context"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/eventhub/armeventhub"
"github.com/go-logr/logr"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/conversion"

"github.com/Azure/azure-service-operator/v2/api/eventhub/v1api20211101/storage"
"github.com/Azure/azure-service-operator/v2/internal/genericarmclient"
. "github.com/Azure/azure-service-operator/v2/internal/logging"
"github.com/Azure/azure-service-operator/v2/pkg/genruntime"
"github.com/Azure/azure-service-operator/v2/pkg/genruntime/secrets"
)

var _ genruntime.KubernetesExporter = &NamespacesAuthorizationRuleExtension{}

func (ext *NamespacesAuthorizationRuleExtension) ExportKubernetesResources(
ctx context.Context,
obj genruntime.MetaObject,
armClient *genericarmclient.GenericClient,
log logr.Logger,
) ([]client.Object, error) {
// This has to be the current hub storage version. It will need to be updated
// if the hub storage version changes.
typedObj, ok := obj.(*storage.NamespacesAuthorizationRule)
if !ok {
return nil, errors.Errorf("cannot run on unknown resource type %T, expected *eventhub.NamespacesAuthorizationRule", obj)
}

// Type assert that we are the hub type. This will fail to compile if
// the hub type has been changed but this extension has not
var _ conversion.Hub = typedObj

hasSecrets := namespacesAuthorizationRuleSecretsSpecified(typedObj)
if !hasSecrets {
log.V(Debug).Info("No secrets retrieval to perform as operatorSpec is empty")
return nil, nil
}

id, err := genruntime.GetAndParseResourceID(typedObj)
if err != nil {
return nil, err
}

// Only bother calling ListKeys if there are secrets to retrieve
var res armeventhub.NamespacesClientListKeysResponse
if hasSecrets {
subscription := id.SubscriptionID
// Using armClient.ClientOptions() here ensures we share the same HTTP connection, so this is not opening a new
// connection each time through
var confClient *armeventhub.NamespacesClient
confClient, err = armeventhub.NewNamespacesClient(subscription, armClient.Creds(), armClient.ClientOptions())
if err != nil {
return nil, errors.Wrapf(err, "failed to create new NamespaceClient")
}

res, err = confClient.ListKeys(ctx, id.ResourceGroupName, id.Parent.Name, typedObj.AzureName(), nil)
if err != nil {
return nil, errors.Wrapf(err, "failed to retreive response")
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: unneeded newline

}

secretSlice, err := namespacesAuthorizationRuleSecretsToWrite(typedObj, res.AccessKeys)
if err != nil {
return nil, err
}

return secrets.SliceToClientObjectSlice(secretSlice), nil
}

func namespacesAuthorizationRuleSecretsSpecified(obj *storage.NamespacesAuthorizationRule) bool {
if obj.Spec.OperatorSpec == nil || obj.Spec.OperatorSpec.Secrets == nil {
return false
}

secrets := obj.Spec.OperatorSpec.Secrets

if secrets.PrimaryKey != nil ||
secrets.SecondaryKey != nil ||
secrets.PrimaryConnectionString != nil ||
secrets.SecondaryConnectionString != nil {
return true
}

return false
}

func namespacesAuthorizationRuleSecretsToWrite(obj *storage.NamespacesAuthorizationRule, keys armeventhub.AccessKeys) ([]*v1.Secret, error) {
operatorSpecSecrets := obj.Spec.OperatorSpec.Secrets
if operatorSpecSecrets == nil {
return nil, errors.Errorf("unexpected nil operatorspec")
}

collector := secrets.NewCollector(obj.Namespace)

collector.AddValue(operatorSpecSecrets.PrimaryKey, *keys.PrimaryKey)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same to.Value comment

collector.AddValue(operatorSpecSecrets.SecondaryKey, *keys.SecondaryKey)
collector.AddValue(operatorSpecSecrets.PrimaryConnectionString, *keys.PrimaryConnectionString)
collector.AddValue(operatorSpecSecrets.SecondaryConnectionString, *keys.SecondaryConnectionString)

return collector.Values()
}
Loading
Loading