繁体   English   中英

通过来自使用烧瓶开发并部署在 APP 引擎中的 IAP 的 HTPP 请求向 GCP 进行身份验证的最佳方法是什么?

[英]what is the best way to authenticate to GCP via an HTPP request from an IAP developed with flask and deployed in APP engine?

有人可以帮我在 GCP 上部署烧瓶 IAP,这是我的问题的解释。

我想在 GCP 上部署我用 flask python 创建的 IAP,在我的 IAP 中我调用了一个存储在谷歌存储中的文件。 问题是当我将 IAP 部署到应用程序引擎时,由于身份验证,我无法查询它。

这是 IAP 代码。

app = Flask(__name__)
@app.route("/get_result", methods = ['GET', 'POST'])
def get_result():
    r = request
    bucket_name = request.args.get('bucket')
    model_name = request.args.get('model')
    authentification = request.args.get('login')

    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = authentification

    nparr = np.fromstring(r.data, np.uint8)
    # decode image
    image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
    images = []
    images.append(image)

    pipeline = load_joblib(bucket_name, model_name)

    pred = pipeline.predict(images)

    return json.dumps({"classe" : pred.tolist()})

if __name__ == '__main__':
     app.run(debug = False) 

这是在 GCP 上部署后查询我的 IAP 的代码

img=cv2.imread('img.jpg')
img = cv2.resize(img, (224, 224), interpolation = cv2.INTER_AREA)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

content_type = 'image/jpeg'
headers = {'content-type': content_type}
# encode image as jpeg
_, img_encoded = cv2.imencode('.jpg', img)
# send http request with image and receive response
url = "https://testapiflask0.ey.r.appspot.com/get_result?bucket=model_test0&model=model.pkl&login=testapiflask0-3fb3109ee673.json"
response = requests.post(url, data=img_encoded.tobytes(), headers=headers)
print(response.text)

这是我得到的错误:

'\n<html><head>\n<meta http-equiv="content-type" content="text/html;charset=utf-8">\n<title>500 Server Error</title>\n</head>\n<body text=#000000 bgcolor=#ffffff>\n<h1>Error: Server Error</h1>\n<h2>The server encountered an error and could not complete your request.<p>Please try again in 30 seconds.</h2>\n<h2></h2>\n</body></html>\n'

在我的代码中,我将我的服务帐户的 JSON 文件的路径作为 http 请求的参数提供给 GCP 进行身份验证。

有人可以告诉我通过 http 请求向 GCP 进行身份验证的最佳方式是什么。

先感谢您

我假设作为第一步,您创建了一个服务帐户,用于运行具有必要权限的代码。

然后,如GCP 文档中所述,您可以尝试将凭证文件提供给 Cloud Storage 客户端:

def explicit():
    from google.cloud import storage

    # Explicitly use service account credentials by specifying the private key
    # file.
    # In your case:
    authentification = request.args.get('login')

    # storage_client = storage.Client.from_service_account_json(
    #    'service_account.json')
    storage_client = storage.Client.from_service_account_json(
        authentification)

    # Make an authenticated API request
    buckets = list(storage_client.list_buckets())
    print(buckets)

话虽如此,请考虑到以这种方式传递凭据相关信息是不推荐的,存在安全风险和不良做法。

相反,当您在 App Engine 中运行应用程序时,您可以使用 GCP 提供的内置安全机制,并使用默认服务帐户用户提供的服务帐户,并授予该服务帐户与您的服务(Cloud Storage)交互所需的权限在这种情况下; 使用此隐式凭据将是一个非常安全的解决方案,并且您的代码更简单:

def implicit():
    from google.cloud import storage

    # If you don't specify credentials when constructing the client, the
    # client library will look for credentials in the environment.
    storage_client = storage.Client()

    # Make an authenticated API request
    buckets = list(storage_client.list_buckets())
    print(buckets)

如您所见,没有任何对安全凭证的引用,所有这些都由 GCP 在幕后处理。

暂无
暂无

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

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