diff --git a/app/services/service_provider_seeder.rb b/app/services/service_provider_seeder.rb index 6a25766ec94..289530dc0cc 100644 --- a/app/services/service_provider_seeder.rb +++ b/app/services/service_provider_seeder.rb @@ -1,11 +1,15 @@ # Update ServiceProvider from config/service_providers.yml (all environments in rake db:seed) class ServiceProviderSeeder + class ExtraServiceProviderError < StandardError; end + def initialize(rails_env: Rails.env, deploy_env: LoginGov::Hostdata.env) @rails_env = rails_env @deploy_env = deploy_env end def run + check_for_missing_sps + service_providers.each do |issuer, config| next unless write_service_provider?(config) ServiceProvider.find_or_create_by!(issuer: issuer) do |sp| @@ -50,4 +54,19 @@ def write_service_provider?(config) !is_production_or_has_a_restriction || (restrict_env == deploy_env) end + + def check_for_missing_sps + return unless %w[prod staging].include? deploy_env + + sps_in_db = ServiceProvider.pluck(:issuer) + sps_in_yaml = service_providers.keys + extra_sps = sps_in_db - sps_in_yaml + + return if extra_sps.empty? + + extra_sp_error = ExtraServiceProviderError.new( + "Extra service providers found in DB: #{extra_sps.join(', ')}", + ) + NewRelic::Agent.notice_error(extra_sp_error) + end end diff --git a/spec/services/service_provider_seeder_spec.rb b/spec/services/service_provider_seeder_spec.rb index b1e5aa85b27..d623a9b379e 100644 --- a/spec/services/service_provider_seeder_spec.rb +++ b/spec/services/service_provider_seeder_spec.rb @@ -79,9 +79,17 @@ ServiceProvider.find_by(issuer: 'urn:gov:login:test-providers:fake-unrestricted-sp'), ).to eq(nil) end + + it 'sends New Relic an error if the DB has an SP not in the config' do + allow(NewRelic::Agent).to receive(:notice_error) + create(:service_provider, issuer: 'missing_issuer') + run + + expect(NewRelic::Agent).to have_received(:notice_error) + end end - context 'in another environment' do + context 'in the staging environment' do let(:deploy_env) { 'staging' } it 'only writes configs with restrict_to_deploy_env for that env, or no restrictions' do @@ -102,6 +110,26 @@ ), ).to be_present end + + it 'sends New Relic an error if the DB has an SP not in the config' do + allow(NewRelic::Agent).to receive(:notice_error) + create(:service_provider, issuer: 'missing_issuer') + run + + expect(NewRelic::Agent).to have_received(:notice_error) + end + end + + context 'in another environment' do + let(:deploy_env) { 'int' } + + it 'does not send New Relic an error if the DB has an SP not in the config' do + allow(NewRelic::Agent).to receive(:notice_error) + create(:service_provider, issuer: 'missing_issuer') + run + + expect(NewRelic::Agent).not_to have_received(:notice_error) + end end context 'when a service provider is invalid' do