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

Use User-Assigned Managed Identity to access Azure Container Registry #122

Open
simonkurtz-MSFT opened this issue Nov 2, 2023 · 3 comments
Labels
documentation Improvements or additions to documentation enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed security Security issue

Comments

@simonkurtz-MSFT
Copy link
Contributor

Please describe the feature.

We need a user-assigned managed identity to access Azure Container Registry in order to pull images to create Azure Container Apps. Using a system-assigned managed identity does not work because we can't create one until the container app create, but that won't exist until we can securely pull an image from the registry using an identity. Chickens and eggs, rejoice!

@simonkurtz-MSFT simonkurtz-MSFT added documentation Improvements or additions to documentation enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed security Security issue labels Nov 2, 2023
@jdrepo
Copy link

jdrepo commented Jun 22, 2024

Hi Simon,

I think it would work if the creation and rbac role assignment for user-assigned managed identity are moved into a separate module and then reference the "containerRegistryUserAssignedIdentityId" in the module output ?

module userassigned-identity.bicep

targetScope = 'resourceGroup'

// ------------------
//    PARAMETERS
// ------------------

@description('The location where the resources will be created.')
param location string = resourceGroup().location

@description('The tags to be assigned to the created resources.')
param tags object = {}

// Container Registry
@description('The name of the container registry.')
param containerRegistryName string

// ------------------
// VARIABLES
// ------------------

var containerRegistryPullRoleGuid='7f951dda-4ed3-4680-a7ca-43fe172d538d'

resource containerRegistry 'Microsoft.ContainerRegistry/registries@2023-01-01-preview' existing = {
  name: containerRegistryName
}

resource containerRegistryUserAssignedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = {
  name: 'aca-user-identity-${uniqueString(resourceGroup().id)}'
  location: location
  tags: tags
}

resource containerRegistryPullRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = if(!empty(containerRegistryName)) {
  name: guid(subscription().id, containerRegistry.id, containerRegistryUserAssignedIdentity.id) 
  scope: containerRegistry
  properties: {
    principalId: containerRegistryUserAssignedIdentity.properties.principalId
    roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', containerRegistryPullRoleGuid)
    principalType: 'ServicePrincipal'
  }
}

output containerRegistryUserAssignedIdentityId string = containerRegistryUserAssignedIdentity.id

and then reference them in container-apps.bicep

module containerRegistryUserAssignedIdentity 'userassigned-identity.bicep' = {
  name: 'containerRegistryUserAssignedIdentity-${uniqueString(resourceGroup().id)}'
  params: {
    containerRegistryName: containerRegistryName
  }
}

module frontendWebAppService 'container-apps/webapp-frontend-service.bicep' = {
  name: 'frontendWebAppService-${uniqueString(resourceGroup().id)}'
  params: {
    frontendWebAppServiceName: frontendWebAppServiceName
    location: location
    tags: tags
    containerAppsEnvironmentId: containerAppsEnvironment.id
    containerRegistryName: containerRegistryName
    // containerRegistryUserAssignedIdentityId: containerRegistryUserAssignedIdentity.id
    containerRegistryUserAssignedIdentityId: containerRegistryUserAssignedIdentity.outputs.containerRegistryUserAssignedIdentityId
    frontendWebAppServiceImage: frontendWebAppServiceImage
    appInsightsInstrumentationKey: applicationInsights.properties.InstrumentationKey
    frontendWebAppPortNumber: frontendWebAppPortNumber

  }
}

@simonkurtz-MSFT simonkurtz-MSFT changed the title User User-Assigned Managed Identity to access Azure Container Registry Use User-Assigned Managed Identity to access Azure Container Registry Jun 23, 2024
@simonkurtz-MSFT
Copy link
Contributor Author

Hi @jdrepo,

Something closely along those lines would do the trick. I won't be able to get to it any time soon due to other priorities, unfortunately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed security Security issue
Projects
Status: 📋 Backlog
Development

No branches or pull requests

3 participants
@jdrepo @simonkurtz-MSFT and others