简体   繁体   English

通过 fiware orion 上下文代理调用外部 API 以使用 keyrock 进行验证

[英]Calling external APIs through fiware orion context broker to validate using keyrock

I am a student working on a project and exploring viability of using fiware for that.我是一名学生,正在从事一个项目并探索为此使用 fiware 的可行性。 So far I've learnt that to call external APIs we can use registrations for an entity to fetch dynamic data.到目前为止,我已经了解到要调用外部 API,我们可以使用实体的注册来获取动态数据。

Here is the situation:这是情况:

In my project, I am calling external APIs for fetching some data at frontend.在我的项目中,我调用外部 API 在前端获取一些数据。

I want to add access control for users so that they are restricted from calling the APIs if not permitted.我想为用户添加访问控制,以便在不允许的情况下限制他们调用 API。 For this reason I am trying to find out a way such that keyrock can validate the requests so that I don't have to manually validate these external APIs.出于这个原因,我试图找到一种方法,使 keyrock 可以验证请求,这样我就不必手动验证这些外部 API。 Since these aren't related to any entity I don't want to use registration for this purpose.由于这些与任何实体无关,我不想为此目的使用注册。

I intend to do user management through keyrock itself.我打算通过keyrock本身进行用户管理。 Currently keyrock can restrict based on resources (ie URL path) of the application and permission.目前keyrock可以根据应用的资源(即URL路径)和权限进行限制。 I am very confused at this point that if I add an API call at any page to fetch data from external API, how can I make use of keyrock access control in this situation.此时我很困惑,如果我在任何页面添加一个 API 调用以从外部 API 获取数据,在这种情况下如何使用 keyrock 访问控制。

Also, can I make orion call the external API somehow and make the data an entity?另外,我可以让 orion 以某种方式调用外部 API 并使数据成为实体吗?

Any help and hint is greatly appreciated.非常感谢任何帮助和提示。 Thanks in Advance.提前致谢。

A registration is a contract to return a series of attributes connected to an entity, how that is connected to an external API is up to you.注册是一个合同,返回一系列连接到实体的属性,如何连接到外部 API 由您决定。 There is an annotated example in the NGSI v2 tutorials - the code is also available for NGSI-LD but the documentation for NGSI-LD needs updating to reflect certain recent changes and clarifications made in NGSI-LD 1.6.1. NGSI v2 教程中有一个带注释的示例 - 该代码也可用于NGSI-LD ,但 NGSI-LD 的文档需要更新以反映 NGSI-LD 1.6.1 中所做的某些最新更改和说明

Regardless of the version of NGSI you use, the steps to call an external API are the same.不管你使用的是哪个版本的NGSI,调用外部API的步骤都是一样的。

  1. Create a proxy service with a handler to deal with one or more NGSI endpoints - for NGSI-v2 this will usually be the batch endpoint /op/query , for NGSI-LD I would recommend /ngsi-ld/v1/entities/<id> .创建一个带有处理程序的代理服务来处理一个或多个 NGSI 端点 - 对于 NGSI-v2 这通常是批处理端点/op/query ,对于 NGSI-LD 我建议/ngsi-ld/v1/entities/<id>

  2. Create a registration from your context broker to this proxy eg for NGSI-v2:创建从您的上下文代理到此代理的注册,例如 NGSI-v2:

curl -iX POST \
  'http://localhost:1026/v2/registrations' \
  -H 'Content-Type: application/json' \
  -d '{
  "description": "Random Weather Conditions",
  "dataProvided": {
    "entities": [
      {
        "id": "urn:ngsi-ld:Store:001",
        "type": "Store"
      }
    ],
    "attrs": [
      "relativeHumidity"
    ]
  },
  "provider": {
    "http": {
      "url": "http://location/of/the/proxy/interface"
    }
  }
}'

Note that you can also pass additional custom information using custom headers or annotating the path of the URL or whatever.请注意,您还可以使用自定义标头或注释 URL 或其他任何路径来传递其他自定义信息。

  1. Within the proxy code make a request to the third party API and convert the response back to NGSI format.在代理代码中向第三方 API 发出请求并将响应转换回 NGSI 格式。 The tutorial example explains how to connect to Twitter or Cat Facts as examples.教程示例解释了如何连接到TwitterCat Facts作为示例。

I want to add access control for users so that they are restricted from calling the APIs if not permitted.我想为用户添加访问控制,以便在不允许的情况下限制他们调用 API。

This is purely a matter of placing a PEP proxy in front of the call to the registrant.这纯粹是在调用注册人之前放置一个 PEP 代理的问题。 Imagine a context broker request like this one to Kong :想象一下像这样的上下文代理请求Kong

curl -X GET \
  http://localhost:8000/orion/v2/entities/urn:ngsi-ld:Store:001?options=keyValues \
  -H 'Authorization: Bearer {{X-Access-token}}'

Either you place the PEP in front of the context broker (in which case the entity is only returned if you have appropriate permissions, or you place a PEP in front of your registrant webservice, in which case the attributes are only appended to the entity if you have appropriate permissions. Note that the context broker Registration needs to be configured to ensure that the Authorization header will be passed on to the registrant as well.您可以将 PEP 放在上下文代理的前面(在这种情况下,只有在您拥有适当的权限时才会返回实体,或者您将 PEP 放在您的注册人 web 服务前面,在这种情况下,属性只会附加到实体,如果你有适当的权限。请注意,需要配置上下文代理注册以确保Authorization header 也将传递给注册人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM