Module: Robocap::SDK::CLI

Defined in:
lib/robocap/sdk/cli.rb

Constant Summary collapse

USAGE =
<<~TXT
  Usage: robocap-decryption-sdk <command> [options]

  Commands:
    import-rsa     Import an RSA key pair into the vault
    delete-rsa     Delete one RSA key version from the vault
    decrypt-cenc   Decrypt a CENC MP4 (RSA-OAEP CEK unwrap + ffmpeg)

  Run `robocap-decryption-sdk <command> --help` for command-specific options.
TXT

Class Method Summary collapse

Class Method Details

.cmd_decrypt_cenc(argv) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/robocap/sdk/cli.rb', line 116

def cmd_decrypt_cenc(argv)
  opts = {}
  OptionParser.new do |o|
    o.banner = 'Usage: robocap-decryption-sdk decrypt-cenc [options]'
    o.on('--mp4-path PATH')     { |v| opts[:mp4_path] = Pathname(v) }
    o.on('--private-key PATH')  { |v| opts[:private_key] = Pathname(v) }
    o.on('--output-dir PATH')   { |v| opts[:output_dir] = Pathname(v) }
    o.on('--sdk-root PATH')     { |v| opts[:sdk_root] = Pathname(v) }
    o.on('--ffmpeg PATH')       { |v| opts[:ffmpeg_executable] = v }
    o.on('--ffprobe PATH')      { |v| opts[:ffprobe_executable] = v }
  end.parse!(argv)

  require_opts!(opts, %i[mp4_path private_key output_dir])
  opts[:output_dir].mkpath
  result = Robocap::SDK.decrypt_cenc_mp4(
    mp4_path: opts[:mp4_path],
    user_private_pem: opts[:private_key].binread,
    output_dir: opts[:output_dir],
    sdk_root: opts[:sdk_root],
    ffmpeg_executable: opts[:ffmpeg_executable],
    ffprobe_executable: opts[:ffprobe_executable],
  )
  emit_json(
    status: 'ok',
    output_path: result.output_path.to_s,
    customer_id: result.customer_id,
    rsa_key_version: result.rsa_key_version,
    kid_hex: result.kid_hex,
  )
end

.cmd_delete_rsa(argv) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/robocap/sdk/cli.rb', line 93

def cmd_delete_rsa(argv)
  opts = {}
  OptionParser.new do |o|
    o.banner = 'Usage: robocap-decryption-sdk delete-rsa [options]'
    o.on('--customer-id CID') { |v| opts[:customer_id] = v }
    o.on('--rsa-key-version N', Integer) { |v| opts[:rsa_key_version] = v }
    o.on('--sdk-root PATH')   { |v| opts[:sdk_root] = Pathname(v) }
  end.parse!(argv)

  require_opts!(opts, %i[customer_id rsa_key_version])
  result = Robocap::SDK.delete_rsa_key_version(
    customer_id: opts[:customer_id],
    rsa_key_version: opts[:rsa_key_version],
    sdk_root: opts[:sdk_root],
  )
  emit_json(
    status: 'ok',
    customer_id: result.customer_id,
    rsa_key_version: opts[:rsa_key_version],
    folder_name: result.folder_name,
  )
end

.cmd_import_rsa(argv) ⇒ Object



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
# File 'lib/robocap/sdk/cli.rb', line 55

def cmd_import_rsa(argv)
  opts = {}
  OptionParser.new do |o|
    o.banner = 'Usage: robocap-decryption-sdk import-rsa [options]'
    o.on('--customer-id CID')       { |v| opts[:customer_id] = v }
    o.on('--public-key PATH')       { |v| opts[:public_key] = Pathname(v) }
    o.on('--private-key PATH')      { |v| opts[:private_key] = Pathname(v) }
    o.on('--rsa-key-version N', Integer) { |v| opts[:rsa_key_version] = v }
    o.on('--rsa-bits N',     ['2048', '4096']) { |v| opts[:rsa_bits] = v.to_i }
    o.on('--device-id ID')          { |v| opts[:device_id] = v }
    o.on('--effective-at ISO8601')  { |v| opts[:effective_at] = v }
    o.on('--sdk-root PATH')         { |v| opts[:sdk_root] = Pathname(v) }
  end.parse!(argv)

  require_opts!(opts, %i[customer_id public_key private_key rsa_key_version])
  opts[:rsa_bits] ||= 2048
  eff = opts[:effective_at] ? Time.iso8601(opts[:effective_at]) : Time.now.utc
  meta = RsaKeyMeta.new(
    rsa_key_version: opts[:rsa_key_version],
    effective_at: eff,
    device_id: opts[:device_id] || opts[:customer_id],
    rsa_bits: opts[:rsa_bits],
  )
  result = Robocap::SDK.import_rsa_key_version(
    customer_id: opts[:customer_id],
    public_pem: opts[:public_key].binread,
    private_pem: opts[:private_key].binread,
    meta: meta,
    sdk_root: opts[:sdk_root],
  )
  emit_json(
    status: 'ok',
    customer_id: result.customer_id,
    rsa_key_version: result.rsa_key_version,
    vault_rsa_dir: result.vault_rsa_dir.to_s,
  )
end

.emit_json(data) ⇒ Object



51
52
53
# File 'lib/robocap/sdk/cli.rb', line 51

def emit_json(data)
  puts JSON.generate(data)
end

.require_opts!(opts, keys) ⇒ Object

Raises:

  • (OptionParser::ParseError)


147
148
149
150
151
# File 'lib/robocap/sdk/cli.rb', line 147

def require_opts!(opts, keys)
  missing = keys.reject { |k| opts.key?(k) }
  return if missing.empty?
  raise OptionParser::ParseError, "missing required option(s): #{missing.map { |k| "--#{k.to_s.tr('_', '-')}" }.join(', ')}"
end

.run(argv) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/robocap/sdk/cli.rb', line 25

def run(argv)
  command = argv.shift
  case command
  when 'import-rsa'   then with_error_handling { cmd_import_rsa(argv) }
  when 'delete-rsa'   then with_error_handling { cmd_delete_rsa(argv) }
  when 'decrypt-cenc' then with_error_handling { cmd_decrypt_cenc(argv) }
  when '--help', '-h', nil
    warn(USAGE)
    0
  else
    warn("Unknown command: #{command}\n\n#{USAGE}")
    2
  end
end

.with_error_handlingObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/robocap/sdk/cli.rb', line 40

def with_error_handling
  yield
  0
rescue Error => exc
  warn(JSON.generate(exc.to_h))
  1
rescue OptionParser::ParseError => exc
  warn(exc.message)
  2
end