Skip to content

Commit

Permalink
Add a shared vcr spec helper for access to secrets
Browse files Browse the repository at this point in the history
We need a way to sanitize actual secrets with fake and consistent values so tests
are reliable.  This mechanism must allow us to provide overide values with real
secrets when we're actually recording new or updated cassettes.

For now, we're reusing the existing mechanism of specifying the secrets:

* relative to the current directory - we run specs for plugins from the plugin's directory
* config/secrets.yml - actual secrets
* config/secrets.defaults.yml - defaults for sanitizing real values with consistent repeatable replacements

Plugins can use this mechanism by adding:

Object.include Spec::Shared::CassetteSecretsHelper to their spec_helper.rb.

You can then access these values via helper methods with interfaces like below:

default_vcr_secret_by_key_path(:vmware_infra, :hostname)
vcr_secret_by_key_path(:vmware_tanzu, :hostname)

Each plugin should define their own:
config/secrets.default.yml, which can look something like this:

```
---
:vmware_cloud:
  :host: vmwarecloudhost
  :userid: VMWARE_CLOUD_USERID
  :password: VMWARE_CLOUD_PASSWORD
:vmware_infra:
  :hostname: HOSTNAME
:vmware_tanzu:
  :hostname: vmware-tanzu-hostname
  :userid: VMWARE_TANZU_USERID
  :password: VMWARE_TANZU_PASSWORD

```

Developers can then copy this file to config/secrets.yml and provide actual values for each of the keys.

When recording, the cassettes will use the real values for connecting to environments, but store the resulting placeholder using the fake value
for that key from the defaults.
  • Loading branch information
jrafanie committed Dec 9, 2024
1 parent a2a7b2d commit a284128
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions spec/shared/cassette_secrets_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Spec
module Shared
module CassetteSecretsHelper
DEFAULT_VCR_SECRETS_PATH = Pathname.new(Dir.pwd).join("config/secrets.defaults.yml")
VCR_SECRETS_PATH = Pathname.new(Dir.pwd).join("config/secrets.yml")

def load_vcr_secrets(pathname)
if pathname.exist?
YAML.load_file(pathname)
else
{}
end
end

def default_vcr_secrets
@default_vcr_secrets ||= load_vcr_secrets(DEFAULT_VCR_SECRETS_PATH)
end

def vcr_secrets
@vcr_secrets ||= load_vcr_secrets(VCR_SECRETS_PATH)
end

def default_vcr_secret_by_key_path(*args)
default_vcr_secrets.dig(*args)
end

def vcr_secret_by_key_path(*args)
vcr_secrets.dig(*args) || default_vcr_secret_by_key_path(*args)
end
end
end
end

0 comments on commit a284128

Please sign in to comment.