vaultkit 1.5.0 → 2.0.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.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +163 -30
  3. data/bin/vkit +8 -0
  4. data/lib/vkit/cli/api/client.rb +15 -3
  5. data/lib/vkit/cli/base_cli.rb +92 -14
  6. data/lib/vkit/cli/commands/agent_tokens_create_command.rb +37 -5
  7. data/lib/vkit/cli/commands/agent_tokens_list_command.rb +2 -0
  8. data/lib/vkit/cli/commands/agent_tokens_revoke_command.rb +2 -0
  9. data/lib/vkit/cli/commands/approval_command.rb +8 -0
  10. data/lib/vkit/cli/commands/approval_watch_command.rb +6 -5
  11. data/lib/vkit/cli/commands/base_command.rb +56 -2
  12. data/lib/vkit/cli/commands/datasource_command.rb +137 -20
  13. data/lib/vkit/cli/commands/doctor_command.rb +69 -0
  14. data/lib/vkit/cli/commands/fetch_command.rb +7 -1
  15. data/lib/vkit/cli/commands/grant_revoke_command.rb +3 -0
  16. data/lib/vkit/cli/commands/init_command.rb +63 -10
  17. data/lib/vkit/cli/commands/login_command.rb +38 -4
  18. data/lib/vkit/cli/commands/logout_command.rb +54 -1
  19. data/lib/vkit/cli/commands/policy_bundle_command.rb +114 -26
  20. data/lib/vkit/cli/commands/policy_deploy_command.rb +113 -7
  21. data/lib/vkit/cli/commands/policy_pack_add_command.rb +16 -1
  22. data/lib/vkit/cli/commands/policy_pack_info_command.rb +10 -2
  23. data/lib/vkit/cli/commands/policy_pack_list_command.rb +4 -0
  24. data/lib/vkit/cli/commands/policy_pack_remove_command.rb +3 -0
  25. data/lib/vkit/cli/commands/policy_pack_upgrade_command.rb +35 -1
  26. data/lib/vkit/cli/commands/policy_revoke_command.rb +4 -0
  27. data/lib/vkit/cli/commands/policy_validate_command.rb +4 -1
  28. data/lib/vkit/cli/commands/registry_diff_command.rb +14 -2
  29. data/lib/vkit/cli/commands/registry_export_command.rb +30 -1
  30. data/lib/vkit/cli/commands/request_command.rb +27 -4
  31. data/lib/vkit/cli/commands/requests_list_command.rb +2 -0
  32. data/lib/vkit/cli/commands/reset_command.rb +12 -7
  33. data/lib/vkit/cli/commands/scan_command.rb +100 -0
  34. data/lib/vkit/cli/commands/setup_login_command.rb +274 -0
  35. data/lib/vkit/cli/commands/whoami_command.rb +16 -2
  36. data/lib/vkit/cli/doctor/mcp_probe.rb +108 -0
  37. data/lib/vkit/cli/doctor.rb +698 -0
  38. data/lib/vkit/cli/error_map.rb +243 -0
  39. data/lib/vkit/cli/errors.rb +32 -0
  40. data/lib/vkit/cli/output.rb +161 -0
  41. data/lib/vkit/cli/policy_bundle_validator.rb +8 -0
  42. data/lib/vkit/cli/secret_input.rb +50 -0
  43. data/lib/vkit/cli.rb +12 -2
  44. data/lib/vkit/core/auth_client.rb +57 -1
  45. data/lib/vkit/core/credential_store.rb +38 -1
  46. data/lib/vkit/core/user_agent.rb +30 -0
  47. data/lib/vkit/policy/approval_groups.rb +47 -0
  48. data/lib/vkit/policy/bundle_compiler.rb +8 -2
  49. data/lib/vkit/policy/git_state.rb +114 -0
  50. data/lib/vkit/policy/policy_validator.rb +20 -0
  51. data/lib/vkit/policy/schema/policy_bundle.schema.json +4 -0
  52. data/lib/vkit/version.rb +1 -1
  53. metadata +40 -2
@@ -29,17 +29,52 @@ module Vkit
29
29
  @fallback_path = File.join(Dir.home, ".vkit", "credentials.json")
30
30
  end
31
31
 
32
- def save(endpoint:, token:, user:)
32
+ # setup: for a setup session's token (`vkit login --setup`), what
33
+ # marks it as one: { "expires_at", "organization" }. A normal login
34
+ # saves none, which also drops a setup session's.
35
+ def save(endpoint:, token:, user:, setup: nil)
33
36
  payload = {
34
37
  "endpoint" => endpoint,
35
38
  "token" => token,
36
39
  "user" => user
37
40
  }
41
+ payload["setup"] = setup if setup
38
42
 
39
43
  store(payload)
40
44
  true
41
45
  end
42
46
 
47
+ # The saved login's setup session ({ "expires_at", "organization" }),
48
+ # or nil for a normal login. Credentials from the environment have
49
+ # none: the control plane says when a setup token there expires.
50
+ def setup_session
51
+ return nil if env_override
52
+
53
+ stored_payload&.dig("setup")
54
+ end
55
+
56
+ # A setup session that was requested but not yet approved, kept so
57
+ # `vkit login --setup --wait` can pick it up in a later command:
58
+ # { "endpoint", "device_code", "user_code", "verification_url",
59
+ # "verification_url_complete", "interval", "expires_at" }.
60
+ def pending_setup
61
+ stored_payload&.dig("pending_setup")
62
+ end
63
+
64
+ def save_pending_setup(pending)
65
+ payload = stored_payload || {}
66
+ payload["pending_setup"] = pending
67
+ store(payload)
68
+ end
69
+
70
+ def clear_pending_setup!
71
+ payload = stored_payload
72
+ return unless payload&.key?("pending_setup")
73
+
74
+ payload.delete("pending_setup")
75
+ store(payload)
76
+ end
77
+
43
78
  def save_user(user)
44
79
  if env_override
45
80
  @override_user = user
@@ -105,6 +140,8 @@ module Vkit
105
140
  return unless payload
106
141
 
107
142
  payload.delete("token")
143
+ payload.delete("setup")
144
+ payload.delete("pending_setup")
108
145
  store(payload)
109
146
  end
110
147
 
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rbconfig"
4
+ require_relative "../version"
5
+
6
+ module Vkit
7
+ module Core
8
+ # The User-Agent on every request vkit makes, e.g.
9
+ # "vkit/1.5.0 (macOS arm64; ruby 3.3.6)", so the console's setup-session
10
+ # approval page (and the control plane's logs) show what asked, rather
11
+ # than Net::HTTP's bare "Ruby".
12
+ module UserAgent
13
+ module_function
14
+
15
+ def to_s
16
+ @to_s ||= "vkit/#{Vkit::VERSION} (#{os_name} #{RbConfig::CONFIG['host_cpu']}; ruby #{RUBY_VERSION})"
17
+ end
18
+
19
+ def os_name(host_os = RbConfig::CONFIG["host_os"])
20
+ case host_os
21
+ when /darwin/ then "macOS"
22
+ when /linux/ then "Linux"
23
+ when /mswin|mingw|cygwin/ then "Windows"
24
+ when /freebsd/ then "FreeBSD"
25
+ else host_os
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vkit
4
+ module Policy
5
+ # Approval group rules shared with the control plane
6
+ # (User::RESERVED_GROUPS and PolicyApprovals::Staffing there):
7
+ #
8
+ # - Built-in role names can't be approval groups. Admins can already
9
+ # approve any request, and a group named admin would let its members
10
+ # approve as admins.
11
+ # - A group nobody active is in doesn't stop a deploy, but warns: org
12
+ # admins approve its requests until someone is added.
13
+ module ApprovalGroups
14
+ RESERVED = %w[admin member approver agent].freeze
15
+ WARNING_CODE = "approval_group_has_no_members"
16
+ WARNING = "%<group>s has no members, so org admins will approve its requests until someone is added."
17
+
18
+ module_function
19
+
20
+ # Every group a compiled bundle's require_approval policies name, as
21
+ # approver_role or in approver_roles, sorted.
22
+ def named(bundle)
23
+ Array(bundle["policies"]).flat_map do |policy|
24
+ next [] unless policy["action"].to_s == "require_approval"
25
+
26
+ approval = policy["approval"] || {}
27
+ ([approval["approver_role"]] + Array(approval["approver_roles"])).map { |r| r.to_s.strip }
28
+ end.reject(&:empty?).uniq.sort
29
+ end
30
+
31
+ # One warning per named group with no active members, given the
32
+ # groups GET /approval_groups returns. Control planes from before
33
+ # active_member_count send member_count, which counts active members.
34
+ def warnings(bundle, groups)
35
+ members = Array(groups).to_h do |g|
36
+ [g["name"].to_s, (g.key?("active_member_count") ? g["active_member_count"] : g["member_count"]).to_i]
37
+ end
38
+
39
+ named(bundle).filter_map do |group|
40
+ next if members.fetch(group, 0).positive?
41
+
42
+ { "code" => WARNING_CODE, "group" => group, "message" => format(WARNING, group: group) }
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -15,7 +15,13 @@ module Vkit
15
15
 
16
16
  # Raises if a policy can never match (see MatchCheck), or if an
17
17
  # installed pack is missing policies (see PackIntegrity).
18
- def self.compile!(org_slug:, bundle_version:, policies_dir:, registry_dir:, source: {}, project_root: Dir.pwd)
18
+ #
19
+ # The checksum covers created_at (the control plane checks the whole
20
+ # bundle), so a rebuild only gets the same checksum, and a redeploy is
21
+ # a no-op, when created_at comes from the inputs rather than the clock.
22
+ # `vkit policy bundle` passes the commit's time; see its created_at.
23
+ def self.compile!(org_slug:, bundle_version:, policies_dir:, registry_dir:, source: {}, project_root: Dir.pwd,
24
+ created_at: Time.now)
19
25
  policies = load_policies(policies_dir)
20
26
  installed_packs = PackIntegrity.check!(project_root: project_root, policies_dir: policies_dir, policies: policies)
21
27
  raw_registry = load_raw_registry(registry_dir)
@@ -27,7 +33,7 @@ module Vkit
27
33
  "format_version" => FORMAT_VERSION,
28
34
  "org_slug" => org_slug,
29
35
  "bundle_version" => bundle_version,
30
- "created_at" => Time.now.utc.iso8601,
36
+ "created_at" => created_at.utc.iso8601,
31
37
  "source" => normalize_source(source),
32
38
  "checksum" => "" # filled below
33
39
  },
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "open3"
4
+ require "pathname"
5
+ require "time"
6
+
7
+ module Vkit
8
+ module Policy
9
+ # The git state of a policy project, for a bundle's version and source.
10
+ # A bundle's version is the commit its policies were committed in, so it
11
+ # can be traced back to them; that only holds when nothing the bundle
12
+ # reads has changed since that commit.
13
+ class GitState
14
+ attr_reader :root
15
+
16
+ def initialize(root)
17
+ @root = root
18
+ end
19
+
20
+ def repo?
21
+ git("rev-parse", "--is-inside-work-tree").first == "true"
22
+ end
23
+
24
+ # nil in a repo with no commits yet.
25
+ def head
26
+ out, ok = git("rev-parse", "--verify", "--quiet", "HEAD")
27
+ ok && !out.empty? ? out : nil
28
+ end
29
+
30
+ # When rev was committed (its committer date), or nil.
31
+ def commit_time(rev)
32
+ out, ok = git("show", "-s", "--format=%cI", rev, "--")
33
+ ok && !out.empty? ? Time.iso8601(out) : nil
34
+ rescue ArgumentError
35
+ nil
36
+ end
37
+
38
+ def branch
39
+ out, ok = git("rev-parse", "--abbrev-ref", "HEAD")
40
+ ok && !out.empty? && out != "HEAD" ? out : nil
41
+ end
42
+
43
+ def remote_url
44
+ out, ok = git("config", "--get", "remote.origin.url")
45
+ ok && !out.empty? ? out : nil
46
+ end
47
+
48
+ # Whether rev names a commit in this repository (a bundle version
49
+ # that's a commit SHA, rather than a --version label).
50
+ def commit?(rev)
51
+ return false if rev.to_s.strip.empty? || rev.to_s.start_with?("-")
52
+
53
+ git("cat-file", "-e", "#{rev}^{commit}").last
54
+ end
55
+
56
+ # Whether anything under `paths` differs between two commits. Paths
57
+ # are relative to `root`; ones that exist in neither are ignored.
58
+ def changed_between?(from, to, paths)
59
+ out, ok = git("diff", "--name-only", from, to, "--", *paths)
60
+ raise "git diff failed in #{root}" unless ok
61
+
62
+ !out.empty?
63
+ end
64
+
65
+ # How many commits `to` has that `from` doesn't (nil when unknown).
66
+ def commits_between(from, to)
67
+ out, ok = git("rev-list", "--count", "#{from}..#{to}")
68
+ ok ? out.to_i : nil
69
+ end
70
+
71
+ # Files under `paths` that differ from HEAD: modified, staged, new,
72
+ # deleted, or ignored by .gitignore (they'd be bundled but never
73
+ # committed). [{ path:, ignored: }], with paths relative to `root`.
74
+ def uncommitted(paths)
75
+ existing = paths.select { |p| File.exist?(File.join(root, p)) }
76
+ return [] if existing.empty?
77
+
78
+ out, ok = git_raw("status", "--porcelain=v1", "--untracked-files=all", "--ignored=traditional", "--", *existing)
79
+ raise "git status failed in #{root}" unless ok
80
+
81
+ out.lines.map(&:chomp).reject(&:empty?).map do |line|
82
+ path = line[3..].sub(/\A"(.*)"\z/, '\1').split(" -> ").last.chomp("/")
83
+ { path: from_root(path), ignored: line.start_with?("!!") }
84
+ end
85
+ end
86
+
87
+ private
88
+
89
+ # git status prints paths relative to the repository's top level, which
90
+ # isn't `root` when the policy project is a subfolder of a bigger repo.
91
+ def from_root(path)
92
+ top, = git("rev-parse", "--show-toplevel")
93
+ Pathname.new(File.join(File.realpath(top), path))
94
+ .relative_path_from(Pathname.new(File.realpath(root))).to_s
95
+ end
96
+
97
+ # [stripped stdout, success?], for commands that print one value.
98
+ def git(*args)
99
+ out, ok = git_raw(*args)
100
+ [out.strip, ok]
101
+ end
102
+
103
+ # [stdout, success?] as printed: `git status --porcelain` lines start
104
+ # with a status column that may be a space. Never raises when git is
105
+ # missing.
106
+ def git_raw(*args)
107
+ out, _err, status = Open3.capture3("git", *args, chdir: root)
108
+ [out, status.success?]
109
+ rescue SystemCallError
110
+ ["", false]
111
+ end
112
+ end
113
+ end
114
+ end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "approval_groups"
4
+
3
5
  module Vkit
4
6
  module Policy
5
7
  class PolicyValidator
@@ -95,12 +97,30 @@ module Vkit
95
97
 
96
98
  APPROVAL_MODES = %w[all any].freeze
97
99
 
100
+ # admin, member, approver and agent are built-in roles, not approval
101
+ # groups (the control plane refuses them too).
102
+ def self.validate_not_reserved!(names, prefix)
103
+ reserved = names.map { |n| n.to_s.strip } & ApprovalGroups::RESERVED
104
+ return if reserved.empty?
105
+
106
+ raise ValidationError, <<~MSG
107
+ #{prefix}#{reserved.join(', ')} #{reserved.size == 1 ? 'is a built-in role' : 'are built-in roles'}, not an approval group.
108
+
109
+ Name an approval group instead (admins can already approve any request), for example:
110
+
111
+ action:
112
+ require_approval: true
113
+ approver_role: data_owner
114
+ MSG
115
+ end
116
+
98
117
  # One group (approver_role), or several (approver_roles) with
99
118
  # approval_mode: all (the default, every group signs off) or any (one
100
119
  # of them does).
101
120
  def self.validate_approvers!(action, prefix)
102
121
  single = action["approver_role"]
103
122
  several = action["approver_roles"]
123
+ validate_not_reserved!([single, *Array(several)], prefix)
104
124
 
105
125
  if single && several
106
126
  raise ValidationError, <<~MSG
@@ -57,6 +57,10 @@
57
57
  "commit_sha": {
58
58
  "type": "string",
59
59
  "pattern": "^[a-f0-9]{7,40}$"
60
+ },
61
+ "dirty": {
62
+ "type": "boolean",
63
+ "description": "True when the bundle was built from uncommitted changes (vkit policy bundle --allow-dirty), so commit_sha doesn't contain all of it."
60
64
  }
61
65
  }
62
66
  },
data/lib/vkit/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Vkit
4
- VERSION = "1.5.0"
4
+ VERSION = "2.0.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vaultkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nnamdi Ogundu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-10 00:00:00.000000000 Z
11
+ date: 2026-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -38,6 +38,34 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '2.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '13.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '13.0'
41
69
  description: Command-line interface for interacting with the VaultKit control plane
42
70
  email:
43
71
  - founders@vaultkit.io
@@ -62,6 +90,7 @@ files:
62
90
  - lib/vkit/cli/commands/approval_watch_command.rb
63
91
  - lib/vkit/cli/commands/base_command.rb
64
92
  - lib/vkit/cli/commands/datasource_command.rb
93
+ - lib/vkit/cli/commands/doctor_command.rb
65
94
  - lib/vkit/cli/commands/fetch_command.rb
66
95
  - lib/vkit/cli/commands/grant_revoke_command.rb
67
96
  - lib/vkit/cli/commands/init_command.rb
@@ -82,11 +111,17 @@ files:
82
111
  - lib/vkit/cli/commands/requests_list_command.rb
83
112
  - lib/vkit/cli/commands/reset_command.rb
84
113
  - lib/vkit/cli/commands/scan_command.rb
114
+ - lib/vkit/cli/commands/setup_login_command.rb
85
115
  - lib/vkit/cli/commands/whoami_command.rb
116
+ - lib/vkit/cli/doctor.rb
117
+ - lib/vkit/cli/doctor/mcp_probe.rb
118
+ - lib/vkit/cli/error_map.rb
86
119
  - lib/vkit/cli/errors.rb
120
+ - lib/vkit/cli/output.rb
87
121
  - lib/vkit/cli/policy_bundle_validator.rb
88
122
  - lib/vkit/cli/policy_pack/manager.rb
89
123
  - lib/vkit/cli/requests_cli.rb
124
+ - lib/vkit/cli/secret_input.rb
90
125
  - lib/vkit/core/ansi.rb
91
126
  - lib/vkit/core/auth_client.rb
92
127
  - lib/vkit/core/credential_resolver.rb
@@ -94,7 +129,10 @@ files:
94
129
  - lib/vkit/core/registry_diff.rb
95
130
  - lib/vkit/core/registry_diff_printer.rb
96
131
  - lib/vkit/core/table_formatter.rb
132
+ - lib/vkit/core/user_agent.rb
133
+ - lib/vkit/policy/approval_groups.rb
97
134
  - lib/vkit/policy/bundle_compiler.rb
135
+ - lib/vkit/policy/git_state.rb
98
136
  - lib/vkit/policy/match_check.rb
99
137
  - lib/vkit/policy/pack_integrity.rb
100
138
  - lib/vkit/policy/packs/ai_safety/metadata.yaml