envpact 0.2.0 → 0.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 +21 -0
- data/README.md +82 -65
- data/lib/envpact/cli/check.rb +2 -2
- data/lib/envpact/cli/command.rb +5 -4
- data/lib/envpact/cli/convert.rb +39 -0
- data/lib/envpact/cli/lint.rb +2 -2
- data/lib/envpact/cli/login.rb +1 -1
- data/lib/envpact/cli/pull.rb +2 -2
- data/lib/envpact/cli/run.rb +1 -1
- data/lib/envpact/cli/scan_prompt.rb +2 -2
- data/lib/envpact/cli/set.rb +1 -1
- data/lib/envpact/cli.rb +2 -0
- data/lib/envpact/entry.rb +1 -1
- data/lib/envpact/parser.rb +5 -2
- data/lib/envpact/scan_prompt.md +16 -11
- data/lib/envpact/source.rb +1 -1
- data/lib/envpact/syntax.md +50 -43
- data/lib/envpact/version.rb +1 -1
- data/lib/envpact/write_flow.rb +2 -2
- data/lib/envpact/yaml_parser.rb +231 -0
- data/lib/envpact/yaml_writer.rb +84 -0
- data/lib/envpact.rb +2 -0
- metadata +7 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: be245c5488740fdd3b7fc0ee40a570636b4dac28d09fe30d4baef258155587d7
|
|
4
|
+
data.tar.gz: c49b7db0b33d5e155a44085026340e4754655b0f97f51626ca00d30d8a01449a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2e2297a1c2e4e1c518b769854e8161329114d9bbc348b037cadcc41f00abdd5bd6210c78013acc0a4558dfdf85fd96621816e4c14f99485f6baff4101c89296b
|
|
7
|
+
data.tar.gz: 4ad46d3b1bc3981a9d0b9e7026f5140134754631de5914f9d404350678009a60f885ca8b64354c2715cb7476f11852faa156d5aac458411ae4c4cc71a90d42e5
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.3.0 — 2026-11-23
|
|
4
|
+
|
|
5
|
+
- **The contract is now `envpact.yml`**, real YAML, instead of the `env.spec`
|
|
6
|
+
DSL: `version: 1`, `source`, `vars` (ConfigMap), `secrets` and `envs`. A key's
|
|
7
|
+
value is empty (required string), a type (`int`, `url(https)`,
|
|
8
|
+
`enum(a, b)`…) or `{type, default, optional, description}`. The model is the
|
|
9
|
+
same, so `configmap_for`, `secrets_for`, `source_for` and `check` are
|
|
10
|
+
unchanged.
|
|
11
|
+
- YAML traps are closed: every value is read as the raw text of its node
|
|
12
|
+
(`no`, `on`, `0.30`, `0800` stay strings); anchors, aliases and tags are
|
|
13
|
+
refused; errors give the line and never echo a value.
|
|
14
|
+
- **`env.spec` is still read**, with a warning, when there is no
|
|
15
|
+
`envpact.yml`. Both files at once is an error. `envpact convert` writes the
|
|
16
|
+
equivalent `envpact.yml` (never overwrites; comments are not carried over).
|
|
17
|
+
- New `description` field (`Entry#description`).
|
|
18
|
+
- API: `EnvPact.locate(dir)`, `EnvPact.parse_yaml(text)`,
|
|
19
|
+
`EnvPact#to_yaml_contract`; `EnvPact.parse_file` picks the format by
|
|
20
|
+
extension.
|
|
21
|
+
- Messages no longer name `env.spec`: "no source for env 'x' in the contract".
|
|
22
|
+
- `scan-prompt` now asks the agent for `envpact.yml`.
|
|
23
|
+
|
|
3
24
|
## 0.2.0 — 2026-11-22
|
|
4
25
|
|
|
5
26
|
envpact now talks to [OpenBao](https://openbao.org) (KV v2) and keeps the
|
data/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# envpact
|
|
2
2
|
|
|
3
|
-
An env var contract for apps. One file, `
|
|
3
|
+
An env var contract for apps. One file, `envpact.yml`, declares which env vars an
|
|
4
4
|
app needs — names, types, required/optional, defaults, Secret vs ConfigMap, and
|
|
5
5
|
per-env differences. It never holds values. The same contract is checked at
|
|
6
6
|
three moments:
|
|
@@ -28,11 +28,12 @@ gem install envpact
|
|
|
28
28
|
## Commands
|
|
29
29
|
|
|
30
30
|
```sh
|
|
31
|
-
envpact lint # syntax check ./
|
|
31
|
+
envpact lint # syntax check ./envpact.yml
|
|
32
32
|
envpact check --env=test # check .env, .env.test, .env.test.local
|
|
33
33
|
envpact check --env=x --from-env # check the current process ENV instead
|
|
34
34
|
envpact exec --env=production -- bin/rails server # boot check, then exec
|
|
35
|
-
envpact scan-prompt | claude # have an agent write
|
|
35
|
+
envpact scan-prompt | claude # have an agent write envpact.yml for this repo
|
|
36
|
+
envpact convert # legacy ./env.spec -> ./envpact.yml
|
|
36
37
|
|
|
37
38
|
# secrets in OpenBao (0.2, see "Secrets with OpenBao")
|
|
38
39
|
envpact login --read # log in once a month, token stored encrypted
|
|
@@ -44,12 +45,13 @@ envpact set --env=production STRIPE_KEY # write one secret
|
|
|
44
45
|
envpact status | envpact lock
|
|
45
46
|
```
|
|
46
47
|
|
|
47
|
-
- The contract path defaults to `./
|
|
48
|
-
`$ENVPACT_SPEC`.
|
|
48
|
+
- The contract path defaults to `./envpact.yml`; override with `--spec=PATH` or
|
|
49
|
+
`$ENVPACT_SPEC`. A legacy `./env.spec` is still read, with a warning; both
|
|
50
|
+
files at once is an error.
|
|
49
51
|
- `--env` is always required (`exec` also reads `$ENVPACT_ENV`). Any env name
|
|
50
|
-
is valid: an env
|
|
52
|
+
is valid: an env with no entry under `envs` gets only the shared keys, and
|
|
51
53
|
every check says which sections it applied:
|
|
52
|
-
`✓
|
|
54
|
+
`✓ envpact.yml satisfied (env: prod — shared only; sections in spec: production)`.
|
|
53
55
|
- `check` reads, in order (later overrides earlier): `.env`, `.env.<env>`,
|
|
54
56
|
`.env.local` (skipped for `test`), `.env.<env>.local`. No interpolation, no
|
|
55
57
|
multiline values.
|
|
@@ -67,17 +69,22 @@ infisical run --env=dev -- envpact exec --env=development -- bin/rails s
|
|
|
67
69
|
|
|
68
70
|
## Secrets with OpenBao
|
|
69
71
|
|
|
70
|
-
The `
|
|
71
|
-
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
openbao
|
|
75
|
-
|
|
72
|
+
The `source` of `envpact.yml` says where each env's secrets live (KV v2):
|
|
73
|
+
|
|
74
|
+
```yaml
|
|
75
|
+
source:
|
|
76
|
+
openbao:
|
|
77
|
+
path: "team/myapp/{env}"
|
|
78
|
+
envs:
|
|
79
|
+
production:
|
|
80
|
+
source:
|
|
81
|
+
openbao:
|
|
82
|
+
path: "cluster/selly/myapp-web"
|
|
76
83
|
```
|
|
77
84
|
|
|
78
|
-
The server address is `addr
|
|
85
|
+
The server address is the source's `addr`, else `$BAO_ADDR`. A token is only ever
|
|
79
86
|
sent to the address it was issued for; a spec naming another address is refused.
|
|
80
|
-
The source holds only the keys declared under `
|
|
87
|
+
The source holds only the keys declared under `secrets`; ConfigMap values keep
|
|
81
88
|
coming from `.env*`, `app.yml` and defaults.
|
|
82
89
|
|
|
83
90
|
| Command | What it does |
|
|
@@ -113,68 +120,78 @@ keep it away from the token files (`.claude/settings.json`):
|
|
|
113
120
|
}
|
|
114
121
|
```
|
|
115
122
|
|
|
116
|
-
##
|
|
117
|
-
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
123
|
+
## envpact.yml syntax
|
|
124
|
+
|
|
125
|
+
```yaml
|
|
126
|
+
version: 1
|
|
127
|
+
|
|
128
|
+
source: # where the secrets live; {env} becomes the env name
|
|
129
|
+
openbao:
|
|
130
|
+
path: "team/myapp/{env}"
|
|
131
|
+
# addr: "https://bao.example.com" # optional
|
|
132
|
+
# mount: secret # optional, default secret
|
|
133
|
+
|
|
134
|
+
vars: # ConfigMap keys, shared by every env
|
|
135
|
+
APP_NAME: # empty value = required string
|
|
136
|
+
LOG_LEVEL: {type: "enum(debug, info, warn, error)", default: "info"}
|
|
137
|
+
DEBUG: {type: bool, optional: true}
|
|
138
|
+
PORT: {type: int, default: "3000", description: "HTTP port"}
|
|
139
|
+
PUBLIC_URL: url
|
|
140
|
+
|
|
141
|
+
secrets: # Secret keys, shared by every env
|
|
142
|
+
DB_PASS:
|
|
143
|
+
OPENAI_API_KEY:
|
|
144
|
+
|
|
145
|
+
envs: # only for what differs per env
|
|
146
|
+
production:
|
|
147
|
+
source:
|
|
148
|
+
openbao:
|
|
149
|
+
path: "cluster/selly/myapp-web"
|
|
150
|
+
vars:
|
|
151
|
+
SENTRY_DSN: url
|
|
152
|
+
secrets:
|
|
153
|
+
STRIPE_LIVE_KEY:
|
|
154
|
+
"production, staging": # one key, several envs
|
|
155
|
+
vars:
|
|
156
|
+
CDN_HOST:
|
|
144
157
|
```
|
|
145
158
|
|
|
146
|
-
- `
|
|
147
|
-
-
|
|
148
|
-
-
|
|
159
|
+
- `version: 1` is required. Unknown keys and fields are errors.
|
|
160
|
+
- A key's value is one of:
|
|
161
|
+
- empty — required string;
|
|
162
|
+
- a type — `str` | `int` | `bool` | `url` | `url(a, b)` | `enum(a, b, c)`;
|
|
163
|
+
- a mapping with `type`, `default`, `optional` (`true`/`false`) and
|
|
164
|
+
`description`.
|
|
165
|
+
- Types:
|
|
149
166
|
- `bool` accepts `true false 1 0 yes no` (any case).
|
|
150
167
|
- `url` needs a scheme and a host: `postgres://u:p@h:5432/db`, `https://x.y`.
|
|
151
168
|
- `url(postgres, redis)` also restricts the scheme (case-insensitive).
|
|
152
|
-
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
-
|
|
160
|
-
|
|
161
|
-
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
declared in overlapping scopes, may not be both ConfigMap and Secret, and must
|
|
165
|
-
have the same type in every env.
|
|
169
|
+
- Inside `{...}` a comma splits fields: quote `enum(...)`/`url(...)` there,
|
|
170
|
+
as in `{type: "enum(a, b)"}`. Quote paths with `{env}` too.
|
|
171
|
+
- `default` is ConfigMap only; secrets cannot have defaults. It must be valid
|
|
172
|
+
for the type (`{type: int, default: "3OOO"}` is a lint error).
|
|
173
|
+
- Every value is read as plain text: `no`, `on`, `0.30` and `0800` stay
|
|
174
|
+
strings, never YAML booleans or numbers. Quote to keep `#` or spaces.
|
|
175
|
+
- Anchors, aliases and tags (`&x`, `*x`, `!!str`) are not allowed.
|
|
176
|
+
- Key names are `A-Z`, `0-9` and `_`, starting with a letter. Env names are
|
|
177
|
+
`[A-Za-z0-9_-]+`.
|
|
178
|
+
- An env with no entry under `envs` gets only the shared keys. A key may not be
|
|
179
|
+
declared both shared and per env, may not be both ConfigMap and Secret, and
|
|
180
|
+
must have the same type in every env.
|
|
166
181
|
- Empty values count as absent.
|
|
167
|
-
- `
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
- After `[source]` any other header resumes keys; `[secrets]` there applies to shared.
|
|
182
|
+
- `source` at the top is the fallback; `envs.<name>.source` wins for that env.
|
|
183
|
+
- The legacy `env.spec` format is still read, with a warning; `envpact convert`
|
|
184
|
+
writes the equivalent `envpact.yml`.
|
|
171
185
|
|
|
172
186
|
## Library API
|
|
173
187
|
|
|
174
188
|
```ruby
|
|
175
189
|
require "envpact"
|
|
176
190
|
|
|
177
|
-
|
|
191
|
+
path, legacy = EnvPact.locate(Dir.pwd) # ["…/envpact.yml", false] or ["…/env.spec", true]
|
|
192
|
+
spec = EnvPact.parse_file(path) # YAML for .yml/.yaml, legacy DSL otherwise
|
|
193
|
+
spec = EnvPact.parse_yaml(text) # or EnvPact.parse(text) for env.spec text
|
|
194
|
+
spec.to_yaml_contract # the contract as envpact.yml text
|
|
178
195
|
spec.envs # env names that have sections
|
|
179
196
|
spec.configmap_for("production") # shared + production, {key => Entry}
|
|
180
197
|
spec.secrets_for("production")
|
data/lib/envpact/cli/check.rb
CHANGED
|
@@ -4,10 +4,10 @@ class EnvPact
|
|
|
4
4
|
class CLI
|
|
5
5
|
class Check < Command
|
|
6
6
|
command_name "check"
|
|
7
|
-
summary "Check .env files (or process ENV) against
|
|
7
|
+
summary "Check .env files (or process ENV) against the contract (envpact.yml)"
|
|
8
8
|
banner "Usage: envpact check --env=NAME [options]"
|
|
9
9
|
description <<~DESC
|
|
10
|
-
Validates values against
|
|
10
|
+
Validates values against the contract (envpact.yml) for one env. By default reads the
|
|
11
11
|
dotenv files for that env from the current directory
|
|
12
12
|
(.env, .env.<env>, .env.local, .env.<env>.local; .env.local is skipped
|
|
13
13
|
for test). Secret values are never printed unless --show-values.
|
data/lib/envpact/cli/command.rb
CHANGED
|
@@ -13,7 +13,7 @@ class EnvPact
|
|
|
13
13
|
# - configure(parser, opts) — define options on the OptionParser
|
|
14
14
|
# - call(argv, opts) — execute; return exit code
|
|
15
15
|
class Command
|
|
16
|
-
DEFAULT_SPEC =
|
|
16
|
+
DEFAULT_SPEC = EnvPact::YAML_FILE
|
|
17
17
|
|
|
18
18
|
class << self
|
|
19
19
|
def command_name(value = nil)
|
|
@@ -95,16 +95,17 @@ class EnvPact
|
|
|
95
95
|
end
|
|
96
96
|
|
|
97
97
|
def spec_option(o, opts)
|
|
98
|
-
o.on("--spec=PATH", "Contract file (default: $ENVPACT_SPEC or ./#{
|
|
98
|
+
o.on("--spec=PATH", "Contract file (default: $ENVPACT_SPEC, ./#{DEFAULT_SPEC}, or the legacy ./#{EnvPact::LEGACY_FILE})") { |v| opts[:spec] = v }
|
|
99
99
|
end
|
|
100
100
|
|
|
101
101
|
def spec_path(opts)
|
|
102
|
-
opts[:spec] || ENV["ENVPACT_SPEC"] ||
|
|
102
|
+
opts[:spec] || ENV["ENVPACT_SPEC"] || EnvPact.locate(".").first
|
|
103
103
|
end
|
|
104
104
|
|
|
105
105
|
def load_spec(opts)
|
|
106
106
|
path = spec_path(opts)
|
|
107
107
|
raise EnvPact::Error, "spec not found: #{path}" unless File.file?(path)
|
|
108
|
+
warn "envpact: #{EnvPact::LEGACY_WARNING}" if File.basename(path) == EnvPact::LEGACY_FILE
|
|
108
109
|
EnvPact.parse_file(path)
|
|
109
110
|
end
|
|
110
111
|
|
|
@@ -114,7 +115,7 @@ class EnvPact
|
|
|
114
115
|
# found in the source go to warnings.
|
|
115
116
|
def fetch_source(spec, env, warnings: nil)
|
|
116
117
|
ref = spec.source_for(env)
|
|
117
|
-
raise EnvPact::Error, "no
|
|
118
|
+
raise EnvPact::Error, "no source for env '#{env}' in the contract" if ref.nil?
|
|
118
119
|
client = Credentials.new(prompt: @prompt).read_client(ref.addr)
|
|
119
120
|
values = Source.fetch(spec, env: env, client: client, warnings: warnings)
|
|
120
121
|
begin
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
require_relative "command"
|
|
2
|
+
|
|
3
|
+
class EnvPact
|
|
4
|
+
class CLI
|
|
5
|
+
class Convert < Command
|
|
6
|
+
command_name "convert"
|
|
7
|
+
summary "Write envpact.yml from a legacy env.spec"
|
|
8
|
+
banner "Usage: envpact convert [--spec=PATH] [--out=PATH]"
|
|
9
|
+
description <<~TXT
|
|
10
|
+
Reads an env.spec (default ./env.spec) and writes the same contract as
|
|
11
|
+
envpact.yml next to it. Never overwrites; comments are not carried over.
|
|
12
|
+
Review the result, then delete the env.spec: with both files present
|
|
13
|
+
every command refuses to run.
|
|
14
|
+
TXT
|
|
15
|
+
|
|
16
|
+
def configure(o, opts)
|
|
17
|
+
o.on("--spec=PATH", "The env.spec to read (default: ./#{EnvPact::LEGACY_FILE})") { |v| opts[:spec] = v }
|
|
18
|
+
o.on("--out=PATH", "Where to write (default: #{EnvPact::YAML_FILE} next to the spec)") { |v| opts[:out] = v }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def call(_rest, opts)
|
|
22
|
+
src = opts[:spec] || (File.file?(EnvPact::LEGACY_FILE) ? EnvPact::LEGACY_FILE : EnvPact.locate(".").first)
|
|
23
|
+
return die("convert reads an env.spec; #{src} is already YAML") if File.extname(src).match?(/\A\.ya?ml\z/i)
|
|
24
|
+
return die("spec not found: #{src}") unless File.file?(src)
|
|
25
|
+
|
|
26
|
+
out = opts[:out] || File.join(File.dirname(src), EnvPact::YAML_FILE)
|
|
27
|
+
out = EnvPact::YAML_FILE if out == "./#{EnvPact::YAML_FILE}"
|
|
28
|
+
return die("#{out} already exists; not overwriting") if File.exist?(out)
|
|
29
|
+
|
|
30
|
+
yaml = EnvPact.parse_file(src).to_yaml_contract
|
|
31
|
+
EnvPact.parse_yaml(yaml) # never write something we cannot read back
|
|
32
|
+
File.write(out, yaml)
|
|
33
|
+
puts "✓ wrote #{out} (comments in #{File.basename(src)} are not carried over)"
|
|
34
|
+
puts " review it, then delete #{src} (git rm #{src}): with both files every command refuses to run"
|
|
35
|
+
0
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
data/lib/envpact/cli/lint.rb
CHANGED
|
@@ -4,9 +4,9 @@ class EnvPact
|
|
|
4
4
|
class CLI
|
|
5
5
|
class Lint < Command
|
|
6
6
|
command_name "lint"
|
|
7
|
-
summary "Parse and validate syntax of
|
|
7
|
+
summary "Parse and validate syntax of the contract (envpact.yml, or the legacy env.spec)"
|
|
8
8
|
banner "Usage: envpact lint [--spec=PATH]"
|
|
9
|
-
description "Parses
|
|
9
|
+
description "Parses the contract (envpact.yml, or the legacy env.spec) and reports any syntax errors."
|
|
10
10
|
|
|
11
11
|
def configure(o, opts)
|
|
12
12
|
spec_option(o, opts)
|
data/lib/envpact/cli/login.rb
CHANGED
|
@@ -13,7 +13,7 @@ class EnvPact
|
|
|
13
13
|
and stores the token encrypted: ~/.ob_read (--read) or ~/.ob_write
|
|
14
14
|
(--write). Without --role, --read uses the server's default role;
|
|
15
15
|
--write needs --role (e.g. --role=escrita).
|
|
16
|
-
The address comes from --addr, $BAO_ADDR or addr
|
|
16
|
+
The address comes from --addr, $BAO_ADDR or the source addr in envpact.yml.
|
|
17
17
|
DESC
|
|
18
18
|
|
|
19
19
|
MIN_PASSPHRASE = 12
|
data/lib/envpact/cli/pull.rb
CHANGED
|
@@ -8,7 +8,7 @@ class EnvPact
|
|
|
8
8
|
summary "Download the env's secrets from OpenBao into a 0600 dotenv file"
|
|
9
9
|
banner "Usage: envpact pull --env=NAME [--out=PATH] [--spec=PATH]"
|
|
10
10
|
description <<~DESC
|
|
11
|
-
Reads the secrets of one env from the
|
|
11
|
+
Reads the secrets of one env from the source in envpact.yml, checks the
|
|
12
12
|
result against the contract (together with the .env* files, except the
|
|
13
13
|
target itself) and, only if it is satisfied, writes the secrets to
|
|
14
14
|
.env.<env>.local (or --out), mode 0600. The values are never printed.
|
|
@@ -26,7 +26,7 @@ class EnvPact
|
|
|
26
26
|
spec = load_spec(opts)
|
|
27
27
|
out = opts[:out] || ".env.#{env}.local"
|
|
28
28
|
ref = spec.source_for(env)
|
|
29
|
-
raise EnvPact::Error, "no
|
|
29
|
+
raise EnvPact::Error, "no source for env '#{env}' in the contract" if ref.nil?
|
|
30
30
|
|
|
31
31
|
# The target is the last file check reads, so stale values in it must
|
|
32
32
|
# not satisfy the contract: leave it out of the merge.
|
data/lib/envpact/cli/run.rb
CHANGED
|
@@ -8,7 +8,7 @@ class EnvPact
|
|
|
8
8
|
summary "Fetch the env's secrets from OpenBao, check, then exec CMD with them"
|
|
9
9
|
banner "Usage: envpact run [--env=NAME] [--spec=PATH] -- CMD [ARGS...]"
|
|
10
10
|
description <<~DESC
|
|
11
|
-
Reads the secrets of the env from the
|
|
11
|
+
Reads the secrets of the env from the source in envpact.yml and puts them
|
|
12
12
|
in the environment of CMD only: nothing is written to disk, and the
|
|
13
13
|
OpenBao token never reaches CMD. Values from the source override the
|
|
14
14
|
current ENV. The result is checked against the contract (unknown ENV
|
|
@@ -4,11 +4,11 @@ class EnvPact
|
|
|
4
4
|
class CLI
|
|
5
5
|
class ScanPromptCommand < Command
|
|
6
6
|
command_name "scan-prompt"
|
|
7
|
-
summary "Print instructions for an AI agent to write
|
|
7
|
+
summary "Print instructions for an AI agent to write envpact.yml"
|
|
8
8
|
banner "Usage: envpact scan-prompt"
|
|
9
9
|
description <<~DESC
|
|
10
10
|
Prints a self-contained prompt that tells an agent (e.g. Claude) how to
|
|
11
|
-
find every env var in this repo and write or update
|
|
11
|
+
find every env var in this repo and write or update envpact.yml.
|
|
12
12
|
Typical use: envpact scan-prompt | claude
|
|
13
13
|
DESC
|
|
14
14
|
|
data/lib/envpact/cli/set.rb
CHANGED
|
@@ -26,7 +26,7 @@ class EnvPact
|
|
|
26
26
|
spec = load_spec(opts)
|
|
27
27
|
|
|
28
28
|
unless spec.secrets_for(env).key?(key)
|
|
29
|
-
return die("'#{key}' is not a declared secret for env '#{env}' in
|
|
29
|
+
return die("'#{key}' is not a declared secret for env '#{env}' in the contract")
|
|
30
30
|
end
|
|
31
31
|
unless @prompt.available?
|
|
32
32
|
return die(Prompt::NO_TTY)
|
data/lib/envpact/cli.rb
CHANGED
|
@@ -11,6 +11,7 @@ require_relative "cli/run"
|
|
|
11
11
|
require_relative "cli/push"
|
|
12
12
|
require_relative "cli/set"
|
|
13
13
|
require_relative "cli/scan_prompt"
|
|
14
|
+
require_relative "cli/convert"
|
|
14
15
|
|
|
15
16
|
class EnvPact
|
|
16
17
|
# Dispatcher for `envpact` subcommands.
|
|
@@ -32,6 +33,7 @@ class EnvPact
|
|
|
32
33
|
"push" => Push,
|
|
33
34
|
"set" => Set,
|
|
34
35
|
"scan-prompt" => ScanPromptCommand,
|
|
36
|
+
"convert" => Convert,
|
|
35
37
|
}.freeze
|
|
36
38
|
|
|
37
39
|
def self.run(argv)
|
data/lib/envpact/entry.rb
CHANGED
data/lib/envpact/parser.rb
CHANGED
|
@@ -26,8 +26,10 @@ class EnvPact
|
|
|
26
26
|
new.tap { |spec| spec.send(:_parse, text) }
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
+
# envpact.yml (or .yaml) is YAML; anything else is the legacy env.spec DSL.
|
|
29
30
|
def self.parse_file(path)
|
|
30
|
-
|
|
31
|
+
text = EnvPact.read_text(path)
|
|
32
|
+
File.extname(path).match?(/\A\.ya?ml\z/i) ? parse_yaml(text) : parse(text)
|
|
31
33
|
end
|
|
32
34
|
|
|
33
35
|
def initialize
|
|
@@ -224,7 +226,8 @@ class EnvPact
|
|
|
224
226
|
optional: e[:optional],
|
|
225
227
|
default: e[:default],
|
|
226
228
|
enum_values: e[:enum_values],
|
|
227
|
-
line: line_no
|
|
229
|
+
line: line_no,
|
|
230
|
+
description: e[:description]
|
|
228
231
|
)
|
|
229
232
|
end
|
|
230
233
|
|
data/lib/envpact/scan_prompt.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
# Task: build or update this repo's
|
|
1
|
+
# Task: build or update this repo's envpact.yml
|
|
2
2
|
|
|
3
|
-
You are working in a code repository. Produce `
|
|
3
|
+
You are working in a code repository. Produce `envpact.yml` at the repo root: the
|
|
4
4
|
env var contract for this app, checked by `envpact` locally, before deploy and at
|
|
5
5
|
boot. The contract lists **names, types and rules only — never values.**
|
|
6
6
|
|
|
@@ -28,25 +28,29 @@ to learn key names.
|
|
|
28
28
|
## 2. Classify each var
|
|
29
29
|
|
|
30
30
|
- **Secret or ConfigMap:** passwords, tokens, API keys, private keys, connection
|
|
31
|
-
strings with credentials → `
|
|
31
|
+
strings with credentials → `secrets:`. Everything else → `vars:` (ConfigMap).
|
|
32
32
|
- **Type:** `int`, `bool`, `url` or `enum(...)` when the code parses or compares
|
|
33
33
|
it that way; otherwise `str`.
|
|
34
|
-
- **Optional (
|
|
34
|
+
- **Optional (`optional: true`):** the code tolerates absence (fallback, `fetch` default,
|
|
35
35
|
`|| default`, feature off when unset). Otherwise required.
|
|
36
|
-
- **Default (
|
|
36
|
+
- **Default (`default: "value"`):** only literal, non-secret defaults that the code itself
|
|
37
37
|
uses. Never invent one.
|
|
38
38
|
- **Scope:** shared unless the var is clearly specific to some envs (e.g. only in
|
|
39
|
-
production config or only in `app_<env>.yml`) → `
|
|
39
|
+
production config or only in `app_<env>.yml`) → under `envs: <name>:`.
|
|
40
40
|
|
|
41
|
-
## 3. Write
|
|
41
|
+
## 3. Write envpact.yml
|
|
42
42
|
|
|
43
43
|
Use exactly this syntax:
|
|
44
44
|
|
|
45
45
|
{{SYNTAX}}
|
|
46
46
|
|
|
47
|
-
Do not invent a `
|
|
47
|
+
Do not invent a `source`: if the file already has one, preserve it as is.
|
|
48
48
|
|
|
49
|
-
|
|
49
|
+
If the repo has a legacy `env.spec` and no `envpact.yml`, run `envpact convert`
|
|
50
|
+
first, delete the `env.spec`, and work on the `envpact.yml`.
|
|
51
|
+
|
|
52
|
+
Put shared keys first (`vars`, `secrets`), then `envs`. Use the long form
|
|
53
|
+
`{type: ..., description: "..."}` when a short note helps the next reader. Group related keys with a short comment. If `envpact.yml`
|
|
50
54
|
already exists, update it in place: keep its comments and existing decisions, add
|
|
51
55
|
what is missing, and never silently drop a key — if a declared key looks unused,
|
|
52
56
|
keep it and flag it.
|
|
@@ -56,8 +60,9 @@ keep it and flag it.
|
|
|
56
60
|
Above any key whose classification is a guess, add a comment:
|
|
57
61
|
|
|
58
62
|
```
|
|
59
|
-
|
|
60
|
-
|
|
63
|
+
secrets:
|
|
64
|
+
# TODO(envpact): looks like a secret (used as Authorization header) — confirm
|
|
65
|
+
API_TOKEN:
|
|
61
66
|
```
|
|
62
67
|
|
|
63
68
|
## 5. Validate and report
|
data/lib/envpact/source.rb
CHANGED
|
@@ -8,7 +8,7 @@ class EnvPact
|
|
|
8
8
|
# value is appended for each. A path that does not exist yields {}.
|
|
9
9
|
def self.fetch(spec, env:, client:, warnings: nil)
|
|
10
10
|
ref = spec.source_for(env)
|
|
11
|
-
raise Error, "no
|
|
11
|
+
raise Error, "no source for env '#{env}' in the contract" if ref.nil?
|
|
12
12
|
|
|
13
13
|
data = client.read(ref.mount, ref.path) || {}
|
|
14
14
|
wanted = spec.secrets_for(env)
|
data/lib/envpact/syntax.md
CHANGED
|
@@ -1,53 +1,60 @@
|
|
|
1
|
-
```
|
|
2
|
-
|
|
3
|
-
APP_NAME
|
|
4
|
-
LOG_LEVEL: enum(debug, info, warn, error) = info
|
|
5
|
-
DEBUG?: bool
|
|
6
|
-
PORT: int = 3000
|
|
7
|
-
PUBLIC_URL: url
|
|
8
|
-
|
|
9
|
-
[source]
|
|
10
|
-
openbao path=team/myapp/{env}
|
|
11
|
-
openbao path=cluster/selly/myapp-web env=production
|
|
1
|
+
```yaml
|
|
2
|
+
version: 1
|
|
12
3
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
4
|
+
source: # where the secrets live; {env} becomes the env name
|
|
5
|
+
openbao:
|
|
6
|
+
path: "team/myapp/{env}"
|
|
7
|
+
# addr: "https://bao.example.com" # optional
|
|
8
|
+
# mount: secret # optional, default secret
|
|
16
9
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
10
|
+
vars: # ConfigMap keys, shared by every env
|
|
11
|
+
APP_NAME: # empty value = required string
|
|
12
|
+
LOG_LEVEL: {type: "enum(debug, info, warn, error)", default: "info"}
|
|
13
|
+
DEBUG: {type: bool, optional: true}
|
|
14
|
+
PORT: {type: int, default: "3000", description: "HTTP port"}
|
|
15
|
+
PUBLIC_URL: url
|
|
21
16
|
|
|
22
|
-
|
|
23
|
-
|
|
17
|
+
secrets: # Secret keys, shared by every env
|
|
18
|
+
DB_PASS:
|
|
19
|
+
OPENAI_API_KEY:
|
|
24
20
|
|
|
25
|
-
|
|
26
|
-
|
|
21
|
+
envs: # only for what differs per env
|
|
22
|
+
production:
|
|
23
|
+
source:
|
|
24
|
+
openbao:
|
|
25
|
+
path: "cluster/selly/myapp-web"
|
|
26
|
+
vars:
|
|
27
|
+
SENTRY_DSN: url
|
|
28
|
+
secrets:
|
|
29
|
+
STRIPE_LIVE_KEY:
|
|
30
|
+
"production, staging": # one key, several envs
|
|
31
|
+
vars:
|
|
32
|
+
CDN_HOST:
|
|
27
33
|
```
|
|
28
34
|
|
|
29
|
-
- `
|
|
30
|
-
-
|
|
31
|
-
-
|
|
35
|
+
- `version: 1` is required. Unknown keys and fields are errors.
|
|
36
|
+
- A key's value is one of:
|
|
37
|
+
- empty — required string;
|
|
38
|
+
- a type — `str` | `int` | `bool` | `url` | `url(a, b)` | `enum(a, b, c)`;
|
|
39
|
+
- a mapping with `type`, `default`, `optional` (`true`/`false`) and
|
|
40
|
+
`description`.
|
|
41
|
+
- Types:
|
|
32
42
|
- `bool` accepts `true false 1 0 yes no` (any case).
|
|
33
43
|
- `url` needs a scheme and a host: `postgres://u:p@h:5432/db`, `https://x.y`.
|
|
34
44
|
- `url(postgres, redis)` also restricts the scheme (case-insensitive).
|
|
35
|
-
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
-
|
|
43
|
-
|
|
44
|
-
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
declared in overlapping scopes, may not be both ConfigMap and Secret, and must
|
|
48
|
-
have the same type in every env.
|
|
45
|
+
- Inside `{...}` a comma splits fields: quote `enum(...)`/`url(...)` there,
|
|
46
|
+
as in `{type: "enum(a, b)"}`. Quote paths with `{env}` too.
|
|
47
|
+
- `default` is ConfigMap only; secrets cannot have defaults. It must be valid
|
|
48
|
+
for the type (`{type: int, default: "3OOO"}` is a lint error).
|
|
49
|
+
- Every value is read as plain text: `no`, `on`, `0.30` and `0800` stay
|
|
50
|
+
strings, never YAML booleans or numbers. Quote to keep `#` or spaces.
|
|
51
|
+
- Anchors, aliases and tags (`&x`, `*x`, `!!str`) are not allowed.
|
|
52
|
+
- Key names are `A-Z`, `0-9` and `_`, starting with a letter. Env names are
|
|
53
|
+
`[A-Za-z0-9_-]+`.
|
|
54
|
+
- An env with no entry under `envs` gets only the shared keys. A key may not be
|
|
55
|
+
declared both shared and per env, may not be both ConfigMap and Secret, and
|
|
56
|
+
must have the same type in every env.
|
|
49
57
|
- Empty values count as absent.
|
|
50
|
-
- `
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
- After `[source]` any other header resumes keys; `[secrets]` there applies to shared.
|
|
58
|
+
- `source` at the top is the fallback; `envs.<name>.source` wins for that env.
|
|
59
|
+
- The legacy `env.spec` format is still read, with a warning; `envpact convert`
|
|
60
|
+
writes the equivalent `envpact.yml`.
|
data/lib/envpact/version.rb
CHANGED
data/lib/envpact/write_flow.rb
CHANGED
|
@@ -19,7 +19,7 @@ class EnvPact
|
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
ref = @spec.source_for(@env)
|
|
22
|
-
raise Error, "no
|
|
22
|
+
raise Error, "no source for env '#{@env}' in the contract" if ref.nil?
|
|
23
23
|
|
|
24
24
|
problems = validate(values)
|
|
25
25
|
unless problems.empty?
|
|
@@ -56,7 +56,7 @@ class EnvPact
|
|
|
56
56
|
entry = secrets[key]
|
|
57
57
|
if entry.nil?
|
|
58
58
|
problems << (configmap.key?(key) ? "'#{key}' is a ConfigMap key in env '#{@env}', not a secret" :
|
|
59
|
-
"'#{key}' is not declared in
|
|
59
|
+
"'#{key}' is not declared in the contract for env '#{@env}'")
|
|
60
60
|
elsif values[key].nil? || values[key].empty?
|
|
61
61
|
problems << "'#{key}' has an empty value"
|
|
62
62
|
elsif values[key].include?("\0")
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
require "psych"
|
|
2
|
+
|
|
3
|
+
# Parses envpact.yml -- the YAML form of the contract. Builds the same model as
|
|
4
|
+
# the env.spec parser (configmap, secrets, sources), so everything downstream is
|
|
5
|
+
# format-agnostic.
|
|
6
|
+
#
|
|
7
|
+
# version: 1
|
|
8
|
+
# source: {openbao: {path: team/app/{env}, addr: https://bao.example.com}}
|
|
9
|
+
# vars: {APP_NAME: , PORT: {type: int, default: "3000"}}
|
|
10
|
+
# secrets: {DB_PASS: }
|
|
11
|
+
# envs:
|
|
12
|
+
# production: {source: {...}, vars: {...}, secrets: {...}}
|
|
13
|
+
# "staging, production": {vars: {...}}
|
|
14
|
+
#
|
|
15
|
+
# Scalars are read as the raw text of the YAML node, never coerced: `no`,
|
|
16
|
+
# `on`, `0.30` and `0800` stay strings. Anchors, aliases and tags are refused.
|
|
17
|
+
# Messages give the line and never echo a value.
|
|
18
|
+
class EnvPact
|
|
19
|
+
YAML_FILE = "envpact.yml".freeze
|
|
20
|
+
LEGACY_FILE = "env.spec".freeze
|
|
21
|
+
|
|
22
|
+
TOP_KEYS = %w[version source vars secrets envs].freeze
|
|
23
|
+
ENV_KEYS = %w[source vars secrets].freeze
|
|
24
|
+
ENTRY_FIELDS = %w[type default optional description].freeze
|
|
25
|
+
KEY_NAME_RE = /\A[A-Z][A-Z0-9_]*\z/.freeze
|
|
26
|
+
NULLS = ["", "~", "null", "Null", "NULL"].freeze
|
|
27
|
+
|
|
28
|
+
def self.parse_yaml(text)
|
|
29
|
+
new.tap { |spec| spec.send(:_parse_yaml, text) }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def _parse_yaml(text)
|
|
35
|
+
text = EnvPact.utf8(text).sub(/\A/, "")
|
|
36
|
+
root = yaml_root(text)
|
|
37
|
+
return if root.nil?
|
|
38
|
+
|
|
39
|
+
top = yaml_mapping(root, "the top level")
|
|
40
|
+
pairs = yaml_pairs(top)
|
|
41
|
+
pairs.each { |k, _v| raise ParseError.new("unknown key '#{k.value}' (valid: #{TOP_KEYS.join(', ')})", line_of(k)) unless TOP_KEYS.include?(k.value) }
|
|
42
|
+
by_key = pairs.map { |k, v| [k.value, [k, v]] }.to_h
|
|
43
|
+
|
|
44
|
+
version = by_key["version"]
|
|
45
|
+
raise ParseError.new("missing 'version: 1'", 1) unless version
|
|
46
|
+
unless yaml_scalar_text(version[1], "version") == "1"
|
|
47
|
+
raise ParseError.new("version must be 1", line_of(version[1]))
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
yaml_source(by_key["source"][1], nil) if by_key["source"]
|
|
51
|
+
yaml_keys(by_key["vars"][1], :configmap, [SHARED], "vars") if by_key["vars"]
|
|
52
|
+
yaml_keys(by_key["secrets"][1], :secret, [SHARED], "secrets") if by_key["secrets"]
|
|
53
|
+
yaml_envs(by_key["envs"][1]) if by_key["envs"]
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def yaml_root(text)
|
|
57
|
+
doc = Psych.parse(text)
|
|
58
|
+
doc && doc.root
|
|
59
|
+
rescue Psych::SyntaxError => e
|
|
60
|
+
# The problem text names a token class, not the content of the line.
|
|
61
|
+
raise ParseError.new("YAML syntax error at line #{e.line} column #{e.column} (#{e.problem})", e.line)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def line_of(node)
|
|
65
|
+
node.start_line + 1
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def yaml_check_plain(node)
|
|
69
|
+
if node.is_a?(Psych::Nodes::Alias) || (node.respond_to?(:anchor) && node.anchor)
|
|
70
|
+
raise ParseError.new("anchors and aliases are not allowed", line_of(node))
|
|
71
|
+
end
|
|
72
|
+
return unless node.respond_to?(:tag) && node.tag
|
|
73
|
+
|
|
74
|
+
raise ParseError.new("tags are not allowed", line_of(node))
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def yaml_mapping(node, where)
|
|
78
|
+
yaml_check_plain(node)
|
|
79
|
+
return node if node.is_a?(Psych::Nodes::Mapping)
|
|
80
|
+
|
|
81
|
+
raise ParseError.new("#{where} must be a mapping", line_of(node))
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# [[key_node, value_node], ...], refusing non-scalar and duplicate keys.
|
|
85
|
+
def yaml_pairs(mapping)
|
|
86
|
+
seen = {}
|
|
87
|
+
mapping.children.each_slice(2).map do |k, v|
|
|
88
|
+
yaml_check_plain(k)
|
|
89
|
+
raise ParseError.new("keys must be plain names", line_of(k)) unless k.is_a?(Psych::Nodes::Scalar)
|
|
90
|
+
raise ParseError.new("duplicate key '#{k.value}'", line_of(k)) if seen[k.value]
|
|
91
|
+
|
|
92
|
+
seen[k.value] = true
|
|
93
|
+
yaml_check_plain(v)
|
|
94
|
+
[k, v]
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# nil for an empty value (KEY:, KEY: ~, KEY: null); the raw text otherwise.
|
|
99
|
+
def yaml_scalar_text(node, what)
|
|
100
|
+
yaml_check_plain(node)
|
|
101
|
+
raise ParseError.new("#{what} must be a single value", line_of(node)) unless node.is_a?(Psych::Nodes::Scalar)
|
|
102
|
+
return nil if node.plain && NULLS.include?(node.value)
|
|
103
|
+
|
|
104
|
+
node.value
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def yaml_empty?(node)
|
|
108
|
+
node.is_a?(Psych::Nodes::Scalar) && node.plain && NULLS.include?(node.value)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def yaml_keys(node, type, scope, section)
|
|
112
|
+
return if yaml_empty?(node)
|
|
113
|
+
|
|
114
|
+
yaml_pairs(yaml_mapping(node, "'#{section}'")).each do |k, v|
|
|
115
|
+
name = k.value
|
|
116
|
+
line = line_of(k)
|
|
117
|
+
raise ParseError.new("invalid key name '#{name}' (expected A-Z, 0-9 and _, starting with a letter)", line) unless KEY_NAME_RE.match?(name)
|
|
118
|
+
|
|
119
|
+
entry = yaml_entry(name, v, line)
|
|
120
|
+
raise ParseError.new("secret '#{name}' cannot have a default value", line) if type == :secret && !entry[:default].nil?
|
|
121
|
+
|
|
122
|
+
check_default_type(entry, line)
|
|
123
|
+
store_entry(type, scope, entry, line)
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def yaml_entry(name, node, line)
|
|
128
|
+
type_str = "str"
|
|
129
|
+
optional = false
|
|
130
|
+
default = nil
|
|
131
|
+
description = nil
|
|
132
|
+
|
|
133
|
+
if node.is_a?(Psych::Nodes::Mapping)
|
|
134
|
+
yaml_pairs(node).each do |k, v|
|
|
135
|
+
field = k.value
|
|
136
|
+
unless ENTRY_FIELDS.include?(field)
|
|
137
|
+
hint = field.include?(")") ? "; inside {...} a comma splits enum(...)/url(...): quote the type, e.g. type: \"enum(a, b)\"" : ""
|
|
138
|
+
raise ParseError.new("unknown field '#{field}' for '#{name}' (valid: #{ENTRY_FIELDS.join(', ')})#{hint}", line_of(k))
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
case field
|
|
142
|
+
when "type" then type_str = yaml_scalar_text(v, "type") || "str"
|
|
143
|
+
when "default" then default = yaml_scalar_text(v, "default")
|
|
144
|
+
when "description" then description = yaml_scalar_text(v, "description")
|
|
145
|
+
when "optional"
|
|
146
|
+
raw = yaml_scalar_text(v, "optional")
|
|
147
|
+
raise ParseError.new("optional for '#{name}' must be true or false", line_of(v)) unless %w[true false].include?(raw)
|
|
148
|
+
|
|
149
|
+
optional = raw == "true"
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
else
|
|
153
|
+
type_str = yaml_scalar_text(node, "the value of '#{name}'") || "str"
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
type, enum_values = parse_type(type_str.strip, line)
|
|
157
|
+
{ name: name, type: type, optional: optional, default: default, enum_values: enum_values, description: description }
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def yaml_envs(node)
|
|
161
|
+
return if yaml_empty?(node)
|
|
162
|
+
|
|
163
|
+
yaml_pairs(yaml_mapping(node, "'envs'")).each do |k, v|
|
|
164
|
+
line = line_of(k)
|
|
165
|
+
envs = k.value.split(",").map(&:strip).reject(&:empty?)
|
|
166
|
+
raise ParseError.new("empty env name in 'envs'", line) if envs.empty?
|
|
167
|
+
|
|
168
|
+
envs.each do |e|
|
|
169
|
+
raise ParseError.new("invalid env name '#{e}' (must match #{ENV_NAME_RE.source}; use commas to separate envs)", line) unless ENV_NAME_RE.match?(e)
|
|
170
|
+
end
|
|
171
|
+
next if yaml_empty?(v)
|
|
172
|
+
|
|
173
|
+
body = yaml_pairs(yaml_mapping(v, "env '#{k.value}'"))
|
|
174
|
+
body.each { |bk, _| raise ParseError.new("unknown key '#{bk.value}' in env '#{k.value}' (valid: #{ENV_KEYS.join(', ')})", line_of(bk)) unless ENV_KEYS.include?(bk.value) }
|
|
175
|
+
parts = body.map { |bk, bv| [bk.value, bv] }.to_h
|
|
176
|
+
yaml_source(parts["source"], envs) if parts["source"]
|
|
177
|
+
yaml_keys(parts["vars"], :configmap, envs, "vars") if parts["vars"]
|
|
178
|
+
yaml_keys(parts["secrets"], :secret, envs, "secrets") if parts["secrets"]
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def yaml_source(node, envs)
|
|
183
|
+
pairs = yaml_pairs(yaml_mapping(node, "'source'"))
|
|
184
|
+
raise ParseError.new("'source' needs one backend (openbao)", line_of(node)) unless pairs.size == 1
|
|
185
|
+
|
|
186
|
+
bk, bv = pairs.first
|
|
187
|
+
line = line_of(bk)
|
|
188
|
+
raise ParseError.new("unknown backend '#{bk.value}' in source (valid: #{SourceParser::BACKENDS.join(', ')})", line) unless SourceParser::BACKENDS.include?(bk.value)
|
|
189
|
+
|
|
190
|
+
opts = {}
|
|
191
|
+
yaml_pairs(yaml_mapping(bv, "source '#{bk.value}'")).each do |k, v|
|
|
192
|
+
opt = k.value
|
|
193
|
+
raise ParseError.new("unknown option '#{opt}' in source (valid: path, mount, addr)", line_of(k)) unless %w[path mount addr].include?(opt)
|
|
194
|
+
|
|
195
|
+
opts[opt] = yaml_scalar_text(v, opt)
|
|
196
|
+
end
|
|
197
|
+
raise ParseError.new("source needs path", line) if opts["path"].nil?
|
|
198
|
+
|
|
199
|
+
begin
|
|
200
|
+
SourceParser.validate_path(opts["path"], line)
|
|
201
|
+
SourceParser.validate_addr(opts["addr"], line) if opts["addr"]
|
|
202
|
+
rescue ParseError => e
|
|
203
|
+
raise ParseError.new(e.message.sub(/\Aline \d+: /, "").sub(" in [source]", " in source"), line)
|
|
204
|
+
end
|
|
205
|
+
mount = opts["mount"] || "secret"
|
|
206
|
+
raise ParseError.new("invalid mount '#{mount}' in source", line) unless SourceParser::MOUNT_RE.match?(mount)
|
|
207
|
+
|
|
208
|
+
sl = SourceLine.new(backend: bk.value, mount: mount, path: opts["path"], addr: opts["addr"], envs: envs, line: line)
|
|
209
|
+
SourceParser.add(@sources, sl)
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
class EnvPact
|
|
214
|
+
# Where the contract of a repo is: [path, legacy?]. envpact.yml wins; env.spec
|
|
215
|
+
# is the legacy fallback; both at once is an error (they would drift). With
|
|
216
|
+
# neither, the envpact.yml path is returned so "not found" names it.
|
|
217
|
+
def self.locate(dir = ".")
|
|
218
|
+
yml = dir == "." ? YAML_FILE : File.join(dir, YAML_FILE)
|
|
219
|
+
legacy = dir == "." ? LEGACY_FILE : File.join(dir, LEGACY_FILE)
|
|
220
|
+
has_yml = File.file?(yml)
|
|
221
|
+
has_legacy = File.file?(legacy)
|
|
222
|
+
if has_yml && has_legacy
|
|
223
|
+
raise Error, "both #{YAML_FILE} and #{LEGACY_FILE} in #{File.expand_path(dir)}: keep only #{YAML_FILE} (envpact convert writes it from #{LEGACY_FILE})"
|
|
224
|
+
end
|
|
225
|
+
return [legacy, true] if has_legacy
|
|
226
|
+
|
|
227
|
+
[yml, false]
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
LEGACY_WARNING = "#{LEGACY_FILE} is deprecated: run `envpact convert` to write #{YAML_FILE}".freeze
|
|
231
|
+
end
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
# Writes the model back as envpact.yml (used by `envpact convert`). Keys keep
|
|
4
|
+
# their order of declaration; keys declared once for several envs
|
|
5
|
+
# ([env: a, b]) stay grouped under "a, b". Comments are not carried over.
|
|
6
|
+
class EnvPact
|
|
7
|
+
def to_yaml_contract
|
|
8
|
+
out = ["version: 1"]
|
|
9
|
+
shared_source = @sources.find { |s| s.envs.nil? }
|
|
10
|
+
out << "" << "source:" << yaml_source_lines(shared_source, " ") if shared_source
|
|
11
|
+
shared_vars = yaml_group(@configmap)[SHARED]
|
|
12
|
+
shared_secrets = yaml_group(@secrets)[SHARED]
|
|
13
|
+
out << "" << "vars:" << yaml_key_lines(shared_vars, " ") if shared_vars
|
|
14
|
+
out << "" << "secrets:" << yaml_key_lines(shared_secrets, " ") if shared_secrets
|
|
15
|
+
|
|
16
|
+
groups = yaml_env_groups
|
|
17
|
+
unless groups.empty?
|
|
18
|
+
out << "" << "envs:"
|
|
19
|
+
groups.each do |label, parts|
|
|
20
|
+
out << " #{JSON.generate(label)}:"
|
|
21
|
+
out << " source:" << yaml_source_lines(parts[:source], " ") if parts[:source]
|
|
22
|
+
out << " vars:" << yaml_key_lines(parts[:vars], " ") if parts[:vars]
|
|
23
|
+
out << " secrets:" << yaml_key_lines(parts[:secrets], " ") if parts[:secrets]
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
out.flatten.join("\n") + "\n"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
# { "*" or "a, b" => [[name, entry], ...] }, regrouping env-scoped keys that
|
|
32
|
+
# came from one declaration (same line) under one label.
|
|
33
|
+
def yaml_group(bucket)
|
|
34
|
+
by_decl = {}
|
|
35
|
+
bucket.each do |env, keys|
|
|
36
|
+
keys.each do |name, entry|
|
|
37
|
+
(by_decl[[name, entry.line]] ||= { entry: entry, envs: [] })[:envs] << env
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
groups = {}
|
|
41
|
+
by_decl.sort_by { |(_, line), _| line }.each do |(name, _), d|
|
|
42
|
+
label = d[:envs].include?(SHARED) ? SHARED : d[:envs].join(", ")
|
|
43
|
+
(groups[label] ||= []) << [name, d[:entry]]
|
|
44
|
+
end
|
|
45
|
+
groups
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def yaml_env_groups
|
|
49
|
+
groups = {}
|
|
50
|
+
@sources.reject { |s| s.envs.nil? }.each { |s| (groups[s.envs.join(", ")] ||= {})[:source] = s }
|
|
51
|
+
yaml_group(@configmap).each { |label, keys| (groups[label] ||= {})[:vars] = keys unless label == SHARED }
|
|
52
|
+
yaml_group(@secrets).each { |label, keys| (groups[label] ||= {})[:secrets] = keys unless label == SHARED }
|
|
53
|
+
groups
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def yaml_source_lines(s, indent)
|
|
57
|
+
lines = ["#{indent}openbao:", "#{indent} path: #{JSON.generate(s.path)}"]
|
|
58
|
+
lines << "#{indent} mount: #{JSON.generate(s.mount)}" unless s.mount == "secret"
|
|
59
|
+
lines << "#{indent} addr: #{JSON.generate(s.addr)}" if s.addr
|
|
60
|
+
lines
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def yaml_key_lines(pairs, indent)
|
|
64
|
+
pairs.map do |name, e|
|
|
65
|
+
type = yaml_type_text(e)
|
|
66
|
+
if !e.optional && e.default.nil? && e.description.nil?
|
|
67
|
+
type == "str" ? "#{indent}#{name}:" : "#{indent}#{name}: #{type}"
|
|
68
|
+
else
|
|
69
|
+
fields = []
|
|
70
|
+
fields << "type: #{type.include?('(') ? JSON.generate(type) : type}" unless type == "str"
|
|
71
|
+
fields << "default: #{JSON.generate(e.default)}" unless e.default.nil?
|
|
72
|
+
fields << "optional: true" if e.optional
|
|
73
|
+
fields << "description: #{JSON.generate(e.description)}" if e.description
|
|
74
|
+
"#{indent}#{name}: {#{fields.join(', ')}}"
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def yaml_type_text(e)
|
|
80
|
+
return e.type.to_s unless e.enum_values
|
|
81
|
+
|
|
82
|
+
"#{e.type}(#{e.enum_values.join(', ')})"
|
|
83
|
+
end
|
|
84
|
+
end
|
data/lib/envpact.rb
CHANGED
|
@@ -3,6 +3,8 @@ require_relative "envpact/errors"
|
|
|
3
3
|
require_relative "envpact/entry"
|
|
4
4
|
require_relative "envpact/source_ref"
|
|
5
5
|
require_relative "envpact/parser"
|
|
6
|
+
require_relative "envpact/yaml_parser"
|
|
7
|
+
require_relative "envpact/yaml_writer"
|
|
6
8
|
require_relative "envpact/types"
|
|
7
9
|
require_relative "envpact/problem"
|
|
8
10
|
require_relative "envpact/result"
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: envpact
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Leadfy
|
|
@@ -58,9 +58,9 @@ dependencies:
|
|
|
58
58
|
- - "~>"
|
|
59
59
|
- !ruby/object:Gem::Version
|
|
60
60
|
version: '13.0'
|
|
61
|
-
description: 'Parses env.spec files: a declarative contract
|
|
62
|
-
an app expects, their types, optionality, and ConfigMap-vs-Secret
|
|
63
|
-
and syncs secrets with OpenBao. Pure stdlib, zero runtime dependencies.'
|
|
61
|
+
description: 'Parses envpact.yml (and legacy env.spec) files: a declarative contract
|
|
62
|
+
describing which env vars an app expects, their types, optionality, and ConfigMap-vs-Secret
|
|
63
|
+
classification, and syncs secrets with OpenBao. Pure stdlib, zero runtime dependencies.'
|
|
64
64
|
email:
|
|
65
65
|
- dev@leadfy.com
|
|
66
66
|
executables:
|
|
@@ -80,6 +80,7 @@ files:
|
|
|
80
80
|
- lib/envpact/cli.rb
|
|
81
81
|
- lib/envpact/cli/check.rb
|
|
82
82
|
- lib/envpact/cli/command.rb
|
|
83
|
+
- lib/envpact/cli/convert.rb
|
|
83
84
|
- lib/envpact/cli/exec.rb
|
|
84
85
|
- lib/envpact/cli/lint.rb
|
|
85
86
|
- lib/envpact/cli/lock.rb
|
|
@@ -111,6 +112,8 @@ files:
|
|
|
111
112
|
- lib/envpact/types.rb
|
|
112
113
|
- lib/envpact/version.rb
|
|
113
114
|
- lib/envpact/write_flow.rb
|
|
115
|
+
- lib/envpact/yaml_parser.rb
|
|
116
|
+
- lib/envpact/yaml_writer.rb
|
|
114
117
|
homepage: https://github.com/repleadfy/envpact
|
|
115
118
|
licenses:
|
|
116
119
|
- MIT
|