Skip to content

Commit 85ec20b

Browse files
authored
Merge pull request azure-javaee#3 from rujche/rujche/java-analyzer
Use managed-identity instead of username and password
2 parents 103a005 + 708681a commit 85ec20b

File tree

3 files changed

+61
-44
lines changed

3 files changed

+61
-44
lines changed

cli/azd/resources/scaffold/templates/db-mysql.bicept

+15-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ param location string = resourceGroup().location
44
param tags object = {}
55

66
param keyVaultName string
7+
param identityName string
78

89
param databaseUser string = 'mysqladmin'
910
param databaseName string = '{{.DatabaseName}}'
@@ -12,14 +13,25 @@ param databasePassword string
1213

1314
param allowAllIPsFirewall bool = false
1415

15-
resource mysqlServer'Microsoft.DBforMySQL/flexibleServers@2023-06-30' = {
16+
resource userAssignedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = {
17+
name: identityName
18+
location: location
19+
}
20+
21+
resource mysqlServer 'Microsoft.DBforMySQL/flexibleServers@2023-06-30' = {
1622
location: location
1723
tags: tags
1824
name: serverName
1925
sku: {
2026
name: 'Standard_B1ms'
2127
tier: 'Burstable'
2228
}
29+
identity: {
30+
type: 'UserAssigned'
31+
userAssignedIdentities: {
32+
'${userAssignedIdentity.id}': {}
33+
}
34+
}
2335
properties: {
2436
version: '8.0.21'
2537
administratorLogin: databaseUser
@@ -67,8 +79,6 @@ resource dbPasswordKey 'Microsoft.KeyVault/vaults/secrets@2022-07-01' = {
6779
}
6880
}
6981

70-
output databaseHost string = mysqlServer.properties.fullyQualifiedDomainName
71-
output databaseName string = databaseName
72-
output databaseUser string = databaseUser
73-
output databaseConnectionKey string = 'databasePassword'
82+
output databaseId string = database.id
83+
output identityName string = userAssignedIdentity.name
7484
{{ end}}

cli/azd/resources/scaffold/templates/host-containerapp.bicept

+40-33
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@ param postgresDatabaseName string
1919
param postgresDatabasePassword string
2020
{{- end}}
2121
{{- if .DbMySql}}
22-
param mysqlDatabaseHost string
23-
param mysqlDatabaseUser string
24-
param mysqlDatabaseName string
25-
@secure()
26-
param mysqlDatabasePassword string
22+
param mysqlDatabaseId string
23+
param mysqlIdentityName string
2724
{{- end}}
2825
{{- if .DbRedis}}
2926
param redisName string
@@ -153,12 +150,6 @@ resource app 'Microsoft.App/containerApps@2023-05-02-preview' = {
153150
value: postgresDatabasePassword
154151
}
155152
{{- end}}
156-
{{- if .DbMySql}}
157-
{
158-
name: 'mysql-db-pass'
159-
value: mysqlDatabasePassword
160-
}
161-
{{- end}}
162153
],
163154
map(secrets, secret => {
164155
name: secret.secretRef
@@ -203,28 +194,6 @@ resource app 'Microsoft.App/containerApps@2023-05-02-preview' = {
203194
value: '5432'
204195
}
205196
{{- end}}
206-
{{- if .DbMySql}}
207-
{
208-
name: 'MYSQL_HOST'
209-
value: mysqlDatabaseHost
210-
}
211-
{
212-
name: 'MYSQL_USERNAME'
213-
value: mysqlDatabaseUser
214-
}
215-
{
216-
name: 'MYSQL_DATABASE'
217-
value: mysqlDatabaseName
218-
}
219-
{
220-
name: 'MYSQL_PASSWORD'
221-
secretRef: 'mysql-db-pass'
222-
}
223-
{
224-
name: 'MYSQL_PORT'
225-
value: '3306'
226-
}
227-
{{- end}}
228197
{{- if .Frontend}}
229198
{{- range $i, $e := .Frontend.Backends}}
230199
{
@@ -266,6 +235,44 @@ resource app 'Microsoft.App/containerApps@2023-05-02-preview' = {
266235
}
267236
}
268237
}
238+
{{- if .DbMySql}}
239+
240+
resource linkerCreatorIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = {
241+
name: 'linkerCreatorIdentity'
242+
location: location
243+
}
244+
245+
resource linkerCreatorRole 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
246+
scope: resourceGroup()
247+
name: guid(subscription().id, resourceGroup().id, linkerCreatorIdentity.id, 'linkerCreatorRole')
248+
properties: {
249+
roleDefinitionId: subscriptionResourceId(
250+
'Microsoft.Authorization/roleDefinitions', 'b24988ac-6180-42a0-ab88-20f7382dd24c')
251+
principalType: 'ServicePrincipal'
252+
principalId: linkerCreatorIdentity.properties.principalId
253+
}
254+
}
255+
256+
resource appLinkToMySql 'Microsoft.Resources/deploymentScripts@2023-08-01' = {
257+
dependsOn: [ linkerCreatorRole ]
258+
name: 'appLinkToMySql'
259+
location: location
260+
kind: 'AzureCLI'
261+
identity: {
262+
type: 'UserAssigned'
263+
userAssignedIdentities: {
264+
'${linkerCreatorIdentity.id}': {}
265+
}
266+
}
267+
properties: {
268+
azCliVersion: '2.63.0'
269+
timeout: 'PT10M'
270+
scriptContent: 'apk update; apk add g++; apk add unixodbc-dev; az extension add --name containerapp; az extension add --name serviceconnector-passwordless --upgrade; az containerapp connection create mysql-flexible --connection appLinkToMySql --source-id ${app.id} --target-id ${mysqlDatabaseId} --client-type springBoot --user-identity client-id=${identity.properties.clientId} subs-id=${subscription().subscriptionId} user-object-id=${linkerCreatorIdentity.properties.principalId} mysql-identity-id=${mysqlIdentityName} -c main --yes; az tag create --resource-id ${app.id} --tags azd-service-name={{.Name}} '
271+
cleanupPreference: 'OnSuccess'
272+
retentionInterval: 'P1D'
273+
}
274+
}
275+
{{- end}}
269276

270277
output defaultDomain string = containerAppsEnvironment.properties.defaultDomain
271278
output name string = app.name

cli/azd/resources/scaffold/templates/main.bicept

+6-6
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ module cosmosDb './app/db-cosmos-mongo.bicep' = {
111111
scope: rg
112112
}
113113
{{- end}}
114-
115114
{{- if .DbPostgres}}
115+
116116
module postgresDb './app/db-postgres.bicep' = {
117117
name: 'postgresDb'
118118
params: {
@@ -126,20 +126,22 @@ module postgresDb './app/db-postgres.bicep' = {
126126
scope: rg
127127
}
128128
{{- end}}
129-
130129
{{- if .DbMySql}}
130+
131131
module mysqlDb './app/db-mysql.bicep' = {
132132
name: 'mysqlDb'
133133
params: {
134134
serverName: '${abbrs.dBforMySQLServers}${resourceToken}'
135135
location: location
136136
tags: tags
137+
identityName: '${abbrs.managedIdentityUserAssignedIdentities}mysql-${resourceToken}'
137138
databasePassword: databasePassword
138139
keyVaultName: keyVault.outputs.name
139140
allowAllIPsFirewall: true
140141
}
141142
scope: rg
142143
}
144+
143145
{{- end}}
144146

145147
{{- range .Services}}
@@ -168,10 +170,8 @@ module {{bicepName .Name}} './app/{{.Name}}.bicep' = {
168170
postgresDatabasePassword: vault.getSecret(postgresDb.outputs.databaseConnectionKey)
169171
{{- end}}
170172
{{- if .DbMySql}}
171-
mysqlDatabaseName: mysqlDb.outputs.databaseName
172-
mysqlDatabaseHost: mysqlDb.outputs.databaseHost
173-
mysqlDatabaseUser: mysqlDb.outputs.databaseUser
174-
mysqlDatabasePassword: vault.getSecret(mysqlDb.outputs.databaseConnectionKey)
173+
mysqlDatabaseId: mysqlDb.outputs.databaseId
174+
mysqlIdentityName: mysqlDb.outputs.identityName
175175
{{- end}}
176176
{{- if (and .Frontend .Frontend.Backends)}}
177177
apiUrls: [

0 commit comments

Comments
 (0)