【问题标题】:Use Curl command to access ElasticCloud-Kibana API secured by Azure AD使用 Curl 命令访问受 Azure AD 保护的 ElasticCloud-Kibana API
【发布时间】:2020-06-30 09:41:14
【问题描述】:

在我将 ElasticCloud 与 Azure AD 集成以进行单点登录后,我无法将 Curl 命令与 AD 身份验证一起使用,这是我正在尝试的:

 curl -X PUT -u myuser:mypassword "elasticcloudhost:port/myindex" -H 'kbn-xsrf: true' -H 'Content-Type: application/json' -d '{"settings" : {"number_of_shards" : 1,"number_of_replicas" : 1}}'

错误信息是:

{"statusCode":404,"error":"Not Found","message":"Not Found"}

我可以使用浏览器将我的 Azure 用户名和密码连接到 kibana,它首先会被重定向到 microsoft-login 页面,然后转到 Kibana 页面,但是它不适用于 Curl 命令。

这是我用于集成的方法:

https://www.elastic.co/blog/saml-based-single-sign-on-with-elasticsearch-and-azure-active-directory

有谁知道如何使它工作? 任何帮助将不胜感激。

更新:

这里我尝试从 Azure AD 应用程序获取访问令牌,然后在 Curl 命令中使用它来获取索引:

#!/bin/bash

host="myApplicationIDURI"
project="test"

token=$(curl -X POST -d "grant_type=client_credentials&client_id=myclientID&client_secret=myclientsecret&resource=myApplicationIDURI" https://login.microsoftonline.com/mytenantID/oauth2/token | awk -F',' '/access_token/ {print $7}' | cut -d ":" -f2 | cut -d'"' -f 2)

echo $token

curl -X GET "$myApplicationIDURI/$project" -H 'kbn-xsrf: true' -H 'Content-Type: application/json' -H "Authorization: Bearer $token"

结果:

 {
"statusCode": 401,
"error": "Unauthorized",
"message": "[security_exception] missing authentication credentials for REST request [/_security/_authenticate], with { header={ WWW-Authenticate={ 0=\"Bearer realm=\\\"security\\\"\" & 1=\"ApiKey\" & 2=\"Basic realm=\\\"security\\\" charset=\\\"UTF-8\\\"\" } } }"

}

我已在我的应用程序清单下添加了此应用程序:

   {
        "allowedMemberTypes": [
            "Application"
        ],
        "description": "Access webapp as an application.",
        "displayName": "access_as_application",
        "id": "b963********",
        "isEnabled": true,
        "lang": null,
        "origin": "Application",
        "value": "access_as_application"
    },

还有 API 权限:

这是我的 kibana.yml

xpack.security.authc.providers: ["saml", "basic"]
server.xsrf.whitelist: ["/api/security/v1/saml"]
xpack.security.authc.saml.realm: azuread-saml

和 elasticsearch.yml:

xpack:
  security:
    authc:
      realms:
        saml:
          azuread-saml:
            order: 2
            attributes.principal: "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"
            attributes.groups: "http://schemas.microsoft.com/ws/2008/06/identity/claims/rolename"
            idp.metadata.path: "https://login.microsoftonline.com/mytenantID/federationmetadata/2007-06/federationmetadata.xml?appid=myapiID"
            idp.entity_id: "https://sts.windows.net/mytenantID/"
            sp.entity_id: "myAppURI"
            sp.acs: "myappURI/api/security/v1/saml"
            sp.logout: "myAppURI/logout"

我在日志中看到的错误是:“内置令牌服务无法解码令牌”

【问题讨论】:

  • 对于令牌,请在 curl 中添加范围并参考此curl -X POST -d “grant_type=client_credentials&client_id=clientid&client_secret= secret&scope= https%3A%2F%2Fgraph.microsoft.com%2F.default” https://login.microsoftonline.com/tenantid/oauth2/v2.0/token
  • @SruthiJ-MSFTIdentity 感谢您的评论。范围从何而来?我在该范围内遇到错误:bash:https%3A%2F%2Fgraph.microsoft.com%2F.default”:找不到命令
  • 401 错误意味着您无权访问它。请您交叉检查您的权限。

标签: curl azure-active-directory kibana elastic-cloud


【解决方案1】:

我认为这个 curl cmd 不会起作用,因为我没有看到您获得访问令牌来执行操作。

curl -X PUT -u myuser:mypassword "elasticcloudhost:port/myindex" -H 'kbn-xsrf: true' -H 'Content-Type: application/json' -d '{"settings" : {"number_of_shards" : 1,"number_of_replicas" : 1}}'

对于第二个问题,出现错误是因为您的企业应用程序已将User assignment required?设置为Yes。请参阅参考资料here

您需要做的是将客户端应用分配给 API 应用的任何应用角色。请参考以下截图。 (请注意,“testGraph”是客户端应用程序,“testG006”是 API 应用程序)顺便说一句,在您的情况下,“myclientID”是客户端应用程序,“myapplicationIDurl”是 API 应用程序。

此步骤将为客户端应用分配 API 应用的应用角色“消费者”。然后你就可以毫无问题地获取访问令牌了。

【讨论】:

  • 我已经创建了它,我现在可以获取令牌但是当我尝试使用 curl 命令和令牌创建索引时它仍然会出错。
  • @Matrix 确保目标资源存在。也许你应该使用GET 方法(如果有的话)来验证它。
  • 你能检查更新的问题吗?我现在收到 401 未经授权的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-01
  • 1970-01-01
  • 2020-02-19
  • 2018-08-24
  • 2021-07-13
  • 1970-01-01
相关资源
最近更新 更多