quonfig 2.2.0 → 2.3.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/README.md +61 -2
- data/lib/quonfig/client.rb +0 -1
- data/lib/quonfig/config_loader.rb +1 -1
- data/lib/quonfig/config_store.rb +1 -1
- data/lib/quonfig/datadir.rb +33 -3
- data/lib/quonfig/encryption.rb +9 -0
- data/lib/quonfig/errors/decryption_error.rb +15 -6
- data/lib/quonfig/qf1.rb +429 -0
- data/lib/quonfig/resolver.rb +56 -56
- data/lib/quonfig/semantic_logger_filter.rb +2 -2
- data/lib/quonfig/version.rb +1 -1
- data/lib/quonfig.rb +1 -0
- data/quonfig.gemspec +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: eaa59334a03230d2d227fba0a94fb881af7d6f2123cedb3e9836ad58ab650c43
|
|
4
|
+
data.tar.gz: 91df016a193ea3f7431055d0961014ce48f659ba1b202ab3353ed2aaba20f213
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e2156143570ba7ef1824347191dfc3a124858cc3171c1576dc5a477d53af3998c5f4dc9e4518f205f2ca7f8014c69b4ca7dc3ae98cc13502f847187faf0d6688
|
|
7
|
+
data.tar.gz: bb85a47e6f448a28a4d4835ef4323751ded9aeea14c9e90e8e996849072a5c059a38b66222dd30db9f6d632f02ad50fcb5bb424bec304897f21ad02eff4db109
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 2.3.0 - 2026-11-30
|
|
4
|
+
|
|
5
|
+
- **Changed: secrets are read from `secrets/` in the qf1 format, and `decryptWith` is no longer read (qfg-8seuf.11).** Secrets are now documents in the workspace's `secrets/` directory (`type: "secret"`) whose values are `qf1:<KEYSET>:<fingerprint>:<payload>` strings, encrypted with X25519, HKDF-SHA256 and AES-256-GCM (spec `project/plans/secrets-qf1-spec.md`). The private keys come from the environment variable named by the keyset, `QUONFIG_KEYSET` or `QUONFIG_KEYSET_<NAME>`: one or more `qf1-priv-...` entries separated by commas. A secret is decrypted lazily, at the get that reads it; a missing or broken keyset, or a key the runtime does not hold, fails only those values with `Quonfig::Errors::DecryptionError` (your `default:` applies, as for any decryption failure) and never fails startup. Error messages name the config key, the qf1 error class (`qf1 key_not_held: ...`), the keyset and the fingerprints held and needed, and never key material, keyset values or plaintext; the new `DecryptionError#qf1_code` returns the class, so code can detect the failure without parsing text. An invalid keyset name in a stored value is reported by its length, never quoted. Datadir mode loads `secrets/` last, so a secret replaces a same-key document in `configs/` (a non-secret file in `secrets/` is skipped with a warning naming only its path); over the wire the SDK accepts `type: "secret"` entries. Telemetry reports a secret's evaluations with type `secret` and its value redacted over the stored ciphertext.
|
|
6
|
+
- **Changed: old-format `decryptWith` values no longer decrypt (qfg-8seuf.11).** A value that still carries `decryptWith` now fails with `Quonfig::Errors::DecryptionError` (its message names `decryptWith`; `qf1_code` is nil) instead of being decrypted, so a typed getter returns your `default:` or, with no default, follows `on_no_default`. Any `decryptWith` other than absent, nil or `""` fails, whatever its JSON type, and no key config is read. The old `ct--iv--tag` format, its key-config lookup and its cycle guard are gone from the read path. Move each such value to `secrets/` with the `qfg` CLI before upgrading. Plain `confidential` values and `provided` ENV_VAR values are unchanged. Approved as a minor by Jeff (qfg-8seuf.11, 2026-11-30; prod corpus scan 2026-11-30: 0 customer workspaces hold decryptWith values).
|
|
7
|
+
- **Deprecated: `Quonfig::Encryption`.** The SDK no longer calls it; it stays so code that references it keeps loading, and will be removed in a future major version.
|
|
8
|
+
- **Platform: reading secrets needs X25519 (Ruby 3.1+ with the openssl gem 3.0+ on OpenSSL 1.1.1+).** The gem already requires Ruby 3.2 (below), so the Ruby floor does not move again. On an OpenSSL build without X25519 (LibreSSL, an older openssl gem) the SDK still loads and serves every non-secret config; the first secret read fails with `DecryptionError` naming the requirement (`qf1 unsupported_platform`), as does each secret read when OpenSSL refuses X25519 at run time (FIPS mode).
|
|
9
|
+
- **Minimum version for secrets: 2.3.0, in both API (delivery) and datadir mode (qfg-8seuf.11).** Workspaces that store secrets in `secrets/` need quonfig 2.3.0 or newer. An older SDK reading a secret over the wire returns the raw `qf1:` string as the value; in datadir mode an older SDK never opens `secrets/`, so a key that exists only there is missing and a key that still has a copy in `configs/` serves that old copy. Upgrade every backend service that reads a secret before creating or migrating it.
|
|
10
|
+
- Integration tests regenerated from integration-test-data tag `v2026.11.30` (the 21 qf1 cases) (qfg-8seuf.11).
|
|
11
|
+
- **Changed: the gem now requires Ruby 3.2 or newer (`required_ruby_version >= 3.2`; it was `>= 3.0`).** Ruby 3.0 and 3.1 are end of life (3.1 since March 2025), and CI tests on 3.2, 3.3 and 3.4. Raising the floor also lets Dependabot resolve this repo's lockfile, since it resolves under the lowest Ruby the gemspec allows, and the locked activesupport 8 needs 3.2.
|
|
12
|
+
|
|
3
13
|
## 2.2.0 - 2026-10-29
|
|
4
14
|
|
|
5
15
|
- **Changed (edge case): the `*_details` getters and `evaluate_details` report a value that cannot be parsed as `error_code: "PARSE_ERROR"` (qfg-wg1n).** A malformed ENV_VAR value (an int config whose env var is `"twelve"`, a duration env var `"PT5S garbage"`) and, on the typed getters, a malformed stored int, double or duration (`"twelve"`, `"abc"`, `"PT0.5H"`) were reported as `TYPE_MISMATCH`; they are now `PARSE_ERROR`, the code OpenFeature uses for this case, with the same `reason: "ERROR"`, nil value, variant and `flag_metadata` as before. A value of another type (`get_int_details` on a string config) is still `TYPE_MISMATCH`. The new constant is `Quonfig::EvaluationDetails::ERROR_PARSE_ERROR`. Only the error code on these failure paths changes: the plain getters, `get_or_raise` and the raised errors (`EnvVarParseError`, `TypeMismatchError`) are unchanged, and `evaluate_details` still returns a malformed stored value as stored with a success reason. Code that compared `error_code` to `TYPE_MISMATCH` to detect a malformed value should also accept `PARSE_ERROR`. quonfig-openfeature 2.3.0 already passes `PARSE_ERROR` straight through. Tests: `test/test_parse_error_details.rb`.
|
data/README.md
CHANGED
|
@@ -52,8 +52,11 @@ client.get_bool('beta-feature', context: { user: { key: 'user-123', plan: 'pro'
|
|
|
52
52
|
|
|
53
53
|
### 2. `with_context` block
|
|
54
54
|
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
With a block, `with_context` yields a `BoundClient` that carries the supplied
|
|
56
|
+
context. The context applies only to calls made on that yielded client
|
|
57
|
+
(`bound` below). It is not ambient: `client.get_*` or `Quonfig.get` calls inside
|
|
58
|
+
the block do not see it. The block's return value is returned from
|
|
59
|
+
`with_context`.
|
|
57
60
|
|
|
58
61
|
```ruby
|
|
59
62
|
result = client.with_context(user: { key: 'user-123', plan: 'pro' }) do |bound|
|
|
@@ -249,10 +252,66 @@ is meaningfully too slow for your use case.
|
|
|
249
252
|
See the [open-source / local how-to](https://docs.quonfig.com/docs/how-tos/open-source-local)
|
|
250
253
|
for the cross-SDK story (sdk-node, sdk-go, sdk-ruby, sdk-python, sdk-java).
|
|
251
254
|
|
|
255
|
+
## Secrets
|
|
256
|
+
|
|
257
|
+
Secrets live in your workspace's `secrets/` directory as qf1 values
|
|
258
|
+
(`qf1:<KEYSET>:<fingerprint>:<payload>`), encrypted to a public key with
|
|
259
|
+
X25519, HKDF-SHA256 and AES-256-GCM. You create and change them with the
|
|
260
|
+
`qfg` CLI; the SDK only decrypts. Read one like any string config:
|
|
261
|
+
|
|
262
|
+
```ruby
|
|
263
|
+
client.get_string('stripe.webhook.secret')
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
The private keys come from the environment variable named by the value's
|
|
267
|
+
keyset, for example `QUONFIG_KEYSET` or `QUONFIG_KEYSET_PROD` (names match
|
|
268
|
+
`^QUONFIG_KEYSET(_[A-Z0-9]+)*$`). It holds one or more `qf1-priv-...` entries
|
|
269
|
+
separated by commas; spaces, tabs and newlines around an entry are ignored.
|
|
270
|
+
The SDK finds the key by keyset name and fingerprint and never tries keys one
|
|
271
|
+
after another, and it never reads any other variable for a secret.
|
|
272
|
+
|
|
273
|
+
- **Decryption is lazy, per value.** Startup never reads a keyset. A missing
|
|
274
|
+
or unparseable keyset, or a key this runtime does not hold, fails only the
|
|
275
|
+
secrets that need it, with `Quonfig::Errors::DecryptionError` (so a typed
|
|
276
|
+
getter with a `default:` returns the default, as for any decryption
|
|
277
|
+
failure). The message names the config key, the qf1 error class (for
|
|
278
|
+
example `qf1 key_not_held`), the keyset and the fingerprints held and
|
|
279
|
+
needed; it never contains key material, the keyset's value or the secret.
|
|
280
|
+
An invalid keyset name is reported by its length, never quoted. Detect the
|
|
281
|
+
failure by class (`DecryptionError`) and `DecryptionError#qf1_code` (the
|
|
282
|
+
class name, or `nil` for a `decryptWith` value), not by parsing the text.
|
|
283
|
+
- **Datadir mode** reads `secrets/` after the other directories, so a secret
|
|
284
|
+
replaces a document with the same key in `configs/`. A file in `secrets/`
|
|
285
|
+
that is not a secret document (not JSON, no key) is skipped with one
|
|
286
|
+
warning naming its path, never its content.
|
|
287
|
+
- **Telemetry** reports secret evaluations with type `secret` and the value
|
|
288
|
+
redacted (`*****` plus a short hash of the stored ciphertext), never the
|
|
289
|
+
plaintext.
|
|
290
|
+
- **The old format is not read.** A value that still carries `decryptWith`
|
|
291
|
+
(the old `ct--iv--tag` format keyed by a config; any non-empty value of
|
|
292
|
+
the field, whatever its type) fails with `DecryptionError` instead of being
|
|
293
|
+
returned, and no key config is read. Move it to `secrets/` with the
|
|
294
|
+
CLI. `Quonfig::Encryption` is deprecated and unused by the SDK.
|
|
295
|
+
- **Platform.** X25519 needs Ruby 3.1+ with the openssl gem 3.0+ on OpenSSL
|
|
296
|
+
1.1.1 or later; this gem requires Ruby 3.2. On a build without X25519
|
|
297
|
+
(LibreSSL, an old openssl gem) the SDK still loads and serves every other
|
|
298
|
+
config; the first secret read fails with a `DecryptionError` naming the
|
|
299
|
+
requirement (`qf1 unsupported_platform`). The same applies when OpenSSL
|
|
300
|
+
refuses X25519 at run time, for example in FIPS mode: each secret read
|
|
301
|
+
fails, and the message names the runtime restriction, not your keys.
|
|
302
|
+
|
|
303
|
+
Older SDKs do not read secrets. Over the wire, an older SDK returns the raw
|
|
304
|
+
`qf1:` string as the value. In datadir mode an older SDK never opens
|
|
305
|
+
`secrets/`: a key that exists only there is missing (your default applies),
|
|
306
|
+
and a key that still has a copy in `configs/` serves that old copy. Upgrade
|
|
307
|
+
every backend service that reads a secret, in either mode, before you create
|
|
308
|
+
or migrate it.
|
|
309
|
+
|
|
252
310
|
## Environment variables
|
|
253
311
|
|
|
254
312
|
| Variable | Purpose |
|
|
255
313
|
|-----------------------------|------------------------------------------------------------------------------------------|
|
|
314
|
+
| `QUONFIG_KEYSET*` | Private keys of a secrets keyset (`qf1-priv-...`, comma-separated), read only when a secret on that keyset is read. See [Secrets](#secrets). |
|
|
256
315
|
| `QUONFIG_BACKEND_SDK_KEY` | SDK key used to authenticate against the Quonfig API. Used when `sdk_key:` is omitted. |
|
|
257
316
|
| `QUONFIG_DIR` | Path to a workspace directory. When set, the SDK runs in datadir/offline mode. |
|
|
258
317
|
| `QUONFIG_ENVIRONMENT` | Environment name (`production`, `staging`, `development`) evaluated in datadir mode. |
|
data/lib/quonfig/client.rb
CHANGED
|
@@ -89,7 +89,7 @@ module Quonfig
|
|
|
89
89
|
# auth-class status. A 200 the guard dropped, or a 304, never qualifies.
|
|
90
90
|
def auth_rejected?
|
|
91
91
|
return false unless @result == :failed
|
|
92
|
-
return false if @statuses.values.
|
|
92
|
+
return false if @statuses.values.intersect?([200, 304])
|
|
93
93
|
|
|
94
94
|
AUTH_STATUSES.include?(primary_status)
|
|
95
95
|
end
|
data/lib/quonfig/config_store.rb
CHANGED
|
@@ -14,7 +14,7 @@ module Quonfig
|
|
|
14
14
|
# test harness and single-key callers.
|
|
15
15
|
#
|
|
16
16
|
# One lookup can read several keys (a flag, then the segments it
|
|
17
|
-
# references
|
|
17
|
+
# references). #pinned holds one snapshot for the
|
|
18
18
|
# current thread for the duration of a block, so every read in that
|
|
19
19
|
# lookup comes from the same envelope even if an install swaps the store
|
|
20
20
|
# mid-evaluation.
|
data/lib/quonfig/datadir.rb
CHANGED
|
@@ -12,6 +12,7 @@ module Quonfig
|
|
|
12
12
|
# <datadir>/feature-flags/*.json
|
|
13
13
|
# <datadir>/segments/*.json
|
|
14
14
|
# <datadir>/log-levels/*.json
|
|
15
|
+
# <datadir>/secrets/*.json (qf1 secrets, loaded LAST)
|
|
15
16
|
#
|
|
16
17
|
# schemas/ is intentionally excluded — those files are raw JSON Schema
|
|
17
18
|
# documents, not Configs, and SDKs do not consume them (qfg-uzsl).
|
|
@@ -20,7 +21,11 @@ module Quonfig
|
|
|
20
21
|
# it down to the ConfigResponse shape that the SSE/HTTP delivery path emits,
|
|
21
22
|
# so ConfigStore consumes both transports uniformly.
|
|
22
23
|
module Datadir
|
|
23
|
-
|
|
24
|
+
# secrets/ MUST stay last (spec 13.7): the store is a map by key and the
|
|
25
|
+
# last write wins, so a secret shadows a same-key document in configs/.
|
|
26
|
+
CONFIG_SUBDIRS = %w[configs feature-flags segments log-levels secrets].freeze
|
|
27
|
+
SECRETS_SUBDIR = 'secrets'
|
|
28
|
+
LOG = Quonfig::InternalLogger.new(self)
|
|
24
29
|
|
|
25
30
|
module_function
|
|
26
31
|
|
|
@@ -42,9 +47,11 @@ module Quonfig
|
|
|
42
47
|
# Always read as UTF-8: under LANG unset / LANG=C the process
|
|
43
48
|
# default is US-ASCII and JSON.parse would raise on any non-ASCII
|
|
44
49
|
# byte (qfg-goi1.2.11 / qfg-xy92).
|
|
45
|
-
raw =
|
|
46
|
-
|
|
50
|
+
raw = subdir == SECRETS_SUBDIR ? read_secret_document(path) : read_document(path)
|
|
51
|
+
next if raw.nil?
|
|
47
52
|
|
|
53
|
+
# The directory, not the stored type, makes a document a secret.
|
|
54
|
+
raw['type'] = 'secret' if subdir == SECRETS_SUBDIR
|
|
48
55
|
coerce_numeric_values(raw)
|
|
49
56
|
configs << to_config_response(raw, env_id)
|
|
50
57
|
end
|
|
@@ -58,6 +65,29 @@ module Quonfig
|
|
|
58
65
|
)
|
|
59
66
|
end
|
|
60
67
|
|
|
68
|
+
def read_document(path)
|
|
69
|
+
raw = JSON.parse(File.read(path, encoding: 'UTF-8'))
|
|
70
|
+
raise ArgumentError, "[quonfig] config has empty key — file is not a Quonfig Config: #{path}" if raw['key'].nil? || raw['key'].to_s.empty?
|
|
71
|
+
|
|
72
|
+
raw
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# A file in secrets/ that is not a secret document (not JSON, not an
|
|
76
|
+
# object, no key, a directory named *.json) is skipped with one warning
|
|
77
|
+
# that names the path only: never the content, which may be key
|
|
78
|
+
# material. One bad file never stops startup.
|
|
79
|
+
def read_secret_document(path)
|
|
80
|
+
raw = JSON.parse(File.read(path, encoding: 'UTF-8'))
|
|
81
|
+
return raw if raw.is_a?(Hash) && raw['key'].is_a?(String) && !raw['key'].empty?
|
|
82
|
+
|
|
83
|
+
LOG.warn "[quonfig] skipping #{path}: not a secret document (no key)"
|
|
84
|
+
nil
|
|
85
|
+
rescue JSON::ParserError, EncodingError, SystemCallError, IOError
|
|
86
|
+
# The exception text can quote the file's content; it is not logged.
|
|
87
|
+
LOG.warn "[quonfig] skipping #{path}: not a readable JSON secret document"
|
|
88
|
+
nil
|
|
89
|
+
end
|
|
90
|
+
|
|
61
91
|
# Convenience: load the envelope and populate a fresh ConfigStore.
|
|
62
92
|
def load_store(datadir, environment = nil)
|
|
63
93
|
envelope = load_envelope(datadir, environment)
|
data/lib/quonfig/encryption.rb
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Quonfig
|
|
4
|
+
# The OLD secrets format (`ct--iv--tag` hex, AES-256-GCM with a key from a
|
|
5
|
+
# `decryptWith` config).
|
|
6
|
+
#
|
|
7
|
+
# @deprecated The SDK no longer reads this format: secrets are qf1 values in
|
|
8
|
+
# `secrets/`, decrypted with `QUONFIG_KEYSET*` keys (qfg-8seuf.11), and a
|
|
9
|
+
# value that still carries `decryptWith` fails with
|
|
10
|
+
# Quonfig::Errors::DecryptionError. Nothing in the SDK calls this class.
|
|
11
|
+
# It stays only so code that referenced it keeps loading; it will be
|
|
12
|
+
# removed in a future major version.
|
|
4
13
|
class Encryption
|
|
5
14
|
CIPHER_TYPE = 'aes-256-gcm' # 32/12
|
|
6
15
|
SEPARATOR = '--'
|
|
@@ -2,17 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
module Quonfig
|
|
4
4
|
module Errors
|
|
5
|
-
# Raised when a
|
|
6
|
-
#
|
|
7
|
-
#
|
|
5
|
+
# Raised when a secret cannot be decrypted: every qf1 reader failure
|
|
6
|
+
# (spec classes malformed, unsupported_version, invalid_keyset_name,
|
|
7
|
+
# keyset_missing, keyset_invalid, key_not_held, invalid_ephemeral_key,
|
|
8
|
+
# decrypt_failed), and a value that still carries the old `decryptWith`
|
|
9
|
+
# field, which this SDK no longer reads.
|
|
8
10
|
#
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
11
|
+
# The message names the config key and, for qf1 failures, the class
|
|
12
|
+
# (`qf1 key_not_held: ...`), keyset and fingerprints. It never contains
|
|
13
|
+
# key material, keyset env values, the ciphertext payload or plaintext.
|
|
14
|
+
#
|
|
15
|
+
# Mirrors sdk-python's QuonfigDecryptionError.
|
|
12
16
|
class DecryptionError < Quonfig::Error
|
|
17
|
+
# The qf1 error class name (for example "key_not_held"), or nil when
|
|
18
|
+
# the failure is not a qf1 one (a leftover decryptWith value).
|
|
19
|
+
attr_reader :qf1_code
|
|
20
|
+
|
|
13
21
|
def initialize(key, cause = nil)
|
|
14
22
|
message = "Decryption failed for config '#{key}'"
|
|
15
23
|
message += ": #{cause}" if cause && !cause.to_s.empty?
|
|
24
|
+
@qf1_code = cause.respond_to?(:code) ? cause.code : nil
|
|
16
25
|
super(message)
|
|
17
26
|
end
|
|
18
27
|
end
|
data/lib/quonfig/qf1.rb
ADDED
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'openssl'
|
|
4
|
+
require 'digest'
|
|
5
|
+
|
|
6
|
+
module Quonfig
|
|
7
|
+
# qf1 secrets reader (qfg-8seuf.11). Normative spec:
|
|
8
|
+
# project/plans/secrets-qf1-spec.md (approved 2026-11-24).
|
|
9
|
+
#
|
|
10
|
+
# qf1:<KEYSET>:<FINGERPRINT>:<base64url-no-pad(E || C || T)>
|
|
11
|
+
# X25519 (RFC 7748) + HKDF-SHA256 (RFC 5869, written out from HMAC) +
|
|
12
|
+
# AES-256-GCM, with the header `qf1:<KEYSET>:<FINGERPRINT>:` as the AAD.
|
|
13
|
+
#
|
|
14
|
+
# The private key is looked up by (keyset name, fingerprint) in the env var
|
|
15
|
+
# named by the keyset; keys are never tried one after another. Read side
|
|
16
|
+
# only: this SDK never encrypts.
|
|
17
|
+
#
|
|
18
|
+
# Error text follows spec 9.2: it names keysets, fingerprints and entry
|
|
19
|
+
# positions, never key material, env values, the payload or plaintext.
|
|
20
|
+
#
|
|
21
|
+
# Every input is handled as bytes (ASCII-8BIT), so an invalid-UTF-8 string
|
|
22
|
+
# is a parse failure, never an ArgumentError from a regex.
|
|
23
|
+
#
|
|
24
|
+
# @api private
|
|
25
|
+
module Qf1
|
|
26
|
+
# A reader failure. +code+ is the spec 9.1 class name.
|
|
27
|
+
class Error < StandardError
|
|
28
|
+
attr_reader :code, :keyset, :fingerprint, :held, :entry
|
|
29
|
+
|
|
30
|
+
def initialize(code, detail, keyset: nil, fingerprint: nil, held: nil, entry: nil)
|
|
31
|
+
@code = code
|
|
32
|
+
@keyset = keyset
|
|
33
|
+
@fingerprint = fingerprint
|
|
34
|
+
@held = held
|
|
35
|
+
@entry = entry
|
|
36
|
+
super("qf1 #{code}: #{detail}")
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# One held key: the public key and the parsed OpenSSL key (kept so a
|
|
41
|
+
# read does not re-parse DER). Deliberately not a Struct and with no
|
|
42
|
+
# reader for the private bytes, so nothing hands key bytes out.
|
|
43
|
+
class HeldKey
|
|
44
|
+
attr_reader :public_key, :pkey, :fingerprint
|
|
45
|
+
|
|
46
|
+
def initialize(public_key, pkey)
|
|
47
|
+
@public_key = public_key.dup.freeze
|
|
48
|
+
@pkey = pkey
|
|
49
|
+
@fingerprint = Quonfig::Qf1.fingerprint(@public_key).freeze
|
|
50
|
+
freeze
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def inspect
|
|
54
|
+
"#<Quonfig::Qf1::HeldKey fingerprint=#{@fingerprint}>"
|
|
55
|
+
end
|
|
56
|
+
alias to_s inspect
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# A cached keyset: the SHA-256 of the env value it was parsed from (not
|
|
60
|
+
# the value) and the held keys.
|
|
61
|
+
CachedKeyset = Struct.new(:digest, :held)
|
|
62
|
+
Parsed = Struct.new(:keyset, :fingerprint, :ephemeral_public_key, :ciphertext_and_tag, :aad)
|
|
63
|
+
|
|
64
|
+
HKDF_INFO = 'quonfig.com/qf1/X25519'.b.freeze
|
|
65
|
+
OKM_LENGTH = 44
|
|
66
|
+
TAG_LENGTH = 16
|
|
67
|
+
MIN_PAYLOAD = 48
|
|
68
|
+
PRIVATE_KEY_PREFIX = 'qf1-priv-'
|
|
69
|
+
ENTRY_BODY_LENGTH = 43
|
|
70
|
+
|
|
71
|
+
# Full-string matches (\A...\z): Ruby's ^ and $ are line anchors.
|
|
72
|
+
KEYSET_NAME_RE = /\AQUONFIG_KEYSET(?:_[A-Z0-9]+)*\z/n
|
|
73
|
+
KEYSET_NAME_PATTERN = '^QUONFIG_KEYSET(_[A-Z0-9]+)*$'
|
|
74
|
+
FINGERPRINT_RE = /\A[0-9a-f]{8}\z/n
|
|
75
|
+
BASE64URL_RE = /\A[A-Za-z0-9_-]+\z/n
|
|
76
|
+
OTHER_VERSION_RE = /\Aqf[1-9][0-9]*:/n
|
|
77
|
+
# Spec 6.2: strip ONLY space, tab, CR and LF (never String#strip, which
|
|
78
|
+
# also removes NUL, VT and FF).
|
|
79
|
+
ENV_TRIM_LEADING_RE = /\A[ \t\r\n]+/n
|
|
80
|
+
ENV_TRIM_TRAILING_RE = /[ \t\r\n]+\z/n
|
|
81
|
+
|
|
82
|
+
PKCS8_X25519_PREFIX = ['302e020100300506032b656e04220420'].pack('H*').freeze
|
|
83
|
+
SPKI_X25519_PREFIX = ['302a300506032b656e032100'].pack('H*').freeze
|
|
84
|
+
BASE_POINT = ("\x09".b + ("\x00".b * 31)).freeze
|
|
85
|
+
ZERO32 = ("\x00".b * 32).freeze
|
|
86
|
+
KEYSET_CACHE_MAX = 64
|
|
87
|
+
|
|
88
|
+
@keyset_cache = {}
|
|
89
|
+
@keyset_cache_mutex = Mutex.new
|
|
90
|
+
@supported = nil
|
|
91
|
+
|
|
92
|
+
module_function
|
|
93
|
+
|
|
94
|
+
# Decrypt the stored value +value+ (spec 8). +env+ answers #[] with the
|
|
95
|
+
# value of an env var or nil (ENV, a Hash). Only the env var named by the
|
|
96
|
+
# ciphertext's keyset is read, and only after that name passes spec 2.
|
|
97
|
+
# Returns a valid UTF-8 String; raises Qf1::Error.
|
|
98
|
+
def decrypt(value, env = ENV)
|
|
99
|
+
parsed = parse(value)
|
|
100
|
+
ensure_supported!
|
|
101
|
+
held = refused_by_openssl { keyset(parsed.keyset, env[parsed.keyset], needed: parsed.fingerprint) }
|
|
102
|
+
key = held[parsed.fingerprint]
|
|
103
|
+
if key.nil?
|
|
104
|
+
fps = held.keys.sort
|
|
105
|
+
raise Error.new('key_not_held',
|
|
106
|
+
"value is encrypted to #{shown_keyset(parsed.keyset)} fingerprint #{parsed.fingerprint}; " \
|
|
107
|
+
"this runtime holds #{shown_keyset(parsed.keyset)} fingerprints [#{fps.join(', ')}]",
|
|
108
|
+
keyset: parsed.keyset, fingerprint: parsed.fingerprint, held: fps)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
shared = refused_by_openssl { x25519(key.pkey, parsed.ephemeral_public_key) }
|
|
112
|
+
if shared == ZERO32
|
|
113
|
+
raise Error.new('invalid_ephemeral_key',
|
|
114
|
+
"the ephemeral key of a value on #{shown_keyset(parsed.keyset)} fingerprint #{parsed.fingerprint} " \
|
|
115
|
+
'gives an all-zero shared secret', keyset: parsed.keyset, fingerprint: parsed.fingerprint)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
derived = derive(shared, parsed.ephemeral_public_key, key.public_key)
|
|
119
|
+
plaintext = open_gcm(derived[:key], derived[:nonce], parsed.aad, parsed.ciphertext_and_tag)
|
|
120
|
+
if plaintext.nil?
|
|
121
|
+
raise Error.new('decrypt_failed',
|
|
122
|
+
"authentication failed for a value on #{shown_keyset(parsed.keyset)} fingerprint #{parsed.fingerprint}",
|
|
123
|
+
keyset: parsed.keyset, fingerprint: parsed.fingerprint)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# Strict UTF-8 (spec 3.3): reject, never replace; a leading BOM stays.
|
|
127
|
+
plaintext.force_encoding(Encoding::UTF_8)
|
|
128
|
+
unless plaintext.valid_encoding?
|
|
129
|
+
raise Error.new('decrypt_failed',
|
|
130
|
+
"the value on #{shown_keyset(parsed.keyset)} fingerprint #{parsed.fingerprint} is not valid UTF-8",
|
|
131
|
+
keyset: parsed.keyset, fingerprint: parsed.fingerprint)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
plaintext
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Spec 3.5, in order: the first failing step decides the class. Does not
|
|
138
|
+
# reject a small-order E (decrypt checks the shared secret instead).
|
|
139
|
+
def parse(value)
|
|
140
|
+
raise Error.new('malformed', 'value is not a string') unless value.is_a?(String)
|
|
141
|
+
|
|
142
|
+
text = value.b
|
|
143
|
+
unless text.start_with?('qf1:')
|
|
144
|
+
raise Error.new('unsupported_version', 'value uses a qf1 format version this SDK does not read') if text.match?(OTHER_VERSION_RE)
|
|
145
|
+
|
|
146
|
+
raise Error.new('malformed', 'value does not start with "qf1:"')
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
fields = text.split(':', -1)
|
|
150
|
+
raise Error.new('malformed', "value has #{fields.size} colon-separated fields, expected 4") unless fields.size == 4
|
|
151
|
+
|
|
152
|
+
_version, keyset_name, fp, payload_text = fields
|
|
153
|
+
unless keyset_name.match?(KEYSET_NAME_RE)
|
|
154
|
+
# Never quote it: a writer-controlled field may hold a pasted key or
|
|
155
|
+
# plaintext, and this text reaches logs.
|
|
156
|
+
raise Error.new('invalid_keyset_name',
|
|
157
|
+
"keyset name (#{keyset_name.bytesize} bytes) does not match #{KEYSET_NAME_PATTERN}")
|
|
158
|
+
end
|
|
159
|
+
raise Error.new('malformed', 'fingerprint is not 8 lowercase hex characters') unless fp.match?(FINGERPRINT_RE)
|
|
160
|
+
|
|
161
|
+
payload = decode_base64url(payload_text)
|
|
162
|
+
raise Error.new('malformed', 'payload is not canonical unpadded base64url') if payload.nil?
|
|
163
|
+
raise Error.new('malformed', "payload is #{payload.bytesize} bytes, below the #{MIN_PAYLOAD}-byte minimum") if payload.bytesize < MIN_PAYLOAD
|
|
164
|
+
|
|
165
|
+
ephemeral = payload.byteslice(0, 32)
|
|
166
|
+
raise Error.new('malformed', 'ephemeral key has its top bit set') if ephemeral.getbyte(31).anybits?(0x80)
|
|
167
|
+
|
|
168
|
+
keyset_name = keyset_name.dup.force_encoding(Encoding::US_ASCII).freeze
|
|
169
|
+
fp = fp.dup.force_encoding(Encoding::US_ASCII).freeze
|
|
170
|
+
aad = "qf1:#{keyset_name}:#{fp}:".b.freeze
|
|
171
|
+
Parsed.new(keyset_name, fp, ephemeral, payload.byteslice(32, payload.bytesize - 32), aad)
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
# Spec 6.2 behind a cache keyed by keyset NAME. Each entry stores a
|
|
175
|
+
# SHA-256 of the env value it came from, never the value: a changed value
|
|
176
|
+
# replaces the entry, and an unset, empty or unparseable one drops it, so
|
|
177
|
+
# a rotated-out private key does not stay in memory. At most
|
|
178
|
+
# KEYSET_CACHE_MAX names (writer-chosen) are kept.
|
|
179
|
+
def keyset(name, value, needed: nil)
|
|
180
|
+
if value.nil? || value.empty?
|
|
181
|
+
drop_cached_keyset(name)
|
|
182
|
+
raise Error.new('keyset_missing', missing_detail(name, needed), keyset: name) if value.nil?
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
digest = Digest::SHA256.digest(value.b)
|
|
186
|
+
cached = @keyset_cache_mutex.synchronize { @keyset_cache[name] }
|
|
187
|
+
return cached.held if cached && cached.digest == digest
|
|
188
|
+
|
|
189
|
+
begin
|
|
190
|
+
held = parse_keyset_env(name, value, needed: needed)
|
|
191
|
+
rescue Error
|
|
192
|
+
drop_cached_keyset(name)
|
|
193
|
+
raise
|
|
194
|
+
end
|
|
195
|
+
@keyset_cache_mutex.synchronize do
|
|
196
|
+
@keyset_cache.clear if @keyset_cache.size >= KEYSET_CACHE_MAX && !@keyset_cache.key?(name)
|
|
197
|
+
@keyset_cache[name.dup.freeze] = CachedKeyset.new(digest, held).freeze
|
|
198
|
+
end
|
|
199
|
+
held
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def drop_cached_keyset(name)
|
|
203
|
+
@keyset_cache_mutex.synchronize { @keyset_cache.delete(name) }
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
def clear_keyset_cache!
|
|
207
|
+
@keyset_cache_mutex.synchronize { @keyset_cache.clear }
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
def keyset_cache_size
|
|
211
|
+
@keyset_cache_mutex.synchronize { @keyset_cache.size }
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
# Parse a keyset env var value (nil = unset) into fingerprint => HeldKey.
|
|
215
|
+
def parse_keyset_env(name, value, needed: nil)
|
|
216
|
+
raise Error.new('keyset_missing', missing_detail(name, needed), keyset: name) if value.nil?
|
|
217
|
+
|
|
218
|
+
pieces = value.to_s.b.split(',', -1)
|
|
219
|
+
.map { |piece| piece.sub(ENV_TRIM_LEADING_RE, '').sub(ENV_TRIM_TRAILING_RE, '') }
|
|
220
|
+
.reject(&:empty?)
|
|
221
|
+
raise Error.new('keyset_missing', "#{shown_keyset(name)} is set but holds no keys", keyset: name) if pieces.empty?
|
|
222
|
+
|
|
223
|
+
held = {}
|
|
224
|
+
pieces.each_with_index do |piece, index|
|
|
225
|
+
private_key =
|
|
226
|
+
begin
|
|
227
|
+
parse_private_key_entry(piece)
|
|
228
|
+
rescue Error => e
|
|
229
|
+
raise unless e.code == 'invalid_key_entry'
|
|
230
|
+
|
|
231
|
+
raise Error.new('keyset_invalid', "#{shown_keyset(name)} entry #{index + 1} is not a qf1 private key",
|
|
232
|
+
keyset: name, entry: index + 1), cause: nil
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
public_key = public_key_from_private(private_key)
|
|
236
|
+
fp = fingerprint(public_key)
|
|
237
|
+
existing = held[fp]
|
|
238
|
+
if existing
|
|
239
|
+
next if existing.public_key == public_key # one key, written twice or clamp-equivalent
|
|
240
|
+
|
|
241
|
+
raise Error.new('keyset_invalid', "#{shown_keyset(name)} holds two different keys with fingerprint #{fp}",
|
|
242
|
+
keyset: name, fingerprint: fp)
|
|
243
|
+
end
|
|
244
|
+
held[fp] = HeldKey.new(public_key, private_pkey(private_key))
|
|
245
|
+
end
|
|
246
|
+
held.freeze
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
# Spec 6.1: `qf1-priv-` + exactly 43 canonical base64url characters (32
|
|
250
|
+
# bytes, stored unclamped). Returns the 32 raw bytes.
|
|
251
|
+
def parse_private_key_entry(entry)
|
|
252
|
+
text = entry.is_a?(String) ? entry.b : nil
|
|
253
|
+
raise Error.new('invalid_key_entry', "private key entry does not start with \"#{PRIVATE_KEY_PREFIX}\"") unless text&.start_with?(PRIVATE_KEY_PREFIX)
|
|
254
|
+
|
|
255
|
+
body = text.byteslice(PRIVATE_KEY_PREFIX.bytesize, text.bytesize)
|
|
256
|
+
bytes = body.bytesize == ENTRY_BODY_LENGTH ? decode_base64url(body) : nil
|
|
257
|
+
unless bytes && bytes.bytesize == 32
|
|
258
|
+
raise Error.new('invalid_key_entry',
|
|
259
|
+
"private key entry is not \"#{PRIVATE_KEY_PREFIX}\" followed by #{ENTRY_BODY_LENGTH} " \
|
|
260
|
+
'canonical base64url characters')
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
bytes
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
# Spec 3.4: unpadded RFC 4648 section 5, strictly. nil on any failure.
|
|
267
|
+
def decode_base64url(text)
|
|
268
|
+
return nil if text.empty? || !text.match?(BASE64URL_RE) || text.bytesize % 4 == 1
|
|
269
|
+
|
|
270
|
+
padded = text.tr('-_', '+/')
|
|
271
|
+
padded += '=' * ((4 - (padded.bytesize % 4)) % 4)
|
|
272
|
+
bytes = padded.unpack1('m0')
|
|
273
|
+
encode_base64url(bytes) == text ? bytes : nil
|
|
274
|
+
rescue ArgumentError
|
|
275
|
+
nil
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
def encode_base64url(bytes)
|
|
279
|
+
[bytes].pack('m0').tr('+/', '-_').delete('=')
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
# Spec 4: lowercase hex of SHA-256(pub)[0:4].
|
|
283
|
+
def fingerprint(public_key)
|
|
284
|
+
Digest::SHA256.digest(public_key).byteslice(0, 4).unpack1('H*')
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
def public_key_from_private(private_key)
|
|
288
|
+
x25519(private_key, BASE_POINT)
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
# X25519(scalar, u) per RFC 7748 (OpenSSL clamps, masks and reduces). The
|
|
292
|
+
# keys are built from DER: OpenSSL::PKey.new_raw_private_key is missing
|
|
293
|
+
# from older openssl gems. OpenSSL refuses a small-order input with
|
|
294
|
+
# "failed during derivation"; ONLY that refusal becomes 32 zero bytes, so
|
|
295
|
+
# the caller's all-zero check covers both behaviours (spec 5.2). Any other
|
|
296
|
+
# error propagates as the bug it is.
|
|
297
|
+
# +scalar+ is the 32 private bytes or an OpenSSL key from #private_pkey.
|
|
298
|
+
def x25519(scalar, u)
|
|
299
|
+
pkey = scalar.is_a?(OpenSSL::PKey::PKey)
|
|
300
|
+
raise ArgumentError, 'x25519 needs 32-byte inputs' unless (pkey || scalar.bytesize == 32) && u.bytesize == 32
|
|
301
|
+
|
|
302
|
+
private_key = pkey ? scalar : private_pkey(scalar)
|
|
303
|
+
public_key = OpenSSL::PKey.read(SPKI_X25519_PREFIX + u.b)
|
|
304
|
+
begin
|
|
305
|
+
private_key.derive(public_key).b
|
|
306
|
+
rescue OpenSSL::PKey::PKeyError => e
|
|
307
|
+
raise unless derivation_refusal?(e)
|
|
308
|
+
|
|
309
|
+
ZERO32.dup
|
|
310
|
+
end
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
def private_pkey(scalar)
|
|
314
|
+
OpenSSL::PKey.read(PKCS8_X25519_PREFIX + scalar.b)
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
# OpenSSL saying the algorithm is unsupported or disallowed (a
|
|
318
|
+
# FIPS-restricted build: "operation not supported for this keytype") is a
|
|
319
|
+
# runtime restriction, not a bad key entry. Any other PKeyError is an
|
|
320
|
+
# unexpected library failure. Neither echoes the library's text.
|
|
321
|
+
PLATFORM_REFUSAL_RE = /not supported|unsupported|disabled|disallowed|not allowed|fips|unknown algorithm|no such algorithm|fetch/i
|
|
322
|
+
|
|
323
|
+
def refused_by_openssl
|
|
324
|
+
yield
|
|
325
|
+
rescue OpenSSL::PKey::PKeyError => e
|
|
326
|
+
if e.message.match?(PLATFORM_REFUSAL_RE)
|
|
327
|
+
raise Error.new('unsupported_platform',
|
|
328
|
+
'OpenSSL refused an X25519 operation in this runtime (for example a FIPS-restricted ' \
|
|
329
|
+
'build); the key entries were not checked'), cause: nil
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
raise Error.new('decrypt_failed', 'unexpected error from the crypto library'), cause: nil
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
def derivation_refusal?(error)
|
|
336
|
+
error.is_a?(OpenSSL::PKey::PKeyError) && error.message.include?('failed during derivation')
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
# Spec 5.3: salt = E || pub_r (E as transmitted), PRK = HMAC(salt, S),
|
|
340
|
+
# OKM = Expand(PRK, INFO, 44), K = OKM[0:32], N = OKM[32:44].
|
|
341
|
+
def derive(shared, ephemeral_public_key, recipient_public_key)
|
|
342
|
+
salt = ephemeral_public_key.b + recipient_public_key.b
|
|
343
|
+
prk = hkdf_extract(salt, shared)
|
|
344
|
+
okm = hkdf_expand(prk, HKDF_INFO, OKM_LENGTH)
|
|
345
|
+
{ salt: salt, prk: prk, okm: okm, key: okm.byteslice(0, 32), nonce: okm.byteslice(32, 12) }
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
# RFC 5869 2.2 Extract, written out from HMAC (not OpenSSL::KDF, which is
|
|
349
|
+
# absent on LibreSSL builds).
|
|
350
|
+
def hkdf_extract(salt, ikm)
|
|
351
|
+
salt = ("\x00".b * 32) if salt.empty?
|
|
352
|
+
OpenSSL::HMAC.digest('SHA256', salt, ikm)
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
# RFC 5869 2.3 Expand: T(i) = HMAC(PRK, T(i-1) || info || i).
|
|
356
|
+
def hkdf_expand(prk, info, length)
|
|
357
|
+
raise ArgumentError, 'HKDF length out of range' if length.negative? || length > 255 * 32
|
|
358
|
+
|
|
359
|
+
okm = ''.b
|
|
360
|
+
t = ''.b
|
|
361
|
+
i = 1
|
|
362
|
+
while okm.bytesize < length
|
|
363
|
+
t = OpenSSL::HMAC.digest('SHA256', prk, t + info.b + i.chr.b)
|
|
364
|
+
okm << t
|
|
365
|
+
i += 1
|
|
366
|
+
end
|
|
367
|
+
okm.byteslice(0, length)
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
# AES-256-GCM open. nil when the tag does not verify; no plaintext is
|
|
371
|
+
# returned before it does.
|
|
372
|
+
def open_gcm(key, nonce, aad, ciphertext_and_tag)
|
|
373
|
+
ciphertext = ciphertext_and_tag.byteslice(0, ciphertext_and_tag.bytesize - TAG_LENGTH)
|
|
374
|
+
tag = ciphertext_and_tag.byteslice(ciphertext_and_tag.bytesize - TAG_LENGTH, TAG_LENGTH)
|
|
375
|
+
cipher = OpenSSL::Cipher.new('aes-256-gcm').decrypt
|
|
376
|
+
cipher.key = key
|
|
377
|
+
cipher.iv = nonce
|
|
378
|
+
cipher.auth_tag = tag
|
|
379
|
+
cipher.auth_data = aad
|
|
380
|
+
# Cipher#update refuses an empty string; the empty plaintext is valid.
|
|
381
|
+
out = ciphertext.empty? ? ''.b : cipher.update(ciphertext)
|
|
382
|
+
out << cipher.final
|
|
383
|
+
out
|
|
384
|
+
rescue OpenSSL::Cipher::CipherError
|
|
385
|
+
nil
|
|
386
|
+
end
|
|
387
|
+
|
|
388
|
+
# True when this OpenSSL build does X25519 through OpenSSL::PKey (Ruby
|
|
389
|
+
# 3.1+ with the openssl gem >= 3.0 on OpenSSL 1.1.1+). Checked lazily, at
|
|
390
|
+
# the first secret read, so a build without it never fails at require.
|
|
391
|
+
def x25519_supported?
|
|
392
|
+
return @supported unless @supported.nil?
|
|
393
|
+
|
|
394
|
+
@supported =
|
|
395
|
+
begin
|
|
396
|
+
OpenSSL::PKey::PKey.method_defined?(:derive) &&
|
|
397
|
+
fingerprint(public_key_from_private("\x01".b * 32)).match?(FINGERPRINT_RE)
|
|
398
|
+
rescue StandardError
|
|
399
|
+
false
|
|
400
|
+
end
|
|
401
|
+
end
|
|
402
|
+
|
|
403
|
+
def ensure_supported!
|
|
404
|
+
return if x25519_supported?
|
|
405
|
+
|
|
406
|
+
raise Error.new('unsupported_platform',
|
|
407
|
+
'reading secrets needs X25519 in OpenSSL::PKey: Ruby >= 3.1 with the openssl gem >= 3.0 ' \
|
|
408
|
+
'on OpenSSL 1.1.1 or later, with X25519 allowed (a FIPS-restricted OpenSSL refuses it); ' \
|
|
409
|
+
"this runtime (Ruby #{RUBY_VERSION}, openssl gem #{OpenSSL::VERSION}, " \
|
|
410
|
+
"#{OpenSSL::OPENSSL_LIBRARY_VERSION}) cannot. The key entries were not checked")
|
|
411
|
+
end
|
|
412
|
+
|
|
413
|
+
def missing_detail(name, needed)
|
|
414
|
+
return "this runtime has no #{shown_keyset(name)}" if needed.nil?
|
|
415
|
+
|
|
416
|
+
"value is encrypted to #{shown_keyset(name)} fingerprint #{needed}; this runtime has no #{shown_keyset(name)}"
|
|
417
|
+
end
|
|
418
|
+
|
|
419
|
+
# A keyset name as error text shows it: a valid name truncated to 64
|
|
420
|
+
# characters; anything else only by its length (never its content).
|
|
421
|
+
def shown_keyset(name)
|
|
422
|
+
text = name.to_s.b
|
|
423
|
+
return "<invalid keyset name, #{text.bytesize} bytes>" unless text.match?(KEYSET_NAME_RE)
|
|
424
|
+
return text if text.bytesize <= 64
|
|
425
|
+
|
|
426
|
+
"#{text.byteslice(0, 64)}..."
|
|
427
|
+
end
|
|
428
|
+
end
|
|
429
|
+
end
|
data/lib/quonfig/resolver.rb
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'digest'
|
|
4
|
-
require 'set'
|
|
5
4
|
|
|
6
5
|
module Quonfig
|
|
7
6
|
# Public-API resolver: looks up a config by key in a ConfigStore and runs
|
|
@@ -47,9 +46,9 @@ module Quonfig
|
|
|
47
46
|
# the legacy "return nil if absent" shape can rescue and recover (see
|
|
48
47
|
# IntegrationTestHelpers.assert_resolved, which folds a missing-key
|
|
49
48
|
# raise into the test's expected default).
|
|
50
|
-
# The whole lookup reads one store snapshot (qfg-i86q): the flag
|
|
51
|
-
# segments it references
|
|
52
|
-
#
|
|
49
|
+
# The whole lookup reads one store snapshot (qfg-i86q): the flag and the
|
|
50
|
+
# segments it references all come from the same envelope even if an
|
|
51
|
+
# install lands mid-evaluation.
|
|
53
52
|
def get(key, context = nil)
|
|
54
53
|
return get_unpinned(key, context) unless @store.respond_to?(:pinned)
|
|
55
54
|
|
|
@@ -58,8 +57,10 @@ module Quonfig
|
|
|
58
57
|
|
|
59
58
|
# Post-evaluation value resolution. Mirrors sdk-node Resolver#resolveValue
|
|
60
59
|
# and sdk-go resolver.Resolve:
|
|
60
|
+
# - a value of a secret (type "secret", from secrets/) → qf1 decrypt
|
|
61
61
|
# - "provided" + ENV_VAR → read ENV[lookup], coerce to config's valueType
|
|
62
|
-
# -
|
|
62
|
+
# - a value carrying decryptWith → DecryptionError (the old format is no
|
|
63
|
+
# longer read; spec 13.3 R1)
|
|
63
64
|
# - everything else passes through unchanged
|
|
64
65
|
def resolve_value(value, config, context = nil, &on_weighted_index)
|
|
65
66
|
resolve_value_on_path(value, config, context, [], &on_weighted_index)
|
|
@@ -88,9 +89,12 @@ module Quonfig
|
|
|
88
89
|
weighted_index = idx
|
|
89
90
|
end
|
|
90
91
|
# Redact from the selected rollout variant, not only the outer
|
|
91
|
-
# weighted_values wrapper (which is never flagged), so a confidential
|
|
92
|
-
#
|
|
93
|
-
|
|
92
|
+
# weighted_values wrapper (which is never flagged), so a confidential
|
|
93
|
+
# variant never reaches telemetry in plaintext (qfg-goi1.2.39). A
|
|
94
|
+
# secret is always redacted, flagged confidential or not.
|
|
95
|
+
secret = secret_config?(config)
|
|
96
|
+
reportable = redacted_reportable_value(eval_result.value, force: secret) ||
|
|
97
|
+
redacted_reportable_value(selected_variants.last, force: secret)
|
|
94
98
|
EvalResult.new(
|
|
95
99
|
value: resolved_value,
|
|
96
100
|
rule_index: eval_result.rule_index,
|
|
@@ -101,18 +105,22 @@ module Quonfig
|
|
|
101
105
|
)
|
|
102
106
|
end
|
|
103
107
|
|
|
104
|
-
# resolve_value plus +key_path
|
|
105
|
-
#
|
|
106
|
-
# DecryptionError instead of recursing until SystemStackError
|
|
107
|
-
# (qfg-9dxb.7, matching sdk-go qfg-9dxb.4). +hash_missing+, when given,
|
|
108
|
+
# resolve_value plus +key_path+ (kept for the call shape; nothing
|
|
109
|
+
# recurses across configs since decryptWith is gone). +hash_missing+, when given,
|
|
108
110
|
# gets an entry for each weighted rollout whose hash property was missing
|
|
109
111
|
# from the context (qfg-9dxb.8). +selected_variants+, when given, gets the
|
|
110
112
|
# stored (pre-resolution) Value of each weighted variant picked, so get()
|
|
111
|
-
# can redact a confidential
|
|
113
|
+
# can redact a confidential variant (qfg-goi1.2.39).
|
|
112
114
|
def resolve_value_on_path(value, config, context, key_path, hash_missing = nil, selected_variants = nil,
|
|
113
115
|
&on_weighted_index)
|
|
114
116
|
return nil if value.nil?
|
|
115
117
|
|
|
118
|
+
reject_decrypt_with!(value, config)
|
|
119
|
+
|
|
120
|
+
# A secret's value is decrypted whatever else it carries: the directory
|
|
121
|
+
# (type "secret"), not the value, marks it as encrypted (spec 10.2).
|
|
122
|
+
return resolve_secret(value, config) if secret_config?(config)
|
|
123
|
+
|
|
116
124
|
type = vget(value, :type, 'type')
|
|
117
125
|
|
|
118
126
|
return resolve_provided(value, config) if type == 'provided'
|
|
@@ -122,14 +130,23 @@ module Quonfig
|
|
|
122
130
|
&on_weighted_index)
|
|
123
131
|
end
|
|
124
132
|
|
|
125
|
-
|
|
133
|
+
value
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# Spec 13.3 R1: a value carrying decryptWith (anything but absent, nil or
|
|
137
|
+
# "": a string, number, boolean, array or hash) is never returned and
|
|
138
|
+
# never triggers a key-config read. Checked before every other branch.
|
|
139
|
+
def reject_decrypt_with!(value, config)
|
|
140
|
+
return unless value.is_a?(Hash)
|
|
141
|
+
|
|
126
142
|
decrypt_with = vget(value, :decryptWith, 'decryptWith', :decrypt_with, 'decrypt_with')
|
|
127
|
-
if
|
|
128
|
-
return resolve_decryption(value, config, context,
|
|
129
|
-
decrypt_with, key_path)
|
|
130
|
-
end
|
|
143
|
+
return if decrypt_with.nil? || decrypt_with == ''
|
|
131
144
|
|
|
132
|
-
|
|
145
|
+
raise Quonfig::Errors::DecryptionError.new(
|
|
146
|
+
config_key(config),
|
|
147
|
+
'value carries decryptWith (the old secrets format), which this SDK no longer reads; ' \
|
|
148
|
+
'move it to secrets/ as a qf1 secret'
|
|
149
|
+
)
|
|
133
150
|
end
|
|
134
151
|
|
|
135
152
|
# If +value+ is confidential or has a decryptWith key, return the
|
|
@@ -139,12 +156,13 @@ module Quonfig
|
|
|
139
156
|
# plaintext-as-stored when only `confidential: true`) — matches
|
|
140
157
|
# ReforgeHQ/sdk-ruby ConfigValueUnwrapper#reportable_wrapped_value
|
|
141
158
|
# (CONFIDENTIAL_PREFIX + first 5 chars of MD5).
|
|
142
|
-
|
|
159
|
+
# +force+ redacts whatever the value's flags say (a secret).
|
|
160
|
+
def redacted_reportable_value(value, force: false)
|
|
143
161
|
return nil if value.nil?
|
|
144
162
|
|
|
145
163
|
confidential = vget(value, :confidential, 'confidential')
|
|
146
164
|
decrypt_with = vget(value, :decryptWith, 'decryptWith', :decrypt_with, 'decrypt_with')
|
|
147
|
-
return nil unless confidential || (decrypt_with && !decrypt_with.to_s.empty?)
|
|
165
|
+
return nil unless force || confidential || (decrypt_with && !decrypt_with.to_s.empty?)
|
|
148
166
|
|
|
149
167
|
raw = vget(value, :value, 'value')
|
|
150
168
|
return nil if raw.nil?
|
|
@@ -247,43 +265,25 @@ module Quonfig
|
|
|
247
265
|
'which is missing from context; hashing an empty value instead'
|
|
248
266
|
end
|
|
249
267
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
key_path += [config_key(config).to_s]
|
|
254
|
-
if key_path.include?(decrypt_with.to_s)
|
|
255
|
-
raise Quonfig::Errors::DecryptionError.new(
|
|
256
|
-
config_key(config), %(decryption key config "#{decrypt_with}" is part of a decryptWith cycle)
|
|
257
|
-
)
|
|
258
|
-
end
|
|
259
|
-
|
|
260
|
-
# A missing, unmatched or empty key is a decryption failure too, so the
|
|
261
|
-
# typed getters apply the caller's default (qfg-zvyy). DecryptionError is
|
|
262
|
-
# a Quonfig::Error, so rescuing Quonfig::Error still catches it.
|
|
263
|
-
key_cfg = @store.get(decrypt_with)
|
|
264
|
-
if key_cfg.nil?
|
|
265
|
-
raise Quonfig::Errors::DecryptionError.new(config_key(config),
|
|
266
|
-
%(decryption key config "#{decrypt_with}" not found))
|
|
267
|
-
end
|
|
268
|
-
|
|
269
|
-
key_match = @evaluator.evaluate_config(key_cfg, context, resolver: self)
|
|
270
|
-
if key_match.nil?
|
|
271
|
-
raise Quonfig::Errors::DecryptionError.new(config_key(config),
|
|
272
|
-
%(decryption key config "#{decrypt_with}" did not match))
|
|
273
|
-
end
|
|
274
|
-
|
|
275
|
-
resolved_key = resolve_value_on_path(key_match.value, key_cfg, context, key_path)
|
|
276
|
-
secret_key = vget(resolved_key, :value, 'value').to_s
|
|
277
|
-
if secret_key.empty?
|
|
278
|
-
raise Quonfig::Errors::DecryptionError.new(config_key(config),
|
|
279
|
-
%(decryption key from "#{decrypt_with}" is empty))
|
|
280
|
-
end
|
|
268
|
+
def secret_config?(config)
|
|
269
|
+
!config.nil? && vget(config, :type, 'type').to_s == 'secret'
|
|
270
|
+
end
|
|
281
271
|
|
|
282
|
-
|
|
272
|
+
# Decrypt a secret's rule value with the qf1 reader (spec 8), lazily on
|
|
273
|
+
# get. The keyset env var is read here, never at startup, so a missing
|
|
274
|
+
# keyset fails only this value (spec 9.3). The DecryptionError carries the
|
|
275
|
+
# qf1 class, keyset and fingerprints only; its cause is the Qf1::Error,
|
|
276
|
+
# whose text follows the same rules.
|
|
277
|
+
def resolve_secret(value, config)
|
|
278
|
+
stored = vget(value, :value, 'value')
|
|
279
|
+
type = vget(value, :type, 'type')
|
|
283
280
|
begin
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
281
|
+
# The writer-controlled type is not echoed.
|
|
282
|
+
raise Quonfig::Qf1::Error.new('malformed', "a secret's rule value is not a string value") unless type.to_s == 'string'
|
|
283
|
+
|
|
284
|
+
plaintext = Quonfig::Qf1.decrypt(stored, ENV)
|
|
285
|
+
rescue Quonfig::Qf1::Error => e
|
|
286
|
+
raise Quonfig::Errors::DecryptionError.new(config_key(config), e)
|
|
287
287
|
end
|
|
288
288
|
|
|
289
289
|
{
|
|
@@ -75,8 +75,8 @@ module Quonfig
|
|
|
75
75
|
|
|
76
76
|
def normalize_level(level)
|
|
77
77
|
case level
|
|
78
|
-
when Symbol
|
|
79
|
-
when String
|
|
78
|
+
when Symbol then level.downcase
|
|
79
|
+
when String then level.downcase.to_sym
|
|
80
80
|
when Integer
|
|
81
81
|
# LogLevel ints from old proto: 1=trace … 9=fatal. Best-effort map.
|
|
82
82
|
case level
|
data/lib/quonfig/version.rb
CHANGED
data/lib/quonfig.rb
CHANGED
data/quonfig.gemspec
CHANGED
|
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
|
|
|
11
11
|
s.description = 'Quonfig — feature flags and live config, stored as files in git.'
|
|
12
12
|
s.homepage = 'https://github.com/quonfig/sdk-ruby'
|
|
13
13
|
s.license = 'MIT'
|
|
14
|
-
s.required_ruby_version = '>= 3.
|
|
14
|
+
s.required_ruby_version = '>= 3.2'
|
|
15
15
|
|
|
16
16
|
s.metadata = {
|
|
17
17
|
'source_code_uri' => 'https://github.com/quonfig/sdk-ruby',
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: quonfig
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.
|
|
4
|
+
version: 2.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jeff Dwyer
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-11-30 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activesupport
|
|
@@ -120,6 +120,7 @@ files:
|
|
|
120
120
|
- lib/quonfig/murmer3.rb
|
|
121
121
|
- lib/quonfig/options.rb
|
|
122
122
|
- lib/quonfig/periodic_sync.rb
|
|
123
|
+
- lib/quonfig/qf1.rb
|
|
123
124
|
- lib/quonfig/quonfig.rb
|
|
124
125
|
- lib/quonfig/rate_limit_cache.rb
|
|
125
126
|
- lib/quonfig/reason.rb
|
|
@@ -157,7 +158,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
157
158
|
requirements:
|
|
158
159
|
- - ">="
|
|
159
160
|
- !ruby/object:Gem::Version
|
|
160
|
-
version: '3.
|
|
161
|
+
version: '3.2'
|
|
161
162
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
162
163
|
requirements:
|
|
163
164
|
- - ">="
|