envpact 0.1.0 → 0.2.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 +26 -0
- data/README.md +74 -3
- data/lib/envpact/agent.rb +197 -0
- data/lib/envpact/atomic_file.rb +27 -0
- data/lib/envpact/boot.rb +25 -0
- data/lib/envpact/cli/check.rb +6 -0
- data/lib/envpact/cli/command.rb +17 -1
- data/lib/envpact/cli/exec.rb +2 -15
- data/lib/envpact/cli/lock.rb +20 -0
- data/lib/envpact/cli/login.rb +97 -0
- data/lib/envpact/cli/pull.rb +63 -0
- data/lib/envpact/cli/push.rb +52 -0
- data/lib/envpact/cli/run.rb +46 -0
- data/lib/envpact/cli/set.rb +52 -0
- data/lib/envpact/cli/status.rb +40 -0
- data/lib/envpact/cli/unlock.rb +68 -0
- data/lib/envpact/cli.rb +16 -0
- data/lib/envpact/credentials.rb +61 -0
- data/lib/envpact/dotenv.rb +4 -1
- data/lib/envpact/dotenv_writer.rb +19 -0
- data/lib/envpact/keyfile.rb +109 -0
- data/lib/envpact/oidc_login.rb +144 -0
- data/lib/envpact/open_bao.rb +163 -0
- data/lib/envpact/parser.rb +30 -3
- data/lib/envpact/problem.rb +5 -1
- data/lib/envpact/prompt.rb +56 -0
- data/lib/envpact/scan_prompt.md +2 -0
- data/lib/envpact/source.rb +22 -0
- data/lib/envpact/source_ref.rb +94 -0
- data/lib/envpact/syntax.md +9 -0
- data/lib/envpact/types.rb +5 -3
- data/lib/envpact/version.rb +1 -1
- data/lib/envpact/write_flow.rb +70 -0
- data/lib/envpact.rb +12 -0
- metadata +32 -6
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
require "time"
|
|
2
|
+
require_relative "command"
|
|
3
|
+
|
|
4
|
+
class EnvPact
|
|
5
|
+
class CLI
|
|
6
|
+
class Pull < Command
|
|
7
|
+
command_name "pull"
|
|
8
|
+
summary "Download the env's secrets from OpenBao into a 0600 dotenv file"
|
|
9
|
+
banner "Usage: envpact pull --env=NAME [--out=PATH] [--spec=PATH]"
|
|
10
|
+
description <<~DESC
|
|
11
|
+
Reads the secrets of one env from the [source] in env.spec, checks the
|
|
12
|
+
result against the contract (together with the .env* files, except the
|
|
13
|
+
target itself) and, only if it is satisfied, writes the secrets to
|
|
14
|
+
.env.<env>.local (or --out), mode 0600. The values are never printed.
|
|
15
|
+
The read token comes from the agent (envpact unlock) or ~/.ob_read.
|
|
16
|
+
DESC
|
|
17
|
+
|
|
18
|
+
def configure(o, opts)
|
|
19
|
+
o.on("--env=NAME", "Env to pull (required)") { |v| opts[:env] = v }
|
|
20
|
+
o.on("--out=PATH", "Target file (default: .env.<env>.local)") { |v| opts[:out] = v }
|
|
21
|
+
spec_option(o, opts)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def call(_rest, opts)
|
|
25
|
+
env = opts[:env] or raise UsageError, "--env=NAME is required"
|
|
26
|
+
spec = load_spec(opts)
|
|
27
|
+
out = opts[:out] || ".env.#{env}.local"
|
|
28
|
+
ref = spec.source_for(env)
|
|
29
|
+
raise EnvPact::Error, "no [source] for env '#{env}' in env.spec" if ref.nil?
|
|
30
|
+
|
|
31
|
+
# The target is the last file check reads, so stale values in it must
|
|
32
|
+
# not satisfy the contract: leave it out of the merge.
|
|
33
|
+
loaded = Dotenv.load(Dir.pwd, env: env, skip: [File.expand_path(out)])
|
|
34
|
+
warnings = []
|
|
35
|
+
fetched = fetch_source(spec, env, warnings: warnings)
|
|
36
|
+
values = loaded[:values].merge(fetched)
|
|
37
|
+
|
|
38
|
+
result = spec.check(values, env: env, strict: false)
|
|
39
|
+
warnings.each { |p| warn " ! #{p.message}" }
|
|
40
|
+
code = report(spec, result, opts)
|
|
41
|
+
return code unless result.ok?
|
|
42
|
+
|
|
43
|
+
header = "# written by envpact pull from openbao:#{ref.mount}/#{ref.path} at " \
|
|
44
|
+
"#{Time.now.utc.iso8601}; do not edit\n"
|
|
45
|
+
AtomicFile.write(out, header + Dotenv.dump(fetched))
|
|
46
|
+
puts "✓ pulled #{fetched.size} secrets → #{out} (0600)"
|
|
47
|
+
warn "! #{out} is not git-ignored" if not_ignored?(out)
|
|
48
|
+
0
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
private
|
|
52
|
+
|
|
53
|
+
# git check-ignore -q: 0 ignored, 1 not ignored, 128 not a repo. Without
|
|
54
|
+
# git, or outside a repo, stay quiet.
|
|
55
|
+
def not_ignored?(path)
|
|
56
|
+
system("git", "check-ignore", "-q", path, out: File::NULL, err: File::NULL)
|
|
57
|
+
$?.exitstatus == 1
|
|
58
|
+
rescue SystemCallError
|
|
59
|
+
false
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
require_relative "command"
|
|
2
|
+
|
|
3
|
+
class EnvPact
|
|
4
|
+
class CLI
|
|
5
|
+
class Push < Command
|
|
6
|
+
command_name "push"
|
|
7
|
+
summary "Write secrets from a dotenv file to OpenBao (merge), after confirmation"
|
|
8
|
+
banner "Usage: envpact push --env=NAME --from=PATH|- [--spec=PATH]"
|
|
9
|
+
description <<~DESC
|
|
10
|
+
Reads KEY=value lines from a dotenv file (or stdin with --from=-), keeps
|
|
11
|
+
the keys the contract declares as secrets for the env, validates them and
|
|
12
|
+
merges them into the env's [source] path. Existing keys in OpenBao are
|
|
13
|
+
never deleted. Asks for confirmation and the ~/.ob_write passphrase on
|
|
14
|
+
the terminal; with no terminal it refuses before any network call.
|
|
15
|
+
DESC
|
|
16
|
+
|
|
17
|
+
def configure(o, opts)
|
|
18
|
+
o.on("--env=NAME", "Env to write (required)") { |v| opts[:env] = v }
|
|
19
|
+
o.on("--from=PATH", "Dotenv file to read, or - for stdin (required)") { |v| opts[:from] = v }
|
|
20
|
+
spec_option(o, opts)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def call(_rest, opts)
|
|
24
|
+
env = opts[:env] or raise UsageError, "--env=NAME is required"
|
|
25
|
+
from = opts[:from] or raise UsageError, "--from=PATH (or -) is required"
|
|
26
|
+
spec = load_spec(opts)
|
|
27
|
+
|
|
28
|
+
text = from == "-" ? $stdin.read : EnvPact.read_text(from)
|
|
29
|
+
input = Dotenv.parse(text.to_s)
|
|
30
|
+
|
|
31
|
+
secrets = spec.secrets_for(env)
|
|
32
|
+
configmap = spec.configmap_for(env)
|
|
33
|
+
values = {}
|
|
34
|
+
input.each do |key, value|
|
|
35
|
+
if configmap.key?(key)
|
|
36
|
+
warn "! #{key} ignored (configmap)"
|
|
37
|
+
elsif value.empty?
|
|
38
|
+
warn "! #{key} is empty (skipped)"
|
|
39
|
+
else
|
|
40
|
+
values[key] = value
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
secrets.each do |key, entry|
|
|
44
|
+
next if entry.optional || values.key?(key) || !entry.default.nil?
|
|
45
|
+
warn "! #{key} not in input (kept as is in OpenBao)"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
WriteFlow.new(spec: spec, env: env, prompt: @prompt).call(values)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
require_relative "exec"
|
|
2
|
+
|
|
3
|
+
class EnvPact
|
|
4
|
+
class CLI
|
|
5
|
+
# `exec` plus the secrets from OpenBao, held only in this process's ENV.
|
|
6
|
+
class Run < Exec
|
|
7
|
+
command_name "run"
|
|
8
|
+
summary "Fetch the env's secrets from OpenBao, check, then exec CMD with them"
|
|
9
|
+
banner "Usage: envpact run [--env=NAME] [--spec=PATH] -- CMD [ARGS...]"
|
|
10
|
+
description <<~DESC
|
|
11
|
+
Reads the secrets of the env from the [source] in env.spec and puts them
|
|
12
|
+
in the environment of CMD only: nothing is written to disk, and the
|
|
13
|
+
OpenBao token never reaches CMD. Values from the source override the
|
|
14
|
+
current ENV. The result is checked against the contract (unknown ENV
|
|
15
|
+
keys are ignored), defaults are filled in, and CMD replaces this
|
|
16
|
+
process. On failure prints the problems and exits 1 without starting
|
|
17
|
+
CMD. The env name comes from --env or $ENVPACT_ENV.
|
|
18
|
+
DESC
|
|
19
|
+
|
|
20
|
+
def call(_rest, opts)
|
|
21
|
+
env = opts[:env] || ENV["ENVPACT_ENV"]
|
|
22
|
+
raise UsageError, "--env=NAME (or $ENVPACT_ENV) is required" if env.nil? || env.empty?
|
|
23
|
+
raise UsageError, "missing command: envpact run [options] -- CMD [ARGS...]" if @command.empty?
|
|
24
|
+
|
|
25
|
+
spec = load_spec(opts)
|
|
26
|
+
# An inherited token must not reach the app, whatever happens next.
|
|
27
|
+
%w[BAO_TOKEN VAULT_TOKEN].each { |k| ENV.delete(k) }
|
|
28
|
+
|
|
29
|
+
warnings = []
|
|
30
|
+
fetched = fetch_source(spec, env, warnings: warnings)
|
|
31
|
+
fetched.each do |key, value|
|
|
32
|
+
raise EnvPact::Error, "value of '#{key}' contains a NUL byte; it cannot be put in the environment" if value.include?("\0")
|
|
33
|
+
end
|
|
34
|
+
ENV.update(fetched)
|
|
35
|
+
warnings.each { |p| warn " ! #{p.message}" }
|
|
36
|
+
|
|
37
|
+
result = spec.check(ENV.to_h, env: env, extras: false)
|
|
38
|
+
return report(spec, result, opts) unless result.ok?
|
|
39
|
+
|
|
40
|
+
warn "✓ #{File.basename(spec_path(opts))} satisfied (#{scope_label(spec, result)})"
|
|
41
|
+
Boot.fill_defaults(spec, env)
|
|
42
|
+
Boot.exec_command(@command)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
require_relative "command"
|
|
2
|
+
|
|
3
|
+
class EnvPact
|
|
4
|
+
class CLI
|
|
5
|
+
class Set < Command
|
|
6
|
+
command_name "set"
|
|
7
|
+
summary "Write one secret to OpenBao (merge), after confirmation"
|
|
8
|
+
banner "Usage: envpact set --env=NAME KEY [--spec=PATH]"
|
|
9
|
+
description <<~DESC
|
|
10
|
+
Sets one secret declared in the contract for the env. The value is read
|
|
11
|
+
from one line of stdin when stdin is not a terminal, otherwise prompted
|
|
12
|
+
for without echo. Never taken from argv or ENV. Asks for confirmation
|
|
13
|
+
and the ~/.ob_write passphrase on the terminal; with no terminal it
|
|
14
|
+
refuses before any network call.
|
|
15
|
+
DESC
|
|
16
|
+
|
|
17
|
+
def configure(o, opts)
|
|
18
|
+
o.on("--env=NAME", "Env to write (required)") { |v| opts[:env] = v }
|
|
19
|
+
spec_option(o, opts)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def call(rest, opts)
|
|
23
|
+
env = opts[:env] or raise UsageError, "--env=NAME is required"
|
|
24
|
+
raise UsageError, "give exactly one KEY: envpact set --env=NAME KEY" unless rest.size == 1
|
|
25
|
+
key = rest.first
|
|
26
|
+
spec = load_spec(opts)
|
|
27
|
+
|
|
28
|
+
unless spec.secrets_for(env).key?(key)
|
|
29
|
+
return die("'#{key}' is not a declared secret for env '#{env}' in env.spec")
|
|
30
|
+
end
|
|
31
|
+
unless @prompt.available?
|
|
32
|
+
return die(Prompt::NO_TTY)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
value = read_value(key)
|
|
36
|
+
WriteFlow.new(spec: spec, env: env, prompt: @prompt).call(key => value)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def read_value(key)
|
|
42
|
+
if $stdin.tty?
|
|
43
|
+
@prompt.secret("Value for #{key}: ")
|
|
44
|
+
else
|
|
45
|
+
line = $stdin.gets
|
|
46
|
+
raise EnvPact::Error, "no value on stdin for #{key}" if line.nil?
|
|
47
|
+
line.chomp.chomp("\r")
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
require "time"
|
|
2
|
+
require_relative "command"
|
|
3
|
+
|
|
4
|
+
class EnvPact
|
|
5
|
+
class CLI
|
|
6
|
+
class Status < Command
|
|
7
|
+
command_name "status"
|
|
8
|
+
summary "Show the agent and the stored tokens (never the tokens)"
|
|
9
|
+
banner "Usage: envpact status"
|
|
10
|
+
|
|
11
|
+
def call(_rest, _opts)
|
|
12
|
+
agent = Agent.request("status")
|
|
13
|
+
if agent && agent["until"]
|
|
14
|
+
puts "agent: unlocked until #{local(agent['until'])} (#{agent['addr']})"
|
|
15
|
+
else
|
|
16
|
+
puts "agent: locked"
|
|
17
|
+
end
|
|
18
|
+
%i[read write].each do |kind|
|
|
19
|
+
name = "~/#{File.basename(Keyfile.path(kind))}"
|
|
20
|
+
meta = Keyfile.meta(kind)
|
|
21
|
+
if meta.nil?
|
|
22
|
+
puts "#{name}: not logged in"
|
|
23
|
+
else
|
|
24
|
+
exp = meta[:expire_time] ? "expires #{local(meta[:expire_time])}" : "no expiry"
|
|
25
|
+
puts "#{name}: role #{meta[:role]}, #{exp} (#{meta[:addr]})"
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
0
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def local(iso)
|
|
34
|
+
Time.iso8601(iso).localtime.strftime("%Y-%m-%d %H:%M")
|
|
35
|
+
rescue ArgumentError
|
|
36
|
+
iso.to_s
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
require "time"
|
|
2
|
+
require_relative "command"
|
|
3
|
+
|
|
4
|
+
class EnvPact
|
|
5
|
+
class CLI
|
|
6
|
+
class Unlock < Command
|
|
7
|
+
command_name "unlock"
|
|
8
|
+
summary "Keep the read token in a local agent for a while (no passphrase for pull/run)"
|
|
9
|
+
banner "Usage: envpact unlock [--for=2h]"
|
|
10
|
+
description <<~DESC
|
|
11
|
+
Decrypts ~/.ob_read (passphrase on the terminal) and hands the token to a
|
|
12
|
+
local agent that serves only this user, for --for (default 2h; units s, m,
|
|
13
|
+
h; at most 8h and never past the token's own expiry). Writes (push/set)
|
|
14
|
+
never use the agent.
|
|
15
|
+
DESC
|
|
16
|
+
|
|
17
|
+
DURATION_RE = /\A(\d+)([smh])\z/.freeze
|
|
18
|
+
UNIT = { "s" => 1, "m" => 60, "h" => 3600 }.freeze
|
|
19
|
+
|
|
20
|
+
def default_opts
|
|
21
|
+
{ for: "2h" }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def configure(o, opts)
|
|
25
|
+
o.on("--for=DURATION", "--por=DURATION", "How long to stay unlocked (default 2h)") { |v| opts[:for] = v }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def self.parse_duration(text)
|
|
29
|
+
m = DURATION_RE.match(text.to_s)
|
|
30
|
+
return nil unless m
|
|
31
|
+
secs = m[1].to_i * UNIT.fetch(m[2])
|
|
32
|
+
secs.positive? ? secs : nil
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def call(_rest, opts)
|
|
36
|
+
secs = self.class.parse_duration(opts[:for])
|
|
37
|
+
raise UsageError, "--for must look like 30m, 2h or 90s (got '#{opts[:for]}')" unless secs
|
|
38
|
+
|
|
39
|
+
raise EnvPact::Error, "unlock needs a terminal to ask for the passphrase" unless @prompt.available?
|
|
40
|
+
unless File.exist?(Keyfile.path(:read))
|
|
41
|
+
raise EnvPact::Error, "no ~/.ob_read; run envpact login --read"
|
|
42
|
+
end
|
|
43
|
+
got = Keyfile.read(:read, passphrase: @prompt.secret("Passphrase for ~/.ob_read: "))
|
|
44
|
+
|
|
45
|
+
client = OpenBao::Client.new(addr: got[:addr], token: got[:token], role: :read)
|
|
46
|
+
client.renew_self
|
|
47
|
+
expire = fresh_expiry(client, got[:expire_time])
|
|
48
|
+
|
|
49
|
+
until_time = Time.now + secs
|
|
50
|
+
until_time = [until_time, expire].min if expire
|
|
51
|
+
raise EnvPact::Error, "the read token has already expired; run envpact login --read" if until_time <= Time.now
|
|
52
|
+
|
|
53
|
+
pid = Agent.start(token: got[:token], addr: got[:addr], until_time: until_time)
|
|
54
|
+
puts "✓ unlocked until #{Agent.cap_until(until_time).localtime.strftime('%Y-%m-%d %H:%M')} (agent pid #{pid})"
|
|
55
|
+
0
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
private
|
|
59
|
+
|
|
60
|
+
def fresh_expiry(client, stale)
|
|
61
|
+
text = client.lookup_self["expire_time"] || stale
|
|
62
|
+
text.nil? || text.to_s.empty? ? nil : Time.iso8601(text)
|
|
63
|
+
rescue OpenBao::Error, ArgumentError
|
|
64
|
+
stale && (Time.iso8601(stale) rescue nil)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
data/lib/envpact/cli.rb
CHANGED
|
@@ -2,6 +2,14 @@ require_relative "cli/command"
|
|
|
2
2
|
require_relative "cli/lint"
|
|
3
3
|
require_relative "cli/check"
|
|
4
4
|
require_relative "cli/exec"
|
|
5
|
+
require_relative "cli/login"
|
|
6
|
+
require_relative "cli/unlock"
|
|
7
|
+
require_relative "cli/lock"
|
|
8
|
+
require_relative "cli/status"
|
|
9
|
+
require_relative "cli/pull"
|
|
10
|
+
require_relative "cli/run"
|
|
11
|
+
require_relative "cli/push"
|
|
12
|
+
require_relative "cli/set"
|
|
5
13
|
require_relative "cli/scan_prompt"
|
|
6
14
|
|
|
7
15
|
class EnvPact
|
|
@@ -15,6 +23,14 @@ class EnvPact
|
|
|
15
23
|
"lint" => Lint,
|
|
16
24
|
"check" => Check,
|
|
17
25
|
"exec" => Exec,
|
|
26
|
+
"login" => Login,
|
|
27
|
+
"unlock" => Unlock,
|
|
28
|
+
"lock" => Lock,
|
|
29
|
+
"status" => Status,
|
|
30
|
+
"pull" => Pull,
|
|
31
|
+
"run" => Run,
|
|
32
|
+
"push" => Push,
|
|
33
|
+
"set" => Set,
|
|
18
34
|
"scan-prompt" => ScanPromptCommand,
|
|
19
35
|
}.freeze
|
|
20
36
|
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
class EnvPact
|
|
2
|
+
# Resolves tokens for the commands.
|
|
3
|
+
# read: the agent when unlocked, otherwise ~/.ob_read + passphrase
|
|
4
|
+
# write: always ~/.ob_write + passphrase; the agent is never consulted
|
|
5
|
+
class Credentials
|
|
6
|
+
def initialize(prompt: Prompt.new)
|
|
7
|
+
@prompt = prompt
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# -> { token:, addr: }
|
|
11
|
+
def read_token
|
|
12
|
+
reply = Agent.request("token")
|
|
13
|
+
if reply && reply["token"].is_a?(String) && !reply["token"].empty?
|
|
14
|
+
return { token: reply["token"], addr: reply["addr"] }
|
|
15
|
+
end
|
|
16
|
+
from_keyfile(:read)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def write_token
|
|
20
|
+
from_keyfile(:write)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# A ready OpenBao client for the read or write token. addr_hint is the
|
|
24
|
+
# address the spec asked for (or nil); with none, $BAO_ADDR, then the
|
|
25
|
+
# address the token was issued for. A token is only ever sent to the
|
|
26
|
+
# address it was issued for: any other address is refused rather than
|
|
27
|
+
# followed (a spec could otherwise point envpact at a server of its own).
|
|
28
|
+
def read_client(addr_hint = nil)
|
|
29
|
+
build_client(:read, read_token, addr_hint)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def write_client(addr_hint = nil)
|
|
33
|
+
build_client(:write, write_token, addr_hint)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Raises, without asking for anything, when ~/.ob_<kind> does not exist.
|
|
37
|
+
def self.require_keyfile!(kind)
|
|
38
|
+
return if File.exist?(Keyfile.path(kind))
|
|
39
|
+
raise Error, "no ~/#{File.basename(Keyfile.path(kind))}; run envpact login --#{kind}"
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
def build_client(kind, cred, addr_hint)
|
|
45
|
+
wanted = addr_hint || (ENV["BAO_ADDR"] unless ENV["BAO_ADDR"].to_s.empty?)
|
|
46
|
+
if wanted && cred[:addr] && wanted.sub(%r{/+\z}, "") != cred[:addr].sub(%r{/+\z}, "")
|
|
47
|
+
raise Error, "refusing to send the OpenBao token to a different address than the one it was " \
|
|
48
|
+
"issued for (#{cred[:addr]} vs #{wanted}); run envpact login --#{kind} --addr=#{wanted}"
|
|
49
|
+
end
|
|
50
|
+
OpenBao::Client.new(addr: cred[:addr] || wanted, token: cred[:token], role: kind)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def from_keyfile(kind)
|
|
54
|
+
# Fail before asking for a passphrase that cannot help.
|
|
55
|
+
self.class.require_keyfile!(kind)
|
|
56
|
+
name = "~/#{File.basename(Keyfile.path(kind))}"
|
|
57
|
+
got = Keyfile.read(kind, passphrase: @prompt.secret("Passphrase for #{name}: "))
|
|
58
|
+
{ token: got[:token], addr: got[:addr] }
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
data/lib/envpact/dotenv.rb
CHANGED
|
@@ -16,11 +16,14 @@ class EnvPact
|
|
|
16
16
|
files
|
|
17
17
|
end
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
# skip: absolute paths to leave out (the pull target is never an input).
|
|
20
|
+
def self.load(dir, env:, skip: [])
|
|
20
21
|
files = []
|
|
21
22
|
values = {}
|
|
23
|
+
skipped = skip.map { |f| File.expand_path(f) }
|
|
22
24
|
files_for(env).each do |name|
|
|
23
25
|
path = File.join(dir, name)
|
|
26
|
+
next if skipped.include?(File.expand_path(path))
|
|
24
27
|
next unless File.file?(path)
|
|
25
28
|
files << name
|
|
26
29
|
values.merge!(parse(EnvPact.read_text(path)))
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
class EnvPact
|
|
2
|
+
module Dotenv
|
|
3
|
+
DUMP_KEY_RE = /\A[A-Za-z_][A-Za-z0-9_]*\z/.freeze
|
|
4
|
+
|
|
5
|
+
# One KEY="value" line per key, sorted. Escapes \ " and newline, the
|
|
6
|
+
# inverse of parse_value, so Dotenv.parse(dump(h)) == h. A value with a
|
|
7
|
+
# carriage return cannot round-trip (parse strips it) and is refused;
|
|
8
|
+
# errors name the key, never the value.
|
|
9
|
+
def self.dump(hash)
|
|
10
|
+
hash.keys.sort.map do |key|
|
|
11
|
+
raise Error, "cannot write key '#{key}' to a dotenv file (invalid name)" unless DUMP_KEY_RE.match?(key)
|
|
12
|
+
value = hash[key].to_s
|
|
13
|
+
raise Error, "value of '#{key}' contains a carriage return; it cannot be written to a dotenv file" if value.include?("\r")
|
|
14
|
+
escaped = value.gsub(/[\\"\n]/) { |c| c == "\n" ? "\\n" : "\\#{c}" }
|
|
15
|
+
%(#{key}="#{escaped}"\n)
|
|
16
|
+
end.join
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
require "openssl"
|
|
3
|
+
|
|
4
|
+
class EnvPact
|
|
5
|
+
# Token files ~/.ob_read and ~/.ob_write: the token is encrypted with
|
|
6
|
+
# AES-256-GCM under a key derived from a passphrase (PBKDF2-HMAC-SHA256).
|
|
7
|
+
# addr, role and expire_time stay readable (for `status`) but, together with
|
|
8
|
+
# the file kind, are bound to the ciphertext as GCM additional data, so
|
|
9
|
+
# editing them breaks decryption.
|
|
10
|
+
module Keyfile
|
|
11
|
+
VERSION = 1
|
|
12
|
+
KDF = "pbkdf2-sha256".freeze
|
|
13
|
+
ITERATIONS = 600_000
|
|
14
|
+
MAX_ITERATIONS = 10_000_000
|
|
15
|
+
KINDS = %i[read write].freeze
|
|
16
|
+
|
|
17
|
+
def self.path(kind)
|
|
18
|
+
File.join(Dir.home, basename(kind))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.write(kind, token:, addr:, role:, expire_time:, passphrase:, iterations: ITERATIONS)
|
|
22
|
+
raise Error, "empty passphrase" if passphrase.nil? || passphrase.empty?
|
|
23
|
+
salt = OpenSSL::Random.random_bytes(16)
|
|
24
|
+
cipher = OpenSSL::Cipher.new("aes-256-gcm").encrypt
|
|
25
|
+
iv = cipher.random_iv
|
|
26
|
+
cipher.key = derive(passphrase, salt, iterations)
|
|
27
|
+
cipher.iv = iv
|
|
28
|
+
cipher.auth_data = aad(kind, addr, role, expire_time)
|
|
29
|
+
ct = cipher.update(token.dup.force_encoding(Encoding::BINARY)) + cipher.final
|
|
30
|
+
tag = cipher.auth_tag
|
|
31
|
+
|
|
32
|
+
doc = {
|
|
33
|
+
"v" => VERSION, "kdf" => KDF, "iter" => iterations,
|
|
34
|
+
"salt" => b64(salt), "iv" => b64(iv), "tag" => b64(tag), "ct" => b64(ct),
|
|
35
|
+
"addr" => addr, "role" => role, "expire_time" => expire_time,
|
|
36
|
+
"created_at" => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
|
|
37
|
+
}
|
|
38
|
+
AtomicFile.write(path(kind), JSON.pretty_generate(doc) + "\n")
|
|
39
|
+
nil
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Readable without the passphrase; nil when absent or unparseable.
|
|
43
|
+
def self.meta(kind)
|
|
44
|
+
doc = load_doc(kind)
|
|
45
|
+
return nil unless doc.is_a?(Hash)
|
|
46
|
+
{ addr: doc["addr"], role: doc["role"], expire_time: doc["expire_time"] }
|
|
47
|
+
rescue Errno::ENOENT, JSON::ParserError, SystemCallError
|
|
48
|
+
nil
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def self.read(kind, passphrase:)
|
|
52
|
+
file = path(kind)
|
|
53
|
+
name = "~/#{basename(kind)}"
|
|
54
|
+
raise Error, "no #{name}; run envpact login --#{kind}" unless File.exist?(file)
|
|
55
|
+
st = File.stat(file)
|
|
56
|
+
raise Error, "#{name} is readable by others; chmod 600 #{name}" if (st.mode & 0o077) != 0
|
|
57
|
+
|
|
58
|
+
corrupted = Error.new("wrong passphrase or corrupted #{name}")
|
|
59
|
+
doc = begin
|
|
60
|
+
JSON.parse(File.read(file))
|
|
61
|
+
rescue JSON::ParserError
|
|
62
|
+
raise corrupted
|
|
63
|
+
end
|
|
64
|
+
raise corrupted unless doc.is_a?(Hash) && doc["v"] == VERSION && doc["kdf"] == KDF
|
|
65
|
+
iter = doc["iter"]
|
|
66
|
+
raise corrupted unless iter.is_a?(Integer) && iter.between?(1, MAX_ITERATIONS)
|
|
67
|
+
|
|
68
|
+
begin
|
|
69
|
+
cipher = OpenSSL::Cipher.new("aes-256-gcm").decrypt
|
|
70
|
+
cipher.key = derive(passphrase, unb64(doc["salt"]), iter)
|
|
71
|
+
cipher.iv = unb64(doc["iv"])
|
|
72
|
+
cipher.auth_tag = unb64(doc["tag"])
|
|
73
|
+
cipher.auth_data = aad(kind, doc["addr"], doc["role"], doc["expire_time"])
|
|
74
|
+
token = cipher.update(unb64(doc["ct"])) + cipher.final
|
|
75
|
+
rescue OpenSSL::Cipher::CipherError, ArgumentError, TypeError, NoMethodError
|
|
76
|
+
raise corrupted
|
|
77
|
+
end
|
|
78
|
+
{ token: token.force_encoding(Encoding::UTF_8), addr: doc["addr"], role: doc["role"], expire_time: doc["expire_time"] }
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def self.basename(kind)
|
|
82
|
+
raise ArgumentError, "unknown key file kind: #{kind.inspect}" unless KINDS.include?(kind)
|
|
83
|
+
kind == :read ? ".ob_read" : ".ob_write"
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def self.load_doc(kind)
|
|
87
|
+
JSON.parse(File.read(path(kind)))
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def self.derive(passphrase, salt, iterations)
|
|
91
|
+
OpenSSL::KDF.pbkdf2_hmac(passphrase, salt: salt, iterations: iterations, length: 32, hash: "sha256")
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def self.aad(kind, addr, role, expire_time)
|
|
95
|
+
JSON.generate(["envpact-keyfile", VERSION, kind.to_s, addr, role, expire_time])
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def self.b64(bytes)
|
|
99
|
+
[bytes].pack("m0")
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def self.unb64(text)
|
|
103
|
+
raise ArgumentError, "not a string" unless text.is_a?(String)
|
|
104
|
+
text.unpack("m0").first
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
private_class_method :basename, :load_doc, :derive, :aad, :b64, :unb64
|
|
108
|
+
end
|
|
109
|
+
end
|