Skip to content

Commit b3916a7

Browse files
committed
Validate identity type on user-assigned identities
Validate the identity type is 'UserAssigned' when using user-assigned identities. Signed-off-by: Bryan Cox <[email protected]>
1 parent cc3e8e9 commit b3916a7

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

api/v1beta1/azuremachine_default_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,13 @@ func createMachineWithUserAssignedIdentities(identitiesList []UserAssignedIdenti
596596
return machine
597597
}
598598

599+
func createMachineWithUserAssignedIdentitiesWithBadIdentity(identitiesList []UserAssignedIdentity) *AzureMachine {
600+
machine := hardcodedAzureMachineWithSSHKey(generateSSHPublicKey(true))
601+
machine.Spec.Identity = VMIdentitySystemAssigned
602+
machine.Spec.UserAssignedIdentities = identitiesList
603+
return machine
604+
}
605+
599606
func hardcodedAzureMachineWithSSHKey(sshPublicKey string) *AzureMachine {
600607
return &AzureMachine{
601608
ObjectMeta: metav1.ObjectMeta{

api/v1beta1/azuremachine_validation.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ func ValidateSystemAssignedIdentity(identityType VMIdentity, oldIdentity, newIde
138138
func ValidateUserAssignedIdentity(identityType VMIdentity, userAssignedIdentities []UserAssignedIdentity, fldPath *field.Path) field.ErrorList {
139139
allErrs := field.ErrorList{}
140140

141+
if len(userAssignedIdentities) > 0 && identityType != VMIdentityUserAssigned {
142+
allErrs = append(allErrs, field.Invalid(fldPath, identityType, "must be set to 'UserAssigned' when assigning any user identity to the machine"))
143+
}
144+
141145
if identityType == VMIdentityUserAssigned {
142146
if len(userAssignedIdentities) == 0 {
143147
allErrs = append(allErrs, field.Required(fldPath, "must be specified for the 'UserAssigned' identity type"))
@@ -160,7 +164,7 @@ func ValidateSystemAssignedIdentityRole(identityType VMIdentity, roleAssignmentN
160164
if roleAssignmentName != "" && role != nil && role.Name != "" {
161165
allErrs = append(allErrs, field.Invalid(fldPath, role.Name, "cannot set both roleAssignmentName and systemAssignedIdentityRole.name"))
162166
}
163-
if identityType == VMIdentitySystemAssigned {
167+
if identityType == VMIdentitySystemAssigned && role != nil {
164168
if role.DefinitionID == "" {
165169
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "systemAssignedIdentityRole", "definitionID"), role.DefinitionID, "the definitionID field cannot be empty"))
166170
}

api/v1beta1/azuremachine_webhook_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ func TestAzureMachine_ValidateCreate(t *testing.T) {
9595
}),
9696
wantErr: false,
9797
},
98+
{
99+
name: "azuremachine with list of user-assigned identities with wrong identity type",
100+
machine: createMachineWithUserAssignedIdentitiesWithBadIdentity([]UserAssignedIdentity{
101+
{ProviderID: "azure:///subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-resource-group/providers/Microsoft.Compute/virtualMachines/default-12345-control-plane-9d5x5"},
102+
{ProviderID: "azure:///subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-resource-group/providers/Microsoft.Compute/virtualMachines/default-12345-control-plane-a1b2c"},
103+
}),
104+
wantErr: true,
105+
},
98106
{
99107
name: "azuremachine with empty list of user-assigned identities",
100108
machine: createMachineWithUserAssignedIdentities([]UserAssignedIdentity{}),

0 commit comments

Comments
 (0)