I am currently using Python to read a file from Azure blob storage and store it in a dataframe. In order to authenticate the blob storage I am extracting storage account key from the Azure key vault using the service principal credentials.
My code is as follows:
from azure.keyvault import KeyVaultClient, KeyVaultAuthentication
from azure.common.credentials import ServicePrincipalCredentials
def auth_callback(server, resource, scope):
credentials = ServicePrincipalCredentials(
client_id = '',
client_secret='',
tenant = '',
resource = "https://samplename.vault.azure.net/"
)
token = credentials.token
return token['token_type'], token['access_token']
client = KeyVaultClient(KeyVaultAuthentication(auth_callback))
key_bundle = client.get_key('https://samplename.vault.azure.net/', '','')
json_key = key_bundle.key
However, I have to save the service principal key inside the code, which I feel is not the best practice.
How can I avoid this?
I have also thought of storing the service principal credentials in a separate config file stored in blob storage and then reading it from Python. But that also involves ultimately storing the credentials of tee service principal in a text file.
I am running Python from Azure Batch.