Module: OCI::Auth::Util

Defined in:
lib/oci/auth/util.rb

Overview

Contains utility methods to support functionality in the OCI::Auth module, for example being able to extract information from certificates and scrubbing certificate information for calls to Auth Service

Constant Summary collapse

AUTHORIZATION_HEADER =
'Authorization'.freeze
AUTHORIZATION_HEADER_VALUE =
'Bearer Oracle'.freeze

Class Method Summary collapse

Class Method Details

.colon_separate_fingerprint(raw_fingerprint) ⇒ Object



26
27
28
# File 'lib/oci/auth/util.rb', line 26

def self.colon_separate_fingerprint(raw_fingerprint)
  raw_fingerprint.gsub(/(.{2})(?=.)/, '\1:\2')
end

.get_metadata_request(request_url, type) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/oci/auth/util.rb', line 38

def self.(request_url, type)
  case type
  when 'post'
    request = Net::HTTP::Post.new(request_url)
  when 'get'
    request = Net::HTTP::Get.new(request_url)
  when 'put'
    request = Net::HTTP::Put.new(request_url)
  else
    raise "Unknown request-type #{type} provided."
  end
  request[AUTHORIZATION_HEADER] = AUTHORIZATION_HEADER_VALUE
  request
end

.get_tenancy_id_from_certificate(x509_certificate) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/oci/auth/util.rb', line 12

def self.get_tenancy_id_from_certificate(x509_certificate)
  subject_array = x509_certificate.subject.to_a
  subject_array.each do |subject_name|
    # subject_name is actually a triple like:
    #   ["OU", "<name>", "<number>"]
    if subject_name[0] == 'OU' && subject_name[1].include?('opc-tenant:')
      # 'opc-tenant:' is 11 character long, so we want to start at the index after that and to the end of the string (-1)
      return subject_name[1][11..-1]
    end
  end

  raise 'Certificate did not contain a tenancy in its subject'
end

.sanitize_certificate_string(cert_string) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/oci/auth/util.rb', line 30

def self.sanitize_certificate_string(cert_string)
  cert_string.gsub('-----BEGIN CERTIFICATE-----', '')
             .gsub('-----END CERTIFICATE-----', '')
             .gsub('-----BEGIN PUBLIC KEY-----', '')
             .gsub('-----END PUBLIC KEY-----', '')
             .delete("\n")
end