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

Class Method Details

.convert_access_key_to_account_id(access_key) ⇒ Object



32
33
34
35
36
# File 'lib/new_relic/agent/aws.rb', line 32

def self.(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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/new_relic/agent/aws.rb', line 46

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, region, account_id) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/new_relic/agent/aws.rb', line 11

def self.create_arn(service, resource, region, )
  # if any of the values are nil, we can't create an ARN
  return unless service && resource && region && 

  "arn:aws:#{service}:#{region}:#{}:#{resource}"
rescue => e
  NewRelic::Agent.logger.warn("Failed to create ARN: #{e}")
end

.decode_to_hex(access_key) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/new_relic/agent/aws.rb', line 38

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

.get_account_id(config) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/new_relic/agent/aws.rb', line 20

def self.(config)
  # if it is set in the agent config, use that first
  return NewRelic::Agent.config[:'cloud.aws.account_id'] if NewRelic::Agent.config[:'cloud.aws.account_id']

  access_key_id = config.credentials.credentials.access_key_id if config&.credentials&.credentials&.respond_to?(:access_key_id)
  return unless access_key_id

  NewRelic::Agent::Aws.(access_key_id)
rescue => e
  NewRelic::Agent.logger.debug("Failed to create account id: #{e}")
end