1 | import requests |
2 | import json |
3 |
|
4 |
|
5 | class TokenParams: |
6 | def __init__(self, host, username, password, client_id, realm): |
7 | self.host = host |
8 | self.username = username |
9 | self.password = password |
10 | self.client_id = client_id |
11 | self.realm = realm |
12 |
|
13 |
|
14 | class TokenResponse: |
15 | def __init__(self, access_token=None, **kwargs): |
16 | self.access_token = access_token |
17 | self.__dict__.update(kwargs) |
18 |
|
19 |
|
20 | def get_access_token(params: TokenParams) -> str: |
21 | url = f"https://{params.host}/auth/realms/{params.realm}/protocol/openid-connect/token" |
22 |
|
23 | data = { |
24 | "grant_type": "password", |
25 | "username": params.username, |
26 | "password": params.password, |
27 | "client_id": params.client_id, |
28 | } |
29 |
|
30 | headers = { |
31 | "Accept": "application/json", |
32 | "Content-Type": "application/x-www-form-urlencoded", |
33 | "cache-control": "no-cache", |
34 | } |
35 |
|
36 | response = requests.post(url, data=data, headers=headers) |
37 |
|
38 | if response.status_code != 200: |
39 | raise Exception(f"HTTP error! status: {response.status_code}") |
40 |
|
41 | data = response.json() |
42 | token_response = TokenResponse(**data) |
43 | token = token_response.access_token |
44 | if not token: |
45 | raise Exception("Could not get access token") |
46 | return token |
47 |
|
48 |
|
49 | def main( |
50 | domain_id="c4503929-0737-4875-8b46-dfbea2512b3f", |
51 | user="sandboxuser", |
52 | passw=None, |
53 | oidc_client="veo-sandbox", |
54 | controls_json: str = "", |
55 | realm: str = "verinice-sandbox", |
56 | api_host: str = "api.sandbox.verinice.com", |
57 | ): |
58 | token = get_access_token( |
59 | TokenParams( |
60 | host="auth.verinice.com", |
61 | username=user, |
62 | password=passw, |
63 | client_id=oidc_client, |
64 | realm=realm, |
65 | ) |
66 | ) |
67 |
|
68 | controls = controls_json.get("items", []) |
69 | print(controls) |
70 |
|
71 | results = [] |
72 | for control in controls: |
73 | control_id = control["id"] |
74 | print(control_id) |
75 | res = requests.get( |
76 | f"https://{api_host}/veo/domains/{domain_id}/controls/{control_id}/control-implementations", |
77 | headers={ |
78 | "Content-Type": "application/json", |
79 | "Authorization": f"Bearer {token}", |
80 | }, |
81 | ) |
82 | data = res.json() |
83 | results.append(data) |
84 |
|
85 | return results |
86 |
|