-
Notifications
You must be signed in to change notification settings - Fork 6
Normalize Lambda and OpenSearch client config #986
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
Merged
Changes from all commits
Commits
Show all changes
3 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
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
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,51 @@ | ||
| # AwsAuth centralizes AWS credential and role-assumption helpers shared by | ||
| # OpenSearch and Lambda initialization. | ||
| # | ||
| # It supports two auth patterns: | ||
| # 1) direct static credentials from environment variables | ||
| # 2) temporary assumed-role credentials via AWS STS | ||
| module AwsAuth | ||
| module_function | ||
|
|
||
| # Builds static credentials directly from environment variables. | ||
| # | ||
| # @return [Aws::Credentials] Credentials built from AWS_ACCESS_KEY_ID, | ||
| # AWS_SECRET_ACCESS_KEY, and optional AWS_SESSION_TOKEN. | ||
| def static_credentials | ||
| Aws::Credentials.new( | ||
| ENV.fetch('AWS_ACCESS_KEY_ID', nil), | ||
| ENV.fetch('AWS_SECRET_ACCESS_KEY', nil), | ||
| ENV.fetch('AWS_SESSION_TOKEN', nil) | ||
| ) | ||
| end | ||
|
|
||
| # Builds an AWS STS client used for role-assumption flows. | ||
| # | ||
| # Region defaults to us-east-1 when AWS_REGION is not set. | ||
| # Optional AWS_SESSION_TOKEN is forwarded when present. | ||
| # | ||
| # @return [Aws::STS::Client] Configured STS client instance. | ||
| def sts_client | ||
| options = { | ||
| region: ENV.fetch('AWS_REGION', 'us-east-1'), | ||
| access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID', nil), | ||
| secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY', nil) | ||
| } | ||
| options[:session_token] = ENV['AWS_SESSION_TOKEN'] if ENV['AWS_SESSION_TOKEN'].present? | ||
|
|
||
| Aws::STS::Client.new(options) | ||
| end | ||
|
|
||
| # Builds auto-refreshing assumed-role credentials backed by STS. | ||
| # | ||
| # @param role_session_name [String] Session identifier used in STS and | ||
| # CloudTrail for traceability. | ||
| # @return [Aws::AssumeRoleCredentials] Refreshing credentials provider. | ||
| def assume_role_credentials(role_session_name:) | ||
| Aws::AssumeRoleCredentials.new( | ||
| role_arn: ENV.fetch('AWS_ROLE_ARN', nil), | ||
| role_session_name: role_session_name, | ||
| client: sts_client | ||
| ) | ||
| 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,77 @@ | ||
| # AwsConfigValidator validates AWS-related environment configuration for | ||
| # OpenSearch and Lambda initialization. | ||
| # | ||
| # Validation is split by integration pathway: | ||
| # 1) Lambda runtime credentials | ||
| # 2) AWS AOSS (OpenSearch Serverless) | ||
| # 3) AWS-managed OpenSearch | ||
| class AwsConfigValidator | ||
| class << self | ||
| # Validates required Lambda credential environment variables. | ||
| # | ||
| # AWS_ROLE_ARN becomes required only when AWS_SESSION_TOKEN is not present. | ||
| # This supports both temporary session-token auth and role-assumption auth. | ||
| # | ||
| # @return [nil] | ||
| # @raise [RuntimeError] when required env vars are missing. | ||
| def validate_lambda_config | ||
| required_vars = { | ||
| 'AWS_ACCESS_KEY_ID' => ENV.fetch('AWS_ACCESS_KEY_ID', nil), | ||
| 'AWS_SECRET_ACCESS_KEY' => ENV.fetch('AWS_SECRET_ACCESS_KEY', nil) | ||
| } | ||
|
|
||
| required_vars['AWS_ROLE_ARN'] = ENV.fetch('AWS_ROLE_ARN', nil) if ENV['AWS_SESSION_TOKEN'].blank? | ||
|
|
||
| validate_required_vars!(required_vars, error_prefix: 'AWS Lambda Config Error') | ||
| end | ||
|
|
||
| # Validates required configuration for AWS AOSS connections. | ||
| # | ||
| # AWS_ROLE_ARN becomes required only when AWS_SESSION_TOKEN is not present. | ||
| # This supports both temporary session-token auth and role-assumption auth. | ||
| # | ||
| # @return [nil] | ||
| # @raise [RuntimeError] when required env vars are missing. | ||
| def validate_aws_aoss_config | ||
| required_vars = { | ||
| 'OPENSEARCH_URL' => ENV.fetch('OPENSEARCH_URL', nil), | ||
| 'AWS_ACCESS_KEY_ID' => ENV.fetch('AWS_ACCESS_KEY_ID', nil), | ||
| 'AWS_SECRET_ACCESS_KEY' => ENV.fetch('AWS_SECRET_ACCESS_KEY', nil) | ||
| } | ||
|
|
||
| # Required only when AWS_SESSION_TOKEN is not present (using role assumption) | ||
| required_vars['AWS_ROLE_ARN'] = ENV.fetch('AWS_ROLE_ARN', nil) if ENV['AWS_SESSION_TOKEN'].blank? | ||
|
JPrevost marked this conversation as resolved.
|
||
|
|
||
| validate_required_vars!(required_vars, error_prefix: 'AWS AOSS Config Error') | ||
| end | ||
|
|
||
| # Validates required configuration for AWS-managed OpenSearch. | ||
| # | ||
| # @return [nil] | ||
| # @raise [RuntimeError] when required env vars are missing. | ||
| def validate_aws_os_config | ||
| validate_required_vars!({ | ||
| 'OPENSEARCH_URL' => ENV.fetch('OPENSEARCH_URL', nil), | ||
| 'AWS_ACCESS_KEY_ID' => ENV.fetch('AWS_ACCESS_KEY_ID', nil), | ||
| 'AWS_SECRET_ACCESS_KEY' => ENV.fetch('AWS_SECRET_ACCESS_KEY', nil) | ||
| }, error_prefix: 'AWS OpenSearch Config Error') | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # Raises a standardized configuration error for missing env vars. | ||
| # | ||
| # @param required_vars [Hash{String => Object}] Mapping of env var names | ||
| # to their current values. | ||
| # @param error_prefix [String] Prefix used to identify the validator path. | ||
| # @return [nil] | ||
| # @raise [RuntimeError] when one or more values are blank. | ||
| def validate_required_vars!(required_vars, error_prefix:) | ||
| missing_vars = required_vars.select { |_key, value| value.blank? }.keys | ||
|
|
||
| return unless missing_vars.any? | ||
|
|
||
| raise "#{error_prefix}: These required environment variables are not set: #{missing_vars.join(', ')}" | ||
| end | ||
| end | ||
| end | ||
This file was deleted.
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.
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.
Normalized how the two clients work. This was a good call out.