Class: Fastlane::Helper::SqCiToolsYandexApiHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/sq_ci_tools/helper/sq_ci_tools_yandex_api_helper.rb

Class Method Summary collapse

Class Method Details

.generate_jwt(keys_file_path) ⇒ Object



38
39
40
41
42
# File 'lib/fastlane/plugin/sq_ci_tools/helper/sq_ci_tools_yandex_api_helper.rb', line 38

def self.generate_jwt(keys_file_path)
  key_file_content = JSON.parse(File.read(keys_file_path))
  
  self.signed_token(key_file_content)
end

.get_iam_token(keys_file_path:) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fastlane/plugin/sq_ci_tools/helper/sq_ci_tools_yandex_api_helper.rb', line 14

def self.get_iam_token(keys_file_path:)
  uri = URI.parse("https://iam.api.cloud.yandex.net")

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.set_debug_output($stdout)

  request = Net::HTTP::Post.new("/iam/v1/tokens")
  request.add_field('Content-Type', 'application/json')
  request.body = {
    "jwt" => self.generate_jwt(keys_file_path)
  }.to_json
  response = http.request(request)

  if response.kind_of? Net::HTTPSuccess
    json_body = JSON.parse(response.body)
    return json_body["iamToken"]
  else
    UI.user_error!("Unable to fetch Yandex API IAM Token: #{response.message}, #{response.body}")
  end
end

.load_private_key(key_file_content) ⇒ Object



65
66
67
68
69
# File 'lib/fastlane/plugin/sq_ci_tools/helper/sq_ci_tools_yandex_api_helper.rb', line 65

def self.load_private_key(key_file_content)
  OpenSSL::PKey::RSA.new(key_file_content['private_key'])
rescue IOError, JSON::ParserError, OpenSSL::PKey::RSAError => e
  raise "Failed to load or parse private key: #{e.message}"
end

.signed_token(key_file_content) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fastlane/plugin/sq_ci_tools/helper/sq_ci_tools_yandex_api_helper.rb', line 44

def self.signed_token(key_file_content)
   = key_file_content['service_account_id']
  key_id = key_file_content['id']

  payload = {
    iss: ,
    exp: Time.now.to_i + 3600,
    iat: Time.now.to_i,
    nbf: Time.now.to_i,
    aud: "https://iam.api.cloud.yandex.net/iam/v1/tokens"
  }

  header = {
    kid: key_id
  }

  private_key = self.load_private_key(key_file_content)

  JWT.encode(payload, private_key, 'PS256', header)
end