constancy 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/constancy.rb +2 -2
- data/lib/constancy/cli.rb +2 -2
- data/lib/constancy/cli/config_command.rb +15 -4
- data/lib/constancy/config.rb +37 -24
- data/lib/constancy/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: 95dfa86b6395376d98383dccb8aaf492a34bea246f8fda4b084144f3bc95e297
|
4
|
+
data.tar.gz: 233a2fcf66cdce878d38cb25f49404c30381c6fe6d9c202e704978fa743303fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '09a3aa2bab4129d27abf9bc6f404fbeed92ef754a1a11876422cbbe8bbbc2127eec94c73d1feb4103ae082a56a5c498daccb5e604f31525f70dc0ef7796e76a8'
|
7
|
+
data.tar.gz: d884c7212d308784b756a3f2900a6658cfc7c766584b7c7f236191f89649e997e0c2d46c2f370e3f8cd6be76f5ff537b32c5ac03019be20697dbd93af1116206
|
data/lib/constancy.rb
CHANGED
@@ -16,8 +16,8 @@ class Constancy
|
|
16
16
|
@@config ||= Constancy::Config.new
|
17
17
|
end
|
18
18
|
|
19
|
-
def configure(path: nil, targets: nil)
|
20
|
-
@@config = Constancy::Config.new(path: path, targets: targets)
|
19
|
+
def configure(path: nil, targets: nil, call_external_apis: true)
|
20
|
+
@@config = Constancy::Config.new(path: path, targets: targets, call_external_apis: call_external_apis)
|
21
21
|
end
|
22
22
|
|
23
23
|
def configured?
|
data/lib/constancy/cli.rb
CHANGED
@@ -64,11 +64,11 @@ USAGE
|
|
64
64
|
exit 1
|
65
65
|
end
|
66
66
|
|
67
|
-
def configure
|
67
|
+
def configure(call_external_apis: true)
|
68
68
|
return if Constancy.configured?
|
69
69
|
|
70
70
|
begin
|
71
|
-
Constancy.configure(path: self.config_file, targets: self.targets)
|
71
|
+
Constancy.configure(path: self.config_file, targets: self.targets, call_external_apis: call_external_apis)
|
72
72
|
|
73
73
|
rescue Constancy::ConfigFileNotFound
|
74
74
|
if self.config_file.nil?
|
@@ -5,11 +5,22 @@ class Constancy
|
|
5
5
|
class ConfigCommand
|
6
6
|
class << self
|
7
7
|
def run
|
8
|
-
Constancy::CLI.configure
|
8
|
+
Constancy::CLI.configure(call_external_apis: false)
|
9
9
|
|
10
|
-
puts "Config file: #{Constancy.config.config_file}"
|
11
|
-
puts "
|
12
|
-
puts "
|
10
|
+
puts " Config file: #{Constancy.config.config_file}"
|
11
|
+
puts " Consul URL: #{Constancy.config.consul.url}"
|
12
|
+
puts " Verbose: #{Constancy.config.verbose?.to_s.bold}"
|
13
|
+
if Constancy.config.consul_token_source == "env"
|
14
|
+
puts
|
15
|
+
puts "Token Source: CONSUL_TOKEN or CONSUL_HTTP_TOKEN environment variable"
|
16
|
+
|
17
|
+
elsif Constancy.config.consul_token_source == "vault"
|
18
|
+
puts
|
19
|
+
puts "Token Source: Vault"
|
20
|
+
puts " Vault URL: #{Constancy.config.vault_config.url}"
|
21
|
+
puts " Token Path: #{Constancy.config.vault_config.consul_token_path}"
|
22
|
+
puts " Token Field: #{Constancy.config.vault_config.consul_token_field}"
|
23
|
+
end
|
13
24
|
puts
|
14
25
|
puts "Sync target defaults:"
|
15
26
|
puts " Chomp trailing newlines from local files: #{Constancy.config.chomp?.to_s.bold}"
|
data/lib/constancy/config.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# This software is public domain. No rights are reserved. See LICENSE for more information.
|
2
2
|
|
3
|
+
require 'ostruct'
|
4
|
+
|
3
5
|
class Constancy
|
4
6
|
class ConfigFileNotFound < RuntimeError; end
|
5
7
|
class ConfigFileInvalid < RuntimeError; end
|
@@ -16,7 +18,7 @@ class Constancy
|
|
16
18
|
DEFAULT_CONSUL_TOKEN_SOURCE = "none"
|
17
19
|
DEFAULT_VAULT_CONSUL_TOKEN_FIELD = "token"
|
18
20
|
|
19
|
-
attr_accessor :config_file, :base_dir, :consul, :sync_targets, :target_whitelist
|
21
|
+
attr_accessor :config_file, :base_dir, :consul, :consul_token_source, :sync_targets, :target_whitelist, :call_external_apis, :vault_config
|
20
22
|
|
21
23
|
class << self
|
22
24
|
# discover the nearest config file
|
@@ -34,7 +36,7 @@ class Constancy
|
|
34
36
|
end
|
35
37
|
end
|
36
38
|
|
37
|
-
def initialize(path: nil, targets: nil)
|
39
|
+
def initialize(path: nil, targets: nil, call_external_apis: true)
|
38
40
|
if path.nil? or File.directory?(path)
|
39
41
|
self.config_file = Constancy::Config.discover(dir: path)
|
40
42
|
elsif File.exist?(path)
|
@@ -50,6 +52,7 @@ class Constancy
|
|
50
52
|
self.config_file = File.expand_path(self.config_file)
|
51
53
|
self.base_dir = File.dirname(self.config_file)
|
52
54
|
self.target_whitelist = targets
|
55
|
+
self.call_external_apis = call_external_apis
|
53
56
|
parse!
|
54
57
|
end
|
55
58
|
|
@@ -106,7 +109,8 @@ class Constancy
|
|
106
109
|
# start with a token from the environment, regardless of the token_source setting
|
107
110
|
consul_token = ENV['CONSUL_HTTP_TOKEN'] || ENV['CONSUL_TOKEN']
|
108
111
|
|
109
|
-
|
112
|
+
self.consul_token_source = raw['consul']['token_source'] || Constancy::Config::DEFAULT_CONSUL_TOKEN_SOURCE
|
113
|
+
case self.consul_token_source
|
110
114
|
when "none"
|
111
115
|
# nothing to do
|
112
116
|
|
@@ -150,29 +154,38 @@ class Constancy
|
|
150
154
|
|
151
155
|
vault_field = raw['vault']['consul_token_field'] || Constancy::Config::DEFAULT_VAULT_CONSUL_TOKEN_FIELD
|
152
156
|
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
157
|
+
self.vault_config = OpenStruct.new(
|
158
|
+
url: vault_addr,
|
159
|
+
consul_token_path: vault_path,
|
160
|
+
consul_token_field: vault_field,
|
161
|
+
)
|
162
|
+
|
163
|
+
# don't waste time talking to Vault if this is just a config parsing run
|
164
|
+
if self.call_external_apis
|
165
|
+
ENV['VAULT_ADDR'] = vault_addr
|
166
|
+
ENV['VAULT_TOKEN'] = vault_token
|
167
|
+
|
168
|
+
begin
|
169
|
+
response = Vault.logical.read(vault_path)
|
170
|
+
consul_token = response.data[vault_field.to_sym]
|
171
|
+
|
172
|
+
if response.lease_id
|
173
|
+
at_exit {
|
174
|
+
begin
|
175
|
+
Vault.sys.revoke(response.lease_id)
|
176
|
+
rescue => e
|
177
|
+
# this is fine
|
178
|
+
end
|
179
|
+
}
|
180
|
+
end
|
181
|
+
|
182
|
+
rescue => e
|
183
|
+
raise Constancy::VaultConfigInvalid.new("Are you logged in to Vault?\n\n#{e}")
|
168
184
|
end
|
169
185
|
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
if consul_token.nil? or consul_token == ""
|
175
|
-
raise Constancy::VaultConfigInvalid.new("Could not acquire a Consul token from Vault")
|
186
|
+
if consul_token.nil? or consul_token == ""
|
187
|
+
raise Constancy::VaultConfigInvalid.new("Could not acquire a Consul token from Vault")
|
188
|
+
end
|
176
189
|
end
|
177
190
|
|
178
191
|
else
|
data/lib/constancy/version.rb
CHANGED