Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
Add workload code into service principal name
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeGinnivan committed Nov 21, 2022
1 parent 9e57476 commit 9807f5d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-singers-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@arkahna/nx-terraform': patch
---

Fixed service principal naming
41 changes: 30 additions & 11 deletions libs/nx-terraform/src/generators/create-environment-sp/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default async function (

const servicePrincipalName =
options.name ??
`gh-actions-${repoSettings.azureResourcePrefix}-${options.environmentName}-sp`
`gh-actions-${repoSettings.azureResourcePrefix}-${options.environmentName}-${repoSettings.azureWorkloadCode}-sp`
const scopes = `/subscriptions/${environmentConfig.subscriptionId}/resourcegroups/${environmentConfig.resourceGroupName}`
const containerScope = `/subscriptions/${environmentConfig.subscriptionId}/resourceGroups/${environmentConfig.resourceGroupName}/providers/Microsoft.Storage/storageAccounts/${environmentConfig.terraformStorageAccount}/blobServices/default/containers/${environmentConfig.terraformStorageContainer}`

Expand Down Expand Up @@ -62,16 +62,7 @@ export default async function (
}

return async () => {
console.log('Ensuring logged in to correct tenant')
const accountShow = await getCurrentAzAccount()
if (accountShow.tenantId !== environmentConfig.tenantId) {
console.log(
'Current subscription belongs to wrong Tenant, select the correct subscription using:',
)
console.log(`> az account set --subscription ${environmentConfig.subscriptionId}`)

throw new Error('Tenant id does not match')
}
await ensureLoggedIntoCorrectTenant(environmentConfig)

console.log(`> ${getEscapedCommand(`az`, createServicePrincipalArgs)}`)
await execa(`az`, createServicePrincipalArgs, {
Expand Down Expand Up @@ -125,3 +116,31 @@ ${environmentConfig.environmentFileBody}
)
}
}
async function ensureLoggedIntoCorrectTenant(environmentConfig: {
environment: string
subscriptionId: string
tenantId: string
resourceGroupName: string
resourceLocation: string
terraformStorageAccount: string
terraformStorageContainer: string
keyVaultName: string
environmentMarkdownFilePath: string
environmentAttributes: Record<string, string>
terragruntConfigFile: string
terraformCloudWorkspaceName: string
environmentFile: string
attributes: Record<string, string>
environmentFileBody: string
}) {
console.log('Ensuring logged in to correct tenant')
const accountShow = await getCurrentAzAccount()
if (accountShow.tenantId !== environmentConfig.tenantId) {
console.log(
'Current subscription belongs to wrong Tenant, select the correct subscription using:',
)
console.log(`> az account set --subscription ${environmentConfig.subscriptionId}`)

throw new Error('Tenant id does not match')
}
}

0 comments on commit 9807f5d

Please sign in to comment.