Skip to content

Commit 726abee

Browse files
authored
Merge pull request ManageIQ#566 from Fryguy/vcr_secrets
Update VCR guide for the new VcrSecrets helper
2 parents 819c16c + faee165 commit 726abee

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

providers/writing_vcr_specs.md

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,45 @@ If you generated your provider plugin with the `--vcr` flag then the VCR configu
1515
VCR.configure do |config|
1616
config.ignore_hosts 'codeclimate.com' if ENV['CI']
1717
config.cassette_library_dir = ManageIQ::Providers::AwesomeCloud::Engine.root.join('spec/vcr_cassettes')
18+
19+
VcrSecrets.define_all_cassette_placeholders(config, :awesome_cloud)
1820
end
1921
```
2022

2123
The next thing we have to take care of is hiding "secrets". Since the VCR YAML files will be committed to source control it is critical that private information like passwords do not make it into these files.
2224

2325
VCR handles this with the `config.define_cassette_placeholder` option. You provide VCR with a string that you want to be replaced, and then what you want it to be replaced with. This allows for hostnames / passwords / etc... to be used when recording the cassette but the values will not be written to the resulting YAML files.
2426

25-
ManageIQ has a pattern to help you with this, simply create a `config/secrets.defaults.yml` file:
27+
ManageIQ has a pattern to help you with this. By default, the generator created a file named `spec/config/secrets.defaults.yml` with a username and password.
28+
```yaml
29+
---
30+
awesome_cloud:
31+
username: AWESOME_CLOUD_USERNAME
32+
password: AWESOME_CLOUD_PASSWORD
33+
```
34+
35+
If your provider uses a different set of secrets, such as access_key and secret_key, you can change the file accordingly as follows:
2636
```yaml
2737
---
28-
test:
29-
awesome_cloud_defaults: &awesome_cloud_defaults
30-
access_key: AWESOME_CLOUD_ACCESS_KEY
31-
secret_key: AWESOME_CLOUD_SECRET_KEY
32-
awesome_cloud:
33-
<<: *awesome_cloud_defaults
38+
awesome_cloud:
39+
access_key: AWESOME_CLOUD_ACCESS_KEY
40+
secret_key: AWESOME_CLOUD_SECRET_KEY
3441
```
3542
36-
Then create a `config/secrets.yml` file (this file will not be committed and should be in your .gitignore):
43+
Finally, create a `spec/config/secrets.yml` file with your real provider secrets. **NOTE**: This file must not be committed and should be in your .gitignore.
3744
```yaml
3845
---
39-
test:
40-
awesome_cloud:
41-
access_key: "YOUR_REAL_ACCESS_KEY"
42-
secret_key: "YOUR_REAL_SECRET_KEY"
46+
awesome_cloud:
47+
access_key: YOUR_REAL_ACCESS_KEY
48+
secret_key: YOUR_REAL_SECRET_KEY
4349
```
4450

45-
Then add the following to your `VCR.configure` block in `spec/spec_helper.rb` after setting the `config.cassette_library_dir`:
51+
And that's all! The `VcrSecrets.define_all_cassette_placeholders` line in `spec/spec_helper.rb` automatically marks everything under the awesome_cloud key as sensitive data.
52+
53+
If you need to manually mark something as sensitive data, then you will need to call `config.define_cassette_placeholder`. To do so, you can add the following to your `VCR.configure` block in `spec/spec_helper.rb` after setting the `config.cassette_library_dir`. For example, if your provider Base64 encodes the access_key and secret_key into a header, you will want to include something like the following:
4654
```ruby
47-
secrets = Rails.application.secrets
48-
secrets.awesome_cloud.each do |key, val|
49-
config.define_cassette_placeholder(secrets.awesome_cloud_defaults[key]) { val }
55+
config.define_cassette_placeholder("AWESOME_CLOUD_AUTHORIZATION") do
56+
Base64.encode("#{VcrSecrets.awesome_cloud.access_key}:#{VcrSecrets.awesome_cloud.secret_key}").chomp
5057
end
5158
```
5259

@@ -62,7 +69,8 @@ describe ManageIQ::Providers::AwesomeCloud::CloudManager::Refresher do
6269
let(:zone) { EvmSpecHelper.create_guid_miq_server_zone.last }
6370
let!(:ems) do
6471
FactoryBot.create(:ems_awesome_cloud, :zone => zone).tap do |ems|
65-
access_key, secret_key = Rails.application.secrets.awesome_cloud.values_at(:access_key, :secret_key)
72+
access_key = VcrSecrets.awesome_cloud.access_key
73+
secret_key = VcrSecrets.awesome_cloud.secret_key
6674
6775
ems.update_authentication(:default => {:userid => access_key, :password => secret_key})
6876
end
@@ -117,12 +125,12 @@ Now fill out the refresher_spec.rb file with more checks to ensure that inventor
117125

118126
### Updating the VCR cassettes
119127

120-
Now that you have your specs recorded, what happens if you want to collect something new? Like you now want to start fetching floating IPs or Cloud Volumes?
128+
Now that you have your specs recorded, what happens if you want to collect something new? For example, if you now want to start fetching floating IPs or Cloud Volumes?
121129

122130
It is simple to re-record your VCR cassette, simply remove the file then rerun the specs against the same environment:
123131
```bash
124132
rm spec/vcr_cassettes/manageiq/providers/awesome_cloud/cloud_manager/refresher.yml
125133
bundle exec rspec spec/models/manageiq/awesome_cloud/cloud_manager/refresher_spec.rb
126134
```
127135

128-
Make sure that you have your `config/secrets.yml` file still present, you might have to update the expected counts as things in your environment have likely changed but you now should have an updated VCR cassette.
136+
Make sure that you have your `config/secrets.yml` file still present. Note that you might have to update the expected counts, as things in your environment have likely changed, but you now should have an updated VCR cassette.

0 commit comments

Comments
 (0)