Allow for (re)configuration after module load via ruby code
Hey Danai!
I have a use case where I'd like to dynamically reconfigure saml_camel based on my own application code, rather than using configuration files that are loaded in advance. In this case my app knows what its own saml configuration should be with its secrets (i.e. the ssl key) stored in a vault instance. This is particularly relevant if you have multiple instances of an app with different saml configurations.
IDEALLY I'd like an interface to specify and/or modify what is currently loaded from filesystem files as SamlCamel::SP_SETTINGS, SamlCamel::Transaction::IDP_CERT, SamlCamel::Transaction::SP_CERT, and SamlCamel::Transaction::SP_KEY.
Here's an example of what I'm doing (in a rails initializer) to dynamically reconfigure saml_camel in my rails 6 app based on its deployment environment:
dir = "#{Rails.root}/config/saml/#{Rails.env}"
begin
# Deserialize the saml settings and inject a few values we care about
saml_settings = JSON.parse(File.read("#{dir}/settings.json"))
rescue Errno::ENOENT
warn 'No default settings json, you should check the base settings into git'
saml_settings = { 'settings' => {} }
end
if ENV['SAML_ACS_BASE'].blank? || ENV['SAML_ENTITY_ID'].blank?
warn 'No SAML_ACS_BASE or SAML_ENTITY_ID set, the web UI will likely not work'
return
end
saml_settings['settings']['acs'] = ENV['SAML_ACS_BASE']
saml_settings['settings']['raw_response_acs'] = ENV['SAML_ACS_BASE'] + '/rawResponse'
saml_settings['settings']['entity_id'] = ENV['SAML_ENTITY_ID']
saml_settings['settings']['clock_drift'] = '360'
File.write("#{dir}/settings.json", saml_settings.to_json)
# We also rely on vault for our saml config
saml_dir = "#{Rails.root}/config/saml/#{Rails.env}"
begin
VaultTool.write_saml_files_to(saml_dir)
rescue StandardError
warn "No SAML found in vault, please save it to #{VaultTool.shared_secret_path('saml')}"
end
# Reconfigure saml camel now to fix a race condition with the conf files
#
return unless Object.constants.include? :SamlCamel
Object.send(:remove_const, :SamlCamel)
Object.send(:load, 'saml_camel.rb')
Object.send(:load, 'saml_camel/engine.rb')