-
Notifications
You must be signed in to change notification settings - Fork 14
Script up process for enabling PGAudit #376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
pburkholder
wants to merge
4
commits into
main
Choose a base branch
from
pb/enable-pgaudit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,146 @@ | ||
| #!/bin/bash | ||
|
|
||
|
|
||
| set -eu -o pipefail | ||
|
|
||
| #org=cloud-gov-operators | ||
| #space=peter.burkholder | ||
| #service_instance=mia-django-db-x-psql15 | ||
|
|
||
| echo "Service Instance: $service_instance in Org: $org, Space: $space" | ||
|
|
||
|
|
||
| function reboot_instance() { | ||
| [ $# -ne 2 ] && echo "Function: reboot_instance <db_instance> <attempt>" && exit 1 | ||
| local db_instance=$1 | ||
| local attempt=$2 | ||
|
|
||
| set +e | ||
| aws rds reboot-db-instance \ | ||
| --db-instance-identifier "$db_instance" | ||
| status=$? | ||
| set -e | ||
|
|
||
| if [ $status -eq 254 ]; then | ||
| echo "Failed to reboot instance $db_instance status code 254" | ||
| attempt=$((attempt + 1)) | ||
| if [ $attempt -gt 5 ]; then | ||
| echo "Exceeded maximum reboot attempts. Exiting." | ||
| exit 1 | ||
| fi | ||
| echo "Sleeping 30 seconds and trying again..." | ||
| sleep 30 | ||
| reboot_instance "$db_instance" $attempt | ||
| elif [ $status -ne 0 ]; then | ||
| echo "Failed to reboot instance $db_instance with status code $status. Exiting." | ||
| exit 1 | ||
| fi | ||
| } | ||
|
|
||
| function create_and_associate_parameter_group() { | ||
| [ $# -ne 2 ] && echo "Function: create_parameter_group <current_parameter_group> <db_instance>" && exit 1 | ||
| local current_parameter_group=$1 | ||
| local db_instance=$2 | ||
|
|
||
| family=$(echo "${current_parameter_group}" | awk -F. '{print $NF}') | ||
|
|
||
| echo ============ create new parameter group =========== | ||
| aws rds create-db-parameter-group \ | ||
| --db-parameter-group-name "$db_instance" \ | ||
| --db-parameter-group-family "$family" \ | ||
| --description "Parameter group with pgaudit enabled" | ||
|
|
||
| setup_pgaudit_parameter "$db_instance" | ||
|
|
||
| echo =========== associate new parameter group with instance =========== | ||
| aws rds modify-db-instance \ | ||
| --db-instance-identifier "$db_instance" \ | ||
| --db-parameter-group-name "$db_instance" \ | ||
| --apply-immediately | ||
| } | ||
|
|
||
|
|
||
| function setup_pgaudit_parameter() { | ||
| [ $# -ne 1 ] && echo "Function: setup_pgaudit <parameter_group>" && exit 1 | ||
| local parameter_group=$1 | ||
|
|
||
| echo =========== modify pgaudit settings in parameter group =========== | ||
| aws rds modify-db-parameter-group \ | ||
| --db-parameter-group-name "$parameter_group" \ | ||
| --parameters "ParameterName=shared_preload_libraries,ParameterValue=pgaudit,ApplyMethod=pending-reboot" | ||
| } | ||
|
|
||
| # check if parameter group already has pgaudit enabled | ||
| function pgaudit_is_enabled() { | ||
| [ $# -ne 1 ] && echo "Function: check_pgaudit_enabled <parameter_group>" && exit 1 | ||
| local parameter_group=$1 | ||
|
|
||
| pgaudit_value=$(aws rds describe-db-parameters \ | ||
| --db-parameter-group-name "$parameter_group" \ | ||
| --query "Parameters[?ParameterName=='shared_preload_libraries'].ParameterValue" \ | ||
| --output text) | ||
|
|
||
| if [[ "$pgaudit_value" == *pgaudit* ]]; then | ||
| echo "pgaudit_value: >$pgaudit_value< indicates pgaudit is enabled" | ||
| return 0 | ||
| else | ||
| echo "pgaudit_value: >$pgaudit_value< indicates pgaudit is NOT enabled" | ||
| return 1 | ||
| fi | ||
| } | ||
|
|
||
|
|
||
| cf target -o $org -s $space > /dev/null | ||
| service_instance_guid=$(cf service $service_instance --guid) | ||
| echo "Service Instance GUID: $service_instance_guid" | ||
|
|
||
| # get ARN | ||
| arn=$(aws resourcegroupstaggingapi get-resources \ | ||
| --resource-type-filters "rds:db" \ | ||
| --tag-filters Key="Instance GUID",Values="$service_instance_guid" \ | ||
| | jq -r '.ResourceTagMappingList[].ResourceARN' | grep -v replica ) | ||
|
|
||
| # get AWS instancea name from ARN | ||
| db_instance=$(echo $arn | awk -F: '{print $NF}') | ||
| echo "RDS Instance: $db_instance" | ||
|
|
||
| # determine current parameter group | ||
| current_parameter_group=$(aws rds describe-db-instances \ | ||
| --db-instance-identifier "$db_instance" \ | ||
| --query 'DBInstances[0].DBParameterGroups[?DBParameterGroupName!=`default`].DBParameterGroupName' \ | ||
| --output text) | ||
|
|
||
| echo "Current parameter group: $current_parameter_group" | ||
|
|
||
| # Determine whether we need to create a new parameter group or modify existing one | ||
| case $current_parameter_group in | ||
| "" ) | ||
| echo "No custom parameter group associated with instance. Failing..." | ||
| exit 1 | ||
| ;; | ||
| *default* ) | ||
| echo "Uses default parameter group $current_parameter_group." | ||
| echo "Creating new parameter group $db_instance and associating it with instance." | ||
| create_and_associate_parameter_group "$current_parameter_group" "$db_instance" | ||
| reboot_instance "$db_instance" 0 | ||
| ;; | ||
| *$db_instance* ) | ||
| echo "Parameter group already set to $db_instance." | ||
| if pgaudit_is_enabled "$current_parameter_group"; then | ||
| echo "pgaudit is already enabled in parameter group $current_parameter_group. Exiting." | ||
| exit 0 | ||
| else | ||
| echo "Enabling pgaudit in parameter group $current_parameter_group." | ||
| setup_pgaudit_parameter "$current_parameter_group" | ||
| reboot_instance "$db_instance" 0 | ||
| fi | ||
| exit 0 | ||
| ;; | ||
| * ) | ||
| echo "Unknown parameter group $current_parameter_group. Failing..." | ||
| exit 1 | ||
| ;; | ||
| esac | ||
|
|
||
| echo "Well, how did we get here?" | ||
| exit 0 | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo (non-blocking): on instances; vvv important, good thing I was curious about this PR so I could make this helpful feedback