Active Tracing Should Be Enabled For API Gateway Stages
More Info:
Active tracing should be enabled for your Amazon API Gateway API stages to sample incoming requests and send traces to AWS X-Ray. Then X-Ray can provide you an end-to-end view of an entire HTTP request, so you can analyze latencies in your APIs and their backend services.
Risk Level
Low
Address
Operational Maturity, Security
Compliance Standards
- APRA CPS 234 (Australia)
- BSI C5 (Germany)
- Brazil LGPD
- CCPA / CPRA (California)
- CIS Critical Security Controls v8
- CMMC 2.0
- CSA Cloud Controls Matrix v4
- Cloudanix Best Practice
- DPDPA
- Digital Operational Resilience Act (EU)
- ISO/IEC 27017
- ISO/IEC 27018
- ISO/IEC 27701
- KSA PDPL
- MAS Technology Risk Management (Singapore)
- MITRE ATT&CK (Cloud)
- NIS2 Directive
- NIST SP 800-171
- NYDFS 23 NYCRR 500
- SWIFT Customer Security Controls Framework
- Sarbanes-Oxley IT General Controls
- StateRAMP
- UK NCSC Cyber Assessment Framework
Triage and Remediation
- Cause
- Remediation
Check Cause
Using Console
-
Sign in to the AWS Management Console and open the Amazon API Gateway console at https://console.aws.amazon.com/apigateway/.
-
In the navigation pane, choose 'APIs'.
-
Select the API you want to check, then in the 'Stages' section, select the stage you want to inspect.
-
In the 'Logs/Tracing' tab, check the 'Enable X-Ray Tracing' box. If it's not checked, Active Tracing is not enabled for that API Gateway Stage.
Using CLI
-
First, you need to install and configure AWS CLI on your local machine. You can do this by following the instructions provided by AWS. Make sure you have the necessary permissions to access the API Gateway.
-
Once the AWS CLI is installed and configured, you can list all the APIs in your account by running the following command:
aws apigateway get-rest-apisThis command will return a list of all the APIs in your account.
-
For each API, you can list all the stages by running the following command:
aws apigateway get-stages --rest-api-id <rest-api-id>Replace
<rest-api-id>with the ID of the API you want to check. This command will return a list of all the stages for the specified API. -
For each stage, you can check if active tracing is enabled by looking at the
tracingEnabledfield in the output. If this field is set tofalse, then active tracing is not enabled for that stage.
Using Python
-
Install the necessary Python libraries: Before you start, make sure you have the necessary Python libraries installed. You will need the boto3 library, which is the Amazon Web Services (AWS) SDK for Python. It allows Python developers to write software that makes use of services like Amazon S3, Amazon EC2, and others. You can install it using pip:
pip install boto3 -
Set up AWS credentials: You need to configure your AWS credentials. You can set your credentials for use by boto3 in several ways, but the simplest is to use the AWS CLI. Run
aws configureand then enter your access key, secret access key, and default region when prompted. -
Write a Python script to check the active tracing status: You can use the
get_stagemethod provided by the boto3 library to retrieve the information about a specific stage for a RestApi resource. ThetracingEnabledattribute in the response indicates whether active tracing is enabled for the API Gateway stage.Here is a sample script:
import boto3client = boto3.client('apigateway')response = client.get_stage(restApiId='your_rest_api_id',stageName='your_stage_name')if 'tracingEnabled' in response:if response['tracingEnabled']:print("Active tracing is enabled for this API Gateway stage.")else:print("Active tracing is not enabled for this API Gateway stage.")else:print("The 'tracingEnabled' attribute is not present in the response.")Replace 'your_rest_api_id' and 'your_stage_name' with your actual RestApi ID and stage name.
-
Run the script: Save the script to a file, then run it using your Python interpreter. The script will print a message indicating whether active tracing is enabled for the specified API Gateway stage. If the 'tracingEnabled' attribute is not present in the response, the script will print a message indicating this.
Remediation
Using Console
To remediate the misconfiguration "Active Tracing Should Be Enabled For API Gateway Stages" for AWS using AWS console, you can follow the below steps:
-
Open the AWS Management Console and navigate to the API Gateway service.
-
Select the API Gateway that contains the stage for which you want to enable active tracing.
-
Click on the Stages tab and select the stage for which you want to enable active tracing.
-
Click on the Settings tab and scroll down to the "Tracing" section.
-
Click on the "Edit" button next to the "Tracing" section.
-
Select the "Enable Active tracing" checkbox.
-
Choose the appropriate tracing level - "ERROR", "INFO", or "DEBUG" based on your requirements.
-
Click on the "Save Changes" button to enable active tracing for the selected stage.
-
Repeat this process for all the stages in your API Gateway.
By following these steps, you can remediate the misconfiguration "Active Tracing Should Be Enabled For API Gateway Stages" for AWS using AWS console.
Using CLI
To remediate this misconfiguration for AWS using AWS CLI, follow these steps:
-
Open the AWS CLI on your local machine.
-
Run the following command to enable active tracing for API Gateway stages:
aws apigateway update-stage --rest-api-id <rest-api-id> --stage-name <stage-name> --patch-operations '[{"op":"replace","path":"/tracingEnabled","value":"True"}]'
Note: Replace <rest-api-id> with the ID of your API Gateway REST API and <stage-name> with the name of the stage you want to enable active tracing for.
- Verify that active tracing has been enabled for the specified stage by running the following command:
aws apigateway get-stage --rest-api-id <rest-api-id> --stage-name <stage-name> | grep tracingEnabled
Note: Replace <rest-api-id> with the ID of your API Gateway REST API and <stage-name> with the name of the stage you enabled active tracing for.
- If the output of the previous command shows
"tracingEnabled": true, then active tracing has been successfully enabled for the specified stage. If it shows"tracingEnabled": false, then repeat steps 2 and 3 to ensure that active tracing is properly enabled.
Using Python
To remediate the misconfiguration "Active Tracing Should Be Enabled For API Gateway Stages" for AWS using Python, you can follow the below steps:
- Install the AWS SDK for Python (Boto3) using the following command:
pip install boto3
- Create a Boto3 client for the Amazon API Gateway service:
import boto3
client = boto3.client('apigateway')
- List all the API Gateway stages for the specified API using the following command:
stages = client.get_stages(restApiId='REST_API_ID')
Note: Replace 'REST_API_ID' with the ID of your API Gateway.
- For each stage, check if active tracing is enabled using the following command:
for stage in stages['item']:
tracing_enabled = client.get_stage(restApiId='REST_API_ID', stageName=stage['stageName'])['tracingEnabled']
- If active tracing is not enabled, enable it using the following command:
if not tracing_enabled:
client.update_stage(restApiId='REST_API_ID', stageName=stage['stageName'], patchOperations=[{'op': 'replace', 'path': '/tracingEnabled', 'value': 'True'}])
- Run the Python script to remediate the misconfiguration.
Note: Make sure to replace 'REST_API_ID' with the ID of your API Gateway.
Using Terraform
resource "aws_api_gateway_rest_api" "THIS_API" {
name = "REPLACE_WITH_API_NAME"
}
resource "aws_api_gateway_deployment" "THIS_DEPLOYMENT" {
rest_api_id = aws_api_gateway_rest_api.THIS_API.id
# NOTE: A deployment triggers when something in the stage / integration changes;
# add a dummy variable or explicit triggers if you need controlled rollouts.
}
resource "aws_api_gateway_stage" "THIS_STAGE" {
rest_api_id = aws_api_gateway_rest_api.THIS_API.id
deployment_id = aws_api_gateway_deployment.THIS_DEPLOYMENT.id
stage_name = "REPLACE_WITH_STAGE_NAME"
# Enable AWS X-Ray active tracing for this stage
tracing_enabled = true
}
Substitute:
REPLACE_WITH_API_NAMEwith your API name.REPLACE_WITH_STAGE_NAMEwith the exact stage name that must have X-Ray active tracing enabled.
This change does not force replacement of the stage; it updates the existing stage to turn on tracing_enabled (same effect as the CLI update-stage ... --patch-operations op=replace,path=/tracingEnabled,value='true').
Verification: terraform plan should show an in-place update on the aws_api_gateway_stage resource with tracing_enabled changing from false (or null) to true.