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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d3e4e575fc5f80a0782fe1b893049209d976179f6d372f32511211d29f04c0fa
4
- data.tar.gz: d516a0d7c2311358205415a811eea26212fba7e7ebc1f6c0e3f2f0ff9df8b193
3
+ metadata.gz: 95dfa86b6395376d98383dccb8aaf492a34bea246f8fda4b084144f3bc95e297
4
+ data.tar.gz: 233a2fcf66cdce878d38cb25f49404c30381c6fe6d9c202e704978fa743303fb
5
5
  SHA512:
6
- metadata.gz: ba9ee9f9c89fcfe995ce3383d273fa08788284e1d064b4a00f994fdf6f8bfdbdefcbb1589f0af7be278e243d9c1e1457a8b03089b139040b9e3a1725da5cf28a
7
- data.tar.gz: 32ba1fd6a825ba89989ca6128ad1a4fbbaa18f7a5603524de59f2c424bc1aac4d09ed6684a06af306bef8bb493727e4b6d7880de29e66d0f611b511124cde64d
6
+ metadata.gz: '09a3aa2bab4129d27abf9bc6f404fbeed92ef754a1a11876422cbbe8bbbc2127eec94c73d1feb4103ae082a56a5c498daccb5e604f31525f70dc0ef7796e76a8'
7
+ data.tar.gz: d884c7212d308784b756a3f2900a6658cfc7c766584b7c7f236191f89649e997e0c2d46c2f370e3f8cd6be76f5ff537b32c5ac03019be20697dbd93af1116206
@@ -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?
@@ -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 " Consul URL: #{Constancy.config.consul.url}"
12
- puts " Verbose: #{Constancy.config.verbose?.to_s.bold}"
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}"
@@ -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
- case raw['consul']['token_source'] || Constancy::Config::DEFAULT_CONSUL_TOKEN_SOURCE
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
- ENV['VAULT_ADDR'] = vault_addr
154
- ENV['VAULT_TOKEN'] = vault_token
155
-
156
- begin
157
- response = Vault.logical.read(vault_path)
158
- consul_token = response.data[vault_field.to_sym]
159
-
160
- if response.lease_id
161
- at_exit {
162
- begin
163
- Vault.sys.revoke(response.lease_id)
164
- rescue => e
165
- # this is fine
166
- end
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
- rescue => e
171
- raise Constancy::VaultConfigInvalid.new("Are you logged in to Vault?\n\n#{e}")
172
- end
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
@@ -1,5 +1,5 @@
1
1
  # This software is public domain. No rights are reserved. See LICENSE for more information.
2
2
 
3
3
  class Constancy
4
- VERSION = "0.2.1"
4
+ VERSION = "0.2.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: constancy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Adams