Class: Aspera::Ascp::DataRepository

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/aspera/ascp/data_repository.rb

Overview

a simple binary data repository

Constant Summary collapse

ELEMENTS =

names of data items

FILES.keys.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instance ⇒ DataRepository

Returns the singleton instance of DataRepository

Returns:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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/aspera/ascp/data_repository.rb', line 19

class DataRepository
  include Singleton

  # name of data item => name of its file in data folder
  FILES = {
    dsa:                        '1',
    rsa:                        '2',
    uuid:                       '3',
    'aspera.global-cli-client': '4',
    'aspera.drive':             '5',
    license:                    '6'
  }.freeze
  # names of data items
  ELEMENTS = FILES.keys.freeze
  DATA_FOLDER_NAME = 'data'

  # Get a data item, decoded to its usable form
  # @param name [Symbol] name of the data item, one of ELEMENTS
  # @return [String] PEM private key (dsa, rsa), UUID (uuid), base64url secret (client apps), or text (license)
  def item(name)
    Aspera.assert_values(name, ELEMENTS, type: ParameterError) { 'data item' }
    raw_data = data(name)
    value =
      case name
      when :dsa, :rsa
        # generate PEM from DER
        # Class of key, not `OpenSSL::PKey.read`: on JRuby (jruby-openssl 0.16), it fails on these keys (DER, traditional format)
        {dsa: OpenSSL::PKey::DSA, rsa: OpenSSL::PKey::RSA}[name].new(raw_data).to_pem
      when :license
        Zlib::Inflate.inflate(raw_data)
      when :uuid
        format('%08x-%04x-%04x-%04x-%04x%08x', *raw_data.unpack('NnnnnN'))
      when :'aspera.global-cli-client', :'aspera.drive'
        Base64.urlsafe_encode64(raw_data)
      else Aspera.error_unexpected_value(name)
      end
    # all items are text
    return value.force_encoding(Encoding::UTF_8)
  end

  private

  private_constant :FILES, :DATA_FOLDER_NAME

  # get binary value from data repository
  # @param name [Symbol] name of the data item
  # @return [String] raw binary data
  def data(name)
    File.binread(File.join(__dir__, DATA_FOLDER_NAME, FILES[name]))
  end
end

Instance Method Details

#item(name) ⇒ String

Get a data item, decoded to its usable form

Parameters:

  • name (Symbol) —

    name of the data item, one of ELEMENTS

Returns:

  • (String) —

    PEM private key (dsa, rsa), UUID (uuid), base64url secret (client apps), or text (license)



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/aspera/ascp/data_repository.rb', line 38

def item(name)
  Aspera.assert_values(name, ELEMENTS, type: ParameterError) { 'data item' }
  raw_data = data(name)
  value =
    case name
    when :dsa, :rsa
      # generate PEM from DER
      # Class of key, not `OpenSSL::PKey.read`: on JRuby (jruby-openssl 0.16), it fails on these keys (DER, traditional format)
      {dsa: OpenSSL::PKey::DSA, rsa: OpenSSL::PKey::RSA}[name].new(raw_data).to_pem
    when :license
      Zlib::Inflate.inflate(raw_data)
    when :uuid
      format('%08x-%04x-%04x-%04x-%04x%08x', *raw_data.unpack('NnnnnN'))
    when :'aspera.global-cli-client', :'aspera.drive'
      Base64.urlsafe_encode64(raw_data)
    else Aspera.error_unexpected_value(name)
    end
  # all items are text
  return value.force_encoding(Encoding::UTF_8)
end