@voxgig/sdkgen 4.48.1 → 4.49.1
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.
- package/bin/voxgig-sdkgen +1 -1
- package/package.json +1 -1
- package/project/.sdk/model/feature/secrets.aontu +3 -0
- package/project/.sdk/src/cmp/c/Config_c.ts +6 -1
- package/project/.sdk/src/cmp/c/fragment/Entity.fragment.c +0 -10
- package/project/.sdk/src/cmp/clojure/fragment/Entity.fragment.clj +0 -9
- package/project/.sdk/src/cmp/csharp/fragment/EntityBase.fragment.cs +0 -11
- package/project/.sdk/src/cmp/go/fragment/Entity.fragment.go +0 -7
- package/project/.sdk/src/cmp/java/fragment/EntityBase.fragment.java +0 -10
- package/project/.sdk/src/cmp/js/fragment/EntityBase.fragment.js +2 -11
- package/project/.sdk/src/cmp/kotlin/fragment/EntityBase.fragment.kt +0 -10
- package/project/.sdk/src/cmp/lua/fragment/Entity.fragment.lua +0 -11
- package/project/.sdk/src/cmp/ocaml/Entity_ocaml.ts +2 -10
- package/project/.sdk/src/cmp/perl/fragment/Entity.fragment.pm +0 -13
- package/project/.sdk/src/cmp/php/fragment/Entity.fragment.php +0 -10
- package/project/.sdk/src/cmp/py/fragment/Entity.fragment.py +0 -10
- package/project/.sdk/src/cmp/rb/fragment/Entity.fragment.rb +0 -10
- package/project/.sdk/src/cmp/rust/fragment/Entity.fragment.rs +0 -12
- package/project/.sdk/src/cmp/scala/fragment/EntityBase.fragment.scala +0 -10
- package/project/.sdk/src/cmp/swift/fragment/EntityBase.fragment.swift +0 -9
- package/project/.sdk/src/cmp/ts/fragment/EntityBase.fragment.ts +0 -7
- package/project/.sdk/src/cmp/zig/fragment/Entity.fragment.zig +1 -14
- package/project/.sdk/tm/c/feature/cache.c +16 -1
- package/project/.sdk/tm/c/feature/secrets.c +149 -18
- package/project/.sdk/tm/c/tests/feature/secrets/secrets_test.c +319 -70
- package/project/.sdk/tm/c/tests/feature_test.c +31 -0
- package/project/.sdk/tm/clojure/feature/secrets/sdk/feature/secrets.clj +74 -29
- package/project/.sdk/tm/clojure/src/sdk/core.clj +55 -6
- package/project/.sdk/tm/clojure/src/sdk/features.clj +16 -2
- package/project/.sdk/tm/clojure/test/sdk/test/feature/secrets.clj +191 -48
- package/project/.sdk/tm/clojure/test/sdk/test/feature.clj +16 -0
- package/project/.sdk/tm/cpp/core/types.hpp +1 -11
- package/project/.sdk/tm/cpp/feature/cache.hpp +14 -1
- package/project/.sdk/tm/cpp/feature/secrets.hpp +133 -18
- package/project/.sdk/tm/cpp/test/feature/secrets/secrets_test.cpp +175 -8
- package/project/.sdk/tm/cpp/test/feature_test.cpp +27 -0
- package/project/.sdk/tm/csharp/core/Control.cs +0 -5
- package/project/.sdk/tm/csharp/feature/CacheFeature.cs +16 -0
- package/project/.sdk/tm/csharp/feature/SecretsFeature.cs +77 -38
- package/project/.sdk/tm/csharp/test/FeatureTest.cs +29 -0
- package/project/.sdk/tm/csharp/test/feature/secrets/SecretsFeatureTest.cs +315 -68
- package/project/.sdk/tm/elixir/lib/projectname/entity_base.ex +0 -13
- package/project/.sdk/tm/elixir/lib/projectname/feature/cache.ex +32 -10
- package/project/.sdk/tm/elixir/lib/projectname/feature/secrets.ex +78 -22
- package/project/.sdk/tm/elixir/test/feature/secrets/secrets_test.exs +178 -19
- package/project/.sdk/tm/elixir/test/feature_test.exs +20 -0
- package/project/.sdk/tm/go/feature/cache_feature.go +14 -1
- package/project/.sdk/tm/go/feature/secrets_feature.go +110 -15
- package/project/.sdk/tm/go/test/feature/secrets/secrets_feature_test.go +373 -87
- package/project/.sdk/tm/go/test/feature_test.go +32 -0
- package/project/.sdk/tm/go/utility/media.go +2 -2
- package/project/.sdk/tm/java/core/Control.java +0 -5
- package/project/.sdk/tm/java/feature/CacheFeature.java +12 -0
- package/project/.sdk/tm/java/feature/SecretsFeature.java +104 -34
- package/project/.sdk/tm/java/test/FeatureTest.java +25 -0
- package/project/.sdk/tm/java/test/PrimaryUtilityTest.java +39 -0
- package/project/.sdk/tm/java/test/feature/secrets/SecretsFeatureTest.java +267 -53
- package/project/.sdk/tm/java/utility/Fetcher.java +18 -2
- package/project/.sdk/tm/java/utility/Media.java +2 -2
- package/project/.sdk/tm/js/src/feature/cache/CacheFeature.js +13 -0
- package/project/.sdk/tm/js/src/feature/secrets/SecretsFeature.js +79 -29
- package/project/.sdk/tm/js/test/feature/secrets/Secrets.test.js +282 -93
- package/project/.sdk/tm/js/test/feature.test.js +17 -0
- package/project/.sdk/tm/kotlin/core/Control.kt +0 -5
- package/project/.sdk/tm/kotlin/feature/CacheFeature.kt +12 -0
- package/project/.sdk/tm/kotlin/feature/SecretsFeature.kt +84 -24
- package/project/.sdk/tm/kotlin/test/FeatureTest.kt +24 -0
- package/project/.sdk/tm/kotlin/test/PrimaryUtilityTest.kt +35 -0
- package/project/.sdk/tm/kotlin/test/feature/secrets/SecretsTest.kt +236 -54
- package/project/.sdk/tm/kotlin/utility/Fetcher.kt +13 -1
- package/project/.sdk/tm/lua/feature/cache_feature.lua +20 -0
- package/project/.sdk/tm/lua/feature/secrets_feature.lua +92 -15
- package/project/.sdk/tm/lua/test/feature/secrets/secrets_feature_test.lua +208 -43
- package/project/.sdk/tm/lua/test/feature_test.lua +18 -0
- package/project/.sdk/tm/lua/utility/clean.lua +1 -0
- package/project/.sdk/tm/ocaml/feature/secrets_feature.ml +100 -14
- package/project/.sdk/tm/ocaml/sdk_features.ml +7 -1
- package/project/.sdk/tm/ocaml/test/feature/secrets/t_secrets.ml +143 -7
- package/project/.sdk/tm/ocaml/test/t_feature.ml +15 -0
- package/project/.sdk/tm/perl/feature/cache_feature.pm +13 -0
- package/project/.sdk/tm/perl/feature/secrets_feature.pm +87 -32
- package/project/.sdk/tm/perl/t/feature/secrets/secrets.t +177 -24
- package/project/.sdk/tm/perl/t/feature.t +24 -0
- package/project/.sdk/tm/php/feature/CacheFeature.php +13 -0
- package/project/.sdk/tm/php/feature/SecretsFeature.php +79 -23
- package/project/.sdk/tm/php/test/FeatureTest.php +23 -0
- package/project/.sdk/tm/php/test/feature/secrets/SecretsTest.php +258 -48
- package/project/.sdk/tm/py/pkg/feature/cache_feature.py +7 -1
- package/project/.sdk/tm/py/pkg/feature/secrets_feature.py +90 -29
- package/project/.sdk/tm/py/test/feature/secrets/test_secrets.py +313 -80
- package/project/.sdk/tm/py/test/test_feature.py +18 -0
- package/project/.sdk/tm/rb/feature/cache_feature.rb +12 -0
- package/project/.sdk/tm/rb/feature/secrets_feature.rb +68 -21
- package/project/.sdk/tm/rb/test/feature/secrets/secrets_feature_test.rb +214 -43
- package/project/.sdk/tm/rb/test/feature_test.rb +20 -0
- package/project/.sdk/tm/rust/feature/cache.rs +15 -1
- package/project/.sdk/tm/rust/feature/secrets.rs +103 -23
- package/project/.sdk/tm/rust/tests/feature/secrets/main.rs +357 -67
- package/project/.sdk/tm/rust/tests/feature_test.rs +41 -0
- package/project/.sdk/tm/scala/core/Control.scala +0 -5
- package/project/.sdk/tm/scala/feature/CacheFeature.scala +12 -0
- package/project/.sdk/tm/scala/feature/SecretsFeature.scala +88 -22
- package/project/.sdk/tm/scala/sdktest/SdkTestMain.scala +61 -1
- package/project/.sdk/tm/scala/sdktest/feature/secrets/SecretsTestMain.scala +230 -73
- package/project/.sdk/tm/scala/utility/Fetcher.scala +13 -1
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/core/Control.swift +0 -5
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/CacheFeature.swift +11 -0
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/SecretsFeature.swift +76 -16
- package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/FeatureTest.swift +21 -0
- package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/feature/secrets/SecretsFeatureTest.swift +257 -73
- package/project/.sdk/tm/ts/src/feature/cache/CacheFeature.ts +13 -0
- package/project/.sdk/tm/ts/src/feature/secrets/SecretsFeature.ts +77 -17
- package/project/.sdk/tm/ts/test/feature/secrets/Secrets.test.ts +282 -87
- package/project/.sdk/tm/ts/test/feature.test.ts +17 -0
- package/project/.sdk/tm/zig/feature/cache.zig +15 -1
- package/project/.sdk/tm/zig/feature/secrets.zig +109 -32
- package/project/.sdk/tm/zig/test/feature/secrets/secrets_test.zig +232 -9
- package/project/.sdk/tm/zig/test/feature_test.zig +33 -0
- package/project/sdkgen-package.json +1 -1
|
@@ -55,6 +55,23 @@ require_relative '../utility/struct/voxgig_struct'
|
|
|
55
55
|
require_relative 'secrets/voxgig_sekreto'
|
|
56
56
|
|
|
57
57
|
class ProjectNameSecretsFeature < ProjectNameBaseFeature
|
|
58
|
+
REDIRECT_MAX = 5
|
|
59
|
+
|
|
60
|
+
# Where a method-keeping redirect points, when that is on the exchange's
|
|
61
|
+
# own origin. Any other redirect would drop the body or leave the origin.
|
|
62
|
+
def self.moved(url, at, res)
|
|
63
|
+
return nil unless res.is_a?(Hash) && [307, 308].include?(res["status"].to_i)
|
|
64
|
+
headers = res["headers"]
|
|
65
|
+
location = headers.is_a?(Hash) ? headers["location"] : nil
|
|
66
|
+
return nil unless location.is_a?(String) && !location.empty?
|
|
67
|
+
nxt = URI.join(at, location)
|
|
68
|
+
here = URI.parse(url)
|
|
69
|
+
origin = ->(u) { [u.scheme.to_s.downcase, u.host.to_s.downcase, u.port] }
|
|
70
|
+
origin.call(nxt) == origin.call(here) ? nxt.to_s : nil
|
|
71
|
+
rescue URI::Error, ArgumentError
|
|
72
|
+
nil
|
|
73
|
+
end
|
|
74
|
+
|
|
58
75
|
def initialize
|
|
59
76
|
super
|
|
60
77
|
@version = "0.1.0"
|
|
@@ -65,6 +82,8 @@ class ProjectNameSecretsFeature < ProjectNameBaseFeature
|
|
|
65
82
|
@client = nil
|
|
66
83
|
@options = {}
|
|
67
84
|
@liveopts = {}
|
|
85
|
+
@declared = false
|
|
86
|
+
@basic = false
|
|
68
87
|
|
|
69
88
|
@secretname = "apikey"
|
|
70
89
|
@cache = true
|
|
@@ -102,6 +121,12 @@ class ProjectNameSecretsFeature < ProjectNameBaseFeature
|
|
|
102
121
|
@liveopts = ctx.options.is_a?(Hash) ? ctx.options : {}
|
|
103
122
|
@active = @options["active"] == true
|
|
104
123
|
|
|
124
|
+
# What prepare_auth was generated from: the config declares auth only
|
|
125
|
+
# where it places a credential, and Basic only where it composes one.
|
|
126
|
+
declared = VoxgigStruct.getpath(ctx.config, "options.auth")
|
|
127
|
+
@declared = declared.is_a?(Hash)
|
|
128
|
+
@basic = @declared && declared["basic"] == true
|
|
129
|
+
|
|
105
130
|
return unless @active
|
|
106
131
|
|
|
107
132
|
@secretname = _str(@options["name"], "apikey")
|
|
@@ -127,6 +152,7 @@ class ProjectNameSecretsFeature < ProjectNameBaseFeature
|
|
|
127
152
|
"response" => _str(xopts["response"], "access_token"),
|
|
128
153
|
"statuses" => statuses,
|
|
129
154
|
"retries" => xopts["retries"].is_a?(Numeric) ? xopts["retries"].to_i : 1,
|
|
155
|
+
"redirect" => "same-origin" == xopts["redirect"] ? "same-origin" : "refuse",
|
|
130
156
|
}
|
|
131
157
|
end
|
|
132
158
|
|
|
@@ -261,6 +287,12 @@ class ProjectNameSecretsFeature < ProjectNameBaseFeature
|
|
|
261
287
|
|
|
262
288
|
private
|
|
263
289
|
|
|
290
|
+
# prepare_auth's decision: auth the config declares, not suppressed by
|
|
291
|
+
# `auth: nil`.
|
|
292
|
+
def _authon
|
|
293
|
+
@declared && !@liveopts["auth"].nil?
|
|
294
|
+
end
|
|
295
|
+
|
|
264
296
|
# Every value this feature resolves or buys is a secret the SDK handles,
|
|
265
297
|
# and none arrives under an option key the intake registration saw.
|
|
266
298
|
def _register(value)
|
|
@@ -274,7 +306,7 @@ class ProjectNameSecretsFeature < ProjectNameBaseFeature
|
|
|
274
306
|
# boolean is the whole of what resolve needs to tell a cacheable HIT from
|
|
275
307
|
# a miss it must not keep.
|
|
276
308
|
def _resolve_once
|
|
277
|
-
return false if @sekreto.nil?
|
|
309
|
+
return false if @sekreto.nil? || !_authon
|
|
278
310
|
|
|
279
311
|
# Miss-vs-error: `try` answers nil for "no store has it" and RAISES for
|
|
280
312
|
# "a store could not answer" - only the miss falls through.
|
|
@@ -311,15 +343,6 @@ class ProjectNameSecretsFeature < ProjectNameBaseFeature
|
|
|
311
343
|
# is the same path expiry takes anyway.
|
|
312
344
|
return true unless apikey.nil? || apikey.empty?
|
|
313
345
|
|
|
314
|
-
# `auth: nil` is the documented way to send NO credential, and a
|
|
315
|
-
# purchase is a credential-bearing call: the refresh token goes to the
|
|
316
|
-
# token endpoint in the request body. _with_refresh honours suppression
|
|
317
|
-
# for the RETRY, but it runs after this - by then the refresh token has
|
|
318
|
-
# already left the process, and no later check can call it back. The
|
|
319
|
-
# suppression has to be honoured here, before the first purchase, or it
|
|
320
|
-
# only ever half-held.
|
|
321
|
-
return false if @liveopts["auth"].nil?
|
|
322
|
-
|
|
323
346
|
_buy
|
|
324
347
|
|
|
325
348
|
true
|
|
@@ -332,12 +355,9 @@ class ProjectNameSecretsFeature < ProjectNameBaseFeature
|
|
|
332
355
|
# because the header was built by the synchronous prepare_auth before
|
|
333
356
|
# this request left, and it carries the token that just failed.
|
|
334
357
|
def _with_refresh(ctx, url, fetchdef, inner)
|
|
335
|
-
#
|
|
336
|
-
#
|
|
337
|
-
|
|
338
|
-
# cannot be fixed by buying one - retrying would transmit exactly the
|
|
339
|
-
# credential the caller suppressed.
|
|
340
|
-
return inner.call(ctx, url, fetchdef) if @liveopts["auth"].nil?
|
|
358
|
+
# A refusal of an unauthenticated request is not an expired token, and
|
|
359
|
+
# retrying would transmit the credential that auth off withholds.
|
|
360
|
+
return inner.call(ctx, url, fetchdef) unless _authon
|
|
341
361
|
|
|
342
362
|
max = @exchange["retries"]
|
|
343
363
|
attempt = 0
|
|
@@ -449,13 +469,26 @@ class ProjectNameSecretsFeature < ProjectNameBaseFeature
|
|
|
449
469
|
# feature wraps, and sending the token request back through it would
|
|
450
470
|
# recurse on the first expiry - and would route the exchange through
|
|
451
471
|
# the test mock, which knows nothing about it.
|
|
452
|
-
|
|
472
|
+
fetchdef = {
|
|
453
473
|
"method" => x["method"],
|
|
454
474
|
"headers" => { "content-type" => "application/json" },
|
|
455
475
|
"body" => JSON.generate({ x["request"] => @refresh }),
|
|
456
|
-
# Never followed: a redirect would carry the refresh token on.
|
|
476
|
+
# Never followed by the client: a redirect would carry the refresh token on.
|
|
457
477
|
"redirect" => "manual",
|
|
458
|
-
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
res, err = fetch.call(url, fetchdef)
|
|
481
|
+
|
|
482
|
+
# By option alone, and only while the exchange stays on its own origin.
|
|
483
|
+
at = url
|
|
484
|
+
hops = 0
|
|
485
|
+
while err.nil? && "same-origin" == x["redirect"] && hops < REDIRECT_MAX
|
|
486
|
+
nxt = ProjectNameSecretsFeature.moved(url, at, res)
|
|
487
|
+
break if nxt.nil?
|
|
488
|
+
at = nxt
|
|
489
|
+
hops += 1
|
|
490
|
+
res, err = fetch.call(at, fetchdef)
|
|
491
|
+
end
|
|
459
492
|
|
|
460
493
|
unless err.nil?
|
|
461
494
|
raise VoxgigSekreto::SekretoError,
|
|
@@ -525,16 +558,26 @@ class ProjectNameSecretsFeature < ProjectNameBaseFeature
|
|
|
525
558
|
name = name.is_a?(String) && !name.empty? ? name.downcase : "authorization"
|
|
526
559
|
|
|
527
560
|
# Suppressed auth means NO header, the same answer prepare_auth gives.
|
|
528
|
-
if
|
|
561
|
+
if !_authon || token.nil? || token.empty?
|
|
529
562
|
headers.delete(name)
|
|
530
563
|
return
|
|
531
564
|
end
|
|
532
565
|
|
|
566
|
+
value = @basic && VoxgigStruct.getpath(@liveopts, "auth.basic") == true ? _pair(token) : token
|
|
567
|
+
|
|
533
568
|
prefix = VoxgigStruct.getpath(@liveopts, "auth.prefix")
|
|
534
569
|
prefix = "" unless prefix.is_a?(String)
|
|
535
570
|
|
|
536
571
|
# Empty prefix (raw apiKey credential) must not add a leading space.
|
|
537
|
-
headers[name] = prefix.empty? ?
|
|
572
|
+
headers[name] = prefix.empty? ? value : "#{prefix} #{value}"
|
|
573
|
+
end
|
|
574
|
+
|
|
575
|
+
# Under Basic the token is the key half, as apikey is: prepare_auth's pair.
|
|
576
|
+
def _pair(token)
|
|
577
|
+
secret = @liveopts["secret"]
|
|
578
|
+
pair = ["#{token}:#{secret.is_a?(String) ? secret : ""}"].pack("m0")
|
|
579
|
+
_register(pair)
|
|
580
|
+
pair
|
|
538
581
|
end
|
|
539
582
|
|
|
540
583
|
# The plugin DEFINITIONS the model selected for this feature, emitted by
|
|
@@ -591,8 +634,12 @@ class ProjectNameSecretsFeature < ProjectNameBaseFeature
|
|
|
591
634
|
# A non-2xx answer IS a response: hand back its status so the caller
|
|
592
635
|
# reports "token exchange failed: <status>" rather than a transport
|
|
593
636
|
# error.
|
|
637
|
+
resp_headers = {}
|
|
638
|
+
resp.each_header { |k, v| resp_headers[k.downcase] = v }
|
|
639
|
+
|
|
594
640
|
return {
|
|
595
641
|
"status" => resp.code.to_i,
|
|
642
|
+
"headers" => resp_headers,
|
|
596
643
|
"json" => -> {
|
|
597
644
|
begin
|
|
598
645
|
raw.nil? || raw.empty? ? nil : JSON.parse(raw)
|
|
@@ -43,6 +43,16 @@ class SecretsFeatureTest < Minitest::Test
|
|
|
43
43
|
name.is_a?(String) && !name.empty? ? name.downcase : "authorization"
|
|
44
44
|
end
|
|
45
45
|
|
|
46
|
+
# The form prepare_auth writes, from the DEFAULT client's auth: under Basic
|
|
47
|
+
# the token is the key half of the encoded pair, and a prefix is
|
|
48
|
+
# space-joined.
|
|
49
|
+
CREDFORM = begin
|
|
50
|
+
auth = ProjectNameSDK.test(nil, nil).options_map["auth"]
|
|
51
|
+
auth = {} unless auth.is_a?(Hash)
|
|
52
|
+
prefix = auth["prefix"].is_a?(String) ? auth["prefix"] : ""
|
|
53
|
+
{ "prefix" => prefix, "basic" => auth["basic"] == true }
|
|
54
|
+
end
|
|
55
|
+
|
|
46
56
|
# ---------------------------------------------------------------------
|
|
47
57
|
# The recording transport: system.fetch for a LIVE client, scripting one
|
|
48
58
|
# status per API call (the last repeating) and a token endpoint for the
|
|
@@ -75,6 +85,7 @@ class SecretsFeatureTest < Minitest::Test
|
|
|
75
85
|
"url" => fullurl,
|
|
76
86
|
"auth" => headers[CREDHEADER],
|
|
77
87
|
"has" => headers.key?(CREDHEADER),
|
|
88
|
+
"values" => headers.values.map(&:to_s).join("\n"),
|
|
78
89
|
"body" => fetchdef["body"],
|
|
79
90
|
"redirect" => fetchdef["redirect"],
|
|
80
91
|
}
|
|
@@ -217,12 +228,15 @@ class SecretsFeatureTest < Minitest::Test
|
|
|
217
228
|
# -------------------------------------------------------------------
|
|
218
229
|
# Support.
|
|
219
230
|
|
|
220
|
-
# The
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
231
|
+
# The header value prepare_auth writes for a token.
|
|
232
|
+
def credential(token, secret = "")
|
|
233
|
+
value = CREDFORM["basic"] ? ["#{token}:#{secret}"].pack("m0") : token
|
|
234
|
+
CREDFORM["prefix"].empty? ? value : "#{CREDFORM["prefix"]} #{value}"
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
def assert_credential(header, token, secret = "")
|
|
238
|
+
assert_equal credential(token, secret), header,
|
|
239
|
+
"expected the #{CREDHEADER} header to carry #{token}"
|
|
226
240
|
end
|
|
227
241
|
|
|
228
242
|
def secrets_feature_of(client)
|
|
@@ -351,7 +365,9 @@ class SecretsFeatureTest < Minitest::Test
|
|
|
351
365
|
|
|
352
366
|
# Read from the config, never from a probe: an SDK whose config declares no
|
|
353
367
|
# auth places no credential, and these tests assert on the one it places.
|
|
368
|
+
# The auth-off test asserts it places none.
|
|
354
369
|
def setup
|
|
370
|
+
return if name.start_with?("test_auth_off_")
|
|
355
371
|
unless ProjectNameConfig.shared_config["options"].key?("auth")
|
|
356
372
|
skip("this SDK's config declares no auth, so it places no credential")
|
|
357
373
|
end
|
|
@@ -417,6 +433,17 @@ class SecretsFeatureTest < Minitest::Test
|
|
|
417
433
|
clearenv("APIKEY")
|
|
418
434
|
end
|
|
419
435
|
|
|
436
|
+
def test_a_resolved_key_is_placed_as_prepare_auth_places_it_the_secret_kept
|
|
437
|
+
setenv("APIKEY", "ENVKEY04")
|
|
438
|
+
wire = Wire.new
|
|
439
|
+
client = secrets_client(wire, { "secret" => "PASS01", "feature" => secrets_opts })
|
|
440
|
+
|
|
441
|
+
drive_op(client, wire)
|
|
442
|
+
assert_credential(wire.api[0]["auth"], "ENVKEY04", "PASS01")
|
|
443
|
+
ensure
|
|
444
|
+
clearenv("APIKEY")
|
|
445
|
+
end
|
|
446
|
+
|
|
420
447
|
def test_custom_provider_objects_are_accepted_verbatim
|
|
421
448
|
asked = []
|
|
422
449
|
wire = Wire.new
|
|
@@ -780,6 +807,29 @@ class SecretsFeatureTest < Minitest::Test
|
|
|
780
807
|
clearenv("REFRESH_TOKEN")
|
|
781
808
|
end
|
|
782
809
|
|
|
810
|
+
def test_a_token_bought_on_a_refusal_is_placed_as_prepare_auth_places_it
|
|
811
|
+
setenv("REFRESH_TOKEN", "REFRESH01")
|
|
812
|
+
wire = Wire.new
|
|
813
|
+
wire.apistatus = [401, 200]
|
|
814
|
+
|
|
815
|
+
client = secrets_client(wire, {
|
|
816
|
+
"secret" => "PASS01",
|
|
817
|
+
"feature" => secrets_opts({
|
|
818
|
+
"name" => "refresh_token",
|
|
819
|
+
"exchange" => { "active" => true },
|
|
820
|
+
}),
|
|
821
|
+
})
|
|
822
|
+
|
|
823
|
+
drive_op(client, wire)
|
|
824
|
+
|
|
825
|
+
api = wire.api
|
|
826
|
+
assert_equal 2, api.length, "expected the request to be retried exactly once"
|
|
827
|
+
assert_credential(api[0]["auth"], "ACCESS01", "PASS01")
|
|
828
|
+
assert_credential(api[1]["auth"], "ACCESS02", "PASS01")
|
|
829
|
+
ensure
|
|
830
|
+
clearenv("REFRESH_TOKEN")
|
|
831
|
+
end
|
|
832
|
+
|
|
783
833
|
# `auth: nil` SUPPRESSES THE PURCHASE, not just the retry.
|
|
784
834
|
#
|
|
785
835
|
# resolve runs before _with_refresh's suppression check, so the refresh
|
|
@@ -925,12 +975,12 @@ class SecretsFeatureTest < Minitest::Test
|
|
|
925
975
|
assert_credential(server.auth, "RAWTOK01")
|
|
926
976
|
end
|
|
927
977
|
|
|
928
|
-
# Real listeners and the raw fallback: a stub follows nothing.
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
answer = [307, ""]
|
|
978
|
+
# Real listeners and the raw fallback: a stub follows nothing. The
|
|
979
|
+
# origin answers the exchange as o["answer"] says and records what else it gets.
|
|
980
|
+
def with_origins
|
|
981
|
+
o = { "elsewhere" => [], "followed" => [], "exchanged" => 0, "answer" => [307, ""] }
|
|
933
982
|
lock = Mutex.new
|
|
983
|
+
o["lock"] = lock
|
|
934
984
|
|
|
935
985
|
listen = lambda do |&handle|
|
|
936
986
|
server = TCPServer.new("127.0.0.1", 0)
|
|
@@ -964,50 +1014,74 @@ class SecretsFeatureTest < Minitest::Test
|
|
|
964
1014
|
end
|
|
965
1015
|
|
|
966
1016
|
other, otherthread = listen.call do |method, path, body|
|
|
967
|
-
lock.synchronize { elsewhere << { "method" => method, "path" => path, "body" => body } }
|
|
1017
|
+
lock.synchronize { o["elsewhere"] << { "method" => method, "path" => path, "body" => body } }
|
|
968
1018
|
ok.call('{"access_token":"ELSEWHERE01"}')
|
|
969
1019
|
end
|
|
970
|
-
origin, originthread = listen.call do |method, path,
|
|
1020
|
+
origin, originthread = listen.call do |method, path, body|
|
|
971
1021
|
if "/api/auth/token" == path
|
|
972
|
-
status, location = lock.synchronize
|
|
1022
|
+
status, location = lock.synchronize do
|
|
1023
|
+
o["exchanged"] += 1
|
|
1024
|
+
o["answer"]
|
|
1025
|
+
end
|
|
973
1026
|
"HTTP/1.1 #{status} Redirect\r\nlocation: #{location}\r\n" \
|
|
974
1027
|
"content-length: 0\r\nconnection: close\r\n\r\n"
|
|
975
1028
|
else
|
|
976
|
-
lock.synchronize { followed << "#{method} #{path}" }
|
|
1029
|
+
lock.synchronize { o["followed"] << "#{method} #{path} #{body}".strip }
|
|
977
1030
|
ok.call('{"access_token":"MOVED01"}')
|
|
978
1031
|
end
|
|
979
1032
|
end
|
|
980
1033
|
|
|
981
|
-
|
|
982
|
-
|
|
1034
|
+
o["origin"] = "http://127.0.0.1:#{origin.addr[1]}"
|
|
1035
|
+
o["base"] = o["origin"] + "/api"
|
|
1036
|
+
o["away"] = "http://127.0.0.1:#{other.addr[1]}/api/auth/token"
|
|
983
1037
|
|
|
984
1038
|
begin
|
|
1039
|
+
yield o
|
|
1040
|
+
ensure
|
|
1041
|
+
origin.close rescue nil
|
|
1042
|
+
other.close rescue nil
|
|
1043
|
+
originthread.join
|
|
1044
|
+
otherthread.join
|
|
1045
|
+
end
|
|
1046
|
+
end
|
|
1047
|
+
|
|
1048
|
+
def redirect_client(o, exchange = {})
|
|
1049
|
+
with_secrets do |extend_it|
|
|
1050
|
+
opts = {
|
|
1051
|
+
"base" => o["base"],
|
|
1052
|
+
"allow" => { "op" => "direct" },
|
|
1053
|
+
"feature" => { "secrets" => {
|
|
1054
|
+
"active" => true,
|
|
1055
|
+
"name" => "refresh_token",
|
|
1056
|
+
"exchange" => { "active" => true, "refresh" => "REFRESH01" }.merge(exchange),
|
|
1057
|
+
} },
|
|
1058
|
+
}
|
|
1059
|
+
opts["extend"] = [ProjectNameSecretsFeature.new] if extend_it
|
|
1060
|
+
ProjectNameSDK.new(opts)
|
|
1061
|
+
end
|
|
1062
|
+
end
|
|
1063
|
+
|
|
1064
|
+
def redirect_probe(client)
|
|
1065
|
+
res = client.direct({ "path" => "/probe" })
|
|
1066
|
+
[res, res.is_a?(Hash) && !res["err"].nil? ? res["err"].to_s : res.inspect]
|
|
1067
|
+
rescue StandardError => e
|
|
1068
|
+
[nil, e.message]
|
|
1069
|
+
end
|
|
1070
|
+
|
|
1071
|
+
def test_a_redirect_answering_the_exchange_is_a_failure_and_nothing_follows_it
|
|
1072
|
+
with_origins do |o|
|
|
1073
|
+
base = o["base"]
|
|
1074
|
+
away = o["away"]
|
|
1075
|
+
elsewhere = o["elsewhere"]
|
|
1076
|
+
followed = o["followed"]
|
|
1077
|
+
|
|
985
1078
|
answers = [[307, away], [308, away], [302, away], [307, "/api/moved/token"]]
|
|
986
1079
|
allowed = []
|
|
987
1080
|
refused = 0
|
|
988
1081
|
|
|
989
1082
|
answers.each do |a|
|
|
990
|
-
lock.synchronize { answer = a }
|
|
991
|
-
|
|
992
|
-
opts = {
|
|
993
|
-
"base" => base,
|
|
994
|
-
"allow" => { "op" => "direct" },
|
|
995
|
-
"feature" => { "secrets" => {
|
|
996
|
-
"active" => true,
|
|
997
|
-
"name" => "refresh_token",
|
|
998
|
-
"exchange" => { "active" => true, "refresh" => "REFRESH01" },
|
|
999
|
-
} },
|
|
1000
|
-
}
|
|
1001
|
-
opts["extend"] = [ProjectNameSecretsFeature.new] if extend_it
|
|
1002
|
-
ProjectNameSDK.new(opts)
|
|
1003
|
-
end
|
|
1004
|
-
|
|
1005
|
-
message = begin
|
|
1006
|
-
res = client.direct({ "path" => "/probe" })
|
|
1007
|
-
res.is_a?(Hash) && !res["err"].nil? ? res["err"].to_s : res.inspect
|
|
1008
|
-
rescue StandardError => e
|
|
1009
|
-
e.message
|
|
1010
|
-
end
|
|
1083
|
+
o["lock"].synchronize { o["answer"] = a }
|
|
1084
|
+
_res, message = redirect_probe(redirect_client(o))
|
|
1011
1085
|
|
|
1012
1086
|
if message.include?("token exchange failed: #{a[0]} from #{base}/auth/token")
|
|
1013
1087
|
refused += 1
|
|
@@ -1023,11 +1097,65 @@ class SecretsFeatureTest < Minitest::Test
|
|
|
1023
1097
|
assert_equal [], followed, "a request followed a refused exchange"
|
|
1024
1098
|
assert_equal [], allowed, "a redirect was not a failed exchange"
|
|
1025
1099
|
assert_equal answers.length, refused
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1100
|
+
end
|
|
1101
|
+
end
|
|
1102
|
+
|
|
1103
|
+
def test_with_the_option_a_redirect_on_the_exchanges_own_origin_is_followed_and_no_other
|
|
1104
|
+
with_origins do |o|
|
|
1105
|
+
sent = ' {"refresh_token":"REFRESH01"}'
|
|
1106
|
+
# Each case: the status, the Location, and the path the exchange is
|
|
1107
|
+
# asked again at, or nil where the redirect is refused.
|
|
1108
|
+
cases = [
|
|
1109
|
+
[307, "/api/moved/token", "/api/moved/token"],
|
|
1110
|
+
[308, o["origin"] + "/api/moved/token", "/api/moved/token"],
|
|
1111
|
+
# A Location of a query alone keeps the path that answered.
|
|
1112
|
+
[307, "?step=2", "/api/auth/token?step=2"],
|
|
1113
|
+
[307, o["away"], nil],
|
|
1114
|
+
[302, "/api/moved/token", nil],
|
|
1115
|
+
[307, "/api/auth/token", nil],
|
|
1116
|
+
]
|
|
1117
|
+
|
|
1118
|
+
wrong = []
|
|
1119
|
+
followed = 0
|
|
1120
|
+
refused = 0
|
|
1121
|
+
looped = 0
|
|
1122
|
+
|
|
1123
|
+
cases.each do |status, location, path|
|
|
1124
|
+
o["lock"].synchronize do
|
|
1125
|
+
o["answer"] = [status, location]
|
|
1126
|
+
o["followed"].clear
|
|
1127
|
+
o["exchanged"] = 0
|
|
1128
|
+
end
|
|
1129
|
+
|
|
1130
|
+
res, message = redirect_probe(redirect_client(o, { "redirect" => "same-origin" }))
|
|
1131
|
+
seen = "#{status} to #{location}: #{message} #{o['followed'].inspect}"
|
|
1132
|
+
|
|
1133
|
+
if path
|
|
1134
|
+
if res.is_a?(Hash) && true == res["ok"] &&
|
|
1135
|
+
"POST #{path}#{sent}" == o["followed"][0] && 2 == o["followed"].length
|
|
1136
|
+
followed += 1
|
|
1137
|
+
else
|
|
1138
|
+
wrong << seen
|
|
1139
|
+
end
|
|
1140
|
+
elsif o["followed"].empty? &&
|
|
1141
|
+
message.include?("token exchange failed: #{status} from #{o['base']}/auth/token")
|
|
1142
|
+
refused += 1
|
|
1143
|
+
else
|
|
1144
|
+
wrong << seen
|
|
1145
|
+
end
|
|
1146
|
+
|
|
1147
|
+
looped = o["exchanged"] if "/api/auth/token" == location
|
|
1148
|
+
end
|
|
1149
|
+
|
|
1150
|
+
puts "secrets: exchange opt-in followed #{followed} redirect(s) on its own origin, " \
|
|
1151
|
+
"refused #{refused}, #{o['elsewhere'].length} elsewhere"
|
|
1152
|
+
|
|
1153
|
+
assert_equal [], o["elsewhere"], "the exchange reached another origin"
|
|
1154
|
+
assert_equal [], wrong
|
|
1155
|
+
assert_equal 3, followed
|
|
1156
|
+
assert_equal 3, refused
|
|
1157
|
+
# A redirect to itself is followed up to the hop limit and no more.
|
|
1158
|
+
assert_equal 6, looped
|
|
1031
1159
|
end
|
|
1032
1160
|
end
|
|
1033
1161
|
|
|
@@ -1058,4 +1186,47 @@ class SecretsFeatureTest < Minitest::Test
|
|
|
1058
1186
|
assert_equal 0, wire.calls.length,
|
|
1059
1187
|
"a refused op must never yield a request: #{wire.calls.inspect}"
|
|
1060
1188
|
end
|
|
1189
|
+
|
|
1190
|
+
# -------------------------------------------------------------------
|
|
1191
|
+
# Off by the config where it declares no auth, else by `auth: nil`: either
|
|
1192
|
+
# way the feature places no credential, buys no token and retries nothing.
|
|
1193
|
+
|
|
1194
|
+
def test_auth_off_places_no_credential_and_buys_no_token
|
|
1195
|
+
declared = ProjectNameConfig.shared_config["options"].key?("auth")
|
|
1196
|
+
by = declared ? "option" : "config"
|
|
1197
|
+
off = declared ? { "auth" => nil } : {}
|
|
1198
|
+
secrets = ["OFFKEY01", "OFFKEY02", "OFFREFRESH01", "ACCESS01"]
|
|
1199
|
+
carries = ->(call) { call["has"] || secrets.any? { |s| call["values"].include?(s) } }
|
|
1200
|
+
sent = []
|
|
1201
|
+
bought = 0
|
|
1202
|
+
|
|
1203
|
+
scenarios = [
|
|
1204
|
+
["a key the chain resolves", { "APIKEY" => "OFFKEY01" }, {}, {}, [200]],
|
|
1205
|
+
["an explicit apikey", {}, { "apikey" => "OFFKEY02" }, {}, [200]],
|
|
1206
|
+
["a refusal", { "REFRESH_TOKEN" => "OFFREFRESH01" }, {},
|
|
1207
|
+
{ "name" => "refresh_token", "exchange" => { "active" => true } }, [401]],
|
|
1208
|
+
]
|
|
1209
|
+
|
|
1210
|
+
scenarios.each do |what, env, sdkopts, fopts, statuses|
|
|
1211
|
+
env.each { |k, v| setenv(k, v) }
|
|
1212
|
+
wire = Wire.new
|
|
1213
|
+
wire.apistatus = statuses
|
|
1214
|
+
client = secrets_client(wire, sdkopts.merge(off).merge("feature" => secrets_opts(fopts)))
|
|
1215
|
+
drive_op(client, wire)
|
|
1216
|
+
|
|
1217
|
+
assert_equal 1, wire.api.length, "#{what}: a request was retried with auth off"
|
|
1218
|
+
assert_equal 0, wire.token.length, "#{what}: a token was bought with auth off"
|
|
1219
|
+
refute carries.call(wire.api[0]), "#{what}: a credential was sent with auth off"
|
|
1220
|
+
assert_equal "", secrets_feature_of(client).credential,
|
|
1221
|
+
"#{what}: the feature resolved a credential with auth off"
|
|
1222
|
+
|
|
1223
|
+
sent.concat(wire.api)
|
|
1224
|
+
bought += wire.token.length
|
|
1225
|
+
ensure
|
|
1226
|
+
env.each_key { |k| clearenv(k) }
|
|
1227
|
+
end
|
|
1228
|
+
|
|
1229
|
+
puts "secrets: auth off by #{by}: #{sent.length} request(s), " \
|
|
1230
|
+
"#{sent.count { |c| carries.call(c) }} credential(s), #{bought} token(s)"
|
|
1231
|
+
end
|
|
1061
1232
|
end
|
|
@@ -569,6 +569,26 @@ class FeatureTest < Minitest::Test
|
|
|
569
569
|
assert_equal 2, h.track(:@_cache)["bypass"]
|
|
570
570
|
end
|
|
571
571
|
|
|
572
|
+
def test_cache_a_body_that_declares_another_type_than_json_passes_by_unread
|
|
573
|
+
skip_unless_feature("cache")
|
|
574
|
+
sent = []
|
|
575
|
+
server, calls = H.recording_server do |n, _fd|
|
|
576
|
+
res = H.make_response(200, nil, { "content-type" => "audio/wav" })
|
|
577
|
+
res["body"] = +"RIFF#{n}"
|
|
578
|
+
sent << res
|
|
579
|
+
[res, nil]
|
|
580
|
+
end
|
|
581
|
+
h = harness([fspec("cache")], server: server)
|
|
582
|
+
a = h.op(path: "/w/1/audio")["ctx"].response
|
|
583
|
+
b = h.op(path: "/w/1/audio")["ctx"].response
|
|
584
|
+
assert_equal 2, calls.length, "the body was stored"
|
|
585
|
+
assert_same sent[0]["body"], a.body, "the cache handed on a copy it had read"
|
|
586
|
+
assert_same sent[0]["json"], a.json_func
|
|
587
|
+
assert_same sent[1]["body"], b.body
|
|
588
|
+
assert_same sent[1]["json"], b.json_func
|
|
589
|
+
assert_equal 2, h.track(:@_cache)["bypass"]
|
|
590
|
+
end
|
|
591
|
+
|
|
572
592
|
def test_cache_refetches_after_the_ttl
|
|
573
593
|
skip_unless_feature("cache")
|
|
574
594
|
clock = H::Clock.new
|
|
@@ -8,6 +8,7 @@ use crate::core::error::ProjectNameError;
|
|
|
8
8
|
use crate::core::helpers::{call_json, get_str, getp, jo, json_thunk, setp};
|
|
9
9
|
use crate::core::types::{Feature, FetcherFn};
|
|
10
10
|
use crate::feature::support::*;
|
|
11
|
+
use crate::utility::media::is_json_media;
|
|
11
12
|
use crate::utility::voxgigstruct::Value;
|
|
12
13
|
|
|
13
14
|
pub struct CacheSnapshot {
|
|
@@ -119,7 +120,7 @@ fn through(
|
|
|
119
120
|
let out = inner(ctx, url, fetchdef);
|
|
120
121
|
|
|
121
122
|
if let Ok(res) = &out {
|
|
122
|
-
if storable(res) {
|
|
123
|
+
if storable(res) && json(res) {
|
|
123
124
|
let snap = Rc::new(snapshot(res));
|
|
124
125
|
let ttl = fopt_int(options, "ttl", 5000);
|
|
125
126
|
let mut t = track.borrow_mut();
|
|
@@ -147,6 +148,19 @@ fn storable(res: &Value) -> bool {
|
|
|
147
148
|
&& !matches!(getp(res, "unreadable"), Value::Bool(true))
|
|
148
149
|
}
|
|
149
150
|
|
|
151
|
+
// A body that declares another type than JSON passes by, unread.
|
|
152
|
+
fn json(res: &Value) -> bool {
|
|
153
|
+
let mut declared = String::new();
|
|
154
|
+
if let Value::Map(hm) = getp(res, "headers") {
|
|
155
|
+
for (k, v) in hm.borrow().iter() {
|
|
156
|
+
if let (true, Value::Str(t)) = (k.eq_ignore_ascii_case("content-type"), v) {
|
|
157
|
+
declared = t.clone();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
declared.trim().is_empty() || is_json_media(&declared)
|
|
162
|
+
}
|
|
163
|
+
|
|
150
164
|
fn snapshot(res: &Value) -> CacheSnapshot {
|
|
151
165
|
let headers = Value::empty_map();
|
|
152
166
|
if let Value::Map(hm) = getp(res, "headers") {
|