Exemple #1
0
def test_credentials_sign_via_params():
    c = Credentials(
        'AKIDEXAMPLE',
        'wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY',
        'us-east-1',
        'iam',
    )
    canon_request = CanonicalRequest(
        'GET',
        '/',
        'Action=ListUsers&Version=2010-05-08',
        {
            'Host': 'iam.amazonaws.com',
            'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
        },
    )
    canon_request._datetime = lambda: DateTime(2015, 8, 30, 12, 36)
    params = c.sign_via_query_string(canon_request)
    assert len(params) == 6
    assert params[0] == ('X-Amz-Algorithm', 'AWS4-HMAC-SHA256')
    assert params[1] == (
        'X-Amz-Credential', 'AKIDEXAMPLE/20150830/us-east-1/iam/aws4_request',
    )
    assert params[2] == ('X-Amz-Date', '20150830T123600Z')
    assert params[3] == ('X-Amz-Expires', '60')
    assert params[4] == ('X-Amz-SignedHeaders', 'content-type;host')
    assert params[5] == (
        'X-Amz-Signature',
        '37ac2f4fde00b0ac9bd9eadeb459b1bbee224158d66e7ae5fcadb70b2d181d02',
    )
Exemple #2
0
def test_credentials_sign_via_headers_add_date():
    c = Credentials(
        'AKIDEXAMPLE',
        'wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY',
        'us-east-1',
        'iam',
    )
    canon_request = CanonicalRequest(
        'GET',
        '/',
        'Action=ListUsers&Version=2010-05-08',
        {
            'Host': 'iam.amazonaws.com',
            'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
        },
    )
    canon_request._datetime = lambda: DateTime(2015, 8, 30, 12, 36)
    headers = c.sign_via_headers(canon_request)
    assert len(headers) == 2
    assert headers[0] == ('X-Amz-Date', '20150830T123600Z')
    assert headers[1] == ('Authorization', (
        'AWS4-HMAC-SHA256 '
        + 'Credential=AKIDEXAMPLE/20150830/us-east-1/iam/aws4_request, '
        + 'SignedHeaders=content-type;host;x-amz-date, '
        + 'Signature=5d672d79c15b13162d9279b0855cfba6789a8edb4c82c400e06b5924a'
        + '6f2b5d7'
    ))
def test_canon_request_set_date_param_already_exists():
    canon_request = CanonicalRequest(
        'GET',
        '/',
        'Action=ListUsers&Version=2010-05-08&X-Amz-Date=20150830T123600Z',
    )
    assert canon_request.set_date_param() is None
    assert canon_request.query == [
        ('Action', 'ListUsers'),
        ('Version', '2010-05-08'),
        ('X-Amz-Date', '20150830T123600Z'),
    ]
def test_canon_request_set_date_param():
    canon_request = CanonicalRequest(
        'GET',
        '/',
        'Action=ListUsers&Version=2010-05-08',
    )
    canon_request._datetime = lambda: DateTime(2015, 8, 30, 12, 37)
    assert canon_request.set_date_param() == '20150830T123700Z'
    assert canon_request.query == [
        ('Action', 'ListUsers'),
        ('Version', '2010-05-08'),
        ('X-Amz-Date', '20150830T123700Z'),
    ]
def test_canon_request_set_date_header_already_exists():
    canon_request = CanonicalRequest(
        'GET',
        '/',
        'Action=ListUsers&Version=2010-05-08',
        {
            'Host': 'iam.amazonaws.com',
            'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
            'X-Amz-Date': '20150830T123600Z',
        },
    )
    assert canon_request.set_date_header() is None
    assert canon_request.headers['X-Amz-Date'] == '20150830T123600Z'
def test_canon_request_set_date_header():
    canon_request = CanonicalRequest(
        'GET',
        '/',
        'Action=ListUsers&Version=2010-05-08',
        {
            'Host': 'iam.amazonaws.com',
            'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
        },
    )
    canon_request._datetime = lambda: DateTime(2015, 8, 30, 12, 37)
    assert canon_request.set_date_header() == '20150830T123700Z'
    assert canon_request.headers['x-amz-date'] == '20150830T123700Z'
def test_canon_request_headers():
    canon_request = CanonicalRequest(
        'GET',
        '/',
        headers={
            'Host': 'iam.amazonaws.com',
            'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
            'X-Amz-Date': '20150830T123600Z',
            'My-header1': '   a   b   c ',
            'My-Header2': '"a   b   c"',
        },
    )
    # Case insensitive keys
    assert canon_request.headers['x-aMZ-daTe'] == '20150830T123600Z'
    assert set(canon_request.headers.keys()) == {
        'content-type', 'host', 'my-header1', 'my-header2', 'x-amz-date',
    }
    canon_request.headers['Testing'] = 'foo'
    assert canon_request.headers['tEsting'] == 'foo'
def test_canon_request_payload():
    canon_request = CanonicalRequest(
        'GET',
        '/',
        'Action=ListUsers&Version=2010-05-08',
        payload=b'foo',
    )
    with pytest.raises(NotImplementedError):
        canon_request.payload

    assert (
        canon_request.hashed_payload ==
        '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae'
    )

    canon_request.payload = b''
    assert (
        canon_request.hashed_payload ==
        'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
    )

    # String input shouldn't be accepted.
    with pytest.raises(TypeError):
        canon_request.payload = u''