-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ARO-4376 add dynamic validation for platformworkloadidentityprofile
- Loading branch information
1 parent
7964440
commit 072ad43
Showing
6 changed files
with
180 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
pkg/validate/dynamic/platformworkloadidentityprofile.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package dynamic | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
sdkauthorization "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/authorization/armauthorization/v3" | ||
"github.com/Azure/go-autorest/autorest/azure" | ||
|
||
"github.com/Azure/ARO-RP/pkg/api" | ||
"github.com/Azure/ARO-RP/pkg/util/azureclient/azuresdk/armauthorization" | ||
"github.com/Azure/ARO-RP/pkg/util/rbac" | ||
"github.com/Azure/ARO-RP/pkg/util/stringutils" | ||
) | ||
|
||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the Apache License 2.0. | ||
|
||
func (dv *dynamic) ValidatePlatformWorkloadIdentityProfile(ctx context.Context, oc *api.OpenShiftCluster, platformWorkloadIdentityRoles []api.PlatformWorkloadIdentityRole, roleDefinitions armauthorization.RoleDefinitionsClient) error { | ||
dv.log.Print("ValidatePlatformWorkloadIdentityProfile") | ||
|
||
platformIdentitiesActionsMap := map[string][]string{} | ||
|
||
requiredOperatorIdentities := []string{} | ||
recievedOperatorIdentities := []string{} | ||
for _, role := range platformWorkloadIdentityRoles { | ||
requiredOperatorIdentities = append(requiredOperatorIdentities, role.OperatorName) | ||
} | ||
for _, role := range oc.Properties.PlatformWorkloadIdentityProfile.PlatformWorkloadIdentities { | ||
recievedOperatorIdentities = append(recievedOperatorIdentities, role.OperatorName) | ||
} | ||
ok := stringutils.ElementsMatch(requiredOperatorIdentities, recievedOperatorIdentities) | ||
if !ok { | ||
return api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodePlatformWorkloadIdentityMismatch, | ||
"properties.ValidatePlatformWorkloadIdentityProfile.PlatformWorkloadIdentities", "There's a mismatch between the required and expected set of platform workload identities for the requested OpenShift version '%s'", oc.Properties.ClusterProfile.Version) | ||
} | ||
|
||
err := dv.validateClusterMSI(ctx, oc) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, role := range platformWorkloadIdentityRoles { | ||
definition, err := dv.roleDefinitions.GetByID(ctx, role.RoleDefinitionID, &sdkauthorization.RoleDefinitionsClientGetByIDOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(definition.Properties.Permissions) <= 0 { | ||
return api.NewCloudError(http.StatusInternalServerError, api.CloudErrorCodeInvalidClusterMSICount, | ||
"dynamic.ValidatePlatformWorkloadIdentityProfile", "No Permissions exists for the role %s", role.RoleDefinitionID) | ||
} | ||
|
||
actions := []string{} | ||
for _, action := range definition.Properties.Permissions[0].Actions { | ||
actions = append(actions, *action) | ||
} | ||
platformIdentitiesActionsMap[role.OperatorName] = actions | ||
} | ||
|
||
dv.platformIdentities = oc.Properties.PlatformWorkloadIdentityProfile.PlatformWorkloadIdentities | ||
dv.platformIdentitiesActionsMap = platformIdentitiesActionsMap | ||
dv.roleDefinitions = roleDefinitions | ||
return nil | ||
} | ||
|
||
func (dv *dynamic) validateClusterMSI(ctx context.Context, oc *api.OpenShiftCluster) error { | ||
if len(oc.Identity.UserAssignedIdentities) != 1 { | ||
return api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidClusterMSICount, | ||
"identity.userAssignedIdentities", "Unexpected number of OpenShift Cluster associated User Assigned Identity are provided for the Workload Identity OpenShift cluster, expected one User Assigned Identity") | ||
} | ||
|
||
for _, identity := range oc.Identity.UserAssignedIdentities { | ||
return dv.validateClusterMSIPermissions(ctx, identity.PrincipalID, oc.Properties.PlatformWorkloadIdentityProfile.PlatformWorkloadIdentities) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (dv *dynamic) validateClusterMSIPermissions(ctx context.Context, oid string, platformIdentities []api.PlatformWorkloadIdentity) error { | ||
definition, err := dv.roleDefinitions.GetByID(ctx, rbac.RoleAzureRedHatOpenShiftFederatedCredentialRole, &sdkauthorization.RoleDefinitionsClientGetByIDOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(definition.Properties.Permissions) <= 0 { | ||
return api.NewCloudError(http.StatusInternalServerError, api.CloudErrorCodeInvalidClusterMSICount, | ||
"dynamic.validateClusterMSIPermissions", "No Permissions exists for the role %s", rbac.RoleAzureRedHatOpenShiftFederatedCredentialRole) | ||
} | ||
|
||
actions := []string{} | ||
for _, action := range definition.Properties.Permissions[0].Actions { | ||
actions = append(actions, *action) | ||
} | ||
|
||
for _, platformIdentity := range platformIdentities { | ||
pid, err := azure.ParseResourceID(platformIdentity.ResourceID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = dv.validateActionsByOID(ctx, &pid, actions, &oid) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters