Skip to content

Commit 8134184

Browse files
feat: Improve SSL debugging flow (#38)
* feat: Improve SSL debugging flow * formatting * Even more formatting * Windows quick fix solution that sometimes works * Further fixes from feedback * Update how-to-debug-ssl-communication-issues.md --------- Co-authored-by: bryance-flexcompute <[email protected]>
1 parent 34acb91 commit 8134184

File tree

1 file changed

+172
-0
lines changed

1 file changed

+172
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
---
2+
title: How to debug SSL communication issues?
3+
date: 2023-10-24 15:55:24
4+
enabled: true
5+
category: "Simulation Troubleshoot"
6+
---
7+
8+
9+
# How to debug SSL communication issues?
10+
11+
Users operating within corporate networks, through a VPN, or behind a proxy server may sometimes encounter SSL errors when running the `tidy3d` python client in their local machines. **Note you can always use the python client with [our cloud-hosted notebook server](https://tidy3d.simulation.cloud/).**
12+
13+
These errors can occur because the corporate network's security measures, intercepts, and re-encrypts HTTPS traffic.
14+
15+
A common error message looks like this:
16+
17+
```bash
18+
requests.exceptions.SSLError: HTTPSConnectionPool(host='tidy3d-api.simulation.cloud', port=443): Max retries exceeded with url: ... (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate ...')))
19+
```
20+
21+
Note that sometimes, SSL errors can also manifest as authentication errors, or communication timeout errors.
22+
23+
In this FAQ, we will overview some steps to help you debug and resolve this issue.
24+
25+
## Step 0: Windows quick fix
26+
Sometimes on Windows running the following will resolve the problem right away. Try this first:
27+
```bash
28+
pip install pip-system-certs
29+
```
30+
If this doesn't work, proceed with the rest of this guide.
31+
32+
## Step 1: The Recommended & Most Secure Solution
33+
34+
The most robust and secure solution is to have your IT department add the Tidy3D SSL certificate to your system's trust store. This allows your machine to verify our servers' identity correctly without disabling security features.
35+
36+
**Action**: Please ask your network administrator to whitelist the API endpoint `https://tidy3d-api.simulation.cloud` and install the following root certificate [https://github.com/flexcompute/tidy3d/blob/develop/tidy3d/web/api/cacert.pem](https://github.com/flexcompute/tidy3d/blob/develop/tidy3d/web/api/cacert.pem)
37+
38+
If these steps do not resolve your issue, please contact our support team and provide the logs from the commands you have tried.
39+
40+
41+
## Step 2: Verify Your Environment and Connectivity
42+
43+
Before changing any settings, let's make sure you can reach the Tidy3D servers.
44+
45+
**Test connectivity using `ping`**. This helps confirm if the issue is with SSL verification or basic network access. You should run these commands from your command line (Terminal, PowerShell, or the Anaconda Prompt).
46+
47+
* Test connection to the web server:
48+
49+
```bash
50+
○ → ping -c 3 tidy3d.simulation.cloud
51+
PING tidy3d.simulation.cloud (3.160.231.60) 56(84) bytes of data.
52+
64 bytes from server-3-160-231-60.mad53.r.cloudfront.net (3.160.231.60): icmp_seq=1 ttl=245 time=58.7 ms
53+
64 bytes from server-3-160-231-60.mad53.r.cloudfront.net (3.160.231.60): icmp_seq=2 ttl=245 time=77.9 ms
54+
64 bytes from server-3-160-231-60.mad53.r.cloudfront.net (3.160.231.60): icmp_seq=3 ttl=245 time=69.2 ms
55+
56+
--- tidy3d.simulation.cloud ping statistics ---
57+
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
58+
rtt min/avg/max/mdev = 58.661/68.594/77.930/7.877 ms
59+
```
60+
61+
* Test connection to the API endpoints:
62+
63+
```bash
64+
○ → curl -X GET https://tidy3d-api.simulation.cloud
65+
{
66+
"error" : "Unauthorized",
67+
"detail" : "Access is denied",
68+
"code" : "401",
69+
"httpStatus" : "401 UNAUTHORIZED",
70+
"warning" : ""
71+
}
72+
```
73+
74+
**Expected output:** A `401 Unauthorized` error message. This is good news; it means you can reach our API server, and the problem is likely with authentication or SSL certificate verification.
75+
76+
Note that many networks can block ping messages so this is not a foolproof check.
77+
78+
## Step 3: Disable SSL Verification (Quick Workaround)
79+
80+
**As a temporary solution**, you can instruct `tidy3d` to bypass SSL certificate verification. This is not ideal for security but is useful for confirming the source of the problem.
81+
82+
This is done by setting the `TIDY3D_SSL_VERIFY` environment variable to `false`.
83+
84+
#### Setting the Environment Variable:
85+
86+
* **On Windows (in Command Prompt)**:
87+
88+
```bash
89+
set TIDY3D_SSL_VERIFY=false
90+
```
91+
92+
To set it permanently, use `setx TIDY3D_SSL_VERIFY false`.
93+
94+
* **On macOS or Linux (in a bash terminal)**:
95+
96+
```bash
97+
export TIDY3D_SSL_VERIFY="false"
98+
```
99+
100+
Note thet the user could run into unexpected server communication issues by disabling SSL. This is not recommended.
101+
102+
#### Verifying the Environment Variable:
103+
104+
**It is essential to ensure the variable is set correctly within the environment where you run your script.**
105+
106+
* **In your terminal**, run the following command.
107+
108+
* Windows: `echo %TIDY3D_SSL_VERIFY%`
109+
* macOS/Linux: `echo $TIDY3D_SSL_VERIFY`
110+
* The output should be `false`.
111+
112+
* **Inside your Python script**, add these lines to the top to see what value the script is reading:
113+
114+
```python
115+
import os
116+
print(f"TIDY3D_SSL_VERIFY is set to: {os.getenv('TIDY3D_SSL_VERIFY')}")
117+
```
118+
119+
When you run your script, you should see the confirmation printed. If you see `None` or an empty string, the variable was not set correctly in your current session.
120+
121+
## Step 4: Run a Diagnostic Script
122+
123+
If the issue persists, running a minimal script can help isolate the problem to the `requests` library, which `tidy3d` uses for communication.
124+
125+
1. Save the following code as a Python file (e.g., `test_connection.py`).
126+
2. Set the `TIDY3D_API_KEY` and `TIDY3D_SSL_VERIFY` environment variables in your terminal as shown in Step 2.
127+
3. Run the script with `python test_connection.py`.
128+
129+
130+
```python
131+
# test_connection.py
132+
import os
133+
import requests
134+
from tidy3d.web.core.constants import HEADER_APIKEY
135+
from tidy3d.web.core.environment import Env
136+
137+
def auth(req):
138+
"""Adds the API key to the request header."""
139+
# Ensure TIDY3D_API_KEY is set in your environment
140+
req.headers[HEADER_APIKEY] = os.getenv("TIDY3D_API_KEY")
141+
return req
142+
143+
# Let tidy3d's environment configuration read the SSL variable
144+
# It reads from the TIDY3D_SSL_VERIFY environment variable.
145+
ssl_verify_setting = Env.current.ssl_verify
146+
147+
print(f"API Endpoint: {Env.current.web_api_endpoint}")
148+
print(f"SSL Verification Enabled: {ssl_verify_setting}")
149+
150+
if not ssl_verify_setting:
151+
# Suppress the warning that will be printed for unverified requests
152+
requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning)
153+
print("InsecureRequestWarning suppressed.")
154+
155+
try:
156+
req = requests.Request("GET", f"{Env.current.web_api_endpoint}/apikey")
157+
auth(req)
158+
resp = requests.get(req.url, headers=req.headers, verify=ssl_verify_setting)
159+
160+
print(f"Response: {resp}")
161+
print(f"Status Code: {resp.status_code}")
162+
print(f"Response Content: {resp.content}")
163+
164+
except requests.exceptions.SSLError as e:
165+
print(f"\nCaught an SSLError. This is the core issue.\nDetails: {e}")
166+
167+
except Exception as e:
168+
print(f"\nAn unexpected error occurred: {e}")
169+
170+
```
171+
172+
With `TIDY3D_SSL_VERIFY` set to `false` and `TIDY3D_API_KEY` set correctly, the expected output is a `status code: 200` and content containing your API key. You will also see an `InsecureRequestWarning` if you don't suppress it.

0 commit comments

Comments
 (0)