Skip to content

Commit

Permalink
Enable AWS Role Assuming (#379)
Browse files Browse the repository at this point in the history
* update scripts with assume role behaviour

* update genrules to pass aws role arn
  • Loading branch information
jon-funk authored May 31, 2024
1 parent f197cd4 commit b7d0bb7
Show file tree
Hide file tree
Showing 24 changed files with 252 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ spec:
- name: AWS_ACCESS_KEY_ID
workspaceKey: {{custom.aws_access_key_id}}
- name: AWS_SECRET_ACCESS_KEY
workspaceKey: {{custom.aws_secret_access_key}}
workspaceKey: {{custom.aws_secret_access_key}}
- name: AWS_ROLE_ARN
workspaceKey: {{custom.aws_role_arn}}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ spec:
- name: AWS_ACCESS_KEY_ID
workspaceKey: {{custom.aws_access_key_id}}
- name: AWS_SECRET_ACCESS_KEY
workspaceKey: {{custom.aws_secret_access_key}}
workspaceKey: {{custom.aws_secret_access_key}}
- name: AWS_ROLE_ARN
workspaceKey: {{custom.aws_role_arn}}
19 changes: 18 additions & 1 deletion codebundles/aws-eks-health/check_eks_cluster_health.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
#!/bin/bash
source ./auth.sh

# Environment Variables:
# AWS_REGION
auth() {
# if required AWS_ cli vars are not set, error and exit 1
if [[ -z $AWS_ACCESS_KEY_ID || -z $AWS_SECRET_ACCESS_KEY || -z $AWS_REGION ]]; then
echo "AWS credentials not set. Please set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables."
exit 1
fi
# if AWS_ROLE_ARN then assume the role using sts and override the pre-existing key ENVs
if [[ -n $AWS_ROLE_ARN ]]; then
sts_output=$(aws sts assume-role --role-arn "$AWS_ROLE_ARN" --role-session-name "AssumeRoleSession")
AWS_ACCESS_KEY_ID=$(echo "$sts_output" | jq -r '.Credentials.AccessKeyId')
AWS_SECRET_ACCESS_KEY=$(echo "$sts_output" | jq -r '.Credentials.SecretAccessKey')
AWS_SESSION_TOKEN=$(echo "$sts_output" | jq -r '.Credentials.SessionToken')
export AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY
export AWS_SESSION_TOKEN
fi
}
auth

# get list of eks clusters
eks_clusters=$(aws eks list-clusters --region $AWS_REGION --output json --query 'clusters[*]' | jq -r '.[]')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
#!/bin/bash
source ./auth.sh

# Environment Variables:
# AWS_REGION
auth() {
# if required AWS_ cli vars are not set, error and exit 1
if [[ -z $AWS_ACCESS_KEY_ID || -z $AWS_SECRET_ACCESS_KEY || -z $AWS_REGION ]]; then
echo "AWS credentials not set. Please set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables."
exit 1
fi
# if AWS_ROLE_ARN then assume the role using sts and override the pre-existing key ENVs
if [[ -n $AWS_ROLE_ARN ]]; then
sts_output=$(aws sts assume-role --role-arn "$AWS_ROLE_ARN" --role-session-name "AssumeRoleSession")
AWS_ACCESS_KEY_ID=$(echo "$sts_output" | jq -r '.Credentials.AccessKeyId')
AWS_SECRET_ACCESS_KEY=$(echo "$sts_output" | jq -r '.Credentials.SecretAccessKey')
AWS_SESSION_TOKEN=$(echo "$sts_output" | jq -r '.Credentials.SessionToken')
export AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY
export AWS_SESSION_TOKEN
fi
}
auth

# get list of eks clusters
eks_clusters=$(aws eks list-clusters --region $AWS_REGION --output json --query 'clusters[*]' | jq -r '.[]')
Expand Down
20 changes: 19 additions & 1 deletion codebundles/aws-eks-health/list_eks_fargate_metrics.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
#!/bin/bash
source ./auth.sh

# Environment Variables:
# AWS_REGION
auth() {
# if required AWS_ cli vars are not set, error and exit 1
if [[ -z $AWS_ACCESS_KEY_ID || -z $AWS_SECRET_ACCESS_KEY || -z $AWS_REGION ]]; then
echo "AWS credentials not set. Please set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables."
exit 1
fi
# if AWS_ROLE_ARN then assume the role using sts and override the pre-existing key ENVs
if [[ -n $AWS_ROLE_ARN ]]; then
sts_output=$(aws sts assume-role --role-arn "$AWS_ROLE_ARN" --role-session-name "AssumeRoleSession")
AWS_ACCESS_KEY_ID=$(echo "$sts_output" | jq -r '.Credentials.AccessKeyId')
AWS_SECRET_ACCESS_KEY=$(echo "$sts_output" | jq -r '.Credentials.SecretAccessKey')
AWS_SESSION_TOKEN=$(echo "$sts_output" | jq -r '.Credentials.SessionToken')
export AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY
export AWS_SESSION_TOKEN
fi
}
auth

METRICS_LIST="vCPU Memory CPUUtilization Duration OnDemand Spot"
START=$(date -d "1 day ago" +%s)
END=$(date +%s)
Expand Down
8 changes: 8 additions & 0 deletions codebundles/aws-eks-health/runbook.robot
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Check EKS Fargate Cluster Health Status
... env=${env}
... secret__AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
... secret__AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
... secret__AWS_ROLE_ARN=${AWS_ROLE_ARN}
IF "Error" in """${process.stdout}"""
RW.Core.Add Issue title=EKS Fargate Cluster in ${AWS_REGION} is Unhealthy
... severity=3
Expand All @@ -41,6 +42,7 @@ Check EKS Cluster Health Status
... env=${env}
... secret__AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
... secret__AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
... secret__AWS_ROLE_ARN=${AWS_ROLE_ARN}
IF "Error" in """${process.stdout}"""
RW.Core.Add Issue title=EKS Cluster in ${AWS_REGION} is Unhealthy
... severity=3
Expand All @@ -59,6 +61,7 @@ List EKS Cluster Metrics
... env=${env}
... secret__AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
... secret__AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
... secret__AWS_ROLE_ARN=${AWS_ROLE_ARN}
RW.Core.Add Pre To Report ${process.stdout}


Expand All @@ -76,10 +79,15 @@ Suite Initialization
... type=string
... description=AWS Secret Access Key
... pattern=\w*
${AWS_ROLE_ARN}= RW.Core.Import Secret AWS_ROLE_ARN
... type=string
... description=AWS Role ARN
... pattern=\w*

Set Suite Variable ${AWS_REGION} ${AWS_REGION}
Set Suite Variable ${AWS_ACCESS_KEY_ID} ${AWS_ACCESS_KEY_ID}
Set Suite Variable ${AWS_SECRET_ACCESS_KEY} ${AWS_SECRET_ACCESS_KEY}
Set Suite Variable ${AWS_ROLE_ARN} ${AWS_ROLE_ARN}


Set Suite Variable
Expand Down
10 changes: 8 additions & 2 deletions codebundles/aws-eks-health/sli.robot
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ Library Process
Suite Setup Suite Initialization

*** Tasks ***
Check EKS Fargate Cluster Health Status
Check EKS Cluster Health Status
[Documentation] This script checks the health status of an Amazon EKS cluster.
[Tags] EKS Cluster Health AWS Kubernetes Pods Nodes
${process}= RW.CLI.Run Bash File check_eks_cluster_health_status.sh
${process}= RW.CLI.Run Bash File check_eks_cluster_health.sh
... env=${env}
... secret__AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
... secret__AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
... secret__AWS_ROLE_ARN=${AWS_ROLE_ARN}
IF "Error" in """${process.stdout}"""
RW.Core.Push Metric 0
ELSE
Expand All @@ -43,10 +44,15 @@ Suite Initialization
... type=string
... description=AWS Secret Access Key
... pattern=\w*
${AWS_ROLE_ARN}= RW.Core.Import Secret AWS_ROLE_ARN
... type=string
... description=AWS Role ARN
... pattern=\w*

Set Suite Variable ${AWS_REGION} ${AWS_REGION}
Set Suite Variable ${AWS_ACCESS_KEY_ID} ${AWS_ACCESS_KEY_ID}
Set Suite Variable ${AWS_SECRET_ACCESS_KEY} ${AWS_SECRET_ACCESS_KEY}
Set Suite Variable ${AWS_ROLE_ARN} ${AWS_ROLE_ARN}

Set Suite Variable
... &{env}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ spec:
- name: AWS_ACCESS_KEY_ID
workspaceKey: {{custom.aws_access_key_id}}
- name: AWS_SECRET_ACCESS_KEY
workspaceKey: {{custom.aws_secret_access_key}}
workspaceKey: {{custom.aws_secret_access_key}}
- name: AWS_ROLE_ARN
workspaceKey: {{custom.aws_role_arn}}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ spec:
- name: AWS_ACCESS_KEY_ID
workspaceKey: {{custom.aws_access_key_id}}
- name: AWS_SECRET_ACCESS_KEY
workspaceKey: {{custom.aws_secret_access_key}}
workspaceKey: {{custom.aws_secret_access_key}}
- name: AWS_ROLE_ARN
workspaceKey: {{custom.aws_role_arn}}
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
#!/bin/bash
source ./auth.sh

# Environment Variables:
# AWS_REGION
auth() {
# if required AWS_ cli vars are not set, error and exit 1
if [[ -z $AWS_ACCESS_KEY_ID || -z $AWS_SECRET_ACCESS_KEY || -z $AWS_REGION ]]; then
echo "AWS credentials not set. Please set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables."
exit 1
fi
# if AWS_ROLE_ARN then assume the role using sts and override the pre-existing key ENVs
if [[ -n $AWS_ROLE_ARN ]]; then
sts_output=$(aws sts assume-role --role-arn "$AWS_ROLE_ARN" --role-session-name "AssumeRoleSession")
AWS_ACCESS_KEY_ID=$(echo "$sts_output" | jq -r '.Credentials.AccessKeyId')
AWS_SECRET_ACCESS_KEY=$(echo "$sts_output" | jq -r '.Credentials.SecretAccessKey')
AWS_SESSION_TOKEN=$(echo "$sts_output" | jq -r '.Credentials.SessionToken')
export AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY
export AWS_SESSION_TOKEN
fi
}
auth

# Variables
METRIC_NAMESPACE="AWS/ElastiCache"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
#!/bin/bash
source ./auth.sh

# Environment Variables:
# AWS_REGION
# REDIS_PASSWORD
auth() {
# if required AWS_ cli vars are not set, error and exit 1
if [[ -z $AWS_ACCESS_KEY_ID || -z $AWS_SECRET_ACCESS_KEY || -z $AWS_REGION ]]; then
echo "AWS credentials not set. Please set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables."
exit 1
fi
# if AWS_ROLE_ARN then assume the role using sts and override the pre-existing key ENVs
if [[ -n $AWS_ROLE_ARN ]]; then
sts_output=$(aws sts assume-role --role-arn "$AWS_ROLE_ARN" --role-session-name "AssumeRoleSession")
AWS_ACCESS_KEY_ID=$(echo "$sts_output" | jq -r '.Credentials.AccessKeyId')
AWS_SECRET_ACCESS_KEY=$(echo "$sts_output" | jq -r '.Credentials.SecretAccessKey')
AWS_SESSION_TOKEN=$(echo "$sts_output" | jq -r '.Credentials.SessionToken')
export AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY
export AWS_SESSION_TOKEN
fi
}
auth

SLOWLOG_ENTRY_LIMIT="10"

Expand Down
19 changes: 18 additions & 1 deletion codebundles/aws-elasticache-redis-health/redis_status_scan.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
#!/bin/bash
source ./auth.sh

# Environment Variables:
# AWS_REGION
auth() {
# if required AWS_ cli vars are not set, error and exit 1
if [[ -z $AWS_ACCESS_KEY_ID || -z $AWS_SECRET_ACCESS_KEY || -z $AWS_REGION ]]; then
echo "AWS credentials not set. Please set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables."
exit 1
fi
# if AWS_ROLE_ARN then assume the role using sts and override the pre-existing key ENVs
if [[ -n $AWS_ROLE_ARN ]]; then
sts_output=$(aws sts assume-role --role-arn "$AWS_ROLE_ARN" --role-session-name "AssumeRoleSession")
AWS_ACCESS_KEY_ID=$(echo "$sts_output" | jq -r '.Credentials.AccessKeyId')
AWS_SECRET_ACCESS_KEY=$(echo "$sts_output" | jq -r '.Credentials.SecretAccessKey')
AWS_SESSION_TOKEN=$(echo "$sts_output" | jq -r '.Credentials.SessionToken')
export AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY
export AWS_SESSION_TOKEN
fi
}
auth

# Variables
serverless_caches=$(aws elasticache describe-serverless-caches --region "$AWS_REGION")
Expand Down
6 changes: 6 additions & 0 deletions codebundles/aws-elasticache-redis-health/runbook.robot
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Scan AWS Elasticache Redis Status
... env=${env}
... secret__AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
... secret__AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
... secret__AWS_ROLE_ARN=${AWS_ROLE_ARN}
RW.Core.Add Pre To Report ${process.stdout}
IF "Snapshot retention limit is set to 0" in """${process.stdout}"""
RW.Core.Add Issue title=Snapshots not configured for Elasticache in region ${AWS_REGION}
Expand Down Expand Up @@ -57,11 +58,16 @@ Suite Initialization
... type=string
... description=AWS Secret Access Key
... pattern=\w*
${AWS_ROLE_ARN}= RW.Core.Import Secret AWS_ROLE_ARN
... type=string
... description=AWS Role ARN
... pattern=\w*


Set Suite Variable ${AWS_REGION} ${AWS_REGION}
Set Suite Variable ${AWS_ACCESS_KEY_ID} ${AWS_ACCESS_KEY_ID}
Set Suite Variable ${AWS_SECRET_ACCESS_KEY} ${AWS_SECRET_ACCESS_KEY}
Set Suite Variable ${AWS_ROLE_ARN} ${AWS_ROLE_ARN}


Set Suite Variable
Expand Down
6 changes: 6 additions & 0 deletions codebundles/aws-elasticache-redis-health/sli.robot
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Scan ElastiCaches
... env=${env}
... secret__AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
... secret__AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
... secret__AWS_ROLE_ARN=${AWS_ROLE_ARN}
Log ${process.stdout}
Log ${process.stderr}
IF ${process.rc} != 0
Expand All @@ -46,10 +47,15 @@ Suite Initialization
... type=string
... description=AWS Secret Access Key
... pattern=\w*
${AWS_ROLE_ARN}= RW.Core.Import Secret AWS_ROLE_ARN
... type=string
... description=AWS Role ARN
... pattern=\w*

Set Suite Variable ${AWS_REGION} ${AWS_REGION}
Set Suite Variable ${AWS_ACCESS_KEY_ID} ${AWS_ACCESS_KEY_ID}
Set Suite Variable ${AWS_SECRET_ACCESS_KEY} ${AWS_SECRET_ACCESS_KEY}
Set Suite Variable ${AWS_ROLE_ARN} ${AWS_ROLE_ARN}

Set Suite Variable
... &{env}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ spec:
- name: AWS_ACCESS_KEY_ID
workspaceKey: {{custom.aws_access_key_id}}
- name: AWS_SECRET_ACCESS_KEY
workspaceKey: {{custom.aws_secret_access_key}}
workspaceKey: {{custom.aws_secret_access_key}}
- name: AWS_ROLE_ARN
workspaceKey: {{custom.aws_role_arn}}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ spec:
- name: AWS_ACCESS_KEY_ID
workspaceKey: {{custom.aws_access_key_id}}
- name: AWS_SECRET_ACCESS_KEY
workspaceKey: {{custom.aws_secret_access_key}}
workspaceKey: {{custom.aws_secret_access_key}}
- name: AWS_ROLE_ARN
workspaceKey: {{custom.aws_role_arn}}
20 changes: 18 additions & 2 deletions codebundles/aws-lambda-health/analyze_lambda_invocation_errors.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
#!/bin/bash
source ./auth.sh

# Environment Variables:
#AWS_REGION

auth() {
# if required AWS_ cli vars are not set, error and exit 1
if [[ -z $AWS_ACCESS_KEY_ID || -z $AWS_SECRET_ACCESS_KEY || -z $AWS_REGION ]]; then
echo "AWS credentials not set. Please set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables."
exit 1
fi
# if AWS_ROLE_ARN then assume the role using sts and override the pre-existing key ENVs
if [[ -n $AWS_ROLE_ARN ]]; then
sts_output=$(aws sts assume-role --role-arn "$AWS_ROLE_ARN" --role-session-name "AssumeRoleSession")
AWS_ACCESS_KEY_ID=$(echo "$sts_output" | jq -r '.Credentials.AccessKeyId')
AWS_SECRET_ACCESS_KEY=$(echo "$sts_output" | jq -r '.Credentials.SecretAccessKey')
AWS_SESSION_TOKEN=$(echo "$sts_output" | jq -r '.Credentials.SessionToken')
export AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY
export AWS_SESSION_TOKEN
fi
}
auth

SINCE="24h"

Expand Down
20 changes: 18 additions & 2 deletions codebundles/aws-lambda-health/list_lambda_runtimes.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
#!/bin/bash
source ./auth.sh

auth() {
# if required AWS_ cli vars are not set, error and exit 1
if [[ -z $AWS_ACCESS_KEY_ID || -z $AWS_SECRET_ACCESS_KEY || -z $AWS_REGION ]]; then
echo "AWS credentials not set. Please set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables."
exit 1
fi
# if AWS_ROLE_ARN then assume the role using sts and override the pre-existing key ENVs
if [[ -n $AWS_ROLE_ARN ]]; then
sts_output=$(aws sts assume-role --role-arn "$AWS_ROLE_ARN" --role-session-name "AssumeRoleSession")
AWS_ACCESS_KEY_ID=$(echo "$sts_output" | jq -r '.Credentials.AccessKeyId')
AWS_SECRET_ACCESS_KEY=$(echo "$sts_output" | jq -r '.Credentials.SecretAccessKey')
AWS_SESSION_TOKEN=$(echo "$sts_output" | jq -r '.Credentials.SessionToken')
export AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY
export AWS_SESSION_TOKEN
fi
}
auth

# Fetch all Lambda function names
function_names=$(aws lambda list-functions --query 'Functions[*].FunctionName' --output text)
Expand Down
Loading

0 comments on commit b7d0bb7

Please sign in to comment.