From 536bfe755b87146296dcf9782c572375728c646d Mon Sep 17 00:00:00 2001 From: Daniel Huckins Date: Tue, 27 Jun 2023 12:45:12 -0400 Subject: [PATCH] add refactored vault server (#135) Signed-off-by: Daniel Huckins --- spec/support/vault_server.rb | 45 ++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/spec/support/vault_server.rb b/spec/support/vault_server.rb index 1856c91..fe8f2f4 100644 --- a/spec/support/vault_server.rb +++ b/spec/support/vault_server.rb @@ -18,6 +18,7 @@ def self.method_missing(m, *args, &block) end attr_reader :token + attr_reader :unseal_token def initialize # If there is already a vault-token, we need to move it so we do not @@ -30,7 +31,10 @@ def initialize end io = Tempfile.new("vault-server") - pid = Process.spawn({}, "vault server -dev", out: io.to_i, err: io.to_i) + pid = Process.spawn( + "vault server -dev -dev-root-token-id=root", + out: io.to_i, err: io.to_i + ) at_exit do Process.kill("INT", pid) @@ -39,9 +43,23 @@ def initialize io.close io.unlink end + wait_for_ready + puts "vault server is ready" + # sleep to get unseal token + sleep 5 - wait_for_ready do - @token = File.read(TOKEN_PATH) + @token = "root" + + output = "" + while io.rewind + output = io.read + break unless output.empty? + end + + if output.match(/Unseal Key.*: (.+)/) + @unseal_token = $1.strip + else + raise "Vault did not return an unseal token!" end end @@ -49,16 +67,23 @@ def address "http://127.0.0.1:8200" end - def wait_for_ready(&block) - Timeout.timeout(5) do - while !File.exist?(TOKEN_PATH) - sleep(0.25) + def wait_for_ready + uri = URI(address + "/v1/sys/health") + Timeout.timeout(15) do + loop do + begin + response = Net::HTTP.get_response(uri) + if response.code != 200 + return true + end + rescue Errno::ECONNREFUSED + puts "waiting for vault to start" + end + sleep 2 end end - - yield rescue Timeout::Error - raise "Vault did not start in 5 seconds!" + raise TimeoutError, "Timed out waiting for vault health check" end end end