-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenrollment_info.py
More file actions
142 lines (109 loc) · 4.65 KB
/
Copy pathenrollment_info.py
File metadata and controls
142 lines (109 loc) · 4.65 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python3
import json
import requests
import sys
import os
import argparse
from typing import Dict, List, Any
from akamai.edgegrid import EdgeGridAuth, EdgeRc
def get_contracts(session, hostname, account_switch_key=None):
base_url = f"https://{hostname}/papi/v1/contracts"
params = {}
if account_switch_key:
params["accountSwitchKey"] = account_switch_key
headers = {
"Accept": "application/json"
}
try:
response = session.get(
base_url,
params=params,
headers=headers
)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"Error fetching contracts: {e}")
sys.exit(1)
def get_enrollments(session, hostname, contract_id, account_switch_key=None):
base_url = f"https://{hostname}/cps/v2/enrollments"
params = {
"contractId": contract_id
}
if account_switch_key:
params["accountSwitchKey"] = account_switch_key
headers = {
"Accept": "application/json"
}
try:
response = session.get(
base_url,
params=params,
headers=headers
)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"Error fetching enrollments for contract {contract_id}: {e}")
return {"enrollments": []}
def extract_data(data):
result = []
if 'enrollments' not in data:
return result
for enrollment in data['enrollments']:
enrollment_data = {
'enrollment_id': enrollment.get('id', 'Unknown'),
'common_name': None,
'dns_names': None,
'disallowed_tls_versions': None
}
if 'csr' in enrollment and 'cn' in enrollment['csr']:
enrollment_data['common_name'] = enrollment['csr']['cn']
if ('networkConfiguration' in enrollment and
'dnsNameSettings' in enrollment['networkConfiguration'] and
'dnsNames' in enrollment['networkConfiguration']['dnsNameSettings']):
enrollment_data['dns_names'] = enrollment['networkConfiguration']['dnsNameSettings']['dnsNames']
if ('networkConfiguration' in enrollment and
'disallowedTlsVersions' in enrollment['networkConfiguration']):
enrollment_data['disallowed_tls_versions'] = enrollment['networkConfiguration']['disallowedTlsVersions']
result.append(enrollment_data)
return result
def setup_session(edgerc_file, edgerc_section):
edgerc_path = os.path.expanduser(edgerc_file)
edgerc = EdgeRc(edgerc_path)
section = edgerc_section
session = requests.Session()
session.auth = EdgeGridAuth(
client_token=edgerc.get(section, 'client_token'),
client_secret=edgerc.get(section, 'client_secret'),
access_token=edgerc.get(section, 'access_token')
)
hostname = edgerc.get(section, 'host')
return session, hostname
def main():
parser = argparse.ArgumentParser(description="Extract CPS enrollment data")
parser.add_argument("--account-switch-key", help="Account Switch Key (optional)")
parser.add_argument("--edgerc", default="~/.edgerc", help="Path to .edgerc file (default: ~/.edgerc)")
parser.add_argument("--section", default="default", help="Section in .edgerc file (default: default)")
args = parser.parse_args()
session, hostname = setup_session(args.edgerc, args.section)
all_enrollment_data = []
# Fetch all contracts
contracts_data = get_contracts(session, hostname, args.account_switch_key)
if 'contracts' in contracts_data and 'items' in contracts_data['contracts']:
for contract in contracts_data['contracts']['items']:
contract_id = contract.get('contractId')
if contract_id:
print(f"Fetching enrollments for contract: {contract_id}", file=sys.stderr)
data = get_enrollments(session, hostname, contract_id, args.account_switch_key)
extracted = extract_data(data)
if extracted:
all_enrollment_data.extend(extracted)
print(f"Found {len(extracted)} enrollments for contract {contract_id}", file=sys.stderr)
else:
print(f"No enrollments found for contract {contract_id}", file=sys.stderr)
else:
print("No contracts found or invalid response format", file=sys.stderr)
print(json.dumps(all_enrollment_data, indent=2))
if __name__ == "__main__":
main()