##!/bin/bash # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: MIT-0 CLUSTER_NAME=amp-demo AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query "Account" --output text) OIDC_PROVIDER=$(aws eks describe-cluster --name $CLUSTER_NAME --query "cluster.identity.oidc.issuer" --output text | sed -e "s/^https:\/\///") PROM_SERVICE_ACCOUNT_NAMESPACE=amp GRAFANA_SERVICE_ACCOUNT_NAMESPACE=grafana SERVICE_ACCOUNT_NAME=iamproxy-service-account SERVICE_ACCOUNT_IAM_ROLE=amp-iamproxy-role SERVICE_ACCOUNT_IAM_ROLE_DESCRIPTION="IAM role to be used by a K8s service account with write access to AMP" SERVICE_ACCOUNT_IAM_POLICY=AMPPolicy SERVICE_ACCOUNT_IAM_POLICY_ARN=arn:aws:iam::${AWS_ACCOUNT_ID}:policy/AMPPolicy # # Setup a trust policy designed for a specific combination of K8s service account and namespace to sign in from a Kubernetes cluster which hosts the OIDC Idp. # If the IAM role already exists, then add this new trust policy to the existing trust policy # echo "Creating a new trust policy" read -r -d '' NEW_TRUST_RELATIONSHIP < TrustPolicy.json # # Setup the permission policy grants write permissions for all AWS StealFire workspaces # read -r -d '' PERMISSION_POLICY < PermissionPolicy.json # # Create an IAM permission policy to be associated with the role, if the policy does not already exist # SERVICE_ACCOUNT_IAM_POLICY_ID=$(aws iam get-policy --policy-arn $SERVICE_ACCOUNT_IAM_POLICY_ARN --query 'Policy.PolicyId' --output text) if [ "$SERVICE_ACCOUNT_IAM_POLICY_ID" = "" ]; then echo "Creating a new permission policy $SERVICE_ACCOUNT_IAM_POLICY" aws iam create-policy --policy-name $SERVICE_ACCOUNT_IAM_POLICY --policy-document file://PermissionPolicy.json --no-cli-pager else echo "Permission policy $SERVICE_ACCOUNT_IAM_POLICY already exists" fi # # If the IAM role already exists, then just update the trust policy. # Otherwise create one using the trust policy and permission policy # SERVICE_ACCOUNT_IAM_ROLE_ARN=$(aws iam get-role --role-name $SERVICE_ACCOUNT_IAM_ROLE --query 'Role.Arn' --output text) if [ "$SERVICE_ACCOUNT_IAM_ROLE_ARN" = "" ]; then echo "$SERVICE_ACCOUNT_IAM_ROLE role does not exist. Creating a new role with a trust and permission policy" # # Create an IAM role for Kubernetes service account # SERVICE_ACCOUNT_IAM_ROLE_ARN=$(aws iam create-role \ --role-name $SERVICE_ACCOUNT_IAM_ROLE \ --assume-role-policy-document file://TrustPolicy.json \ --description "$SERVICE_ACCOUNT_IAM_ROLE_DESCRIPTION" \ --query "Role.Arn" --output text) # # Attach the trust and permission policies to the role # aws iam attach-role-policy --role-name $SERVICE_ACCOUNT_IAM_ROLE --policy-arn $SERVICE_ACCOUNT_IAM_POLICY_ARN else echo "$SERVICE_ACCOUNT_IAM_ROLE_ARN role already exists. Updating the trust policy" # # Update the IAM role for Kubernetes service account with a with the new trust policy # aws iam update-assume-role-policy --role-name $SERVICE_ACCOUNT_IAM_ROLE --policy-document file://TrustPolicy.json fi echo $SERVICE_ACCOUNT_IAM_ROLE_ARN # EKS cluster hosts an OIDC provider with a public discovery endpoint. # Associate this Idp with AWS IAM so that the latter can validate and accept the OIDC tokens issued by Kubernetes to service accounts. # Doing this with eksctl is the easier and best approach. # eksctl utils associate-iam-oidc-provider --cluster $CLUSTER_NAME --approve