Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implementing more granular authz level #326

Open
wants to merge 93 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
93 commits
Select commit Hold shift + click to select a range
87f620b
revert to authlib==0.14.2
grugna May 29, 2020
90f2778
Merge branch 'master' into pcdc_dev
grugna May 29, 2020
51516b3
add configurable node name/model for granular auth
grugna Jun 10, 2020
78fe8ec
expand auth functions to check if user has access on the level under …
grugna Jun 10, 2020
3886c9b
fix bug on select entities
grugna Jun 10, 2020
4920522
added auth function when exporting nodes to handle 1 level below proj…
grugna Jun 10, 2020
58891f0
Merge branch 'master' into pcdc_dev
grugna Jun 10, 2020
212f293
Merge pull request #1 from uc-cdis/master
grugna Aug 17, 2020
6668bb7
remove patch since main dependency has been fixed
grugna Aug 17, 2020
76dc42b
improve create_resource in sheepdog after submission
grugna Sep 24, 2020
1cc80cc
fix
grugna Sep 24, 2020
d9faa3c
test create resource granular
grugna Sep 25, 2020
bdf10e2
collect person class at startup
grugna Sep 25, 2020
be794e4
logs
grugna Sep 25, 2020
ba94134
add fix remove logs
grugna Sep 25, 2020
b4fc68e
fix
grugna Sep 25, 2020
94af737
Merge pull request #2 from chicagopcdc/pcdc_dev
grugna Oct 19, 2020
dd7bf08
Merge pull request #3 from uc-cdis/master
grugna Oct 19, 2020
b2a44fa
Merge pull request #4 from chicagopcdc/pcdc_dev
grugna Nov 20, 2020
0564f57
Merge branch 'integration202012' of https://github.com/uc-cdis/sheepd…
grugna Dec 3, 2020
28efd63
Merge branch 'uc-cdis-integration202012' into pcdc_dev
grugna Dec 3, 2020
b03b46e
Merge pull request #6 from chicagopcdc/pcdc_dev
grugna Dec 18, 2020
852fc45
Merge pull request #341 from uc-cdis/master
atharvar28 Feb 22, 2021
081bb3e
Merge pull request #7 from uc-cdis/integration202103
grugna Mar 9, 2021
847fe0e
fix bug in deeper resource auth
grugna Mar 12, 2021
0dbfc5f
Merge pull request #8 from chicagopcdc/pcdc_dev
grugna Mar 25, 2021
ac1692a
Update __init__.py
grugna Apr 6, 2021
88b4dd9
Merge pull request #9 from uc-cdis/master
grugna Apr 6, 2021
e2df231
Merge pull request #10 from chicagopcdc/pcdc_dev
grugna Apr 6, 2021
7ccda72
Merge pull request #11 from chicagopcdc/test_fix
grugna Apr 7, 2021
19f53bd
cleanup
grugna Apr 16, 2021
ccf3a93
Merge pull request #12 from uc-cdis/master
grugna Apr 16, 2021
c64a077
Merge pull request #13 from chicagopcdc/pcdc_dev
grugna Apr 27, 2021
e4177f6
Merge pull request #14 from uc-cdis/master
grugna May 6, 2021
e9a2e64
fix syntax
grugna May 6, 2021
9190474
Update api.py
grugna May 6, 2021
d700e99
Update api.py
grugna May 6, 2021
1f060c8
add missing import
grugna May 7, 2021
7155d81
Update api.py
grugna May 7, 2021
fa5686b
Merge pull request #15 from chicagopcdc/pcdc_dev
grugna Jun 1, 2021
beaf2ed
Merge pull request #16 from uc-cdis/integration202108
grugna Aug 3, 2021
c516ff3
Merge pull request #17 from chicagopcdc/pcdc_dev
grugna Aug 25, 2021
290bab6
Merge branch 'integration202110' of https://github.com/uc-cdis/sheepd…
grugna Sep 16, 2021
dff6e39
merge from 202110
grugna Sep 16, 2021
4b8ce3a
Merge pull request #19 from chicagopcdc/pcdc_dev
grugna Nov 3, 2021
2e476af
Merge pull request #20 from uc-cdis/integration202111
grugna Nov 3, 2021
78b2c24
Merge pull request #21 from chicagopcdc/pcdc_dev
grugna Dec 9, 2021
828de2a
Merge pull request #22 from uc-cdis/integration202112
grugna Dec 9, 2021
ed8111d
Update and rename LICENSE to LICENSE.md
grugna Jan 6, 2022
5a13727
Merge pull request #23 from chicagopcdc/pcdc_dev
grugna Jan 25, 2022
049b96f
Merge pull request #24 from uc-cdis/integration202201
grugna Jan 25, 2022
94d5eb9
Merge pull request #25 from chicagopcdc/pcdc_dev
grugna Jan 25, 2022
720dd54
patch to self referenced outerjoin
grugna Jan 27, 2022
006c00e
Update graph_to_doc.py
grugna Jan 28, 2022
2d06de6
Merge pull request #26 from chicagopcdc/pcdc_dev
grugna Jan 31, 2022
ff4a02a
Merge pull request #27 from chicagopcdc/master
grugna Feb 28, 2022
b3183f7
Merge pull request #28 from uc-cdis/integration202204
grugna Mar 30, 2022
e5c5148
Merge pull request #29 from chicagopcdc/pcdc_dev
grugna Apr 5, 2022
38ebc6e
Merge pull request #30 from chicagopcdc/master
grugna Apr 5, 2022
4f26660
Update __init__.py
grugna Apr 22, 2022
b29ad04
Update graph_to_doc.py
grugna Apr 22, 2022
e792a80
Update graph_to_doc.py
grugna Apr 22, 2022
0665969
Update graph_to_doc.py
grugna Apr 25, 2022
d072286
Update graph_to_doc.py
grugna Apr 25, 2022
f79b0e8
Update graph_to_doc.py
grugna Apr 25, 2022
ce04af9
Update graph_to_doc.py
grugna Apr 25, 2022
c23a670
Update graph_to_doc.py
grugna Apr 25, 2022
29bbecd
Update graph_to_doc.py
grugna Apr 25, 2022
799bd40
Update graph_to_doc.py
grugna Apr 25, 2022
4c7ab2d
Update graph_to_doc.py
grugna Apr 25, 2022
e377fd0
Update graph_to_doc.py
grugna Apr 25, 2022
95a4314
clean up debugging prints
grugna Apr 25, 2022
630e2f4
cleanup
grugna Apr 25, 2022
e0c6480
Merge pull request #32 from uc-cdis/integration202206
grugna Jun 7, 2022
203d133
Merge pull request #34 from uc-cdis/integration202207
grugna Jun 30, 2022
57b94dd
Merge pull request #36 from uc-cdis/integration202210
grugna Oct 4, 2022
d2b5c20
Merge pull request #38 from uc-cdis/integration202211
grugna Nov 3, 2022
6cc8d90
Merge pull request #40 from uc-cdis/master
grugna Dec 8, 2022
4cd0662
Merge pull request #41 from uc-cdis/integration202302
grugna Jan 30, 2023
02ce1ec
Merge branch 'integration202303' of https://github.com/uc-cdis/sheepd…
grugna Mar 1, 2023
094e20f
Merge branch 'uc-cdis-integration202303' into pcdc_dev
grugna Mar 1, 2023
f0f29d7
Merge pull request #45 from uc-cdis/master
grugna Mar 14, 2023
a3e790f
Merge pull request #47 from uc-cdis/master
grugna May 17, 2023
2a40fc0
Merge pull request #49 from uc-cdis/integration202308
grugna Aug 29, 2023
cd9b7f5
Merge pull request #53 from uc-cdis/integration202310
grugna Sep 25, 2023
6318795
Merge pull request #56 from uc-cdis/integration202311
grugna Nov 1, 2023
5a74e9d
Merge pull request #58 from uc-cdis/integration202312
grugna Nov 22, 2023
d3c46ef
Merge pull request #60 from uc-cdis/integration202403
grugna Feb 27, 2024
c9e1faf
Merge pull request #62 from uc-cdis/integration202404
grugna Mar 26, 2024
dcabddd
Merge pull request #64 from uc-cdis/master
grugna Apr 30, 2024
bd3c0b9
Merge pull request #66 from uc-cdis/integration202408
grugna Jul 30, 2024
2ea8b9e
Update Dockerfile
grugna Aug 16, 2024
c35bb69
Merge pull request #68 from uc-cdis/integration202411
grugna Oct 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ENV appname=sheepdog
RUN pip install --upgrade pip poetry
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential libffi-dev musl-dev gcc libxml2-dev libxslt-dev \
curl bash git vim
curl bash git vim postgresql-client

RUN mkdir -p /var/www/$appname \
&& mkdir -p /var/www/.cache/Python-Eggs/ \
Expand Down
201 changes: 0 additions & 201 deletions LICENSE

This file was deleted.

2 changes: 2 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[![Creative Commons License](https://i.creativecommons.org/l/by-nc/4.0/88x31.png)](http://creativecommons.org/licenses/by-nc/4.0/)
This work is licensed under a [Creative Commons Attribution-NonCommercial 4.0 International License](http://creativecommons.org/licenses/by-nc/4.0/).
18 changes: 18 additions & 0 deletions sheepdog/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys
import importlib
import logging
import traceback

Expand Down Expand Up @@ -166,6 +167,23 @@ def app_init(app):
app.auth = ArboristClient()


app.node_authz_entity_name = os.environ.get("AUTHZ_ENTITY_NAME", None)
app.node_authz_entity = None
app.subject_entity = None
if app.node_authz_entity_name:
full_module_name = "datamodelutils.models"
mymodule = importlib.import_module(full_module_name)
for i in dir(mymodule):
app.logger.warn(i)
if i.lower() == "person":
attribute = getattr(mymodule, i)
app.subject_entity = attribute
if i.lower() == app.node_authz_entity_name.lower():
attribute = getattr(mymodule, i)
app.node_authz_entity = attribute



app = Flask(__name__)


Expand Down
110 changes: 107 additions & 3 deletions sheepdog/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import flask

from sheepdog.errors import AuthNError, AuthZError
from sheepdog.globals import ROLES


logger = get_logger(__name__)
Expand Down Expand Up @@ -110,20 +111,53 @@ def authorize_and_call(*args, **kwargs):
return authorize_and_call


def authorize(program, project, roles):
def authorize(program, project, roles, resource_list=None):
resource = "/programs/{}/projects/{}".format(program, project)

resources = []
if resource_list:
for res in resource_list:
resources.append(resource + res)
else:
resources = [resource]

jwt = get_jwt_from_header()
authz = flask.current_app.auth.auth_request(
jwt=jwt, service="sheepdog", methods=roles, resources=[resource]
jwt=jwt, service="sheepdog", methods=roles, resources=resources
)

if not authz:
raise AuthZError("user is unauthorized")


def create_resource(program, project=None):
def create_resource(program, project=None, data=None):
resource = "/programs/{}".format(program)

if project:
resource += "/projects/{}".format(project)

if isinstance(data, list):
for d in data:
get_and_create_resource_values(resource, d)
else:
get_and_create_resource_values(resource, data)


def get_and_create_resource_values(resource, data):
stop_node = flask.current_app.node_authz_entity
person_node = flask.current_app.subject_entity
if data and data["type"] == person_node.label:
resource += "/persons/{}".format(data["submitter_id"])
elif data and data["type"] == stop_node.label:
person = None
if isinstance(data["persons"], list):
person = data["persons"][0]
else:
person = data["persons"]
resource += "/persons/{}/subjects/{}".format(person["submitter_id"], data["submitter_id"])
logger.warn(resource)


logger.info("Creating arborist resource {}".format(resource))

json_data = {
Expand All @@ -139,3 +173,73 @@ def create_resource(program, project=None):
resp.error.code, resp.error.message
)
)


def check_resource_access(program, project, nodes):
subject_submitter_ids = []
stop_node = flask.current_app.node_authz_entity_name

for node in nodes:
if node.label == stop_node:
subject_submitter_ids.append({"id": node.node_id, "submitter_id": node.props.get("submitter_id", None)})
else:
for link in node._pg_links:
tmp_dads = getattr(node, link, None)
if tmp_dads:
tmp_dad = tmp_dads[0]
nodeType = link
path_tmp = nodeType
tmp = node._pg_links[link]["dst_type"]
while tmp.label != stop_node and tmp.label != "program":
# assuming ony one parents
nodeType = list(tmp._pg_links.keys())[0]
path_tmp = path_tmp + "." + nodeType
tmp = tmp._pg_links[nodeType]["dst_type"]
# TODO double check this with deeper relationship > 2 nodes under project
tmp_dad = getattr(tmp_dad, nodeType)[0]

if tmp.label == stop_node:
subject_submitter_ids.append({"id": tmp_dad.node_id, "submitter_id": tmp_dad.props.get("submitter_id", None)})
else:
logger.warn("resource not found " + node.label)
logger.warn(node)

try:
resources = [
"/{}s/{}".format(stop_node, node["submitter_id"])
for node in subject_submitter_ids
]
authorize(program, project, [ROLES["READ"]], resources)
except AuthZError:
return "You do not have read permission on project {} for one or more of the subjects requested"


# TEST BUT YOU NEED TO ADD ACTUAL ID LIST NOT ONLY THE ONE LISTED IN THE DB
def get_authorized_ids(program, project):
try:
mapping = flask.current_app.auth.auth_mapping(current_user.username)
except AuthZError as e:
logger.warn(
"Unable to retrieve auth mapping for user `{}`: {}".format(current_user.username, e)
)
mapping = {}

base_resource_path = "/programs/{}/projects/{}".format(program, project)
result = [resource_path for resource_path, permissions in mapping.items() if base_resource_path in resource_path]
ids = []

for path in result:
parts = path.strip("/").split("/")
if path != "/" and parts[0] != "programs":
continue

if len(parts) > 6 or (len(parts) > 2 and parts[2] != "projects") or (len(parts) > 4 and (flask.current_app.node_authz_entity_name is None or flask.current_app.node_authz_entity is None or parts[4] != (flask.current_app.node_authz_entity_name + "s"))):
continue

if len(parts) < 6:
return(None)
else:
ids.append(parts[5])

return(ids)

Loading
Loading