Come faccio a utilizzare la funzionalità dei ruoli IAM per gli account di servizio (IRSA) con Amazon EKS per limitare l'accesso a un bucket Amazon S3?

5 minuti di lettura
0

Voglio limitare l'accesso a un bucket Amazon Simple Storage Service (Amazon S3) a livello di pod in Amazon Elastic Kubernetes Service (Amazon EKS). Voglio inoltre mantenere i privilegi minimi per la mia applicazione con i ruoli AWS Identity and Access Management (IAM) per gli account di servizio (IRSA).

Risoluzione

**Importante:**Prima di utilizzare IRSA con Amazon EKS, devi creare un provider OIDC IAM per il tuo cluster.

Creazione di una policy e di un ruolo IAM

1.    Crea un file JSON denominato iam-policy.json.

La seguente policy di esempio limita le autorizzazioni di Amazon S3 e Amazon DynamoDB. Gli utenti IAM possono accedere a un bucket S3 e a una tabella DynamoDB specifica.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "ListAndDescribe",
            "Effect": "Allow",
            "Action": [
                "dynamodb:List*",
                "dynamodb:Describe*"
            ],
            "Resource": "arn:aws:dynamodb:*:*:table/YOUR_TABLE"
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:ListBucket"
            ],
            "Resource": "arn:aws:s3:::YOUR_BUCKET"
        },
        {
            "Sid": "List",
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:GetObjectVersion"
            ],
            "Resource": "arn:aws:s3:::YOUR_BUCKET/*"
        }
    ]
}

Nota: Sostituisci YOUR_TABLE con la tua tabella. Sostituisci YOUR\ _NAMESPACE con il tuo spazio dei nomi.

2.    Crea una policy IAM denominata YOUR-IAM-POLICY.

$ aws iam create-policy \
    --policy-name YOUR-IAM-POLICY \
    --policy-document file://iam-policy.json

**Nota:**Sostituisci YOUR-IAM-POLICY con il nome della tua policy.

3.    Usa la console IAM per creare un ruolo IAM per il tuo account di servizio, quindi annota un account di servizio con quel ruolo IAM. Oppure, usa eksctl per creare un ruolo IAM per il tuo account di servizio. Ad esempio:

$ eksctl create iamserviceaccount \
  --name YOUR-SERVICEACCOUNT \
  --namespace YOUR-NAMESPACE \
  --cluster YOUR-CLUSTER \
  --attach-policy-arn arn:aws:iam::1111122222:policy/YOUR-IAM-POLICY \
  --approve

**Nota:**Sostituisci 1111122222 il tuo Amazon Resource Name (ARN). Puoi anche creare un ruolo IAM per il tuo account di servizio utilizzando la console IAM.

Creazione di un pod Amazon EKS

Nei passaggi seguenti, sostituisci la tua applicazione con un'immagine aws-cli. Ciò consente di verificare se il pod può assumere il ruolo IAM specificato con le autorizzazioni IAM corrette.

1.    Crea un file YAML denominato aws-cli-pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: aws-cli
  namespace: YOUR_NAMESPACE
spec:
  serviceAccountName: YOUR_SERVICEACCOUNT
  containers:
  - name: aws-cli
    image: amazon/aws-cli:latest
    command:
      - sleep
      - "3600"
    imagePullPolicy: IfNotPresent
  restartPolicy: Always

Nota: Sostituisci YOUR\ _NAMESPACE con il tuo spazio dei nomi. Sostituisci SERVICE_ACCOUNT con il nome del tuo account di servizio Kubernetes.

2.    Crea un pod Amazon EKS:

$ kubectl apply -f ./aws-cli.yaml

Test del tuo pod Amazon EKS

Verifica che il tuo pod utilizzi il ruolo IAM corretto con azioni limite per Amazon S3 e DynamoDB. Nell'esempio seguente, il pod può elencare solo il bucket S3 (YOUR\ _BUCKET) e la tabella DynamoDB (YOUR\ _TABLE).

1.    Trova il ruolo IAM che utilizza le credenziali:

$ kubectl exec -it aws-cli -- aws sts get-caller-identity

L'output sarà simile a:

{
    "UserId": "AIDACKCEVSQ6C2EXAMPLE:botocore-session-1111122222",
    "Account": "111122223333",
    "Arn": "arn:aws:sts::111122223333:assumed-role/eksctl-EKS-LAB-addon-iamserviceaccount-defau-Role1-ASA1UEXAMPLE/botocore-session-1111122222"
}

Se specifichi uno spazio dei nomi, aggiungi l'argomento dello spazio dei nomi (-n) a tutti i comandi kubectl. Sostituisci YOUR_NAMESPACE con il tuo spazio dei nomi. 

$ kubectl -n YOUR_NAMESPACE exec -it aws-cli -- aws sts get-caller-identity

2.    Verifica che il tuo pod disponga delle autorizzazioni s3:ListBuckets per il tuo bucket S3 (YOUR_BUCKET):

$ kubectl exec -it aws-cli -- aws s3 ls s3://YOUR_BUCKET

Nota: Sostituisci YOUR_BUCKET con il tuo bucket S3.

Output:

2021-09-28 09:59:22        269 demo-test-file

3.    Verifica che il tuo pod non possa eliminare il bucket S3 ( YOUR_BUCKET):

$ kubectl exec -it aws-cli -- aws s3 rm s3://YOUR_BUCKET/demo-test-file

Nota: Sostituisci YOUR_BUCKET con il tuo bucket S3.

Il comando restituisce il seguente errore "Accesso negato" perché il pod non dispone delle autorizzazioni s3:DeleteObject:

delete failed: s3://YOUR_BUCKET/demo-test-file An error occurred (AccessDenied) when calling the DeleteObject operation: Access Denied
command terminated with exit code 1

4.    Verifica che il tuo pod disponga delle autorizzazioni dynamodb:List per la tua tabella DynamoDB (YOUR_TABLE):

$ kubectl exec -it aws-cli -- aws dynamodb describe-table --table-name YOUR_TABLE

Nota: Sostituisci YOUR_TABLE con la tua tabella DynamoDB.

Output:

{
    "Table": {
        "AttributeDefinitions": [
            {
                "AttributeName": "demo",
                "AttributeType": "S"
            }
        ],
        "TableName": "YOUR_TABLE",
        "KeySchema": [
            {
                "AttributeName": "demo",
                "KeyType": "HASH"
            }
        ],
        "TableStatus": "ACTIVE",
        "CreationDateTime": "2021-09-28T10:05:53.599000+00:00",
        "ProvisionedThroughput": {
            "NumberOfDecreasesToday": 0,
            "ReadCapacityUnits": 5,
            "WriteCapacityUnits": 5
        },
        "TableSizeBytes": 0,
        "ItemCount": 0,
        "TableArn": "arn:aws:dynamodb:eu-west-1:000000000000:table/YOUR_TABLE",
        "TableId": "42bd1238-e042-4016-b6b2-77548939c101"
    }
}

5.    Verifica che il tuo pod non possa eliminare la tabella DynamoDB ( YOUR_TABLE):

$ kubectl exec -it aws-cli -- aws dynamodb delete-table --table-name YOUR_TABLE

Nota: Sostituisci YOUR_TABLE con la tua tabella DynamoDB.

Il comando restituisce il seguente errore "Accesso negato" perché il pod non dispone delle autorizzazioni dynamodb:DeleteTable:

An error occurred (AccessDeniedException) when calling the DeleteTable operation: User: arn:aws:sts::1111122222:assumed-role/eksctl-EKS-LAB-addon-iamserviceaccount-defau-Role1-ASA1U7NRNSEC/botocore-session-1632822777 is not authorized to perform: dynamodb:DeleteTable on resource: arn:aws:dynamodb:eu-west-1: 1111122222:table/MyTable
command terminated with exit code 254

AWS UFFICIALE
AWS UFFICIALEAggiornata 2 anni fa