Skip to content

Commit

Permalink
add refactored vault server
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Huckins <dhuckins@users.noreply.github.com>
  • Loading branch information
dhuckins committed Jun 27, 2023
1 parent 5d22355 commit f0676b3
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions spec/support/vault_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -39,26 +43,47 @@ 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

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

0 comments on commit f0676b3

Please sign in to comment.