1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RubyからHashiCorp Vaultの機密情報を読み書きする

Posted at

はじめに

HashiCorpからVaultとRubyを連携させるためのgemが提供されていたのでサンプルアプリを作成して動作確認してみました。

参考

公式 : Vault by HashiCorp
Github : hashicorp/vault-ruby

Vault

起動

Vaultを起動して「127.0.0.1:8200」でHTTPリクエストを受信できる状態にしておく。
(unsealも忘れずに)

参考:HashiCorp Vault で HTTP API を利用する - Qiita

Ruby

gem

$ gem install vault

機密情報の読み書き

HithubのREADMEではproxyやHTTPS向けの設定が記述されてましたが、今回はHTTPなので必要な設定だけ残しました。config.addressconfig.tokenもベタ書きにしてます。

機密情報の書き込み

require "vault"

Vault.configure do |config|
  # The address of the Vault server, also read as ENV["VAULT_ADDR"]
  config.address = "http://127.0.0.1:8200"

  # The token to authenticate with Vault, also read as ENV["VAULT_TOKEN"]
  config.token = "xxxxx-xxxxx-xxxxx-xxxxx-xxxxx"
end

# Vaultへ機密情報を書き込む

secret_path = "secret/bacon"
write_secret = { delicious: true, cooktime: "11" }

Vault.with_retries(Vault::HTTPConnectionError) do
  Vault.logical.write(secret_path, write_secret)
  # => true
end

機密情報の読み込み

require "vault"

Vault.configure do |config|
  # The address of the Vault server, also read as ENV["VAULT_ADDR"]
  config.address = "http://127.0.0.1:8200"

  # The token to authenticate with Vault, also read as ENV["VAULT_TOKEN"]
  config.token = "xxxxx-xxxxx-xxxxx-xxxxx-xxxxx"
end

# Vaultから機密情報を読み込む

secret_path = "secret/bacon"

Vault.with_retries(Vault::HTTPConnectionError) do
  Vault.logical.read(secret_path)
  # => #<Vault::Secret:0x007fa1088d99a0
  #      @auth=nil, @data={:cooktime=>"11", :delicious=>true},
  #      @lease_duration=2764800, @lease_id="",
  #      @renewable=false, @warnings=nil, @wrap_info=nil>
end
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?