Use security_policy_bot users to start pipelines
What does this MR do and why?
This is the second step towards running security scan execution pipelines as bot users.
Scan execution policies can trigger pipelines on projects. The user that triggers the pipeline, is the last user that modified the security policy project. This user might not have the permission to start a pipeline on the linked project, though. To fix it, we want to create a bot user on every project that has a security policy. This bot user will then be the user to trigger the pipelines.
In the previous MR I added the functionality that creates the bot user. This MR will use the bot user, if it exists, to trigger the pipeline. It:
- Gives bot users the right access to run pipelines
- Starts scan execution pipelines as the bot user
There is one bug in this MR: Pipelines will not be triggered for protected branches because the access check for this is not based on policies. I'm going to fix this bug in one of the next MRs before we enable the feature flag.
Related to #394958 (closed)
How to set up and validate locally
- Prepare your environment
- Switch to the
andysoiron/security-scan-results-policy-bots-use-for-workers
branch - Restart GDK
- Enable the feature flag
Feature.enable(:scan_execution_bot_users)
- Switch to the
- Prepare the test project. Follow the steps below or clone https://gitlab.com/gitlab-org/govern/security-policies/andys-test-group/scan-execution-policy-test
- Create a new project with a working
.gitlab-ci.yml
file. For example, use the bash template. - Add an empty named
package.json
to the project. This is required for the SAST scan to run. - On the left sidebar select Repository and Branches
- Select New branch
- Enter
test
as name - Select Create branch
- Create a new project with a working
- Prepare the security policy
- On the left sidebar, select Security and Compliance* and Policies.
- Select New policy.
- Select Scan execution policy
- Switch to
.yaml mode
- Replace the content with the example policy yaml below
- Select Configure with a merge request
- Select Merge.
- Go back to your initial project
- There should now be a
bot
member named GitLab Security Policy Bot.
- Start a rails console and trigger the
OrchestrationPolicyRuleScheduleWorker
:
Security::OrchestrationPolicyRuleSchedule.update_all(next_run_at: Time.now - 1.day)
Security::OrchestrationPolicyRuleScheduleWorker.new.perform
- View the pipelines page of your project
- There should be a new pipeline triggered by the GitLab Security Policy Bot
Example policy yaml
type: scan_execution_policy
name: test
description: ''
enabled: true
rules:
- type: schedule
branches:
- test
cadence: 0 0 * * *
actions:
- scan: sast
tags: []
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
-
I have evaluated the MR acceptance checklist for this MR.