diff --git a/lib/nomad/client.rb b/lib/nomad/client.rb index ee940a8..22b6423 100644 --- a/lib/nomad/client.rb +++ b/lib/nomad/client.rb @@ -136,6 +136,9 @@ def request(verb, path, data = {}, headers = {}) # Get a list of headers headers = DEFAULT_HEADERS.merge(headers) + # Dynamically read Nomad ACL token + headers = headers.merge({"X-Nomad-Token" => nomad_token}) + # Add headers headers.each do |key, value| req.add_field(key, value) diff --git a/lib/nomad/configurable.rb b/lib/nomad/configurable.rb index ad06e45..09dc927 100644 --- a/lib/nomad/configurable.rb +++ b/lib/nomad/configurable.rb @@ -5,6 +5,7 @@ module Configurable def self.keys @keys ||= [ :address, + :nomad_token, :hostname, :open_timeout, :proxy_address, diff --git a/lib/nomad/defaults.rb b/lib/nomad/defaults.rb index e5ebfcd..0b1cdd0 100644 --- a/lib/nomad/defaults.rb +++ b/lib/nomad/defaults.rb @@ -42,6 +42,10 @@ def address ENV["NOMAD_ADDR"] || NOMAD_ADDRESS end + def nomad_token + ENV["NOMAD_TOKEN"] + end + # The SNI host to use when connecting to Nomad via TLS. # @return [String, nil] def hostname diff --git a/lib/nomad/response.rb b/lib/nomad/response.rb index c81b719..91d241b 100644 --- a/lib/nomad/response.rb +++ b/lib/nomad/response.rb @@ -31,7 +31,7 @@ class Response # Parses the value as a string, converting "" to nil (go compat). string_as_nil: ->(item) { - if item.nil? || item.strip.empty? + if item.nil? || item.to_s.strip.empty? nil else item diff --git a/spec/unit/defaults_spec.rb b/spec/unit/defaults_spec.rb index 8b025a0..17113ff 100644 --- a/spec/unit/defaults_spec.rb +++ b/spec/unit/defaults_spec.rb @@ -22,6 +22,15 @@ module Nomad end end + describe ".nomad_token" do + it "uses ENV['NOMAD_TOKEN'] if present" do + with_stubbed_env("NOMAD_TOKEN" => "test") do + expect(Defaults.nomad_token).to eq("test") + end + end + end + + describe ".hostname" do it "defaults to ENV['NOMAD_TLS_SERVER_NAME']" do with_stubbed_env("NOMAD_TLS_SERVER_NAME" => "www.foo.com") do