Last active
August 2, 2021 12:28
-
-
Save pafonta/36c12fa457e71e5f6b8b909fb98ff5ab to your computer and use it in GitHub Desktop.
Nexus v1 - Create a table summarizing the permissions identities have on a path and its ancestors
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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Create a table summarizing the permissions identities have on a path and its ancestors" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"author: @pafonta" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Python minimum version: 3.5" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"To do before and *outside* the notebook: `pip install nexus-sdk pandas openpyxl`" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"scrolled": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import nexussdk as nxs" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Example: https://sandbox.bluebrainnexus.io/v1\n", | |
"with open(\"DEPLOYMENT\", \"r\") as f:\n", | |
" DEPLOYMENT = f.readline().rstrip()\n", | |
" nxs.config.set_environment(DEPLOYMENT)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"with open(\"TOKEN\", \"r\") as f:\n", | |
" TOKEN = f.readline().rstrip()\n", | |
" nxs.config.set_token(TOKEN)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"pp = nxs.tools.pretty_print" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def permissions_summary(subpath: str, filename: str = None, ancestors: bool = True):\n", | |
" from collections import namedtuple\n", | |
" import pandas as pd\n", | |
"\n", | |
" def clean(_id: str) -> str:\n", | |
" base = \"{}/\".format(DEPLOYMENT)\n", | |
" return _id.replace(base, \"\").replace(\"realms/\", \"\")\n", | |
"\n", | |
" Row = namedtuple(\"Row\", [\"path\", \"identity\", \"permissions\"])\n", | |
"\n", | |
" try:\n", | |
" listing = nxs.acls.list(subpath, ancestors=ancestors, self=False)\n", | |
" \n", | |
" except nxs.HTTPError as e:\n", | |
" print(\"<ERROR>\")\n", | |
" try:\n", | |
" pp(e.response.json())\n", | |
" except ValueError:\n", | |
" print(e.response.text)\n", | |
" \n", | |
" else:\n", | |
" if listing[\"_total\"]:\n", | |
" flattened = [Row(x[\"_path\"], clean(y[\"identity\"][\"@id\"]), y[\"permissions\"])\n", | |
" for x in listing[\"_results\"] for y in x[\"acl\"]]\n", | |
"\n", | |
" tuples = [(x.path, x.identity) for x in flattened]\n", | |
" data = [{y: \"#\" for y in x.permissions} for x in flattened]\n", | |
"\n", | |
" headers = pd.MultiIndex.from_tuples(tuples, names=[\"path\", \"identity\"])\n", | |
"\n", | |
" df = pd.DataFrame(data, index=headers).transpose().sort_index(axis=0).fillna(\"\")\n", | |
"\n", | |
" if filename:\n", | |
" df.to_excel(filename)\n", | |
" \n", | |
" props = [(\"text-align\", \"center\"), (\"border\", \"1px black solid !important\")]\n", | |
" \n", | |
" return df.style.set_table_styles(\n", | |
" [{\"selector\": \"th\", \"props\": props}, {\"selector\": \"td\", \"props\": props}])\n", | |
" else:\n", | |
" print(\"<NO PERMISSIONS TO DISPLAY>\")\n", | |
" if filename:\n", | |
" print(\"<NO FILE WRITTEN>\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Notes:\n", | |
"\n", | |
"- To get the root path, use `\"\"` as value for `subpath`.\n", | |
"\n", | |
"- If `filename` is not provided, no file will be created.\n", | |
"\n", | |
"- Created Excel file can be further visually improved." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<style type=\"text/css\" >\n", | |
" #T_1738fde6_5b7b_11e9_b205_784f437a4191 th {\n", | |
" text-align: center;\n", | |
" border: 1px black solid !important;\n", | |
" } #T_1738fde6_5b7b_11e9_b205_784f437a4191 td {\n", | |
" text-align: center;\n", | |
" border: 1px black solid !important;\n", | |
" }</style><table id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191\" ><thead> <tr> <th class=\"index_name level0\" >path</th> <th class=\"col_heading level0 col0\" colspan=2>/</th> <th class=\"col_heading level0 col2\" >/neurosciencegraph/datamodels</th> </tr> <tr> <th class=\"index_name level1\" >identity</th> <th class=\"col_heading level1 col0\" >github/authenticated</th> <th class=\"col_heading level1 col1\" >anonymous</th> <th class=\"col_heading level1 col2\" >github/authenticated</th> </tr></thead><tbody>\n", | |
" <tr>\n", | |
" <th id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191level0_row0\" class=\"row_heading level0 row0\" >organizations/create</th>\n", | |
" <td id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191row0_col0\" class=\"data row0 col0\" >#</td>\n", | |
" <td id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191row0_col1\" class=\"data row0 col1\" ></td>\n", | |
" <td id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191row0_col2\" class=\"data row0 col2\" ></td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191level0_row1\" class=\"row_heading level0 row1\" >organizations/read</th>\n", | |
" <td id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191row1_col0\" class=\"data row1 col0\" >#</td>\n", | |
" <td id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191row1_col1\" class=\"data row1 col1\" ></td>\n", | |
" <td id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191row1_col2\" class=\"data row1 col2\" ></td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191level0_row2\" class=\"row_heading level0 row2\" >permissions/read</th>\n", | |
" <td id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191row2_col0\" class=\"data row2 col0\" >#</td>\n", | |
" <td id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191row2_col1\" class=\"data row2 col1\" >#</td>\n", | |
" <td id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191row2_col2\" class=\"data row2 col2\" ></td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191level0_row3\" class=\"row_heading level0 row3\" >projects/create</th>\n", | |
" <td id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191row3_col0\" class=\"data row3 col0\" >#</td>\n", | |
" <td id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191row3_col1\" class=\"data row3 col1\" ></td>\n", | |
" <td id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191row3_col2\" class=\"data row3 col2\" ></td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191level0_row4\" class=\"row_heading level0 row4\" >projects/read</th>\n", | |
" <td id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191row4_col0\" class=\"data row4 col0\" >#</td>\n", | |
" <td id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191row4_col1\" class=\"data row4 col1\" ></td>\n", | |
" <td id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191row4_col2\" class=\"data row4 col2\" >#</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191level0_row5\" class=\"row_heading level0 row5\" >realms/read</th>\n", | |
" <td id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191row5_col0\" class=\"data row5 col0\" >#</td>\n", | |
" <td id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191row5_col1\" class=\"data row5 col1\" >#</td>\n", | |
" <td id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191row5_col2\" class=\"data row5 col2\" ></td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191level0_row6\" class=\"row_heading level0 row6\" >resources/read</th>\n", | |
" <td id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191row6_col0\" class=\"data row6 col0\" ></td>\n", | |
" <td id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191row6_col1\" class=\"data row6 col1\" ></td>\n", | |
" <td id=\"T_1738fde6_5b7b_11e9_b205_784f437a4191row6_col2\" class=\"data row6 col2\" >#</td>\n", | |
" </tr>\n", | |
" </tbody></table>" | |
], | |
"text/plain": [ | |
"<pandas.io.formats.style.Styler at 0x10273d518>" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"permissions_summary(\"neurosciencegraph/datamodels\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<ERROR>\n", | |
"{\n", | |
" \"resource\": \"https://sandbox.bluebrainnexus.io/v1/acls/neurosciencegraph/datamodels\",\n", | |
" \"reason\": \"Access 'acls/read' to resource 'https://sandbox.bluebrainnexus.io/v1/acls/neurosciencegraph/datamodels' denied.\",\n", | |
" \"permission\": \"acls/read\",\n", | |
" \"@type\": \"AccessDenied\",\n", | |
" \"@context\": \"https://bluebrain.github.io/nexus/contexts/error.json\"\n", | |
"}\n" | |
] | |
} | |
], | |
"source": [ | |
"permissions_summary(\"neurosciencegraph/datamodels\", ancestors=False)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<style type=\"text/css\" >\n", | |
" #T_1752ec24_5b7b_11e9_a041_784f437a4191 th {\n", | |
" text-align: center;\n", | |
" border: 1px black solid !important;\n", | |
" } #T_1752ec24_5b7b_11e9_a041_784f437a4191 td {\n", | |
" text-align: center;\n", | |
" border: 1px black solid !important;\n", | |
" }</style><table id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191\" ><thead> <tr> <th class=\"index_name level0\" >path</th> <th class=\"col_heading level0 col0\" >/myorg20190404121608714038</th> </tr> <tr> <th class=\"index_name level1\" >identity</th> <th class=\"col_heading level1 col0\" >github/users/pafonta</th> </tr></thead><tbody>\n", | |
" <tr>\n", | |
" <th id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191level0_row0\" class=\"row_heading level0 row0\" >acls/read</th>\n", | |
" <td id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191row0_col0\" class=\"data row0 col0\" >#</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191level0_row1\" class=\"row_heading level0 row1\" >acls/write</th>\n", | |
" <td id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191row1_col0\" class=\"data row1 col0\" >#</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191level0_row2\" class=\"row_heading level0 row2\" >files/write</th>\n", | |
" <td id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191row2_col0\" class=\"data row2 col0\" >#</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191level0_row3\" class=\"row_heading level0 row3\" >organizations/create</th>\n", | |
" <td id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191row3_col0\" class=\"data row3 col0\" >#</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191level0_row4\" class=\"row_heading level0 row4\" >organizations/read</th>\n", | |
" <td id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191row4_col0\" class=\"data row4 col0\" >#</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191level0_row5\" class=\"row_heading level0 row5\" >organizations/write</th>\n", | |
" <td id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191row5_col0\" class=\"data row5 col0\" >#</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191level0_row6\" class=\"row_heading level0 row6\" >projects/create</th>\n", | |
" <td id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191row6_col0\" class=\"data row6 col0\" >#</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191level0_row7\" class=\"row_heading level0 row7\" >projects/read</th>\n", | |
" <td id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191row7_col0\" class=\"data row7 col0\" >#</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191level0_row8\" class=\"row_heading level0 row8\" >projects/write</th>\n", | |
" <td id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191row8_col0\" class=\"data row8 col0\" >#</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191level0_row9\" class=\"row_heading level0 row9\" >resolvers/write</th>\n", | |
" <td id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191row9_col0\" class=\"data row9 col0\" >#</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191level0_row10\" class=\"row_heading level0 row10\" >resources/read</th>\n", | |
" <td id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191row10_col0\" class=\"data row10 col0\" >#</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191level0_row11\" class=\"row_heading level0 row11\" >resources/write</th>\n", | |
" <td id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191row11_col0\" class=\"data row11 col0\" >#</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191level0_row12\" class=\"row_heading level0 row12\" >schemas/write</th>\n", | |
" <td id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191row12_col0\" class=\"data row12 col0\" >#</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191level0_row13\" class=\"row_heading level0 row13\" >views/query</th>\n", | |
" <td id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191row13_col0\" class=\"data row13 col0\" >#</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191level0_row14\" class=\"row_heading level0 row14\" >views/write</th>\n", | |
" <td id=\"T_1752ec24_5b7b_11e9_a041_784f437a4191row14_col0\" class=\"data row14 col0\" >#</td>\n", | |
" </tr>\n", | |
" </tbody></table>" | |
], | |
"text/plain": [ | |
"<pandas.io.formats.style.Styler at 0x1038c3a90>" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"permissions_summary(\"myorg20190404121608714038\", ancestors=False)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.5.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment