-
Notifications
You must be signed in to change notification settings - Fork 0
Project 4: Custos Python SDK Client
Created a tenant
Requested for Tenant approval
Used a Python SDK client to setup Custos testing, and then used the following:
pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple custos-sdk==1.0.18
- User management client
- Group management client
- Resource secret management client
- Sharing management client
- Identity management client
import os
import json
import random, string
from custos.clients.user_management_client import UserManagementClient
from custos.clients.group_management_client import GroupManagementClient
from custos.clients.resource_secret_management_client import ResourceSecretManagementClient
from custos.clients.sharing_management_client import SharingManagementClient
from custos.clients.identity_management_client import IdentityManagementClient
from custos.transport.settings import CustosServerClientSettings
import custos.clients.utils.utilities as utl
from google.protobuf.json_format import MessageToJson
try :
# read settings
custos_settings = CustosServerClientSettings(custos_host='custos.scigap.org',
custos_port='31499',
custos_client_id='fake custos client id',
custos_client_sec='fake custos client key')
# create custos user management client
user_management_client = UserManagementClient(custos_settings)
# create custos group management client
group_management_client = GroupManagementClient(custos_settings)
# create custos resource secret client
resource_secret_client = ResourceSecretManagementClient(custos_settings)
# create sharing management client
sharing_management_client = SharingManagementClient(custos_settings)
# create identity management client
identity_management_client = IdentityManagementClient(custos_settings)
# obtain base 64 encoded token for tenant
b64_encoded_custos_token = utl.get_token(custos_settings=custos_settings)
created_groups = {}
admin_user_name = "name"
admin_password = "password"
resource_ids = []
print("Successfully configured all custos clients")
except Exception as e:
raise e
print("Custos Id and Secret may wrong "+ str(e))
Successfully configured all custos clients
####Verfy user
- Get admin user
- Update admin user profie
def verifiy_user(login_user_id,login_user_password):
print("Login user "+ login_user_id)
login_reponse = identity_management_client.token(token=b64_encoded_custos_token, username=login_user_id, password=login_user_password, grant_type='password')
login_reponse = MessageToJson(login_reponse)
print("Login response: ", login_reponse)
response = user_management_client.get_user(token=b64_encoded_custos_token, username=login_user_id)
print(" Updating user profile... ")
user_management_client.update_user_profile(
token=b64_encoded_custos_token,
username=response.username,
email=response.email,
first_name=response.first_name,
last_name=response.last_name)
print(" User "+ login_user_id + " successfully logged in and updated profile")
print("verifiy_user method is defined")
verifiy_user method is defined
try:
verifiy_user(admin_user_name,admin_password)
print("Successfully verified user")
except Exception as e:
print("verifiy_user is not defined or user may not be created in the teanant"+ str(e))
Login user [email protected]
Login response: {
"session_state": "5ba796e3-57ab-4cc6-a71f-b22645c737f6",
"token_type": "bearer",
"id_token": "eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJOY0tvV2EtU1FtcHJaQmdkQXNoLVR2RFVPLVdhR1dkaDFtT04wSC05WldvIn0.eyJleHAiOjE2NTE1MzYzMjksImlhdCI6MTY1MTUzNDUyOSwiYXV0aF90aW1lIjowLCJqdGkiOiIzYTk5OWY1My0zNTg4LTQxMTctOTFmOS0zZWIwNmI5ZDg4MTYiLCJpc3MiOiJodHRwczovL2tleWNsb2FrLmN1c3Rvcy5zY2lnYXAub3JnOjMxMDAwL2F1dGgvcmVhbG1zLzEwMDAzNDE3IiwiYXVkIjoiY3VzdG9zLWZwbnV1Z3BnM2NkZDRwMnU5cW15LTEwMDAzNDE3Iiwic3ViIjoiYzg3N2UzZjAtNzYwNy00MTNhLTkxYTctOTFlZDE0MTg0M2NmIiwidHlwIjoiSUQiLCJhenAiOiJjdXN0b3MtZnBudXVncGczY2RkNHAydTlxbXktMTAwMDM0MTciLCJzZXNzaW9uX3N0YXRlIjoiNWJhNzk2ZTMtNTdhYi00Y2M2LWE3MWYtYjIyNjQ1YzczN2Y2IiwiYWNyIjoiMSIsImVtYWlsX3ZlcmlmaWVkIjp0cnVlLCJuYW1lIjoiR2FkYWcsIFZpbmF5YWthIFNoYXJhbmFwcGEiLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJ2Z2FkYWdAaXUuZWR1IiwiZ2l2ZW5fbmFtZSI6IkdhZGFnLCBWaW5heWFrYSIsImZhbWlseV9uYW1lIjoiU2hhcmFuYXBwYSIsImVtYWlsIjoidmdhZGFnQGl1LmVkdSJ9.kjbhdvhW867m1BtLBIP5VIYRZVXg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2UtZXZlbnRzIiwibWFuYWdlLXJlYWxtIiwidmlldy1ldmVudHMiLCJ2aWV3LXVzZXJzIiwidmlldy1jbGllbnRzIiwibWFuYWdlLWF1dGhvcml6YXRpb24iLCJtYW5hZ2UtY2xpZW50cyIsInF1ZXJ5LWdyb3VwcyJdfSwiYWNjb3VudCI6eyJyb2xlcyI6WyJtYW5hZ2UtYWNjb3VudCIsIm1hbmFnZS1hY2NvdW50LWxpbmtzIiwidmlldy1wcm9maWxlIl19fSwic2NvcGUiOiJvcGVuaWQgZW1haWwgcHJvZmlsZSIsImVtYWlsX3ZlcmlmaWVkIjp0cnVlLCJuYW1lIjoiR2FkYWcsIFZpbmF5YWthIFNoYXJhbmFwcGEiLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJ2Z2FkYWdAaXUuZWR1IiwiZ2l2ZW5fbmFtZSI6IkdhZGFnLCBWaW5heWFrYSIsImZhbWlseV9uYW1lIjoiU2hhcmFuYXBwYSIsImVtYWlsIjoidmdhZGFnQGl1LmVkdSJ9.NIODnR6Q3RQmSkhOUJ-TzLBjOlHyA_Rzl1M0Fn43t-0qvce4g9r7hLFm1AY7sFYPJLcfC0I47SPW8zRYOwuR0vFzunX-KF171Ps27qa1X-VipNE0iDMnPqtDpAcT397g6pdJSDryf64yhTC3xanO6-TT2uEepjDyNVAwO2VamACmnq_yGfimF7TAAGoRShnG7J4I6RIOxBNoTgxH_ZIy5wTJ81snPWFTXILtiIWqDptnoahQuGd_HHma4jPlikYPIN631MY9F9QBhhzlpnRVvaQDF7PneLIuYsf0iVOBxLpimy9l60Z_h2E9guMR3689QxGgwUIKjWnbDXirv7YWJQ",
"scope": "openid email profile",
"not-before-policy": 0.0
}
Updating user profile...
User [email protected] successfully logged in and updated profile
Successfully verified user
def register_users(users): for user in users: print("Registering user: " + user['username']) try: user_management_client.register_user(token=b64_encoded_custos_token, username=user['username'], first_name=user['first_name'], last_name=user['last_name'], password=user['password'], email=user['email'], is_temp_password=False) user_management_client.enable_user(token=b64_encoded_custos_token, username=user['username']) except Exception: print("User may be already exist") print("register_users method is defined")
register_users method is defined
users = [
{
'username': 'alice',
'first_name': 'Alice',
'last_name': 'Aron',
'password': '12345678',
'email': '[email protected]'
},
{
'username': 'audrey',
'first_name': 'Audrey',
'last_name': 'Aron',
'password': '12345678',
'email': '[email protected]'
},
{
'username': 'sophia',
'first_name': 'Sophia',
'last_name': 'Aron',
'password': '12345678',
'email': '[email protected]'
},
{
'username': 'abelota',
'first_name': 'Abelota',
'last_name': 'Aron',
'password': '12345678',
'email': '[email protected]'
},
{
'username': 'abigaill',
'first_name': 'Abigaill',
'last_name': 'Aron',
'password': '12345678',
'email': '[email protected]'
},
{
'username': 'adalee',
'first_name': 'Adalee',
'last_name': 'Aron',
'password': '12345678',
'email': '[email protected]'
}
]
try:
register_users(users)
except Exception:
print("please defined method register_users")
Registering user: alice
Registering user: audrey
Registering user: sophia
Registering user: abelota
Registering user: abigaill
Registering user: adalee
- Admin group
- Read only admin group
- Gateway users group
def create_groups(groups):
for group in groups:
try:
print("Creating group: " + group['name'])
grResponse = group_management_client.create_group(token=b64_encoded_custos_token,
name=group['name'],
description=group['description'],
owner_id=group['owner_id'])
resp = MessageToJson(grResponse)
print(resp)
respData = json.loads(resp)
print("Created group id of "+ group['name'] + ": " +respData['id'] )
created_groups[respData['name']] = respData['id']
except Exception as e:
print(e)
print("Group may be already created")
print("create_groups method is defined")
create_groups method is defined
groups = [
{
'name': 'Admin',
'description': 'Group for gateway read only admins',
'owner_id': admin_user_name
},
{
'name': 'Read Only Admin',
'description': 'Group for gateway admins',
'owner_id': admin_user_name
},
{
'name': 'Gateway User',
'description': 'Group for gateway users',
'owner_id': admin_user_name
}
]
try :
create_groups(groups)
except Exception as e:
print(e)
print("please defined method create_groups")
Creating group: Admin
{
"id": "admin_b8f763fe-1642-4875-aed0-a5fa422bc631",
"name": "Admin",
"createdTime": "1651534557000",
"lastModifiedTime": "1651534557000",
"description": "Group for gateway read only admins",
"ownerId": "[email protected]"
}
Created group id of Admin: admin_b8f763fe-1642-4875-aed0-a5fa422bc631
Creating group: Read Only Admin
{
"id": "read_only_admin_0afbcffd-7ca1-401b-aed8-fc8efce91fc3",
"name": "Read Only Admin",
"createdTime": "1651534557000",
"lastModifiedTime": "1651534557000",
"description": "Group for gateway admins",
"ownerId": "[email protected]"
}
Created group id of Read Only Admin: read_only_admin_0afbcffd-7ca1-401b-aed8-fc8efce91fc3
Creating group: Gateway User
{
"id": "gateway_user_1f02cbf8-e9ae-4f9d-b9c8-e11349212741",
"name": "Gateway User",
"createdTime": "1651534558000",
"lastModifiedTime": "1651534558000",
"description": "Group for gateway users",
"ownerId": "[email protected]"
}
Created group id of Gateway User: gateway_user_1f02cbf8-e9ae-4f9d-b9c8-e11349212741
- Admin : alice, audery
- Read only admin : sophia,abelota
- Gateway User : abgaill, adalee
def allocate_users_to_groups(user_group_mapping):
for usr_map in user_group_mapping:
try:
group_id = created_groups[usr_map['group_name']]
print("Assigning user " + usr_map['username'] + " to group " + usr_map['group_name'])
val =group_management_client.add_user_to_group(token=b64_encoded_custos_token,
username=usr_map['username'],
group_id=group_id,
membership_type='Member'
)
resp = MessageToJson(val)
print(resp)
except Exception as e:
print(e)
print("User allocation error")
print("allocate_users_to_groups method is defined")
allocate_users_to_groups method is defined
user_group_mapping = [
{
'group_name': 'Admin',
'username': 'alice'
},
{
'group_name': 'Admin',
'username': 'audrey'
},
{
'group_name': 'Read Only Admin',
'username': 'sophia'
},
{
'group_name': 'Read Only Admin',
'username': 'abelota'
},
{
'group_name': 'Gateway User',
'username': 'abigaill'
},
{
'group_name': 'Gateway User',
'username': 'adalee'
}
]
try:
allocate_users_to_groups(user_group_mapping)
except Exception:
print("please defined method allocate_users_to_groups")
Assigning user alice to group Admin { "status": true } Assigning user audrey to group Admin { "status": true } Assigning user sophia to group Read Only Admin { "status": true } Assigning user abelota to group Read Only Admin { "status": true } Assigning user abigaill to group Gateway User { "status": true } Assigning user adalee to group Gateway User { "status": true }
- Assign Admin group as a child of Read Only Admin group
def allocate_child_group_to_parent_group(gr_gr_mapping):
for gr_map in gr_gr_mapping:
try:
child_id = created_groups[gr_map['child_name']]
parent_id = created_groups[gr_map['parent_name']]
print("Assigning child group " + gr_map['child_name'] + " to parent group " + gr_map['parent_name'])
group_management_client.add_child_group(token=b64_encoded_custos_token,
parent_group_id=parent_id,
child_group_id=child_id)
except Exception:
print("Child group allocation error")
print("allocate_child_group_to_parent_group method is defined")
allocate_child_group_to_parent_group method is defined
child_gr_parent_gr_mapping = [
{
"child_name": 'Admin',
"parent_name": 'Read Only Admin'
}
]
try:
allocate_child_group_to_parent_group(child_gr_parent_gr_mapping)
except Exception:
print("please defined method allocate_child_group_to_parent_group")
Assigning child group Admin to parent group Read Only Admin
- WRITE
- READ
def create_permissions(permissions):
for perm in permissions:
print("Creating permission " + perm['id'])
try:
sharing_management_client.create_permission_type(token=b64_encoded_custos_token,
client_id=custos_settings.CUSTOS_CLIENT_ID,
id=perm['id'],
name=perm['name'],
description=perm['description'])
except Exception:
print("Permission may be already created")
print("create_permissions method is defined")
create_permissions method is defined
permissions = [
{
'id': 'READ',
'name': 'READ',
'description': 'Read permission'
},
{
'id': 'WRITE',
'name': 'WRITE',
'description': 'WRITE permission'
}
]
try :
create_permissions(permissions)
except Exception:
print("please defined method create_permissions")
Creating permission READ
Creating permission WRITE
Categorization of entities (digital objects) you want to share
- Project
- Experiment
def create_entity_types(entity_types):
for type in entity_types:
print("Creating entity types " + type['id'])
try:
sharing_management_client.create_entity_type(token=b64_encoded_custos_token,
client_id=custos_settings.CUSTOS_CLIENT_ID,
id=type['id'],
name=type['name'],
description=type['description'])
except Exception:
print("Entity type may be already created")
print("create_entity_types method is defined")
create_entity_types method is defined
entity_types = [
{
'id': 'PROJECT',
'name': 'PROJECT',
'description': 'PROJECT entity type'
},
{
'id': 'EXPERIMENT',
'name': 'EXPERIMENT',
'description': 'EXPERIMENT entity type'
}
]
try :
create_entity_types(entity_types)
except Exception:
print("please defined method create_entity_types")
Creating entity types PROJECT
Creating entity types EXPERIMENT