Skip to content

Commit

Permalink
Added Sigv4 Debug Logging (#150)
Browse files Browse the repository at this point in the history
* Added Sigv4 Debug Logging

Signed-off-by: Theo Truong <[email protected]>
  • Loading branch information
nhtruong authored Feb 28, 2023
1 parent a07ee16 commit 4db656f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 43 deletions.
55 changes: 16 additions & 39 deletions .github/workflows/test-unreleased.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,34 @@ jobs:
strategy:
fail-fast: false
matrix:
opensearch_ref: [ '1.x', '2.x', '2.0', 'main' ]
ruby_version: 3.1
entry:
- { opensearch_ref: '1.x' }
- { opensearch_ref: '2.x' }
- { opensearch_ref: '2.0' }
- { opensearch_ref: 'main' }
steps:
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby_version }}
ruby-version: 3.1

- name: Checkout OpenSearch
uses: actions/checkout@v3
uses: actions/checkout@v2
with:
repository: opensearch-project/OpenSearch
ref: ${{ matrix.opensearch_ref }}
ref: ${{ matrix.entry.opensearch_ref }}
path: opensearch

- name: Get OpenSearch branch top
id: get-key
working-directory: opensearch
run: echo key=`git log -1 --format='%H'` >> $GITHUB_OUTPUT

- name: Restore cached build
id: cache-restore
uses: actions/cache/restore@v3
with:
path: opensearch/distribution/archives/linux-tar/build/distributions
key: ${{ steps.get-key.outputs.key }}

- name: Assemble OpenSearch
if: steps.cache-restore.outputs.cache-hit != 'true'
working-directory: opensearch
run: ./gradlew :distribution:archives:linux-tar:assemble

- name: Save cached build
if: steps.cache-restore.outputs.cache-hit != 'true'
uses: actions/cache/save@v3
with:
path: opensearch/distribution/archives/linux-tar/build/distributions
key: ${{ steps.get-key.outputs.key }}
run: |
cd opensearch
./gradlew assemble
- name: Run OpenSearch
working-directory: opensearch/distribution/archives/linux-tar/build/distributions
# This step runs the docker image generated during gradle assemble in OpenSearch. It is tagged as opensearch:test.
# Reference: https://github.com/opensearch-project/OpenSearch/blob/2.0/distribution/docker/build.gradle#L190
- name: Run Docker Image
run: |
tar xf opensearch-min-*
./opensearch-*/bin/opensearch &
for attempt in {1..20}; do sleep 5; if curl -s localhost:9200; then echo '=====> ready'; break; fi; echo '=====> waiting...'; done
docker run -p 9200:9200 -p 9600:9600 -d -e "discovery.type=single-node" -e "bootstrap.memory_lock=true" opensearch:test
sleep 90
- name: Checkout Ruby Client
uses: actions/checkout@v2
Expand All @@ -80,10 +64,3 @@ jobs:
run: cd opensearch-dsl && bundle exec rake test:all
- name: opensearch-aws-sigv4
run: cd opensearch-aws-sigv4 && bundle exec rake test:all

- name: Save server logs
if: failure()
uses: actions/upload-artifact@v3
with:
name: opensearch-logs-${{ matrix.opensearch_ref }}-ruby-${{ matrix.ruby_version }}
path: opensearch/distribution/archives/linux-tar/build/distributions/**/logs/*
10 changes: 8 additions & 2 deletions opensearch-aws-sigv4/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## [Unreleased]
### Added
- Added support for Amazon OpenSearch Serverless ([#131](https://github.com/opensearch-project/opensearch-ruby/issues/131))

- Ability to printout Sigv4 Signature for debugging ([#149](https://github.com/opensearch-project/opensearch-ruby/issues/149))
### Changed
### Deprecated
### Removed
### Fixed

### Security

## [1.1.0]
### Added
- Added support for Amazon OpenSearch Serverless ([#131](https://github.com/opensearch-project/opensearch-ruby/issues/131))
### Fixed
- Sign validation requests when using AWS Sigv4 ([#134](https://github.com/opensearch-project/opensearch-ruby/pull/134))

### Security
Expand Down
31 changes: 30 additions & 1 deletion opensearch-aws-sigv4/lib/opensearch-aws-sigv4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ class Sigv4Client < ::OpenSearch::Client
# @param [Hash] transport_args arguments for OpenSearch::Transport::Client.
# @param [&block] block code block to be passed to OpenSearch::Transport::Client.
# @param [Aws::Sigv4::Signer] sigv4_signer an instance of AWS Sigv4 Signer.
def initialize(transport_args = {}, sigv4_signer, &block)
# @param [Hash] options
# @option options [Boolean] :sigv4_debug whether to log debug info for Sigv4 Signing
def initialize(transport_args = {}, sigv4_signer, options: {}, &block)
unless sigv4_signer.is_a?(::Aws::Sigv4::Signer)
raise ArgumentError, "Please pass a Aws::Sigv4::Signer. A #{sigv4_signer.class} was given."
end

@sigv4_signer = sigv4_signer
@sigv4_debug = options[:sigv4_debug]
@logger = nil
super(transport_args, &block)
end

Expand All @@ -57,6 +61,8 @@ def perform_request(method, path, params = {}, body = nil, headers = nil)
headers: headers,
body: signature_body)
headers = (headers || {}).merge(signature.headers)

log_signature_info(signature)
super(method, path, params, body, headers)
end

Expand All @@ -72,6 +78,29 @@ def signature_url(path, params)
query_string = params.empty? ? '' : "#{Faraday::Utils::ParamsHash[params].to_query}"
URI::HTTP.build(host: host, path: path, query: query_string)
end

# @param [Aws::Sigv4::Signature] signature
def log_signature_info(signature)
return unless @sigv4_debug

log('string to sign', signature.string_to_sign)
log('canonical request', signature.canonical_request)
log('signature headers', signature.headers)
end

def log(title, message)
logger.debug("#{title.upcase}:\n\e[36m#{message}\e[0m")
end

def logger
return @logger if @logger

require 'logger'
@logger = Logger.new(
STDOUT,
progname: 'Sigv4',
formatter: proc { |_severity, datetime, progname, msg| "\e[34m(#{datetime}) #{progname} - #{msg}\e[0m\n\n" })
end
end
end
end
2 changes: 1 addition & 1 deletion opensearch-aws-sigv4/lib/opensearch-aws-sigv4/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
module OpenSearch
module Aws
module Sigv4
VERSION = '1.1.0'.freeze
VERSION = '1.2.0'.freeze
end
end
end

0 comments on commit 4db656f

Please sign in to comment.