azure - 如何读取使用 Azure AD 身份验证的 Jenkins 中的队列状态?

标签 azure jenkins groovy

我们拥有授权模式登录用户可以执行任何操作的 Jenkins 实例以及带有 token 的单个管理员 super 用户

我们使用这个 groovy 脚本来查询构建队列:

def builder = new groovy.json.JsonBuilder()

builder {
  queueItems(Jenkins.instance.queue.items.collect { obj ->
      [
        id: obj.id,
        name: obj.task.name,
        isStuck: obj.stuck,
        isBlocked: obj.blocked,
        isDisabled: obj.task.disabled,
        inQueueSince: obj.inQueueSince,
        why: obj.why,
        causesDescription: obj.causesDescription,
        queueItemUrl: obj.url,
        taskUrl: obj.task.url
      ]
   })
}

println builder.toPrettyString()

在我们安装 Azure AD plugin 之前,这一切都运行良好。 。 当我们这样做时,脚本执行请求会产生错误:

Date: Thu, 01 Aug 2019 15:03:29 GMT 
X-Content-Type-Options: nosniff 
X-You-Are-Authenticated-As: superuser 
X-You-Are-In-Group-Disabled: JENKINS-39402: use -Dhudson.security.AccessDeniedException2.REPORT_GROUP_HEADERS=true or use /whoAmI to diagnose 
X-Required-Permission: hudson.model.Hudson.RunScripts 
X-Permission-Implied-By: hudson.model.Hudson.Administer 
Content-Type: text/html;charset=utf-8 
Expires: Thu, 01 Jan 1970 00:00:00 GMT 
Cache-Control: no-cache,no-store,must-revalidate 
X-Hudson-Theme: default 
Referrer-Policy: same-origin 
X-Hudson: 1.395 
X-Jenkins: 2.186 
X-Jenkins-Session: 9cf3a1af 
X-Hudson-CLI-Port: 50000 
X-Jenkins-CLI-Port: 50000 
X-Jenkins-CLI2-Port: 50000 
X-Frame-Options: sameorigin 
Server: Jetty(9.4.z-SNAPSHOT) 
Transfer-Encoding: chunked 

我们尝试使用域用户来验证此请求,如下所示:

curl -k -X POST -u "USERNAME@DOMAIN:PASSWORD"  -d "script=%0A%20%20%20%20def%20builder%20%3D%20new%20groovy.json.JsonBuilder()%0A%0A%09builder%20{%0A%09%20%20queueItems(Jenkins.instance.queue.items.collect%20{%20obj%20-%3E%0A%09%09%20%20%5B%0A%09%09%09id%3A%20obj.id%2C%0A%09%09%09name%3A%20obj.task.name%2C%0A%09%09%09isStuck%3A%20obj.stuck%2C%0A%09%09%09isBlocked%3A%20obj.blocked%2C%0A%09%09%09isDisabled%3A%20obj.task.disabled%2C%0A%09%09%09inQueueSince%3A%20obj.inQueueSince%2C%0A%09%09%09why%3A%20obj.why%2C%0A%09%09%09causesDescription%3A%20obj.causesDescription%2C%0A%09%09%09queueItemUrl%3A%20obj.url%2C%0A%09%09%09taskUrl%3A%20obj.task.url%0A%09%09%20%20%5D%0A%09%20%20%20})%0A%09}%0A%0A%09println%20builder.toPrettyString()" https://jenkinsurl/scriptText

它在 RESter Chrome 插件中使用效果很好,但我们无法使用其他程序(如 CURL)使其工作。

我们发现了 cookie 的问题,在发送第一个请求之前无法设置该 cookie。

那么,在这样的 Jenkins 配置中读取作业队列(使用curl)的正确方法是什么?

我们需要使用现有的 Jenkins 用户 token 而不是 AD 身份验证来执行该操作,或者使用某些插件来读取队列并返回此数据而无需任何身份验证。

最佳答案

目前我只能找到两种解决方法。该问题已在 Jenkins GitHub 上提出。

沉杰在 Jenkins issue page 上提出了以下建议用于帮助确保访问:

In the Azure Active Directory Matrix-based security section, you need to have two users in the matrix to make it work for accessing Jenkins API. The auto-completion will provide you a user name like 'username (object id)'. Besides this, you need to add a user name like 'username (user ID)' and give it the reading permission. By that, the API things should work.

此外,请确认您的curl 请求与RESter Chrome 插件请求的输出相匹配。您的curl命令很可能不会生成与RESter Chrome插件相同的输出。

关于azure - 如何读取使用 Azure AD 身份验证的 Jenkins 中的队列状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57328078/

相关文章:

java - 在java中为客户端生成azure存储SAS token

azure - 适用于 Linux 的 Azure Web App 的文件夹权限

azure - 维护 azure 资源管理器模板的正确方法

jenkins - 为什么 Pipeline @Library() 语法中有时会有一个尾随下划线

jenkins - 有没有一种简单的方法可以将现有的 Jenkins 自由式作业转换为新的 Jenkins 2.x 管道 View

grails - Groovy/Grails : Enhance a Static Closure

azure - 创建 Azure 策略和蓝图所需的最低 IAM 角色是什么

json - 在 Jenkins Pipeline 中从 Groovy 变量创建 JSON 字符串

git - 在 OpenShift 构建之前运行 Grunt 构建

java - Groovy:JLine不等待输入Gradle