Skip to content

Add scope for couple of audit event type YML files

What does this MR do and why?

  1. Use the below script to add scope to the existing YMLs from the logs generated via https://log.gprd.gitlab.net/app/r/s/MAFyV
add_scope_to_event_type_ymls.rb
require 'csv'
require 'yaml'

AUDIT_EVENT_TYPE_CSV = ARGV[0] || '/Users/huzaifa/Downloads/event_type_and_scopes.csv'
GITLAB_PROJECT_DIRECTORY = ARGV[1] || '/Users/huzaifa/Work/gitlab-development-kit/gitlab'

=begin

###################################################

Sample AUDIT_EVENT_TYPE_CSV looks like below:

"Top 1000 values of json.event_type.keyword","Top 3 values of json.scope.keyword","Unique count of json.event_type.keyword"
"add_gpg_key",User,1
"allow_author_approval_updated",Group,1
"allow_committer_approval_updated",Group,1
"allow_merge_on_skipped_pipeline_updated",Project,1
"allow_overrides_to_approver_list_per_merge_request_updated",Group,1
"amazon_s3_configuration_created",Group,1
"application_setting_updated","Gitlab::Audit::InstanceScope",1
"environment_protected",Group,1
"environment_protected",Project,1
"environment_unprotected",Group,1
"environment_unprotected",Project,1

###################################################

=end

table = CSV.read(AUDIT_EVENT_TYPE_CSV, liberal_parsing: true)

# drop the first row as it is the headers of the CSV file
table.drop(1).each do |row|
  if File.file?("#{GITLAB_PROJECT_DIRECTORY}/ee/config/audit_events/types/#{row[0]}.yml")
    filename = "#{GITLAB_PROJECT_DIRECTORY}/ee/config/audit_events/types/#{row[0]}.yml"
  else
    filename = "#{GITLAB_PROJECT_DIRECTORY}/config/audit_events/types/#{row[0]}.yml"
  end

  event_type_yml = YAML.load_file(filename)

  row[1] = 'Instance' if row[1] == "Gitlab::Audit::InstanceScope"
	
  # skip updating event type YML files that already have same scopes defined
  next if event_type_yml["scope"] != nil && event_type_yml["scope"].include?(row[1])

  # Add a blank line at the end if the YML file
  File.write(filename, "\n", mode: 'a+') if File.read(filename).chars[-1] != "\n"

  if event_type_yml["scope"] != nil
    scope = event_type_yml["scope"].append(row[1])
    scope.delete("-")
    text = File.read(filename)
    updated_text = text.gsub(/scope.*/, "scope: #{scope}")
    File.open(filename, "w") { |file| file << updated_text }
  else
    File.write(filename, "scope: [#{row[1]}]\n", mode: 'a+')
  end
end

puts "All existing audit event type YML files updated. See your GitLab project for changes and create a merge request."

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Screenshots or screen recordings

Screenshots are required for UI changes, and strongly recommended for all other merge requests.

Before After

How to set up and validate locally

Numbered steps to set up and validate the change are strongly suggested.

Related to #438620 (closed)

Merge request reports