Class: TencentCloud::Common::OIDCCredential

Inherits:
Object
  • Object
show all
Defined in:
lib/tencentcloud-sdk-common/oidc_credential.rb

Constant Summary collapse

SES_NAME =
'tencentcloud-ruby-sdk-'
SES_DUR =
7200
API_VERSION =
'2018-08-13'
API_ENDPOINT =
'sts.tencentcloudapi.com'
API_ACTION =
'AssumeRoleWithWebIdentity'
SDK_VERSION =
'CLB_' + File.read(File.expand_path('../VERSION', __dir__)).strip

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOIDCCredential



16
17
18
19
# File 'lib/tencentcloud-sdk-common/oidc_credential.rb', line 16

def initialize
  @expire_t = 0
  initialize_args
end

Instance Attribute Details

#secret_idObject

Returns the value of attribute secret_id.



14
15
16
# File 'lib/tencentcloud-sdk-common/oidc_credential.rb', line 14

def secret_id
  @secret_id
end

#secret_keyObject

Returns the value of attribute secret_key.



14
15
16
# File 'lib/tencentcloud-sdk-common/oidc_credential.rb', line 14

def secret_key
  @secret_key
end

#tokenObject

Returns the value of attribute token.



14
15
16
# File 'lib/tencentcloud-sdk-common/oidc_credential.rb', line 14

def token
  @token
end

Instance Method Details

#credentialObject



21
22
23
24
# File 'lib/tencentcloud-sdk-common/oidc_credential.rb', line 21

def credential
  refresh
  [@secret_id, @secret_key, @token]
end

#initialize_argsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tencentcloud-sdk-common/oidc_credential.rb', line 26

def initialize_args
  @region = ENV['TKE_REGION']
  @provider_id = ENV['TKE_PROVIDER_ID']
  token_file = ENV['TKE_WEB_IDENTITY_TOKEN_FILE']
  @role_arn = ENV['TKE_ROLE_ARN']
  @ses_name = SES_NAME + (Time.now.to_r * 1_000).to_i.to_s
  @ses_dur = SES_DUR

  if @region.nil? || @provider_id.nil? || token_file.nil? || @role_arn.nil? || @ses_name.nil? || @ses_dur.nil?
    raise TencentCloudSDKException.new('InvalidCredential', 'env TKE_REGION, TKE_PROVIDER_ID, TKE_WEB_IDENTITY_TOKEN_FILE, TKE_ROLE_ARN not exist')
  end

  @token = File.read(token_file).strip
end

#refreshObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/tencentcloud-sdk-common/oidc_credential.rb', line 41

def refresh
  if @expire_t - Time.now.to_i > SES_DUR / 10
    return
  end

  initialize_args

  client = AbstractClient.new(nil, @region, API_VERSION, API_ENDPOINT, SDK_VERSION, nil)

  req = {
    'ProviderId': @provider_id,
    'WebIdentityToken': @token,
    'RoleArn': @role_arn,
    'RoleSessionName': @ses_name,
    'DurationSeconds': @ses_dur,
  }
  response = JSON.parse(client.send_request(API_ACTION, req))
  if response['Response'].key?('Error')
    code = response['Response']['Error']['Code']
    message = response['Response']['Error']['Message']
    reqid = response['Response']['RequestId']
    raise TencentCloud::Common::TencentCloudSDKException.new(code, message, reqid)
  end

  @secret_id = response['Response']['Credentials']['TmpSecretId']
  @secret_key = response['Response']['Credentials']['TmpSecretKey']
  @token = response['Response']['Credentials']['Token']
  @expire_t = response['Response']['ExpiredTime']
end