Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Use rails credentials instead of secrets #928

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions config/secrets.defaults.yml

This file was deleted.

5 changes: 0 additions & 5 deletions lib/manageiq/providers/vmware/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ class Engine < ::Rails::Engine

config.autoload_paths << root.join('lib').to_s

initializer :append_secrets do |app|
app.config.paths["config/secrets"] << root.join("config", "secrets.defaults.yml").to_s
app.config.paths["config/secrets"] << root.join("config", "secrets.yml").to_s
end

def self.vmdb_plugin?
true
end
Expand Down
12 changes: 6 additions & 6 deletions spec/factories/ext_management_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
zone { EvmSpecHelper.local_miq_server.zone }

after(:build) do |ems|
ems.hostname = Rails.application.secrets.vmware_cloud[:host]
ems.hostname = test_credentials(:vmware_cloud, :host)
end

after(:create) do |ems|
userid = Rails.application.secrets.vmware_cloud[:userid]
password = Rails.application.secrets.vmware_cloud[:password]
userid = test_credentials(:vmware_cloud, :userid)
password = test_credentials(:vmware_cloud, :password)

cred = {
:userid => userid,
Expand All @@ -30,11 +30,11 @@

factory :ems_vmware_tanzu_with_vcr_authentication, :parent => :ems_vmware_tanzu do
after(:create) do |ems|
userid = Rails.application.secrets.vmware_tanzu[:userid]
password = Rails.application.secrets.vmware_tanzu[:password]
userid = test_credentials(:vmware_tanzu, :userid)
password = test_credentials(:vmware_tanzu, :password)

ems.default_endpoint.update!(
:hostname => Rails.application.secrets.vmware_tanzu[:hostname],
:hostname => test_credentials(:vmware_tanzu, :hostname),
:verify_ssl => OpenSSL::SSL::VERIFY_NONE
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe ManageIQ::Providers::Vmware::CloudManager::Refresher do
before do
@host = Rails.application.secrets.vmware_cloud[:host]
@host = test_credentials(:vmware_cloud, :host)
host_uri = URI.parse("https://#{@host}")

@hostname = host_uri.host
Expand All @@ -15,8 +15,8 @@
:api_version => '5.5'
)

@userid = Rails.application.secrets.vmware_cloud[:userid]
@password = Rails.application.secrets.vmware_cloud[:password]
@userid = test_credentials(:vmware_cloud, :userid)
@password = test_credentials(:vmware_cloud, :password)

cred = {
:userid => @userid,
Expand Down
6 changes: 3 additions & 3 deletions spec/models/manageiq/providers/vmware/cloud_manager_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
describe ManageIQ::Providers::Vmware::CloudManager do
before(:context) do
@host = Rails.application.secrets.vmware_cloud[:host]
@host = test_credentials(:vmware_cloud, :host)
host_uri = URI.parse("https://#{@host}")

@hostname = host_uri.host
@port = host_uri.port == 443 ? nil : host_uri.port

@userid = Rails.application.secrets.vmware_cloud[:userid]
@password = Rails.application.secrets.vmware_cloud[:password]
@userid = test_credentials(:vmware_cloud, :userid)
@password = test_credentials(:vmware_cloud, :password)
end

before(:example) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

let!(:ems) do
zone = EvmSpecHelper.local_miq_server.zone
hostname = Rails.application.secrets.vmware_infra[:hostname]
hostname = test_credentials(:vmware_infra, :hostname)
FactoryBot.create(:ems_vmware_with_authentication, :hostname => hostname, :zone => zone).tap do |ems|
# NOTE: VCR filter_sensitive_data was replacing rootFolder with VMWARE_USERNAME and
# vmware_soap_string_abcdef with VMWARE_PASSWORD_string_abcdef, given these are the
Expand Down
37 changes: 27 additions & 10 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,41 @@
end
end

TEST_CREDENTIALS_DEFAULTS = {
:vmware_cloud_defaults => {:host => "vmwarecloudhost", :userid => "VMWARE_CLOUD_USERID", :password => "VMWARE_CLOUD_PASSWORD"},
:vmware_infra_defaults => {:hostname => "HOSTNAME"},
:vmware_tanzu_defaults => {:hostname => "vmware-tanzu-hostname", :userid => "VMWARE_TANZU_USERID", :password => "VMWARE_TANZU_PASSWORD"}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm open to make the constant have the DEFAULTS name an simplify the keys to no longer have defaults in the name to avoid redundancy. Let me know your thoughts.

}.freeze
Copy link
Member Author

@jrafanie jrafanie Nov 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@agrare I'm thinking this constant ☝️ would live in each plugin...

The two methods below could live in core spec/shared:
👇


def test_credentials(*args)
Rails.application.credentials.dig(*args) || test_credentials_defaults(*args)
end

def test_credentials_defaults(*args)
args[0] = "#{args[0]}_defaults".to_sym
TEST_CREDENTIALS_DEFAULTS.dig(*args)
end

VCR.configure do |config|
# config.default_cassette_options = { :record => :all }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

placeholder to verify cassette recording uses the correct values from local rails credentials and falls back to the defaults.


config.ignore_hosts 'codeclimate.com' if ENV['CI']
config.cassette_library_dir = File.join(ManageIQ::Providers::Vmware::Engine.root, 'spec/vcr_cassettes')

secrets = Rails.application.secrets
config.define_cassette_placeholder(Rails.application.secrets.vmware_infra_defaults[:hostname]) do
Rails.application.secrets.vmware_infra[:hostname]
config.define_cassette_placeholder(test_credentials_defaults(:vmware_infra_defaults, :hostname)) do
test_credentials(:vmware_infra, :hostname)
end
config.define_cassette_placeholder(Rails.application.secrets.vmware_cloud_defaults[:host]) do
Rails.application.secrets.vmware_cloud[:host]
config.define_cassette_placeholder(test_credentials_defaults(:vmware_cloud, :host)) do
test_credentials(:vmware_cloud, :host)
end
config.define_cassette_placeholder("VMWARE_CLOUD_AUTHORIZATION") do
Base64.encode64("#{Rails.application.secrets.vmware_cloud[:userid]}:#{Rails.application.secrets.vmware_cloud[:password]}").chomp
Base64.encode64("#{test_credentials(:vmware_cloud, :userid)}:#{test_credentials(:vmware_cloud, :password)}").chomp
end
config.define_cassette_placeholder("VMWARE_CLOUD_INVALIDAUTHORIZATION") do
Base64.encode64("#{Rails.application.secrets.vmware_cloud[:userid]}:invalid").chomp
end
secrets.vmware_tanzu.each do |key, val|
config.define_cassette_placeholder(secrets.vmware_tanzu_defaults[key]) { val }
Base64.encode64("#{test_credentials(:vmware_cloud, :userid)}:invalid").chomp
end

config.define_cassette_placeholder(test_credentials_defaults(:vmware_tanzu, :hostname)) { test_credentials(:vmware_tanzu, :hostname) }
config.define_cassette_placeholder(test_credentials_defaults(:vmware_tanzu, :userid)) { test_credentials(:vmware_tanzu, :userid) }
config.define_cassette_placeholder(test_credentials_defaults(:vmware_tanzu, :password)) { test_credentials(:vmware_tanzu, :password) }
end