raannakasturi commited on
Commit
12b0c6f
·
1 Parent(s): 4d46788

Refactor get_zero_ssl_eab function to handle API key errors and exceptions

Browse files
Files changed (2) hide show
  1. getZeroSSLEAB.py +21 -9
  2. main.py +7 -6
getZeroSSLEAB.py CHANGED
@@ -4,14 +4,26 @@ from dotenv import load_dotenv
4
 
5
  def gen_zero_ssl_eab():
6
  load_dotenv()
7
- apikey = os.getenv("ZEROSSLAPI")
8
- url = "https://api.zerossl.com/acme/eab-credentials"
 
 
 
 
9
  headers = {'Content-Type': 'application/json'}
10
- resp = requests.post(url, params={'access_key': apikey}, headers=headers)
11
- if resp.json()['success'] == False:
12
- print("Error: ", resp.json()['error'])
 
 
 
 
 
 
 
 
 
 
 
 
13
  return "Error", "Error"
14
- else:
15
- kid = resp.json()['eab_kid']
16
- hmac = resp.json()['eab_hmac_key']
17
- return kid, hmac
 
4
 
5
  def gen_zero_ssl_eab():
6
  load_dotenv()
7
+ apikey = os.getenv("ZEROSSL_API")
8
+ if not apikey:
9
+ print("Error: API key not found. Ensure it's set in your environment variables.")
10
+ return "Error", "Error"
11
+
12
+ url = f"https://api.zerossl.com/acme/eab-credentials?access_key={apikey}"
13
  headers = {'Content-Type': 'application/json'}
14
+
15
+ try:
16
+ resp = requests.post(url, headers=headers)
17
+ resp_json = resp.json()
18
+
19
+ if resp.status_code == 200 and resp_json.get('success') == 1:
20
+ kid = resp_json.get('eab_kid')
21
+ hmac = resp_json.get('eab_hmac_key')
22
+ print(f"Kid: {kid}\nHMAC: {hmac}")
23
+ return kid, hmac
24
+ else:
25
+ print(f"Error: {resp_json.get('error', 'Unknown error')}")
26
+ return "Error", "Error"
27
+ except requests.exceptions.RequestException as e:
28
+ print(f"Request failed: {e}")
29
  return "Error", "Error"
 
 
 
 
main.py CHANGED
@@ -100,7 +100,6 @@ def main(i_domains, wildcard, email, ca_server, key_type, key_size=None, key_cur
100
  print("size", key_size)
101
  print("curve", key_curve)
102
  print("All data filled")
103
-
104
  domains = get_domains(i_domains)
105
  exchange = extract_subdomains(domains=domains)
106
  if wildcard:
@@ -118,8 +117,7 @@ def main(i_domains, wildcard, email, ca_server, key_type, key_size=None, key_cur
118
  account = new_account(pgk_client, email, kid=kid, hmac=hmac)
119
  except Exception as e:
120
  print(f"Account Error: {e}")
121
- if not account:
122
- return handle_error("Cannot generate your SSL. Too many requests for this domain.")
123
  private_key, csr = gen_pvt_csr(domains=domains, email=email, key_type=key_type, key_curve=key_curve, key_size=key_size)
124
  verification_tokens, challs, order = get_tokens(pgk_client, csr, ca_server_url)
125
  try:
@@ -165,15 +163,15 @@ def main(i_domains, wildcard, email, ca_server, key_type, key_size=None, key_cur
165
  return private_key, csr, cert, email_status
166
 
167
  if __name__ == "__main__":
168
- DOMAINS = 'raannakasturi.eu.org'
169
  ca_server = "Google" #Let's Encrypt (Testing), Let's Encrypt, Google (Testing), Google, Buypass (Testing), Buypass, ZeroSSL, SSL.com
170
  EMAIL = "[email protected]"
171
  key_type = "ecc"
172
  key_curve = "ec384"
173
- key_size = None
174
  KID = None
175
  HMAC = None
176
- private_key, csr, cert = main(i_domains=DOMAINS, wildcard=True, email=EMAIL, ca_server=ca_server, key_type=key_type, key_size=key_size,key_curve=key_curve, kid=KID, hmac=HMAC)
177
  print("Private Key:")
178
  print(private_key)
179
  print()
@@ -182,3 +180,6 @@ if __name__ == "__main__":
182
  print()
183
  print("Certificate:")
184
  print(cert)
 
 
 
 
100
  print("size", key_size)
101
  print("curve", key_curve)
102
  print("All data filled")
 
103
  domains = get_domains(i_domains)
104
  exchange = extract_subdomains(domains=domains)
105
  if wildcard:
 
117
  account = new_account(pgk_client, email, kid=kid, hmac=hmac)
118
  except Exception as e:
119
  print(f"Account Error: {e}")
120
+ return handle_error(e)
 
121
  private_key, csr = gen_pvt_csr(domains=domains, email=email, key_type=key_type, key_curve=key_curve, key_size=key_size)
122
  verification_tokens, challs, order = get_tokens(pgk_client, csr, ca_server_url)
123
  try:
 
163
  return private_key, csr, cert, email_status
164
 
165
  if __name__ == "__main__":
166
+ DOMAINS = 'thenayankasturi.eu.org'
167
  ca_server = "Google" #Let's Encrypt (Testing), Let's Encrypt, Google (Testing), Google, Buypass (Testing), Buypass, ZeroSSL, SSL.com
168
  EMAIL = "[email protected]"
169
  key_type = "ecc"
170
  key_curve = "ec384"
171
+ key_size = "4096"
172
  KID = None
173
  HMAC = None
174
+ private_key, csr, cert, email_status = main(i_domains=DOMAINS, wildcard=True, email=EMAIL, ca_server=ca_server, key_type=key_type, key_size=key_size,key_curve=key_curve, kid=KID, hmac=HMAC)
175
  print("Private Key:")
176
  print(private_key)
177
  print()
 
180
  print()
181
  print("Certificate:")
182
  print(cert)
183
+ print()
184
+ print(email_status)
185
+ print()