-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): enforce datasource list permissions (#15)
This PR ensures that a a user can only get/list datasources that they have permission to. Close #14
- Loading branch information
Showing
5 changed files
with
133 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,3 +115,93 @@ def test_create_role_as_user(client): | |
assert 200 == roles_response.status_code, response.json | ||
|
||
print(json.dumps(response.json)) | ||
|
||
|
||
def test_access_permitted_resources_as_user(client): | ||
""" | ||
This tests that a user given read access to a specific datasource, will only be able to | ||
list/read from that since datasource | ||
:param client: | ||
:return: | ||
""" | ||
|
||
# Create an admin user | ||
admin_session = tests.get_admin_session(app=client) | ||
|
||
# Create a datasource | ||
payload = { | ||
"type": "minio", | ||
"name": "finance6", | ||
"connection": { | ||
"user": "caikuodda", | ||
"password": "some.password", | ||
"host": "localhost", | ||
"port": "5432", | ||
"database": "initdb", | ||
}, | ||
} | ||
|
||
# Create finance6 datasource | ||
response = client.post( | ||
f"/v1/datasources", | ||
headers=tests.get_header(token=admin_session["token"]), | ||
data=json.dumps(payload), | ||
) | ||
assert 200 == response.status_code, response.json | ||
|
||
# Create finance7 datasource | ||
response = client.post( | ||
f"/v1/datasources", | ||
headers=tests.get_header(token=admin_session["token"]), | ||
data=json.dumps({**payload, "name": "finance7"}), | ||
) | ||
assert 200 == response.status_code, response.json | ||
|
||
# A role allowing access to the finance6 datasource | ||
role = { | ||
"name": f"document-manager{random.randint(3, 19)}", | ||
"policy": { | ||
"items": [ | ||
{"action": "read", "resource": "datasources/finance6"}, | ||
] | ||
}, | ||
} | ||
response = client.post( | ||
f"/v1/roles", | ||
headers=tests.get_header(token=admin_session["token"]), | ||
data=json.dumps(role), | ||
) | ||
assert 200 == response.status_code, response.json | ||
|
||
# Admin creates a regular user assigning them access to the finance6 datasource | ||
user_data = { | ||
"name": "Test User", | ||
"email": "[email protected]", | ||
"password": "password", | ||
"roles": [response.json["id"]], | ||
"enabled": True, | ||
} | ||
|
||
response = client.post( | ||
f"/v1/users", | ||
headers=tests.get_header(token=admin_session["token"]), | ||
data=json.dumps(user_data), | ||
) | ||
assert 200 == response.status_code, response.json | ||
|
||
user_session = client.post( | ||
f"/v1/sessions", | ||
headers=tests.get_header(), | ||
data=json.dumps(user_data), | ||
).json | ||
assert 200 == response.status_code, response.json | ||
|
||
get_datasources = client.get( | ||
f"/v1/datasources", | ||
headers=tests.get_header(token=user_session["token"]), | ||
data=json.dumps(user_data), | ||
).json | ||
|
||
assert 1 == len( | ||
get_datasources["items"] | ||
), f"Expected 1 datasource by received {len(get_datasources.json['items'])}" |