|
| 1 | +import logging |
| 2 | +import sys |
| 3 | + |
| 4 | +import requests |
| 5 | + |
| 6 | +logging.basicConfig( |
| 7 | + level="DEBUG", format="%(asctime)s %(levelname)s: %(message)s", stream=sys.stdout |
| 8 | +) |
| 9 | + |
| 10 | +log = logging.getLogger(__name__) |
| 11 | + |
| 12 | +# user to headers mapping |
| 13 | +headers: dict[str, dict[str, str]] = {} |
| 14 | + |
| 15 | +# Jane Doe has access to specific resources. |
| 16 | +user_jane_doe = { |
| 17 | + "first_name": "Jane", |
| 18 | + "last_name": "Doe", |
| 19 | + "username": "jane.doe", |
| 20 | + |
| 21 | + "roles": [{"name": "User"}], |
| 22 | + "password": "T8mn72D9", |
| 23 | +} |
| 24 | +# Richard Roe has no access. |
| 25 | +user_richard_roe = { |
| 26 | + "first_name": "Richard", |
| 27 | + "last_name": "Roe", |
| 28 | + "username": "richard.roe", |
| 29 | + |
| 30 | + "roles": [{"name": "User"}], |
| 31 | + "password": "NvfpU518", |
| 32 | +} |
| 33 | + |
| 34 | +url = "http://airflow-webserver-default:8080" |
| 35 | +api = "api/v2" |
| 36 | +url_login = f"{url}/auth/login" |
| 37 | + |
| 38 | + |
| 39 | +def obtain_access_token(user: dict[str, str]) -> str: |
| 40 | + token_url = f"{url}/auth/token" |
| 41 | + |
| 42 | + data = {"username": user["username"], "password": user["password"]} |
| 43 | + |
| 44 | + headers = {"Content-Type": "application/json"} |
| 45 | + |
| 46 | + response = requests.post(token_url, headers=headers, json=data) |
| 47 | + |
| 48 | + if response.status_code == 200 or response.status_code == 201: |
| 49 | + token_data = response.json() |
| 50 | + access_token = token_data["access_token"] |
| 51 | + log.info(f"Got access token: {access_token}") |
| 52 | + return access_token |
| 53 | + else: |
| 54 | + log.error( |
| 55 | + f"Failed to obtain access token: {response.status_code} - {response.text}" |
| 56 | + ) |
| 57 | + sys.exit(1) |
| 58 | + |
| 59 | + |
| 60 | +def assert_status_code(msg, left, right): |
| 61 | + if left != right: |
| 62 | + raise AssertionError(f"{msg}\n\tleft: {left}\n\tright: {right}") |
| 63 | + |
| 64 | + |
| 65 | +def check_api_authorization_for_user( |
| 66 | + user, expected_status_code, method, endpoint, data=None |
| 67 | +): |
| 68 | + api_url = f"{url}/{api}" |
| 69 | + |
| 70 | + response = requests.request( |
| 71 | + method, f"{api_url}/{endpoint}", headers=headers[user["email"]], json=data |
| 72 | + ) |
| 73 | + |
| 74 | + assert_status_code( |
| 75 | + f"Unexpected status code for {user["email"]=}", |
| 76 | + response.status_code, |
| 77 | + expected_status_code, |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +def check_api_authorization(method, endpoint, expected_status_code=200, data=None): |
| 82 | + check_api_authorization_for_user( |
| 83 | + user_jane_doe, expected_status_code, method=method, endpoint=endpoint, data=data |
| 84 | + ) |
| 85 | + check_api_authorization_for_user( |
| 86 | + user_richard_roe, 403, method=method, endpoint=endpoint, data=data |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | +def check_website_authorization_for_user(user, expected_status_code): |
| 91 | + username = user["username"] |
| 92 | + password = user["password"] |
| 93 | + with requests.Session() as session: |
| 94 | + login_response = session.post( |
| 95 | + url_login, |
| 96 | + data=f"username={username}&password={password}", |
| 97 | + allow_redirects=True, |
| 98 | + headers={"Content-Type": "application/x-www-form-urlencoded"}, |
| 99 | + ) |
| 100 | + assert login_response.ok, f"Login for {username} failed" |
| 101 | + home_response = session.get(f"{url}/home", allow_redirects=True) |
| 102 | + assert_status_code( |
| 103 | + f"GET /home for user [{username}] failed", |
| 104 | + home_response.status_code, |
| 105 | + expected_status_code, |
| 106 | + ) |
| 107 | + |
| 108 | + |
| 109 | +def test_is_authorized_configuration(): |
| 110 | + # section == null |
| 111 | + check_api_authorization("GET", "config") |
| 112 | + # section != null |
| 113 | + check_api_authorization("GET", "config/section/core/option/dags_folder") |
| 114 | + |
| 115 | + |
| 116 | +def test_is_authorized_connection(): |
| 117 | + # conn_id == null |
| 118 | + check_api_authorization("GET", "connections") |
| 119 | + |
| 120 | + |
| 121 | +def test_is_authorized_dag(): |
| 122 | + # access_entity == null and id == null |
| 123 | + # There is no API endpoint to test this case. |
| 124 | + |
| 125 | + # access_entity == null and id != null |
| 126 | + check_api_authorization("GET", "dags/example_trigger_target_dag") |
| 127 | + |
| 128 | + # access_entity != null and id == null |
| 129 | + # Check "GET /dags/~/dagRuns" because access to "GET /dags" is always allowed |
| 130 | + check_api_authorization("GET", "dags/~/dagRuns") |
| 131 | + |
| 132 | + # access_entity != null and id != null |
| 133 | + check_api_authorization("GET", "dags/example_trigger_target_dag/dagRuns") |
| 134 | + |
| 135 | + |
| 136 | +def test_is_authorized_dataset(): |
| 137 | + # uri == null |
| 138 | + check_api_authorization("GET", "datasets") |
| 139 | + # uri != null |
| 140 | + check_api_authorization("GET", "datasets/s3%3A%2F%2Fdag1%2Foutput_1.txt") |
| 141 | + |
| 142 | + |
| 143 | +def test_is_authorized_pool(): |
| 144 | + # name == null |
| 145 | + check_api_authorization("GET", "pools") |
| 146 | + # name != null |
| 147 | + check_api_authorization("GET", "pools/default_pool") |
| 148 | + |
| 149 | + |
| 150 | +def test_is_authorized_variable(): |
| 151 | + # key != null |
| 152 | + check_api_authorization( |
| 153 | + "POST", "variables", 201, data={"key": "myVar", "value": "1"} |
| 154 | + ) |
| 155 | + # key == null |
| 156 | + check_api_authorization("GET", "variables/myVar") |
| 157 | + |
| 158 | + |
| 159 | +def test_is_authorized_asset(): |
| 160 | + # name == null |
| 161 | + check_api_authorization("GET", "assets") |
| 162 | + # name != null |
| 163 | + check_api_authorization("GET", "assets/3") ## 'test-asset' has id 3 |
| 164 | + |
| 165 | + |
| 166 | +def test_is_authorized_view(): |
| 167 | + check_website_authorization_for_user(user_jane_doe, 200) |
| 168 | + check_website_authorization_for_user(user_richard_roe, 200) |
| 169 | + |
| 170 | + |
| 171 | +access_token_jane_doe = obtain_access_token(user_jane_doe) |
| 172 | +headers[user_jane_doe["email"]] = { |
| 173 | + "Authorization": f"Bearer {access_token_jane_doe}", |
| 174 | + "Content-Type": "application/json", |
| 175 | +} |
| 176 | +access_token_richard_roe = obtain_access_token(user_richard_roe) |
| 177 | +headers[user_richard_roe["email"]] = { |
| 178 | + "Authorization": f"Bearer {access_token_richard_roe}", |
| 179 | + "Content-Type": "application/json", |
| 180 | +} |
| 181 | + |
| 182 | +test_is_authorized_configuration() |
| 183 | +test_is_authorized_connection() |
| 184 | +test_is_authorized_dag() |
| 185 | +test_is_authorized_pool() |
| 186 | +test_is_authorized_variable() |
| 187 | +test_is_authorized_view() |
| 188 | +test_is_authorized_asset() |
0 commit comments