4

hello I'm trying to convert a google service account JSON key (contained in a base64 encoded field named privateKeyData in file foo.json - more context here ) into the actual JSON file (I need that format as ansible only accepts that)

The foo.json file is obtained using this google python api method

what I'm trying to do (though I am using python) is also described this thread which by the way does not work for me (tried on OSx and Linux).

#!/usr/bin/env python3

import json
import base64

with open('/tmp/foo.json', 'r') as f:
    ymldict = json.load(f)

b64encodedCreds = ymldict['privateKeyData']

b64decodedBytes = base64.b64decode(b64encodedCreds,validate=True)

outputStr = b64decodedBytes
print(outputStr)

#issue
outputStr = b64decodedBytes.decode('UTF-8')
print(outputStr)

yields

./test.py 
b'0\x82\t\xab\x02\x01\x030\x82\td\x06\t*\x86H\x86\xf7\r\x01\x07\x01\xa0\x82\tU\x04\x82\tQ0\x82\tM0\x82\x05q\x06\t*\x86H\x86\xf7\r\x01\x07\x01\xa0\x82\x05b\x04\x82\x05^0\x82\x05Z0\x82\x05V\x06\x0b*\x86H\x86\xf7\r\x01\x0c\n\x01\x02\xa0\x82\x#TRUNCATING HERE
    Traceback (most recent call last):
      File "./test.py", line 17, in <module>
        outputStr = b64decodedBytes.decode('UTF-8')
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0x82 in position 1: invalid start byte

I think I have run out of ideas and spent now more than a day on this :(

what am I doing wrong?

Nellicus
  • 43
  • 1
  • 3

1 Answers1

0

Your base64 decoding logic looks fine to me. The problem you are facing is probably due to a character encoding mismatch. The response body you received after calling create (your foo.json file) is probably not encoded with UTF-8. Check out the response header's Content-Type field. It should look something like this:

Content-Type: text/javascript; charset=Shift_JIS

Try to decode your base64 decoded string with the encoding used in the content type

b64decodedBytes.decode('Shift_JIS')
MUG4N
  • 19,377
  • 11
  • 56
  • 83
  • thank you for your help Mug4n. I've tried running that `create` method with tcpdump on all I can see grepping (can't seem to filter on that connection specifically) shows Content-Type with charset utf-8... :( – Nellicus Nov 25 '18 at 20:38
  • it is now working and funny enough I have no idea what changed.thanks again – Nellicus Nov 25 '18 at 21:58
  • privateKeyData was starting with "MII...." when I was hitting the issue. now I get back "ewo....." . god knows what happened here – Nellicus Nov 25 '18 at 22:00