《OpenShift / RHEL / DevSecOps 汇总目录》
文本已在 OpenShift 4.11 + RHACS 3.71.0 环境中进行验证。
请在开始以下操作前先完成 OpenShift + RHACS 环境安装。
$ git clone https://github.com/liuxiaoyu-git/openshift-demo && cd openshift-demo/bin
$ ./setup_cicd_projects.sh
NEXUS_PVC_SIZE="300Gi"
JENKINS_PVC_SIZE="10Gi"
SONAR_PVC_SIZE="10Gi"
$ ./setup_nexus.sh
$ echo https://$(oc get route nexus -n ci-cd -o jsonpath='{.spec.host}')
$ NEXUS_REGISTRY=$(oc get route nexus-registry -n ci-cd -o jsonpath='{.spec.host}') && echo $NEXUS_REGISTRY
$ yum install skopeo -y
$ NEXUS_PASSWORD=<PASSWORD>
$ allImages="backend:v1 backend:11-ubuntu backend:CVE-2020-36518 frontend-js:v1 frontend-js:node log4shell:latest backend-native:v1 backend-native:v1-distroless"
$ for image in $allImages
do
echo "############## Copy $image ##############"
skopeo copy --src-tls-verify=true --dest-tls-verify=false --src-no-creds --dest-username admin --dest-password $NEXUS_PASSWORD \
docker://quay.io/voravitl/$image docker://$NEXUS_REGISTRY/$image
done
$ ./setup_jenkins.sh
$ ./setup_sonar.sh
在安装完 Jenkins、Nexus、Sonar 后确认在 ci-cd 项目中应该部署好了以下资源:
$ ROX_CENTRAL_ADDRESS=$(oc get route central -n stackrox -o jsonpath='{.spec.host}'):443
$ roxctl --insecure-skip-tls-verify -e ${ROX_CENTRAL_ADDRESS} image check --image=${NEXUS_REGISTRY}/backend:v1 --output=table --token-file=acs-token
Policy check results for image: nexus-registry-ci-cd.apps.cluster-k8qw5.k8qw5.sandbox638.opentlc.com/backend:v1
(TOTAL: 1, LOW: 1, MEDIUM: 0, HIGH: 0, CRITICAL: 0)
+--------------------------------+----------+--------------+--------------------------------+--------------------------------+--------------------------------+
| POLICY | SEVERITY | BREAKS BUILD | DESCRIPTION | VIOLATION | REMEDIATION |
+--------------------------------+----------+--------------+--------------------------------+--------------------------------+--------------------------------+
| Red Hat Package Manager in | LOW | - | Alert on deployments with | - Image includes component | Run `rpm -e --nodeps $(rpm -qa |
| Image | | | components of the Red | 'microdnf' (version | '*rpm*' '*dnf*' '*libsolv*' |
| | | | Hat/Fedora/CentOS package | 3.8.0-2.el8.x86_64) | '*hawkey*' 'yum*')` in the |
| | | | management system. | | image build for production |
| | | | | - Image includes | containers. |
| | | | | component 'rpm' (version | |
| | | | | 4.14.3-23.el8.x86_64) | |
+--------------------------------+----------+--------------+--------------------------------+--------------------------------+--------------------------------+
WARN: A total of 1 policies have been violated
$ roxctl --insecure-skip-tls-verify -e ${ROX_CENTRAL_ADDRESS} image scan --image=${NEXUS_REGISTRY}/backend:v1 --output=table --token-file=acs-token
Scan results for image: nexus-registry-ci-cd.apps.cluster-k8qw5.k8qw5.sandbox638.opentlc.com/backend:v1
(TOTAL-COMPONENTS: 41, TOTAL-VULNERABILITIES: 86, LOW: 37, MODERATE: 0, IMPORTANT: 0, CRITICAL: 0)
+------------------------+---------------------------+------------------+-----------+---------------------------------------------------------+
| COMPONENT | VERSION | CVE | SEVERITY | LINK |
+------------------------+---------------------------+------------------+-----------+---------------------------------------------------------+
| arc | 2.10.2.final | CVE-2005-2945 | LOW | https://nvd.nist.gov/vuln/detail/CVE-2005-2945 |
+ + +------------------+-----------+---------------------------------------------------------+
| | | CVE-2005-2992 | LOW | https://nvd.nist.gov/vuln/detail/CVE-2005-2992 |
+------------------------+---------------------------+------------------+-----------+---------------------------------------------------------+
...
...
+------------------------+---------------------------+------------------+-----------+---------------------------------------------------------+
| systemd-libs | 239-58.el8.x86_64 | CVE-2018-20839 | MODERATE | https://access.redhat.com/security/cve/CVE-2018-20839 |
+ + +------------------+-----------+---------------------------------------------------------+
| | | CVE-2021-3997 | MODERATE | https://access.redhat.com/security/cve/CVE-2021-3997 |
+------------------------+---------------------------+------------------+-----------+---------------------------------------------------------+
WARN: A total of 86 vulnerabilities were found in 41 components
$ roxctl --insecure-skip-tls-verify -e ${ROX_CENTRAL_ADDRESS} deployment check --file=../manifests/backend-bad-example.yaml --token-file=acs-token
Policy check results for deployments: [backend-v2]
(TOTAL: 2, LOW: 0, MEDIUM: 2, HIGH: 0, CRITICAL: 0)
+--------------------------------+----------+---------------+------------+--------------------------------+--------------------------------+--------------------------------+
| POLICY | SEVERITY | BREAKS DEPLOY | DEPLOYMENT | DESCRIPTION | VIOLATION | REMEDIATION |
+--------------------------------+----------+---------------+------------+--------------------------------+--------------------------------+--------------------------------+
| No resource requests or limits | MEDIUM | - | backend-v2 | Alert on deployments that have | - CPU limit set to 0 cores for | Specify the requests and |
| specified | | | | containers without resource | container 'backend' | limits of CPU and Memory for |
| | | | | requests and limits | | your deployment. |
| | | | | | - CPU request set to 0 cores | |
| | | | | | for container 'backend' | |
| | | | | | | |
| | | | | | - Memory limit set to 0 MB for | |
| | | | | | container 'backend' | |
| | | | | | | |
| | | | | | - Memory request set to 0 MB | |
| | | | | | for container 'backend' | |
+--------------------------------+----------+---------------+------------+--------------------------------+--------------------------------+--------------------------------+
| Pod Service Account Token | MEDIUM | - | backend-v2 | Protect pod default service | - Deployment mounts the | Add |
| Automatically Mounted | | | | account tokens from compromise | service account tokens. | `automountServiceAccountToken: |
| | | | | by minimizing the mounting | | false` or a value distinct |
| | | | | of the default service | - Namespace has name 'default' | from 'default' for the |
| | | | | account token to only those | | `serviceAccountName` key |
| | | | | pods whose application | - Service Account is set to | to the deployment's Pod |
| | | | | requires interaction with the | 'default' | configuration. |
| | | | | Kubernetes API. | | |
+--------------------------------+----------+---------------+------------+--------------------------------+--------------------------------+--------------------------------+
WARN: A total of 2 policies have been violated
$ oc create secret generic stackrox-token -n ci-cd --from-file=token=acs-token
$ cat ../manifests/backend-build-stackrox-pipeline.yaml | sed 's/value: NEXUS_REGISTRY/value: '$NEXUS_REGISTRY'/' | oc create -n ci-cd -f -
$ oc get buildconfig -n ci-cd
NAME TYPE FROM LATEST
backend-build-stackrox-pipeline JenkinsPipeline Git@cve 1
$ oc start-build backend-build-stackrox-pipeline
在 Jenkins 中可以看到提示,即发现构建的镜像中包含的 Violation 违规项目。
如果在 Jenkins 中安装了 Blue Ocean 插件,可以看到以下执行结果。
确认此时在 Nexus 中的 docker repository 中也有生成的 tag 为 1.0.0-1 容器镜像了。
在 OpenShift 控制台管理员视图中修改名为 backend-build-stackrox-pipeline 配置构建,将下图中的 ref: cve 修改为 ref: master。
source:
contextDir: Jenkinsfile/build-stackrox
git:
ref: master
uri: https://gitlab.com/ocp-demo/backend_quarkus.git
$ oc start-build backend-build-stackrox-pipeline
如果在 Jenkins 中安装了 Blue Ocean 插件,可以看到以下执行结果。
8. 确认在 dev 项目中已经有部署成功的 backend 应用了。
以下问题是由于 ACS 的 scanner 组件无法访问造成的,请确认该组件对应的 Pod 运行正常。
ERROR: checking image failed after 3 retries: could not check build-time alerts: rpc error: code = Internal desc = image enrichment error: error scanning image: nexus-registry-ci-cd.apps.cluster-k8qw5.k8qw5.sandbox638.opentlc.com/backend:v1 error: scanning "nexus-registry-ci-cd.apps.cluster-k8qw5.k8qw5.sandbox638.opentlc.com/backend:v1" with scanner "Stackrox Scanner": Get "https://scanner.stackrox.svc:8080/scanner/sha/sha256:1fb8cb4ceb8153b374d85017c83f167fd99ce7039b6e2162d64de1c16e1d06de": dial tcp 172.30.5.237:8080: i/o timeout
https://github.com/rhthsa/openshift-demo