lex-vault 0.1.4 → 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.
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'legion/json'
4
+ require 'legion/extensions/vault/errors'
3
5
  require 'legion/extensions/vault/helpers/client'
4
6
 
5
7
  module Legion
@@ -10,68 +12,74 @@ module Legion
10
12
  include Legion::Extensions::Vault::Helpers::Client
11
13
 
12
14
  def read_secret(path:, mount: 'secret', version: nil, **)
13
- url = "/v1/#{mount}/data/#{path}"
14
15
  params = version ? { version: version } : {}
15
- response = connection(**).get(url, params)
16
- { result: response.body }
16
+ vault_normalize(connection(**).get("/v1/#{mount}/data/#{path}", params).body)
17
17
  end
18
18
 
19
- def write_secret(path:, data:, mount: 'secret', cas: nil, **)
20
- url = "/v1/#{mount}/data/#{path}"
19
+ def write_secret(path:, data:, mount: 'secret', cas: nil, read_only: false, **)
20
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
21
+
21
22
  body = { data: data }
22
23
  body[:options] = { cas: cas } if cas
23
- response = connection(**).post(url, body)
24
- { result: response.body }
24
+ vault_normalize(connection(**).post("/v1/#{mount}/data/#{path}", body).body)
25
25
  end
26
26
 
27
- def patch_secret(path:, data:, mount: 'secret', **)
28
- url = "/v1/#{mount}/data/#{path}"
27
+ def patch_secret(path:, data:, mount: 'secret', read_only: false, **)
28
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
29
+
29
30
  conn = connection(**)
30
31
  conn.headers['Content-Type'] = 'application/merge-patch+json'
31
- response = conn.patch(url, { data: data })
32
- { result: response.body }
32
+ # Explicit serialize: with a non-json Content-Type pre-set,
33
+ # Faraday's json middleware skips body serialization.
34
+ response = conn.patch("/v1/#{mount}/data/#{path}", json_dump({ data: data }))
35
+ vault_normalize(response.body)
33
36
  end
34
37
 
35
- def delete_secret(path:, mount: 'secret', **)
36
- response = connection(**).delete("/v1/#{mount}/data/#{path}")
37
- { result: response.status == 204 }
38
+ def delete_secret(path:, mount: 'secret', read_only: false, **)
39
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
40
+
41
+ { status: connection(**).delete("/v1/#{mount}/data/#{path}").status }
38
42
  end
39
43
 
40
- def delete_versions(path:, versions:, mount: 'secret', **)
41
- response = connection(**).post("/v1/#{mount}/delete/#{path}", { versions: versions })
42
- { result: response.status == 204 }
44
+ def delete_versions(path:, versions:, mount: 'secret', read_only: false, **)
45
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
46
+
47
+ { status: connection(**).post("/v1/#{mount}/delete/#{path}", { versions: versions }).status }
43
48
  end
44
49
 
45
- def undelete_versions(path:, versions:, mount: 'secret', **)
46
- response = connection(**).post("/v1/#{mount}/undelete/#{path}", { versions: versions })
47
- { result: response.status == 204 }
50
+ def undelete_versions(path:, versions:, mount: 'secret', read_only: false, **)
51
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
52
+
53
+ { status: connection(**).post("/v1/#{mount}/undelete/#{path}", { versions: versions }).status }
48
54
  end
49
55
 
50
- def destroy_versions(path:, versions:, mount: 'secret', **)
51
- response = connection(**).put("/v1/#{mount}/destroy/#{path}", { versions: versions })
52
- { result: response.status == 204 }
56
+ def destroy_versions(path:, versions:, mount: 'secret', read_only: false, **)
57
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
58
+
59
+ { status: connection(**).put("/v1/#{mount}/destroy/#{path}", { versions: versions }).status }
53
60
  end
54
61
 
55
62
  def list_secrets(path:, mount: 'secret', **)
56
- response = connection(**).get("/v1/#{mount}/metadata/#{path}", { list: true })
57
- { result: response.body }
63
+ vault_normalize(connection(**).get("/v1/#{mount}/metadata/#{path}", { list: true }).body)
58
64
  end
59
65
 
60
66
  def read_metadata(path:, mount: 'secret', **)
61
- response = connection(**).get("/v1/#{mount}/metadata/#{path}")
62
- { result: response.body }
67
+ vault_normalize(connection(**).get("/v1/#{mount}/metadata/#{path}").body)
63
68
  end
64
69
 
65
- def write_metadata(path:, mount: 'secret', max_versions: nil, cas_required: nil, delete_version_after: nil, **)
70
+ def write_metadata(path:, max_versions: nil, cas_required: nil, delete_version_after: nil,
71
+ mount: 'secret', read_only: false, **)
72
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
73
+
66
74
  body = { max_versions: max_versions, cas_required: cas_required,
67
75
  delete_version_after: delete_version_after }.compact
68
- response = connection(**).post("/v1/#{mount}/metadata/#{path}", body)
69
- { result: response.status == 204 }
76
+ { status: connection(**).post("/v1/#{mount}/metadata/#{path}", body).status }
70
77
  end
71
78
 
72
- def delete_metadata(path:, mount: 'secret', **)
73
- response = connection(**).delete("/v1/#{mount}/metadata/#{path}")
74
- { result: response.status == 204 }
79
+ def delete_metadata(path:, mount: 'secret', read_only: false, **)
80
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
81
+
82
+ { status: connection(**).delete("/v1/#{mount}/metadata/#{path}").status }
75
83
  end
76
84
 
77
85
  include Legion::Extensions::Helpers::Lex if Legion::Extensions.const_defined?(:Helpers, false) &&
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'legion/extensions/vault/errors'
3
4
  require 'legion/extensions/vault/helpers/client'
4
5
 
5
6
  module Legion
@@ -10,24 +11,24 @@ module Legion
10
11
  include Legion::Extensions::Vault::Helpers::Client
11
12
 
12
13
  def lookup_lease(lease_id:, **)
13
- response = connection(**).put('/v1/sys/leases/lookup', { lease_id: lease_id })
14
- { result: response.body }
14
+ vault_normalize(connection(**).put('/v1/sys/leases/lookup', { lease_id: lease_id }).body)
15
15
  end
16
16
 
17
- def renew_lease(lease_id:, increment: nil, **)
17
+ def renew_lease(lease_id:, increment: nil, read_only: false, **)
18
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
19
+
18
20
  body = { lease_id: lease_id, increment: increment }.compact
19
- response = connection(**).put('/v1/sys/leases/renew', body)
20
- { result: response.body }
21
+ vault_normalize(connection(**).put('/v1/sys/leases/renew', body).body)
21
22
  end
22
23
 
23
- def revoke_lease(lease_id:, **)
24
- response = connection(**).put('/v1/sys/leases/revoke', { lease_id: lease_id })
25
- { result: response.status == 204 }
24
+ def revoke_lease(lease_id:, read_only: false, **)
25
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
26
+
27
+ { status: connection(**).put('/v1/sys/leases/revoke', { lease_id: lease_id }).status }
26
28
  end
27
29
 
28
30
  def list_leases(prefix:, **)
29
- response = connection(**).get("/v1/sys/leases/lookup/#{prefix}", { list: true })
30
- { result: response.body }
31
+ vault_normalize(connection(**).get("/v1/sys/leases/lookup/#{prefix}", { list: true }).body)
31
32
  end
32
33
 
33
34
  include Legion::Extensions::Helpers::Lex if Legion::Extensions.const_defined?(:Helpers, false) &&
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'legion/extensions/vault/errors'
3
4
  require 'legion/extensions/vault/helpers/client'
4
5
 
5
6
  module Legion
@@ -10,23 +11,23 @@ module Legion
10
11
  include Legion::Extensions::Vault::Helpers::Client
11
12
 
12
13
  def list_namespaces(**)
13
- response = connection(**).get('/v1/sys/namespaces')
14
- { result: response.body }
14
+ vault_normalize(connection(**).get('/v1/sys/namespaces').body)
15
15
  end
16
16
 
17
17
  def get_namespace(name:, **)
18
- response = connection(**).get("/v1/sys/namespaces/#{name}")
19
- { result: response.body }
18
+ vault_normalize(connection(**).get("/v1/sys/namespaces/#{name}").body)
20
19
  end
21
20
 
22
- def create_namespace(name:, **)
23
- response = connection(**).post("/v1/sys/namespaces/#{name}")
24
- { result: response.body }
21
+ def create_namespace(name:, read_only: false, **)
22
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
23
+
24
+ vault_normalize(connection(**).post("/v1/sys/namespaces/#{name}").body)
25
25
  end
26
26
 
27
- def delete_namespace(name:, **)
28
- response = connection(**).delete("/v1/sys/namespaces/#{name}")
29
- { result: response.status == 204 }
27
+ def delete_namespace(name:, read_only: false, **)
28
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
29
+
30
+ { status: connection(**).delete("/v1/sys/namespaces/#{name}").status }
30
31
  end
31
32
 
32
33
  include Legion::Extensions::Helpers::Lex if Legion::Extensions.const_defined?(:Helpers, false) &&
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'legion/extensions/vault/errors'
4
+ require 'legion/extensions/vault/helpers/client'
5
+
6
+ module Legion
7
+ module Extensions
8
+ module Vault
9
+ module Runners
10
+ module Nomad
11
+ include Legion::Extensions::Vault::Helpers::Client
12
+
13
+ def create_role(name:, policies: nil, global: true, type: 'management', mount: 'nomad', read_only: false, **)
14
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
15
+
16
+ body = { name: name, policies: policies, global: global, type: type }
17
+ { status: connection(**).post("/v1/#{mount}/role/#{name}", body).status }
18
+ end
19
+
20
+ def read_role(name:, mount: 'nomad', **)
21
+ vault_normalize(connection(**).get("/v1/#{mount}/role/#{name}").body)
22
+ end
23
+
24
+ def list_roles(mount: 'nomad', **)
25
+ vault_normalize(connection(**).get("/v1/#{mount}/role", { list: true }).body)
26
+ end
27
+
28
+ def delete_role(name:, mount: 'nomad', read_only: false, **)
29
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
30
+
31
+ { status: connection(**).delete("/v1/#{mount}/role/#{name}").status }
32
+ end
33
+
34
+ # Nomad mints credentials via POST /cred/{accessor} — unlike the
35
+ # GET /creds/{role} shape of the other dynamic engines.
36
+ def generate_credential(accessor:, mount: 'nomad', **)
37
+ vault_normalize(connection(**).post("/v1/#{mount}/cred/#{accessor}").body)
38
+ end
39
+
40
+ def configure_access(mount: 'nomad', read_only: false, **)
41
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
42
+
43
+ { status: connection(**).post("/v1/#{mount}/config/access", {}).status }
44
+ end
45
+
46
+ def read_config_access(mount: 'nomad', **)
47
+ vault_normalize(connection(**).get("/v1/#{mount}/config/access").body)
48
+ end
49
+
50
+ def configure_lease(mount: 'nomad', read_only: false, **)
51
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
52
+
53
+ { status: connection(**).post("/v1/#{mount}/config/lease", {}).status }
54
+ end
55
+
56
+ def read_lease(mount: 'nomad', **)
57
+ vault_normalize(connection(**).get("/v1/#{mount}/config/lease").body)
58
+ end
59
+
60
+ def delete_lease(mount: 'nomad', read_only: false, **)
61
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
62
+
63
+ { status: connection(**).delete("/v1/#{mount}/config/lease").status }
64
+ end
65
+
66
+ include Legion::Extensions::Helpers::Lex if Legion::Extensions.const_defined?(:Helpers, false) &&
67
+ Legion::Extensions::Helpers.const_defined?(:Lex, false)
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'legion/extensions/vault/errors'
3
4
  require 'legion/extensions/vault/helpers/client'
4
5
 
5
6
  module Legion
@@ -9,59 +10,63 @@ module Legion
9
10
  module Pki
10
11
  include Legion::Extensions::Vault::Helpers::Client
11
12
 
12
- def issue(role:, common_name:, mount: 'pki', alt_names: nil, ttl: nil, **)
13
+ def issue(role:, common_name:, mount: 'pki', alt_names: nil, ttl: nil, read_only: false, **)
14
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
15
+
13
16
  body = { common_name: common_name, alt_names: alt_names, ttl: ttl }.compact
14
- response = connection(**).post("/v1/#{mount}/issue/#{role}", body)
15
- { result: response.body }
17
+ vault_normalize(connection(**).post("/v1/#{mount}/issue/#{role}", body).body)
16
18
  end
17
19
 
18
- def sign_csr(role:, csr:, common_name:, mount: 'pki', alt_names: nil, ttl: nil, **)
20
+ def sign_csr(role:, csr:, common_name:, mount: 'pki', alt_names: nil, ttl: nil, read_only: false, **)
21
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
22
+
19
23
  body = { csr: csr, common_name: common_name, alt_names: alt_names, ttl: ttl }.compact
20
- response = connection(**).post("/v1/#{mount}/sign/#{role}", body)
21
- { result: response.body }
24
+ vault_normalize(connection(**).post("/v1/#{mount}/sign/#{role}", body).body)
22
25
  end
23
26
 
24
- def revoke(serial_number:, mount: 'pki', **)
25
- response = connection(**).post("/v1/#{mount}/revoke", { serial_number: serial_number })
26
- { result: response.body }
27
+ def revoke(serial_number:, mount: 'pki', read_only: false, **)
28
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
29
+
30
+ vault_normalize(connection(**).post("/v1/#{mount}/revoke", { serial_number: serial_number }).body)
27
31
  end
28
32
 
29
33
  def list_certs(mount: 'pki', **)
30
- response = connection(**).get("/v1/#{mount}/certs", { list: true })
31
- { result: response.body }
34
+ vault_normalize(connection(**).get("/v1/#{mount}/certs", { list: true }).body)
32
35
  end
33
36
 
34
37
  def get_cert(serial:, mount: 'pki', **)
35
- response = connection(**).get("/v1/#{mount}/cert/#{serial}")
36
- { result: response.body }
38
+ vault_normalize(connection(**).get("/v1/#{mount}/cert/#{serial}").body)
37
39
  end
38
40
 
41
+ # Plain-text PEM endpoint: the body is not JSON, so it is wrapped to
42
+ # keep the runner-returns-Hash contract.
39
43
  def ca_chain(mount: 'pki', **)
40
- response = connection(**).get("/v1/#{mount}/ca_chain")
41
- { result: response.body }
44
+ body = connection(**).get("/v1/#{mount}/ca_chain").body
45
+ body.is_a?(Hash) ? vault_normalize(body) : { data: body }
42
46
  end
43
47
 
44
48
  def list_pki_roles(mount: 'pki', **)
45
- response = connection(**).get("/v1/#{mount}/roles", { list: true })
46
- { result: response.body }
49
+ vault_normalize(connection(**).get("/v1/#{mount}/roles", { list: true }).body)
47
50
  end
48
51
 
49
52
  def get_pki_role(role_name:, mount: 'pki', **)
50
- response = connection(**).get("/v1/#{mount}/roles/#{role_name}")
51
- { result: response.body }
53
+ vault_normalize(connection(**).get("/v1/#{mount}/roles/#{role_name}").body)
52
54
  end
53
55
 
54
- def create_pki_role(role_name:, mount: 'pki', allowed_domains: nil, allow_subdomains: nil, max_ttl: nil, **)
56
+ def create_pki_role(role_name:, allowed_domains: nil, allow_subdomains: nil, max_ttl: nil,
57
+ mount: 'pki', read_only: false, **)
58
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
59
+
55
60
  body = { allowed_domains: allowed_domains, allow_subdomains: allow_subdomains,
56
61
  max_ttl: max_ttl }.compact
57
- response = connection(**).post("/v1/#{mount}/roles/#{role_name}", body)
58
- { result: response.status == 204 }
62
+ { status: connection(**).post("/v1/#{mount}/roles/#{role_name}", body).status }
59
63
  end
60
64
 
61
- def tidy(mount: 'pki', tidy_cert_store: true, tidy_revoked_certs: true, **)
65
+ def tidy(mount: 'pki', tidy_cert_store: true, tidy_revoked_certs: true, read_only: false, **)
66
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
67
+
62
68
  body = { tidy_cert_store: tidy_cert_store, tidy_revoked_certs: tidy_revoked_certs }
63
- response = connection(**).post("/v1/#{mount}/tidy", body)
64
- { result: response.body }
69
+ vault_normalize(connection(**).post("/v1/#{mount}/tidy", body).body)
65
70
  end
66
71
 
67
72
  include Legion::Extensions::Helpers::Lex if Legion::Extensions.const_defined?(:Helpers, false) &&
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'legion/extensions/vault/errors'
3
4
  require 'legion/extensions/vault/helpers/client'
4
5
 
5
6
  module Legion
@@ -9,82 +10,95 @@ module Legion
9
10
  module System
10
11
  include Legion::Extensions::Vault::Helpers::Client
11
12
 
13
+ # Informational: non-200 statuses (sealed, standby, uninit) are data,
14
+ # so this bypasses the Raiser-registered pipe.
12
15
  def health(**)
13
- response = connection(**).get('/v1/sys/health')
14
- { result: response.body }
16
+ resolved = auth_resolve(**)
17
+ body = build_connection(address: resolved[:address], token: resolved[:token],
18
+ namespace: resolved[:namespace], raise_errors: false)
19
+ .get('/v1/sys/health').body
20
+ vault_normalize(body)
15
21
  end
16
22
 
17
23
  def seal_status(**)
18
- response = connection(**).get('/v1/sys/seal-status')
19
- { result: response.body }
24
+ resolved = auth_resolve(**)
25
+ body = build_connection(address: resolved[:address], token: resolved[:token],
26
+ namespace: resolved[:namespace], raise_errors: false)
27
+ .get('/v1/sys/seal-status').body
28
+ vault_normalize(body)
20
29
  end
21
30
 
22
- def seal(**)
23
- response = connection(**).put('/v1/sys/seal')
24
- { result: response.status == 204 }
31
+ def seal(read_only: false, **)
32
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
33
+
34
+ { status: connection(**).put('/v1/sys/seal').status }
25
35
  end
26
36
 
27
- def unseal(key:, **)
28
- response = connection(**).put('/v1/sys/unseal', { key: key })
29
- { result: response.body }
37
+ def unseal(key:, read_only: false, **)
38
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
39
+
40
+ vault_normalize(connection(**).put('/v1/sys/unseal', { key: key }).body)
30
41
  end
31
42
 
32
- def init(secret_shares: 5, secret_threshold: 3, **)
43
+ def init(secret_shares: 5, secret_threshold: 3, read_only: false, **)
44
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
45
+
33
46
  body = { secret_shares: secret_shares, secret_threshold: secret_threshold }
34
- response = connection(**).put('/v1/sys/init', body)
35
- { result: response.body }
47
+ vault_normalize(connection(**).put('/v1/sys/init', body).body)
36
48
  end
37
49
 
38
50
  def list_mounts(**)
39
- response = connection(**).get('/v1/sys/mounts')
40
- { result: response.body }
51
+ vault_normalize(connection(**).get('/v1/sys/mounts').body)
41
52
  end
42
53
 
43
- def mount(path:, type:, description: nil, config: nil, **)
54
+ def mount(path:, type:, description: nil, config: nil, read_only: false, **)
55
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
56
+
44
57
  body = { type: type, description: description, config: config }.compact
45
- response = connection(**).post("/v1/sys/mounts/#{path}", body)
46
- { result: response.status == 204 }
58
+ { status: connection(**).post("/v1/sys/mounts/#{path}", body).status }
47
59
  end
48
60
 
49
- def unmount(path:, **)
50
- response = connection(**).delete("/v1/sys/mounts/#{path}")
51
- { result: response.status == 204 }
61
+ def unmount(path:, read_only: false, **)
62
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
63
+
64
+ { status: connection(**).delete("/v1/sys/mounts/#{path}").status }
52
65
  end
53
66
 
54
67
  def list_auth(**)
55
- response = connection(**).get('/v1/sys/auth')
56
- { result: response.body }
68
+ vault_normalize(connection(**).get('/v1/sys/auth').body)
57
69
  end
58
70
 
59
- def enable_auth(path:, type:, description: nil, **)
71
+ def enable_auth(path:, type:, description: nil, read_only: false, **)
72
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
73
+
60
74
  body = { type: type, description: description }.compact
61
- response = connection(**).post("/v1/sys/auth/#{path}", body)
62
- { result: response.status == 204 }
75
+ { status: connection(**).post("/v1/sys/auth/#{path}", body).status }
63
76
  end
64
77
 
65
- def disable_auth(path:, **)
66
- response = connection(**).delete("/v1/sys/auth/#{path}")
67
- { result: response.status == 204 }
78
+ def disable_auth(path:, read_only: false, **)
79
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
80
+
81
+ { status: connection(**).delete("/v1/sys/auth/#{path}").status }
68
82
  end
69
83
 
70
84
  def list_policies(**)
71
- response = connection(**).get('/v1/sys/policies/acl')
72
- { result: response.body }
85
+ vault_normalize(connection(**).get('/v1/sys/policies/acl').body)
73
86
  end
74
87
 
75
88
  def get_policy(name:, **)
76
- response = connection(**).get("/v1/sys/policies/acl/#{name}")
77
- { result: response.body }
89
+ vault_normalize(connection(**).get("/v1/sys/policies/acl/#{name}").body)
78
90
  end
79
91
 
80
- def put_policy(name:, policy:, **)
81
- response = connection(**).put("/v1/sys/policies/acl/#{name}", { policy: policy })
82
- { result: response.status == 204 }
92
+ def put_policy(name:, policy:, read_only: false, **)
93
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
94
+
95
+ { status: connection(**).put("/v1/sys/policies/acl/#{name}", { policy: policy }).status }
83
96
  end
84
97
 
85
- def delete_policy(name:, **)
86
- response = connection(**).delete("/v1/sys/policies/acl/#{name}")
87
- { result: response.status == 204 }
98
+ def delete_policy(name:, read_only: false, **)
99
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
100
+
101
+ { status: connection(**).delete("/v1/sys/policies/acl/#{name}").status }
88
102
  end
89
103
 
90
104
  include Legion::Extensions::Helpers::Lex if Legion::Extensions.const_defined?(:Helpers, false) &&
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'legion/extensions/vault/errors'
3
4
  require 'legion/extensions/vault/helpers/client'
4
5
 
5
6
  module Legion
@@ -9,70 +10,73 @@ module Legion
9
10
  module Token
10
11
  include Legion::Extensions::Vault::Helpers::Client
11
12
 
12
- def create_token(policies: nil, ttl: nil, renewable: nil, display_name: nil, role_name: nil, **)
13
+ def create_token(policies: nil, ttl: nil, renewable: nil, display_name: nil, role_name: nil, read_only: false, **)
14
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
15
+
13
16
  body = { policies: policies, ttl: ttl, renewable: renewable, display_name: display_name }.compact
14
17
  path = role_name ? "/v1/auth/token/create/#{role_name}" : '/v1/auth/token/create'
15
- response = connection(**).post(path, body)
16
- { result: response.body }
18
+ vault_normalize(connection(**).post(path, body).body)
17
19
  end
18
20
 
19
21
  def lookup_token(token_to_lookup:, **)
20
- response = connection(**).post('/v1/auth/token/lookup', { token: token_to_lookup })
21
- { result: response.body }
22
+ vault_normalize(connection(**).post('/v1/auth/token/lookup', { token: token_to_lookup }).body)
22
23
  end
23
24
 
24
25
  def lookup_self(**)
25
- response = connection(**).get('/v1/auth/token/lookup-self')
26
- { result: response.body }
26
+ vault_normalize(connection(**).get('/v1/auth/token/lookup-self').body)
27
27
  end
28
28
 
29
- def renew_token(token_to_renew:, increment: nil, **)
29
+ def renew_token(token_to_renew:, increment: nil, read_only: false, **)
30
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
31
+
30
32
  body = { token: token_to_renew, increment: increment }.compact
31
- response = connection(**).post('/v1/auth/token/renew', body)
32
- { result: response.body }
33
+ vault_normalize(connection(**).post('/v1/auth/token/renew', body).body)
33
34
  end
34
35
 
35
- def renew_self(increment: nil, **)
36
+ def renew_self(increment: nil, read_only: false, **)
37
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
38
+
36
39
  body = increment ? { increment: increment } : {}
37
- response = connection(**).post('/v1/auth/token/renew-self', body)
38
- { result: response.body }
40
+ vault_normalize(connection(**).post('/v1/auth/token/renew-self', body).body)
39
41
  end
40
42
 
41
- def revoke_token(token_to_revoke:, **)
42
- response = connection(**).post('/v1/auth/token/revoke', { token: token_to_revoke })
43
- { result: response.status == 204 }
43
+ def revoke_token(token_to_revoke:, read_only: false, **)
44
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
45
+
46
+ { status: connection(**).post('/v1/auth/token/revoke', { token: token_to_revoke }).status }
44
47
  end
45
48
 
46
- def revoke_self(**)
47
- response = connection(**).post('/v1/auth/token/revoke-self')
48
- { result: response.status == 204 }
49
+ def revoke_self(read_only: false, **)
50
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
51
+
52
+ { status: connection(**).post('/v1/auth/token/revoke-self').status }
49
53
  end
50
54
 
51
55
  def list_accessors(**)
52
- response = connection(**).get('/v1/auth/token/accessors', { list: true })
53
- { result: response.body }
56
+ vault_normalize(connection(**).get('/v1/auth/token/accessors', { list: true }).body)
54
57
  end
55
58
 
56
59
  def list_token_roles(**)
57
- response = connection(**).get('/v1/auth/token/roles', { list: true })
58
- { result: response.body }
60
+ vault_normalize(connection(**).get('/v1/auth/token/roles', { list: true }).body)
59
61
  end
60
62
 
61
63
  def get_token_role(role_name:, **)
62
- response = connection(**).get("/v1/auth/token/roles/#{role_name}")
63
- { result: response.body }
64
+ vault_normalize(connection(**).get("/v1/auth/token/roles/#{role_name}").body)
64
65
  end
65
66
 
66
- def create_token_role(role_name:, allowed_policies: nil, orphan: nil, renewable: nil, token_ttl: nil, **)
67
+ def create_token_role(role_name:, allowed_policies: nil, orphan: nil, renewable: nil, token_ttl: nil,
68
+ read_only: false, **)
69
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
70
+
67
71
  body = { allowed_policies: allowed_policies, orphan: orphan, renewable: renewable,
68
72
  token_ttl: token_ttl }.compact
69
- response = connection(**).post("/v1/auth/token/roles/#{role_name}", body)
70
- { result: response.status == 204 }
73
+ { status: connection(**).post("/v1/auth/token/roles/#{role_name}", body).status }
71
74
  end
72
75
 
73
- def delete_token_role(role_name:, **)
74
- response = connection(**).delete("/v1/auth/token/roles/#{role_name}")
75
- { result: response.status == 204 }
76
+ def delete_token_role(role_name:, read_only: false, **)
77
+ raise Legion::Extensions::Vault::Errors::ReadOnlyError, 'write operations disabled (read_only mode)' if read_only
78
+
79
+ { status: connection(**).delete("/v1/auth/token/roles/#{role_name}").status }
76
80
  end
77
81
 
78
82
  include Legion::Extensions::Helpers::Lex if Legion::Extensions.const_defined?(:Helpers, false) &&