-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Nic Cheneweth <[email protected]>
- Loading branch information
1 parent
9532358
commit 210277b
Showing
7 changed files
with
85 additions
and
44 deletions.
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
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,11 @@ | ||
[[source]] | ||
url = "https://pypi.org/simple" | ||
verify_ssl = true | ||
name = "pypi" | ||
|
||
[packages] | ||
|
||
[dev-packages] | ||
|
||
[requires] | ||
python_version = "3.10" |
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,54 @@ | ||
import boto3 | ||
import sys | ||
import json | ||
|
||
if len(sys.argv) < 2 or len(sys.argv) > 3: | ||
print("Usage: rotate PATH [filename]") | ||
exit(1) | ||
|
||
user_path = f"/{sys.argv[1]}/" | ||
credential_file = "credentials" if len(sys.argv) == 2 else sys.argv[2] | ||
print(f"Rotate all IAM User access credentials on path :user{user_path}\n") | ||
|
||
iam = boto3.client('iam') | ||
|
||
svc_accounts = iam.list_users( | ||
PathPrefix=user_path, | ||
MaxItems=100 | ||
) | ||
|
||
if len(svc_accounts['Users']): | ||
new_credentials = {} | ||
|
||
for user in svc_accounts['Users']: | ||
print(f"Rotate: {user['UserName']}") | ||
access_keys = iam.list_access_keys(UserName=user['UserName'])["AccessKeyMetadata"] | ||
|
||
# delete the oldest key (if there is more than one; currently IAM Users are permitted only 2 keys) | ||
if len(access_keys) > 1: | ||
# sort by creation date to find oldest key (by convention, service accounts always use the newest key) | ||
access_keys.sort(key=lambda x: x["CreateDate"]) | ||
|
||
print(f"Delete out of date key: **********{access_keys[0]['AccessKeyId'][-5:]}") | ||
response = iam.delete_access_key( | ||
UserName=user['UserName'], | ||
AccessKeyId=access_keys[0]['AccessKeyId'] | ||
) | ||
|
||
else: | ||
print(f"Skipping: {user['UserName']} has only one key") | ||
|
||
# generate new key, add details to list of new keys | ||
new_access_key = iam.create_access_key(UserName=user['UserName']) | ||
print(f"New access key created: **********{new_access_key['AccessKey']['AccessKeyId'][-5:]}\n") | ||
new_credentials[user['UserName']] = {} | ||
new_credentials[user['UserName']]['AccessKeyId'] = new_access_key['AccessKey']['AccessKeyId'] | ||
new_credentials[user['UserName']]['SecretAccessKey'] = new_access_key['AccessKey']['SecretAccessKey'] | ||
|
||
# write new_credentials to local file, to be processed into your secrets store | ||
print(json.dumps(new_credentials, indent = 2)) | ||
with open(credential_file, "w") as outfile: | ||
json.dump(new_credentials, outfile) | ||
|
||
else: | ||
print("No PSK service accounts found!") |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
variable "aws_region" { type = string } | ||
variable "aws_account_id" { type = string } | ||
#variable "aws_account_id" { type = string } | ||
|
||
# create service accounts, groups, and group assignments only in state account | ||
variable "is_state_account" { | ||
|
@@ -12,10 +12,10 @@ variable "all_production_account_roles" { type = list(any) } | |
|
||
# [email protected] service account gpg public key for encrypting aws credentials | ||
# not a secret, but even public keys can set off secret scanners | ||
variable "twdpsio_gpg_public_key_base64" { | ||
type = string | ||
sensitive = true | ||
} | ||
# variable "twdpsio_gpg_public_key_base64" { | ||
# type = string | ||
# sensitive = true | ||
# } | ||
|
||
|
||
# # ========= original | ||
|
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