2022-09-21 01:16:09 +08:00
|
|
|
#! /usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
__author__ = 'MOIS3Y'
|
|
|
|
|
2022-09-20 19:54:32 +08:00
|
|
|
import requests
|
|
|
|
from models_vm import VmAccount
|
|
|
|
from models_dci import AuthUser
|
2022-09-20 20:46:17 +08:00
|
|
|
from settings import CLIENT_HOST, PLATFORM
|
2022-09-20 19:54:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Access(object):
|
|
|
|
"""
|
|
|
|
Allows you to get a link with an authorization key
|
|
|
|
For quick access to the web interface of the client platform.
|
|
|
|
The PLATFORM variable can be set as an environment variable.
|
|
|
|
Default PLATFORM='vm'
|
|
|
|
"""
|
|
|
|
def __init__(self, platform):
|
|
|
|
if platform == 'vm':
|
|
|
|
self.input_container = 'vm_box'
|
|
|
|
self.user_table = VmAccount
|
|
|
|
if platform == 'dci':
|
|
|
|
self.input_container = 'input'
|
|
|
|
self.user_table = AuthUser
|
2022-09-21 02:43:00 +08:00
|
|
|
self.platform = platform
|
2022-09-20 19:54:32 +08:00
|
|
|
|
|
|
|
def get_admin(self):
|
2022-09-25 14:27:32 +08:00
|
|
|
"""
|
|
|
|
The method gets the first 10 active database users
|
|
|
|
and finds the admin among them. I couldn't create
|
|
|
|
a request filtering by the roles field because
|
|
|
|
peewee doesn't understand json and can't make a selection
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
_dict_: admin data
|
|
|
|
"""
|
2022-09-20 19:54:32 +08:00
|
|
|
query_admin = (self.user_table.select(
|
|
|
|
self.user_table.id,
|
|
|
|
self.user_table.email,
|
|
|
|
self.user_table.state,
|
|
|
|
self.user_table.roles).where(
|
|
|
|
(self.user_table.id <= 10) &
|
|
|
|
(self.user_table.state == "active")
|
|
|
|
)
|
|
|
|
)
|
2022-09-25 14:27:32 +08:00
|
|
|
# !peewee doesn't work well with JSONfield MySQL
|
2022-09-20 19:54:32 +08:00
|
|
|
for field in query_admin:
|
|
|
|
if field.roles[0] == '@admin':
|
2022-09-20 22:34:59 +08:00
|
|
|
admin = dict(
|
|
|
|
id=field.id,
|
|
|
|
email=field.email,
|
|
|
|
state=field.state,
|
|
|
|
role=field.roles[0]
|
|
|
|
)
|
2022-09-20 19:54:32 +08:00
|
|
|
break
|
|
|
|
return admin
|
|
|
|
|
|
|
|
def get_key(self, admin):
|
2022-09-25 14:27:32 +08:00
|
|
|
"""
|
|
|
|
Makes a post request to the endpoint
|
|
|
|
passing the admin data and
|
|
|
|
returns the generated access key
|
|
|
|
|
|
|
|
Args:
|
|
|
|
admin (_dict_): _admin data_
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
_dict_: _access key_
|
|
|
|
"""
|
2022-09-20 19:54:32 +08:00
|
|
|
host = self.input_container
|
2022-09-25 14:49:44 +08:00
|
|
|
url = 'http://{}:1500/auth/v4/user/{}/key'.format(host, admin['id'])
|
2022-09-20 19:54:32 +08:00
|
|
|
headers = {"Internal-Auth": "on", "Accept": "application/json"}
|
|
|
|
req = requests.post(url, headers=headers, data={})
|
|
|
|
result = req.json()
|
|
|
|
return result
|
|
|
|
|
2022-09-25 14:27:32 +08:00
|
|
|
def make_access_link(self, key):
|
|
|
|
"""
|
|
|
|
Generates a clickable link which will be received in stdout
|
|
|
|
|
|
|
|
Args:
|
|
|
|
key (_dict_): _access key data_
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
_str_: link to access the platform
|
|
|
|
"""
|
|
|
|
key_type = 'key' # why for dcimgr key-4 ???
|
|
|
|
|
|
|
|
access_link = 'https://{}/auth/{}/{}'.format(
|
|
|
|
CLIENT_HOST, key_type, key['key']
|
|
|
|
)
|
|
|
|
if self.platform == 'dci':
|
|
|
|
access_link = 'https://{}/auth/{}/{}'.format(
|
|
|
|
CLIENT_HOST, key_type, key['key']
|
|
|
|
)
|
|
|
|
return access_link
|
|
|
|
|
2022-09-20 19:54:32 +08:00
|
|
|
|
2022-09-20 20:46:17 +08:00
|
|
|
def main():
|
|
|
|
"""
|
|
|
|
app entrypoint
|
|
|
|
"""
|
2022-09-20 19:54:32 +08:00
|
|
|
access = Access(PLATFORM)
|
2022-09-20 22:34:59 +08:00
|
|
|
print("="*47)
|
2022-09-20 22:41:21 +08:00
|
|
|
print("\nGetting an admin account, please waiting....\n")
|
2022-09-20 22:34:59 +08:00
|
|
|
|
2022-09-20 19:54:32 +08:00
|
|
|
account = access.get_admin()
|
2022-09-20 22:34:59 +08:00
|
|
|
|
2022-09-20 20:46:17 +08:00
|
|
|
print("Account will be used:")
|
2022-09-20 22:34:59 +08:00
|
|
|
print(account, "\n")
|
|
|
|
print("Getting an access key, please waiting...\n")
|
|
|
|
|
2022-09-20 19:54:32 +08:00
|
|
|
key = access.get_key(account)
|
2022-09-20 22:34:59 +08:00
|
|
|
|
2022-09-20 20:46:17 +08:00
|
|
|
print("Key will be used:")
|
2022-09-20 22:34:59 +08:00
|
|
|
print(key, "\n")
|
|
|
|
|
2022-09-25 14:27:32 +08:00
|
|
|
access_link = access.make_access_link(key)
|
2022-09-20 22:34:59 +08:00
|
|
|
|
2022-09-20 22:41:21 +08:00
|
|
|
print("Your access link: \n")
|
2022-09-20 22:45:18 +08:00
|
|
|
print(access_link, "\n")
|
2022-09-20 22:34:59 +08:00
|
|
|
print("ENJOY MY FRIEND! \n")
|
|
|
|
print("="*47)
|
2022-09-20 20:46:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|