身份验证


builds for Red Hat OpenShift 1.3

了解运行时的身份验证

Red Hat OpenShift Documentation Team

摘要

本文档提供有关运行时身份验证的信息。

第 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
1
base64 编码的 pull secret。
2
build.shipwright.io/referenced.secret 注解的值被设为 true

此注解会过滤构建实例中未引用的 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
Kubernetes secret 的类型。
2
以明文形式存储用户名和密码的字段。

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
Kubernetes secret 的类型。
2
用于向 Git 进行身份验证的 SSH 密钥的 Base64 编码。您可以使用 base64 ~/.ssh/id_rsa.pub 命令生成此密钥,其中 ~/.ssh/id_rsa.pub 表示通常用来向 Git 进行身份验证的密钥的默认位置。

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)中引用它。

流程

  1. 运行以下命令以生成 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>
    1
    & lt;registry_host& gt; 值表示此格式 https://<registry_server>/<registry_host > 的 URL。
    2
    & lt;username > 值是用户 ID。
    3
    & lt;password > 值可以是您的容器 registry 密码或访问令牌。
  2. 运行以下命令来注解 secret:

    $ oc --namespace <namespace> annotate secrets <container_registry_secret_name> build.shipwright.io/referenced.secret='true'
  3. 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 资源的读取访问权限,如 BuildStrategyClusterBuildStrategyBuild、build 和 BuildRun。此角色聚合到 Kubernetes view 角色。
  • shipwright-build-aggregate-edit :授予您对命名空间级别配置的 Builds 资源的写入权限。构建资源包括 BuildStrategyBuildBuildRun。为所有 ClusterBuildStrategy 资源授予读取访问权限。此角色聚合到 Kubernetes editadmin 角色。

只有集群管理员有权访问 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.

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.