gpt4 book ai didi

jenkins - 通过脚本更新 Jenkins 凭据

转载 作者:行者123 更新时间:2023-12-03 23:26:07 53 4
gpt4 key购买 nike

我有一个在 Windows 上运行的 Jenkins 服务器。它将用户名:密码存储在凭据插件中。这是一个定期更新密码的服务用户。

我正在寻找一种运行脚本的方法,最好是 Powershell,它将更新 Jenkins 密码存储中的凭据,以便在构建作业脚本中使用它时它始终是最新的。

密码由 Thycotic Secret Server 安装管理,因此我应该能够自动更新此密码的过程,但我几乎没有发现如何完成此操作的线索,即使 blog post编写凭据 api 的人几乎完全提到了这种情况,然后继续链接到凭据插件的下载页面,该页面没有说明如何实际使用 api。

更新

接受的答案完美无缺,但其余方法调用示例使用的是 curl,如果您使用的是 Windows,则它没有多大帮助。特别是如果您尝试调用 REST URL 但您的 Jenkins 服务器正在使用 AD 集成。为此,您可以使用以下脚本。

通过转到人员 > 用户 > 配置 > 显示 API token 找到 userId 和 API token 。

$user = "UserID"
$pass = "APIToken"
$pair = "${user}:${pass}"

$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
$base64 = [System.Convert]::ToBase64String($bytes)

$basicAuthValue = "Basic $base64"

$headers = @{ Authorization = $basicAuthValue }



Invoke-WebRequest `
-uri "http://YourJenkinsServer:8080/scriptler/run/changeCredentialPassword.groovy?username=UrlEncodedTargetusername&password=URLEncodedNewPassword" `
-method Get `
-Headers $headers

最佳答案

Jenkins 支持使用 Groovy 语言编写脚本。您可以获得 scripting console通过在浏览器中打开 URL /script您的 Jenkins 实例。 (即: http://localhost:8080/script )

Groovy 语言(优于 powershell 或其他任何东西)的优点是那些 Groovy 脚本在 Jenkins 中执行并且可以访问所有内容(配置、插件、作业等)。

然后以下代码会将用户“BillHurt”的密码更改为“s3crEt!”:

import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl

def changePassword = { username, new_password ->
def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class,
Jenkins.instance
)

def c = creds.findResult { it.username == username ? it : null }

if ( c ) {
println "found credential ${c.id} for username ${c.username}"

def credentials_store = Jenkins.instance.getExtensionList(
'com.cloudbees.plugins.credentials.SystemCredentialsProvider'
)[0].getStore()

def result = credentials_store.updateCredentials(
com.cloudbees.plugins.credentials.domains.Domain.global(),
c,
new UsernamePasswordCredentialsImpl(c.scope, c.id, c.description, c.username, new_password)
)

if (result) {
println "password changed for ${username}"
} else {
println "failed to change password for ${username}"
}
} else {
println "could not find credential for ${username}"
}
}

changePassword('BillHurt', 's3crEt!')

经典自动化 ( /scriptText )

要自动执行此脚本,您可以将其保存到一个文件(比如 /tmp/changepassword.groovy )并运行以下 curl 命令:

curl -d "script=$(cat /tmp/changepassword.groovy)" http://localhost:8080/scriptText

应该以 HTTP 200 响应状态和文本:

found credential 801cf176-3455-4b6d-a461-457a288fd202 for username BillHurt

password changed for BillHurt



使用 Scriptler 插件实现自动化

您还可以安装 Jenkins Scriptler plugin并按照以下步骤进行:

enter image description here
  • 在侧边菜单中打开 Scriptler 工具

  • enter image description here
  • 填写第 3 个字段,注意将 Id 字段设置为 changeCredentialPassword.groovy
  • 选中定义脚本参数复选框
  • 添加 2 个参数:usernamepassword
  • 粘贴以下脚本:

  •     import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl

    def changePassword = { username, new_password ->
    def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
    com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class,
    jenkins.model.Jenkins.instance
    )

    def c = creds.findResult { it.username == username ? it : null }

    if ( c ) {
    println "found credential ${c.id} for username ${c.username}"

    def credentials_store = jenkins.model.Jenkins.instance.getExtensionList(
    'com.cloudbees.plugins.credentials.SystemCredentialsProvider'
    )[0].getStore()

    def result = credentials_store.updateCredentials(
    com.cloudbees.plugins.credentials.domains.Domain.global(),
    c,
    new UsernamePasswordCredentialsImpl(c.scope, null, c.description, c.username, new_password)
    )

    if (result) {
    println "password changed for ${username}"
    } else {
    println "failed to change password for ${username}"
    }
    } else {
    println "could not find credential for ${username}"
    }
    }

    changePassword("$username", "$password")
  • 然后点击提交按钮

  • 现在您可以调用以下 URL 来更改密码(替换 usernamepassword 参数): http://localhost:8080/scriptler/run/changeCredentialPassword.groovy?username=BillHurt&password=s3crEt%21 (注意需要 urlencode 参数的值)

    或 curl :
    curl -G http://localhost:8080/scriptler/run/changeCredentialPassword.groovy --data-urlencode 'username=BillHurt' --data-urlencode "password=s3crEt!"

    来源:
  • Printing a list of credentials and their IDs
  • Create UserPrivateKeySource Credential via Groovy?
  • credential plugin source code
  • Scriptler plugin

  • 搜索引擎提示:使用关键字 'Jenkins.instance.' , 'com.cloudbees.plugins.credentials'UsernamePasswordCredentialsImpl

    关于jenkins - 通过脚本更新 Jenkins 凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32208763/

    53 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com