Attempting to retrieve binary secret value: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte

Viewed 27

I have a token file that is way too long to retrieve the contents from SecretsManager, so I am compressing the file and storing the bytes in SecretsManager. I then am trying to retrieve the secret value so I can use the token in my application. The secret value is the bytes from the file. I'm running into this error: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte

Looking to see how I can fix this error and retrieve the secret value.

#declare tar file name
tar = "token.cache.tar.gz"
file = tarfile.open(tar,"w:gz")
file.add("token.cache")
file.close()

# store cache in aws secrets manager
client = boto3.client('secretsmanager')
with open("token.cache.tar.gz", "rb") as bts:
     response = client.create_secret(
         Name = 'ms-graph-binary',
         SecretBinary = bts.read()
 )

response = client.get_secret_value(
    SecretId='ac-demo-ms-graph-binary'
) ['SecretBinary'] 
with tarfile.open(fileobj=response.decode('utf-8'), mode='r') as cf:
    cf.extractall("token.cache.decompressed")

Edit: Ended up fixing the code and this is what I used to retrieve the secret.

import tarfile
import io
import boto3

client=boto3.client('secretsmanager')
with tarfile.open('token.cache.tar.gz', "w:gz") as tar:
    tar.add('token.cache')
bts = open('token.cache.tar.gz','rb').read()

print("Length before",len(bts))

sec=client.update_secret(SecretId="ac-demo-ms-graph", SecretBinary=bts)

sec=client.get_secret_value(SecretId="ac-demo-ms-graph")['SecretBinary']

print("Length after",len(sec))

with tarfile.open(fileobj=io.BytesIO(sec), mode='r:gz') as t:

    d=t.extractfile('token.cache')

    #print file content

    print("File content",str(d.read()))
0 Answers
Related