|
| 1 | +"""Tests the worker ship niceties. |
| 2 | +
|
| 3 | +:Module: starfleet.tests.worker_ship_utils.test_niceties |
| 4 | +:Copyright: (c) 2023 by Gemini Trust Company, LLC., see AUTHORS for more info |
| 5 | +:License: See the LICENSE file for details |
| 6 | +:Author: Mike Grima <[email protected]> |
| 7 | +""" |
| 8 | +# pylint: disable=unused-argument |
| 9 | +import datetime |
| 10 | + |
| 11 | + |
| 12 | +def test_unwrap_json() -> None: |
| 13 | + """This tests the unwrapping of AWS Config json.""" |
| 14 | + from starfleet.worker_ships.niceties import un_wrap_json |
| 15 | + |
| 16 | + # Simple AWS policy JSON: |
| 17 | + test_str = ( |
| 18 | + '{"policyText": "{\\"Version\\":\\"2008-10-17\\",\\"Statement\\":[{\\"Sid\\":\\"AccountReadOnly\\",\\"Effect\\":\\"Allow\\",\\"Principal\\":{' |
| 19 | + '\\"AWS\\":[\\"arn:aws:iam::000000000001:root\\"]},\\"Action\\":[\\"s3:Get*\\",\\"s3:List*\\"],\\"Resource\\":[' |
| 20 | + '\\"arn:aws:s3:::some-bucket\\",\\"arn:aws:s3:::some-bucket/*\\"]}]}"} ' |
| 21 | + ) |
| 22 | + should_equal = { |
| 23 | + "policyText": { |
| 24 | + "Version": "2008-10-17", |
| 25 | + "Statement": [ |
| 26 | + { |
| 27 | + "Sid": "AccountReadOnly", |
| 28 | + "Effect": "Allow", |
| 29 | + "Principal": {"AWS": ["arn:aws:iam::000000000001:root"]}, |
| 30 | + "Action": ["s3:Get*", "s3:List*"], |
| 31 | + "Resource": ["arn:aws:s3:::some-bucket", "arn:aws:s3:::some-bucket/*"], |
| 32 | + } |
| 33 | + ], |
| 34 | + } |
| 35 | + } |
| 36 | + assert un_wrap_json(test_str) == should_equal |
| 37 | + |
| 38 | + # With URL encoding: |
| 39 | + test_str_with_url_encoding = ( |
| 40 | + '{"path":"/","roleName":"SomeRole","roleId":"AROAALSKDJFLAKSDJFKLJSDF",' |
| 41 | + '"arn":"arn:aws:iam::000000000001:role/SomeRole","createDate":"2023-11-12T18:41:33.000Z",' |
| 42 | + '"assumeRolePolicyDocument":"%7B%22Version%22%3A%222012-10-17%22%2C%22Statement%22%3A%5B%7B%22Effect%22%3A%22Allow%22%2C' |
| 43 | + '%22Principal%22%3A%7B%22Service%22%3A%22ec2.amazonaws.com%22%7D%2C%22Action%22%3A%22sts%3AAssumeRole%22%7D%5D%7D",' |
| 44 | + '"instanceProfileList":[{"path":"/","instanceProfileName":"SomeRole",' |
| 45 | + '"instanceProfileId":"AROAALSKDJFLAKSDJFKLJSDF",' |
| 46 | + '"arn":"arn:aws:iam::000000000001:instance-profile/SomeRole","createDate":"2023-11-12T18:41:33.000Z",' |
| 47 | + '"roles":[{"path":"/","roleName":"SomeRole","roleId":"AROAALSKDJFLAKSDJFKLJSDF",' |
| 48 | + '"arn":"arn:aws:iam::000000000001:role/SomeRole","createDate":"2023-11-12T18:41:33.000Z",' |
| 49 | + '"assumeRolePolicyDocument":"%7B%22Version%22%3A%222012-10-17%22%2C%22Statement%22%3A%5B%7B%22Effect%22%3A%22Allow%22%2C' |
| 50 | + '%22Principal%22%3A%7B%22Service%22%3A%22ec2.amazonaws.com%22%7D%2C%22Action%22%3A%22sts%3AAssumeRole%22%7D%5D%7D",' |
| 51 | + '"description":null,"maxSessionDuration":null,"permissionsBoundary":null,"tags":[],"roleLastUsed":null}]}],' |
| 52 | + '"rolePolicyList":[{"policyName":"Config",' |
| 53 | + '"policyDocument":"%7B%22Statement%22%3A%5B%7B%22Action%22%3A%5B%22config%3Aselectaggregateresourceconfig%22%5D%2C' |
| 54 | + '%22Effect%22%3A%22Allow%22%2C%22Resource%22%3A%5B%22%2A%22%5D%7D%5D%2C%22Version%22%3A%222012-10-17%22%7D"}],' |
| 55 | + '"attachedManagedPolicies":[{"policyName":"SomePolicy",' |
| 56 | + '"policyArn":"arn:aws:iam::000000000001:policy/SomePolicy"}],"permissionsBoundary":null,"tags":[],' |
| 57 | + '"roleLastUsed":null} ' |
| 58 | + ) |
| 59 | + should_equal = { |
| 60 | + "path": "/", |
| 61 | + "roleName": "SomeRole", |
| 62 | + "roleId": "AROAALSKDJFLAKSDJFKLJSDF", |
| 63 | + "arn": "arn:aws:iam::000000000001:role/SomeRole", |
| 64 | + "createDate": "2023-11-12T18:41:33.000Z", |
| 65 | + "assumeRolePolicyDocument": { |
| 66 | + "Version": "2012-10-17", |
| 67 | + "Statement": [{"Effect": "Allow", "Principal": {"Service": "ec2.amazonaws.com"}, "Action": "sts:AssumeRole"}], |
| 68 | + }, |
| 69 | + "instanceProfileList": [ |
| 70 | + { |
| 71 | + "path": "/", |
| 72 | + "instanceProfileName": "SomeRole", |
| 73 | + "instanceProfileId": "AROAALSKDJFLAKSDJFKLJSDF", |
| 74 | + "arn": "arn:aws:iam::000000000001:instance-profile/SomeRole", |
| 75 | + "createDate": "2023-11-12T18:41:33.000Z", |
| 76 | + "roles": [ |
| 77 | + { |
| 78 | + "path": "/", |
| 79 | + "roleName": "SomeRole", |
| 80 | + "roleId": "AROAALSKDJFLAKSDJFKLJSDF", |
| 81 | + "arn": "arn:aws:iam::000000000001:role/SomeRole", |
| 82 | + "createDate": "2023-11-12T18:41:33.000Z", |
| 83 | + "assumeRolePolicyDocument": { |
| 84 | + "Version": "2012-10-17", |
| 85 | + "Statement": [{"Effect": "Allow", "Principal": {"Service": "ec2.amazonaws.com"}, "Action": "sts:AssumeRole"}], |
| 86 | + }, |
| 87 | + "description": None, |
| 88 | + "maxSessionDuration": None, |
| 89 | + "permissionsBoundary": None, |
| 90 | + "tags": [], |
| 91 | + "roleLastUsed": None, |
| 92 | + } |
| 93 | + ], |
| 94 | + } |
| 95 | + ], |
| 96 | + "rolePolicyList": [ |
| 97 | + { |
| 98 | + "policyName": "Config", |
| 99 | + "policyDocument": { |
| 100 | + "Statement": [{"Action": ["config:selectaggregateresourceconfig"], "Effect": "Allow", "Resource": ["*"]}], |
| 101 | + "Version": "2012-10-17", |
| 102 | + }, |
| 103 | + } |
| 104 | + ], |
| 105 | + "attachedManagedPolicies": [{"policyName": "SomePolicy", "policyArn": "arn:aws:iam::000000000001:policy/SomePolicy"}], |
| 106 | + "permissionsBoundary": None, |
| 107 | + "tags": [], |
| 108 | + "roleLastUsed": None, |
| 109 | + } |
| 110 | + assert un_wrap_json(test_str_with_url_encoding) == should_equal |
| 111 | + |
| 112 | + # And again with some strange nesting: |
| 113 | + test_nested = { |
| 114 | + "how": [ |
| 115 | + "nested", |
| 116 | + { |
| 117 | + "can": '{"we": "really", "really": "[\\"get\\", \\"{\\\\\\"into\\\\\\": \\\\\\"{\\\\\\\\\\\\\\"really\\\\\\\\\\\\\\": ' |
| 118 | + '\\\\\\\\\\\\\\"deep\\\\\\\\\\\\\\"}\\\\\\"}\\"]"}' |
| 119 | + }, |
| 120 | + 94, |
| 121 | + 3.14, |
| 122 | + {"more": '{"and again": "{\\"this and\\": \\"that\\"}"}'}, |
| 123 | + ] |
| 124 | + } |
| 125 | + should_equal = { |
| 126 | + "how": ["nested", {"can": {"we": "really", "really": ["get", {"into": {"really": "deep"}}]}}, 94, 3.14, {"more": {"and again": {"this and": "that"}}}] |
| 127 | + } |
| 128 | + assert un_wrap_json(test_nested) == should_equal |
| 129 | + |
| 130 | + # And values that are non-JSON: |
| 131 | + now = datetime.datetime.utcnow() |
| 132 | + assert un_wrap_json(now) == str(now) |
| 133 | + assert un_wrap_json(19) == 19 |
| 134 | + assert un_wrap_json(3.14) == 3.14 |
| 135 | + assert un_wrap_json(True) is True |
| 136 | + |
| 137 | + # Try it with something bizarre, like a function: |
| 138 | + assert un_wrap_json(test_unwrap_json) == test_unwrap_json # pylint: disable=comparison-with-callable |
| 139 | + |
| 140 | + # Test that we are sorting lists: |
| 141 | + test_sorted = {"a_list": [11, 5.2, 2, 0, 3], "b_list": ["a", "b", "c"], "with_nested_objs": [{"A": "Value"}, {"qwerty": "uiop[]"}]} |
| 142 | + assert un_wrap_json(test_sorted) == {"a_list": [0, 2, 3, 5.2, 11], "b_list": ["a", "b", "c"], "with_nested_objs": [{"A": "Value"}, {"qwerty": "uiop[]"}]} |
0 commit comments