Module: NewRelic::Agent::Aws
- Defined in:
- lib/new_relic/agent/aws.rb
Constant Summary collapse
- CHARACTERS =
%w[A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 2 3 4 5 6 7].freeze
- HEX_MASK =
'7fffffffff80'
Class Method Summary collapse
- .convert_access_key_to_account_id(access_key) ⇒ Object
- .convert_section(section) ⇒ Object
- .create_arn(service, resource, config) ⇒ Object
- .decode_to_hex(access_key) ⇒ Object
Class Method Details
.convert_access_key_to_account_id(access_key) ⇒ Object
20 21 22 23 24 |
# File 'lib/new_relic/agent/aws.rb', line 20 def self.convert_access_key_to_account_id(access_key) decoded_key = Integer(decode_to_hex(access_key[4..-1]), 16) mask = Integer(HEX_MASK, 16) (decoded_key & mask) >> 7 end |
.convert_section(section) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/new_relic/agent/aws.rb', line 34 def self.convert_section(section) buffer = 0 section.each do |chunk| buffer = (buffer << 5) + chunk end chunk_count = (section.length * 5.0 / 8.0).floor if section.length < 8 buffer >>= (5 - (chunk_count * 8)) % 5 end decoded = [] chunk_count.times do |i| shift = 8 * (chunk_count - 1 - i) decoded << ((buffer >> shift) & 255).to_s(16) end decoded end |
.create_arn(service, resource, config) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/new_relic/agent/aws.rb', line 11 def self.create_arn(service, resource, config) region = config.region account_id = NewRelic::Agent::Aws.convert_access_key_to_account_id(config.credentials.access_key_id) "arn:aws:#{service}:#{region}:#{account_id}:#{resource}" rescue => e NewRelic::Agent.logger.warn("Failed to create ARN: #{e}") end |
.decode_to_hex(access_key) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/new_relic/agent/aws.rb', line 26 def self.decode_to_hex(access_key) bytes = access_key.delete('=').each_char.map { |c| CHARACTERS.index(c) } bytes.each_slice(8).map do |section| convert_section(section) end.flatten[0...6].join end |