Skip to content
This repository has been archived by the owner on Jan 25, 2023. It is now read-only.

sign-request.py is not compatible with python 3.x #148

Open
eedwards-sk opened this issue Apr 30, 2019 · 4 comments · May be fixed by #243
Open

sign-request.py is not compatible with python 3.x #148

eedwards-sk opened this issue Apr 30, 2019 · 4 comments · May be fixed by #243

Comments

@eedwards-sk
Copy link
Contributor

eedwards-sk commented Apr 30, 2019

see https://github.com/hashicorp/terraform-aws-vault/blob/master/examples/vault-consul-ami/auth/sign-request.py#L43

Traceback (most recent call last):
File "/opt/vault-login/sign-request.py", line 52, in <module>
vault_request = generate_vault_request()
File "/opt/vault-login/sign-request.py", line 34, in generate_vault_request
'iam_request_url': base64.b64encode(request.url),
File "/usr/local/lib/python3.6/base64.py", line 58, in b64encode
encoded = binascii.b2a_base64(s, newline=False)
TypeError: a bytes-like object is required, not 'str'
boto3==1.9.138
python 3.6
@eedwards-sk
Copy link
Contributor Author

This appears to be an issue with running against python 3.x

in Python 2.x, base64.b64encode takes a string object

in Python 3.x, it takes a bytes object

I'm not sure what the best approach is for making it compatible with both, but here's how I got it working in python 3.x:

    return {
        'iam_http_request_method': request.method,
        'iam_request_url':         base64.b64encode(bytes(request.url, 'utf-8')),
        'iam_request_body':        base64.b64encode(bytes(request.body, 'utf-8')),
        'iam_request_headers':     base64.b64encode(json.dumps(headers_to_go_style(dict(request.headers)))), # It's a CaseInsensitiveDict, which is not JSON-serializable
    }

also had to change iteritems to items

def headers_to_go_style(headers):
    retval = {}
    for k, v in headers.items():
        retval[k] = [v]
    return retval

probably some more changes too... I'll log them here as I find them

@eedwards-sk eedwards-sk changed the title sign-request.py fails due to base64 encoding error sign-request.py is not compatible with python 3.x Apr 30, 2019
@Etiene
Copy link
Contributor

Etiene commented May 1, 2019

I'm not sure what the best approach is for making it compatible with both

I think it's okay if it's py 3 only. PRs welcome! :)

@yorinasub17
Copy link
Contributor

What about adding a separate py3 version of the file (sign-request-py3.py)? Then users can pick and choose which one to include in their AMI when they are copying the examples.

I agree that we shouldn't try to make the existing file py2 + 3 compatible because without six, that will make the code littered with if statements.

@eedwards-sk
Copy link
Contributor Author

okay, it needed a few more tweaks, but here's a version for 3.x:

https://gist.github.com/eedwards-sk/161e98844db85919f6a85a3f7dce58f7

please feel free to toss into a PR or we could use this to figure out a 3.x/2.x compatible version

bastianb added a commit to bastianb/terraform-aws-vault that referenced this issue Apr 16, 2021
bastianb added a commit to bastianb/terraform-aws-vault that referenced this issue Apr 16, 2021
bastianb added a commit to bastianb/terraform-aws-vault that referenced this issue Apr 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants