-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from caktus/aws-s3-iam-role
Amazon S3: buckets with IAM role for K8s service accounts
- Loading branch information
Showing
7 changed files
with
205 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.vscode | ||
.python-version |
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
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,107 @@ | ||
--- | ||
|
||
# | ||
# Public S3 assets bucket | ||
# | ||
|
||
- name: "create public bucket {{ k8s_s3_public_bucket }}" | ||
s3_bucket: | ||
name: "{{ k8s_s3_public_bucket }}" | ||
state: present | ||
versioning: yes | ||
region: "{{ k8s_s3_region }}" | ||
|
||
# | ||
# Private S3 assets bucket | ||
# | ||
|
||
- name: "create private bucket {{ k8s_s3_private_bucket }}" | ||
s3_bucket: | ||
name: "{{ k8s_s3_private_bucket }}" | ||
state: present | ||
versioning: yes | ||
region: "{{ k8s_s3_region }}" | ||
encryption: AES256 | ||
|
||
# Not available via Ansible module as of 7/2020 | ||
- name: "block all public access to {{ k8s_s3_private_bucket }}" | ||
command: | ||
argv: | ||
- aws | ||
- s3api | ||
- put-public-access-block | ||
- --bucket | ||
- "{{ k8s_s3_private_bucket }}" | ||
- --public-access-block-configuration | ||
- BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true | ||
|
||
# | ||
# IAM OIDC identity provider and issuer | ||
# | ||
|
||
# https://docs.aws.amazon.com/eks/latest/userguide/enable-iam-roles-for-service-accounts.html | ||
# Possibly replace in future with (to remove eksctl requirement): | ||
# 1. https://docs.aws.amazon.com/cli/latest/reference/iam/create-open-id-connect-provider.html | ||
# 2. https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_create_oidc_verify-thumbprint.html | ||
- name: create an IAM OIDC identity provider for the cluster | ||
command: "eksctl utils associate-iam-oidc-provider --cluster {{ k8s_s3_cluster_name }} --approve" | ||
register: associate_response | ||
changed_when: "'created' in associate_response.stdout" | ||
|
||
# Not available via Ansible module as of 7/2020 | ||
- name: describe cluster to obtain OIDC issuer | ||
command: "aws eks describe-cluster --region {{ k8s_s3_region }} --name {{ k8s_s3_cluster_name }} --output json" | ||
changed_when: false | ||
register: cluster_query | ||
|
||
- name: parse OIDC issuer from response | ||
set_fact: | ||
oidc_issuer: "{{ cluster_query.stdout | from_json | json_query('cluster.identity.oidc.issuer') | regex_replace('https://') }}" | ||
|
||
# | ||
# AWS Account ID | ||
# | ||
|
||
- name: get the current caller identity information | ||
aws_caller_info: | ||
register: caller_info | ||
|
||
- name: parse AWS account ID | ||
set_fact: | ||
aws_account_id: "{{ caller_info.account }}" | ||
|
||
# | ||
# IAM Role | ||
# | ||
|
||
# https://docs.aws.amazon.com/eks/latest/userguide/create-service-account-iam-policy-and-role.html | ||
- name: Create IAM role for K8s service account | ||
iam_role: | ||
name: "{{ k8s_s3_iam_role }}" | ||
assume_role_policy_document: "{{ lookup('template', 's3/TrustPolicy.json.j2') }}" | ||
description: IAM role for K8s service account | ||
|
||
- name: Attach inline policy to user | ||
iam_policy: | ||
iam_type: role | ||
iam_name: "{{ k8s_s3_iam_role }}" | ||
policy_name: "EKSBucketPolicy" | ||
state: present | ||
policy_json: "{{ lookup( 'template', 's3/AssetManagementPolicy.json.j2') }}" | ||
|
||
# | ||
# Service Account | ||
# | ||
|
||
# https://docs.aws.amazon.com/eks/latest/userguide/specify-service-account-role.html | ||
- name: "Associate IAM role with the service account in your cluster" | ||
k8s: | ||
state: present | ||
definition: | ||
apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
name: "{{ k8s_s3_serviceaccount }}" | ||
namespace: "{{ k8s_s3_namespace }}" | ||
annotations: | ||
eks.amazonaws.com/role-arn: "arn:aws:iam::{{ aws_account_id }}:role/{{ k8s_s3_iam_role }}" |
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,25 @@ | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"s3:ListBucket" | ||
], | ||
"Resource": [ | ||
"arn:aws:s3:::{{ k8s_s3_public_bucket }}", | ||
"arn:aws:s3:::{{ k8s_s3_private_bucket }}" | ||
] | ||
}, | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"s3:*" | ||
], | ||
"Resource": [ | ||
"arn:aws:s3:::{{ k8s_s3_public_bucket }}/*", | ||
"arn:aws:s3:::{{ k8s_s3_private_bucket }}/*" | ||
] | ||
} | ||
] | ||
} |
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,17 @@ | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Federated": "arn:aws:iam::{{ aws_account_id }}:oidc-provider/{{ oidc_issuer }}" | ||
}, | ||
"Action": "sts:AssumeRoleWithWebIdentity", | ||
"Condition": { | ||
"StringEquals": { | ||
"{{ oidc_issuer }}:sub": "system:serviceaccount:{{ k8s_s3_namespace }}:{{ k8s_s3_serviceaccount }}" | ||
} | ||
} | ||
} | ||
] | ||
} |
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