82 lines
2.2 KiB
Python
82 lines
2.2 KiB
Python
import requests
|
|
from models_vm import VmAccount
|
|
from models_dci import AuthUser
|
|
from settings import CLIENT_HOST, PLATFORM
|
|
|
|
|
|
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
|
|
|
|
def get_admin(self):
|
|
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")
|
|
)
|
|
)
|
|
for field in query_admin:
|
|
if field.roles[0] == '@admin':
|
|
admin = dict(
|
|
id=field.id,
|
|
email=field.email,
|
|
state=field.state,
|
|
role=field.roles[0]
|
|
)
|
|
break
|
|
return admin
|
|
|
|
def get_key(self, admin):
|
|
host = self.input_container
|
|
admin_id = admin['id']
|
|
url = 'http://{}:1500/auth/v4/user/{}/key'.format(host, admin_id)
|
|
headers = {"Internal-Auth": "on", "Accept": "application/json"}
|
|
req = requests.post(url, headers=headers, data={})
|
|
result = req.json()
|
|
return result
|
|
|
|
|
|
def main():
|
|
"""
|
|
app entrypoint
|
|
"""
|
|
access = Access(PLATFORM)
|
|
print("="*47)
|
|
print("\nGetting an admin account, please waiting....\n")
|
|
|
|
account = access.get_admin()
|
|
|
|
print("Account will be used:")
|
|
print(account, "\n")
|
|
print("Getting an access key, please waiting...\n")
|
|
|
|
key = access.get_key(account)
|
|
|
|
print("Key will be used:")
|
|
print(key, "\n")
|
|
|
|
access_link = 'https://{}/auth/key/{}'.format(CLIENT_HOST, key['key'])
|
|
|
|
print("Your access link: \n")
|
|
print(access_link)
|
|
print("ENJOY MY FRIEND! \n")
|
|
print("="*47)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|