kubernetes - Google Cloud Endpoints 不限制对我的 API 的访问
问题描述
我在这里使用 Google Kubernetes 引擎教程遵循了谷歌云端点:https ://cloud.google.com/endpoints/docs/openapi/get-started-kubernetes-engine 使用我自己的 docker 映像。Kubernetes 部分工作正常,我可以通过负载均衡器 IP 访问我的服务。
但是,当我尝试将我的服务放在云端点之后以保护它时,端点仍然是公开的,并且可以在没有 API 密钥的情况下访问。这是我的openapi.yaml
,部署gcloud endpoints services deploy openapi.yaml
:
swagger: "2.0"
info:
description: "A test."
title: "API test"
version: "1.0.0"
host: "<test-api.endpoints.MY-PROJECT-ID.cloud.goog>"
x-google-endpoints:
- name: "<test-api.endpoints.MY-PROJECT-ID.cloud.goog>"
target: "<MY LOADBALANCER IP>"
#require an API key to access project
security:
- api_key: []
paths:
/:
get:
summary: Return django REST default page
description: test
operationId: test
responses:
"200":
description: OK
securityDefinitions:
# This section configures basic authentication with an API key.
api_key:
type: "apiKey"
name: "key"
in: "query"
当我尝试通过值访问我的服务时host
(因为它仍处于打开状态而被隐藏),它仍然处于打开状态并且不需要 API 密钥。云端点日志中也没有显示任何内容。据我了解,openapi.yaml
单独的配置应该足以限制访问?
解决方案
在此页面(https://swagger.io/docs/specification/2-0/authentication/api-keys/)之后,您的安全性放错了位置。它应该看起来像这样:
swagger: "2.0"
info:
description: "A test."
title: "API test"
version: "1.0.0"
host: "<test-api.endpoints.MY-PROJECT-ID.cloud.goog>"
x-google-endpoints:
- name: "<test-api.endpoints.MY-PROJECT-ID.cloud.goog>"
target: "<MY LOADBALANCER IP>"
#require an API key to access project
paths:
/:
get:
security:
- api_key: []
summary: Return django REST default page
description: test
operationId: test
responses:
"200":
description: OK
securityDefinitions:
# This section configures basic authentication with an API key.
api_key:
type: "apiKey"
name: "key"
in: "query"
编辑:
如果您希望在标头中使用 api-key,则必须更改定义,但您可以将安全性保留在您声明的位置:
swagger: "2.0"
info:
description: "A test."
title: "API test"
version: "1.0.0"
host: "<test-api.endpoints.MY-PROJECT-ID.cloud.goog>"
x-google-endpoints:
- name: "<test-api.endpoints.MY-PROJECT-ID.cloud.goog>"
target: "<MY LOADBALANCER IP>"
#require an API key to access project
security:
- api_key: []
paths:
/:
get:
summary: Return django REST default page
description: test
operationId: test
responses:
"200":
description: OK
securityDefinitions:
# This section configures basic authentication with an API key.
api_key:
type: "apiKey"
name: "key"
in: "header"
我不明白你喜欢哪个版本,所以我都调整了。
推荐阅读
- vbscript - 这是一段代码,每分钟将文件夹中的所有 .csv 文件转换为 .xls。但我寻求一些帮助以避免重复
- ios - 在 Objective-C 中检查给定字符是否是表情符号
- android - 限制支持蓝牙 LE 广告的设备在 Play 商店中的 Android 应用
- php - 优化查询 - 如何选择最高和最低行,从日期时间列按日期分组并返回数组
- python - 具有 Softmax 输出层的二元分类 NN 预测不正确
- javascript - if条件下的异步操作
- spring - 从休眠 3 升级到休眠 4 后 getHibernateTemplate.find() 不工作
- php - 如何用 And 而不是 Like 构建 Zend 谓词
- linux - 在没有提示的情况下使用 FTP 的 mget
- python - python 自定义链表是否在每个节点中携带所有属性?