身份验证
第 1 章 了解运行时的身份验证
在构建镜像时,您可能需要在以下情况下定义身份验证:
- 向容器 registry 进行身份验证
- 从 Git 中拉取源代码
身份验证通过定义存储所需敏感数据的 secret 进行。
1.1. 构建 secret 注解
您可以将注解 build.shipwright.io/referenced.secret: "true"
添加到构建 secret 中。根据此注解,构建控制器会在事件时执行协调操作,如为构建 secret 创建、更新或删除触发器。以下示例显示了使用带有 secret 的注解:
apiVersion: v1 data: .dockerconfigjson: <pull_secret> 1 kind: Secret metadata: annotations: build.shipwright.io/referenced.secret: "true" 2 name: secret-docker type: kubernetes.io/dockerconfigjson
此注解会过滤构建实例中未引用的 secret。例如,如果 secret 没有此注解,则即使为 secret 触发事件,构建控制器也不会协调。协调事件时,构建控制器可以在构建配置上重新触发验证,可帮助您了解是否缺少依赖项。
1.2. Git 存储库身份验证
您可以为 Git 存储库定义以下类型的身份验证:
- 基本身份验证(Basic authentication)
- Secure Shell (SSH)身份验证
您还可以在 Build
CR 中使用两种类型的身份验证配置 Git secret。
1.2.1. 基本身份验证(Basic authentication)
使用基本身份验证,您必须配置 Git 存储库的用户名和密码。以下示例显示了对 Git 的基本身份验证的使用:
apiVersion: v1 kind: Secret metadata: name: secret-git-basic-auth annotations: build.shipwright.io/referenced.secret: "true" type: kubernetes.io/basic-auth 1 stringData: 2 username: <cleartext_username> password: <cleartext_password>
1.2.2. SSH 身份验证
使用 SSH 身份验证时,您必须配置 Tekton 注解来指定 Git 存储库供应商的主机名供使用。例如,GitHub 的 github.com
或 GitLab 的 gitlab.com
。
以下示例显示了对 Git 的 SSH 身份验证:
apiVersion: v1 kind: Secret metadata: name: secret-git-ssh-auth annotations: build.shipwright.io/referenced.secret: "true" type: kubernetes.io/ssh-auth 1 data: ssh-privatekey: | 2 # Insert ssh private key, base64 encoded
1.2.3. 使用 Git secret
在相关命名空间中创建 secret 后,您可以在 Build
自定义资源(CR)中引用它。您可以使用两种类型的身份验证配置 Git secret。
以下示例显示了使用带有 SSH 验证类型的 Git secret:
apiVersion: shipwright.io/v1beta1 kind: Build metadata: name: buildah-golang-build spec: source: git: url: git@gitlab.com:userjohn/newtaxi.git cloneSecret: secret-git-ssh-auth
以下示例显示了使用带有基本身份验证类型的 Git secret:
apiVersion: shipwright.io/v1beta1 kind: Build metadata: name: buildah-golang-build spec: source: git: url: https://gitlab.com/userjohn/newtaxi.git cloneSecret: secret-git-basic-auth
1.3. 容器 registry 身份验证
要将镜像推送到私有容器 registry,您必须在对应的命名空间中定义一个 secret,然后在 Build
自定义资源(CR)中引用它。
流程
运行以下命令以生成 secret:
$ oc --namespace <namespace> create secret docker-registry <container_registry_secret_name> \ --docker-server=<registry_host> \ 1 --docker-username=<username> \ 2 --docker-password=<password> \ 3 --docker-email=<email_address>
运行以下命令来注解 secret:
$ oc --namespace <namespace> annotate secrets <container_registry_secret_name> build.shipwright.io/referenced.secret='true'
将
spec.output.pushSecret
字段的值设置为Build
CR 中的 secret 名称:apiVersion: shipwright.io/v1beta1 kind: Build metadata: name: buildah-golang-build # ... output: image: <path_to_image> pushSecret: <container_registry_secret_name>
1.4. 基于角色的访问控制
发行版本部署 YAML 文件包含两个用于 Builds 对象的集群范围角色。以下角色默认安装:
-
shpwright-build-aggregate-view
:授予您对 Builds 资源的读取访问权限,如BuildStrategy
、ClusterBuildStrategy
、Build
、build 和BuildRun
。此角色聚合到 Kubernetesview
角色。 -
shipwright-build-aggregate-edit
:授予您对命名空间级别配置的 Builds 资源的写入权限。构建资源包括BuildStrategy
、Build
和BuildRun
。为所有ClusterBuildStrategy
资源授予读取访问权限。此角色聚合到 Kubernetesedit
和admin
角色。
只有集群管理员有权访问 ClusterBuildStrategy
资源。您可以通过创建具有这些权限的独立 Kubernetes ClusterRole
角色,并将角色绑定到适当的用户来更改此设置。
Legal Notice
Copyright © 2024 Red Hat, Inc.
OpenShift documentation is licensed under the Apache License 2.0 (https://www.apache.org/licenses/LICENSE-2.0).
Modified versions must remove all Red Hat trademarks.
Portions adapted from https://github.com/kubernetes-incubator/service-catalog/ with modifications by Red Hat.
Red Hat, Red Hat Enterprise Linux, the Red Hat logo, the Shadowman logo, JBoss, OpenShift, Fedora, the Infinity logo, and RHCE are trademarks of Red Hat, Inc., registered in the United States and other countries.
Linux® is the registered trademark of Linus Torvalds in the United States and other countries.
Java® is a registered trademark of Oracle and/or its affiliates.
XFS® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United States and/or other countries.
MySQL® is a registered trademark of MySQL AB in the United States, the European Union and other countries.
Node.js® is an official trademark of Joyent. Red Hat Software Collections is not formally related to or endorsed by the official Joyent Node.js open source or commercial project.
The OpenStack® Word Mark and OpenStack logo are either registered trademarks/service marks or trademarks/service marks of the OpenStack Foundation, in the United States and other countries and are used with the OpenStack Foundation’s permission. We are not affiliated with, endorsed or sponsored by the OpenStack Foundation, or the OpenStack community.
All other trademarks are the property of their respective owners.