Search code examples
pythongoogle-cloud-platformgoogle-secret-manager

Accessing GCP Secret Version Value using Discovery.build with python


Weird instance happening when trying to pull a secret version value using python. I created a client with the discovery.build method, here: https://googleapis.github.io/google-api-python-client/docs/dyn/secretmanager_v1.projects.secrets.versions.html

sm = discovery.build("secretmanager", "v1", credentials = credentials)

I can return the secret,

response = self.sm.projects().secrets().versions().access(name 'projects/17344xxxxx/secrets/api/versions/latest').execute()
pwd = response['payload']['data']

I am not sure how to decode the response to get the actual value, for after filtering the dictionary down, it returns a string.

Thanks


Solution

  • You have to decode the base64 encoded string data to utf-8

    import base64
    pwd = base64.b64decode(response['payload']['data']).decode("utf-8")