Commit d7ad2b5b authored by Max Woolf's avatar Max Woolf

Create one audit event per SAML config change

parent 490388aa
...@@ -9,6 +9,10 @@ module GroupSaml ...@@ -9,6 +9,10 @@ module GroupSaml
delegate :group, to: :saml_provider delegate :group, to: :saml_provider
AUDIT_LOG_ALLOWLIST = %w[
enabled certificate_fingerprint sso_url enforced_sso enforced_group_managed_accounts prohibited_outer_forks default_membership_role git_check_enforced
].freeze
def initialize(current_user, saml_provider, params:) def initialize(current_user, saml_provider, params:)
@saml_provider = saml_provider @saml_provider = saml_provider
@current_user = current_user @current_user = current_user
...@@ -26,14 +30,18 @@ module GroupSaml ...@@ -26,14 +30,18 @@ module GroupSaml
end end
end end
if saml_provider.previous_changes.present? saml_provider.previous_changes.each do |attribute, changes|
::Gitlab::Audit::Auditor.audit( next unless AUDIT_LOG_ALLOWLIST.include?(attribute)
audit_context = {
name: audit_name, name: audit_name,
author: current_user, author: current_user,
scope: saml_provider.group, scope: saml_provider.group,
target: saml_provider.group, target: saml_provider.group,
message: message message: message(attribute, changes)
) }
::Gitlab::Audit::Auditor.audit(audit_context)
end end
end end
...@@ -55,19 +63,12 @@ module GroupSaml ...@@ -55,19 +63,12 @@ module GroupSaml
raise ActiveRecord::Rollback raise ActiveRecord::Rollback
end end
def message def message(attribute, changes)
audit_logs_allowlist = %w[enabled certificate_fingerprint sso_url enforced_sso enforced_group_managed_accounts prohibited_outer_forks default_membership_role git_check_enforced] change_text = if changes[0].nil?
change_text = saml_provider "#{attribute} changed to #{changes[1]}. "
.previous_changes else
.map do |k, v| "#{attribute} changed from #{changes[0]} to #{changes[1]}. "
next unless audit_logs_allowlist.include?(k) end
if v[0].nil?
"#{k} changed to #{v[1]}. "
else
"#{k} changed from #{v[0]} to #{v[1]}. "
end
end.join
"Group SAML SSO configuration changed: #{change_text}" "Group SAML SSO configuration changed: #{change_text}"
end end
......
...@@ -24,7 +24,7 @@ RSpec.shared_examples 'base SamlProvider service' do ...@@ -24,7 +24,7 @@ RSpec.shared_examples 'base SamlProvider service' do
author: current_user, author: current_user,
scope: group, scope: group,
target: group }) target: group })
).and_call_original ).exactly(4).times.and_call_original
expect do expect do
service.execute service.execute
...@@ -33,13 +33,7 @@ RSpec.shared_examples 'base SamlProvider service' do ...@@ -33,13 +33,7 @@ RSpec.shared_examples 'base SamlProvider service' do
.and change { group.saml_provider&.certificate_fingerprint }.to(fingerprint) .and change { group.saml_provider&.certificate_fingerprint }.to(fingerprint)
.and change { group.saml_provider&.enabled? }.to(true) .and change { group.saml_provider&.enabled? }.to(true)
.and change { group.saml_provider&.enforced_sso? }.to(true) .and change { group.saml_provider&.enforced_sso? }.to(true)
.and change { AuditEvent.count }.by(1) .and change { AuditEvent.count }.by(4)
expect(AuditEvent.last.details[:custom_message])
.to match(%r{enabled changed([\w\s]*)to true})
.and match(%r{enforced_sso changed([\w\s]*)to true})
.and match(%r{https:\/\/test})
.and match(%r{#{fingerprint}})
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment