我使用Flask和Gunicorn编写Restful API,以最小化对Kubernetes REST的访问。例如,Kubernet REST提供了可用URL的列表:
{
"paths": [
..
..
..
"/apis/xxxx.io",
"/apis/xxxx.io/v1alpha1",
..
. ..
"/metrics",
"/swagger-2.0.0.json",
"/swagger-2.0.0.pb-v1",
"/swagger-2.0.0.pb-v1.gz",
"/swagger-ui/",
"/swagger.json",
"/swaggerapi",
"/ui",
"/ui/",
"/version"
]
}
我想将访问权限限制为:
"/apis/xxxx.io",
"/apis/xxxx.io/v1alpha1",
以及在访问后返回的这两个URL(子URL)下提供的任何GET。
问题:我想允许访问的。我已经找到了http://flask.pocoo.org/snippets/57/,它似乎不能与以下代码一起工作:
import json
import logging
from flask import Flask, jsonify
from flask_cors import CORS
from kubernetes import client, config
app = Flask(__name__)
CORS(app)
config.load_incluster_config()
api_instance = client.CoreV1Api()
def read_file(filename):
with open(filename, 'r') as content_file:
content = content_file.read()
return content
def set_configuration():
configuration = client.Configuration()
configuration.verify_ssl = False
configuration.debug = True
return configuration
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
logger.info("Request Path: " + path)
bearer_header = {"Authorization": "Bearer %s" % read_file('/etc/token')}
client.Configuration.set_default(set_configuration())
v1 = client.CoreV1Api()
ret = v1.api_client.rest_client.GET('https://kubernetes/', bearer_header)
return jsonify(requests.get(url).json())
if __name__ == "__main__":
logging.basicConfig(level=10)
logger = logging.getLogger(__name__)
app.run(host="0.0.0.0", port="5000")
作为Python中Flask/Restful的初学者,您会给出您的见解或线索吗?
致以敬意,
发布于 2018-07-28 03:48:38
看起来我已经知道了如何绕过,但仍然需要弄清楚如何限制访问。我认为Kubernetes服务帐户或规则可能有答案:
@app.route('/',defaults={'path':''}) @app.route('/')默认fms_rest(path):bearer_header ={“授权”:“持有者%s”%read_file(‘/etc/fms_rest’)}
client.Configuration.set_default(set_configuration())
v1 = client.CoreV1Api()
ret = v1.api_client.rest_client.GET('https://kubernetes/', bearer_header)
data = json.loads(ret.data)
if '/' + path in data["paths"]:
return jsonify(json.loads(v1.api_client.rest_client.GET('https://kubernetes/' + path, bearer_header).data))
data["url"] = path
return jsonify(data)
https://stackoverflow.com/questions/51561545
复制相似问题