about summary refs log tree commit diff
path: root/scripts/authentication
diff options
context:
space:
mode:
authorzsloan2022-01-14 18:22:32 +0000
committerzsloan2022-01-14 18:22:32 +0000
commit68ac19153b128f60b660e11365e5fd4304c95300 (patch)
tree198e03522af43a2d41f3c02cf3785bcfd4635fc4 /scripts/authentication
parentf588ad96ae5045499860fa6e2740e101ad4410d7 (diff)
parent9ab0c3b6cc146e1711f1478242d4198eed720e4c (diff)
downloadgenenetwork2-68ac19153b128f60b660e11365e5fd4304c95300.tar.gz
Merge branch 'testing' of github.com:genenetwork/genenetwork2 into feature/add_rqtl_pairscan
Diffstat (limited to 'scripts/authentication')
-rwxr-xr-xscripts/authentication/editors.py46
-rwxr-xr-x[-rw-r--r--]scripts/authentication/group.py120
-rwxr-xr-x[-rw-r--r--]scripts/authentication/resource.py18
3 files changed, 129 insertions, 55 deletions
diff --git a/scripts/authentication/editors.py b/scripts/authentication/editors.py
new file mode 100755
index 00000000..dc3b1075
--- /dev/null
+++ b/scripts/authentication/editors.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python3
+"""Manually add editors users"""
+import redis
+import json
+import uuid
+import datetime
+
+if __name__ == "__main__":
+    conn = redis.Redis(decode_responses=True)
+    group_uid = ""
+    for guid in conn.hgetall("groups"):
+        group_details = json.loads(conn.hget("groups", guid))
+        if group_details.get("name") == "editors":
+            group_uid = guid
+            break
+
+    if not group_uid:
+        group_uid = str(uuid.uuid4())
+        timestamp = datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p')
+        conn.hset(
+            "groups",
+            group_uid,
+            json.dumps(
+                {
+                    "name": "editors",
+                    "admins": [],
+                    "members": ["8ad942fe-490d-453e-bd37-56f252e41603"],
+                    "created_timestamp": timestamp,
+                    "changed_timestamp": timestamp,
+                }))
+
+    for resource in conn.hgetall("resources"):
+        _resource = json.loads(conn.hget("resources", resource))
+        _resource["default_mask"] = {
+            'data': 'view',
+            'metadata': 'view',
+            'admin': 'not-admin',
+        }
+        _resource["group_masks"] = {
+            group_uid: {
+                'metadata': 'edit',
+                'data': 'edit',
+                'admin': 'edit-admins',
+            }}
+        conn.hset("resources", resource, json.dumps(_resource))
+    print("Done adding editor's group to resources!")
diff --git a/scripts/authentication/group.py b/scripts/authentication/group.py
index c8c2caad..dd9ba808 100644..100755
--- a/scripts/authentication/group.py
+++ b/scripts/authentication/group.py
@@ -1,3 +1,4 @@
+#!/usr/bin/env python3
 """A script for adding users to a specific group.
 
 Example:
@@ -6,22 +7,40 @@ Assuming there are no groups and 'test@bonfacemunyoki.com' does not
 exist in Redis:
 
 .. code-block:: bash
-   python group.py -g "editors" -m "test@bonfacemunyoki.com"
+  python group.py -n "editors" \
+                  -m "me@bonfacemunyoki.com"
 
 results in::
 
-   Successfully created the group: 'editors'
-   Data: '{"admins": [], "members": []}'
+  Successfully created the group: 'editors'
+  `HGET groups 0360449f-5a96-4940-8522-b22d62085da9`: {'name': 'editors', 'admins': [], 'members': ['8ad942fe-490d-453e-bd37-56f252e41603'], 'changed_timestamp': 'Oct 19 2021 09:34AM', 'created_timestamp': 'Oct 19 2021 09:34AM'}
+  
 
-If 'me@bonfacemunyoki.com' exists in 'users' in Redis and we run:
+Assuming we have a group's unique id:
 
 .. code-block:: bash
-   python group.py -g "editors" -m "me@bonfacemunyoki.com"
+  python group.py -n "editors" \
+                  -m "me@bonfacemunyoki.com" \
+                  -g "8ad942fe-490d-453e-bd37-56f252e41603"
 
 now results in::
 
-   No new group was created.
-   Updated Data: {'admins': [], 'members': ['me@bonfacemunyoki.com']}
+  Successfully created the group: 'editors'
+  `HGET groups 8ad942fe-490d-453e-bd37-56f252e41603`: {'name': 'editors', 'admins': [], 'members': ['8ad942fe-490d-453e-bd37-56f252e41603'], 'changed_timestamp': 'Oct 19 2021 09:38AM', 'created_timestamp': 'Oct 19 2021 09:38AM'}
+  
+If 'me@bonfacemunyoki.com' exists in 'users' in Redis for the above
+command and we run:
+
+.. code-block:: bash
+  python group.py -n "editors" \
+                  -m "me@bonfacemunyoki.com" \
+                  -g "8ad942fe-490d-453e-bd37-56f252e41603"
+
+now results in::
+
+  No new group was created.
+  `HGET groups 8ad942fe-490d-453e-bd37-56f252e41603`: {'name': 'editors', 'admins': [], 'members': ['8ad942fe-490d-453e-bd37-56f252e41603'], 'changed_timestamp': 'Oct 19 2021 09:40AM'}
+
 
 """
 
@@ -36,44 +55,48 @@ from typing import Dict, Optional, Set
 
 def create_group_data(users: Dict, target_group: str,
                       members: Optional[str] = None,
-                      admins: Optional[str] = None) -> Dict:
-    """Return a dictionary that contains the following keys: "key",
-    "field", and "value" that can be used in a redis hash as follows:
-    HSET key field value
+                      admins: Optional[str] = None,
+                      group_id: Optional[str] = None) -> Dict:
+    """Create group data which is isomorphic to a redis HSET i.e.: KEY,
+    FIELD and VALUE.  If the group_id is not specified, a unique hash
+    will be generated.
 
     The "field" return value is a unique-id that is used to
     distinguish the groups.
 
-    Parameters:
-
-    - `users`: a list of users for example:
-
-    {'8ad942fe-490d-453e-bd37-56f252e41603':
-    '{"email_address": "me@test.com",
-      "full_name": "John Doe",
-      "organization": "Genenetwork",
-      "password": {"algorithm": "pbkdf2",
-                   "hashfunc": "sha256",
-                    "salt": "gJrd1HnPSSCmzB5veMPaVk2ozzDlS1Z7Ggcyl1+pciA=",
-                    "iterations": 100000, "keylength": 32,
-                    "created_timestamp": "2021-09-22T11:32:44.971912",
-                    "password": "edcdaa60e84526c6"},
-                    "user_id": "8ad942fe", "confirmed": 1,
-                    "registration_info": {
-                        "timestamp": "2021-09-22T11:32:45.028833",
-                        "ip_address": "127.0.0.1",
-                        "user_agent": "Mozilla/5.0"}}'}
-
-    - `target_group`: the group name that will be stored inside the
-      "groups" hash in Redis.
-
-    - `members`: a comma-separated list of values that contain members
-      of the `target_group` e.g. "me@test1.com, me@test2.com,
-      me@test3.com"
-
-    - `admins`: a comma-separated list of values that contain
-      administrators of the `target_group` e.g. "me@test1.com,
-      me@test2.com, me@test3.com"
+    Args:
+      - users: a list of users for example:
+      {'8ad942fe-490d-453e-bd37-56f252e41603':
+      '{"email_address": "me@test.com",
+        "full_name": "John Doe",
+        "organization": "Genenetwork",
+        "password": {"algorithm": "pbkdf2",
+                     "hashfunc": "sha256",
+                      "salt": "gJrd1HnPSSCmzB5veMPaVk2ozzDlS1Z7Ggcyl1+pciA=",
+                      "iterations": 100000, "keylength": 32,
+                      "created_timestamp": "2021-09-22T11:32:44.971912",
+                      "password": "edcdaa60e84526c6"},
+                      "user_id": "8ad942fe", "confirmed": 1,
+                      "registration_info": {
+                          "timestamp": "2021-09-22T11:32:45.028833",
+                          "ip_address": "127.0.0.1",
+                          "user_agent": "Mozilla/5.0"}}'}
+
+      - target_group: the group name that will be stored inside the
+        "groups" hash in Redis.
+      - members: an optional comma-separated list of values that
+        contain members of the `target_group` e.g. "me@test1.com,
+        me@test2.com, me@test3.com"
+      - admins: an optional comma-separated list of values that
+        contain administrators of the `target_group`
+        e.g. "me@test1.com, me@test2.com, me@test3.com"
+      - group_id: an optional unique identifier for a group. If not
+        set, a unique value will be auto-generated.
+
+    Returns:
+      A dictionary that contains the following keys: "key", "field",
+    and "value" that can be used in a redis hash as follows: HSET key
+    field value
 
     """
     # Emails
@@ -95,11 +118,12 @@ def create_group_data(users: Dict, target_group: str,
 
     timestamp: str = datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p')
     return {"key": "groups",
-            "field": str(uuid.uuid4()),
+            "field": (group_id or str(uuid.uuid4())),
             "value": json.dumps({
                 "name": target_group,
                 "admins": list(admin_ids),
                 "members": list(member_ids),
+                "created_timestamp": timestamp,
                 "changed_timestamp": timestamp,
             })}
 
@@ -107,8 +131,10 @@ def create_group_data(users: Dict, target_group: str,
 if __name__ == "__main__":
     # Initialising the parser CLI arguments
     parser = argparse.ArgumentParser()
-    parser.add_argument("-g", "--group-name",
+    parser.add_argument("-n", "--group-name",
                         help="This is the name of the GROUP mask")
+    parser.add_argument("-g", "--group-id",
+                        help="[Optional] This is the name of the GROUP mask")
     parser.add_argument("-m", "--members",
                         help="Members of the GROUP mask")
     parser.add_argument("-a", "--admins",
@@ -132,7 +158,8 @@ if __name__ == "__main__":
         users=USERS,
         target_group=args.group_name,
         members=members,
-        admins=admins)
+        admins=admins,
+        group_id=args.group_id)
 
     if not REDIS_CONN.hget("groups", data.get("field")):
         updated_data = json.loads(data["value"])
@@ -143,11 +170,10 @@ if __name__ == "__main__":
     created_p = REDIS_CONN.hset(data.get("key", ""),
                                 data.get("field", ""),
                                 data.get("value", ""))
-
     groups = json.loads(REDIS_CONN.hget("groups",
                                         data.get("field")))  # type: ignore
     if created_p:
         exit(f"\nSuccessfully created the group: '{args.group_name}'\n"
-             f"`HGETALL groups {args.group_name}`: {groups}\n")
+             f"`HGET groups {data.get('field')}`: {groups}\n")
     exit("\nNo new group was created.\n"
-         f"`HGETALL groups {args.group_name}`: {groups}\n")
+         f"`HGET groups {data.get('field')}`: {groups}\n")
diff --git a/scripts/authentication/resource.py b/scripts/authentication/resource.py
index 4996f34c..4b8d801a 100644..100755
--- a/scripts/authentication/resource.py
+++ b/scripts/authentication/resource.py
@@ -1,3 +1,4 @@
+#!/usr/bin/env python3
 """A script that:
 
 - Optionally restores data from a json file.
@@ -37,14 +38,14 @@ from datetime import datetime
 def recover_hash(name: str, file_path: str, set_function) -> bool:
     """Recover back-ups using the `set_function`
 
-    Parameters:
+    Args:
+      - name: Redis hash where `file_path` will be restored
+      - file_path: File path where redis hash is sourced from
+      - set_function: Function used to do the Redis backup for
+        example: HSET
 
-    - `name`: Redis hash where `file_path` will be restored
-
-    - `file_path`: File path where redis hash is sourced from
-
-    - `set_function`: Function used to do the Redis backup for
-      example: HSET
+    Returns:
+      A boolean indicating whether the function ran successfully.
 
     """
     try:
@@ -97,7 +98,8 @@ if __name__ == "__main__":
     for resource_id, resource in RESOURCES.items():
         _resource = json.loads(resource)  # str -> dict conversion
         _resource["group_masks"] = {args.group_id: {"metadata": "edit",
-                                                    "data": "edit"}}
+                                                    "data": "edit",
+                                                    "admin": "not-admin"}}
         REDIS_CONN.hset("resources",
                         resource_id,
                         json.dumps(_resource))