Skip to content

Commit

Permalink
HashiTalks 2025 - Zero Trust Security using Hashi Vault and AWS IAM
Browse files Browse the repository at this point in the history
  • Loading branch information
vinod827 committed Feb 19, 2025
1 parent cfaf2d1 commit 937b63e
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
6 changes: 6 additions & 0 deletions iac/demo/zerotrust/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM amazon/aws-cli:latest

Check warning on line 1 in iac/demo/zerotrust/Dockerfile

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

iac/demo/zerotrust/Dockerfile#L1

Using latest is prone to errors if the image will ever update. Pin the version explicitly to a release tag

# Install jq
RUN yum update -y && \
yum install -y jq && \
yum clean all
Binary file added iac/demo/zerotrust/hashitalks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions iac/demo/zerotrust/vault-acl-policy.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
path "aws/creds/dev-role" {
capabilities = ["read"]
}

25 changes: 25 additions & 0 deletions iac/demo/zerotrust/vault-aws-config.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Enable AWS secrets engine
path "sys/mounts/aws" {
capabilities = ["create", "update"]
}

# Configure AWS secrets engine with root credentials
path "aws/config/root" {
capabilities = ["create", "update"]
data = {
access_key = "<ACCESS_KEY>"
secret_key = "<SECRET_ACCESS_KEY>"
region = "us-east-1"
}
}

# Create a Vault AWS role that generates IAM user credentials
path "aws/roles/dev-role" {
capabilities = ["create", "update"]
data = {
credential_type = "iam_user"
policy_arn = "<IAM Policy ARN>"
max_ttl = "24h"
ttl = "1h"
}
}
95 changes: 95 additions & 0 deletions iac/demo/zerotrust/vault-s3-uploader.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: vault-s3-uploader-config
namespace: vault
data:
VAULT_ADDR: "http://vault.vault.svc.cluster.local:8200"
VAULT_ROLE: "dev-role"
S3_BUCKET: "hashitalks2025-zerotrust"
S3_REGION: "us-east-1"
---
apiVersion: v1

Check warning on line 12 in iac/demo/zerotrust/vault-s3-uploader.yaml

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

iac/demo/zerotrust/vault-s3-uploader.yaml#L12

Apply security context to your containers

Check warning on line 12 in iac/demo/zerotrust/vault-s3-uploader.yaml

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

iac/demo/zerotrust/vault-s3-uploader.yaml#L12

Apply security context to your pods and containers

Check warning on line 12 in iac/demo/zerotrust/vault-s3-uploader.yaml

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

iac/demo/zerotrust/vault-s3-uploader.yaml#L12

Containers should not run with allowPrivilegeEscalation

Check warning on line 12 in iac/demo/zerotrust/vault-s3-uploader.yaml

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

iac/demo/zerotrust/vault-s3-uploader.yaml#L12

Containers should run as a high UID to avoid host conflict

Check warning on line 12 in iac/demo/zerotrust/vault-s3-uploader.yaml

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

iac/demo/zerotrust/vault-s3-uploader.yaml#L12

Ensure that Service Account Tokens are only mounted where necessary

Check warning on line 12 in iac/demo/zerotrust/vault-s3-uploader.yaml

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

iac/demo/zerotrust/vault-s3-uploader.yaml#L12

Ensure that the seccomp profile is set to docker/default or runtime/default

Check warning on line 12 in iac/demo/zerotrust/vault-s3-uploader.yaml

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

iac/demo/zerotrust/vault-s3-uploader.yaml#L12

Image Pull Policy should be Always

Check warning on line 12 in iac/demo/zerotrust/vault-s3-uploader.yaml

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

iac/demo/zerotrust/vault-s3-uploader.yaml#L12

Image should use digest

Check warning on line 12 in iac/demo/zerotrust/vault-s3-uploader.yaml

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

iac/demo/zerotrust/vault-s3-uploader.yaml#L12

Liveness Probe Should be Configured

Check warning on line 12 in iac/demo/zerotrust/vault-s3-uploader.yaml

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

iac/demo/zerotrust/vault-s3-uploader.yaml#L12

Minimize the admission of containers with capabilities assigned

Check warning on line 12 in iac/demo/zerotrust/vault-s3-uploader.yaml

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

iac/demo/zerotrust/vault-s3-uploader.yaml#L12

Minimize the admission of containers with the NET_RAW capability

Check warning on line 12 in iac/demo/zerotrust/vault-s3-uploader.yaml

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

iac/demo/zerotrust/vault-s3-uploader.yaml#L12

Minimize the admission of root containers

Check warning on line 12 in iac/demo/zerotrust/vault-s3-uploader.yaml

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

iac/demo/zerotrust/vault-s3-uploader.yaml#L12

Readiness Probe Should be Configured

Check warning on line 12 in iac/demo/zerotrust/vault-s3-uploader.yaml

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

iac/demo/zerotrust/vault-s3-uploader.yaml#L12

Use read-only filesystem for containers where possible
kind: Pod
metadata:
name: vault-s3-uploader
namespace: vault
labels:
app: vault-s3-uploader
spec:
serviceAccountName: vault-sa # Bind to the service account
imagePullSecrets:
- name: ecr-secret # Reference the secret for pulling ECR images
containers:
- name: s3-uploader
image: 730335385934.dkr.ecr.us-east-1.amazonaws.com/hashitalks2025:v1.0.0
resources:
limits:
memory: "128Mi"
cpu: "500m"
requests:
memory: "64Mi"
cpu: "250m"
env:
- name: VAULT_ADDR
valueFrom:
configMapKeyRef:
name: vault-s3-uploader-config
key: VAULT_ADDR
- name: VAULT_ROLE
valueFrom:
configMapKeyRef:
name: vault-s3-uploader-config
key: VAULT_ROLE
- name: S3_BUCKET
valueFrom:
configMapKeyRef:
name: vault-s3-uploader-config
key: S3_BUCKET
command: ["/bin/sh", "-c"]
args:
- |
echo "Retrieving AWS credentials from Vault..."
VAULT_PATH="aws/creds/${VAULT_ROLE}"
# Get JWT token from Kubernetes service account
K8S_TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
# Authenticate with Vault using Kubernetes auth
VAULT_RESPONSE=$(curl -s --request POST --data "{\"jwt\": \"${K8S_TOKEN}\", \"role\": \"${VAULT_ROLE}\"}" ${VAULT_ADDR}/v1/auth/kubernetes/login)
VAULT_TOKEN=$(echo "$VAULT_RESPONSE" | jq -r '.auth.client_token')
# Get temporary AWS credentials (IAM User)
CREDS=$(curl -s --header "X-Vault-Token: ${VAULT_TOKEN}" ${VAULT_ADDR}/v1/${VAULT_PATH})
AWS_ACCESS_KEY_ID=$(echo "$CREDS" | jq -r '.data.access_key')
AWS_SECRET_ACCESS_KEY=$(echo "$CREDS" | jq -r '.data.secret_key')
if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
echo "Failed to retrieve AWS credentials from Vault. Exiting..."
exit 1
fi
# Export credentials for AWS CLI
export AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY
echo "AWS credentials retrieved successfully."
sleep 4
# Creating a dummy file
echo "Hello everyone! Welcome to the HashiTalks 2025" > hashitalks.txt
aws s3 cp hashitalks.txt s3://$S3_BUCKET/
echo "Upload completed!"
volumeMounts:
- name: sa-token
mountPath: /var/run/secrets/kubernetes.io/serviceaccount
readOnly: true
volumes:
- name: sa-token
projected:
sources:
- serviceAccountToken:
path: token
expirationSeconds: 600
audience: vault
restartPolicy: Never

0 comments on commit 937b63e

Please sign in to comment.