簡體   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