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
@@ -16,6 +16,8 @@ module Aspera
16
16
  # Secrets are identified:
17
17
  # - by key name, in structures (`secret?`, `hidden_copy`) and in strings with known patterns (`hide_secrets_in_string`)
18
18
  # - by value, when declared where they come from (`add_secret_values`, `add_secrets_in`): option values, tokens, ...
19
+ # - in a structure encoded in base64 JSON in a URL query (e.g. context of a Faspex 5 public link): declared by `add_secrets_in`,
20
+ # else hidden in logs only (`hide_serialized_secrets`, e.g. in an API response)
19
21
  #
20
22
  # @!method self.instance
21
23
  # Returns the singleton instance of Secret::Hider
@@ -27,13 +29,16 @@ module Aspera
27
29
  HIDDEN_PASSWORD = '🔑'
28
30
  # env vars for ascp with secrets
29
31
  ASCP_ENV_SECRETS = %w[ASPERA_SCP_PASS ASPERA_SCP_KEY ASPERA_SCP_FILEPASS ASPERA_PROXY_PASS ASPERA_SCP_TOKEN].freeze
30
- # keys in hash that contain secrets
31
- KEY_SECRETS = %w[password secret passphrase _key apikey crn token].freeze
32
+ # keys in hash that contain secrets (`passcode`: e.g. of a Faspex 5 public link, in its context, or in header `Passcode`)
33
+ KEY_SECRETS = %w[password secret passphrase passcode _key apikey crn token].freeze
32
34
  HTTP_SECRETS = %w[Authorization].freeze
33
35
  ALL_SECRETS = (ASCP_ENV_SECRETS + KEY_SECRETS + HTTP_SECRETS).freeze
34
36
  NON_ENV_SECRETS = (KEY_SECRETS + HTTP_SECRETS).freeze
35
37
  # not secrets: `*_file` is the path of a file (e.g. `ca_file`)
36
38
  KEY_FALSE_POSITIVES = [/^access_key$/, /^fallback_private_key$/, /public_key$/, /^token_type$/, /_file$/].freeze
39
+ # Keys of a form or URL query whose values are credentials, though their names do not denote a secret (not in JSON, e.g. `code` of an error):
40
+ # OAuth authorization code (RFC 6749 §4.1.2), e.g. in the URL of a redirect, PKCE code verifier (RFC 7636 §4.5), JWT assertion (RFC 7523 §2.1)
41
+ FORM_CREDENTIALS = %w[code code_verifier assertion].freeze
37
42
  # boolean values as text: not secrets, even for a name that denotes a secret (e.g. `show_secrets`)
38
43
  BOOLEAN_TEXTS = %w[yes no true false].freeze
39
44
  # min length of hidden secrets, use `+` or `{n,}`
@@ -41,28 +46,47 @@ module Aspera
41
46
  # min length of a declared secret value to be hidden inside a string: shorter ones are hidden only when they are the whole string,
42
47
  # else common words would be hidden (e.g. password `aspera` in URLs), or optionally as a word (`short_in_text`)
43
48
  VALUE_IN_STRING_LENGTH = 8
44
- # regex that define named captures :begin and :end
49
+ # regex that define named captures :begin and :end, and :key for the name of a key (not hidden if it is not a secret, see `KEY_FALSE_POSITIVES`)
45
50
  REGEX_LOG_REPLACES = [
46
- # private key values (place first)
47
- /(?<begin>--+BEGIN [^-]+ KEY--+)[[:ascii:]]+?(?<end>--+?END [^-]+ KEY--+)\n*/,
51
+ # private key values, e.g. `PRIVATE`, `RSA PRIVATE`, `ENCRYPTED PRIVATE`, `OPENSSH PRIVATE` (place first); a public key is not a secret
52
+ /(?<begin>--+BEGIN (?:[A-Z0-9]+ )*PRIVATE KEY--+)[[:ascii:]]+?(?<end>--+?END (?:[A-Z0-9]+ )*PRIVATE KEY--+)\n*/,
48
53
  # CLI manager get/set options
49
54
  /(?<begin>[sg]et (?:#{KEY_SECRETS.join('|')})=).*(?<end>)/i,
50
55
  # env var ascp exec
51
56
  /(?<begin> (?:#{ASCP_ENV_SECRETS.join('|')})=)[^ \n]+(?<end> |$)/,
52
57
  # rendered JSON or Ruby (quoted key or legacy symbol key: `"k":"v"`, `"k" => "v"`, `:k=>"v"`)
53
- /(?<begin>(?:(?<quote>["'])|:)[^"':=]*(?:#{ALL_SECRETS.join('|')})[^"':=]*\k<quote>? *(?:=>|:) *")(?:[^"\\]|\\.)+(?<end>")/i,
58
+ /(?<begin>(?:(?<quote>["'])|:)(?<key>[^"':=]*(?:#{ALL_SECRETS.join('|')})[^"':=]*)\k<quote>? *(?:=>|:) *")(?:[^"\\]|\\.)+(?<end>")/i,
54
59
  # rendered Ruby 3.4+ symbol key: `k: "v"`
55
- /(?<begin>\b\w*(?:#{ALL_SECRETS.join('|')})\w*: *")(?:[^"\\]|\\.)+(?<end>")/i,
60
+ /(?<begin>\b(?<key>\w*(?:#{ALL_SECRETS.join('|')})\w*): *")(?:[^"\\]|\\.)+(?<end>")/i,
56
61
  # rendered Ruby Struct or object: `k="v"`, `@k="v"`
57
- /(?<begin>@?\b\w*(?:#{ALL_SECRETS.join('|')})\w*=")(?:[^"\\]|\\.)+(?<end>")/i,
62
+ /(?<begin>@?\b(?<key>\w*(?:#{ALL_SECRETS.join('|')})\w*)=")(?:[^"\\]|\\.)+(?<end>")/i,
58
63
  # query parameter of URL: `?token=v` or `&client_secret=v`
59
- /(?<begin>[?&][^=&\s"]*(?:#{KEY_SECRETS.join('|')})[^=&\s"]*=)[^&\s"#]+(?<end>)/i,
60
- # logged data
61
- /(?<begin>(?:#{NON_ENV_SECRETS.join('|')})[ =:]+)[^ "\n]#{SECRET_LENGTH}(?<end>$)/i,
64
+ /(?<begin>[?&](?<key>[^=&\s"]*(?:#{KEY_SECRETS.join('|')})[^=&\s"]*)=)[^&\s"#]+(?<end>)/i,
65
+ # query parameter of URL, OAuth credential: `?code=v`
66
+ /(?<begin>[?&](?:#{FORM_CREDENTIALS.join('|')})=)[^&\s"#]+(?<end>)/,
67
+ # logged data (:key is the end of the name, e.g. `_key` of `access_key: v`)
68
+ /(?<begin>(?<key>#{NON_ENV_SECRETS.join('|')})[ =:]+)[^ "\n]#{SECRET_LENGTH}(?<end>$)/i,
62
69
  # cred in http dump
63
70
  /(?<begin>(?:#{HTTP_SECRETS.join('|')}): )[^\\]+(?<end>\\)/i
64
71
  ].freeze
65
- private_constant :HIDDEN_PASSWORD, :ASCP_ENV_SECRETS, :KEY_SECRETS, :HTTP_SECRETS, :ALL_SECRETS, :NON_ENV_SECRETS, :KEY_FALSE_POSITIVES, :BOOLEAN_TEXTS, :SECRET_LENGTH, :VALUE_IN_STRING_LENGTH, :REGEX_LOG_REPLACES
72
+ # Key/value pairs of a structure serialized in a string (e.g. raw HTTP body), with named captures :begin, :key, :value and :end
73
+ REGEX_SERIALIZED_PAIRS = {
74
+ # JSON, string value: `"key":"value"`
75
+ json: /(?<begin>"(?<key>(?:[^"\\]|\\.)*)"\s*:\s*")(?<value>(?:[^"\\]|\\.)*)(?<end>")/,
76
+ # form or URL query: `key=value&key=value`, `?key=value`
77
+ form: /(?<begin>(?:\A|[?&])(?<key>[^=&\s"#?]+)=)(?<value>[^&\s"#]*)(?<end>)/
78
+ }.freeze
79
+ # HTTP header with cookies, e.g. in the debug output of `Net::HTTP`: `Cookie: name=value; name=value`, `Set-Cookie: name=value; Path=/`
80
+ REGEX_COOKIE_HEADER = /^(?<begin>(?<name>(?:Set-)?Cookie):[ \t]*)(?<value>[^\r\n]*)/i
81
+ # `name=value` of a cookie in the header, with named capture :begin. `Set-Cookie`: the first one only, others are attributes (e.g. `Max-Age=60`)
82
+ REGEX_COOKIE_PAIR = /(?<begin>(?:\A|;)[ \t]*[^=;\s]+=)[^;\r\n]+/
83
+ # Value of a query parameter of a URL, long enough to be a structure encoded in base64 (see `encoded_secret?`), with named captures :begin and :value.
84
+ # No `?` in the name: else polynomial on many `?`. No `\` in the value: end of a URL in JSON with `&` escaped (e.g. `\u0026` by Go)
85
+ REGEX_QUERY_ENCODED = /(?<begin>[?&][^=&\s"#?]+=)(?<value>[^&\s"#\\]{16,})/
86
+ # A character that is not in the name of a key in a log line (see `false_positive_key?`)
87
+ REGEX_NOT_IN_KEY = /[\s,;{}\[\]()<>@#|"':=&?]/
88
+ private_constant :HIDDEN_PASSWORD, :ASCP_ENV_SECRETS, :KEY_SECRETS, :HTTP_SECRETS, :ALL_SECRETS, :NON_ENV_SECRETS, :KEY_FALSE_POSITIVES, :BOOLEAN_TEXTS, :SECRET_LENGTH, :VALUE_IN_STRING_LENGTH, :REGEX_LOG_REPLACES, :REGEX_SERIALIZED_PAIRS,
89
+ :FORM_CREDENTIALS, :REGEX_COOKIE_HEADER, :REGEX_COOKIE_PAIR, :REGEX_QUERY_ENCODED, :REGEX_NOT_IN_KEY
66
90
  attr_accessor :log_secrets
67
91
 
68
92
  # @return [Proc] new log formatter that hides secrets
@@ -85,7 +109,7 @@ module Aspera
85
109
  marker = hidden_marker
86
110
  value = hide_values_in_string(value, marker)
87
111
  (all ? REGEX_LOG_REPLACES : REGEX_LOG_REPLACES.first(1)).each do |reg_ex|
88
- value = value.gsub(reg_ex) { "#{Regexp.last_match(:begin)}#{marker}#{Regexp.last_match(:end)}" }
112
+ value = hide_pattern(value, reg_ex, marker)
89
113
  end
90
114
  return value
91
115
  end
@@ -100,7 +124,7 @@ module Aspera
100
124
  return true if @additional_keys.include?(keyword)
101
125
  keyword = keyword.downcase
102
126
  # those are not secrets
103
- return false if KEY_FALSE_POSITIVES.any? { |f| f.match?(keyword) }
127
+ return false if false_positive?(keyword)
104
128
  # check if keyword (name) contains an element that designate it as a secret
105
129
  ALL_SECRETS.any? { |kw| keyword.include?(kw.downcase) }
106
130
  end
@@ -110,20 +134,67 @@ module Aspera
110
134
  # @param obj [Object] Hash, Array, Struct, String, or other value (returned as is)
111
135
  # @param short_in_text [Boolean] `true`: a declared value shorter than 8 characters is also hidden inside a text, as a word
112
136
  # (not next to a letter or digit), e.g. a short password in an error message of `mcp server`
137
+ # @param serialized [Boolean] `true`: also hide secrets by key name in structures serialized in strings (see `hide_serialized_secrets`)
113
138
  # @return [Object] Copy of obj with secrets replaced by placeholder
114
- def hidden_copy(obj, short_in_text: false)
139
+ def hidden_copy(obj, short_in_text: false, serialized: false)
140
+ copy = ->(item) { hidden_copy(item, short_in_text: short_in_text, serialized: serialized) }
115
141
  case obj
116
- when Hash
117
- obj.to_h do |k, v|
118
- [k.is_a?(String) ? hidden_copy(k, short_in_text: short_in_text) : k, secret?(k, v) ? hidden_marker : hidden_copy(v, short_in_text: short_in_text)]
119
- end
120
- when Array then obj.map { |i| hidden_copy(i, short_in_text: short_in_text) }
121
- when Struct then obj.dup.tap { |s| s.each_pair { |k, v| s[k] = secret?(k, v) ? hidden_marker : hidden_copy(v, short_in_text: short_in_text) } }
122
- when String then hide_values_in_string(obj, hidden_marker, short_in_text: short_in_text)
142
+ when Hash then obj.to_h { |k, v| [k.is_a?(String) ? copy.call(k) : k, secret?(k, v) ? hidden_marker : copy.call(v)] }
143
+ when Array then obj.map(&copy)
144
+ when Struct then obj.dup.tap { |s| s.each_pair { |k, v| s[k] = secret?(k, v) ? hidden_marker : copy.call(v) } }
145
+ when String
146
+ text = hide_values_in_string(obj, hidden_marker, short_in_text: short_in_text)
147
+ serialized ? hide_serialized_secrets(text) : text
123
148
  else obj
124
149
  end
125
150
  end
126
151
 
152
+ # Hides secrets by key name (see `secret?`) in a structure serialized in a string, e.g. the raw body of an HTTP request or response:
153
+ # JSON (`"key":"value"`), form or URL query (`key=value&...`, also OAuth credentials such as `code`, see `FORM_CREDENTIALS`),
154
+ # the values of cookies in HTTP headers (`Cookie`, `Set-Cookie`), and the value of a URL query that is a structure encoded in base64 JSON
155
+ # with a secret (e.g. the context of a Faspex 5 public link in an API response, not declared: shown in results, see `add_encoded_secrets`).
156
+ # Keys, names of cookies and other values are kept.
157
+ # Needed before such a string is dumped in logs: the dump escapes its quotes, so the patterns of the log formatter would not match.
158
+ # @param value [String] Text
159
+ # @return [String] Text with the values of secret keys replaced by placeholder, or `value` itself if it has none
160
+ def hide_serialized_secrets(value)
161
+ marker = hidden_marker
162
+ text = value
163
+ unless text.valid_encoding? && Encoding.compatible?(text, marker)
164
+ # e.g. binary data with non-ASCII bytes (raw HTTP body): UTF-8 text if valid, else bytes
165
+ text = value.dup.force_encoding(Encoding::UTF_8)
166
+ text, marker = value.b, marker.b unless text.valid_encoding?
167
+ end
168
+ hidden = false
169
+ REGEX_SERIALIZED_PAIRS.each do |format, reg_ex|
170
+ text = text.gsub(reg_ex) do
171
+ match = Regexp.last_match
172
+ key = match[:key]
173
+ key = (URI.decode_www_form_component(key).scrub rescue key) if format.eql?(:form)
174
+ next match[0] unless secret?(key, match[:value]) || (format.eql?(:form) && FORM_CREDENTIALS.include?(key))
175
+ hidden = true
176
+ "#{match[:begin]}#{marker}#{match[:end]}"
177
+ end
178
+ end
179
+ text = text.gsub(REGEX_COOKIE_HEADER) do
180
+ match = Regexp.last_match
181
+ pairs = match[:value].public_send(match[:name].casecmp?('Cookie') ? :gsub : :sub, REGEX_COOKIE_PAIR) do
182
+ hidden = true
183
+ "#{Regexp.last_match(:begin)}#{marker}"
184
+ end
185
+ "#{match[:begin]}#{pairs}"
186
+ end
187
+ if text.include?('=')
188
+ text = text.gsub(REGEX_QUERY_ENCODED) do
189
+ match = Regexp.last_match
190
+ next match[0] unless encoded_secret?(match[:value])
191
+ hidden = true
192
+ "#{match[:begin]}#{marker}"
193
+ end
194
+ end
195
+ hidden ? text : value
196
+ end
197
+
127
198
  # @param obj [Object] Hash, Array, Struct, String, or other value
128
199
  # @return [Boolean] `true` if obj contains a secret: by key name, or declared value
129
200
  def contains_secret?(obj)
@@ -149,12 +220,13 @@ module Aspera
149
220
  end
150
221
  end
151
222
 
152
- # Declare the secret values found by key name in a structure
153
- # @param obj [Object] Hash or Array, e.g. a transfer spec
223
+ # Declare the secret values found by key name in a structure, also in a structure encoded in the query of a URL (see `add_encoded_secrets`)
224
+ # @param obj [Object] Hash, Array or String, e.g. a transfer spec, the value of an option
154
225
  def add_secrets_in(obj)
155
226
  case obj
156
227
  when Array then obj.each { |i| add_secrets_in(i) }
157
228
  when Hash then obj.each { |k, v| secret?(k, v) ? add_secret_values(v) : add_secrets_in(v) }
229
+ when String then add_encoded_secrets(obj)
158
230
  end
159
231
  end
160
232
 
@@ -188,6 +260,69 @@ module Aspera
188
260
  @mutex = Mutex.new
189
261
  end
190
262
 
263
+ # Declare the secrets of a structure encoded in base64 JSON in the query of a URL, e.g. the passcode in the `context` of a Faspex 5 public link:
264
+ # the secrets found by key name in the structure, and the encoded value (as in the URL, and URL-decoded), as it reveals them once decoded.
265
+ # The rest of the URL is not hidden.
266
+ # @param text [String] Text, e.g. URL
267
+ def add_encoded_secrets(text)
268
+ return unless text.include?('=') && text.valid_encoding?
269
+ text.scan(REGEX_QUERY_ENCODED) do
270
+ raw = Regexp.last_match(:value)
271
+ encoded, structure = decode_query_structure(raw)
272
+ next unless encoded_secret?(raw, structure)
273
+ add_secrets_in(structure)
274
+ add_secret_values(raw, encoded)
275
+ end
276
+ end
277
+
278
+ # @param raw [String] Value of a query parameter of a URL, as in the URL
279
+ # @return [Array(String, Object), nil] The value URL-decoded, and the structure it encodes in base64 JSON (standard or URL-safe), or `nil`
280
+ def decode_query_structure(raw)
281
+ # `+` kept: it is one of base64, also when not URL-encoded
282
+ encoded = URI.decode_uri_component(raw)
283
+ [encoded, JSON.parse(encoded.tr('-_', '+/').unpack1('m'))]
284
+ rescue StandardError
285
+ # Not a structure encoded in base64 JSON (e.g. `?context=packages`)
286
+ nil
287
+ end
288
+
289
+ # @param raw [String] Value of a query parameter of a URL, as in the URL
290
+ # @param structure [Object] Structure it encodes, if already decoded (see `decode_query_structure`)
291
+ # @return [Boolean] `true` if the value is a structure (Hash or Array) encoded in base64 JSON that contains a secret, by key name or declared value
292
+ def encoded_secret?(raw, structure = decode_query_structure(raw)&.last)
293
+ (structure.is_a?(Hash) || structure.is_a?(Array)) && contains_secret?(structure)
294
+ end
295
+
296
+ # @param keyword [String] Key name, in lower case
297
+ # @return [Boolean] `true` if the key is not a secret, though its name contains one that denotes a secret (e.g. `token_type`)
298
+ def false_positive?(keyword)
299
+ KEY_FALSE_POSITIVES.any? { |f| f.match?(keyword) }
300
+ end
301
+
302
+ # @param text [String] Text
303
+ # @param reg_ex [Regexp] One of `REGEX_LOG_REPLACES`
304
+ # @param marker [String] Placeholder
305
+ # @return [String] Text with the values matched by reg_ex replaced by placeholder, but the values of keys that are not secrets (as `secret?`)
306
+ def hide_pattern(text, reg_ex, marker)
307
+ text.gsub(reg_ex) do
308
+ match = Regexp.last_match
309
+ next "#{match[:begin]}#{marker}#{match[:end]}" unless match.names.include?('key') && false_positive_key?(match)
310
+ # Not a secret: its value may contain others, e.g. `&token=` after `access_key=` for "logged data"
311
+ value = match[0][match[:begin].length...(match[0].length - match[:end].length)]
312
+ "#{match[:begin]}#{hide_pattern(value, reg_ex, marker)}#{match[:end]}"
313
+ end
314
+ end
315
+
316
+ # @param match [MatchData] Match of a regex of `REGEX_LOG_REPLACES` with a named capture :key
317
+ # @return [Boolean] `true` if the key is not a secret (see `secret?`). Its name ends with the capture, and starts after the last character that
318
+ # cannot be in a name, e.g. `access_key` for `_key` in `data: access_key: v`, `token_type` for ` {token_type` in `text: {token_type: "v"}`
319
+ def false_positive_key?(match)
320
+ head = match.string[0, match.end(:key)]
321
+ start = head.rindex(REGEX_NOT_IN_KEY)
322
+ keyword = start.nil? ? head : head[(start + 1)..]
323
+ !@additional_keys.include?(keyword) && false_positive?(keyword.downcase)
324
+ end
325
+
191
326
  # @param value [String] Input string
192
327
  # @param marker [String] Placeholder
193
328
  # @param short_in_text [Boolean] `true`: also hide short values as a word
@@ -19,6 +19,14 @@ module Aspera
19
19
  class Error < Aspera::Error
20
20
  end
21
21
 
22
+ # No secret, or several secrets, for the label or identifier
23
+ class NotFound < Error
24
+ end
25
+
26
+ # Credentials of the store refused, e.g. password of the file
27
+ class AuthenticationError < Error
28
+ end
29
+
22
30
  # Parsed reference
23
31
  # @!attribute text [String] Reference as given
24
32
  # @!attribute store [String] Name of store
@@ -113,7 +113,7 @@ module Aspera
113
113
  found = store(name)
114
114
  found.list_folder(path, options)
115
115
  rescue StandardError => e
116
- raise Error, "secret store #{name}#{", folder #{path}" unless path.empty?}: #{reason(e, found)}"
116
+ raise error_class(e), "secret store #{name}#{", folder #{path}" unless path.empty?}: #{reason(e, found)}"
117
117
  end
118
118
 
119
119
  # Write a new secret to a writable store
@@ -155,7 +155,7 @@ module Aspera
155
155
  rescue Aspera::Secret::Error => e
156
156
  # Re-raise with store context if the message doesn't already include it
157
157
  raise e if e.message.start_with?('secret references configuration:')
158
- raise Error, "secret references configuration: store #{name}: #{e.message}"
158
+ raise e.class, "secret references configuration: store #{name}: #{e.message}"
159
159
  rescue StandardError => e
160
160
  raise Error, "secret references configuration: store #{name}: #{e.message}"
161
161
  end
@@ -164,7 +164,7 @@ module Aspera
164
164
  def create_store(name, params)
165
165
  Store::Factory.create(params)
166
166
  rescue StandardError => e
167
- raise Error, "store #{name}: #{e.message}"
167
+ raise error_class(e), "store #{name}: #{e.message}"
168
168
  end
169
169
 
170
170
  # Checks reference and calls block with its store
@@ -175,9 +175,13 @@ module Aspera
175
175
  found.check_reference(ref)
176
176
  yield(found)
177
177
  rescue StandardError => e
178
- raise Error, "secret reference #{ref.text}: #{reason(e, found)}"
178
+ raise error_class(e), "secret reference #{ref.text}: #{reason(e, found)}"
179
179
  end
180
180
 
181
+ # @param error [StandardError] Error
182
+ # @return [Class] Class of the error raised with the reference or store: the one of a `Secret::Error` (e.g. `NotFound`), else `Error`
183
+ def error_class(error) = error.is_a?(Error) ? error.class : Error
184
+
181
185
  # @param error [StandardError] Error
182
186
  # @param store [Store::Base, nil] Store that raised the error
183
187
  # @return [String] Reason of failure, without secret
@@ -52,18 +52,18 @@ module Aspera
52
52
  end
53
53
 
54
54
  def validate_set(options)
55
- Aspera.assert_type(options, Hash) { 'options' }
55
+ Aspera.assert_type(options, Hash, type: Error) { 'options' }
56
56
  unsupported = options.keys - CONTENT_KEYS
57
- Aspera.assert(unsupported.empty?) { "unsupported options: #{unsupported}, use #{CONTENT_KEYS.join(', ')}" }
57
+ Aspera.assert(unsupported.empty?, type: Error) { "unsupported options: #{unsupported}, use #{CONTENT_KEYS.join(', ')}" }
58
58
  options.each_pair do |k, v|
59
- Aspera.assert_type(v, String) { k.to_s }
59
+ Aspera.assert_type(v, String, type: Error) { k.to_s }
60
60
  end
61
- Aspera.assert(options.key?(:label), 'label is required')
61
+ Aspera.assert(options.key?(:label), 'label is required', type: Error)
62
62
  end
63
63
 
64
64
  # Labels are unique: `set` does not overwrite an existing secret
65
65
  def assert_new_label(label)
66
- Aspera.assert(get(label: label, exception: false).nil?) { "secret #{label} already exist, delete first" }
66
+ Aspera.assert(get(label: label, exception: false).nil?, type: Error) { "secret #{label} already exist, delete first" }
67
67
  end
68
68
 
69
69
  # Checks before access: options of reference, paths allowed by configuration
@@ -77,9 +77,10 @@ module Aspera
77
77
  # Read a secret by reference: the path is the label, the field is `password` by default
78
78
  # @param ref [Reference]
79
79
  # @return [Hash] `value` (`String`), `version` (`nil` if store has no version), `updated_time` (`Time` or `nil`)
80
+ # @raise [NotFound] No secret for the label, or no such field
80
81
  def read_reference(ref)
81
82
  entry = get(label: ref.path, exception: false)
82
- raise Error, "no label: #{ref.path}" if entry.nil?
83
+ raise NotFound, "no label: #{ref.path}" if entry.nil?
83
84
  {value: field_value(entry, ref, default: DEFAULT_FIELD), version: nil, updated_time: nil}
84
85
  end
85
86
 
@@ -97,10 +98,10 @@ module Aspera
97
98
  # Field names of a secret by reference, without values: the path is the label
98
99
  # @param ref [Reference] Reference, its field is not used
99
100
  # @return [Hash] `fields` (`Array<String>`, fields that a reference can read), `version` (`nil`), `updated_time` (`nil`)
100
- # @raise [Error] No secret
101
+ # @raise [NotFound] No secret
101
102
  def read_fields(ref)
102
103
  entry = get(label: ref.path, exception: false)
103
- raise Error, "no label: #{ref.path}" if entry.nil?
104
+ raise NotFound, "no label: #{ref.path}" if entry.nil?
104
105
  {fields: field_names(entry.reject { |name, _| name.to_s.eql?('label') }), version: nil, updated_time: nil}
105
106
  end
106
107
 
@@ -141,7 +142,7 @@ module Aspera
141
142
  raise Error, 'field required: #<field>' if field.nil?
142
143
  value = data.key?(field.to_sym) ? data[field.to_sym] : data[field]
143
144
  case value
144
- when nil then raise Error, "no field: #{field}"
145
+ when nil then raise NotFound, "no field: #{field}"
145
146
  when String then value
146
147
  when Numeric, true, false then value.to_s
147
148
  else raise Error, "field #{field} is not a string"
@@ -82,7 +82,7 @@ module Aspera
82
82
  end
83
83
 
84
84
  def get(label:, exception: true)
85
- Aspera.assert(secrets.key?(label)) { "Label not found: #{label}" } if exception
85
+ Aspera.assert(secrets.key?(label), type: NotFound) { "Label not found: #{label}" } if exception
86
86
  result = secrets[label].clone
87
87
  result[:label] = label if result.is_a?(Hash)
88
88
  return result
@@ -90,7 +90,7 @@ module Aspera
90
90
 
91
91
  def delete(label:)
92
92
  with_lock do
93
- Aspera.assert(secrets_unlocked.key?(label)) { "Label not found: #{label}" }
93
+ Aspera.assert(secrets_unlocked.key?(label), type: NotFound) { "Label not found: #{label}" }
94
94
  secrets_unlocked.delete(label)
95
95
  save
96
96
  end
@@ -191,9 +191,9 @@ module Aspera
191
191
  vault_file = File.read(@path)
192
192
  if vault_file.start_with?('---')
193
193
  vault_info = Yaml.safe_load(vault_file)
194
- Aspera.assert_type(vault_info, Hash) { "Invalid vault file: #{@path}" }
194
+ Aspera.assert_type(vault_info, Hash, type: Error) { "Invalid vault file: #{@path}" }
195
195
  sorted_keys = vault_info.keys.sort
196
- Aspera.assert(sorted_keys == FILE_KEYS || sorted_keys == FILE_KEYS_KDF) { "Invalid vault file: #{@path}: #{sorted_keys}" }
196
+ Aspera.assert(sorted_keys == FILE_KEYS || sorted_keys == FILE_KEYS_KDF, type: Error) { "Invalid vault file: #{@path}: #{sorted_keys}" }
197
197
  @cipher_name = vault_info['cipher']
198
198
  @kdf_params = vault_info['kdf']
199
199
  # vault created before PBKDF2 was introduced (v4.26.0 format: no kdf field)
@@ -213,12 +213,12 @@ module Aspera
213
213
 
214
214
  # @param data [String] Encrypted secrets
215
215
  # @return [Hash] Secrets
216
- # @raise [Error] Wrong password
216
+ # @raise [AuthenticationError] Wrong password
217
217
  def decrypt(data)
218
218
  Yaml.safe_load(@cipher.decrypt(data)) || {}
219
219
  rescue OpenSSL::Cipher::CipherError, Psych::Exception
220
220
  # Psych: rarely, a wrong key gives valid padding, then garbage
221
- raise Error, "cannot decrypt #{@path} (password?)"
221
+ raise AuthenticationError, "cannot decrypt #{@path} (password?)"
222
222
  end
223
223
 
224
224
  # Minimal cipher wrapper using OpenSSL, compatible with the SymmetricEncryption
@@ -144,7 +144,7 @@ module Aspera
144
144
  # KV v2: a deleted secret may have metadata but no data
145
145
  data = call { |client| client.logical.read(path(label)) }&.data&.[](:data)
146
146
  if data.nil?
147
- raise "Secret '#{label}' not found" if exception
147
+ raise NotFound, "Secret '#{label}' not found" if exception
148
148
  return
149
149
  end
150
150
  return data
@@ -185,7 +185,7 @@ module Aspera
185
185
  def read_reference(ref)
186
186
  mount, path = mount_and_path(ref)
187
187
  secret = read(path: path, mount: mount, version: reference_version(ref))
188
- raise Error, 'not found' if secret.nil?
188
+ raise NotFound, 'not found' if secret.nil?
189
189
  {value: field_value(secret[:data], ref), version: secret[:version], updated_time: secret[:created_time]}
190
190
  end
191
191
 
@@ -218,7 +218,7 @@ module Aspera
218
218
  def read_fields(ref)
219
219
  mount, path = mount_and_path(ref)
220
220
  secret = read(path: path, mount: mount, version: reference_version(ref))
221
- raise Error, 'not found' if secret.nil?
221
+ raise NotFound, 'not found' if secret.nil?
222
222
  {fields: field_names(secret[:data]), version: secret[:version], updated_time: secret[:created_time]}
223
223
  end
224
224
 
@@ -105,8 +105,8 @@ module Aspera
105
105
  records = @client.get_secrets
106
106
  matches = records.select { |record| record.uid.eql?(identifier) }
107
107
  matches = records.select { |record| record.title.eql?(identifier) } if matches.empty?
108
- raise "Multiple secrets found with label '#{identifier}': use id to disambiguate" if matches.size > 1
109
- raise "Secret '#{identifier}' not found" if matches.empty? && exception
108
+ raise NotFound, "Multiple secrets found with label '#{identifier}': use id to disambiguate" if matches.size > 1
109
+ raise NotFound, "Secret '#{identifier}' not found" if matches.empty? && exception
110
110
  matches.first
111
111
  end
112
112
 
@@ -4,7 +4,6 @@
4
4
  # SPDX-License-Identifier: Apache-2.0
5
5
 
6
6
  # https://github.com/fastlane-community/security
7
- require_relative '../../cli/info'
8
7
  require_relative '../../log'
9
8
  require_relative '../../assert'
10
9
  require_relative '../../environment'
@@ -226,7 +225,7 @@ module Aspera
226
225
  raise Error, 'type system: only on macOS' unless Environment.instance.os.eql?(Environment::OS_MACOS)
227
226
  @keychain_name = name.nil? ? 'default keychain' : name
228
227
  @keychain = name.nil? ? MacosSecurity::Keychain.default : MacosSecurity::Keychain.by_name(name)
229
- Aspera.assert(!@keychain.nil?) { "no such keychain #{name}" }
228
+ Aspera.assert(!@keychain.nil?, type: Error) { "no such keychain #{name}" }
230
229
  end
231
230
 
232
231
  # @return [Hash] Vault information metadata
@@ -261,7 +260,7 @@ module Aspera
261
260
  def get(label:, exception: true)
262
261
  info = @keychain.password(:find, :generic, label: label)
263
262
  if info.nil?
264
- raise "Secret '#{label}' not found" if exception
263
+ raise NotFound, "Secret '#{label}' not found" if exception
265
264
  return
266
265
  end
267
266
  return {
@@ -71,10 +71,10 @@ module Aspera
71
71
  # Resolve an item id from its title, or else from its id; raises if ambiguous or missing
72
72
  def resolve_id(label, exception: true)
73
73
  items = @api.read("v1/vaults/#{@vault_id}/items", {filter: "title eq \"#{label}\""})
74
- raise "Multiple secrets found with label '#{label}': use id to disambiguate" if items.length > 1
74
+ raise NotFound, "Multiple secrets found with label '#{label}': use id to disambiguate" if items.length > 1
75
75
  # No title matches: the identifier may be an item id
76
76
  items = @api.read("v1/vaults/#{@vault_id}/items").select { |item| item['id'] == label } if items.empty?
77
- raise "Secret '#{label}' not found" if items.empty? && exception
77
+ raise NotFound, "Secret '#{label}' not found" if items.empty? && exception
78
78
  items.first&.fetch('id')
79
79
  end
80
80
 
@@ -65,11 +65,11 @@ module Aspera
65
65
  identifier = id || label
66
66
  stdout, stderr, status = op_capture('item', 'get', identifier, '--format=json', exception: false)
67
67
  if !status.success?
68
- raise "Multiple secrets found with label '#{label}': use id to disambiguate, or option vault to restrict" if stderr.to_s.match?(/more than one item/i)
68
+ raise NotFound, "Multiple secrets found with label '#{label}': use id to disambiguate, or option vault to restrict" if stderr.to_s.match?(/more than one item/i)
69
69
  # Other failures (e.g. not signed in) must not be reported as a missing secret
70
70
  # `op` prefixes its messages with `[ERROR] <date> <time>`
71
- raise "1Password CLI failed for '#{label}': #{stderr.to_s.sub(%r{\A\[ERROR\]\s+\d{4}/\d\d/\d\d\s+[\d:]+\s+}, '').strip}" unless stderr.to_s.match?(/isn't an item|not found/i)
72
- raise "Secret '#{label}' not found" if exception
71
+ raise Error, "1Password CLI failed for '#{label}': #{stderr.to_s.sub(%r{\A\[ERROR\]\s+\d{4}/\d\d/\d\d\s+[\d:]+\s+}, '').strip}" unless stderr.to_s.match?(/isn't an item|not found/i)
72
+ raise NotFound, "Secret '#{label}' not found" if exception
73
73
  return
74
74
  end
75
75
  full = JSON.parse(stdout)
@@ -67,7 +67,7 @@ module Aspera
67
67
  if !status.success?
68
68
  # no error message when the item does not exist
69
69
  raise Error, stderr.strip unless stderr.strip.empty?
70
- raise "Secret '#{label}' not found" if exception
70
+ raise NotFound, "Secret '#{label}' not found" if exception
71
71
  return
72
72
  end
73
73
  attributes = search(label).last
@@ -82,7 +82,7 @@ module Aspera
82
82
 
83
83
  # @param label [String] Label of the secret
84
84
  def delete(label:)
85
- raise "Secret '#{label}' not found" if get(label: label, exception: false).nil?
85
+ raise NotFound, "Secret '#{label}' not found" if get(label: label, exception: false).nil?
86
86
  _, stderr, status = secret_tool('clear', ATTR_SERVICE, @name, 'label', label)
87
87
  raise Error, stderr.strip unless status.success?
88
88
  nil
data/lib/aspera/ssl.rb CHANGED
@@ -16,15 +16,15 @@ module Aspera
16
16
  attr_reader :extra_options
17
17
 
18
18
  def option_list=(options)
19
- Aspera.assert_type(options, Array) { 'ssl_options' }
19
+ Aspera.assert_type(options, Array, type: ParameterError) { 'ssl_options' }
20
20
  options.each do |opt|
21
- Aspera.assert_type(opt, String, Integer) { 'Expected String or Integer in ssl_options' }
21
+ Aspera.assert_type(opt, String, Integer, type: ParameterError) { 'Expected String or Integer in ssl_options' }
22
22
  case opt
23
23
  when Integer
24
24
  @extra_options = opt
25
25
  when String
26
26
  name = "OP_#{opt.start_with?('-') ? opt[1..] : opt}".upcase
27
- Aspera.assert(OpenSSL::SSL.const_defined?(name), type: Cli::BadArgument) { "Unknown ssl_option: #{name}, use one of: #{OpenSSL::SSL.constants.grep(/^OP_/).map { |c| c.to_s.sub(/^OP_/, '') }.join(', ')}" }
27
+ Aspera.assert(OpenSSL::SSL.const_defined?(name), type: ParameterError) { "Unknown ssl_option: #{name}, use one of: #{OpenSSL::SSL.constants.grep(/^OP_/).map { |c| c.to_s.sub(/^OP_/, '') }.join(', ')}" }
28
28
  if opt.start_with?('-')
29
29
  @extra_options &= ~OpenSSL::SSL.const_get(name)
30
30
  else
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ # SPDX-FileCopyrightText: IBM Corp.
4
+ # SPDX-License-Identifier: Apache-2.0
5
+
6
+ module Aspera
7
+ # The standard input given where a command reads a file (e.g. a log file to analyze): the command reads it, as it would read the file.
8
+ # On command line: the argument `-`, or the extended value `@stdin:stream`.
9
+ # Unlike `@stdin:`, which reads the standard input when the value is evaluated, it is not read until the command reads it.
10
+ class StandardInput
11
+ # Name of the standard input where a file name is expected, also in results and messages
12
+ NAME = '-'
13
+
14
+ # @return [String] `-`, e.g. as the file name in results
15
+ def to_s = NAME
16
+
17
+ # @return [String] the content of the standard input, in binary mode
18
+ def read = $stdin.binmode.read
19
+ end
20
+ end
@@ -5,7 +5,7 @@
5
5
  # include/async/common/Sync_types.h (SSSTATE values)
6
6
  # Verified against a live snap.db using: ascli config sync admin overview <path>
7
7
  $schema: https://json-schema.org/draft/2020-12/schema
8
- $id: https://github.com/IBM/aspera-cli/tree/main/lib/aspera/schema/async_tables.yaml
8
+ $id: https://github.com/IBM/aspera-cli/tree/main/lib/aspera/sync/async_tables.yaml
9
9
  title: Async snap database tables
10
10
  description: Schemas for the SQLite tables stored inside the Async snap database (`snap.db`).
11
11
  type: object
@@ -17,10 +17,16 @@ class SqLite3Wrapper
17
17
  @db_path = db_path
18
18
  end
19
19
 
20
+ # `async` may have the database open while it synchronizes: the connection is set read-only (`PRAGMA query_only`,
21
+ # a core SQLite feature, so it works the same with the JDBC driver), so that a query (for example option `sql`)
22
+ # cannot modify the live database (it blocks `UPDATE`, `DELETE`, `INSERT` and `VACUUM INTO`). The database is still
23
+ # opened read-write so that a database in WAL mode can be read (a read-only open needs to write the `-shm` index).
24
+ READ_ONLY = 'PRAGMA query_only = ON'
20
25
  if defined?(JRUBY_VERSION)
21
26
  def execute(sql)
22
27
  db = Sequel.connect("jdbc:sqlite:#{@db_path}")
23
28
  begin
29
+ db.run(READ_ONLY)
24
30
  normalize_rows(db.fetch(sql).all)
25
31
  ensure
26
32
  db.disconnect
@@ -30,6 +36,7 @@ class SqLite3Wrapper
30
36
  def execute(sql)
31
37
  db = SQLite3::Database.new(@db_path).tap { |d| d.results_as_hash = true }
32
38
  begin
39
+ db.execute(READ_ONLY)
33
40
  normalize_rows(db.execute(sql))
34
41
  ensure
35
42
  db.close