-
Notifications
You must be signed in to change notification settings - Fork 9
[APPSEC-61865] Add inferred span and AppSec calls for API Gateway #136
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
Strech
wants to merge
4
commits into
main
Choose a base branch
from
appsec-61865-add-inferred-span-for-api-gateway
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
ea96ca3
Add inferred spans for API Gateway and AppSec integration
Strech 3f02303
Fix aws.lambda span service and resource names
Strech b74924e
Add span tags and HTTP enrichment to aws.lambda span
Strech f5449a9
Add http.status_code to both spans
Strech 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| AllCops: | ||
| TargetRubyVersion: 3.2 | ||
| Exclude: | ||
| - 'test/**/*' | ||
|
|
||
| Metrics/MethodLength: | ||
| Max: 20 |
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,86 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative 'appsec/request' | ||
|
|
||
| module Datadog | ||
| module Lambda | ||
| # AppSec integration for AWS Lambda invocations. | ||
| module AppSec | ||
| class << self | ||
| def on_start(event, trace:, span:, inferred_span: nil) | ||
| @request = nil | ||
| @inferred_span = inferred_span | ||
| return unless enabled? | ||
|
|
||
| context = create_context(trace, span) | ||
| return unless Datadog::AppSec::Context.active | ||
|
|
||
| @request = Request.from_event(event) | ||
|
|
||
| payload = Datadog::AppSec::Instrumentation::Gateway::DataContainer.new( | ||
| event, context: context | ||
| ) | ||
| Datadog::AppSec::Instrumentation.gateway.push('aws_lambda.request.start', payload) | ||
| rescue StandardError => e | ||
| Datadog::Utils.logger.debug "failed to start AppSec: #{e}" | ||
| end | ||
|
|
||
| def on_finish(response) | ||
| return unless enabled? | ||
|
|
||
| context = Datadog::AppSec::Context.active | ||
| return unless context | ||
|
|
||
| payload = Datadog::AppSec::Instrumentation::Gateway::DataContainer.new( | ||
| response, context: context | ||
| ) | ||
|
|
||
| Datadog::AppSec::Instrumentation.gateway.push('aws_lambda.response.start', payload) | ||
| Datadog::AppSec::Event.record(context, request: @request) | ||
| copy_appsec_span_tags(context.span, @inferred_span) | ||
|
|
||
| context.export_metrics | ||
| context.export_request_telemetry | ||
| rescue StandardError => e | ||
| Datadog::Utils.logger.debug "failed to finish AppSec: #{e}" | ||
| ensure | ||
| Datadog::AppSec::Context.deactivate if context | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def enabled? | ||
| defined?(Datadog::AppSec) && | ||
| Datadog::AppSec.respond_to?(:enabled?) && | ||
| Datadog::AppSec.enabled? | ||
| end | ||
|
|
||
| # NOTE: _dd.appsec.json is required on both the service-entry span | ||
| # and the inferred span. | ||
| def copy_appsec_span_tags(source_span, destination_span) | ||
| return unless source_span && destination_span | ||
|
|
||
| appsec_json = source_span.get_tag('_dd.appsec.json') | ||
| destination_span.set_tag('_dd.appsec.json', appsec_json) if appsec_json | ||
|
|
||
| appsec_enabled = source_span.get_metric('_dd.appsec.enabled') | ||
| destination_span.set_metric('_dd.appsec.enabled', appsec_enabled) if appsec_enabled | ||
| end | ||
|
|
||
| def create_context(trace, span) | ||
| return if trace.nil? || span.nil? | ||
|
|
||
| security_engine = Datadog::AppSec.security_engine | ||
| return unless security_engine | ||
|
|
||
| context = Datadog::AppSec::Context.new(trace, span, security_engine.new_runner) | ||
| Datadog::AppSec::Context.activate(context) | ||
|
|
||
| span.set_metric(Datadog::AppSec::Ext::TAG_APPSEC_ENABLED, 1) | ||
|
|
||
| context | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
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,47 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Datadog | ||
| module Lambda | ||
| module AppSec | ||
| # Minimal request object for AppSec event recording. | ||
| # | ||
| # WARNING: It's a minimal data for interface compliance | ||
| # | ||
| # @see Datadog::AppSec::Event.record | ||
| # @see Datadog::AppSec::Contrib::Rack::Gateway::Request | ||
| class Request | ||
| attr_reader :host, :user_agent, :remote_addr, :headers | ||
|
|
||
| class << self | ||
| def from_event(event) | ||
| headers = normalize_headers(event) | ||
| remote_addres = event.dig('requestContext', 'identity', 'sourceIp') || | ||
| event.dig('requestContext', 'http', 'sourceIp') | ||
|
|
||
| new( | ||
| host: headers['host'], | ||
| user_agent: headers['user-agent'], | ||
| remote_addr: remote_addres, | ||
| headers: headers | ||
| ) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def normalize_headers(event) | ||
| event.fetch('headers', {}).each_with_object({}) do |(key, value), hash| | ||
| hash[key.downcase] = value | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def initialize(host:, user_agent:, remote_addr:, headers:) | ||
| @host = host | ||
| @user_agent = user_agent | ||
| @remote_addr = remote_addr | ||
| @headers = headers | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
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,26 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # | ||
| # Unless explicitly stated otherwise all files in this repository are licensed | ||
| # under the Apache License Version 2.0. | ||
| # | ||
| # This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| # Copyright 2026 Datadog, Inc. | ||
| # | ||
|
|
||
| require_relative 'event_source/api_gateway_v1' | ||
| require_relative 'event_source/api_gateway_v2' | ||
|
|
||
| module Datadog | ||
| module Lambda | ||
| # Detects and parses Lambda event payloads into a uniform interface. | ||
| module EventSource | ||
| SOURCES = [ApiGatewayV1, ApiGatewayV2].freeze | ||
|
|
||
| def self.for(event) | ||
| klass = SOURCES.find { |source| source.match?(event) } | ||
| klass&.new(event) | ||
| end | ||
| end | ||
| end | ||
| end |
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,54 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # | ||
| # Unless explicitly stated otherwise all files in this repository are licensed | ||
| # under the Apache License Version 2.0. | ||
| # | ||
| # This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| # Copyright 2026 Datadog, Inc. | ||
| # | ||
|
|
||
| module Datadog | ||
| module Lambda | ||
| module EventSource | ||
| # Parses API Gateway REST API (v1) Lambda proxy integration events | ||
| # into a uniform interface. | ||
| # | ||
| # @see https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format | ||
| class ApiGatewayV1 | ||
| class << self | ||
| def match?(payload) | ||
| api_gateway?(payload) && payload.key?('httpMethod') | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def api_gateway?(payload) | ||
| payload.is_a?(Hash) && | ||
| payload.key?('requestContext') && payload['requestContext'].key?('stage') | ||
| end | ||
| end | ||
|
|
||
| def initialize(payload) | ||
| @payload = payload | ||
| @request_context = payload.fetch('requestContext', {}) | ||
| end | ||
|
|
||
| def method = @payload['httpMethod'] | ||
| def path = @payload.fetch('path', '/') | ||
| def resource_path = @request_context.fetch('resourcePath', path) | ||
| def domain = @request_context['domainName'] | ||
| def api_id = @request_context['apiId'] | ||
| def stage = @request_context['stage'] | ||
| def request_time_ms = @request_context['requestTimeEpoch'] | ||
| def user_agent = @request_context.dig('identity', 'userAgent') | ||
|
|
||
| def http_url | ||
| return path unless domain | ||
|
|
||
| "https://#{domain}#{path}" | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
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,55 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # | ||
| # Unless explicitly stated otherwise all files in this repository are licensed | ||
| # under the Apache License Version 2.0. | ||
| # | ||
| # This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| # Copyright 2026 Datadog, Inc. | ||
| # | ||
|
|
||
| module Datadog | ||
| module Lambda | ||
| module EventSource | ||
| # Parses API Gateway HTTP API (v2) Lambda proxy integration events | ||
| # into a uniform interface. | ||
| # | ||
| # @see https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html#http-api-develop-integrations-lambda.proxy-format | ||
| class ApiGatewayV2 | ||
| class << self | ||
| def match?(payload) | ||
| api_gateway?(payload) && payload.key?('routeKey') | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def api_gateway?(payload) | ||
| payload.is_a?(Hash) && | ||
| payload.key?('requestContext') && payload['requestContext'].key?('stage') | ||
| end | ||
| end | ||
|
|
||
| def initialize(payload) | ||
| @payload = payload | ||
| @request_context = payload.fetch('requestContext', {}) | ||
| @http = @request_context.fetch('http', {}) | ||
| end | ||
|
|
||
| def method = @http['method'] | ||
| def path = @payload.fetch('rawPath', '/') | ||
| def resource_path = @payload['routeKey']&.sub(/\A[A-Z]+ /, '') || path | ||
| def domain = @request_context['domainName'] | ||
| def api_id = @request_context['apiId'] | ||
| def stage = @request_context['stage'] | ||
| def request_time_ms = @request_context['timeEpoch'] | ||
| def user_agent = @http['userAgent'] | ||
|
|
||
| def http_url | ||
| return path unless domain | ||
|
|
||
| "https://#{domain}#{path}" | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.