From a3e7f62178473ef371a88465548f7d34a5dbb2fa Mon Sep 17 00:00:00 2001 From: tcosgrave Date: Fri, 23 Mar 2018 16:55:34 +0100 Subject: [PATCH 1/4] Add .vscode to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 7ed4cee5..720977b0 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ *.gem *.rbc /.config +/.vscode /coverage/ /InstalledFiles /pkg/ From ccffc6284fa09bd3f1df8709a61518f7e2e71a99 Mon Sep 17 00:00:00 2001 From: tcosgrave Date: Fri, 23 Mar 2018 17:02:41 +0100 Subject: [PATCH 2/4] Add shudown funciton to client This change exposes the persistent pool shutdown fuction to the vault client to help manage sockets kept open by long running proccesses. --- lib/vault/client.rb | 6 ++++ spec/integration/client_spec.rb | 60 +++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/lib/vault/client.rb b/lib/vault/client.rb index c66ad400..32c60413 100644 --- a/lib/vault/client.rb +++ b/lib/vault/client.rb @@ -158,6 +158,12 @@ def pool private :pool + # Shutdown any open pool connections. Pool will be recreated upon next request. + def shutdown + @nhp.shutdown() + @nhp = nil + end + # Creates and yields a new client object with the given token. This may be # used safely in a threadsafe manner because the original client remains # unchanged. The value of the block is returned. diff --git a/spec/integration/client_spec.rb b/spec/integration/client_spec.rb index 9452e1a6..86c6dc7d 100644 --- a/spec/integration/client_spec.rb +++ b/spec/integration/client_spec.rb @@ -52,5 +52,65 @@ def free_address }.to raise_error(MissingTokenError) end end + + describe "#shutdown" do + it "clears the pool after calling shutdown and sets nhp to nil" do + TCPServer.open('localhost', 0) do |server| + Thread.new do + loop do + client = server.accept + sleep 0.25 + client.close + end + end + + address = "http://%s:%s" % ["localhost", server.addr[1]] + + client = described_class.new(address: address, token: "foo") + + expect { client.request(:get, "/", {}, {}) }.to raise_error(HTTPConnectionError) + + pool = client.instance_variable_get(:@nhp).pool + + client.shutdown() + + expect(pool.available.instance_variable_get(:@enqueued)).to eq(0) + expect(pool.available.instance_variable_get(:@shutdown_block)).not_to be_nil + expect(client.instance_variable_get(:@nhp)).to be_nil + + server.close + end + end + + it "the pool is recreated on the following request" do + TCPServer.open('localhost', 0) do |server| + Thread.new do + loop do + client = server.accept + sleep 0.25 + client.close + end + end + + address = "http://%s:%s" % ["localhost", server.addr[1]] + + client = described_class.new(address: address, token: "foo") + + expect { client.request(:get, "/", {}, {}) }.to raise_error(HTTPConnectionError) + + client.shutdown() + + expect { client.request(:get, "/", {}, {}) }.to raise_error(HTTPConnectionError) + + pool = client.instance_variable_get(:@nhp).pool + + expect(pool.available.instance_variable_get(:@enqueued)).to eq(1) + expect(pool.available.instance_variable_get(:@shutdown_block)).to be_nil + expect(client.instance_variable_get(:@nhp)).not_to be_nil + + server.close + end + end + end end end From 7de30f9ca917cc9c26b79e02226ab0668e6fe41f Mon Sep 17 00:00:00 2001 From: tcosgrave Date: Tue, 21 May 2019 15:21:07 +0200 Subject: [PATCH 3/4] Adding name parameter to tls auth --- lib/vault/api/auth.rb | 6 ++++-- spec/integration/api/auth_spec.rb | 10 ++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/vault/api/auth.rb b/lib/vault/api/auth.rb index 156c1945..af6e929b 100644 --- a/lib/vault/api/auth.rb +++ b/lib/vault/api/auth.rb @@ -287,11 +287,13 @@ def gcp(role, jwt, path = 'gcp') # The path to the auth backend to use for the login procedure. # # @return [Secret] - def tls(pem = nil, path = 'cert') + def tls(pem = nil, path = 'cert', name = nil) new_client = client.dup new_client.ssl_pem_contents = pem if !pem.nil? + + payload = name.nil? ? {} : { name: name } - json = new_client.post("/v1/auth/#{CGI.escape(path)}/login") + json = new_client.post("/v1/auth/#{CGI.escape(path)}/login", JSON.fast_generate(payload)) secret = Secret.decode(json) client.token = secret.auth.client_token return secret diff --git a/spec/integration/api/auth_spec.rb b/spec/integration/api/auth_spec.rb index 6a380789..b63832e9 100644 --- a/spec/integration/api/auth_spec.rb +++ b/spec/integration/api/auth_spec.rb @@ -202,6 +202,16 @@ module Vault expect(subject.token).to eq(result.auth.client_token) end + it "authenticates with named ssl_pem_file" do + pending "dev server does not support tls" + + subject.auth_tls.set_certificate("kaelumania", certificate) + subject.ssl_pem_file = auth_cert + + result = subject.auth.tls(name: "kaelumania") + expect(subject.token).to eq(result.auth.client_token) + end + it "raises an error if the authentication is bad", vault: "> 0.6.1" do subject.sys.disable_auth("cert") From 81748a6186d91cf3528164689dc9ab4453ab18c8 Mon Sep 17 00:00:00 2001 From: tcosgrave Date: Fri, 31 May 2019 12:25:34 +0200 Subject: [PATCH 4/4] allowing for generic request with support for any verb, going rouge --- lib/vault/client.rb | 15 +++++++++++++-- lib/vault/version.rb | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/vault/client.rb b/lib/vault/client.rb index ddc9aa28..f68a7019 100644 --- a/lib/vault/client.rb +++ b/lib/vault/client.rb @@ -237,7 +237,14 @@ def delete(path, params = {}, headers = {}) def request(verb, path, data = {}, headers = {}) # Build the URI and request object from the given information uri = build_uri(verb, path, data) - request = class_for_request(verb).new(uri.request_uri) + request_class = class_for_request(verb) + + if request_class.nil? # Support Verbs not in Net::HTTP + request = Net::HTTPGenericRequest.new(verb.to_s.upcase,false, true, uri.request_uri) + else + request = request_class.new(uri.request_uri) + end + if uri.userinfo() request.basic_auth uri.user, uri.password end @@ -335,7 +342,11 @@ def build_uri(verb, path, params = {}) # # @return [Class] def class_for_request(verb) - Net::HTTP.const_get(verb.to_s.capitalize) + begin + Net::HTTP.const_get(verb.to_s.capitalize) + rescue NameError # The contsant doesn't exist + nil + end end # Convert the given hash to a list of query string parameters. Each key and diff --git a/lib/vault/version.rb b/lib/vault/version.rb index 3357c48a..9a246152 100644 --- a/lib/vault/version.rb +++ b/lib/vault/version.rb @@ -1,3 +1,3 @@ module Vault - VERSION = "0.12.0" + VERSION = "0.12.2" end