Skip to content

feat: add identity and role assignment lib steps#1197

Open
xinWeiWei24 wants to merge 4 commits into
v2from
xinwei/lib-identity-role
Open

feat: add identity and role assignment lib steps#1197
xinWeiWei24 wants to merge 4 commits into
v2from
xinwei/lib-identity-role

Conversation

@xinWeiWei24
Copy link
Copy Markdown
Collaborator

Add CreateManagedIdentity (exports IDENTITY_CLIENT_ID and IDENTITY_ID as ADO variables) and CreateRoleAssignment to lib/steps/azure.

Add CreateManagedIdentity (exports IDENTITY_CLIENT_ID and IDENTITY_ID
as ADO variables) and CreateRoleAssignment to lib/steps/azure.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Comment thread kcl/lib/steps/azure/identity.k Outdated
--name "${name}" \\
--subscription "${subscription}" \\
--query clientId -o tsv)
echo "##vso[task.setvariable variable=IDENTITY_CLIENT_ID]$IDENTITY_CLIENT_ID"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The functions here push the value to a hard-coded var IDENTITY_CLIENT_ID. Can we take a param to allow user to pass in a env name they want to store the value? This would help if there are multiple identities they want to create.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

fixed

Comment thread kcl/lib/steps/azure/identity.k Outdated
--query clientId -o tsv)
echo "##vso[task.setvariable variable=IDENTITY_CLIENT_ID]$IDENTITY_CLIENT_ID"

IDENTITY_ID=$(az identity show \\
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Does az identity show return both client id and identity id? I am a bit surprised if you want to call it twice.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Thanks! now only call once

Comment thread kcl/lib/steps/azure/role_assignment.k Outdated
--assignee "${assignee}" \\
--subscription "${subscription}"
"""
AzCli(serviceConnection, "Assign role ${role}", script)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Would be good to show assignee as well

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

added

Comment thread kcl/lib/steps/azure/role_assignment.k Outdated
@@ -0,0 +1,12 @@
import azure_pipelines.ap.steps

CreateRoleAssignment = lambda serviceConnection: str, scope: str, role: str, assignee: str, subscription: str -> steps.Step {
Copy link
Copy Markdown
Collaborator

@wonderyl wonderyl May 28, 2026

Choose a reason for hiding this comment

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

This function doesn't provide much value, would it be easier if the user just call AzCli with the script directly?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

When create a function, ask yourself, what does this function encapsulate?

Copy link
Copy Markdown
Collaborator Author

@xinWeiWei24 xinWeiWei24 May 28, 2026

Choose a reason for hiding this comment

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

This is intended to be invoked multiple times, since the 3 input variables ( scope, role, assignee) can differ per call. Wrapping it as a function avoids duplicating the same CLI script logic.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

will delete this and add the step to subnet creation

@@ -0,0 +1,18 @@
import azure_pipelines.ap.steps

CreateManagedIdentity = lambda serviceConnection: str, subscription: str, resourceGroup: str, name: str, exportVar: str = "IDENTITY" -> steps.Step {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

IIUC, you always need to create managed identity, then assign role, then bind it to a service account. Does it make sense to encapsulate the 3 steps into one?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Not for my case. I’m not binding it to a service account, and the role assignments target different resources and roles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants