Skip to content

Commit b22ba2d

Browse files
committed
Update AWS Organizations config generation to append Steampipe connections instead of overwriting
1 parent 014e844 commit b22ba2d

2 files changed

Lines changed: 31 additions & 28 deletions

File tree

all/aws-organizations-scripts/README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ Please refer to the [documentation on steampipe.io](https://steampipe.io/docs/gu
1212

1313
* **[generate_config_for_cross_account_roles.sh](https://github.com/turbot/steampipe-samples/tree/main/all/aws-organizations-scripts/generate_config_for_cross_account_roles.sh)**\
1414
This script can be used to generate the aws config file and steampipe aws.spc files for a single AWS Organization. Usage is:
15-
`./generate_config_for_cross_account_roles.sh [IMDS | LOCAL ] <AUDITROLE> <AWS_CONFIG_FILE> <SOURCE_PROFILE>`, where:
16-
`IMDS` if you're running in EC2.
17-
`LOCAL` if you're running from the local machine.
18-
`AUDITROLE` is the name fo the [cross-account role](https://docs.aws.amazon.com/IAM/latest/UserGuide/tutorial_cross-account-with-roles.html) created in all accounts.
19-
`AWS_CONFIG_FILE` is where the script will output the AWS SDK profiles
20-
`SOURCE_PROFILE` is only required when `LOCAL` is specified. It is the profile with the local credentials used to perform the [AssumeRole](https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html) on the cross-account role.
15+
`./generate_config_for_cross_account_roles.sh [IMDS | LOCAL ] <AUDIT_ROLE> <AWS_CONFIG_FILE> <SOURCE_PROFILE>`, where:
16+
* `IMDS` if you're running in EC2.
17+
* `LOCAL` if you're running from the local machine.
18+
* `AUDIT_ROLE` is the name fo the [cross-account role](https://docs.aws.amazon.com/IAM/latest/UserGuide/tutorial_cross-account-with-roles.html) created in all accounts.
19+
* `AWS_CONFIG_FILE` is where the script will output the AWS SDK profiles
20+
* `SOURCE_PROFILE` is only required when `LOCAL` is specified. It is the profile with the local credentials used to perform the [AssumeRole](https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html) on the cross-account role.
2121

2222
* **[generate_config_for_multipayer.py](https://github.com/turbot/steampipe-samples/tree/main/all/aws-organizations-scripts/generate_config_for_multipayer.py)**\
2323
This script takes a list of AWS Management Accounts, and uses the specified `--rolename` to AssumeRole into the management account, list the child accounts, and build an AWS Config File and aws.spc file. Usage is:
@@ -29,4 +29,3 @@ Please refer to the [documentation on steampipe.io](https://steampipe.io/docs/gu
2929
--payers PAYERS [PAYERS ...]
3030
[--role-session-name ROLE_SESSION_NAME]
3131
```
32-

all/aws-organizations-scripts/generate_config_for_cross_account_roles.sh

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,25 @@
33
#
44
# generate_config_for_cross_account_roles.sh
55
#
6-
# Iterates across a list of AWS Accounts from `aws organizations list-accounts` to:
7-
# 1. Generate an entry in the AWS_CONFIG_FILE specified on the command line
8-
# 2. Generate an associated connection entry in the aws.spc file for steampipe CLI
6+
# Iterates across a list of AWS accounts from `aws organizations list-accounts` to:
7+
# 1. Generate a profile entry in the AWS_CONFIG_FILE specified on the command line
8+
# 2. Append a Steampipe connection entry in the aws.spc file
99
#
10-
# Note: you will need to execute this script with permissions form the AWS Organizations Management Account
11-
# or any AWS account that is configured as a Delegated Administrator for an AWS Organizations
12-
# service (like GuardDuty or IAM Access Analyzer)
10+
# Note: you will need to execute this script with permissions form the AWS
11+
# Organizations management account or any AWS account that is configured as a
12+
# delegated administrator for an AWS Organizations service (like GuardDuty or
13+
# IAM Access Analyzer)
1314
#
1415

1516
set -e
1617

1718
COMMAND=$1
18-
AUDITROLE=$2
19+
AUDIT_ROLE=$2
1920
AWS_CONFIG_FILE=$3
2021
SOURCE_PROFILE=$4
2122

2223
usage () {
23-
echo "Usage: $0 [IMDS | ECS | LOCAL ] <AUDITROLE> <AWS_CONFIG_FILE> <SOURCE_PROFILE>"
24+
echo "Usage: $0 [IMDS | ECS | LOCAL ] <AUDIT_ROLE> <AWS_CONFIG_FILE> <SOURCE_PROFILE>"
2425
exit 1
2526
}
2627

@@ -30,7 +31,7 @@ if [ -z "$COMMAND" ] ; then
3031
fi
3132

3233
if [ $COMMAND != "IMDS" ] && [ $COMMAND != "ECS" ] && [ $COMMAND != "LOCAL" ] ; then
33-
echo "ERROR: Invalid Command: $COMMAND"
34+
echo "ERROR: Invalid command: $COMMAND"
3435
usage
3536
fi
3637

@@ -50,26 +51,28 @@ fi
5051

5152
# STEAMPIPE_INSTALL_DIR overrides the default steampipe directory of ~/.steampipe
5253
if [ -z $STEAMPIPE_INSTALL_DIR ] ; then
53-
echo "STEAMPIPE_INSTALL_DIR not defined. Using the default."
54+
echo "STEAMPIPE_INSTALL_DIR not defined, using the default location"
5455
export STEAMPIPE_INSTALL_DIR=~/.steampipe
5556
fi
5657

5758
if [ ! -d $STEAMPIPE_INSTALL_DIR ] ; then
58-
echo "STEAMPIPE_INSTALL_DIR: $STEAMPIPE_INSTALL_DIR doesn't exist. Creating it."
59+
echo "STEAMPIPE_INSTALL_DIR: $STEAMPIPE_INSTALL_DIR doesn't exist, creating it"
5960
mkdir -p ${STEAMPIPE_INSTALL_DIR}/config/
6061
fi
6162

6263
if [ -f $AWS_CONFIG_FILE ] ; then
63-
echo "$AWS_CONFIG_FILE exists. Aborting rather than overwriting a critical file."
64+
echo "$AWS_CONFIG_FILE exists, aborting rather than overwriting a critical file"
6465
exit 1
6566
fi
6667

6768
SP_CONFIG_FILE=${STEAMPIPE_INSTALL_DIR}/config/aws.spc
6869
ALL_REGIONS='["*"]'
6970

70-
echo "Creating Steampipe Connections in $SP_CONFIG_FILE and AWS Profiles in $AWS_CONFIG_FILE"
71-
echo "# Automatically Generated at `date`" > $SP_CONFIG_FILE
72-
echo "# Steampipe profiles, Automatically Generated at `date`" > $AWS_CONFIG_FILE
71+
echo "Generating AWS profiles in $AWS_CONFIG_FILE"
72+
echo "# Steampipe profiles, automatically generated at `date`" > $AWS_CONFIG_FILE
73+
74+
echo "Appending Steampipe connections in $SP_CONFIG_FILE"
75+
echo "# Automatically generated at `date`" >> $SP_CONFIG_FILE
7376

7477
if [ $COMMAND == "IMDS" ] || [ $COMMAND == "ECS" ] ; then
7578
# Your AWS Config file needs a [default] section
@@ -81,7 +84,7 @@ fi
8184
8285
cat <<EOF>>$SP_CONFIG_FILE
8386
84-
# Create an aggregator of _all_ the accounts as the first entry in the search path.
87+
# Create an aggregator for all accounts as the first entry in the search path.
8588
connection "aws" {
8689
plugin = "aws"
8790
type = "aggregator"
@@ -112,7 +115,7 @@ if [ $COMMAND == "IMDS" ] ; then
112115
cat <<EOF>>$AWS_CONFIG_FILE
113116
114117
[profile sp_${ACCOUNT_NAME}]
115-
role_arn = arn:aws:iam::${ACCOUNT_ID}:role/${AUDITROLE}
118+
role_arn = arn:aws:iam::${ACCOUNT_ID}:role/${AUDIT_ROLE}
116119
credential_source = Ec2InstanceMetadata
117120
role_session_name = steampipe
118121
EOF
@@ -122,23 +125,23 @@ elif [ $COMMAND == "ECS" ] ; then
122125
cat <<EOF>>$AWS_CONFIG_FILE
123126
124127
[profile sp_${ACCOUNT_NAME}]
125-
role_arn = arn:aws:iam::${ACCOUNT_ID}:role/${AUDITROLE}
128+
role_arn = arn:aws:iam::${ACCOUNT_ID}:role/${AUDIT_ROLE}
126129
credential_source = EcsContainer
127130
role_session_name = steampipe
128131
EOF
129132
130133
else
131134
132-
133135
cat <<EOF>>$AWS_CONFIG_FILE
134136
135137
[profile sp_${ACCOUNT_NAME}]
136-
role_arn = arn:aws:iam::${ACCOUNT_ID}:role/${AUDITROLE}
138+
role_arn = arn:aws:iam::${ACCOUNT_ID}:role/${AUDIT_ROLE}
137139
source_profile = ${SOURCE_PROFILE}
138140
role_session_name = steampipe
139141
EOF
140142
fi
141143
144+
142145
# And append an entry to the Steampipe config file
143146
cat <<EOF>>$SP_CONFIG_FILE
144147
connection "aws_${SP_NAME}" {
@@ -152,7 +155,8 @@ EOF
152155
done < <(aws organizations list-accounts --query "Accounts[?Status!='SUSPENDED'].[Name,Id,Status]" --output text --profile $SOURCE_PROFILE | sort -f)
153156
154157
if [ $COMMAND == "LOCAL" ] ; then
155-
echo "Now append $AWS_CONFIG_FILE to your active config file where $SOURCE_PROFILE is defined"
158+
echo "Append $AWS_CONFIG_FILE to your active AWS config file where profile $SOURCE_PROFILE is defined"
159+
echo "Review your aws.spc file to ensure there are no conflicting connections"
156160
fi
157161
158162
# All done!

0 commit comments

Comments
 (0)