From a9a5fc9553c8aaebb06b4d1a2acdb7f06f5b6357 Mon Sep 17 00:00:00 2001 From: rujche Date: Thu, 31 Oct 2024 00:26:14 +0800 Subject: [PATCH 1/2] Support detect Azure Event Hubs, connect by connection string. --- cli/azd/internal/repository/infra_confirm.go | 67 ++++++++++++------- ...ent-hubs-namespace-connection-string.bicep | 3 +- ...rvicebus-namespace-connection-string.bicep | 1 - ...et-storage-account-connection-string.bicep | 19 ++++++ .../scaffold/templates/resources.bicept | 34 +++++++++- 5 files changed, 96 insertions(+), 28 deletions(-) create mode 100644 cli/azd/resources/scaffold/base/modules/set-storage-account-connection-string.bicep diff --git a/cli/azd/internal/repository/infra_confirm.go b/cli/azd/internal/repository/infra_confirm.go index 342b2815ffa..eb116dc9a61 100644 --- a/cli/azd/internal/repository/infra_confirm.go +++ b/cli/azd/internal/repository/infra_confirm.go @@ -348,42 +348,61 @@ azureDepPrompt: } } - authType := scaffold.AuthType(0) - switch azureDep.(type) { + switch dependency := azureDep.(type) { case appdetect.AzureDepServiceBus: - _authType, err := i.console.Prompt(ctx, input.ConsoleOptions{ - Message: fmt.Sprintf("Input the authentication type you want for (%s), "+ - "1 for connection string, 2 for managed identity", azureDep.ResourceDisplay()), - Help: "Authentication type:\n\n" + - "Enter 1 if you want to use connection string to connect to the Service Bus.\n" + - "Enter 2 if you want to use user assigned managed identity to connect to the Service Bus.", - }) + authType, err := i.chooseAuthType(ctx, azureDepName) if err != nil { return err } - - if _authType != "1" && _authType != "2" { - i.console.Message(ctx, "Invalid authentication type. Please enter 0 or 1.") - continue azureDepPrompt - } - if _authType == "1" { - authType = scaffold.AuthType_PASSWORD - } else { - authType = scaffold.AuthType_TOKEN_CREDENTIAL - } - } - - switch dependency := azureDep.(type) { - case appdetect.AzureDepServiceBus: spec.AzureServiceBus = &scaffold.AzureDepServiceBus{ Name: azureDepName, Queues: dependency.Queues, AuthUsingConnectionString: authType == scaffold.AuthType_PASSWORD, AuthUsingManagedIdentity: authType == scaffold.AuthType_TOKEN_CREDENTIAL, } - break azureDepPrompt + case appdetect.AzureDepEventHubs: + authType, err := i.chooseAuthType(ctx, azureDepName) + if err != nil { + return err + } + spec.AzureEventHubs = &scaffold.AzureDepEventHubs{ + Name: azureDepName, + EventHubNames: dependency.Names, + AuthUsingConnectionString: authType == scaffold.AuthType_PASSWORD, + AuthUsingManagedIdentity: authType == scaffold.AuthType_TOKEN_CREDENTIAL, + } + case appdetect.AzureDepStorageAccount: + authType, err := i.chooseAuthType(ctx, azureDepName) + if err != nil { + return err + } + spec.AzureStorageAccount = &scaffold.AzureDepStorageAccount{ + Name: azureDepName, + ContainerNames: dependency.ContainerNames, + AuthUsingConnectionString: authType == scaffold.AuthType_PASSWORD, + AuthUsingManagedIdentity: authType == scaffold.AuthType_TOKEN_CREDENTIAL, + } } break azureDepPrompt } return nil } + +func (i *Initializer) chooseAuthType(ctx context.Context, serviceName string) (scaffold.AuthType, error) { + portOptions := []string{ + "User assigned managed identity", + "Connection string", + } + selection, err := i.console.Select(ctx, input.ConsoleOptions{ + Message: "Choose auth type for '" + serviceName + "'?", + Options: portOptions, + }) + if err != nil { + return scaffold.AUTH_TYPE_UNSPECIFIED, err + } + if selection == 0 { + return scaffold.AuthType_TOKEN_CREDENTIAL, nil + } else { + return scaffold.AuthType_PASSWORD, nil + } +} diff --git a/cli/azd/resources/scaffold/base/modules/set-event-hubs-namespace-connection-string.bicep b/cli/azd/resources/scaffold/base/modules/set-event-hubs-namespace-connection-string.bicep index 82d13f9a83d..7eee8d73cdc 100644 --- a/cli/azd/resources/scaffold/base/modules/set-event-hubs-namespace-connection-string.bicep +++ b/cli/azd/resources/scaffold/base/modules/set-event-hubs-namespace-connection-string.bicep @@ -14,7 +14,6 @@ resource connectionStringSecret 'Microsoft.KeyVault/vaults/secrets@2022-07-01' = name: connectionStringSecretName parent: keyVault properties: { - value: listKeys(concat(resourceId('Microsoft.EventHub/namespaces', eventHubsNamespaceName), '/AuthorizationRules/RootManageSharedAccessKey'), '2024-01-01').primaryConnectionString + value: listKeys(concat(resourceId('Microsoft.EventHub/namespaces', eventHubsNamespaceName), '/AuthorizationRules/RootManageSharedAccessKey'), eventHubsNamespace.apiVersion).primaryConnectionString } } - diff --git a/cli/azd/resources/scaffold/base/modules/set-servicebus-namespace-connection-string.bicep b/cli/azd/resources/scaffold/base/modules/set-servicebus-namespace-connection-string.bicep index 42f0779bb9d..1152b5dcc12 100644 --- a/cli/azd/resources/scaffold/base/modules/set-servicebus-namespace-connection-string.bicep +++ b/cli/azd/resources/scaffold/base/modules/set-servicebus-namespace-connection-string.bicep @@ -17,4 +17,3 @@ resource serviceBusConnectionStringSecret 'Microsoft.KeyVault/vaults/secrets@202 value: listKeys(concat(resourceId('Microsoft.ServiceBus/namespaces', serviceBusNamespaceName), '/AuthorizationRules/RootManageSharedAccessKey'), serviceBusNamespace.apiVersion).primaryConnectionString } } - diff --git a/cli/azd/resources/scaffold/base/modules/set-storage-account-connection-string.bicep b/cli/azd/resources/scaffold/base/modules/set-storage-account-connection-string.bicep new file mode 100644 index 00000000000..2b04668f17b --- /dev/null +++ b/cli/azd/resources/scaffold/base/modules/set-storage-account-connection-string.bicep @@ -0,0 +1,19 @@ +param storageAccountName string +param connectionStringSecretName string +param keyVaultName string + +resource storageAccount 'Microsoft.Storage/storageAccounts@2023-05-01' existing = { + name: storageAccountName +} + +resource keyVault 'Microsoft.KeyVault/vaults@2022-07-01' existing = { + name: keyVaultName +} + +resource connectionStringSecret 'Microsoft.KeyVault/vaults/secrets@2022-07-01' = { + name: connectionStringSecretName + parent: keyVault + properties: { + value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccount.name};AccountKey=${storageAccount.listKeys().keys[0].value};EndpointSuffix=${environment().suffixes.storage}' + } +} diff --git a/cli/azd/resources/scaffold/templates/resources.bicept b/cli/azd/resources/scaffold/templates/resources.bicept index a5564fed40f..19fdfe57a8a 100644 --- a/cli/azd/resources/scaffold/templates/resources.bicept +++ b/cli/azd/resources/scaffold/templates/resources.bicept @@ -197,10 +197,21 @@ module storageAccount 'br/public:avm/res/storage/storage-account:0.14.3' = { ] networkAcls: { defaultAction: 'Allow' - } + } tags: tags } } + +{{- if (and .AzureStorageAccount .AzureStorageAccount.AuthUsingConnectionString) }} +module storageAccountConnectionString './modules/set-storage-account-connection-string.bicep' = { + name: 'storageAccountConnectionString' + params: { + storageAccountName: storageAccountName + connectionStringSecretName: 'STORAGE-ACCOUNT-CONNECTION-STRING' + keyVaultName: keyVault.outputs.name + } +} +{{end}} {{end}} {{- if .AzureServiceBus }} @@ -340,6 +351,13 @@ module {{bicepName .Name}} 'br/public:avm/res/app/container-app:0.8.0' = { keyVaultUrl: '${keyVault.outputs.uri}secrets/SERVICEBUS-CONNECTION-STRING' } {{- end}} + {{- if (and .AzureStorageAccount .AzureStorageAccount.AuthUsingConnectionString) }} + { + name: 'storage-account-connection-string' + identity:{{bicepName .Name}}Identity.outputs.resourceId + keyVaultUrl: '${keyVault.outputs.uri}secrets/STORAGE-ACCOUNT-CONNECTION-STRING' + } + {{- end}} ], map({{bicepName .Name}}Secrets, secret => { name: secret.secretRef @@ -467,6 +485,20 @@ module {{bicepName .Name}} 'br/public:avm/res/app/container-app:0.8.0' = { value: {{bicepName .Name}}Identity.outputs.clientId } {{- end}} + {{- if (and .AzureStorageAccount .AzureStorageAccount.AuthUsingConnectionString) }} + { + name: 'SPRING_CLOUD_AZURE_STORAGE_CONNECTION_STRING' + secretRef: 'storage-account-connection-string' + } + { + name: 'SPRING_CLOUD_AZURE_STORAGE_CREDENTIAL_MANAGEDIDENTITYENABLED' + value: 'false' + } + { + name: 'SPRING_CLOUD_AZURE_STORAGE_CREDENTIAL_CLIENTID' + value: '' + } + {{- end}} {{- if .AzureServiceBus }} { From ce679d7a3712e1d979a918f49ebe53ba9002c81a Mon Sep 17 00:00:00 2001 From: rujche Date: Thu, 31 Oct 2024 17:15:56 +0800 Subject: [PATCH 2/2] Fix error of environment variables. --- .../scaffold/templates/resources.bicept | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/cli/azd/resources/scaffold/templates/resources.bicept b/cli/azd/resources/scaffold/templates/resources.bicept index 19fdfe57a8a..247da419c05 100644 --- a/cli/azd/resources/scaffold/templates/resources.bicept +++ b/cli/azd/resources/scaffold/templates/resources.bicept @@ -443,7 +443,7 @@ module {{bicepName .Name}} 'br/public:avm/res/app/container-app:0.8.0' = { {{- end}} {{- if (and .AzureEventHubs .AzureEventHubs.AuthUsingManagedIdentity) }} { - name: 'SPRING_CLOUD_AZURE_EVENTHUBS_CONNECTION_STRING' + name: 'SPRING_CLOUD_AZURE_EVENTHUBS_CONNECTIONSTRING' value: '' } { @@ -457,7 +457,7 @@ module {{bicepName .Name}} 'br/public:avm/res/app/container-app:0.8.0' = { {{- end}} {{- if (and .AzureEventHubs .AzureEventHubs.AuthUsingConnectionString) }} { - name: 'SPRING_CLOUD_AZURE_EVENTHUBS_CONNECTION_STRING' + name: 'SPRING_CLOUD_AZURE_EVENTHUBS_CONNECTIONSTRING' secretRef: 'event-hubs-connection-string' } { @@ -477,25 +477,29 @@ module {{bicepName .Name}} 'br/public:avm/res/app/container-app:0.8.0' = { {{- end}} {{- if (and .AzureStorageAccount .AzureStorageAccount.AuthUsingManagedIdentity) }} { - name: 'SPRING_CLOUD_AZURE_STORAGE_CREDENTIAL_MANAGEDIDENTITYENABLED' + name: 'SPRING_CLOUD_AZURE_EVENTHUBS_PROCESSOR_CHECKPOINTSTORE_CONNECTIONSTRING' + value: '' + } + { + name: 'SPRING_CLOUD_AZURE_EVENTHUBS_PROCESSOR_CHECKPOINTSTORE_CREDENTIAL_MANAGEDIDENTITYENABLED' value: 'true' } { - name: 'SPRING_CLOUD_AZURE_STORAGE_CREDENTIAL_CLIENTID' + name: 'SPRING_CLOUD_AZURE_EVENTHUBS_PROCESSOR_CHECKPOINTSTORE_CREDENTIAL_CLIENTID' value: {{bicepName .Name}}Identity.outputs.clientId } {{- end}} {{- if (and .AzureStorageAccount .AzureStorageAccount.AuthUsingConnectionString) }} { - name: 'SPRING_CLOUD_AZURE_STORAGE_CONNECTION_STRING' + name: 'SPRING_CLOUD_AZURE_EVENTHUBS_PROCESSOR_CHECKPOINTSTORE_CONNECTIONSTRING' secretRef: 'storage-account-connection-string' } { - name: 'SPRING_CLOUD_AZURE_STORAGE_CREDENTIAL_MANAGEDIDENTITYENABLED' + name: 'SPRING_CLOUD_AZURE_EVENTHUBS_PROCESSOR_CHECKPOINTSTORE_CREDENTIAL_MANAGEDIDENTITYENABLED' value: 'false' } { - name: 'SPRING_CLOUD_AZURE_STORAGE_CREDENTIAL_CLIENTID' + name: 'SPRING_CLOUD_AZURE_EVENTHUBS_PROCESSOR_CHECKPOINTSTORE_CREDENTIAL_CLIENTID' value: '' } {{- end}} @@ -508,7 +512,7 @@ module {{bicepName .Name}} 'br/public:avm/res/app/container-app:0.8.0' = { {{- end}} {{- if (and .AzureServiceBus .AzureServiceBus.AuthUsingManagedIdentity) }} { - name: 'SPRING_CLOUD_AZURE_SERVICEBUS_CONNECTION_STRING' + name: 'SPRING_CLOUD_AZURE_SERVICEBUS_CONNECTIONSTRING' value: '' } { @@ -522,7 +526,7 @@ module {{bicepName .Name}} 'br/public:avm/res/app/container-app:0.8.0' = { {{- end}} {{- if (and .AzureServiceBus .AzureServiceBus.AuthUsingConnectionString) }} { - name: 'SPRING_CLOUD_AZURE_SERVICEBUS_CONNECTION_STRING' + name: 'SPRING_CLOUD_AZURE_SERVICEBUS_CONNECTIONSTRING' secretRef: 'servicebus-connection-string' } {