Skip to content

Commit df58ee5

Browse files
committed
Use Rails credentials instead of deprecated and removed secrets
Rails 7.1 removes access to modifying secrets as we should be moved over to rails credentials. Here we describe how to setup these rails crednetials for the purpose of recording VCR cassettes. See also: ManageIQ/manageiq-providers-autosde#253
1 parent 88f6105 commit df58ee5

File tree

3 files changed

+55
-19
lines changed

3 files changed

+55
-19
lines changed

developer_setup/seeding_test_inventory.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ ems = ManageIQ::Providers::Amazon::CloudManager.create!(
3939
We can find these values by looking at the amazon provider's vcr cassette file,
4040
`src/manageiq/manageiq-providers-amazon/spec/vcr_cassettes/manageiq/providers/amazon/cloud_manager/refresher_inventory_object.yml`
4141

42-
We can see that `'us-east-1'` is used as the region in the URIs, and in the `config/secrets.defaults.yml`
42+
We can see that `'us-east-1'` is used as the region in the URIs, and in the Rails credentials
4343
and `spec/factories/ext_management_system.rb` files we can see that `AMAZON_CLIENT_ID` and
4444
`AMAZON_CLIENT_SECRET` are used for the userid and password values for the authentication.
4545

providers/subclassing_an_existing_provider.md

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ Initialized empty Git repository in /home/grare/adam/src/manageiq/manageiq/plugi
3636
create bin/update
3737
create bundler.d
3838
create bundler.d/.keep
39-
create config/secrets.defaults.yml
4039
create config/settings.yml
4140
create lib/manageiq-providers-awesome_private_cloud.rb
4241
create lib/manageiq/providers/awesome_private_cloud/engine.rb

providers/writing_vcr_specs.md

+54-17
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,71 @@ The next thing we have to take care of is hiding "secrets". Since the VCR YAML
2222

2323
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.
2424

25-
ManageIQ has a pattern to help you with this, simply create a `config/secrets.defaults.yml` file:
25+
ManageIQ has a pattern to help you with this. We use rails credentials. Run the following command in the main application directory:
26+
27+
```
28+
EDITOR=vi be rails credentials:edit --help
29+
```
30+
31+
This will provide more information about rails credentials.
32+
33+
A sample workflow for VCR cassettes would be to edit the test environment's credentials and use them in test.
34+
35+
Run the prior command without the help option and with the 'test' environment:
36+
37+
```
38+
EDITOR=vi be rails credentials:edit --environment test
39+
```
40+
41+
You can use your preferred editor by specifying it on the command line.
42+
43+
In the editor, set your credentials, for example:
44+
2645
```yaml
2746
---
28-
test:
29-
awesome_cloud_defaults: &awesome_cloud_defaults
47+
awesome_cloud:
3048
access_key: AWESOME_CLOUD_ACCESS_KEY
3149
secret_key: AWESOME_CLOUD_SECRET_KEY
32-
awesome_cloud:
33-
<<: *awesome_cloud_defaults
3450
```
3551
36-
Then create a `config/secrets.yml` file (this file will not be committed and should be in your .gitignore):
37-
```yaml
38-
---
39-
test:
40-
awesome_cloud:
41-
access_key: "YOUR_REAL_ACCESS_KEY"
42-
secret_key: "YOUR_REAL_SECRET_KEY"
52+
After saving, this will create or update:
53+
* a plain text key file if you haven't already created one: config/credentials/test.key
54+
* an encrypted credentials file: config/credentials/test.yml.enc
55+
56+
Add memoized methods in spec/spec_helper.rb with the defaults you want to assumed when running from cassettes to avoid leaking actual credentials:
57+
58+
```ruby
59+
def credentials_awesome_cloud_host
60+
@credentials_awesome_cloud_host ||= Rails.application.credentials.awesome_cloud_host || "awesome-cloud-host"
61+
end
62+
63+
def credentials_awesome_cloud_user
64+
@credentials_awesome_cloud_user ||= Rails.application.credentials.awesome_cloud_user || "awesome-cloud-user"
65+
end
66+
67+
def credentials_awesome_cloud_password
68+
@credentials_autosde_password ||= Rails.application.credentials.awesome_cloud_password || "change_me"
69+
end
4370
```
4471

4572
Then add the following to your `VCR.configure` block in `spec/spec_helper.rb` after setting the `config.cassette_library_dir`:
73+
74+
4675
```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 }
76+
defaults = {
77+
"host_key" => credentials_awesome_cloud_host,
78+
"access_key" => credentials_awesome_cloud_user,
79+
"secret_key" => credentials_awesome_cloud_password
80+
}
81+
82+
defaults.each do |key, value|
83+
config.define_cassette_placeholder(value) do
84+
Rails.application.credentials.dig(:awesome_cloud, key)
85+
end
5086
end
5187
```
5288

89+
5390
### Writing the tests
5491

5592
Now that we have VCR configured it is time to start writing your spec tests. First we will start with the Refresher to test the refresh process of your new provider.
@@ -62,7 +99,7 @@ describe ManageIQ::Providers::AwesomeCloud::CloudManager::Refresher do
6299
let(:zone) { EvmSpecHelper.create_guid_miq_server_zone.last }
63100
let!(:ems) do
64101
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)
102+
access_key, secret_key = credentials_awesome_cloud_user, credentials_awesome_cloud_password
66103

67104
ems.update_authentication(:default => {:userid => access_key, :password => secret_key})
68105
end
@@ -125,4 +162,4 @@ rm spec/vcr_cassettes/manageiq/providers/awesome_cloud/cloud_manager/refresher.y
125162
bundle exec rspec spec/models/manageiq/awesome_cloud/cloud_manager/refresher_spec.rb
126163
```
127164

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.
165+
Make sure that you have your rails credentials 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.

0 commit comments

Comments
 (0)