Class: Aspera::Plugins::Cos::Api

Inherits:
Node::Api show all
Defined in:
lib/aspera/plugins/cos/api.rb

Overview

Cloud Object Storage Node API Client

Constant Summary collapse

IBM_CLOUD_TOKEN_URL =
'https://iam.cloud.ibm.com/identity'
TOKEN_FIELD =
'delegated_refresh_token'
FASP_INFO_KEYS =
%w[ATSEndpoint AccessKey].freeze
SERVICE_CREDS_PARAMS =
%w[apikey resource_instance_id endpoints].freeze

Constants inherited from Node::Api

Node::Api::ACCESS_LEVELS, Node::Api::HEADER_ACCEPT_VERSION, Node::Api::HEADER_X_ASPERA_ACCESS_KEY, Node::Api::HEADER_X_CACHE_CONTROL, Node::Api::HEADER_X_NEXT_ITER_TOKEN, Node::Api::HEADER_X_TOTAL_COUNT, Node::Api::OPTIONS, Node::Api::PATH_SEPARATOR

Instance Attribute Summary

Attributes inherited from Node::Api

#app_info

Attributes inherited from Rest::Client

#auth_params, #base_url, #headers

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node::Api

add_cache_control, add_private_key, add_public_key, #add_tspec_info, #base_spec, bearer_headers, bearer_token, decode_bearer_token, #entry_has_link_information, #find_files, #list_files, #node_id_to_node, #process_folder_tree, #read_folder_content, #read_with_pages, #read_with_paging, #resolve_api_fid, #resolve_api_fid_paths, split_folder, #transfer_spec_gen4, #transport_params

Methods inherited from Rest::Client

#call, #cancel, #create, #delete, #oauth, #params, #read, #update

Constructor Details

#initialize(instance_id:, api_key:, endpoint:, bucket:, auth_url: IBM_CLOUD_TOKEN_URL) ⇒ Api

Returns a new instance of Api.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/aspera/plugins/cos/api.rb', line 46

def initialize(instance_id:, api_key:, endpoint:, bucket:, auth_url: IBM_CLOUD_TOKEN_URL)
  Aspera.assert_type(instance_id, String) { 'resource instance id (crn)' }
  Aspera.assert_type(endpoint, String) { 'endpoint' }
  endpoint = "https://#{endpoint}" unless endpoint.start_with?('http')
  @auth_url = auth_url
  @api_key = api_key
  s3_api = Aspera::Rest::Client.new(
    base_url:       endpoint,
    not_auth_codes: %w[401 403], # error codes when not authorized
    headers:        {'ibm-service-instance-id' => instance_id},
    auth:           {
      type:          :oauth2,
      grant_method:  :generic,
      base_url:      @auth_url,
      grant_type:    'urn:ibm:params:oauth:grant-type:apikey',
      response_type: 'cloud_iam',
      apikey:        @api_key
    }
  )
  # read FASP connection information for bucket
  xml_result_text = s3_api.call(
    operation: 'GET',
    subpath:   bucket,
    headers:   {'Accept' => 'application/xml'},
    query:     {'faspConnectionInfo' => nil},
    ret:       :resp
  ).body
  ats_info = XmlSimple.xml_in(xml_result_text, {'ForceArray' => false})
  Log.dump(:ats_info, ats_info)
  Aspera.assert_hash_all(ats_info, String, nil) { 'ats_info' }
  Aspera.assert((FASP_INFO_KEYS - ats_info.keys).empty?, 'ats_info missing required keys')
  Aspera.assert_hash_all(ats_info['AccessKey'], String, String) { 'ats_info' }
  @storage_credentials = {
    'type'  => 'token',
    'token' => {TOKEN_FIELD => nil}
  }
  super(
    base_url: ats_info['ATSEndpoint'],
    auth:     {
      type:     :basic,
      username: ats_info['AccessKey']['Id'],
      password: ats_info['AccessKey']['Secret']
    },
    add_tspec: {'tags'=>{Aspera::Transfer::Spec::TAG_RESERVED=>{'node'=>{'storage_credentials'=>@storage_credentials}}}}
    )
  # update storage_credentials AND Rest params
  generate_token
end

Class Method Details

.parameters_from_svc_credentials(service_credentials, bucket_region) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/aspera/plugins/cos/api.rb', line 26

def parameters_from_svc_credentials(service_credentials, bucket_region)
  # check necessary contents
  Aspera.assert_type(service_credentials, Hash, type: ParameterError) { 'service_credentials' }
  Log.dump(:service_credentials, service_credentials)
  SERVICE_CREDS_PARAMS.each do |field|
    Aspera.assert(service_credentials.key?(field), type: ParameterError) { "service_credentials must have a field: #{field}" }
  end
  # read endpoints from service provided in service credentials
  endpoints = Aspera::Rest::Client.new(base_url: service_credentials['endpoints']).read('')
  Log.dump(:endpoints, endpoints)
  endpoint = endpoints.dig('service-endpoints', 'regional', bucket_region, 'public', bucket_region)
  Aspera.assert(!endpoint.nil?, type: ParameterError) { "no such region: #{bucket_region}" }
  return {
    instance_id: service_credentials['resource_instance_id'],
    api_key:     service_credentials['apikey'],
    endpoint:    endpoint
  }
end

Instance Method Details

#generate_token ⇒ Object

potentially call this if delegated token is expired



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/aspera/plugins/cos/api.rb', line 96

def generate_token
  # OAuth API to get delegated token
  delegated_oauth = OAuth::Factory.instance.create(
    base_url:     @auth_url,
    grant_method: :generic,
    token_field:  TOKEN_FIELD,
    grant_type:          'urn:ibm:params:oauth:grant-type:apikey',
    response_type:       'delegated_refresh_token',
    apikey:              @api_key,
    receiver_client_ids: 'aspera_ats'
  )
  # get delegated token to be placed in rest call header and in transfer tags
  @storage_credentials['token'][TOKEN_FIELD] = delegated_oauth.token
  headers['X-Aspera-Storage-Credentials'] = JSON.generate(@storage_credentials)
end