Skip to content

Commit

Permalink
Refactor Role PolicyDoc creation
Browse files Browse the repository at this point in the history
Adds support for both operator roles (with multiple service accounts)
and addon roles (with a single service account)
  • Loading branch information
tbrisker committed Jun 30, 2022
1 parent ff12518 commit 1535b67
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
4 changes: 2 additions & 2 deletions cmd/create/operatorroles/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func createRoles(r *rosa.Runtime,
}
policyDetails = policies["operator_iam_role_policy"]

policy, err := aws.GenerateRolePolicyDoc(cluster, accountID, operator, policyDetails)
policy, err := aws.GenerateOperatorRolePolicyDoc(cluster, accountID, operator, policyDetails)
if err != nil {
return err
}
Expand Down Expand Up @@ -360,7 +360,7 @@ func buildCommands(r *rosa.Runtime,
}

policyDetail := policies["operator_iam_role_policy"]
policy, err := aws.GenerateRolePolicyDoc(cluster, accountID, operator, policyDetail)
policy, err := aws.GenerateOperatorRolePolicyDoc(cluster, accountID, operator, policyDetail)
if err != nil {
return "", err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/upgrade/operatorroles/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ func buildMissingOperatorRoleCommand(missingRoles map[string]*cmv1.STSOperator,
roleName := getRoleName(cluster, operator)
policyARN := aws.GetOperatorPolicyARN(accountID, prefix, operator.Namespace(), operator.Name())
policyDetails := policies["operator_iam_role_policy"]
policy, err := aws.GenerateRolePolicyDoc(cluster, accountID, operator, policyDetails)
policy, err := aws.GenerateOperatorRolePolicyDoc(cluster, accountID, operator, policyDetails)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -415,7 +415,7 @@ func upgradeMissingOperatorRole(missingRoles map[string]*cmv1.STSOperator, clust
policyDetails := policies["operator_iam_role_policy"]

policyARN := aws.GetOperatorPolicyARN(accountID, prefix, operator.Namespace(), operator.Name())
policy, err := aws.GenerateRolePolicyDoc(cluster, accountID, operator, policyDetails)
policy, err := aws.GenerateOperatorRolePolicyDoc(cluster, accountID, operator, policyDetails)
if err != nil {
return err
}
Expand Down
28 changes: 19 additions & 9 deletions pkg/aws/policy_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,7 @@ func getPolicyDocument(policyDocument *string) (*PolicyDocument, error) {
return &data, nil
}

func GenerateRolePolicyDoc(cluster *cmv1.Cluster, accountID string, operator *cmv1.STSOperator,
policyDetails string) (string, error) {
func GenerateRolePolicyDoc(cluster *cmv1.Cluster, accountID, serviceAccounts, policyDetails string) (string, error) {
oidcEndpointURL, err := url.ParseRequestURI(cluster.AWS().STS().OIDCEndpointURL())
if err != nil {
return "", err
Expand All @@ -280,17 +279,28 @@ func GenerateRolePolicyDoc(cluster *cmv1.Cluster, accountID string, operator *cm

oidcProviderARN := fmt.Sprintf("arn:aws:iam::%s:oidc-provider/%s", accountID, issuerURL)

serviceAccounts := []string{}
for _, sa := range operator.ServiceAccounts() {
serviceAccounts = append(serviceAccounts,
fmt.Sprintf("system:serviceaccount:%s:%s", operator.Namespace(), sa))
}

policy := InterpolatePolicyDocument(policyDetails, map[string]string{
"oidc_provider_arn": oidcProviderARN,
"issuer_url": issuerURL,
"service_accounts": strings.Join(serviceAccounts, `" , "`),
"service_accounts": serviceAccounts,
})

return policy, nil
}

func GenerateOperatorRolePolicyDoc(cluster *cmv1.Cluster, accountID string, operator *cmv1.STSOperator,
policyDetails string) (string, error) {
serviceAccounts := make([]string, len(operator.ServiceAccounts()))
for i, sa := range operator.ServiceAccounts() {
serviceAccounts[i] = fmt.Sprintf("system:serviceaccount:%s:%s", operator.Namespace(), sa)
}
service_accounts := strings.Join(serviceAccounts, `" , "`)

return GenerateRolePolicyDoc(cluster, accountID, service_accounts, policyDetails)
}

func GenerateAddonPolicyDoc(cluster *cmv1.Cluster, accountID string, cr *cmv1.CredentialRequest,
policyDetails string) (string, error) {
service_accounts := fmt.Sprintf("system:serviceaccount:%s:%s", cr.Namespace(), cr.ServiceAccount())
return GenerateRolePolicyDoc(cluster, accountID, service_accounts, policyDetails)
}

0 comments on commit 1535b67

Please sign in to comment.