vaultez-cli 0.2.0 → 0.2.2
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/lib/vaultez/cli.rb +1 -0
- data/lib/vaultez/client.rb +10 -2
- data/lib/vaultez/commands/fetch.rb +48 -24
- data/lib/vaultez/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 14c54ce2993304e11bb04d4295bdc8fbab8596f7387db6fa4639c1e32dd40e7c
|
|
4
|
+
data.tar.gz: ec8e484b39065e1a3221c60bc3d5acf431c51f8daeb77c03658cbc439f5b73b7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 13ba71b6797e10a7cb46d15b9b61e76b18817d1de62c541eadfc44a994172bf2c21df2f526e9b7b2b0f0bbb6464402caf4a0562ca51aa62d20ebcbc698913007
|
|
7
|
+
data.tar.gz: 9bc5011daa631088362b7bfb6b3153f87eec3471ef568d477b8dde69572690620b67257c819f3a3b9e98e57351637f0200e43c6e0dc3d62f2ad68ac20bc272f6
|
data/lib/vaultez/cli.rb
CHANGED
|
@@ -69,6 +69,7 @@ module Vaultez
|
|
|
69
69
|
option :projects, type: :boolean, desc: "List projects in a company"
|
|
70
70
|
option :project, type: :string, desc: "Project name"
|
|
71
71
|
option :secret, type: :string, desc: "Secret name (returns value only)"
|
|
72
|
+
option :json, type: :boolean, desc: "Output as JSON (errors go to stderr)"
|
|
72
73
|
def fetch; super; end
|
|
73
74
|
|
|
74
75
|
desc "config", "Set default company or token"
|
data/lib/vaultez/client.rb
CHANGED
|
@@ -51,7 +51,9 @@ module Vaultez
|
|
|
51
51
|
def request(method, path, body = nil, authenticated: true)
|
|
52
52
|
uri = URI("#{@api_url}#{path}")
|
|
53
53
|
http = Net::HTTP.new(uri.host, uri.port)
|
|
54
|
-
http.use_ssl
|
|
54
|
+
http.use_ssl = uri.scheme == "https"
|
|
55
|
+
http.open_timeout = 10
|
|
56
|
+
http.read_timeout = 15
|
|
55
57
|
|
|
56
58
|
req = build_request(method, uri)
|
|
57
59
|
req["Content-Type"] = "application/json"
|
|
@@ -65,7 +67,13 @@ module Vaultez
|
|
|
65
67
|
|
|
66
68
|
req.body = body.to_json if body
|
|
67
69
|
|
|
68
|
-
|
|
70
|
+
begin
|
|
71
|
+
response = http.request(req)
|
|
72
|
+
rescue StandardError => error
|
|
73
|
+
raise Vaultez::ApiError, "Could not reach the Vaultez API: #{error.message}"
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
parse_response(response)
|
|
69
77
|
end
|
|
70
78
|
|
|
71
79
|
def build_request(method, uri)
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
1
3
|
module Vaultez
|
|
2
4
|
module Commands
|
|
3
5
|
module Fetch
|
|
@@ -15,33 +17,45 @@ module Vaultez
|
|
|
15
17
|
elsif options[:project]
|
|
16
18
|
fetch_secrets(client)
|
|
17
19
|
else
|
|
18
|
-
|
|
19
|
-
exit 1
|
|
20
|
+
fail!("not enough options. See `vaultez help fetch`.")
|
|
20
21
|
end
|
|
21
22
|
rescue Vaultez::NotAuthenticatedError => error
|
|
22
|
-
|
|
23
|
-
exit 1
|
|
23
|
+
fail!(error.message)
|
|
24
24
|
rescue Vaultez::NotFoundError => error
|
|
25
|
-
|
|
26
|
-
exit 1
|
|
25
|
+
fail!(error.message)
|
|
27
26
|
rescue Vaultez::ApiError => error
|
|
28
|
-
|
|
29
|
-
exit 1
|
|
27
|
+
fail!(error.message)
|
|
30
28
|
end
|
|
31
29
|
|
|
32
30
|
private
|
|
33
31
|
|
|
32
|
+
def fail!(message)
|
|
33
|
+
if options[:json]
|
|
34
|
+
warn "Error: #{message}"
|
|
35
|
+
else
|
|
36
|
+
puts "Error: #{message}"
|
|
37
|
+
end
|
|
38
|
+
exit 1
|
|
39
|
+
end
|
|
40
|
+
|
|
34
41
|
def fetch_with_project_token(client)
|
|
35
42
|
if options[:secret]
|
|
36
43
|
secrets = fetch_all_secrets_for_token(client)
|
|
37
44
|
secret = secrets.find { |s| s["name"] == options[:secret] }
|
|
38
45
|
unless secret
|
|
39
|
-
|
|
40
|
-
|
|
46
|
+
fail!("secret \"#{options[:secret]}\" not found.")
|
|
47
|
+
end
|
|
48
|
+
if options[:json]
|
|
49
|
+
puts secret.to_json
|
|
50
|
+
else
|
|
51
|
+
print secret["value"]
|
|
41
52
|
end
|
|
42
|
-
print secret["value"]
|
|
43
53
|
else
|
|
44
54
|
secrets = fetch_all_secrets_for_token(client)
|
|
55
|
+
if options[:json]
|
|
56
|
+
puts secrets.to_json
|
|
57
|
+
return
|
|
58
|
+
end
|
|
45
59
|
if secrets.empty?
|
|
46
60
|
puts "No secrets found."
|
|
47
61
|
return
|
|
@@ -54,20 +68,22 @@ module Vaultez
|
|
|
54
68
|
companies = client.companies
|
|
55
69
|
company = companies.first
|
|
56
70
|
unless company
|
|
57
|
-
|
|
58
|
-
exit 1
|
|
71
|
+
fail!("no company found for this token.")
|
|
59
72
|
end
|
|
60
73
|
projects = client.projects(company["id"])
|
|
61
74
|
project = projects.first
|
|
62
75
|
unless project
|
|
63
|
-
|
|
64
|
-
exit 1
|
|
76
|
+
fail!("no project found for this token.")
|
|
65
77
|
end
|
|
66
78
|
client.secrets(project["id"])
|
|
67
79
|
end
|
|
68
80
|
|
|
69
81
|
def fetch_companies(client)
|
|
70
82
|
companies = client.companies
|
|
83
|
+
if options[:json]
|
|
84
|
+
puts companies.to_json
|
|
85
|
+
return
|
|
86
|
+
end
|
|
71
87
|
if companies.empty?
|
|
72
88
|
puts "No companies found."
|
|
73
89
|
return
|
|
@@ -81,6 +97,10 @@ module Vaultez
|
|
|
81
97
|
def fetch_projects(client)
|
|
82
98
|
company = resolve_company(client)
|
|
83
99
|
projects = client.projects(company["id"])
|
|
100
|
+
if options[:json]
|
|
101
|
+
puts projects.to_json
|
|
102
|
+
return
|
|
103
|
+
end
|
|
84
104
|
if projects.empty?
|
|
85
105
|
puts "No projects found in #{company["name"]}."
|
|
86
106
|
return
|
|
@@ -95,6 +115,10 @@ module Vaultez
|
|
|
95
115
|
company = resolve_company(client)
|
|
96
116
|
project = resolve_project(client, company)
|
|
97
117
|
secrets = client.secrets(project["id"])
|
|
118
|
+
if options[:json]
|
|
119
|
+
puts secrets.to_json
|
|
120
|
+
return
|
|
121
|
+
end
|
|
98
122
|
if secrets.empty?
|
|
99
123
|
puts "No secrets found in #{project["name"]}."
|
|
100
124
|
return
|
|
@@ -111,11 +135,14 @@ module Vaultez
|
|
|
111
135
|
secret = secrets.find { |s| s["name"] == options[:secret] }
|
|
112
136
|
|
|
113
137
|
unless secret
|
|
114
|
-
|
|
115
|
-
exit 1
|
|
138
|
+
fail!("secret \"#{options[:secret]}\" not found in #{project["name"]}.")
|
|
116
139
|
end
|
|
117
140
|
|
|
118
|
-
|
|
141
|
+
if options[:json]
|
|
142
|
+
puts secret.to_json
|
|
143
|
+
else
|
|
144
|
+
print secret["value"]
|
|
145
|
+
end
|
|
119
146
|
end
|
|
120
147
|
|
|
121
148
|
def resolve_company(client)
|
|
@@ -124,8 +151,7 @@ module Vaultez
|
|
|
124
151
|
if options[:company]
|
|
125
152
|
company = companies.find { |c| c["name"] == options[:company] }
|
|
126
153
|
unless company
|
|
127
|
-
|
|
128
|
-
exit 1
|
|
154
|
+
fail!("company \"#{options[:company]}\" not found.")
|
|
129
155
|
end
|
|
130
156
|
return company
|
|
131
157
|
end
|
|
@@ -138,16 +164,14 @@ module Vaultez
|
|
|
138
164
|
|
|
139
165
|
return companies.first if companies.size == 1
|
|
140
166
|
|
|
141
|
-
|
|
142
|
-
exit 1
|
|
167
|
+
fail!("multiple companies found. Specify one with --company or set a default with `vaultez config --default-company`.")
|
|
143
168
|
end
|
|
144
169
|
|
|
145
170
|
def resolve_project(client, company)
|
|
146
171
|
projects = client.projects(company["id"])
|
|
147
172
|
project = projects.find { |p| p["name"] == options[:project] }
|
|
148
173
|
unless project
|
|
149
|
-
|
|
150
|
-
exit 1
|
|
174
|
+
fail!("project \"#{options[:project]}\" not found in #{company["name"]}.")
|
|
151
175
|
end
|
|
152
176
|
project
|
|
153
177
|
end
|
data/lib/vaultez/version.rb
CHANGED