GitLab CI Integration¶
Overview¶
Security Emphasis
GitLab CI pipelines have access to your Kubernetes clusters and containers, potentially exposing sensitive information. This task implements security best practices including pipeline-specific RBAC, ephemeral credentials, and automatic resource cleanup to minimize security risks.
This task guides you through integrating Kube CINC Secure Scanner with GitLab CI/CD pipelines. Completing this task will enable you to automate container security scanning within your GitLab pipelines while maintaining a strong security posture.
Time to complete: 30-45 minutes
Security risk: 🟡 Medium - Requires GitLab CI with Kubernetes cluster access
Security approach: Implements secure CI/CD integration with ephemeral credentials, pipeline-specific RBAC generation, and proper permission boundaries between GitLab and Kubernetes resources
Security Architecture¶
Understanding Permission Layers
Integrating GitLab CI with Kubernetes scanning requires managing multiple permission layers:
1. GitLab Runner/Pipeline Permissions * Control: Access to GitLab repository resources, ability to run pipelines, store artifacts * Risk area: Could expose repository secrets or allow unauthorized access * Mitigation: Use protected variables and dedicated runners with limited scope
2. CI/CD System Kubernetes Permissions * Control: Initial access to create and manage Kubernetes resources, including RBAC setup * Risk area: Overly permissive access could allow broader cluster access than needed * Mitigation: Store kubeconfig as protected variable with namespace-scoped permissions
3. Container Scanner RBAC Permissions * Control: What the scanner itself can access within Kubernetes during scan operations * Risk area: Scanning permissions that are too broad could allow access to unintended resources * Mitigation: Generate short-lived, minimal-scope tokens scoped only to target containers
The pipelines in this guide demonstrate proper separation of these permission layers with pipeline-specific RBAC permissions that are unique to each pipeline run and automatically cleaned up afterward.
Security Prerequisites¶
- A GitLab repository where you have permissions to set up CI/CD pipelines
- A Kubernetes cluster that meets the requirements for existing clusters
- Understanding of Kubernetes RBAC for creating secure service accounts
- GitLab runners with the ability to execute commands against your Kubernetes cluster
- Kubernetes setup with appropriate permissions
Step-by-Step Instructions¶
Step 1: Configure GitLab CI/CD Variables¶
Security Consideration
Store Kubernetes credentials as protected and masked variables to prevent exposure in logs and limit their use to protected branches only.
- In your GitLab repository, go to Settings > CI/CD > Variables
- Add the following variables:
-
KUBE_CONFIG
: Base64-encoded kubeconfig file (mark as Protected and Masked) -
SCANNER_NAMESPACE
: The namespace where scanning resources will be created CINC_PROFILE_PATH
: Path to the CINC Auditor profile (e.g.,dev-sec/linux-baseline
)THRESHOLD_VALUE
: Minimum passing score for scans (e.g.,70
)
Step 2: Create .gitlab-ci.yml File¶
Security Consideration
The pipeline creates isolated, temporary RBAC resources with unique identifiers for each pipeline run to prevent permission reuse.
- Create a
.gitlab-ci.yml
file in your repository root:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
|
Step 3: Advanced Approach: Pipeline-Specific Namespaces¶
Security Consideration
For higher security, you can isolate each scan pipeline in its own namespace to provide complete resource isolation.
Create a more secure version with isolated namespaces:
Step 4: Configure Quality Gates¶
Security Consideration
Enforcing quality gates in the pipeline prevents security issues from progressing further in your CI/CD process.
Modify the run_scan
job to enforce security thresholds:
Security Best Practices¶
- Use short-lived tokens (15 minutes or less) to minimize the access window
- Configure RBAC to limit access to only the specific resources needed for scanning
- Use pipeline-specific resource names with unique identifiers (${CI_PIPELINE_ID})
- Store sensitive information in protected and masked CI/CD variables
- Clean up all resources even when pipelines fail using the
when: always
option - Limit the permissions of the kubeconfig file stored in CI/CD variables
- Consider using pipeline-specific namespaces for complete isolation
Verification Steps¶
- Check that the pipeline runs successfully
- Verify that RBAC resources are automatically cleaned up after the pipeline completes
- Review the scan results in the GitLab pipeline artifacts or merge request comments
Troubleshooting¶
Issue | Solution |
---|---|
Pipeline fails at the deploy stage | Verify that the kubeconfig has proper permissions and the namespace exists |
RBAC creation fails | Check the permissions of the kubeconfig and ensure it can create RBAC resources |
Token generation fails | Make sure you're using Kubernetes 1.24+ for the token creation command or implement a different token generation approach for older versions |
Scan fails with access denied | Verify that the token is being correctly created and roles have the proper permissions |
SAF-CLI installation fails | Ensure your GitLab runner has Node.js properly installed |
Next Steps¶
After completing this task, consider:
- Configure threshold values for comprehensive pass/fail criteria
- Set up RBAC with label-based targeting for flexible container selection
- Integrate with GitLab security dashboards
- Use GitLab services for advanced container scanning