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

Add sample for custom role definition and assignment #1045

Merged
merged 2 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/examples/101/custom-role-definition-assignment/main.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
targetScope = 'subscription'

param principalId string
var roleName = 'Bicep Custom Role demo'

resource definition 'Microsoft.Authorization/roleDefinitions@2018-01-01-preview' = {
name: guid(roleName)
properties: {
roleName: roleName
description: 'Custom role create with bicep'
permissions: [
{
actions: [
'*/read'
]
}
]
assignableScopes: [
subscription().id
]
}
}

resource assignment 'Microsoft.Authorization/roleAssignments@2020-04-01-preview' = {
name: guid(concat(roleName, principalId))
Copy link
Collaborator

Choose a reason for hiding this comment

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

instead of concatting these strings together, I would add arguments to the guid function (which can take as many arguments as you want to provide). I would also add the subscription id as an argument to ensure uniqueness:

name: guid(roleName, principalId, subscription().subscriptionId

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, good idea !
I make the small tweak

properties: {
roleDefinitionId: definition.id
principalId: principalId
}
}
46 changes: 46 additions & 0 deletions docs/examples/101/custom-role-definition-assignment/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"principalId": {
"type": "string"
}
},
"functions": [],
"variables": {
"roleName": "Bicep Custom Role demo"
},
"resources": [
{
"type": "Microsoft.Authorization/roleDefinitions",
"apiVersion": "2018-01-01-preview",
"name": "[guid(variables('roleName'))]",
"properties": {
"roleName": "[variables('roleName')]",
"description": "Custom role create with bicep",
"permissions": [
{
"actions": [
"*/read"
]
}
],
"assignableScopes": [
"[subscription().id]"
]
}
},
{
"type": "Microsoft.Authorization/roleAssignments",
"apiVersion": "2020-04-01-preview",
"name": "[guid(concat(variables('roleName'), parameters('principalId')))]",
"properties": {
"roleDefinitionId": "[subscriptionResourceId('Microsoft.Authorization/roleDefinitions', guid(variables('roleName')))]",
"principalId": "[parameters('principalId')]"
},
"dependsOn": [
"[subscriptionResourceId('Microsoft.Authorization/roleDefinitions', guid(variables('roleName')))]"
]
}
]
}