前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >jenkins凭证管理和规范化实践,看这一篇就够了

jenkins凭证管理和规范化实践,看这一篇就够了

作者头像
DevOps在路上
发布2024-04-22 10:58:16
1240
发布2024-04-22 10:58:16
举报
文章被收录于专栏:DevOps实践之路DevOps实践之路

许多三方网站和应用可以与Jenkins交互,如Artifact仓库,基于云的存储系统和服务等. 在Jenkins中添加/配置credentials,Pipeline项目就可以使用 credentials 与三方应用交互

  • Credential 类型
  • Credential 安全
  • Credential 创建
    • Credential ID 定义
  • Credential 使用
    • Credential 相关插件
  • 最佳实践

unsetunsetCredential 类型unsetunset

参考: https://jenkins.io/zh/doc/book/using/using-credentials/

Jenkins可以存储以下类型的credentials:

  • Secret text - API token之类的token (如GitHub个人访问token)
  • Username and password - 可以为独立的字段,也可以为冒号分隔的字符串:username:password(更多信息请参照 处理 credentials)
  • Secret file - 保存在文件中的加密内容
  • SSH Username with private key - SSH 公钥/私钥对
  • Certificate - a PKCS#12 证书文件 和可选密码
  • Docker Host Certificate Authentication credentials.

unsetunsetCredential 安全unsetunset

为了最大限度地提高安全性,在Jenins中配置的 credentials 以加密形式存储在Jenkins 主节点上(用Jenkins ID加密),并且 只能通过 credentials ID 在Pipeline项目中获取

这最大限度地减少了向Jenkins用户公开credentials真实内容的可能性,并且阻止了将credentials复制到另一台Jenkins实例

unsetunsetCredential 创建unsetunset

  • 选择适合的凭证类型
  • 创建 “Username and password” 凭证
  • 创建 “SSH Username with private key” 凭证

Credential ID 定义

  • 在 ID 字段中,必须指定一个有意义的Credential ID- 例如 jenkins-user-for-xyz-artifact-repository。注意: 该字段是可选的。 如果您没有指定值, Jenkins 则Jenkins会分配一个全局唯一ID(GUID)值。
  • 请记住: 一旦设置了credential ID,就不能再进行更改。

unsetunsetCredential 使用unsetunset

参考: https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#handling-credentials

存储在Jenkins中的credentials可以被使用:

  1. 适用于Jenkins的任何地方 (即全局 credentials),
  2. 通过特定的Pipeline项目/项目 (在 处理 credentials 和 使用Jenkinsfile部分了解更多信息),
  3. 由特定的Jenkins用户 (如 Pipeline 项目中创建 Blue Ocean的情况).
    • Blue Ocean 自动生成一个 SSH 公共/私有密钥对, 确保 SSH 公共/私有秘钥对在继续之前已经被注册到你的Git服务器

实际使用中,下面几个场景会用到creential

  • gitlab 访问、API调用
  • jenkins slave 创建

Credential 相关插件

注意: 上述 Credential 类型都依赖于 jenkins插件,同样jenkins pipeline 也需要这些插件的安装以支持代码片段

  • Credentials Binding: https://plugins.jenkins.io/credentials-binding/
    • For secret text, usernames and passwords, and secret files
代码语言:javascript
复制
environment {
MAGE_REPO_CREDENTIALS = credentials('COMPOSER_REPO_MAGENTO')
COMPOSER_AUTH = """{
  "http-basic": {
      "repo.magento.com": {
          "username": "${env.MAGE_REPO_CREDENTIALS_USR}",
          "password": "${env.MAGE_REPO_CREDENTIALS_PSW}"
      }
  } }"""
}
  • For other credential types
代码语言:javascript
复制
withCredentials([usernamePassword(credentialsId: 'amazon', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
// available as an env variable, but will be masked if you try to print it out any which way
// note: single quotes prevent Groovy interpolation; expansion is by Bourne Shell, which is what you want
sh 'echo $PASSWORD'
// also available as a Groovy variable
echo USERNAME
// or inside double quotes for string interpolation
echo "username is $USERNAME"
}
  • Jenkins Plain Credentials Plugin: https://plugins.jenkins.io/plain-credentials/
  • SSH Credentials: https://plugins.jenkins.io/ssh-credentials/

unsetunset最佳实践unsetunset

  • 为了便于管理和使用, 强烈建议使用统一的约定来指定credential ID
  • 建议使用类似下面的format做为credential ID, 便于jenkinsfile开发时直接使用,同时在”描述“里写清楚credential的作用gitlab-api-token、gitlab-private-key、gitlab-userpwd-pair、harbor-xxx-xxx

实践:

  • 如下所示,将凭证使用统一的ID命名之后,便于复用,凭证定义一次,可多次,多个地方统一使用,无论是后期维护,复用都非常方便!
代码语言:javascript
复制
    environment {
        // HARBOR="harbor.devopsing.site"
        HARBOR_ACCESS_KEY = credentials('harbor-userpwd-pair')
        SERVER_ACCESS_KEY = credentials('deploy-userpwd-pair')
            }
    .....
    docker login --username=${HARBOR_ACCESS_KEY_USR} --password=${HARBOR_ACCESS_KEY_PSW} ${HARBOR}
    sshpass -p "${SERVER_ACCESS_KEY_PSW}" ssh -o StrictHostKeyChecking=no ${SERVER_ACCESS_KEY_USR}@${DEPLOY_SERVER} "$runCmd"
本文参与?腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2024-04-17,如有侵权请联系?cloudcommunity@tencent.com 删除

本文分享自 DevOps在路上 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • unsetunsetCredential 类型unsetunset
  • unsetunsetCredential 安全unsetunset
  • unsetunsetCredential 创建unsetunset
    • Credential ID 定义
    • unsetunsetCredential 使用unsetunset
      • Credential 相关插件
      • unsetunset最佳实践unsetunset
      相关产品与服务
      容器服务
      腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
      http://www.vxiaotou.com