aspera-cli 4.28.0 → 4.29.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 (237) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/BUGS.md +5 -10
  4. data/CHANGELOG.md +143 -2
  5. data/CODE_OF_CONDUCT.md +18 -0
  6. data/CONTRIBUTING.md +135 -56
  7. data/GOVERNANCE.md +30 -0
  8. data/LICENSE +201 -0
  9. data/README.md +103 -26
  10. data/SECURITY.md +70 -0
  11. data/bin/ascli +8 -5
  12. data/bin/asession +9 -6
  13. data/docs/README.md +1633 -609
  14. data/docs/test-mcp-with-ai.md +8 -8
  15. data/lib/aspera/agent/base.rb +23 -2
  16. data/lib/aspera/agent/connect.rb +16 -12
  17. data/lib/aspera/agent/desktop.rb +10 -7
  18. data/lib/aspera/agent/direct.rb +20 -19
  19. data/lib/aspera/agent/factory.rb +6 -3
  20. data/lib/aspera/agent/httpgw.rb +7 -5
  21. data/lib/aspera/agent/node.rb +15 -7
  22. data/lib/aspera/agent/transferd.rb +167 -43
  23. data/lib/aspera/api/alee.rb +4 -1
  24. data/lib/aspera/api/aoc.rb +14 -11
  25. data/lib/aspera/api/ats.rb +4 -2
  26. data/lib/aspera/api/cos_node.rb +7 -4
  27. data/lib/aspera/api/faspex.rb +16 -9
  28. data/lib/aspera/api/httpgw.rb +9 -6
  29. data/lib/aspera/api/node.rb +20 -20
  30. data/lib/aspera/ascmd.rb +8 -5
  31. data/lib/aspera/ascp/installation.rb +71 -65
  32. data/lib/aspera/ascp/log.schema.yaml +1084 -0
  33. data/lib/aspera/ascp/log_analyzer/advice.rb +97 -0
  34. data/lib/aspera/ascp/log_analyzer/diagnostics.rb +344 -0
  35. data/lib/aspera/ascp/log_analyzer/lines.rb +75 -0
  36. data/lib/aspera/ascp/log_analyzer/log_file.rb +593 -0
  37. data/lib/aspera/ascp/log_analyzer/records.rb +379 -0
  38. data/lib/aspera/ascp/log_analyzer/rules.rb +172 -0
  39. data/lib/aspera/ascp/log_analyzer/session.rb +782 -0
  40. data/lib/aspera/ascp/log_analyzer.rb +354 -0
  41. data/lib/aspera/ascp/management.rb +16 -8
  42. data/lib/aspera/ascp/session_errors.rb +217 -0
  43. data/lib/aspera/assert.rb +4 -0
  44. data/lib/aspera/cli/ascp_actions.rb +12 -19
  45. data/lib/aspera/cli/ascp_logs_actions.rb +176 -0
  46. data/lib/aspera/cli/async_transfer_store.rb +7 -4
  47. data/lib/aspera/cli/bootstrapper.rb +37 -33
  48. data/lib/aspera/cli/command_line.rb +24 -6
  49. data/lib/aspera/cli/command_registry.rb +6 -1
  50. data/lib/aspera/cli/command_spec.rb +34 -7
  51. data/lib/aspera/cli/completion/ascli.bash +19 -5
  52. data/lib/aspera/cli/completion/ascli.fish +13 -2
  53. data/lib/aspera/cli/completion/ascli.ps1 +31 -9
  54. data/lib/aspera/cli/completion/ascli.zsh +22 -6
  55. data/lib/aspera/cli/completion_actions.rb +303 -69
  56. data/lib/aspera/cli/context.rb +9 -4
  57. data/lib/aspera/cli/deprecation.rb +5 -2
  58. data/lib/aspera/cli/error.rb +3 -0
  59. data/lib/aspera/cli/extended_value.rb +61 -27
  60. data/lib/aspera/cli/formatter.rb +29 -22
  61. data/lib/aspera/cli/gem_checker.rb +9 -6
  62. data/lib/aspera/cli/hints.rb +22 -10
  63. data/lib/aspera/cli/http.rb +12 -9
  64. data/lib/aspera/cli/info.rb +12 -7
  65. data/lib/aspera/cli/mailer.rb +6 -3
  66. data/lib/aspera/cli/mcp/http_servlet.rb +7 -8
  67. data/lib/aspera/cli/mcp/secrets.rb +8 -5
  68. data/lib/aspera/cli/mcp/tool.rb +15 -14
  69. data/lib/aspera/cli/option_declarator.rb +10 -4
  70. data/lib/aspera/cli/option_registry.rb +6 -3
  71. data/lib/aspera/cli/option_types.rb +17 -2
  72. data/lib/aspera/cli/option_value.rb +65 -12
  73. data/lib/aspera/cli/options.schema.yaml +487 -63
  74. data/lib/aspera/cli/parser.rb +56 -32
  75. data/lib/aspera/cli/plugins/alee.rb +7 -4
  76. data/lib/aspera/cli/plugins/aoc.rb +244 -35
  77. data/lib/aspera/cli/plugins/ats.rb +9 -8
  78. data/lib/aspera/cli/plugins/base.rb +17 -12
  79. data/lib/aspera/cli/plugins/basic_auth.rb +5 -2
  80. data/lib/aspera/cli/plugins/config.rb +198 -103
  81. data/lib/aspera/cli/plugins/console.rb +6 -3
  82. data/lib/aspera/cli/plugins/cos.rb +8 -4
  83. data/lib/aspera/cli/plugins/factory.rb +21 -7
  84. data/lib/aspera/cli/plugins/faspex5.rb +28 -25
  85. data/lib/aspera/cli/plugins/faspio.rb +7 -5
  86. data/lib/aspera/cli/plugins/httpgw.rb +7 -4
  87. data/lib/aspera/cli/plugins/mcp.rb +28 -22
  88. data/lib/aspera/cli/plugins/node.rb +31 -30
  89. data/lib/aspera/cli/plugins/oauth.rb +5 -1
  90. data/lib/aspera/cli/plugins/orchestrator.rb +272 -72
  91. data/lib/aspera/cli/plugins/preview.rb +69 -44
  92. data/lib/aspera/cli/plugins/server.rb +63 -11
  93. data/lib/aspera/cli/plugins/shares.rb +86 -16
  94. data/lib/aspera/cli/plugins/transferd.rb +223 -0
  95. data/lib/aspera/cli/preset_actions.rb +144 -30
  96. data/lib/aspera/cli/preset_manager.rb +52 -18
  97. data/lib/aspera/cli/prompt.rb +4 -1
  98. data/lib/aspera/cli/result.rb +12 -8
  99. data/lib/aspera/cli/runner.rb +69 -46
  100. data/lib/aspera/cli/secret_finder.rb +4 -1
  101. data/lib/aspera/cli/security.rb +79 -9
  102. data/lib/aspera/cli/special_values.rb +4 -1
  103. data/lib/aspera/cli/sync_actions.rb +13 -10
  104. data/lib/aspera/cli/table_fitter.rb +3 -0
  105. data/lib/aspera/cli/terminal_formatter.rb +9 -6
  106. data/lib/aspera/cli/transfer_actions.rb +11 -8
  107. data/lib/aspera/cli/transfer_agent.rb +29 -20
  108. data/lib/aspera/cli/transfer_progress.rb +13 -45
  109. data/lib/aspera/cli/vault_actions.rb +45 -0
  110. data/lib/aspera/cli/vault_manager.rb +188 -42
  111. data/lib/aspera/cli/version.rb +4 -1
  112. data/lib/aspera/cli/web_secret.rb +12 -10
  113. data/lib/aspera/cli/wizard.rb +42 -39
  114. data/lib/aspera/command_line_builder.rb +9 -5
  115. data/lib/aspera/command_line_converter.rb +4 -1
  116. data/lib/aspera/coverage.rb +103 -33
  117. data/lib/aspera/data_repository.rb +4 -1
  118. data/lib/aspera/dot_container.rb +4 -1
  119. data/lib/aspera/duration.rb +5 -2
  120. data/lib/aspera/environment.rb +21 -12
  121. data/lib/aspera/exec_spec.rb +3 -0
  122. data/lib/aspera/faspex_gw.rb +13 -9
  123. data/lib/aspera/faspex_postproc.rb +15 -12
  124. data/lib/aspera/formatter_interface.rb +4 -1
  125. data/lib/aspera/graphql.rb +5 -1
  126. data/lib/aspera/hash_ext.rb +3 -0
  127. data/lib/aspera/id_generator.rb +4 -2
  128. data/lib/aspera/json_rpc/client.rb +6 -5
  129. data/lib/aspera/json_rpc/version.rb +3 -0
  130. data/lib/aspera/line_logger.rb +4 -1
  131. data/lib/aspera/link_header.rb +4 -1
  132. data/lib/aspera/log.rb +18 -5
  133. data/lib/aspera/markdown.rb +5 -2
  134. data/lib/aspera/mime.rb +3 -0
  135. data/lib/aspera/nagios.rb +6 -4
  136. data/lib/aspera/node_emulator.rb +15 -13
  137. data/lib/aspera/oauth/client/base.rb +91 -70
  138. data/lib/aspera/oauth/client/boot.rb +7 -4
  139. data/lib/aspera/oauth/client/factory.rb +6 -3
  140. data/lib/aspera/oauth/client/generic.rb +5 -1
  141. data/lib/aspera/oauth/client/json_credentials.rb +4 -1
  142. data/lib/aspera/oauth/client/jwt.rb +6 -3
  143. data/lib/aspera/oauth/client/shares_web.rb +85 -0
  144. data/lib/aspera/oauth/client/url_json.rb +4 -1
  145. data/lib/aspera/oauth/client/web.rb +39 -12
  146. data/lib/aspera/oauth/server/pkce.rb +6 -5
  147. data/lib/aspera/orchestrator_workflow_checker.rb +119 -1
  148. data/lib/aspera/persistency_action_once.rb +5 -3
  149. data/lib/aspera/persistency_folder.rb +6 -3
  150. data/lib/aspera/preview/file_types.rb +6 -3
  151. data/lib/aspera/preview/generator.rb +9 -5
  152. data/lib/aspera/preview/options.schema.yaml +3 -3
  153. data/lib/aspera/preview/terminal.rb +8 -4
  154. data/lib/aspera/preview/utils.rb +6 -4
  155. data/lib/aspera/products/connect.rb +5 -2
  156. data/lib/aspera/products/desktop.rb +4 -1
  157. data/lib/aspera/products/other.rb +4 -2
  158. data/lib/aspera/products/transferd.rb +6 -2
  159. data/lib/aspera/products/transferd_service.rb +610 -0
  160. data/lib/aspera/proxy_auto_config.js +2 -2
  161. data/lib/aspera/proxy_auto_config.rb +5 -2
  162. data/lib/aspera/rainbow.rb +3 -0
  163. data/lib/aspera/rest/aspera_errors.rb +4 -1
  164. data/lib/aspera/rest/call_error.rb +3 -0
  165. data/lib/aspera/rest/client.rb +22 -29
  166. data/lib/aspera/rest/error_analyzer.rb +5 -2
  167. data/lib/aspera/rest/list.rb +8 -4
  168. data/lib/aspera/rest/parameters.rb +3 -0
  169. data/lib/aspera/rest/util.rb +8 -5
  170. data/lib/aspera/rest.rb +9 -6
  171. data/lib/aspera/schema/IBM Aspera Faspex API-5.0-enhanced.yaml +9 -9
  172. data/lib/aspera/schema/IBM Aspera Orchestrator API-v1-enhanced.yaml +118 -30
  173. data/lib/aspera/schema/IBM Aspera on Cloud API-0.2.6-enhanced.yaml +11 -11
  174. data/lib/aspera/schema/IBM Aspera on Cloud Automation API-1.0.5-enhanced.yaml +2437 -1437
  175. data/lib/aspera/schema/async_tables.yaml +11 -10
  176. data/lib/aspera/schema/documentation.rb +8 -2
  177. data/lib/aspera/schema/reader.rb +7 -0
  178. data/lib/aspera/schema/registry.rb +43 -28
  179. data/lib/aspera/schema/transferd-config.schema.json +333 -0
  180. data/lib/aspera/schema/validator.rb +4 -1
  181. data/lib/aspera/sealed_hash.rb +166 -0
  182. data/lib/aspera/secret/hider.rb +201 -0
  183. data/lib/aspera/secret/reference.rb +150 -0
  184. data/lib/aspera/secret/resolver.rb +212 -0
  185. data/lib/aspera/secret/store/base.rb +153 -0
  186. data/lib/aspera/secret/store/encrypted_hash.rb +329 -0
  187. data/lib/aspera/secret/store/factory.rb +103 -0
  188. data/lib/aspera/secret/store/hashicorp_vault.rb +390 -0
  189. data/lib/aspera/secret/store/keeper.rb +141 -0
  190. data/lib/aspera/secret/store/macos_security.rb +284 -0
  191. data/lib/aspera/secret/store/one_password_api.rb +99 -0
  192. data/lib/aspera/secret/store/one_password_base.rb +42 -0
  193. data/lib/aspera/secret/store/one_password_cli.rb +110 -0
  194. data/lib/aspera/secret/store/secret_service.rb +125 -0
  195. data/lib/aspera/secret/strength.rb +119 -0
  196. data/lib/aspera/servlet_helpers.rb +5 -2
  197. data/lib/aspera/ssh.rb +10 -7
  198. data/lib/aspera/ssl.rb +5 -2
  199. data/lib/aspera/string_ext.rb +3 -0
  200. data/lib/aspera/sync/args.schema.yaml +3 -0
  201. data/lib/aspera/sync/conf.schema.yaml +9 -4
  202. data/lib/aspera/sync/database.rb +3 -0
  203. data/lib/aspera/sync/operations.rb +16 -14
  204. data/lib/aspera/temp_file_manager.rb +8 -4
  205. data/lib/aspera/timer_limiter.rb +3 -0
  206. data/lib/aspera/transfer/error.rb +4 -1
  207. data/lib/aspera/transfer/faux_file.rb +4 -1
  208. data/lib/aspera/transfer/parameters.rb +17 -14
  209. data/lib/aspera/transfer/result.rb +3 -0
  210. data/lib/aspera/transfer/resumer.rb +6 -4
  211. data/lib/aspera/transfer/spec.rb +9 -22
  212. data/lib/aspera/transfer/units.rb +75 -0
  213. data/lib/aspera/uri_reader.rb +57 -7
  214. data/lib/aspera/web_auth/index.html.erb +6 -2
  215. data/lib/aspera/web_auth/paste.rb +109 -0
  216. data/lib/aspera/web_auth/receiver.rb +50 -0
  217. data/lib/aspera/web_auth/relay.rb +76 -0
  218. data/lib/aspera/web_auth/reply.html.erb +150 -0
  219. data/lib/aspera/web_auth/server.rb +47 -10
  220. data/lib/aspera/web_page.rb +7 -2
  221. data/lib/aspera/web_server_simple.rb +6 -3
  222. data/lib/aspera/yaml.rb +5 -1
  223. data/llms.txt +81 -0
  224. data/skills/ascli/SKILL.md +88 -0
  225. data.tar.gz.sig +0 -0
  226. metadata +72 -19
  227. metadata.gz.sig +0 -0
  228. data/lib/aspera/keychain/base.rb +0 -30
  229. data/lib/aspera/keychain/encrypted_hash.rb +0 -192
  230. data/lib/aspera/keychain/factory.rb +0 -58
  231. data/lib/aspera/keychain/hashicorp_vault.rb +0 -77
  232. data/lib/aspera/keychain/macos_security.rb +0 -277
  233. data/lib/aspera/keychain/one_password_api.rb +0 -91
  234. data/lib/aspera/keychain/one_password_base.rb +0 -39
  235. data/lib/aspera/keychain/one_password_cli.rb +0 -100
  236. data/lib/aspera/secret_hider.rb +0 -196
  237. data/lib/aspera/secret_strength.rb +0 -114
@@ -1,10 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # SPDX-FileCopyrightText: IBM Corp.
4
+ # SPDX-License-Identifier: Apache-2.0
5
+
6
+ require_relative 'assert'
7
+ require_relative 'log'
8
+ require_relative 'rest'
9
+ require_relative 'temp_file_manager'
3
10
  require 'uri'
4
11
  require 'base64'
5
- require 'aspera/assert'
6
- require 'aspera/rest'
7
- require 'aspera/temp_file_manager'
8
12
 
9
13
  module Aspera
10
14
  # Read content from a URI; supported schemes: file:, http:, https:, data:
@@ -25,13 +29,15 @@ module Aspera
25
29
  # The canonical form built by {file_url} uses the `file:///` prefix.
26
30
  module UriReader
27
31
  SCHEME_FILE = 'file'
28
- SCHEME_FILE_PFX1 = "#{SCHEME_FILE}:"
32
+ SCHEME_FILE_PFX1 = "#{SCHEME_FILE}:".freeze
29
33
  # Canonical prefix for file: URLs (no host, three slashes).
30
34
  # What follows is the literal path: relative or absolute (starting with a second slash).
31
- SCHEME_FILE_PFX2 = "#{SCHEME_FILE_PFX1}///"
32
- private_constant :SCHEME_FILE, :SCHEME_FILE_PFX1, :SCHEME_FILE_PFX2
35
+ SCHEME_FILE_PFX2 = "#{SCHEME_FILE_PFX1}///".freeze
36
+ # Timeout in seconds when another URL remains to be tried
37
+ FAIL_FAST_TIMEOUT = 10
38
+ private_constant :SCHEME_FILE, :SCHEME_FILE_PFX1, :SCHEME_FILE_PFX2, :FAIL_FAST_TIMEOUT
33
39
  class << self
34
- # @return [Boolean] true if `url` uses the file: scheme recognised by this module
40
+ # @return [Boolean] true if `url` uses the file: scheme recognized by this module
35
41
  def file?(url)
36
42
  url.start_with?(SCHEME_FILE_PFX1)
37
43
  end
@@ -87,6 +93,50 @@ module Aspera
87
93
  end
88
94
  end
89
95
 
96
+ # Read URL with a short timeout and no retry (temporarily changes global HTTP parameters)
97
+ # @param url [String] URL to read
98
+ # @return [String] content
99
+ def read_fail_fast(url)
100
+ parameters = Rest::Parameters.instance
101
+ session_cb = parameters.session_cb
102
+ retry_max = parameters.retry_max
103
+ parameters.retry_max = 0
104
+ parameters.session_cb = lambda do |http_session|
105
+ session_cb&.call(http_session)
106
+ http_session.open_timeout = [http_session.open_timeout, FAIL_FAST_TIMEOUT].compact.min
107
+ http_session.read_timeout = [http_session.read_timeout, FAIL_FAST_TIMEOUT].compact.min
108
+ end
109
+ read(url)
110
+ ensure
111
+ parameters.session_cb = session_cb
112
+ parameters.retry_max = retry_max
113
+ end
114
+
115
+ # Read the first URL that provides valid content.
116
+ # All but the last URL are read with a short timeout and no retry, so that fallback is quick.
117
+ # On failure of a URL (read error, or validation error), a warning is logged and the next URL is tried.
118
+ # The failure of the last URL is raised.
119
+ # @param urls [Array<String>] URLs, in order of preference
120
+ # @param what [String] description of the content, for messages
121
+ # @yield [content] Optional: validates and converts content, shall raise on invalid content
122
+ # @yieldparam content [String] content read
123
+ # @yieldreturn [Object] value returned by this method
124
+ # @return [Object] result of block if given, else content
125
+ def read_first(urls, what: 'content')
126
+ Aspera.assert(!urls.empty?) { "no URL for #{what}" }
127
+ urls.each_with_index do |url, index|
128
+ last = index.eql?(urls.length - 1)
129
+ Log.log.debug { "Retrieving #{what} from #{url}" }
130
+ begin
131
+ content = last ? read(url) : read_fail_fast(url)
132
+ return block_given? ? yield(content) : content
133
+ rescue StandardError => e
134
+ raise if last
135
+ Log.log.warn { "Failed to retrieve #{what} from #{url}: #{e.message}, trying next" }
136
+ end
137
+ end
138
+ end
139
+
90
140
  # Return the local file-system path for the content at `url`, downloading to a temp file if needed.
91
141
  # For file: URLs the path is extracted directly (no download).
92
142
  # For data: and http(s): URLs the content is written to a temporary file and its path is returned.
@@ -5,8 +5,8 @@
5
5
  </svg>
6
6
  </div>
7
7
 
8
- <h1>Authentication Successful</h1>
9
- <p>You can close this window and return to your terminal.</p>
8
+ <h1>Authentication Received</h1>
9
+ <p>Return to your terminal: it completes the login and shows the result. You can then close this window.</p>
10
10
 
11
11
  <% unless additional_info.to_s.empty? %>
12
12
  <div class="info-box">
@@ -14,3 +14,7 @@
14
14
  </div>
15
15
  <% end %>
16
16
 
17
+ <%# The callback URL may contain a code or tokens: remove the query from the address bar and from the entry of the session history %>
18
+ <script>
19
+ if (window.history && history.replaceState) history.replaceState(null, '', location.pathname);
20
+ </script>
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ # SPDX-FileCopyrightText: IBM Corp.
4
+ # SPDX-License-Identifier: Apache-2.0
5
+
6
+ require_relative '../assert'
7
+ require_relative '../log'
8
+ require_relative '../cli/prompt'
9
+ require_relative '../cli/info'
10
+ require_relative '../sealed_hash'
11
+ require_relative '../rainbow'
12
+ require 'uri'
13
+ using Rainbow
14
+
15
+ module Aspera
16
+ module WebAuth
17
+ # Receives the result of a web login from the user, who gives it in the terminal.
18
+ # No server is started: used when the browser cannot reach the loopback address of this host (e.g. SSH session, without tunnel).
19
+ # The user gives either:
20
+ # - a code (`SealedHash`) shown by the browser of the workstation, after `ascli config web_login <code>` (`ascli` is installed there)
21
+ # - the URL the browser was redirected to (it shows a connection error), or its query
22
+ # Same interface as `Server` for the part used by OAuth clients: `received_request`.
23
+ class Paste
24
+ class << self
25
+ # Extract the query parameters from what the user pasted
26
+ # @param input [String] Code, or full redirect URL, or the query only (with or without leading `?`)
27
+ # @return [Hash{String=>String}] Query parameters, includes a non-empty `state`
28
+ # @raise [ParameterError] if the input has no query, or no `state`
29
+ def parse(input)
30
+ text = input.to_s.strip
31
+ raise ParameterError, 'Nothing was pasted' if text.empty?
32
+ params =
33
+ if SealedHash.sealed?(text)
34
+ data = SealedHash.unseal(text, base_key: Cli::Info::CMD_NAME)
35
+ raise ParameterError, 'This code is for ascli on the workstation: paste the code shown by the browser after login' if data.key?('login_url')
36
+ data
37
+ else
38
+ parse_query(text)
39
+ end
40
+ raise ParameterError, 'No "state" found in what was pasted' if params['state'].to_s.empty?
41
+ params
42
+ end
43
+
44
+ private
45
+
46
+ # @param text [String] Full redirect URL, or query
47
+ # @return [Hash{String=>String}]
48
+ def parse_query(text)
49
+ # fragment is not sent to a server, ignore it
50
+ text = text.sub(/#.*\z/m, '')
51
+ query =
52
+ if text.match?(%r{\A[a-z][a-z0-9+.-]*://}i)
53
+ URI.parse(text).query.to_s
54
+ else
55
+ text.delete_prefix('?')
56
+ end
57
+ raise ParameterError, 'No query found in pasted URL: copy the full URL from the address bar of the browser' if query.empty?
58
+ URI.decode_www_form(query).to_h
59
+ rescue URI::InvalidURIError, ArgumentError => e
60
+ raise ParameterError, "Pasted text is not a valid code, URL or query: #{e.message}"
61
+ end
62
+ end
63
+
64
+ # @param redirect_uri [String] Loopback URL registered for the login (the browser fails to connect to it)
65
+ # @param login_url [String, nil] URL of the login page: if set, a code is given to the user for `ascli` on the workstation
66
+ # @param reply_keys [Array, nil] Keys of the query needed to continue (with `login_url`)
67
+ def initialize(redirect_uri, login_url: nil, reply_keys: nil)
68
+ @redirect_uri = redirect_uri
69
+ @login_url = login_url
70
+ @reply_keys = reply_keys
71
+ end
72
+
73
+ # @return [Boolean] true if the message given to the user contains the login URL (so it shall not be displayed separately)
74
+ def shows_login_url?
75
+ !@login_url.nil?
76
+ end
77
+
78
+ # Ask the user for the code, or the URL the browser was redirected to
79
+ # Input is not echoed on a terminal: it contains an authorization code or tokens.
80
+ # @return [Hash{String=>String}] The query
81
+ def received_request
82
+ Log.log.warn do
83
+ browser_steps = [
84
+ "The browser shows a connection error on #{@redirect_uri}: this is expected.",
85
+ 'Copy the full URL from the address bar of the browser, and paste it in the prompt below.'
86
+ ]
87
+ if @login_url.nil?
88
+ (['USER ACTION: Log in with the web browser, then:'] + browser_steps.map { |s| " - #{s}" }).join("\n")
89
+ else
90
+ code = SealedHash.seal({'login_url' => @login_url.to_s, 'redirect_uri' => @redirect_uri, 'reply_keys' => @reply_keys}, base_key: Cli::Info::CMD_NAME)
91
+ [
92
+ 'USER ACTION: To continue, choose one of these two ways:',
93
+ " 1. With any browser (#{Cli::Info::CMD_NAME} is not needed on the workstation):",
94
+ ' - Open this URL in a browser and log in:',
95
+ " #{@login_url.to_s.red}",
96
+ *browser_steps.map { |s| " - #{s}" },
97
+ " 2. If #{Cli::Info::CMD_NAME} is installed on the workstation that has the browser:",
98
+ ' - Run there:',
99
+ " #{Cli::Info::CMD_NAME} config web_login #{code}",
100
+ ' - Log in in the page that opens.',
101
+ ' - Copy the code shown by the browser, and paste it in the prompt below.'
102
+ ].join("\n")
103
+ end
104
+ end
105
+ self.class.parse(Cli::Prompt.prompt_user_input('Code or URL', sensitive: true))
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ # SPDX-FileCopyrightText: IBM Corp.
4
+ # SPDX-License-Identifier: Apache-2.0
5
+
6
+ require_relative 'server'
7
+ require_relative 'paste'
8
+ require_relative '../environment'
9
+ require_relative '../log'
10
+
11
+ module Aspera
12
+ module WebAuth
13
+ class << self
14
+ # Create the object that receives the result of a web login, according to `Environment#web_auth` (option `ui.web_auth`):
15
+ # `:port` listens on the loopback address of `redirect_uri`, `:paste` asks the user to give the result in the terminal.
16
+ # Both provide `received_request`.
17
+ # @param redirect_uri [String] Loopback URL that receives the result
18
+ # @param additional_info [String] Information in web page (only with `:port`)
19
+ # @param login_url [String, URI] URL of the login page: with `:paste`, a code for `ascli` on the workstation is given to the user,
20
+ # unless the browser is started by `ascli` (the user has no time to use the code, and the browser is local)
21
+ # @param reply_keys [Array<String>] Keys of the query that the client needs (with `login_url`)
22
+ # @return [Server, Paste]
23
+ def receiver(redirect_uri, additional_info = nil, login_url: nil, reply_keys: nil)
24
+ env = Environment.instance
25
+ case env.web_auth
26
+ when :paste
27
+ login_url = nil if env.browser_command || env.url_method.eql?(:graphical)
28
+ Paste.new(redirect_uri, login_url: login_url, reply_keys: reply_keys)
29
+ when :port
30
+ if env.url_method.eql?(:text) && env.browser_command.nil? && (ENV.key?('SSH_CONNECTION') || ENV.key?('SSH_TTY'))
31
+ Log.log.warn do
32
+ "Web login: the browser must reach port #{URI.parse(redirect_uri).port} of this host (SSH tunnel). " \
33
+ 'If not possible, use option --ui.web_auth=paste'
34
+ end
35
+ end
36
+ Server.new(redirect_uri, additional_info)
37
+ else Aspera.error_unexpected_value(env.web_auth) { 'ui.web_auth' }
38
+ end
39
+ end
40
+
41
+ # Let the user open the login page, unless the receiver already gives the URL in its own message
42
+ # @param login_receiver [Server, Paste] Result of `receiver`
43
+ # @param login_url [String, URI] URL of the login page
44
+ def open_login_page(login_receiver, login_url)
45
+ return if login_receiver.is_a?(Paste) && login_receiver.shows_login_url?
46
+ Environment.instance.open_uri(login_url)
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ # SPDX-FileCopyrightText: IBM Corp.
4
+ # SPDX-License-Identifier: Apache-2.0
5
+
6
+ require_relative 'server'
7
+ require_relative '../web_server_simple'
8
+ require_relative '../environment'
9
+ require_relative '../assert'
10
+ require 'uri'
11
+
12
+ module Aspera
13
+ module WebAuth
14
+ # Login on the workstation that has the browser, for `ascli` that runs on another host (option `ui.web_auth=paste`).
15
+ # The remote `ascli` gives a code (`SealedHash`) with the URL of the login page, the loopback URL to which the browser is redirected,
16
+ # and the keys of the reply. Here, a local server receives the redirection, and the page shown in the browser gives a code (type reply)
17
+ # to paste in the terminal of the remote `ascli`.
18
+ module Relay
19
+ # @!attribute [r] login_url
20
+ # @return [URI] URL of the login page (https)
21
+ # @!attribute [r] redirect_uri
22
+ # @return [String] Loopback URL to which the browser is redirected
23
+ # @!attribute [r] reply_keys
24
+ # @return [Array<String>] Keys of the query that are given to the remote `ascli`
25
+ # @!attribute [r] host
26
+ # @return [String] Host of the login page, calculated here (not given by the remote `ascli`)
27
+ Login = Struct.new(:login_url, :redirect_uri, :reply_keys, :host, keyword_init: true)
28
+ # Keys of the reply are names of query parameters
29
+ REPLY_KEY_REGEX = /\A[a-z][a-z0-9_]{0,63}\z/
30
+ MAX_REPLY_KEYS = 16
31
+ private_constant :REPLY_KEY_REGEX, :MAX_REPLY_KEYS
32
+
33
+ class << self
34
+ # The code comes from another host, or from a message: its content is checked.
35
+ # @param data [Hash] Content of a code of type login: `login_url`, `redirect_uri`, `reply_keys`
36
+ # @return [Login]
37
+ # @raise [ParameterError] if content is not acceptable
38
+ def parse(data)
39
+ Aspera.assert_type(data, Hash, type: ParameterError) { 'login code' }
40
+ login_url = parse_uri(data['login_url'], 'login_url')
41
+ raise ParameterError, "URL of the login page shall be https: #{login_url.scheme}://#{login_url.host}" unless login_url.scheme.eql?('https')
42
+ raise ParameterError, 'URL of the login page has no host' if login_url.host.to_s.empty?
43
+ redirect = parse_uri(data['redirect_uri'], 'redirect_uri')
44
+ raise ParameterError, "redirect_uri shall be http or https, with a port: #{redirect}" unless %w[http https].include?(redirect.scheme) && redirect.port
45
+ raise ParameterError, "redirect_uri host shall be loopback (localhost, 127.0.0.0/8, ::1): #{redirect.host}" unless WebServerSimple.loopback?(redirect.hostname.to_s)
46
+ keys = data['reply_keys']
47
+ valid_keys = keys.is_a?(Array) && !keys.empty? && keys.length <= MAX_REPLY_KEYS && keys.all? { |key| key.is_a?(String) && key.match?(REPLY_KEY_REGEX) }
48
+ raise ParameterError, 'reply_keys shall be a list of parameter names' unless valid_keys
49
+ Login.new(login_url: login_url, redirect_uri: redirect.to_s, reply_keys: keys, host: login_url.host.downcase)
50
+ end
51
+
52
+ # Open the login page, receive the redirection, and show the code to the user in the browser.
53
+ # @param login [Login] Result of `parse`
54
+ # @return [nil]
55
+ def run(login)
56
+ server = Server.new(login.redirect_uri, "#{login.host} - login for ascli on another host", reply_keys: login.reply_keys)
57
+ Environment.instance.open_uri(login.login_url)
58
+ server.received_request
59
+ # the page that gives the code is sent after the request is received: wait for it before ending
60
+ server.wait_stopped
61
+ nil
62
+ end
63
+
64
+ private
65
+
66
+ # @return [URI]
67
+ def parse_uri(value, name)
68
+ raise ParameterError, "#{name} is missing" unless value.is_a?(String) && !value.empty?
69
+ URI.parse(value)
70
+ rescue URI::InvalidURIError => e
71
+ raise ParameterError, "#{name} is not a valid URL: #{e.message}"
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,150 @@
1
+ <%# Content of the page displayed after login when the reply is given to the user, to paste in the terminal of ascli, in layout of Aspera::WebPage. Variables: additional_info, code, nonce %>
2
+ <style>
3
+ .code-box {
4
+ display: flex;
5
+ align-items: stretch;
6
+ gap: 0.5rem;
7
+ margin-top: 1.25rem;
8
+ }
9
+
10
+ .code {
11
+ flex: 1;
12
+ min-width: 0;
13
+ padding: 0.6rem 0.75rem;
14
+ text-align: left;
15
+ font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
16
+ font-size: 0.8rem;
17
+ line-height: 1.4;
18
+ letter-spacing: 0.05em;
19
+ word-break: break-all;
20
+ max-height: 9rem;
21
+ overflow-y: auto;
22
+ background: var(--surface);
23
+ color: var(--text);
24
+ border: 1px solid var(--border);
25
+ border-radius: 6px;
26
+ user-select: none;
27
+ }
28
+
29
+ .code.revealed {
30
+ user-select: all;
31
+ }
32
+
33
+ .code-box button.icon {
34
+ flex: none;
35
+ width: 2.75rem;
36
+ padding: 0;
37
+ display: flex;
38
+ align-items: center;
39
+ justify-content: center;
40
+ background: var(--card-bg);
41
+ color: var(--text-secondary);
42
+ border: 1px solid var(--input-border);
43
+ }
44
+
45
+ .code-box button.icon:hover {
46
+ color: var(--accent);
47
+ border-color: var(--accent);
48
+ }
49
+
50
+ .code-box button.icon svg.hide,
51
+ .code-box button.icon.on svg.show {
52
+ display: none;
53
+ }
54
+
55
+ .code-box button.icon.on svg.hide {
56
+ display: block;
57
+ }
58
+
59
+ .status {
60
+ min-height: 1.4rem;
61
+ font-size: 0.85rem;
62
+ color: var(--success);
63
+ }
64
+ </style>
65
+
66
+ <div class="icon-circle ok">
67
+ <svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" viewBox="0 0 32 32" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round">
68
+ <polyline points="6 16 13 23 26 9"/>
69
+ </svg>
70
+ </div>
71
+
72
+ <h1>Login Complete</h1>
73
+ <p>Copy this code, and paste it in the terminal where <b>ascli</b> waits for it. You can then close this window.</p>
74
+
75
+ <% unless additional_info.to_s.empty? %>
76
+ <div class="info-box">
77
+ <%= h additional_info %>
78
+ </div>
79
+ <% end %>
80
+
81
+ <div class="code-box">
82
+ <div id="code" class="code" aria-live="polite">&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;</div>
83
+ <button type="button" id="reveal" class="icon" title="Show the code" aria-label="Show the code" aria-pressed="false">
84
+ <svg class="show" xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
85
+ <path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/>
86
+ <circle cx="12" cy="12" r="3"/>
87
+ </svg>
88
+ <svg class="hide" xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
89
+ <path d="M17.94 17.94A10.07 10.07 0 0 1 12 20c-7 0-11-8-11-8a18.45 18.45 0 0 1 5.06-5.94M9.9 4.24A9.12 9.12 0 0 1 12 4c7 0 11 8 11 8a18.5 18.5 0 0 1-2.16 3.19m-6.72-1.07a3 3 0 1 1-4.24-4.24"/>
90
+ <line x1="1" y1="1" x2="23" y2="23"/>
91
+ </svg>
92
+ </button>
93
+ </div>
94
+ <div class="actions">
95
+ <button type="button" id="copy" class="allow">Copy</button>
96
+ </div>
97
+ <p id="status" class="status" role="status"></p>
98
+
99
+ <%# The code is in this script only, not in the page: it is inserted in the page when the user shows it %>
100
+ <script nonce="<%= h nonce %>">
101
+ (function () {
102
+ var code = <%= JSON.generate(code.to_s).gsub('<', '\u003c') %>;
103
+ var box = document.getElementById('code');
104
+ var mask = box.textContent;
105
+ var reveal = document.getElementById('reveal');
106
+ var status = document.getElementById('status');
107
+ var shown = false;
108
+
109
+ reveal.addEventListener('click', function () {
110
+ shown = !shown;
111
+ box.textContent = shown ? code : mask;
112
+ box.classList.toggle('revealed', shown);
113
+ reveal.classList.toggle('on', shown);
114
+ reveal.setAttribute('aria-pressed', shown ? 'true' : 'false');
115
+ var label = shown ? 'Hide the code' : 'Show the code';
116
+ reveal.title = label;
117
+ reveal.setAttribute('aria-label', label);
118
+ });
119
+
120
+ function legacyCopy() {
121
+ var area = document.createElement('textarea');
122
+ area.value = code;
123
+ area.setAttribute('readonly', '');
124
+ area.style.position = 'fixed';
125
+ area.style.opacity = '0';
126
+ document.body.appendChild(area);
127
+ area.select();
128
+ var ok = false;
129
+ try { ok = document.execCommand('copy'); } catch (e) { ok = false; }
130
+ document.body.removeChild(area);
131
+ return ok;
132
+ }
133
+
134
+ function done(ok) {
135
+ status.style.color = ok ? '' : 'var(--danger)';
136
+ status.textContent = ok ? 'Copied. Paste it in the terminal.' : 'Copy failed: show the code, select it and copy it.';
137
+ }
138
+
139
+ document.getElementById('copy').addEventListener('click', function () {
140
+ if (navigator.clipboard && window.isSecureContext) {
141
+ navigator.clipboard.writeText(code).then(function () { done(true); }, function () { done(legacyCopy()); });
142
+ } else {
143
+ done(legacyCopy());
144
+ }
145
+ });
146
+
147
+ // The callback URL contains a code or tokens: remove the query from the address bar and from the entry of the session history
148
+ if (window.history && history.replaceState) history.replaceState(null, '', location.pathname);
149
+ })();
150
+ </script>
@@ -1,9 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'aspera/web_server_simple'
4
- require 'aspera/servlet_helpers'
5
- require 'aspera/web_page'
6
- require 'aspera/assert'
3
+ # SPDX-FileCopyrightText: IBM Corp.
4
+ # SPDX-License-Identifier: Apache-2.0
5
+
6
+ require_relative '../web_server_simple'
7
+ require_relative '../servlet_helpers'
8
+ require_relative '../web_page'
9
+ require_relative '../sealed_hash'
10
+ require_relative '../cli/info'
11
+ require_relative '../assert'
12
+ require 'json'
13
+ require 'securerandom'
7
14
 
8
15
  module Aspera
9
16
  module WebAuth
@@ -13,7 +20,11 @@ module Aspera
13
20
 
14
21
  # ERB template of page displayed in browser after authentication
15
22
  HTML_TEMPLATE_PATH = File.join(__dir__, 'index.html.erb')
16
- private_constant :HTML_TEMPLATE_PATH
23
+ # ERB template of page displayed in browser after authentication, when the reply is given to the user to paste in a terminal
24
+ REPLY_TEMPLATE_PATH = File.join(__dir__, 'reply.html.erb')
25
+ # Policy of the reply page: it has a secret. Only its own script (with nonce) and style, nothing external
26
+ REPLY_CSP = "default-src 'none'; img-src data:; style-src 'unsafe-inline'; script-src 'nonce-%<nonce>s'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'"
27
+ private_constant :HTML_TEMPLATE_PATH, :REPLY_TEMPLATE_PATH, :REPLY_CSP
17
28
 
18
29
  # @param server [WEBrick::HTTPServer] the HTTP server instance
19
30
  # @param web_auth [Server] the Server instance to record the callback
@@ -33,25 +44,42 @@ module Aspera
33
44
  Aspera.assert_values(request.path, [@web_auth.expected_path], type: WEBrick::HTTPStatus::NotFound) { "Unsupported path: #{request.path}" }
34
45
  # response to browser
35
46
  response.status = 200
36
- WebPage.secure_headers(response)
37
- response.body = WebPage.render(HTML_TEMPLATE_PATH, title: 'Authentication Complete', additional_info: @web_auth.additional_info)
47
+ if @web_auth.reply_keys.nil?
48
+ WebPage.secure_headers(response)
49
+ response.body = WebPage.render(HTML_TEMPLATE_PATH, title: 'Authentication Complete', additional_info: @web_auth.additional_info)
50
+ else
51
+ render_reply(request, response)
52
+ end
38
53
  # continue internal processing
39
54
  @web_auth.signal_request(request)
40
55
  nil
41
56
  end
57
+
58
+ private
59
+
60
+ # Page that gives the reply to the user: the code to paste in the terminal of the remote ascli
61
+ def render_reply(request, response)
62
+ nonce = SecureRandom.base64(16)
63
+ WebPage.secure_headers(response, content_security_policy: format(REPLY_CSP, nonce: nonce))
64
+ code = SealedHash.seal(request.query.slice(*@web_auth.reply_keys).transform_values(&:to_s), base_key: Cli::Info::CMD_NAME)
65
+ response.body = WebPage.render(REPLY_TEMPLATE_PATH, title: 'Login Complete', additional_info: @web_auth.additional_info, code: code, nonce: nonce)
66
+ end
42
67
  end
43
68
 
44
69
  # start a local web server
45
70
  # then start a browser that will callback the local server upon authentication
46
71
  # store the final query
47
72
  class Server < WebServerSimple
48
- attr_reader :additional_info, :expected_path
73
+ # @return [Array<String>, nil] Keys of the query that the page gives to the user as a code to paste in a terminal, `nil` for the standard page
74
+ attr_reader :additional_info, :expected_path, :reply_keys
49
75
 
50
76
  # @param endpoint_url [String] e.g. 'https://127.0.0.1:12345'
51
77
  # @param additional_info [String] Information in web page
52
- def initialize(endpoint_url, additional_info = nil)
78
+ # @param reply_keys [Array<String>, nil] If set, the page shows to the user a code with these keys of the query, to paste in the terminal of `ascli` that waits for it
79
+ def initialize(endpoint_url, additional_info = nil, reply_keys: nil)
53
80
  uri = URI.parse(endpoint_url)
54
81
  super(uri)
82
+ @reply_keys = reply_keys
55
83
  @mutex = Mutex.new
56
84
  @cond = ConditionVariable.new
57
85
  @expected_path = uri.path.empty? ? '/' : uri.path
@@ -60,7 +88,16 @@ module Aspera
60
88
  # last argument (self) is provided to constructor of servlet
61
89
  mount(@expected_path, Servlet, self)
62
90
  # server runs in thread
63
- Thread.new { start }
91
+ @thread = Thread.new { start }
92
+ end
93
+
94
+ # Wait until the server thread has ended: the response of the last request (page shown to the user) is sent.
95
+ # To call after `received_request`, when the process does not continue with a long operation.
96
+ # @param timeout [Integer] Maximum time to wait, in seconds
97
+ # @return [nil]
98
+ def wait_stopped(timeout = 10)
99
+ @thread.join(timeout)
100
+ nil
64
101
  end
65
102
 
66
103
  # Called by web server thread on received request
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # SPDX-FileCopyrightText: IBM Corp.
4
+ # SPDX-License-Identifier: Apache-2.0
5
+
3
6
  require 'erb'
4
7
 
5
8
  module Aspera
@@ -41,10 +44,12 @@ module Aspera
41
44
 
42
45
  class << self
43
46
  # Set the headers of an HTML page: type and security headers
44
- # @param response [WEBrick::HTTPResponse]
47
+ # @param response [WEBrick::HTTPResponse]
48
+ # @param content_security_policy [String, nil] Policy that replaces the default one, e.g. to allow a script of the page
45
49
  # @return [nil]
46
- def secure_headers(response)
50
+ def secure_headers(response, content_security_policy: nil)
47
51
  HEADERS.each { |name, value| response[name] = value }
52
+ response['Content-Security-Policy'] = content_security_policy unless content_security_policy.nil?
48
53
  nil
49
54
  end
50
55
 
@@ -1,11 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # SPDX-FileCopyrightText: IBM Corp.
4
+ # SPDX-License-Identifier: Apache-2.0
5
+
6
+ require_relative 'log'
7
+ require_relative 'assert'
8
+ require_relative 'hash_ext'
3
9
  require 'delegate'
4
10
  require 'ipaddr'
5
11
  require 'webrick'
6
- require 'aspera/log'
7
- require 'aspera/assert'
8
- require 'aspera/hash_ext'
9
12
 
10
13
  module Aspera
11
14
  # Simple WEBrick server with HTTPS support
data/lib/aspera/yaml.rb CHANGED
@@ -1,6 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'aspera/assert'
3
+ # SPDX-FileCopyrightText: IBM Corp.
4
+ # SPDX-License-Identifier: Apache-2.0
5
+
6
+ require_relative 'assert'
7
+ require 'date'
4
8
  require 'yaml'
5
9
 
6
10
  module Aspera