diff --git a/doc/administration/audit_events.md b/doc/administration/audit_events.md index 8fbf06343a59f8713f4447837f5fef36f6dcc99f..03f0699f7768daaee0f48b5fc416f69860ab1803 100644 --- a/doc/administration/audit_events.md +++ b/doc/administration/audit_events.md @@ -86,6 +86,7 @@ From there, you can see the following actions: - 2FA enforcement or grace period changed. - Roles allowed to create project changed. - Group CI/CD variable added, removed, or protected status changed. [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/30857) in GitLab 13.3. +- Compliance framework created, updated, or deleted. [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/340649) in GitLab 14.6. Group events can also be accessed via the [Group Audit Events API](../api/audit_events.md#group-audit-events) diff --git a/ee/app/services/compliance_management/frameworks/create_service.rb b/ee/app/services/compliance_management/frameworks/create_service.rb index 24632b4aa74c4e9790745229cb633dea0b9ba2f4..fc5ce6a17ecffa0a79607c9af8fe41609a5c6d54 100644 --- a/ee/app/services/compliance_management/frameworks/create_service.rb +++ b/ee/app/services/compliance_management/frameworks/create_service.rb @@ -36,9 +36,22 @@ def permitted? end def success + audit_create ServiceResponse.success(payload: { framework: framework }) end + def audit_create + audit_context = { + name: 'create_compliance_framework', + author: current_user, + scope: framework.namespace, + target: framework, + message: "Created compliance framework #{framework.name}" + } + + ::Gitlab::Audit::Auditor.audit(audit_context) + end + def error ServiceResponse.error(message: _('Failed to create framework'), payload: framework.errors ) end diff --git a/ee/app/services/compliance_management/frameworks/destroy_service.rb b/ee/app/services/compliance_management/frameworks/destroy_service.rb index 713d92b9701dce478398eff13ebbf3e39a705696..18e00416c680896764c8124cb7c21cfc42060a34 100644 --- a/ee/app/services/compliance_management/frameworks/destroy_service.rb +++ b/ee/app/services/compliance_management/frameworks/destroy_service.rb @@ -23,12 +23,25 @@ def permitted? end def success + audit_destroy ServiceResponse.success(message: _('Framework successfully deleted')) end def error ServiceResponse.error(message: _('Failed to create framework'), payload: framework.errors ) end + + def audit_destroy + audit_context = { + name: 'destroy_compliance_framework', + author: current_user, + scope: framework.namespace, + target: framework, + message: "Destroyed compliance framework #{framework.name}" + } + + ::Gitlab::Audit::Auditor.audit(audit_context) + end end end end diff --git a/ee/app/services/compliance_management/frameworks/update_service.rb b/ee/app/services/compliance_management/frameworks/update_service.rb index 737fa607e9daf55ed70d7db013428006d97a77c6..92df82adf64143353cc4800eecb64e48cab79d67 100644 --- a/ee/app/services/compliance_management/frameworks/update_service.rb +++ b/ee/app/services/compliance_management/frameworks/update_service.rb @@ -25,6 +25,7 @@ def execute end def success + audit_changes ServiceResponse.success(payload: { framework: framework }) end @@ -34,6 +35,20 @@ def error private + def audit_changes + framework.previous_changes.each do |attribute, changes| + audit_context = { + name: 'update_compliance_framework', + author: current_user, + scope: framework.namespace, + target: framework, + message: "Changed compliance framework's #{attribute} from #{changes[0]} to #{changes[1]}" + } + + ::Gitlab::Audit::Auditor.audit(audit_context) + end + end + def permitted? can? current_user, :manage_compliance_framework, framework end diff --git a/ee/spec/services/compliance_management/frameworks/create_service_spec.rb b/ee/spec/services/compliance_management/frameworks/create_service_spec.rb index 5021ac96aed5d57abd7fd52b08b3d36b0f0f387b..9b74a3a6cb2a1b53384f0398d3d1be0392751b40 100644 --- a/ee/spec/services/compliance_management/frameworks/create_service_spec.rb +++ b/ee/spec/services/compliance_management/frameworks/create_service_spec.rb @@ -97,6 +97,10 @@ context 'when using parameters for a valid compliance framework' do subject { described_class.new(namespace: namespace, params: params, current_user: namespace.owner) } + it 'audits the changes' do + expect { subject.execute }.to change { AuditEvent.count }.by(1) + end + it 'creates a new compliance framework' do expect { subject.execute }.to change { ComplianceManagement::Framework.count }.by(1) end diff --git a/ee/spec/services/compliance_management/frameworks/destroy_service_spec.rb b/ee/spec/services/compliance_management/frameworks/destroy_service_spec.rb index 68307f3468c3f099b7bcdcf021d9f935ec21d9d0..fffc9d5e91820f29726cf4642d3ea96000568baa 100644 --- a/ee/spec/services/compliance_management/frameworks/destroy_service_spec.rb +++ b/ee/spec/services/compliance_management/frameworks/destroy_service_spec.rb @@ -37,6 +37,10 @@ it 'is successful' do expect(subject.execute.success?).to be true end + + it 'audits the destruction' do + expect { subject.execute }.to change { AuditEvent.count }.by(1) + end end context 'when current user is not the namespace owner' do diff --git a/ee/spec/services/compliance_management/frameworks/update_service_spec.rb b/ee/spec/services/compliance_management/frameworks/update_service_spec.rb index 48788799f7ddc9c1519d82076cf173bba35b5f4a..f7c0177e49811605bc9233edf239f84a4c8d1810 100644 --- a/ee/spec/services/compliance_management/frameworks/update_service_spec.rb +++ b/ee/spec/services/compliance_management/frameworks/update_service_spec.rb @@ -66,6 +66,18 @@ it 'is successful' do expect(subject.execute.success?).to be true end + + it 'audits the changes' do + expect { subject.execute }.to change { AuditEvent.count }.by(3) + + messages = AuditEvent.last(3).map { |e| e.details[:custom_message] } + + expect(messages).to contain_exactly( + 'Changed compliance framework\'s name from GDPR to New Name', + 'Changed compliance framework\'s color from #004494 to #000001', + 'Changed compliance framework\'s description from The General Data Protection Regulation (GDPR) is a regulation in EU law on data protection and privacy in the European Union (EU) and the European Economic Area (EEA). to New Description' + ) + end end end end