aspera-cli 4.29.1 → 4.30.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (264) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +4 -4
  3. data/CHANGELOG.md +119 -0
  4. data/CONTRIBUTING.md +8 -1
  5. data/README.md +2 -0
  6. data/TODO.md +4 -5
  7. data/bin/asession +2 -2
  8. data/docs/README.md +616 -86
  9. data/docs/test-mcp-with-ai.md +4 -0
  10. data/lib/aspera/agent/base.rb +62 -6
  11. data/lib/aspera/agent/factory.rb +127 -20
  12. data/lib/aspera/ascp/data_repository.rb +71 -0
  13. data/lib/aspera/ascp/installation.rb +16 -11
  14. data/lib/aspera/ascp/log_analyzer/lines.rb +4 -4
  15. data/lib/aspera/ascp/log_analyzer/log_file.rb +5 -3
  16. data/lib/aspera/ascp/log_analyzer/records.rb +11 -6
  17. data/lib/aspera/ascp/log_analyzer.rb +43 -18
  18. data/lib/aspera/ascp/log_locations.rb +2 -2
  19. data/lib/aspera/assert.rb +8 -0
  20. data/lib/aspera/bundle_registry.rb +528 -0
  21. data/lib/aspera/cli/argument.rb +110 -0
  22. data/lib/aspera/cli/bool_value.rb +41 -0
  23. data/lib/aspera/cli/bootstrapper.rb +31 -24
  24. data/lib/aspera/cli/command/mount.rb +53 -0
  25. data/lib/aspera/cli/command/registry.rb +434 -0
  26. data/lib/aspera/cli/command.rb +95 -0
  27. data/lib/aspera/cli/command_line.rb +91 -91
  28. data/lib/aspera/cli/complete.rb +22 -0
  29. data/lib/aspera/cli/completion_actions.rb +28 -26
  30. data/lib/aspera/cli/context.rb +29 -6
  31. data/lib/aspera/cli/error.rb +39 -0
  32. data/lib/aspera/cli/extended_value.rb +25 -4
  33. data/lib/aspera/cli/formatter.rb +8 -8
  34. data/lib/aspera/cli/hints.rb +17 -52
  35. data/lib/aspera/cli/http.rb +17 -12
  36. data/lib/aspera/cli/mailer.rb +7 -24
  37. data/lib/aspera/cli/mcp/tool.rb +10 -2
  38. data/lib/aspera/cli/option/declarator.rb +80 -0
  39. data/lib/aspera/cli/option/registry.rb +74 -0
  40. data/lib/aspera/cli/option/source.rb +38 -0
  41. data/lib/aspera/cli/option/type.rb +37 -0
  42. data/lib/aspera/cli/option/value.rb +376 -0
  43. data/lib/aspera/cli/option.rb +78 -0
  44. data/lib/aspera/cli/options.schema.yaml +28 -783
  45. data/lib/aspera/cli/parser.rb +73 -41
  46. data/lib/aspera/cli/plugins/base.rb +122 -42
  47. data/lib/aspera/cli/plugins/basic_auth.rb +13 -2
  48. data/lib/aspera/cli/plugins/config.rb +101 -167
  49. data/lib/aspera/cli/plugins/factory.rb +139 -43
  50. data/lib/aspera/cli/plugins/mcp.rb +61 -54
  51. data/lib/aspera/cli/plugins/oauth.rb +19 -6
  52. data/lib/aspera/cli/poll.rb +14 -20
  53. data/lib/aspera/cli/preset_actions.rb +11 -3
  54. data/lib/aspera/cli/preset_manager.rb +16 -6
  55. data/lib/aspera/cli/result.rb +2 -2
  56. data/lib/aspera/cli/runner.rb +71 -99
  57. data/lib/aspera/cli/secret_finder.rb +1 -1
  58. data/lib/aspera/cli/security.rb +102 -4
  59. data/lib/aspera/{preview/terminal.rb → cli/terminal_image.rb} +93 -94
  60. data/lib/aspera/cli/vault_manager.rb +3 -3
  61. data/lib/aspera/cli/version.rb +1 -1
  62. data/lib/aspera/cli/wizard.rb +9 -3
  63. data/lib/aspera/coverage.rb +1 -2
  64. data/lib/aspera/line_logger.rb +35 -6
  65. data/lib/aspera/log.rb +3 -2
  66. data/lib/aspera/oauth/client/base.rb +19 -1
  67. data/lib/aspera/oauth/client/factory.rb +1 -7
  68. data/lib/aspera/plugin.schema.yaml +178 -0
  69. data/lib/aspera/plugins/alee/api.rb +25 -0
  70. data/lib/aspera/{cli/plugins/alee.rb → plugins/alee/commands.rb} +10 -8
  71. data/lib/aspera/plugins/alee/plugin.yaml +3 -0
  72. data/lib/aspera/plugins/aoc/admin_resources.rb +148 -0
  73. data/lib/aspera/plugins/aoc/api.rb +783 -0
  74. data/lib/aspera/{cli/plugins/aoc.rb → plugins/aoc/commands.rb} +404 -330
  75. data/lib/aspera/plugins/aoc/graphql.rb +45 -0
  76. data/lib/aspera/plugins/aoc/plugin.yaml +31 -0
  77. data/lib/aspera/{schema → plugins/aoc/schemas}/IBM Aspera on Cloud API-0.2.6-enhanced.yaml +15042 -7076
  78. data/lib/aspera/plugins/aoc/schemas/options.yaml +47 -0
  79. data/lib/aspera/plugins/ats/api.rb +51 -0
  80. data/lib/aspera/{cli/plugins/ats.rb → plugins/ats/commands.rb} +33 -33
  81. data/lib/aspera/plugins/ats/plugin.yaml +6 -0
  82. data/lib/aspera/plugins/connect/agent.rb +199 -0
  83. data/lib/aspera/plugins/connect/plugin.yaml +11 -0
  84. data/lib/aspera/plugins/connect/schemas/agent_options.yaml +6 -0
  85. data/lib/aspera/{cli/plugins/console.rb → plugins/console/commands.rb} +41 -33
  86. data/lib/aspera/plugins/console/plugin.yaml +8 -0
  87. data/lib/aspera/plugins/cos/api.rb +114 -0
  88. data/lib/aspera/{cli/plugins/cos.rb → plugins/cos/commands.rb} +19 -16
  89. data/lib/aspera/plugins/cos/plugin.yaml +9 -0
  90. data/lib/aspera/plugins/cos/schemas/options.yaml +30 -0
  91. data/lib/aspera/plugins/desktop/agent.rb +165 -0
  92. data/lib/aspera/plugins/desktop/plugin.yaml +11 -0
  93. data/lib/aspera/plugins/desktop/schemas/agent_options.yaml +6 -0
  94. data/lib/aspera/plugins/direct/agent.rb +518 -0
  95. data/lib/aspera/plugins/direct/plugin.yaml +11 -0
  96. data/lib/aspera/plugins/direct/schemas/agent_options.yaml +86 -0
  97. data/lib/aspera/plugins/faspex4/gateway_options.rb +18 -0
  98. data/lib/aspera/plugins/faspex4/gw_servlet.rb +122 -0
  99. data/lib/aspera/plugins/faspex4/plugin.yaml +7 -0
  100. data/lib/aspera/plugins/faspex4/schemas/options.yaml +57 -0
  101. data/lib/aspera/plugins/faspex5/api.rb +269 -0
  102. data/lib/aspera/{cli/plugins/faspex5.rb → plugins/faspex5/commands.rb} +152 -114
  103. data/lib/aspera/plugins/faspex5/faspex4_post_proc_servlet.rb +241 -0
  104. data/lib/aspera/plugins/faspex5/plugin.yaml +14 -0
  105. data/lib/aspera/{schema → plugins/faspex5/schemas}/IBM Aspera Faspex API-5.0-enhanced.yaml +279 -76
  106. data/lib/aspera/plugins/faspex5/schemas/options.yaml +87 -0
  107. data/lib/aspera/{cli/plugins/faspio.rb → plugins/faspio/commands.rb} +11 -7
  108. data/lib/aspera/plugins/faspio/plugin.yaml +8 -0
  109. data/lib/aspera/{schema → plugins/faspio/schemas}/IBM Aspera faspio Gateway API-1.0.0-enhanced.yaml +4 -2
  110. data/lib/aspera/plugins/httpgw/agent.rb +126 -0
  111. data/lib/aspera/plugins/httpgw/api.rb +411 -0
  112. data/lib/aspera/{cli/plugins/httpgw.rb → plugins/httpgw/commands.rb} +9 -9
  113. data/lib/aspera/plugins/httpgw/plugin.yaml +12 -0
  114. data/lib/aspera/plugins/httpgw/schemas/agent_options.yaml +24 -0
  115. data/lib/aspera/plugins/node/agent.rb +184 -0
  116. data/lib/aspera/plugins/node/api.rb +699 -0
  117. data/lib/aspera/{cli/plugins/node.rb → plugins/node/commands.rb} +166 -141
  118. data/lib/aspera/plugins/node/emulator.rb +774 -0
  119. data/lib/aspera/plugins/node/plugin.yaml +20 -0
  120. data/lib/aspera/plugins/node/schemas/agent_options.yaml +20 -0
  121. data/lib/aspera/plugins/node/schemas/options.yaml +190 -0
  122. data/lib/aspera/{cli/plugins/orchestrator.rb → plugins/orchestrator/commands.rb} +139 -110
  123. data/lib/aspera/plugins/orchestrator/plugin.yaml +34 -0
  124. data/lib/aspera/{schema → plugins/orchestrator/schemas}/IBM Aspera Orchestrator API-v1-enhanced.yaml +486 -16
  125. data/lib/aspera/plugins/orchestrator/schemas/options.yaml +143 -0
  126. data/lib/aspera/plugins/orchestrator/workflow_checker.rb +692 -0
  127. data/lib/aspera/{cli/plugins/preview.rb → plugins/preview/commands.rb} +53 -51
  128. data/lib/aspera/plugins/preview/file_types.rb +121 -0
  129. data/lib/aspera/plugins/preview/generator.rb +350 -0
  130. data/lib/aspera/plugins/preview/plugin.yaml +12 -0
  131. data/lib/aspera/{preview/options.schema.yaml → plugins/preview/schemas/options.yaml} +1 -1
  132. data/lib/aspera/plugins/preview/utils.rb +213 -0
  133. data/lib/aspera/plugins/server/ascmd.rb +374 -0
  134. data/lib/aspera/{cli/plugins/server.rb → plugins/server/commands.rb} +48 -32
  135. data/lib/aspera/plugins/server/plugin.yaml +9 -0
  136. data/lib/aspera/plugins/server/schemas/options.yaml +30 -0
  137. data/lib/aspera/{cli/plugins/shares.rb → plugins/shares/commands.rb} +29 -21
  138. data/lib/aspera/plugins/shares/plugin.yaml +10 -0
  139. data/lib/aspera/plugins/shares/shares_web.rb +97 -0
  140. data/lib/aspera/plugins/transferd/agent.rb +258 -0
  141. data/lib/aspera/plugins/transferd/api.rb +180 -0
  142. data/lib/aspera/plugins/transferd/client.rb +67 -0
  143. data/lib/aspera/{cli/plugins/transferd.rb → plugins/transferd/commands.rb} +43 -42
  144. data/lib/aspera/plugins/transferd/config_commands.rb +38 -0
  145. data/lib/aspera/plugins/transferd/plugin.yaml +27 -0
  146. data/lib/aspera/plugins/transferd/product.rb +146 -0
  147. data/lib/aspera/plugins/transferd/schemas/agent_options.yaml +31 -0
  148. data/lib/aspera/plugins/transferd/schemas/options.yaml +80 -0
  149. data/lib/aspera/plugins/transferd/service.rb +617 -0
  150. data/lib/aspera/plugins/xfer/ascp_actions.rb +131 -0
  151. data/lib/aspera/plugins/xfer/ascp_logs_actions.rb +198 -0
  152. data/lib/aspera/plugins/xfer/async_transfer_store.rb +131 -0
  153. data/lib/aspera/plugins/xfer/config_commands.rb +119 -0
  154. data/lib/aspera/plugins/xfer/helper.rb +19 -0
  155. data/lib/aspera/plugins/xfer/plugin.yaml +42 -0
  156. data/lib/aspera/plugins/xfer/schemas/agent_extension.schema.yaml +26 -0
  157. data/lib/aspera/plugins/xfer/schemas/options.yaml +125 -0
  158. data/lib/aspera/plugins/xfer/sync_actions.rb +253 -0
  159. data/lib/aspera/plugins/xfer/transfer_actions.rb +176 -0
  160. data/lib/aspera/plugins/xfer/transfer_agent.rb +505 -0
  161. data/lib/aspera/products/cli_v3.rb +34 -0
  162. data/lib/aspera/products/connect.rb +1 -1
  163. data/lib/aspera/products/drive.rb +29 -0
  164. data/lib/aspera/products/hsts.rb +34 -0
  165. data/lib/aspera/products/transferd.rb +25 -3
  166. data/lib/aspera/products.rb +54 -0
  167. data/lib/aspera/prompt.rb +184 -0
  168. data/lib/aspera/proxy_auto_config.rb +3 -3
  169. data/lib/aspera/rest/aspera_errors.rb +4 -38
  170. data/lib/aspera/rest/error_analyzer.rb +6 -2
  171. data/lib/aspera/rest/list.rb +4 -5
  172. data/lib/aspera/schema/reader.rb +15 -0
  173. data/lib/aspera/schema/registry.rb +239 -45
  174. data/lib/aspera/schema/validator.rb +63 -17
  175. data/lib/aspera/sealed_hash.rb +4 -0
  176. data/lib/aspera/secret/hider.rb +159 -24
  177. data/lib/aspera/secret/reference.rb +8 -0
  178. data/lib/aspera/secret/resolver.rb +8 -4
  179. data/lib/aspera/secret/store/base.rb +10 -9
  180. data/lib/aspera/secret/store/encrypted_hash.rb +6 -6
  181. data/lib/aspera/secret/store/hashicorp_vault.rb +3 -3
  182. data/lib/aspera/secret/store/keeper.rb +2 -2
  183. data/lib/aspera/secret/store/macos_security.rb +2 -3
  184. data/lib/aspera/secret/store/one_password_api.rb +2 -2
  185. data/lib/aspera/secret/store/one_password_cli.rb +3 -3
  186. data/lib/aspera/secret/store/secret_service.rb +2 -2
  187. data/lib/aspera/ssl.rb +3 -3
  188. data/lib/aspera/standard_input.rb +20 -0
  189. data/lib/aspera/{schema → sync}/async_tables.yaml +1 -1
  190. data/lib/aspera/sync/database.rb +7 -0
  191. data/lib/aspera/sync/log_parser.rb +138 -0
  192. data/lib/aspera/sync/operations.rb +59 -34
  193. data/lib/aspera/transfer/command_line_builder.rb +248 -0
  194. data/lib/aspera/transfer/command_line_converter.rb +37 -0
  195. data/lib/aspera/transfer/faux_file.rb +2 -2
  196. data/lib/aspera/transfer/parameters.rb +9 -10
  197. data/lib/aspera/transfer/spec.rb +28 -1
  198. data/lib/aspera/transfer/spec.schema.yaml +30 -5
  199. data/lib/aspera/units/decimal.rb +1 -1
  200. data/lib/aspera/units/timestamp.rb +84 -0
  201. data/lib/aspera/uri_reader.rb +2 -2
  202. data/lib/aspera/web_auth/paste.rb +2 -2
  203. data/lib/aspera/web_server_simple.rb +10 -4
  204. data.tar.gz.sig +0 -0
  205. metadata +137 -80
  206. metadata.gz.sig +0 -0
  207. data/lib/aspera/agent/connect.rb +0 -188
  208. data/lib/aspera/agent/desktop.rb +0 -154
  209. data/lib/aspera/agent/direct.rb +0 -496
  210. data/lib/aspera/agent/httpgw.rb +0 -100
  211. data/lib/aspera/agent/node.rb +0 -166
  212. data/lib/aspera/agent/transferd.rb +0 -341
  213. data/lib/aspera/api/alee.rb +0 -19
  214. data/lib/aspera/api/aoc.rb +0 -776
  215. data/lib/aspera/api/ats.rb +0 -49
  216. data/lib/aspera/api/cos_node.rb +0 -107
  217. data/lib/aspera/api/faspex.rb +0 -258
  218. data/lib/aspera/api/httpgw.rb +0 -409
  219. data/lib/aspera/api/node.rb +0 -684
  220. data/lib/aspera/ascmd.rb +0 -248
  221. data/lib/aspera/cli/ascp_actions.rb +0 -113
  222. data/lib/aspera/cli/ascp_logs_actions.rb +0 -188
  223. data/lib/aspera/cli/async_transfer_store.rb +0 -129
  224. data/lib/aspera/cli/command_registry.rb +0 -371
  225. data/lib/aspera/cli/command_spec.rb +0 -273
  226. data/lib/aspera/cli/option_declarator.rb +0 -74
  227. data/lib/aspera/cli/option_registry.rb +0 -72
  228. data/lib/aspera/cli/option_types.rb +0 -120
  229. data/lib/aspera/cli/option_value.rb +0 -345
  230. data/lib/aspera/cli/prompt.rb +0 -186
  231. data/lib/aspera/cli/sync_actions.rb +0 -181
  232. data/lib/aspera/cli/transfer_actions.rb +0 -98
  233. data/lib/aspera/cli/transfer_agent.rb +0 -348
  234. data/lib/aspera/command_line_builder.rb +0 -244
  235. data/lib/aspera/command_line_converter.rb +0 -35
  236. data/lib/aspera/data_repository.rb +0 -69
  237. data/lib/aspera/faspex_gw.rb +0 -172
  238. data/lib/aspera/faspex_postproc.rb +0 -237
  239. data/lib/aspera/graphql.rb +0 -41
  240. data/lib/aspera/node_emulator.rb +0 -758
  241. data/lib/aspera/oauth/client/shares_web.rb +0 -94
  242. data/lib/aspera/orchestrator_workflow_checker.rb +0 -688
  243. data/lib/aspera/preview/file_types.rb +0 -119
  244. data/lib/aspera/preview/generator.rb +0 -345
  245. data/lib/aspera/preview/utils.rb +0 -211
  246. data/lib/aspera/products/other.rb +0 -92
  247. data/lib/aspera/products/transferd_service.rb +0 -610
  248. /data/lib/aspera/{data → ascp/data}/1 +0 -0
  249. /data/lib/aspera/{data → ascp/data}/2 +0 -0
  250. /data/lib/aspera/{data → ascp/data}/3 +0 -0
  251. /data/lib/aspera/{data → ascp/data}/4 +0 -0
  252. /data/lib/aspera/{data → ascp/data}/5 +0 -0
  253. /data/lib/aspera/{data → ascp/data}/6 +0 -0
  254. /data/lib/aspera/{api → plugins/aoc}/queries/bss_subscription_account.graphql +0 -0
  255. /data/lib/aspera/{api → plugins/aoc}/queries/bss_subscription_usage.graphql +0 -0
  256. /data/lib/aspera/{schema → plugins/aoc/schemas}/IBM Aspera on Cloud Automation API-1.0.5-enhanced.yaml +0 -0
  257. /data/lib/aspera/{schema → plugins/console/schemas}/IBM Aspera Console-enhanced.yaml +0 -0
  258. /data/lib/aspera/{schema → plugins/node/schemas}/IBM Aspera Node API-4.4.6.yaml +0 -0
  259. /data/lib/aspera/{preview → plugins/preview}/image_error.png +0 -0
  260. /data/lib/aspera/{preview → plugins/preview}/video_error.png +0 -0
  261. /data/lib/aspera/{schema → plugins/shares/schemas}/IBM_Aspera_Shares.yaml +0 -0
  262. /data/lib/{transferd_pb.rb → aspera/plugins/transferd/grpc/transferd_pb.rb} +0 -0
  263. /data/lib/{transferd_services_pb.rb → aspera/plugins/transferd/grpc/transferd_services_pb.rb} +0 -0
  264. /data/lib/aspera/{schema → plugins/transferd/schemas}/transferd-config.schema.json +0 -0
@@ -5,6 +5,7 @@
5
5
 
6
6
  require_relative 'extended_value'
7
7
  require_relative 'vault_manager'
8
+ require_relative 'security'
8
9
  require_relative '../dot_container'
9
10
  require_relative 'version'
10
11
  require_relative 'info'
@@ -58,6 +59,7 @@ module Aspera
58
59
  @use_plugin_defaults = use_plugin_defaults
59
60
  @config_presets = {}
60
61
  @checksum_on_disk = nil
62
+ @checksum_loaded = nil
61
63
  read_config_file
62
64
  end
63
65
 
@@ -114,19 +116,26 @@ module Aspera
114
116
  end
115
117
  # Hidden wherever they appear (logs, results), also when copied under another key name
116
118
  Secret::Hider.instance.add_secrets_in(@config_presets)
119
+ # After the changes made by the load itself (version stamp): saved also at a security level that refuses changes
120
+ @checksum_loaded = checksum
117
121
  end
118
122
 
119
123
  # Save to disk only if content changed since last load/save.
124
+ # At security levels `medium` and `high`, the configuration is read-only: a command shall not change it (the commands that write it are refused),
125
+ # so that its presets are the ones of the operator, see `Security#check_credential_host`.
120
126
  # @return [Boolean] true if actually written
127
+ # @raise [BadArgument] If the presets were changed, at security level `medium` or `high`
121
128
  def save_if_needed
122
129
  Aspera.assert(!@config_presets.nil?, type: Cli::Error) { 'no configuration loaded' }
123
130
  current = checksum
124
131
  return false if @checksum_on_disk.eql?(current)
132
+ Security.instance.check_level(:low, 'saving the configuration file') unless current.eql?(@checksum_loaded)
125
133
  FileUtils.mkdir_p(File.dirname(@config_file))
126
134
  Environment.restrict_file_access(File.dirname(@config_file))
127
135
  Log.log.info { "Saving config file: #{@config_file}" }
128
136
  Environment.write_file_restricted(@config_file, force: true) { @config_presets.to_yaml }
129
137
  @checksum_on_disk = current
138
+ @checksum_loaded = current
130
139
  true
131
140
  end
132
141
 
@@ -142,7 +151,7 @@ module Aspera
142
151
  Aspera.assert(!@config_presets.nil?, 'config_presets shall be defined')
143
152
  return unless @use_plugin_defaults
144
153
  return unless @config_presets.key?(Key::DEFAULTS)
145
- Aspera.assert_type(@config_presets[Key::DEFAULTS], Hash) { 'default section' }
154
+ Aspera.assert_type(@config_presets[Key::DEFAULTS], Hash, type: Cli::Error) { 'default section' }
146
155
  return unless @config_presets[Key::DEFAULTS].key?(plugin_name_sym.to_s)
147
156
  default_name = @config_presets[Key::DEFAULTS][plugin_name_sym.to_s]
148
157
  unless @config_presets.key?(default_name)
@@ -160,7 +169,7 @@ module Aspera
160
169
 
161
170
  # Resolve and return a deep-cloned, extended copy of a named preset.
162
171
  # Supports dot-notation (e.g. "outer.inner").
163
- # Meta keys (prefix `_`) are removed, extended values are evaluated, and a secret read alone is declared to the secret hider.
172
+ # Meta keys (prefix `_`) are removed, extended values are evaluated, a secret read alone is declared to the secret hider, and values are declared as read from the configuration.
164
173
  # @param config_name [String] Preset name, optionally followed by dot-separated sub keys
165
174
  # @param include_path [Array<String>] Names already being resolved, to detect include loops
166
175
  # @return [Hash, String, Object] Copy of the preset, or of the selected sub value
@@ -184,7 +193,8 @@ module Aspera
184
193
  current = ExtendedValue.instance.evaluate(current, context: 'preset')
185
194
  # A secret read alone has no key name: declared, so that it is hidden wherever it appears
186
195
  Secret::Hider.instance.add_secret_values(current) if Secret::Hider.instance.secret?(names.last, current)
187
- current
196
+ # Values of the configuration: a secret can be sent to its URLs only, see `Security#check_credential_host`
197
+ Security.instance.add_configured(current)
188
198
  end
189
199
 
190
200
  # Set a single key in a preset hash (creates the preset if absent).
@@ -204,7 +214,7 @@ module Aspera
204
214
  Log.log.debug { "Unknown preset name: #{preset}, initializing" }
205
215
  selected = @config_presets[preset] = {}
206
216
  end
207
- Aspera.assert_type(selected, Hash) { "#{preset}.#{param_name}" }
217
+ Aspera.assert_type(selected, Hash, type: Cli::Error) { "#{preset}.#{param_name}" }
208
218
  if param_name.include?(DotContainer::SEPARATOR)
209
219
  keys = param_name.split(DotContainer::SEPARATOR)
210
220
  # Navigate to the parent hash
@@ -361,8 +371,8 @@ module Aspera
361
371
  # @raise [Cli::Error, StandardError] Unexpected structure
362
372
  def validate_config_presets!
363
373
  Log.dump(:available_presets, @config_presets, level: :trace1)
364
- Aspera.assert_type(@config_presets, Hash) { 'config file YAML' }
365
- Aspera.assert(@config_presets.key?(Key::CONFIG)) { "Cannot find key: #{Key::CONFIG}" }
374
+ Aspera.assert_type(@config_presets, Hash, type: Cli::Error) { 'config file YAML' }
375
+ Aspera.assert(@config_presets.key?(Key::CONFIG), type: Cli::Error) { "Cannot find key: #{Key::CONFIG}" }
366
376
  version = @config_presets[Key::CONFIG][Key::VERSION]
367
377
  Aspera.assert(!version.nil?, type: Cli::Error) { 'No version found in config section.' }
368
378
  Log.log.debug { "conf version: #{version}" }
@@ -9,7 +9,7 @@ require_relative '../nagios'
9
9
  require_relative '../log'
10
10
  require_relative '../environment'
11
11
  require_relative '../uri_reader'
12
- require_relative '../preview/terminal'
12
+ require_relative 'terminal_image'
13
13
  require_relative '../dot_container'
14
14
  require 'base64'
15
15
  require 'uri'
@@ -83,7 +83,7 @@ module Aspera
83
83
  data
84
84
  end
85
85
  # here, data is the image blob
86
- formatter.display_message(:data, Preview::Terminal.build(data, **formatter.image_options))
86
+ formatter.display_message(:data, TerminalImage.build(data, **formatter.image_options))
87
87
  end
88
88
  else Aspera.error_unexpected_value(formatter.format_type) { 'format' }
89
89
  end
@@ -3,7 +3,7 @@
3
3
  # SPDX-FileCopyrightText: IBM Corp.
4
4
  # SPDX-License-Identifier: Apache-2.0
5
5
 
6
- require_relative 'command_spec'
6
+ require_relative 'argument'
7
7
  require_relative 'context'
8
8
  require_relative 'parser'
9
9
  require_relative 'formatter'
@@ -16,14 +16,14 @@ require_relative 'mailer'
16
16
  require_relative 'secret_finder'
17
17
  require_relative 'extended_value'
18
18
  require_relative 'security'
19
- require_relative 'transfer_agent'
20
19
  require_relative 'version'
21
20
  require_relative 'info'
22
21
  require_relative 'hints'
23
22
  require_relative 'result'
24
- require_relative '../transfer/result'
25
23
  require_relative '../units/duration'
26
24
  require_relative '../secret/hider'
25
+ require_relative '../secret/reference'
26
+ require_relative '../ssh'
27
27
  require_relative '../log'
28
28
  require_relative '../assert'
29
29
  require_relative '../schema/documentation'
@@ -36,71 +36,18 @@ module Aspera
36
36
  module Cli
37
37
  # The main CLI class
38
38
  class Runner
39
- # Plugins store transfer result using this key and use result_transfer_multiple()
40
- STATUS_FIELD = 'status'
41
-
42
- class << self
43
- # Process the typed result of a finished (or submitted) transfer.
44
- # @param transfer_result [Transfer::Result] typed result from TransferAgent#start
45
- # @raise [StandardError] if the transfer failed
46
- # @return [Result] CLI result object
47
- def result_transfer(transfer_result)
48
- case transfer_result
49
- when Transfer::Result::Async
50
- return Result::SingleObject.new(transfer_result.to_h)
51
- when Transfer::Result::Error
52
- raise transfer_result.exception
53
- when Transfer::Result::Success
54
- return Result::Nothing.new
55
- when Transfer::Result::DryRun
56
- # Secrets are hidden by the formatter, as in any result
57
- return Result::SingleObject.new(transfer_result.transfer_spec)
58
- else
59
- raise "Unexpected transfer result type: #{transfer_result.class}"
60
- end
61
- end
62
-
63
- # Result of a command that creates an object and transfers its files, e.g. `packages send`.
64
- # @param object [Hash] Created object
65
- # @param transfer_result [Transfer::Result] Typed result from TransferAgent#start
66
- # @raise [StandardError] if the transfer failed
67
- # @return [Result::SingleObject] The object, with field `transfer` (`job_id`) if the transfer is asynchronous
68
- def result_object_transfer(object, transfer_result)
69
- status = result_transfer(transfer_result)
70
- object = object.merge('transfer' => status.data) if status.is_a?(Result::SingleObject)
71
- Result::SingleObject.new(object)
72
- end
73
-
74
- # Used when one command executes several transfer jobs (each job being possibly multi session)
75
- # @param status_table [Array] [{STATUS_FIELD=>Transfer::Result,...},...]
76
- # @return [Result] a status object suitable as command result
77
- def result_transfer_multiple(status_table)
78
- failed_result = nil
79
- status_table.each do |item|
80
- tr = item[STATUS_FIELD]
81
- case tr
82
- when Transfer::Result::Error
83
- failed_result ||= tr
84
- item[STATUS_FIELD] = tr.exception.message
85
- when Transfer::Result::Success
86
- item[STATUS_FIELD] = 'success'
87
- when Transfer::Result::Async
88
- item[STATUS_FIELD] = "async:#{tr.job_id}"
89
- when Transfer::Result::DryRun
90
- item[STATUS_FIELD] = 'dry_run'
91
- else
92
- item[STATUS_FIELD] = tr.to_s
93
- end
94
- end
95
- raise failed_result.exception unless failed_result.nil?
96
- return Result::ObjectList.new(status_table)
97
- end
98
- end
99
-
100
39
  # Minimum initialization, no exception raised
101
40
  # @param argv [Array<String>] command line arguments
41
+ # @param main_folder [String, nil] main folder set by the program (not by the arguments), e.g. the one of the MCP server; `nil`: option `home`
42
+ # @param config_file [String, nil] configuration file set by the program (not by the arguments), e.g. the one of the MCP server; `nil`: option `config_file`
43
+ # @param vault [Hash, nil] option `vault` set by the program (not by the arguments), e.g. the one of the MCP server; `nil`: option `vault`
44
+ # @param vault_password [String, nil] option `vault_password` set by the program (not by the arguments); `nil`: option `vault_password`
102
45
  # @return [nil]
103
- def initialize(argv)
46
+ def initialize(argv, main_folder: nil, config_file: nil, vault: nil, vault_password: nil)
47
+ @main_folder = main_folder
48
+ @config_file = config_file
49
+ @vault = vault
50
+ @vault_password = vault_password
104
51
  # Dumped (trace2) with secrets hidden by `CommandLine`
105
52
  @argv = argv
106
53
  @option_help = false
@@ -115,11 +62,21 @@ module Aspera
115
62
  # @return [Result, nil] the result of the command, or nil if nothing to execute
116
63
  def run_with_result
117
64
  init_agents_and_options
65
+ @context.config.add_plugin_lookup_folders
118
66
  Plugins::Factory.instance.add_plugins_from_lookup_folders
67
+ # Config is created before the plugin folders are added: modules mounted on config by their bundles, with their options
68
+ Plugins::Config.include_mounts(Plugins::Factory.instance.registry)
69
+ Plugins::Config.declare_options(@context.options, target: @context.config, parse: true)
70
+ # The service `transfer` declares its options (after those of config), and parses option `transfer`: agents of the bundles of the plugin folders are known
71
+ @context.transfer
119
72
  # Help requested without command? Show global options + plugin list
120
73
  return result_usage if @option_help && @context.options.command_or_arg_empty?
121
74
  # Shell completion is executed on each `Tab`: no network access
122
75
  @context.config.periodic_check_newer_gem_version unless @context.options.next_arguments?(COMPLETION_COMMAND)
76
+ # A plugin that cannot be loaded is not in the list of commands: give the reason, not an unknown command
77
+ Plugins::Factory.instance.unavailable_plugins.each do |name, reason|
78
+ raise Cli::Error, reason if @context.options.next_arguments?([name.to_s])
79
+ end
123
80
  command_sym =
124
81
  if @option_show_config && @context.options.command_or_arg_empty?
125
82
  COMMAND_CONFIG
@@ -185,16 +142,22 @@ module Aspera
185
142
  @context.formatter.display_results(result) if result
186
143
  rescue Net::SSH::AuthenticationFailed => e; exception_info = {e: e, t: 'SSH', security: true, exit: ExitCode::AUTH}
187
144
  rescue OpenSSL::SSL::SSLError => e; exception_info = {e: e, t: 'SSL', exit: ExitCode::NETWORK}
188
- rescue Cli::BadArgument => e; exception_info = {e: e, t: 'Argument', usage: true, exit: ExitCode::USAGE}
145
+ rescue Cli::BadArgument, ParameterError => e; exception_info = {e: e, t: 'Argument', usage: true, exit: ExitCode::USAGE}
189
146
  rescue Cli::MissingArgument => e; exception_info = {e: e, t: 'Missing', exit: ExitCode::USAGE}
190
- rescue Cli::BadIdentifier, EntityNotFound => e; exception_info = {e: e, t: 'Identifier', exit: ExitCode::NOT_FOUND}
147
+ rescue Cli::BadIdentifier, EntityNotFound, Aspera::Secret::NotFound => e
148
+ exception_info = {e: e, t: 'Identifier', exit: ExitCode::NOT_FOUND}
149
+ rescue AuthenticationError, Aspera::Secret::AuthenticationError => e
150
+ exception_info = {e: e, t: 'Authentication', exit: ExitCode::AUTH}
191
151
  rescue Cli::SchemaRequest => e; exception_info = {e: e, t: 'Schema', exit: ExitCode::SUCCESS}
192
152
  rescue Cli::PollConditionNotMet => e; exception_info = {e: e, t: 'Poll', exit: ExitCode::ERROR}
193
153
  rescue Cli::Error => e; exception_info = {e: e, t: 'Tool', usage: true, exit: ExitCode::ERROR}
194
- rescue Transfer::Error => e; exception_info = {e: e, t: 'Transfer', exit: ExitCode::TRANSFER}
154
+ rescue EnvironmentError => e; exception_info = {e: e, t: 'Environment', exit: ExitCode::ERROR}
155
+ # Label `Vault`, as the option: with `Secret`, the log would hide a message of one word, as a secret value
156
+ rescue Aspera::Secret::Error => e; exception_info = {e: e, t: 'Vault', exit: ExitCode::ERROR}
157
+ rescue Aspera::Ssh::Error => e; exception_info = {e: e, t: 'SSH', exit: ExitCode::ERROR}
195
158
  rescue Rest::CallError => e; exception_info = {e: e, t: 'Rest', exit: ExitCode.for_call_error(e)}
196
159
  rescue *NETWORK_ERRORS => e; exception_info = {e: e, t: 'Network', exit: ExitCode::NETWORK}
197
- rescue StandardError => e; exception_info = {e: e, t: "Other(#{e.class.name})", debug: true, exit: ExitCode::ERROR}
160
+ rescue StandardError => e; exception_info = other_error_info(e)
198
161
  rescue Interrupt => e; exception_info = {e: e, t: 'Interruption', debug: true, exit: ExitCode::INTERRUPTED}
199
162
  end
200
163
  # 1- processing of error condition
@@ -307,7 +270,7 @@ module Aspera
307
270
  when Numeric then v
308
271
  else Units::Duration.new(v).to_unit('s')
309
272
  end
310
- else Aspera.error_unexpected_value(k, type: BadArgument) { 'temp sub-option (folder, clean, expiry)' }
273
+ else Aspera.error_unexpected_value(k) { 'temp sub-option (folder, clean, expiry)' }
311
274
  end
312
275
  end
313
276
  rescue ParameterError => e
@@ -316,6 +279,16 @@ module Aspera
316
279
 
317
280
  private
318
281
 
282
+ # @param error [StandardError] Error that is neither of the CLI, nor of a REST call, nor of the network
283
+ # @return [Hash] Information on the error: label and exit code of its category if a bundle added one (`ExitCode.add_error`),
284
+ # else `Error` for an error raised on purpose by the gem, else `Other` with location: a bug (`AssertError`, `InternalError`), or an error not of the gem
285
+ def other_error_info(error)
286
+ category = ExitCode.error_category(error)
287
+ return {e: error, t: category[:label], exit: category[:code]} unless category.nil?
288
+ return {e: error, t: 'Error', exit: ExitCode::ERROR} if error.is_a?(Aspera::Error) && !error.is_a?(AssertError) && !error.is_a?(InternalError)
289
+ {e: error, t: "Other(#{error.class.name})", debug: true, exit: ExitCode::ERROR}
290
+ end
291
+
319
292
  # Location of an unexpected error in the code of a plugin (built-in or from option `plugin_folder`)
320
293
  # @param error [Exception] Error
321
294
  # @return [String, nil] First line of the backtrace in a plugin folder, e.g. `<FOLDER>/my_plugin.rb:12:in 'action_list'`, `nil` if none
@@ -340,14 +313,20 @@ module Aspera
340
313
  lines << "\nPLUGINS"
341
314
  col_w = plugin_names.map { |n| n.to_s.length }.max + 2
342
315
  plugin_names.each do |name|
343
- app = Plugins::Factory.instance.plugin_class(name).application_name
316
+ app =
317
+ begin
318
+ Plugins::Factory.instance.plugin_class(name).application_name
319
+ rescue Cli::Error => e
320
+ # A plugin that cannot be loaded does not prevent the help
321
+ e.message
322
+ end
344
323
  lines << " #{name.to_s.ljust(col_w)} #{app}"
345
324
  end
346
325
  else
347
326
  path = plugin.help_path || []
348
327
  registry = plugin.class.command_registry
349
328
  cmds = registry.children_of(path)
350
- label = plugin.class.name.split('::').last.downcase
329
+ label = plugin.class.plugin_name
351
330
  # Build label with positional argument slots inserted after each command
352
331
  # e.g. [:access_keys, :do, :download] -> "node access_keys do <access_key_id> download"
353
332
  path.each_index do |i|
@@ -370,15 +349,8 @@ module Aspera
370
349
  lines << "\nCOMMAND: #{label}"
371
350
  lines << " #{spec.description}" if spec&.description
372
351
  display_args = registry.arguments_at(path)
373
- # transfer_paths commands use --sources for the file list; default is positional args (@args)
374
- if spec&.transfer_paths
375
- file_desc = if spec.transfer_paths == :receive
376
- "Remote path(s) to download (default --sources=#{TransferAgent::FILE_LIST_FROM_ARGS}; see also --to-folder)"
377
- else
378
- "Source file(s) to upload (default --sources=#{TransferAgent::FILE_LIST_FROM_ARGS}; see also --src-type, --to-folder)"
379
- end
380
- display_args += [ArgumentSpec.new(name: :source_file, description: file_desc, mandatory: false, multiple: true)]
381
- end
352
+ # The files of a command with `transfer_paths` are given as the service `transfer` reads them (e.g. positional arguments)
353
+ display_args += [@context.transfer.paths_argument(spec.transfer_paths)] if spec&.transfer_paths
382
354
  if display_args.any?
383
355
  lines << "\nARGUMENTS:"
384
356
  col_w = display_args.map { |a| a.name.to_s.length }.max + 2
@@ -403,7 +375,7 @@ module Aspera
403
375
  end
404
376
 
405
377
  # Render the structure of a Hash argument's JSON schema in command help.
406
- # @param arg [ArgumentSpec]
378
+ # @param arg [Argument]
407
379
  # @return [Array<String>]
408
380
  def schema_help_lines(arg)
409
381
  description = free_form_description(arg.schema)
@@ -450,10 +422,9 @@ module Aspera
450
422
  end
451
423
 
452
424
  # @param schema_path [String] Path of the schema in the registry
453
- # @return [Schema::Documentation] Documentation of the schema, for the terminal
425
+ # @return [Schema::Documentation] Documentation of the schema, for the terminal, with the options given by the code that registered it
454
426
  def schema_documentation(schema_path)
455
- options = schema_path.eql?(Schema::Registry::TRANSFER_SPEC) ? Transfer::Spec.doc_options : {}
456
- Schema::Documentation.new(TerminalFormatter, Schema::Registry.instance.reader(schema_path), **options).build
427
+ Schema::Documentation.new(TerminalFormatter, Schema::Registry.instance.reader(schema_path), **Schema::Registry.instance.doc_options(schema_path)).build
457
428
  end
458
429
 
459
430
  # Initialize agents and options
@@ -475,7 +446,7 @@ module Aspera
475
446
  declare_global_options
476
447
  # Bootstrap: populate context services (main_folder, persistency, presets, vault, http_config,
477
448
  # progress_bar) and configure global singletons before any plugin is instantiated.
478
- @bootstrapper = Bootstrapper.new(@context)
449
+ @bootstrapper = Bootstrapper.new(@context, main_folder: @main_folder, config_file: @config_file, vault: @vault, vault_password: @vault_password)
479
450
  @bootstrapper.run(gem_plugins_folder: Plugins::Config.gem_plugins_folder)
480
451
  # Do not display config commands if help is asked
481
452
  @context.man_header = false
@@ -488,8 +459,7 @@ module Aspera
488
459
  @context.mailer = Mailer.new(@context.options, @context.main_folder)
489
460
  # Secret finder: depends on options (:secret) and presets, both set by Bootstrapper
490
461
  @context.secret_finder = SecretFinder.new(@context.options, @context.presets)
491
- # The TransferAgent plugin may use the @preset parser
492
- @context.transfer = TransferAgent.new(@context)
462
+ # The service `transfer` is created once the plugin folders are added (see `run_with_result`)
493
463
  @context.validate
494
464
  # Set banner when all environment is created so that additional extended value modifiers are known, e.g. @preset
495
465
  end
@@ -524,7 +494,9 @@ module Aspera
524
494
  #{t}Options begin with a '-' (minus), and value is provided on command line.
525
495
  #{t}Special values are supported beginning with special prefix @pfx:, where pfx is one of:
526
496
  #{t}#{ExtendedValue.instance.modifiers.reject(&:empty?).join(', ')}
527
- #{t}Dates format is 'YYYY-MM-DD HH:MM:SS', or 'now' or '-<num>h'
497
+ #{t}Durations: a number of seconds, or with units w, d, h, m, s, ms, us (e.g. 1h30m), or 1d 02:00:00
498
+ #{t}Dates: @date:<DATE> gives <DATE> in UTC, ISO 8601. <DATE> is a date (e.g. 2026-11-27 10:00:00),
499
+ #{t}or now, today, yesterday, tomorrow, optionally followed by -<DURATION> or +<DURATION> (e.g. now-24h)
528
500
 
529
501
  ARGS
530
502
  #{t}Some commands require mandatory arguments, e.g. a path.
@@ -538,12 +510,12 @@ module Aspera
538
510
  # @return [nil]
539
511
  def declare_global_options
540
512
  Log.log.debug('declare_global_options')
541
- @context.options.declare(:help, description: 'Show this message', allowed: Type::NONE, short: 'h') do
513
+ @context.options.declare(:help, description: 'Show this message', allowed: Option::Type::NONE, short: 'h') do
542
514
  @option_help = true
543
515
  @context.help_requested = true
544
516
  end
545
- @context.options.declare(:show_config, description: 'Display parameters used for the provided action', allowed: Type::NONE) { @option_show_config = true }
546
- @context.options.declare(:version, description: 'Display version', allowed: Type::NONE, short: 'v') { @context.formatter.display_message(:data, Cli::VERSION); ::Process.exit(0) } # rubocop:disable Style/Semicolon
517
+ @context.options.declare(:show_config, description: 'Display parameters used for the provided action', allowed: Option::Type::NONE) { @option_show_config = true }
518
+ @context.options.declare(:version, description: 'Display version', allowed: Option::Type::NONE, short: 'v') { @context.formatter.display_message(:data, Cli::VERSION); ::Process.exit(0) } # rubocop:disable Style/Semicolon
547
519
  @context.options.declare(
548
520
  :ui, description: 'User interface (dot-notation: mode, web_auth, hidden_silent, command)',
549
521
  allowed: [Hash, String], shorthand: 'mode', schema: Schema::Registry::UI_OPTIONS,
@@ -559,17 +531,17 @@ module Aspera
559
531
  @context.options.declare(:logger, description: 'Logging method', allowed: Log::LOG_TYPES, on_set: Log.instance.method(:logger_type=))
560
532
  @context.options.declare(:log, description: 'Logging options (dot-notation: level, type, format, secrets)', on_set: method(:option_log=), schema: Schema::Registry::LOG_OPTIONS)
561
533
  @context.options.declare(:security, description: 'Security restrictions (dot-notation: level, allowed_paths)', on_set: Security.instance.method(:options=), schema: Schema::Registry::SECURITY_OPTIONS)
562
- @context.options.declare(:lock_port, description: 'Prevent dual execution of a command, e.g. in cron', allowed: Type::INTEGER)
563
- @context.options.declare(:once_only, description: 'Process only new items (some commands)', allowed: Type::BOOLEAN, default: false)
564
- @context.options.declare(:dry_run, description: 'Do not perform changes, only show what would be done', allowed: Type::BOOLEAN, default: false)
565
- @context.options.declare(:log_secrets, description: 'Show passwords in logs', allowed: Type::BOOLEAN, default: Secret::Hider.instance.log_secrets, on_set: Secret::Hider.instance.method(:log_secrets=))
534
+ @context.options.declare(:lock_port, description: 'Prevent dual execution of a command, e.g. in cron', allowed: Option::Type::INTEGER)
535
+ @context.options.declare(:once_only, description: 'Process only new items (some commands)', allowed: Option::Type::BOOLEAN, default: false)
536
+ @context.options.declare(:dry_run, description: 'Do not perform changes, only show what would be done', allowed: Option::Type::BOOLEAN, default: false)
537
+ @context.options.declare(:log_secrets, description: 'Show passwords in logs', allowed: Option::Type::BOOLEAN, default: Secret::Hider.instance.log_secrets, on_set: Secret::Hider.instance.method(:log_secrets=))
566
538
  @context.options.declare(
567
539
  :temp, description: 'Temporary files (dot-notation: folder, clean, expiry)',
568
540
  allowed: [Hash, String], shorthand: 'folder', schema: Schema::Registry::TEMP_OPTIONS,
569
541
  on_set: method(:option_temp=)
570
542
  )
571
- @context.options.declare(:clean_temp, description: 'Cleanup temporary files on exit', allowed: Type::BOOLEAN, on_set: TempFileManager.instance.method(:cleanup_on_exit=), deprecation: {last: '4.27.5', message: 'use --temp.clean'})
572
- @context.options.declare(:temp_folder, description: 'Temporary folder', on_set: TempFileManager.instance.method(:global_temp=), deprecation: {last: '4.27.5', message: 'use --temp.folder'}, complete: :folder)
543
+ @context.options.declare(:clean_temp, description: 'Cleanup temporary files on exit', allowed: Option::Type::BOOLEAN, on_set: TempFileManager.instance.method(:cleanup_on_exit=), deprecation: {last: '4.27.5', message: 'use --temp.clean'})
544
+ @context.options.declare(:temp_folder, description: 'Temporary folder', on_set: TempFileManager.instance.method(:global_temp=), deprecation: {last: '4.27.5', message: 'use --temp.folder'}, complete: :folder, security: :low)
573
545
  @context.options.declare(:pid_file, description: 'Write process identifier to file, delete on exit', complete: :file, security: :low)
574
546
  @context.options.declare(
575
547
  :parser, description: 'Default parser for structured parameters and options',
@@ -590,7 +562,7 @@ module Aspera
590
562
  # Load default preset options for this plugin from config file
591
563
  default_config_name = @context.presets.plugin_default_name(plugin_name_sym)
592
564
  Log.log.debug { "add_plugin_default_preset:#{plugin_name_sym}:#{default_config_name}" }
593
- @context.options.add_option_preset(@context.presets.by_name(default_config_name), 'default_plugin', override: false) unless default_config_name.nil?
565
+ @context.options.add_option_preset(@context.presets.by_name(default_config_name), 'default_plugin', override: false, configured: true) unless default_config_name.nil?
594
566
  command_plugin = Plugins::Factory.instance.create(plugin_name_sym, context: @context)
595
567
  return command_plugin
596
568
  end
@@ -9,7 +9,7 @@ module Aspera
9
9
  module Cli
10
10
  # Resolves the secret (password) for a given URL + username pair.
11
11
  # Injected into Context as :secret_finder so that any component
12
- # (plugins, Api::AoC, ...) can look up secrets without going through Plugins::Config.
12
+ # (plugins, Plugins::Aoc::Api, ...) can look up secrets without going through Plugins::Config.
13
13
  class SecretFinder
14
14
  # Special value for the :secret option that triggers a preset lookup
15
15
  PRESET_MAGIC = 'PRESET'
@@ -32,13 +32,20 @@ module Aspera
32
32
  }.freeze
33
33
  # Levels that allow processing a secret (e.g. by an extended value): the result could reveal it, as only declared values are hidden
34
34
  SECRET_PROCESSING_LEVELS = %i[low].freeze
35
- private_constant :EXTENDED_VALUE_LEVELS, :SECRET_PROCESSING_LEVELS
35
+ # Sources (`Option::Source`) of an option value set by the one who starts the process (environment, defaults, the program itself), not by a client.
36
+ # A value from a preset is not one of them: a preset can also be given by the client (e.g. `--preset=@json:...`), see #add_configured.
37
+ OPERATOR_SOURCES = %i[default code env].freeze
38
+ # Keys of a `Hash` option (e.g. parameters of agent `node` in option `transfer`) that hold a secret sent to its key `url`
39
+ CREDENTIAL_KEYS = %w[password client_secret private_key passphrase api_key].freeze
40
+ # Keys of a transfer spec that hold a credential for its `remote_host`
41
+ TRANSFER_CREDENTIAL_KEYS = %w[remote_password token ssh_private_key ssh_private_key_passphrase].freeze
42
+ private_constant :EXTENDED_VALUE_LEVELS, :SECRET_PROCESSING_LEVELS, :OPERATOR_SOURCES, :CREDENTIAL_KEYS, :TRANSFER_CREDENTIAL_KEYS
36
43
 
37
44
  def initialize
38
45
  reset
39
46
  end
40
47
 
41
- # Back to default: level `low`, no allowed path, not locked
48
+ # Back to default: level `low`, no allowed path, not locked, no value read from the configuration
42
49
  # @return [nil]
43
50
  def reset
44
51
  @level = nil
@@ -46,6 +53,8 @@ module Aspera
46
53
  @allowed_paths = []
47
54
  @writable_paths = []
48
55
  @locked = nil
56
+ @configured_values = Set.new
57
+ nil
49
58
  end
50
59
 
51
60
  # @return [Symbol] Level set by option `security`, else default level
@@ -141,6 +150,80 @@ module Aspera
141
150
  raise BadArgument, "#{what}: cannot process a secret at security level #{level}, as the result could reveal it: #{hint}, or use level low (#{where_to_set})"
142
151
  end
143
152
 
153
+ # Declare values of the configuration of the server: read from a preset (by its name) or the vault, or set in the environment of the process.
154
+ # The provenance of a value is known by the value itself, whatever the way it is referenced (`--preset=<name>`, `@preset:`, `@vault:`, in a preset),
155
+ # so that #check_credential_host knows that a secret is the one of the server, and that a URL is one of its configuration.
156
+ # @param value [Object] Value read: a `String`, or a `Hash` or `Array` of values
157
+ # @return [Object] The value
158
+ def add_configured(value)
159
+ case value
160
+ when Hash then value.each_value { |v| add_configured(v) }
161
+ when Array then value.each { |v| add_configured(v) }
162
+ when String then @configured_values.add(value)
163
+ end
164
+ value
165
+ end
166
+
167
+ # @param value [Object] Value of an option
168
+ # @return [Boolean] `true` if the value was read from the configuration, see #add_configured
169
+ def configured?(value)
170
+ value.is_a?(String) && @configured_values.include?(value)
171
+ end
172
+
173
+ # A secret of the server must not be sent to a host (option `url`) chosen by a client, so that a client cannot redirect the saved credentials of the server
174
+ # to a host it controls: at levels `medium` and `high`, a secret of the server can be sent only to a URL of its configuration.
175
+ # A secret is the one of the server when it is read from the configuration (preset, vault), whatever the way it is referenced (e.g. `@preset:<name>.password`),
176
+ # or when it comes from an operator source (environment). Same for the URL.
177
+ # The configuration cannot be changed by a client at these levels (commands that write presets and the vault are refused), so its URLs are the ones of the operator.
178
+ # A secret of the server sent to the URL of another of its presets is not refused.
179
+ # @param what [String] For the error message, e.g. `option password`
180
+ # @param secret [Object] Value of the secret option
181
+ # @param secret_source [Symbol, nil] Source of the secret option value (`Option::Source`)
182
+ # @param url [String, nil] Value of option `url`
183
+ # @param url_source [Symbol, nil] Source of option `url`
184
+ # @raise [BadArgument] If, at level `medium` or `high`, the secret is the one of the server, and the URL is not one of its configuration
185
+ def check_credential_host(what:, secret:, secret_source:, url:, url_source:)
186
+ return if level.eql?(:low) || url.nil?
187
+ # The secret is not the one of the server: the client's own
188
+ return unless configured?(secret) || OPERATOR_SOURCES.include?(secret_source)
189
+ return if configured?(url) || OPERATOR_SOURCES.include?(url_source)
190
+ raise BadArgument, "#{what}: a secret of the configuration cannot be sent to a URL that is not in the configuration at security level #{level}: " \
191
+ "use the preset that has both the URL and the secret, or give the secret itself (#{where_to_set})"
192
+ end
193
+
194
+ # Same as #check_credential_host for the keys of a `Hash`, e.g. the parameters of agent `node` in option `transfer`: key `url` and secret keys.
195
+ # The provenance of a key is known only by its value, see #add_configured.
196
+ # @param value [Object] Value of an option, checked if it is a `Hash` with key `url`
197
+ # @param what [String] For the error message, e.g. `option transfer`
198
+ # @raise [BadArgument] If, at level `medium` or `high`, a secret of the configuration is sent to a URL that is not in the configuration
199
+ def check_credentials_in(value, what:)
200
+ return unless value.is_a?(Hash)
201
+ url = value['url'] || value[:url]
202
+ return unless url.is_a?(String)
203
+ CREDENTIAL_KEYS.each do |key|
204
+ secret = value[key] || value[key.to_sym]
205
+ check_credential_host(what: "#{what}: #{key}", secret: secret, secret_source: nil, url: url, url_source: nil) unless secret.nil?
206
+ end
207
+ end
208
+
209
+ # A transfer spec built by a command holds credentials for its `remote_host` (e.g. a token of the application, or the password of a preset):
210
+ # at levels `medium` and `high`, option `ts` cannot send them to another host, unless that host is in the configuration (see #add_configured).
211
+ # Credentials given in option `ts` with the host are the ones of the client.
212
+ # @param transfer_spec [Hash] Final transfer spec, option `ts` merged
213
+ # @param user_transfer_spec [Hash] Option `ts`
214
+ # @param application_host [String, nil] `remote_host` of the transfer spec of the command, before option `ts`
215
+ # @raise [BadArgument] If, at level `medium` or `high`, option `ts` changes the host of credentials that it does not give
216
+ def check_transfer_host(transfer_spec, user_transfer_spec, application_host:)
217
+ host = user_transfer_spec['remote_host']
218
+ return if level.eql?(:low) || host.nil? || host.eql?(application_host) || configured?(host)
219
+ credentials = TRANSFER_CREDENTIAL_KEYS.select do |key|
220
+ transfer_spec.key?(key) && (!user_transfer_spec.key?(key) || configured?(user_transfer_spec[key]))
221
+ end
222
+ return if credentials.empty?
223
+ raise BadArgument, "option ts: remote_host: the credentials of the transfer (#{credentials.join(', ')}) cannot be sent to a host that is not in the configuration " \
224
+ "at security level #{level}: use the host of the command, or give the credentials with the host (#{where_to_set})"
225
+ end
226
+
144
227
  # At level `medium`, the real path is checked, so that neither `..` nor a symbolic link leads outside of allowed paths.
145
228
  # A missing file gets the same error as a file outside allowed paths: no information on other files.
146
229
  # @param path [String] Path of a file to read
@@ -168,6 +251,17 @@ module Aspera
168
251
  # @raise [BadArgument] If reading the file is refused at current level
169
252
  def read_file(path, ...) = File.read(readable_path(path), ...)
170
253
 
254
+ # A file or folder found by a command, e.g. in a folder given by the client, can be outside of allowed paths (symbolic link):
255
+ # the command skips it, instead of failing. Same rules as `readable_path`.
256
+ # @param path [String] Path of a file or folder to read
257
+ # @return [Boolean] `true` if reading it is allowed at current level
258
+ def readable_path?(path)
259
+ readable_path(path)
260
+ true
261
+ rescue BadArgument
262
+ false
263
+ end
264
+
171
265
  # Writing a file where the client chooses (e.g. downloaded file) could replace a file that is used later (e.g. plugin, shell startup file).
172
266
  # At level `medium` and `high`, files can be written only in `writable_paths` (none by default): the real path of the folder is checked,
173
267
  # so that neither `..` nor a symbolic link leads outside of them.
@@ -178,13 +272,17 @@ module Aspera
178
272
  def writable_path(path)
179
273
  return path if level.eql?(:low)
180
274
  path = File.expand_path(path)
181
- # The first existing parent gives the real path, the missing part contains no link
275
+ # The first existing parent gives the real path, the missing part contains no link.
276
+ # A symbolic link exists even when its target does not (`File.exist?` follows it): writing creates its target, wherever it is
182
277
  existing = path
183
278
  missing = []
184
- until File.exist?(existing) || File.dirname(existing).eql?(existing)
279
+ until File.exist?(existing) || File.symlink?(existing) || File.dirname(existing).eql?(existing)
185
280
  missing.unshift(File.basename(existing))
186
281
  existing = File.dirname(existing)
187
282
  end
283
+ Aspera.assert(File.exist?(existing), type: BadArgument) do
284
+ "writing is refused by security level #{level}: symbolic link to a missing file: #{existing}"
285
+ end
188
286
  real_path = File.join(File.realpath(existing), *missing)
189
287
  Aspera.assert(@writable_paths.any? { |allowed| real_path.eql?(allowed) || real_path.start_with?(File.join(allowed, '')) }, type: BadArgument) do
190
288
  "writing is refused by security level #{level}: not in security.writable_paths: #{path} (#{where_to_set})"