Ensure security policy bot for scheduled pipeline policies
What does this MR do and why?
Projects with scheduled pipeline execution policies should always have a security policy bot user. The bot is typically created when a security policy is first applied to the project, but may be missing in some cases.
This MR adds a safeguard to create the security policy bot on-demand during scheduled pipeline execution if it doesn't already exist, similar to the approach used for scheduled scan execution policies introduced in MR !132112 (merged).
Steps to to test locally
- Create a new group.
- Create a new project on the group.
- Add a simple CI config file
policy-ci.yml:pipeline execution policy job: stage: .pipeline-policy-pre script: - echo "Enforce your policy here" - Add a
.gitlab/security-policies/policy.ymlfile to the project with content (Replacepath/to/projectwith the path to your project):--- experiments: pipeline_execution_schedule_policy: enabled: true pipeline_execution_policy: [] approval_policy: [] pipeline_execution_schedule_policy: - name: test description: '' enabled: true pipeline_config_strategy: schedule content: include: - project: path/to/project file: policy-ci.yml schedules: - type: daily start_time: '10:00' time_window: value: 600 distribution: random - Go back to the group page and on the left sidebar, select Security & Compliance and Policies.
- Select Edit policy project and select your project. Then select Save.
- Copy the ID of your project
- Open a rails console
- Find your project and delete the security policy bot (Replace
YOUR_PROJECT_IDwith the id of your project):project = Project.find(YOUR_PROJECT_ID) Users::DestroyService.new(User.first).execute(project.security_policy_bot, hard_delete: false, skip_authorization: true) - On your project left sidebar, select Manage and Members and make sure there is no GitLab Security Policy Bot.
- Start the schedule worker manually to trigger the schedule (Replace
YOUR_PROJECT_IDwith the id of your project):Security::PipelineExecutionPolicies::RunScheduleWorker.new.perform(Security::PipelineExecutionProjectSchedule.find_by_project_id(YOUR_PROJECT_ID) - Refresh the members page. There should now be a GitLab Security Policy Bot
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.
Edited by Andy Schoenen