Jenkins实践指南-08-Jenkins 凭证管理

4. Jenkins 凭证管理

    [作者:Surpassme]随着网络环境的变化,如果在Jenkins中使用明文密码会造成一些安全隐患。为此Jenkins也提供凭证管理功能,本章节来系统学习一下。

4.1 凭证定义

    [作者:Surpassme]凭证(Credentials)是Jenkins在进行一些受限操作时的钥匙。例如SSH登录时的密码、Gitlab相关账户信息等,这些重要信息是不能以明文形式配置在Jenkins中,因此需要对这些凭证进行统一管理和使用。

    为了最大限度的提高安全性,在Jenkins master节点上对凭证进行加密存储,然后通过凭证ID在pipeline中使用。

4.2 创建凭证

    [作者:Surpassme]因为Jenkins本身也有一定的权限控制,因此在创建凭证时,请确保使用的账户具有创建凭证的权限。其创建凭证的操作步骤如下所示:

  • 1.Manage Jenkins -> Manage Credentials ,如下所示:

  • 2.Credentials -> System -> Global credentials(unrestricted) -> Add credentials,如下所示

    1. 进入创建凭证后的界面如下所示:

    其主要参数如下所示:

  • 类型:创建凭证的类型

  • 范围:凭证的作用域,主要有两种
    Global: 全局作用域。若要在pipeline中使用,则使用该类型作用域
    System: 如果凭证用于Jenkins本身系统管理,则使用该类型作用域

  • ID:使用该凭证的唯一标识,如果创建时,不填写则由Jenkins自动生成。

  • 描述: 对该凭证的声明式描述,建议填写,方便区分和查找凭证

原文地址:作者:Surpassme:https://www.cnblogs.com/surpassme/p/16995400.html

4.3 常用凭证

    [作者:Surpassme]在pipeline中使用凭证时,需要检查插件Credentials Binding Plugin(https://plugins.jenkins.io/credentials-binding/)是否安装。

4.3.1 Username with password

    Username with password 是比较常用的凭证,是指用户和密码凭证,添加方法如下所示:

    在pipeline中的使用示例如下所示:

pipeline{
    agent any
    options{
        timestamps()
    }
    stages{
        stage("credential demo"){
            steps{
                withCredentials([
                    usernamePassword(
                        credentialsId:"surpass-123",
                        usernameVariable:"username",
                        passwordVariable:"passwd"
                        )
                    ]){
                        echo "username is ${username} \n password is ${passwd}"
                    }
            }
        }
    }
}

4.3.2 Secret file

    [作者:Surpassme]Secret file 是指需要保密的文件,添加方法如下所示:

    在pipeline中的使用示例如下所示:

pipeline{
    agent any
    options{
        timestamps()
    }
    stages{
        stage("credential demo"){
            steps{
                withCredentials([
                    file(credentialsId:"secret-file-123",variable:"secretContent")]){
                        echo "secret contetn is: ${secretContent}"
                    }
            }
        }
    }
}

4.3.3 Secret text

    [作者:Surpassme]Secret text 是一串需要加密的文件,例如各种token信息等,添加方法如下所示:

    在pipeline中的使用示例如下所示:

pipeline{
    agent any
    options{
        timestamps()
    }
    stages{
        stage("credential demo"){
            steps{
                withCredentials([
                    string(credentialsId:"secret-text-123",variable:"secretText")]){
                        echo "secret text is: ${secretText}"
                    }
            }
        }
    }
}

4.3.4 SSH Username with private key

    [作者:Surpassme]SSH Username with private key是指一对SSH用户名和密钥,添加方法如下所示:

    在pipeline中的使用示例如下所示:

pipeline{
    agent any
    options{
        timestamps()
    }
    stages{
        stage("credential demo"){
            steps{
                withCredentials([
                    sshUserPrivateKey(
                        credentialsId:"ssh-username-key-123",
                        usernameVariable:"username",
                        keyFileVariable:"keyFile"
                        )
                    ]){
                          echo "username is ${username}\nkey is: ${keyFile}"
                    }
            }
        }
    }
}

4.4 凭证使用扩展

    [作者:Surpassme]看到前面每次使用凭证比较麻烦,为此Jenkins也提供另外一种扩展方法来使用凭证,使用credentials hepler(仅允许在environment中使用)来简化凭证使用。而credentials hepler也仅支持Secret text、Username with password和Secret file三种凭证

4.4.1 Username with password

    在pipeline中的使用示例如下所示:

pipeline{
    agent any
    options{
        timestamps()
    }
    environment{
        GET_USERNAME_PASSWD=credentials("surpass-123")
    }
    stages{
        stage("credential demo"){
            steps{
               echo "username is ${GET_USERNAME_PASSWD_USR}\npassword is ${GET_USERNAME_PASSWD_PSW}"
            }
        }
    }
}

GET_USERNAME_PASSWD_USR的值是一个字符串,其格式为:<用户名>:<密码>

4.4.2 Secret file

    在pipeline中的使用示例如下所示:

pipeline{
    agent any
    options{
        timestamps()
    }
    environment{
        SECRET_FILE=credentials("secret-file-123")
    }
    stages{
        stage("credential demo"){
            steps{
               echo "secret file is ${SECRET_FILE}"
            }
        }
    }
}

4.4.3 Secret text

    [作者:Surpassme]在pipeline中的使用示例如下所示:

pipeline{
    agent any
    options{
        timestamps()
    }
    environment{
        SECRET_TEXT=credentials("secret-text-123")
    }
    stages{
        stage("credential demo"){
            steps{
               echo "secret text is ${SECRET_TEXT}"
            }
        }
    }
}

原文地址:https://www.jianshu.com/p/e99708a14aa0

本文同步在微信订阅号上发布,如各位小伙伴们喜欢我的文章,也可以关注我的微信订阅号:woaitest,或扫描下面的二维码添加关注:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章