@voxgig/sdkgen 4.48.1 → 4.49.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.
- 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 +8 -1
- 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
|
@@ -23,6 +23,8 @@
|
|
|
23
23
|
# trims it, along with the feature source and the vendored library, for a
|
|
24
24
|
# project whose model does not select `secrets`.
|
|
25
25
|
|
|
26
|
+
import base64
|
|
27
|
+
import contextlib
|
|
26
28
|
import json
|
|
27
29
|
import os
|
|
28
30
|
import http.server
|
|
@@ -42,9 +44,9 @@ ENVPREFIX = "PROJECTENV_TEST_SECRETS_"
|
|
|
42
44
|
|
|
43
45
|
# Read from the config, never from a probe: an SDK whose config declares no
|
|
44
46
|
# auth places no credential, and these tests assert on the one it places.
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
reason="this SDK's config declares no auth, so it places no credential")
|
|
47
|
+
NOAUTH = "auth" not in shared_config().get("options", {})
|
|
48
|
+
placing = pytest.mark.skipif(
|
|
49
|
+
NOAUTH, reason="this SDK's config declares no auth, so it places no credential")
|
|
48
50
|
|
|
49
51
|
|
|
50
52
|
def cred_header():
|
|
@@ -61,6 +63,27 @@ def cred_header():
|
|
|
61
63
|
CREDHEADER = cred_header()
|
|
62
64
|
|
|
63
65
|
|
|
66
|
+
def cred_form():
|
|
67
|
+
"""The form prepare_auth writes, from the DEFAULT client's auth: under
|
|
68
|
+
Basic the token is the key half of the encoded pair, and a prefix is
|
|
69
|
+
space-joined."""
|
|
70
|
+
sdk = ProjectNameSDK({"base": "http://localhost:8080"})
|
|
71
|
+
auth = sdk.options_map().get("auth") or {}
|
|
72
|
+
prefix = auth.get("prefix")
|
|
73
|
+
return (prefix if isinstance(prefix, str) else "", auth.get("basic") is True)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
CREDFORM = cred_form()
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def credential(token, secret=""):
|
|
80
|
+
"""The header value prepare_auth writes for a token."""
|
|
81
|
+
value = token
|
|
82
|
+
if CREDFORM[1]:
|
|
83
|
+
value = base64.b64encode((token + ":" + secret).encode("utf-8")).decode("ascii")
|
|
84
|
+
return value if "" == CREDFORM[0] else CREDFORM[0] + " " + value
|
|
85
|
+
|
|
86
|
+
|
|
64
87
|
def secrets_sdk(sdkopts):
|
|
65
88
|
"""An SDK with the secrets feature ACTIVE.
|
|
66
89
|
|
|
@@ -75,6 +98,74 @@ def secrets_sdk(sdkopts):
|
|
|
75
98
|
return ProjectNameSDK(sdkopts)
|
|
76
99
|
|
|
77
100
|
|
|
101
|
+
# Two real listeners: the origin answers the exchange as `answer` says and
|
|
102
|
+
# records what else it gets.
|
|
103
|
+
@contextlib.contextmanager
|
|
104
|
+
def _origins():
|
|
105
|
+
o = {"elsewhere": [], "followed": [], "exchanged": [0], "answer": [307, ""]}
|
|
106
|
+
|
|
107
|
+
def _recorder(log, respond, token):
|
|
108
|
+
class _Handler(http.server.BaseHTTPRequestHandler):
|
|
109
|
+
def _any(self):
|
|
110
|
+
raw = self.rfile.read(
|
|
111
|
+
int(self.headers.get("content-length") or 0))
|
|
112
|
+
if respond(self):
|
|
113
|
+
return
|
|
114
|
+
log.append((self.command, self.path, raw.decode("utf-8")))
|
|
115
|
+
out = json.dumps({"access_token": token}).encode("utf-8")
|
|
116
|
+
self.send_response(200)
|
|
117
|
+
self.send_header("content-type", "application/json")
|
|
118
|
+
self.send_header("content-length", str(len(out)))
|
|
119
|
+
self.end_headers()
|
|
120
|
+
self.wfile.write(out)
|
|
121
|
+
|
|
122
|
+
do_GET = do_POST = do_PUT = do_PATCH = do_DELETE = _any
|
|
123
|
+
|
|
124
|
+
def log_message(self, *args):
|
|
125
|
+
pass
|
|
126
|
+
return _Handler
|
|
127
|
+
|
|
128
|
+
def _exchange(handler):
|
|
129
|
+
if "/api/auth/token" != handler.path:
|
|
130
|
+
return False
|
|
131
|
+
o["exchanged"][0] += 1
|
|
132
|
+
handler.send_response(o["answer"][0])
|
|
133
|
+
handler.send_header("location", o["answer"][1])
|
|
134
|
+
handler.send_header("content-length", "0")
|
|
135
|
+
handler.end_headers()
|
|
136
|
+
return True
|
|
137
|
+
|
|
138
|
+
origin = http.server.HTTPServer(
|
|
139
|
+
("127.0.0.1", 0), _recorder(o["followed"], _exchange, "MOVED01"))
|
|
140
|
+
other = http.server.HTTPServer(
|
|
141
|
+
("127.0.0.1", 0), _recorder(o["elsewhere"], lambda h: False, "ELSEWHERE01"))
|
|
142
|
+
for srv in (origin, other):
|
|
143
|
+
threading.Thread(target=srv.serve_forever, daemon=True).start()
|
|
144
|
+
|
|
145
|
+
o["origin"] = "http://127.0.0.1:%d" % origin.server_port
|
|
146
|
+
o["base"] = o["origin"] + "/api"
|
|
147
|
+
o["away"] = "http://127.0.0.1:%d/api/auth/token" % other.server_port
|
|
148
|
+
|
|
149
|
+
try:
|
|
150
|
+
yield o
|
|
151
|
+
finally:
|
|
152
|
+
for srv in (origin, other):
|
|
153
|
+
srv.shutdown()
|
|
154
|
+
srv.server_close()
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _redirect_sdk(o, exchange=None):
|
|
158
|
+
return secrets_sdk({
|
|
159
|
+
"base": o["base"],
|
|
160
|
+
"feature": {"secrets": {
|
|
161
|
+
"active": True,
|
|
162
|
+
"name": "refresh_token",
|
|
163
|
+
"exchange": dict({"active": True, "refresh": "REFRESH01"},
|
|
164
|
+
**(exchange or {})),
|
|
165
|
+
}},
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
|
|
78
169
|
def envchain(extra=None):
|
|
79
170
|
"""The feature options most of these tests use: an env provider."""
|
|
80
171
|
secrets = {
|
|
@@ -98,6 +189,38 @@ class RecordingTransport:
|
|
|
98
189
|
"json": lambda: {}, "body": "{}"}, None
|
|
99
190
|
|
|
100
191
|
|
|
192
|
+
class StubFetch:
|
|
193
|
+
"""A live-mode system.fetch answering the token endpoint and the API, the
|
|
194
|
+
API by a script of statuses whose last repeats. Each call keeps a copy
|
|
195
|
+
of its headers, since a retry rewrites them in place."""
|
|
196
|
+
|
|
197
|
+
def __init__(self, statuses=None):
|
|
198
|
+
self.calls = []
|
|
199
|
+
self.statuses = statuses or [200]
|
|
200
|
+
self.tokens = ["ACCESS01", "ACCESS02", "ACCESS03"]
|
|
201
|
+
self.issued = 0
|
|
202
|
+
self.apicalls = 0
|
|
203
|
+
|
|
204
|
+
def __call__(self, url, fetchdef):
|
|
205
|
+
self.calls.append({"url": url, "headers": dict(fetchdef.get("headers") or {})})
|
|
206
|
+
if url.endswith("/auth/token"):
|
|
207
|
+
token = self.tokens[min(self.issued, len(self.tokens) - 1)]
|
|
208
|
+
self.issued += 1
|
|
209
|
+
return {"status": 200, "statusText": "OK", "headers": {},
|
|
210
|
+
"json": lambda: {"access_token": token}, "body": ""}, None
|
|
211
|
+
status = self.statuses[min(self.apicalls, len(self.statuses) - 1)]
|
|
212
|
+
self.apicalls += 1
|
|
213
|
+
return {"status": status, "statusText": "X", "headers": {},
|
|
214
|
+
"json": lambda: {"ok": status < 400}, "body": "{}"}, None
|
|
215
|
+
|
|
216
|
+
def api(self):
|
|
217
|
+
return [c for c in self.calls if not c["url"].endswith("/auth/token")]
|
|
218
|
+
|
|
219
|
+
def token(self):
|
|
220
|
+
return [c for c in self.calls if c["url"].endswith("/auth/token")]
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
@placing
|
|
101
224
|
class TestSecrets(unittest.TestCase):
|
|
102
225
|
|
|
103
226
|
def setUp(self):
|
|
@@ -143,15 +266,9 @@ class TestSecrets(unittest.TestCase):
|
|
|
143
266
|
self.assertIsNone(err)
|
|
144
267
|
return transport.calls[-1]["fetchdef"].get("headers") or {}
|
|
145
268
|
|
|
146
|
-
def assert_credential(self, headers, token):
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
# let the prefix be whatever this SDK's API declares.
|
|
150
|
-
got = str(headers.get(CREDHEADER) or "")
|
|
151
|
-
self.assertTrue(
|
|
152
|
-
got == token or got.endswith(" " + token),
|
|
153
|
-
"expected the %s header to carry %s, got: %r"
|
|
154
|
-
% (CREDHEADER, token, got))
|
|
269
|
+
def assert_credential(self, headers, token, secret=""):
|
|
270
|
+
self.assertEqual(credential(token, secret), headers.get(CREDHEADER),
|
|
271
|
+
"expected the %s header to carry %s" % (CREDHEADER, token))
|
|
155
272
|
|
|
156
273
|
# --- the chain ---
|
|
157
274
|
|
|
@@ -176,6 +293,15 @@ class TestSecrets(unittest.TestCase):
|
|
|
176
293
|
headers = self._send(sdk, transport)
|
|
177
294
|
self.assert_credential(headers, "ENVKEY01")
|
|
178
295
|
|
|
296
|
+
def test_a_resolved_key_is_placed_as_prepare_auth_places_it(self):
|
|
297
|
+
os.environ[ENVPREFIX + "APIKEY"] = "ENVKEY03"
|
|
298
|
+
opts = {"secret": "PASS01"}
|
|
299
|
+
opts.update(envchain())
|
|
300
|
+
sdk, transport = self._sdk(opts)
|
|
301
|
+
|
|
302
|
+
headers = self._send(sdk, transport)
|
|
303
|
+
self.assert_credential(headers, "ENVKEY03", "PASS01")
|
|
304
|
+
|
|
179
305
|
def test_custom_provider_supplies_the_credential(self):
|
|
180
306
|
# A custom provider is a MAP of callables (the exact parallel of
|
|
181
307
|
# ts's verbatim object-with-functions: python's options clone
|
|
@@ -347,6 +473,7 @@ class _TokenEndpoint(http.server.BaseHTTPRequestHandler):
|
|
|
347
473
|
pass
|
|
348
474
|
|
|
349
475
|
|
|
476
|
+
@placing
|
|
350
477
|
class TestSecretsReviewPins(unittest.TestCase):
|
|
351
478
|
|
|
352
479
|
def test_exchange_uses_the_raw_fallback_and_marshals_the_body(self):
|
|
@@ -423,88 +550,127 @@ class TestSecretsReviewPins(unittest.TestCase):
|
|
|
423
550
|
|
|
424
551
|
# Real listeners and the raw fallback transport: a stub follows nothing.
|
|
425
552
|
def test_a_redirect_answering_the_exchange_is_a_failure(self):
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
answer = [307, ""]
|
|
429
|
-
|
|
430
|
-
def _recorder(log, respond):
|
|
431
|
-
class _Handler(http.server.BaseHTTPRequestHandler):
|
|
432
|
-
def _any(self):
|
|
433
|
-
raw = self.rfile.read(
|
|
434
|
-
int(self.headers.get("content-length") or 0))
|
|
435
|
-
if respond(self):
|
|
436
|
-
return
|
|
437
|
-
log.append((self.command, self.path, raw.decode("utf-8")))
|
|
438
|
-
out = b'{"access_token":"ELSEWHERE01"}'
|
|
439
|
-
self.send_response(200)
|
|
440
|
-
self.send_header("content-type", "application/json")
|
|
441
|
-
self.send_header("content-length", str(len(out)))
|
|
442
|
-
self.end_headers()
|
|
443
|
-
self.wfile.write(out)
|
|
444
|
-
|
|
445
|
-
do_GET = do_POST = do_PUT = do_PATCH = do_DELETE = _any
|
|
446
|
-
|
|
447
|
-
def log_message(self, *args):
|
|
448
|
-
pass
|
|
449
|
-
return _Handler
|
|
450
|
-
|
|
451
|
-
def _exchange(handler):
|
|
452
|
-
if "/api/auth/token" != handler.path:
|
|
453
|
-
return False
|
|
454
|
-
handler.send_response(answer[0])
|
|
455
|
-
handler.send_header("location", answer[1])
|
|
456
|
-
handler.send_header("content-length", "0")
|
|
457
|
-
handler.end_headers()
|
|
458
|
-
return True
|
|
459
|
-
|
|
460
|
-
origin = http.server.HTTPServer(
|
|
461
|
-
("127.0.0.1", 0), _recorder(followed, _exchange))
|
|
462
|
-
other = http.server.HTTPServer(
|
|
463
|
-
("127.0.0.1", 0), _recorder(elsewhere, lambda h: False))
|
|
464
|
-
for srv in (origin, other):
|
|
465
|
-
threading.Thread(target=srv.serve_forever, daemon=True).start()
|
|
466
|
-
|
|
467
|
-
base = "http://127.0.0.1:%d/api" % origin.server_port
|
|
468
|
-
away = "http://127.0.0.1:%d/api/auth/token" % other.server_port
|
|
469
|
-
|
|
470
|
-
try:
|
|
471
|
-
answers = [[307, away], [308, away], [302, away],
|
|
553
|
+
with _origins() as o:
|
|
554
|
+
answers = [[307, o["away"]], [308, o["away"]], [302, o["away"]],
|
|
472
555
|
[307, "/api/moved/token"]]
|
|
473
556
|
allowed = []
|
|
474
557
|
refused = 0
|
|
475
558
|
|
|
476
559
|
for case in answers:
|
|
477
|
-
answer[:] = case
|
|
478
|
-
sdk = secrets_sdk({
|
|
479
|
-
"base": base,
|
|
480
|
-
"feature": {"secrets": {
|
|
481
|
-
"active": True,
|
|
482
|
-
"name": "refresh_token",
|
|
483
|
-
"exchange": {"active": True, "refresh": "REFRESH01"},
|
|
484
|
-
}},
|
|
485
|
-
})
|
|
560
|
+
o["answer"][:] = case
|
|
486
561
|
|
|
487
|
-
res =
|
|
562
|
+
res = _redirect_sdk(o).direct({"path": "/thing"})
|
|
488
563
|
err = res.get("err") if isinstance(res, dict) else None
|
|
489
564
|
message = str(res if err is None else err)
|
|
490
565
|
|
|
491
566
|
if ("token exchange failed: %d from %s/auth/token"
|
|
492
|
-
% (case[0], base)) in message:
|
|
567
|
+
% (case[0], o["base"])) in message:
|
|
493
568
|
refused += 1
|
|
494
569
|
else:
|
|
495
570
|
allowed.append("%d to %s: %s" % (case[0], case[1], message))
|
|
496
571
|
|
|
497
572
|
print("secrets: exchange refused %d redirect(s), %d followed"
|
|
498
|
-
% (refused, len(elsewhere) + len(followed)))
|
|
573
|
+
% (refused, len(o["elsewhere"]) + len(o["followed"])))
|
|
499
574
|
|
|
500
|
-
self.assertEqual([], elsewhere, "the exchange reached another origin")
|
|
501
|
-
self.assertEqual([], followed, "a request followed a refused exchange")
|
|
575
|
+
self.assertEqual([], o["elsewhere"], "the exchange reached another origin")
|
|
576
|
+
self.assertEqual([], o["followed"], "a request followed a refused exchange")
|
|
502
577
|
self.assertEqual([], allowed, "a redirect was not a failed exchange")
|
|
503
578
|
self.assertEqual(len(answers), refused)
|
|
579
|
+
|
|
580
|
+
def test_with_the_option_a_redirect_on_the_exchange_s_own_origin_is_followed(self):
|
|
581
|
+
with _origins() as o:
|
|
582
|
+
# Each case: the status, the Location, and the path the exchange is
|
|
583
|
+
# asked again at, or None where the redirect is refused.
|
|
584
|
+
cases = [
|
|
585
|
+
(307, "/api/moved/token", "/api/moved/token"),
|
|
586
|
+
(308, o["origin"] + "/api/moved/token", "/api/moved/token"),
|
|
587
|
+
# A Location of a query alone keeps the path that answered.
|
|
588
|
+
(307, "?step=2", "/api/auth/token?step=2"),
|
|
589
|
+
(307, o["away"], None),
|
|
590
|
+
(302, "/api/moved/token", None),
|
|
591
|
+
(307, "/api/auth/token", None),
|
|
592
|
+
]
|
|
593
|
+
|
|
594
|
+
# The call after the exchange goes through the SDK's own transport,
|
|
595
|
+
# which imports requests. Where that is missing, the token the
|
|
596
|
+
# client holds shows the exchange was answered.
|
|
597
|
+
try:
|
|
598
|
+
import requests # noqa: F401
|
|
599
|
+
transport = True
|
|
600
|
+
except ImportError:
|
|
601
|
+
transport = False
|
|
602
|
+
|
|
603
|
+
wrong = []
|
|
604
|
+
followed = refused = looped = 0
|
|
605
|
+
|
|
606
|
+
for status, location, path in cases:
|
|
607
|
+
o["answer"][:] = [status, location]
|
|
608
|
+
o["followed"].clear()
|
|
609
|
+
o["exchanged"][0] = 0
|
|
610
|
+
|
|
611
|
+
sdk = _redirect_sdk(o, {"redirect": "same-origin"})
|
|
612
|
+
res = sdk.direct({"path": "/thing"})
|
|
613
|
+
err = res.get("err") if isinstance(res, dict) else None
|
|
614
|
+
message = str(res if err is None else err)
|
|
615
|
+
seen = "%d to %s: %s %s" % (status, location, message, o["followed"])
|
|
616
|
+
|
|
617
|
+
if path is not None:
|
|
618
|
+
first = o["followed"][0] if o["followed"] else None
|
|
619
|
+
called = (isinstance(res, dict) and res.get("ok") is True
|
|
620
|
+
and 2 == len(o["followed"]))
|
|
621
|
+
if (first is not None
|
|
622
|
+
and ("POST", path) == first[:2]
|
|
623
|
+
and {"refresh_token": "REFRESH01"} == json.loads(first[2])
|
|
624
|
+
and "MOVED01" == sdk.options.get("apikey")
|
|
625
|
+
and (called or (not transport and 1 == len(o["followed"])))):
|
|
626
|
+
followed += 1
|
|
627
|
+
else:
|
|
628
|
+
wrong.append(seen)
|
|
629
|
+
elif 0 == len(o["followed"]) and (
|
|
630
|
+
"token exchange failed: %d from %s/auth/token"
|
|
631
|
+
% (status, o["base"])) in message:
|
|
632
|
+
refused += 1
|
|
633
|
+
else:
|
|
634
|
+
wrong.append(seen)
|
|
635
|
+
|
|
636
|
+
if "/api/auth/token" == location:
|
|
637
|
+
looped = o["exchanged"][0]
|
|
638
|
+
|
|
639
|
+
print("secrets: exchange opt-in followed %d redirect(s) on its own "
|
|
640
|
+
"origin, refused %d, %d elsewhere"
|
|
641
|
+
% (followed, refused, len(o["elsewhere"])))
|
|
642
|
+
|
|
643
|
+
self.assertEqual([], o["elsewhere"], "the exchange reached another origin")
|
|
644
|
+
self.assertEqual([], wrong)
|
|
645
|
+
self.assertEqual(3, followed)
|
|
646
|
+
self.assertEqual(3, refused)
|
|
647
|
+
# A redirect to itself is followed five times and no more.
|
|
648
|
+
self.assertEqual(6, looped)
|
|
649
|
+
|
|
650
|
+
def test_a_token_bought_on_a_refusal_is_placed_as_prepare_auth_places_it(self):
|
|
651
|
+
os.environ[ENVPREFIX + "REFRESH_TOKEN"] = "REFRESH01"
|
|
652
|
+
try:
|
|
653
|
+
stub = StubFetch([401, 200])
|
|
654
|
+
sdk = secrets_sdk({
|
|
655
|
+
"base": "http://exchange.test/api",
|
|
656
|
+
"secret": "PASS01",
|
|
657
|
+
"system": {"fetch": stub},
|
|
658
|
+
"feature": {"secrets": {
|
|
659
|
+
"active": True,
|
|
660
|
+
"name": "refresh_token",
|
|
661
|
+
"providers": [{"kind": "env", "prefix": ENVPREFIX}],
|
|
662
|
+
"exchange": {"active": True},
|
|
663
|
+
}},
|
|
664
|
+
})
|
|
665
|
+
|
|
666
|
+
sdk.direct({"path": "/thing"})
|
|
667
|
+
|
|
668
|
+
api = stub.api()
|
|
669
|
+
self.assertEqual(2, len(api), "expected the request to be retried")
|
|
670
|
+
self.assertEqual(credential("ACCESS01", "PASS01"), api[0]["headers"].get(CREDHEADER))
|
|
671
|
+
self.assertEqual(credential("ACCESS02", "PASS01"), api[1]["headers"].get(CREDHEADER))
|
|
504
672
|
finally:
|
|
505
|
-
|
|
506
|
-
srv.shutdown()
|
|
507
|
-
srv.server_close()
|
|
673
|
+
os.environ.pop(ENVPREFIX + "REFRESH_TOKEN", None)
|
|
508
674
|
|
|
509
675
|
def test_a_supplied_fetch_is_asked_not_to_follow_the_exchange(self):
|
|
510
676
|
calls = []
|
|
@@ -582,8 +748,75 @@ class TestSecretsReviewPins(unittest.TestCase):
|
|
|
582
748
|
return transport.calls[-1]["fetchdef"].get("headers") or {}
|
|
583
749
|
|
|
584
750
|
def assert_credential(self, headers, token):
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
751
|
+
self.assertEqual(credential(token), headers.get(CREDHEADER),
|
|
752
|
+
"expected the %s header to carry %s" % (CREDHEADER, token))
|
|
753
|
+
|
|
754
|
+
|
|
755
|
+
# Off by the config where it declares no auth, else by `auth: None`: either
|
|
756
|
+
# way the feature places no credential, buys no token and retries nothing.
|
|
757
|
+
AUTHOFF = ("config", {}) if NOAUTH else ("option", {"auth": None})
|
|
758
|
+
|
|
759
|
+
|
|
760
|
+
class TestSecretsAuthOff(unittest.TestCase):
|
|
761
|
+
|
|
762
|
+
SECRETS = ["OFFKEY01", "OFFKEY02", "OFFREFRESH01", "ACCESS01"]
|
|
763
|
+
sent = []
|
|
764
|
+
bought = 0
|
|
765
|
+
|
|
766
|
+
def setUp(self):
|
|
767
|
+
os.environ.pop(ENVPREFIX + "APIKEY", None)
|
|
768
|
+
os.environ.pop(ENVPREFIX + "REFRESH_TOKEN", None)
|
|
769
|
+
|
|
770
|
+
tearDown = setUp
|
|
771
|
+
|
|
772
|
+
@classmethod
|
|
773
|
+
def tearDownClass(cls):
|
|
774
|
+
print("secrets: auth off by %s: %d request(s), %d credential(s), %d token(s)"
|
|
775
|
+
% (AUTHOFF[0], len(cls.sent),
|
|
776
|
+
len([c for c in cls.sent if cls.carries(c)]), cls.bought))
|
|
777
|
+
|
|
778
|
+
@classmethod
|
|
779
|
+
def carries(cls, call):
|
|
780
|
+
"""The header prepare_auth writes, or any header holding a secret."""
|
|
781
|
+
headers = call["headers"]
|
|
782
|
+
return CREDHEADER in headers or any(
|
|
783
|
+
secret in str(value) for value in headers.values() for secret in cls.SECRETS)
|
|
784
|
+
|
|
785
|
+
def _offsdk(self, stub, sdkopts, fopts):
|
|
786
|
+
opts = {"base": "http://authoff.test/api", "system": {"fetch": stub}}
|
|
787
|
+
opts.update(sdkopts)
|
|
788
|
+
opts.update(AUTHOFF[1])
|
|
789
|
+
secrets = {"active": True, "providers": [{"kind": "env", "prefix": ENVPREFIX}]}
|
|
790
|
+
secrets.update(fopts)
|
|
791
|
+
opts["feature"] = {"secrets": secrets}
|
|
792
|
+
sdk = secrets_sdk(opts)
|
|
793
|
+
sdk.direct({"path": "/thing"})
|
|
794
|
+
TestSecretsAuthOff.sent.extend(stub.api())
|
|
795
|
+
TestSecretsAuthOff.bought += len(stub.token())
|
|
796
|
+
return sdk
|
|
797
|
+
|
|
798
|
+
def test_a_key_the_chain_resolves_is_not_placed(self):
|
|
799
|
+
os.environ[ENVPREFIX + "APIKEY"] = "OFFKEY01"
|
|
800
|
+
stub = StubFetch()
|
|
801
|
+
sdk = self._offsdk(stub, {}, {})
|
|
802
|
+
|
|
803
|
+
self.assertEqual(1, len(stub.api()))
|
|
804
|
+
self.assertFalse(self.carries(stub.api()[0]), "a credential was sent with auth off")
|
|
805
|
+
self.assertEqual("", sdk.options.get("apikey"),
|
|
806
|
+
"the resolved key was placed where prepare_auth reads it")
|
|
807
|
+
|
|
808
|
+
def test_an_explicit_apikey_is_not_placed(self):
|
|
809
|
+
stub = StubFetch()
|
|
810
|
+
self._offsdk(stub, {"apikey": "OFFKEY02"}, {})
|
|
811
|
+
|
|
812
|
+
self.assertEqual(1, len(stub.api()))
|
|
813
|
+
self.assertFalse(self.carries(stub.api()[0]), "a credential was sent with auth off")
|
|
814
|
+
|
|
815
|
+
def test_a_refusal_buys_no_token_and_is_not_retried(self):
|
|
816
|
+
os.environ[ENVPREFIX + "REFRESH_TOKEN"] = "OFFREFRESH01"
|
|
817
|
+
stub = StubFetch([401])
|
|
818
|
+
self._offsdk(stub, {}, {"name": "refresh_token", "exchange": {"active": True}})
|
|
819
|
+
|
|
820
|
+
self.assertEqual(0, len(stub.token()), "a token was bought with auth off")
|
|
821
|
+
self.assertEqual(1, len(stub.api()), "a refusal was retried with auth off")
|
|
822
|
+
self.assertFalse(self.carries(stub.api()[0]), "a credential was sent with auth off")
|
|
@@ -349,6 +349,24 @@ class TestCacheFeature:
|
|
|
349
349
|
assert json_func() == {"v": 1}
|
|
350
350
|
assert json_func() == {"v": 1}
|
|
351
351
|
|
|
352
|
+
def test_a_body_that_declares_another_type_than_json_passes_by_unread(self):
|
|
353
|
+
sent = []
|
|
354
|
+
|
|
355
|
+
def reply(_n, _fd):
|
|
356
|
+
res = make_response(200, None, {"content-type": "audio/wav"})
|
|
357
|
+
res["body"] = b"RIFF"
|
|
358
|
+
sent.append(res)
|
|
359
|
+
return res
|
|
360
|
+
|
|
361
|
+
server, calls = recording_server(reply)
|
|
362
|
+
h = make_client([{"name": "cache"}], server=server)
|
|
363
|
+
a = h.op(op="load", path="/w/1/audio")
|
|
364
|
+
b = h.op(op="load", path="/w/1/audio")
|
|
365
|
+
assert len(calls) == 2, "the body was stored"
|
|
366
|
+
assert a["ctx"].response is sent[0], "the cache handed on a copy it had read"
|
|
367
|
+
assert b["ctx"].response is sent[1]
|
|
368
|
+
assert h.client._cache["bypass"] == 2
|
|
369
|
+
|
|
352
370
|
def test_inactive_cache_does_not_wrap(self):
|
|
353
371
|
server, calls = recording_server()
|
|
354
372
|
h = make_client([{"name": "cache", "options": {"active": False}}],
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
# inspection. Replayed responses expose a re-readable "json" body.
|
|
10
10
|
|
|
11
11
|
require_relative 'base_feature'
|
|
12
|
+
require_relative '../utility/media'
|
|
12
13
|
|
|
13
14
|
class ProjectNameCacheFeature < ProjectNameBaseFeature
|
|
14
15
|
def initialize
|
|
@@ -57,6 +58,12 @@ class ProjectNameCacheFeature < ProjectNameBaseFeature
|
|
|
57
58
|
|
|
58
59
|
res, err = inner.call(ctx, url, fetchdef)
|
|
59
60
|
|
|
61
|
+
# A body that declares another type than JSON passes by, unread.
|
|
62
|
+
if err.nil? && _cacheable(res) && !_json(res)
|
|
63
|
+
_track("bypass")
|
|
64
|
+
return res, err
|
|
65
|
+
end
|
|
66
|
+
|
|
60
67
|
if err.nil? && _cacheable(res)
|
|
61
68
|
snapshot = _snapshot(res)
|
|
62
69
|
ttl = @options["ttl"].nil? ? 5000 : @options["ttl"]
|
|
@@ -78,6 +85,11 @@ class ProjectNameCacheFeature < ProjectNameBaseFeature
|
|
|
78
85
|
status.is_a?(Numeric) && status >= 200 && status < 300
|
|
79
86
|
end
|
|
80
87
|
|
|
88
|
+
def _json(res)
|
|
89
|
+
type = ProjectNameUtilities.header_value(res["headers"], "content-type")
|
|
90
|
+
type.empty? || ProjectNameUtilities.json_media?(type)
|
|
91
|
+
end
|
|
92
|
+
|
|
81
93
|
def _snapshot(res)
|
|
82
94
|
data = nil
|
|
83
95
|
jf = res["json"]
|