-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.py
93 lines (68 loc) · 2.62 KB
/
register.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import random
import string
import requests
from dotenv import load_dotenv
from pydantic import BaseModel
from requests.auth import HTTPBasicAuth
class ContentSelector(BaseModel):
name: str
description: str
expression: str
class Role(BaseModel):
id: str
name: str
description: str
privileges: list[str]
roles: list[str]
class User(BaseModel):
userId: str
firstName: str
lastName: str
emailAddress: str
password: str
status: str = "active"
roles: list[str]
class RepositoryContentSelector(BaseModel):
name: str
description: str
actions: list[str]
format: str = None
repository: str
contentSelector: str
load_dotenv()
base_url = os.environ["nexus_url"] + "/service/rest/"
base_roles = os.environ["base_roles"].split(",")
domains = input("Please enter the required group ids tld.domain.sub space seperated:\n").split(" ")
base_domain = domains[0]
description = ", ".join(domains)
firstName = input("First name:\n")
lastName = input("Last Name:\n")
mail = input("Mail:\n") or base_domain + "@eldonexus.de"
session = requests.Session()
session.auth = HTTPBasicAuth(os.environ['nexus_user'], os.environ['nexus_password'])
def post(url: str, data: BaseModel):
response = session.post(f"{base_url}{url}", json=data.dict(),
headers={"Content-Type": "application/json",
"accept": "application/json"})
response.raise_for_status()
# Create selectors
select = " or ".join([f"path =^ \"/{e.replace('.', '/')}\"" for e in domains])
select = f'format == "maven2" and ({select})'
print("Creating selector")
post("v1/security/content-selectors", ContentSelector(name=base_domain, description=description, expression=select))
privilege = RepositoryContentSelector(name=base_domain, description=description, actions=["ALL"], repository="*",
contentSelector=base_domain, format="maven2")
print("Creating content selector privilege")
post("v1/security/privileges/repository-content-selector", privilege)
print("Creating role")
post("v1/security/roles",
Role(id=base_domain, name=base_domain, description=description, privileges=[base_domain], roles=base_roles))
user = User(userId=base_domain, firstName=firstName, lastName=lastName, emailAddress=mail, status="active",
password=''.join(random.choice(string.ascii_lowercase) for i in range(20)), roles=[base_domain])
print("Creating user")
post("v1/security/users", user)
print("User created.")
print(f"Id: {user.userId}")
print(f"Password: {user.password}")
print(f"Active domains: {', '.join(domains)}")