Acquire Keyvault Secret within a httptrigger and Use it to Acquire Info to be output by Function-Python

大城市里の小女人 提交于 2021-01-20 07:15:21

问题


I have the following code which I use to acquire a secret, use secret to log into portal and download a csv table. This works ok outside a function.

import pandas as pd
import pandas as pd
from arcgis.gis import GIS
from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient

credential = DefaultAzureCredential()
secret_client = SecretClient(vault_url="https://xxxx-dev-vault.vault.azure.net/", credential=credential)
secret = secret_client.get_secret("Secret")

#Log into portal
gis = GIS("https://url.com", "Username", secret.value)

#Extracting Table
item=gis.content.get('content_id')
table=item.layers[0].query().sdf 

I need to include this bit of code in in my httptrigger so that the function logs into portal, extracts csv/table so that the table is returned as a json body of the trigger response or is stored into a blob. How can I achieve this?

I initially thought I could achieve this by integrating the vault in the http trigger in this post. However, I ended up with the Secret being returned instead and I have been unable to use the secret within the function.

I dont mind, even an example logging into an email account or any other portal will suffice provided the secret password is acquired within the function runtime. Ultimately, I am interested in understanding how to retrieve a secret and use it within a function to power/resource a function output.


回答1:


The code is what I test in my side with a csv file in local. But I'm not sure if the line dict_reader = csv.DictReader(table) works in your side. You can do some test and modify the code by yourself if it show error.

import logging

import azure.functions as func
from arcgis.gis import GIS
import csv
import json

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    # do some configuration in application settings of your function app as previous post mentioned, and then we can get the secret of key vault.
    # secret = os.environ["testkeyvault"]

    # gis = GIS("https://url.com", "Username", secret)

    #Extracting Table
    # item=gis.content.get('content_id')
    # table=item.layers[0].query().sdf

    # The four lines below is what I test in my side, I use a csv file in local and convert it to json and use "return func.HttpResponse(json_from_csv)" at the end of the code. The function will response json.
    file = open("C:\\Users\\Administrator\\Desktop\\user.csv", "r")
    dict_reader = csv.DictReader(file)
    dict_from_csv = list(dict_reader)[0]
    json_from_csv = json.dumps(dict_from_csv)

    # Your code should be like the three lines below. But as I didn't test with the csv file from gis, so I'm not sure if "dict_reader = csv.DictReader(table)" can work.
    # dict_reader = csv.DictReader(table)
    # dict_from_csv = list(dict_reader)[0]
    # json_from_csv = json.dumps(dict_from_csv)
    
    return func.HttpResponse(json_from_csv)

=============================Update============================

Change the code to match OP's requirements(And do not forget deploy the function to azure, or we can't get the keyvault secret on azure):



来源:https://stackoverflow.com/questions/65119379/acquire-keyvault-secret-within-a-httptrigger-and-use-it-to-acquire-info-to-be-ou

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!