Aurora Postgres
1. Enabling the PGAudit extension
There are different ways of auditing and logging in postgres. For this exercise, we will use PGAudit, the open source audit logging extension for PostgreSQL 9.5+. This extension supports logging for Sessions or Objects. Configure either session auditing or object auditing. You cannot enable both at the same time.
Steps to enable PGAudit
-
Create the database parameter group
-
Enable auditing using either one of the following:
a. Enabling PGAudit Session Auditing
b. Enabling PGAudit Object Auditing
-
Associating the DB Parameter Group with the database Instance
Creating the database parameter group
When you create a database instance, it is associated with the default parameter group. Follow these steps to create a new parameter group:
Procedure
-
Go to Services > Database > Parameter groups.
-
Click Create Parameter Group in the left pane.
-
Enter the parameter group details.
• Select the parameter group family. For example, aurora-postgres12. This version should match the version of the database that is created and with which this parameter group is to be associated.
• Enter the DB parameter group name.
• Enter the DB parameter group description.
-
Click Save. The new group appears in the Parameter Groups section.
Enabling PGAudit Session Auditing
Session Auditing allows you to log activities that are selected in the pgaudit.log for logging. Be cautious when you select which activities will be logged, as logged activities can affect the performance of the database instance.
Procedure
-
In the Amazon RDS panel, select Parameter Groups.
-
Select the parameter group that you created.
-
Click Edit parameters and add these settings:
•
pgaudit.log = all, -misc(Select the options from the Allowed values list. You can specify multiple values, and separate them with “,”. The values that are marked with “-” are excluded while logging.)
•
pgaudit.log_catalog = 0•
pgaudit.log_parameter = 0•
shared_preload_libraries = pgaudit•
log_error_verbosity = default
Enabling PGAudit Object Auditing
Object auditing affects the performance less than session auditing, due to the fine-grained criteria of tables and columns that you can choose for auditing.
Procedure
-
Set these parameters:
•
pgaudit.log = none(since this is not needed for extensive SESSION logging)•
pgaudit.role = rds_pgaudit•
pgaudit.log_catalog = 0•
pgaudit.log_parameter = 0•
shared_preload_libraries = pgaudit•
log_error_verbosity = default -
Provide the required permissions to the rds_pgaudit role while associating it with the table that is audited. For example, grant
ALLon<relation_name>tords_pgaudit(this grant enables fullSELECT,INSERT,UPDATE, andDELETE logging on therelation_name`).
Associating the DB parameter group with the database instance
Procedure
- Go to Services > Database > RDS > Databases.
- Click the Aurora Postgres database instance to be updated.
- Click Modify.
- Go to the Additional Configuration section > database options > DB Parameter Group menu and select the newly-created group.
- Click Continue.
- Select the database instance in its configuration section. The state of the DB Parameter Group is pending-reboot.
- Reboot the database instance for the changes to take effect.
2. Viewing the PGAudit logs
The PGAudit logs (both session and object logs) can be seen in log files in RDS, and also on CloudWatch:
Viewing the auditing details in RDS log files
The RDS log files can be viewed, watched, and downloaded. The name of the RDS log file is modifiable and is controlled by parameter log_filename.
Procedure
- Go to Services > Database > RDS > Databases.
- Select the database instance.
- Select the Logs & Events section.
- The end of the Logs section lists the files that contain the auditing details. The newest file is the last page.
Viewing the logs entries on CloudWatch
By default, each database instance has an associated log group with a name in this format: /aws/rds/cluster/<instance_name>/postgresql. You can use this log group, or you can create a new one and associate it with the database instance.
Procedure
- On the AWS Console page, open the Services menu.
- Enter the CloudWatch string in the search box.
- Click CloudWatch to redirect to the CloudWatch dashboard.
- In the left panel, select Logs.
- Click Log Groups.
Notes
- Guardium Data Protection requires installation of the json_encode filter plug-in.
3. Exporting Cloudwatch Logs to SQS using lambda function
In order to achieve load balancing of audit logs between different collectors, the audit logs must exported from Cloudwatch to SQS
Creating the SQS
Procedure
- Go to https://console.aws.amazon.com/
- Click Services
- Search for SQS and click on Simple Queue Services
- Click on Create Queue
- Select the type as Standard
- Enter the name for the queue
- Keep the rest of the default settings
Create Policy for the relevant IAM User
-
For the IAM User using which the SQS logs are to be accessed in Guardium, perform the below steps
-
Go to
IAM service>Policies>Create Policy -
Select
service as SQS -
Select the check boxes having actions as:
ListQueues,DeleteMessage,DeleteMessageBatch,GetQueueAttributes,GetQueueUrl,ReceiveMessage,ChangeMessageVisibility,ChangeMessageVisibilityBatch -
In the resources, specify the ARN of the queue created in the above step
-
Click
Review policyand specify the policy name -
Click
Create policy -
Assign the policy to the user
a. Log in to the IAM console as IAM user (https://console.aws.amazon.com/iam/)
b. Go to
Userson the console and select the relevant IAM user to whom you want to give permissions. Click the user name linkc. In the
Permissionstab, clickAdd permissionsd. Click
Attach existing policies directlye. Search for the policy created and check the checkbox next to it
f. Click
Next: Reviewg. Click
Add permissions
Creating the lambda function
Create IAM Role
Create the IAM role that will be used in the Lambda function set up. The AWS lambda service will require permission to log events and write to the SQS created. Create the IAM Role “Export-RDS-CloudWatch-to-SQS-Lambda” with “AmazonSQSFullAccess”, “CloudWatchLogsFullAccess”, and “CloudWatchEventsFullAccess” policies.
Procedure
- Go to https://console.aws.amazon.com/
- Go to
IAM->Roles - Click
Create Role - Under
use caseselectLambdaand clickNext - Search for “AmazonSQSFullAccess” and select it
- Search for “CloudWatchLogsFullAccess” and select it
- Search for “CloudWatchEventsFullAccess” and select it
- Set the
Role Name: e.g., “Export-RDS-CloudWatch-to-SQS-Lambda” and clickCreate role
Create the lambda function
Procedure
- Go to https://console.aws.amazon.com/
- Go to
Services. Search for lambda function - Click
Functions - Click
Create Function - Keep
Author for Scratchselected - Set
Function namee.g., Export-RDS-CloudWatch-Logs-To-SQS - Under
Runtime, selectPython 3.x - Under
Permissions, selectUse an existing roleand select the IAM role that you created in the previous step (Export-RDS-CloudWatch-to-SQS-Lambda) - Click
Create functionand navigate toCode view - Add the function code from lambdaFunction
- Click
Configuration->Environment Variables - Create 2 variables:
- Key = GROUP_NAME value =
e.g., /aws/rds/cluster/aurora-postgres/postgresql - Key = QUEUE_NAME value =
e.g., https://sqs.ap-south-1.amazonaws.com/1111111111/PostgresQueue
- Key = GROUP_NAME value =
- Save the function
- Click on the Deploy button
Automating the lambda function
Procedure
- Go to the CloudWatch dashboard
- Go to
Events->Ruleson the left pane - Click
Create Rule - Enter the name for the rule e.g., cloudwatchToSqs
- Under
Rule Type, selectSchedule - Define the schedule. In
schedule patternselect a schedule that runs at a regular rate, such as every 10 minutes - Enter the rate expression, meaning the rate at which the function should execute. This value must match the time specified in the lambda function code that calculates the time delta. (If the function code it is set to 2 minutes, set the rate to 2 minutes unless changed in the code). Click
Next - Select the
Target1. Select theTarget TypeasAWS Service - Select
TargetasLambda Function - Select the lambda function created in the above step. e.g., Export-RDS-CloudWatch-Logs-To-SQS
- Add the tag if needed
- Click
Create Rule
Note
- Before making any changes to the lambda function code, first disable the above rule. Deploy the change and then re-enable the rule.
- When large amount of events are to be pushed from Cloudwatch to SQS, it is possible that Lambda function may get timed out. In order to avoid data loss, 'Timeout' value needs to be updated. However, this value should be less than the value configured in scheduling of the Event Bridge Rule. We can even adjust the scheduling of the Event Bridge to suit the load requirements. The steps to change the timeout parameter :-
- Edit the created Lambda function.
Click Configuration->General configuration- Click Edit
- Set
Timeout, eg : 1 min 45 sec
4. Configuring the Postgres filters in Guardium
The Guardium universal connector is the Guardium entry point for native audit logs. The universal connector identifies and parses the received events, and converts them to a standard Guardium format. The output of the universal connector is forwarded to the Guardium sniffer on the collector, for policy and auditing enforcements. Configure Guardium to read the native audit logs by customizing the Postgres template.
Authorizing outgoing traffic from AWS to Guardium
Procedure
-
Log in to the Guardium Collector's API.
-
Issue these commands:
•
grdapi add_domain_to_universal_connector_allowed_domains domain=amazonaws.com•
grdapi add_domain_to_universal_connector_allowed_domains domain=amazon.com
Before you begin
• Configure the policies you require. See policies for more information.
• You must have permission for the S-Tap Management role. The admin user includes this role by default.
• This plug-in is automatically available with Guardium Data Protection versions 12.x, 11.4 with appliance bundle 11.0p490 or later or Guardium Data Protection version 11.5 with appliance bundle 11.0p540 or later releases.
• For Guardium Data Protection version 11.0p540 and/or 11.0p6505 and/or 12.0 and/or 12p15 download the cloudwatch_logs plug-in
Note: For Guardium Data Protection version 11.4 without appliance bundle 11.0p490 or prior or Guardium Data Protection version 11.5 without appliance bundle 11.0p540 or prior, download the postgres-offline-plugins-7.5.2.zip plug-in. (Do not unzip the offline-package file throughout the procedure).
Procedure
- On the collector, go to Setup > Tools and Views > Configure Universal Connector.
- Enable the universal connector if it is disabled.
- Click Upload File and
- Select the offline postgres-offline-plugins-7.5.2.zip plug-in. After it is uploaded, click OK. This is not necessary for Guardium Data Protection v11.0p490 or later, v11.0p540 or later, v12.0 or later.
- If you have installed Guardium Data Protection version 11.0p540 and/or 11.0p6505 and/or 12.0 and/or 12p15, select the offline cloudwatch_logs plug-in. After it is uploaded, click OK.
- Click the Plus sign to open the Connector Configuration dialog box.
- Type a name in the Connector name field.
- If the audit logs are to be fetched from CloudWatch directly, use the details from the postgresCloudwatch.conf file. But if the audit logs are to be fetched from SQS, use the details from the postgreSQS.conf file. Update the input section to add the details from the corresponding file's input part, omitting the keyword "input{" at the beginning and its corresponding "}" at the end. More details on how to configure the relevant input plugin, see Cloudwatch_logs input plug-in.
Note:If you want to configure Cloudwatch with role_arn instead of access_key and secret_key then refer to the Configuration for role_arn parameter in the cloudwatch_logs input plug-in topic.
- If the audit logs are to be fetched from CloudWatch directly, use the details from the postgresCloudwatch.conf file. But if the audit logs are to be fetched from SQS, use the details from the postgreSQS.conf file. Update the filter section to add the details from the corresponding file's filter part, omitting the keyword "filter{" at the beginning and its corresponding "}" at the end
- The "type" fields should match in the input and the filter configuration sections. This field should be unique for every individual connector added.
- Click Save. Guardium validates the new connector and displays it in the Configure Universal Connector page.