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

azurerm_role_assignment: Allow for sub aliases scopes #20895

Merged
merged 6 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
62 changes: 42 additions & 20 deletions internal/services/authorization/parse/role_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@ import (
// TODO: @tombuildsstuff: this wants refactoring and fixing into sub-ID parsers

type RoleAssignmentId struct {
SubscriptionID string
ResourceGroup string
ManagementGroup string
ResourceScope string
ResourceProvider string
Name string
TenantId string
IsSubscriptionLevel bool
SubscriptionID string
ResourceGroup string
ManagementGroup string
ResourceScope string
ResourceProvider string
Name string
SubscriptionAlias string
TenantId string
IsSubscriptionLevel bool
IsSubscriptionAliasLevel bool
}

func NewRoleAssignmentID(subscriptionId, resourceGroup, resourceProvider, resourceScope, managementGroup, name, tenantId string, isSubLevel bool) (*RoleAssignmentId, error) {
if subscriptionId == "" && resourceGroup == "" && managementGroup == "" && !isSubLevel {
return nil, fmt.Errorf("one of subscriptionId, resourceGroup, managementGroup or isSubscriptionLevel must be provided")
func NewRoleAssignmentID(subscriptionId, resourceGroup, resourceProvider, resourceScope, managementGroup, name, tenantId, subscriptionAlias string, isSubLevel bool, isSubAliasLevel bool) (*RoleAssignmentId, error) {
if subscriptionId == "" && resourceGroup == "" && managementGroup == "" && !isSubLevel && !isSubAliasLevel {
return nil, fmt.Errorf("one of subscriptionId, resourceGroup, managementGroup, isSubscriptionLevel or isSubscriptionAliasLevel must be provided")
}

if managementGroup != "" {
Expand All @@ -37,21 +39,29 @@ func NewRoleAssignmentID(subscriptionId, resourceGroup, resourceProvider, resour
}
}

if isSubAliasLevel {
if subscriptionId != "" || resourceGroup != "" || managementGroup != "" {
return nil, fmt.Errorf("cannot provide subscriptionId, resourceGroup or managementGroup when isSubscriptionAliasLevel is provided")
}
}

if resourceGroup != "" {
if subscriptionId == "" {
return nil, fmt.Errorf("subscriptionId must not be empty when resourceGroup is provided")
}
}

return &RoleAssignmentId{
SubscriptionID: subscriptionId,
ResourceGroup: resourceGroup,
ResourceProvider: resourceProvider,
ResourceScope: resourceScope,
ManagementGroup: managementGroup,
Name: name,
TenantId: tenantId,
IsSubscriptionLevel: isSubLevel,
SubscriptionID: subscriptionId,
ResourceGroup: resourceGroup,
ResourceProvider: resourceProvider,
ResourceScope: resourceScope,
ManagementGroup: managementGroup,
SubscriptionAlias: subscriptionAlias,
Name: name,
TenantId: tenantId,
IsSubscriptionLevel: isSubLevel,
IsSubscriptionAliasLevel: isSubAliasLevel,
}, nil
}

Expand All @@ -73,6 +83,11 @@ func (id RoleAssignmentId) AzureResourceID() string {
return fmt.Sprintf(fmtString, id.SubscriptionID, id.ResourceGroup, id.Name)
}

if id.IsSubscriptionAliasLevel {
fmtString := "/providers/Microsoft.Subscription/aliases/%s/providers/Microsoft.Authorization/roleAssignments/%s"
return fmt.Sprintf(fmtString, id.SubscriptionAlias, id.Name)
}

if id.IsSubscriptionLevel {
fmtString := "/providers/Microsoft.Subscription/providers/Microsoft.Authorization/roleAssignments/%s"
return fmt.Sprintf(fmtString, id.Name)
Expand Down Expand Up @@ -131,7 +146,14 @@ func RoleAssignmentID(input string) (*RoleAssignmentId, error) {
if len(idParts) != 2 {
return nil, fmt.Errorf("could not parse Role Assignment ID %q for subscription scope", input)
}
roleAssignmentId.IsSubscriptionLevel = true
if strings.Contains(input, "/aliases/") {
roleAssignmentId.IsSubscriptionAliasLevel = true
aliasParts := strings.Split(idParts[0], "/")
alias := aliasParts[len(aliasParts)-1]
roleAssignmentId.SubscriptionAlias = alias
} else {
roleAssignmentId.IsSubscriptionLevel = true
}
if idParts[1] == "" {
return nil, fmt.Errorf("ID was missing a value for the roleAssignments element")
}
Expand Down
52 changes: 42 additions & 10 deletions internal/services/authorization/parse/role_assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import (

func TestRoleAssignmentIDFormatter(t *testing.T) {
testData := []struct {
SubscriptionId string
ResourceGroup string
ResourceProvider string
ResourceScope string
ManagementGroup string
IsSubscriptionLevel bool
Name string
TenantId string
Expected string
SubscriptionId string
ResourceGroup string
ResourceProvider string
ResourceScope string
ManagementGroup string
SubscriptionAlias string
IsSubscriptionLevel bool
IsSubscriptionAliasLevel bool
Name string
TenantId string
Expected string
}{
{
SubscriptionId: "",
Expand Down Expand Up @@ -92,10 +94,17 @@ func TestRoleAssignmentIDFormatter(t *testing.T) {
TenantId: "34567812-3456-7653-6742-345678901234",
Expected: "/providers/Microsoft.Subscription/providers/Microsoft.Authorization/roleAssignments/23456781-2349-8764-5631-234567890121|34567812-3456-7653-6742-345678901234",
},
{
IsSubscriptionAliasLevel: true,
Name: "23456781-2349-8764-5631-234567890121",
TenantId: "34567812-3456-7653-6742-345678901234",
SubscriptionAlias: "my-awesome-sub",
Expected: "/providers/Microsoft.Subscription/aliases/my-awesome-sub/providers/Microsoft.Authorization/roleAssignments/23456781-2349-8764-5631-234567890121|34567812-3456-7653-6742-345678901234",
},
}
for _, v := range testData {
t.Logf("testing %+v", v)
actual, err := NewRoleAssignmentID(v.SubscriptionId, v.ResourceGroup, v.ResourceProvider, v.ResourceScope, v.ManagementGroup, v.Name, v.TenantId, v.IsSubscriptionLevel)
actual, err := NewRoleAssignmentID(v.SubscriptionId, v.ResourceGroup, v.ResourceProvider, v.ResourceScope, v.ManagementGroup, v.Name, v.TenantId, v.SubscriptionAlias, v.IsSubscriptionLevel, v.IsSubscriptionAliasLevel)
if err != nil {
if v.Expected == "" {
continue
Expand Down Expand Up @@ -166,6 +175,16 @@ func TestRoleAssignmentID(t *testing.T) {
},
},

{
// valid at subscriptions aliases scope
Input: "/providers/Microsoft.Subscription/aliases/my-awesome-sub/providers/Microsoft.Authorization/roleAssignments/23456781-2349-8764-5631-234567890121",
Expected: &RoleAssignmentId{
IsSubscriptionAliasLevel: true,
Name: "23456781-2349-8764-5631-234567890121",
favoretti marked this conversation as resolved.
Show resolved Hide resolved
SubscriptionAlias: "my-awesome-sub",
},
},

{
// valid at subscription scope
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/providers/Microsoft.Authorization/roleAssignments/23456781-2349-8764-5631-234567890121",
Expand Down Expand Up @@ -274,5 +293,18 @@ func TestRoleAssignmentID(t *testing.T) {
if actual.ManagementGroup != v.Expected.ManagementGroup {
t.Fatalf("Expected %q but got %q for Role Assignment Management Group", v.Expected.ManagementGroup, actual.ManagementGroup)
}

if actual.IsSubscriptionLevel != v.Expected.IsSubscriptionLevel {
t.Fatalf("Expected %v but got %v for Role Assignment SubscriptionLevel flag", v.Expected.IsSubscriptionLevel, actual.IsSubscriptionLevel)
}

if actual.IsSubscriptionAliasLevel != v.Expected.IsSubscriptionAliasLevel {
t.Fatalf("Expected %v but got %v for Role Assignment SubscriptionAliasLevel flag", v.Expected.IsSubscriptionAliasLevel, actual.IsSubscriptionAliasLevel)
}

if actual.SubscriptionAlias != v.Expected.SubscriptionAlias {
t.Fatalf("Expected %q but got %q for Role Assignment SubscriptionAlias", v.Expected.SubscriptionAlias, actual.SubscriptionAlias)
}

}
}
5 changes: 2 additions & 3 deletions internal/services/authorization/role_assignment_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -60,9 +61,7 @@ func resourceArmRoleAssignment() *pluginsdk.Resource {
// Elevated access for a global admin is needed to assign roles in this scope:
// https://docs.microsoft.com/en-us/azure/role-based-access-control/elevate-access-global-admin#azure-cli
// It seems only user account is allowed to be elevated access.
validation.StringInSlice([]string{
"/providers/Microsoft.Subscription",
}, false),
validation.StringMatch(regexp.MustCompile("/providers/Microsoft.Subscription.*"), "Subscription scope is invalid"),

billingValidate.EnrollmentID,
commonids.ValidateManagementGroupID,
Expand Down