Skip to content

Commit 5861c7c

Browse files
rtaycherdbanty
andauthored
feat: Add verify_ssl option to generated Client, allowing users to ignore ssl verification (#497). Thanks @rtaycher!
Co-authored-by: Dylan Anthony <[email protected]>
1 parent 5708074 commit 5861c7c

30 files changed

+72
-3
lines changed

end_to_end_tests/golden-record/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,26 @@ my_data: MyDataModel = await get_my_data_model.asyncio(client=client)
4141
response: Response[MyDataModel] = await get_my_data_model.asyncio_detailed(client=client)
4242
```
4343

44+
By default, when you're calling an HTTPS API it will attempt to verify that SSL is working correctly. Using certificate verification is highly recommended most of the time, but sometimes you may need to authenticate to a server (especially an internal server) using a custom certificate bundle.
45+
46+
```python
47+
client = AuthenticatedClient(
48+
base_url="https://internal_api.example.com",
49+
token="SuperSecretToken",
50+
verify_ssl="/path/to/certificate_bundle.pem",
51+
)
52+
```
53+
54+
You can also disable certificate validation altogether, but beware that **this is a security risk**.
55+
56+
```python
57+
client = AuthenticatedClient(
58+
base_url="https://internal_api.example.com",
59+
token="SuperSecretToken",
60+
verify_ssl=False
61+
)
62+
```
63+
4464
Things to know:
4565
1. Every path/method combo becomes a Python module with four functions:
4666
1. `sync`: Blocking request that returns parsed data (if successful) or `None`

end_to_end_tests/golden-record/my_test_api_client/api/default/get_common_parameters.py

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def _get_kwargs(
2727
"cookies": cookies,
2828
"timeout": client.get_timeout(),
2929
"params": params,
30+
"verify": client.verify_ssl,
3031
}
3132

3233

end_to_end_tests/golden-record/my_test_api_client/api/default/post_common_parameters.py

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def _get_kwargs(
2727
"cookies": cookies,
2828
"timeout": client.get_timeout(),
2929
"params": params,
30+
"verify": client.verify_ssl,
3031
}
3132

3233

end_to_end_tests/golden-record/my_test_api_client/api/location/get_location_query_optionality.py

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def _get_kwargs(
4848
"cookies": cookies,
4949
"timeout": client.get_timeout(),
5050
"params": params,
51+
"verify": client.verify_ssl,
5152
}
5253

5354

end_to_end_tests/golden-record/my_test_api_client/api/parameters/delete_common_parameters_overriding_param.py

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def _get_kwargs(
2828
"cookies": cookies,
2929
"timeout": client.get_timeout(),
3030
"params": params,
31+
"verify": client.verify_ssl,
3132
}
3233

3334

end_to_end_tests/golden-record/my_test_api_client/api/parameters/get_common_parameters_overriding_param.py

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def _get_kwargs(
2828
"cookies": cookies,
2929
"timeout": client.get_timeout(),
3030
"params": params,
31+
"verify": client.verify_ssl,
3132
}
3233

3334

end_to_end_tests/golden-record/my_test_api_client/api/parameters/get_same_name_multiple_locations_param.py

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def _get_kwargs(
3636
"cookies": cookies,
3737
"timeout": client.get_timeout(),
3838
"params": params,
39+
"verify": client.verify_ssl,
3940
}
4041

4142

end_to_end_tests/golden-record/my_test_api_client/api/parameters/multiple_path_parameters.py

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def _get_kwargs(
2626
"headers": headers,
2727
"cookies": cookies,
2828
"timeout": client.get_timeout(),
29+
"verify": client.verify_ssl,
2930
}
3031

3132

end_to_end_tests/golden-record/my_test_api_client/api/tag1/get_tag_with_number.py

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def _get_kwargs(
2020
"headers": headers,
2121
"cookies": cookies,
2222
"timeout": client.get_timeout(),
23+
"verify": client.verify_ssl,
2324
}
2425

2526

end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def _get_kwargs(
8080
"cookies": cookies,
8181
"timeout": client.get_timeout(),
8282
"params": params,
83+
"verify": client.verify_ssl,
8384
}
8485

8586

end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_booleans.py

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def _get_kwargs(
2020
"headers": headers,
2121
"cookies": cookies,
2222
"timeout": client.get_timeout(),
23+
"verify": client.verify_ssl,
2324
}
2425

2526

end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_floats.py

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def _get_kwargs(
2020
"headers": headers,
2121
"cookies": cookies,
2222
"timeout": client.get_timeout(),
23+
"verify": client.verify_ssl,
2324
}
2425

2526

end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_integers.py

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def _get_kwargs(
2020
"headers": headers,
2121
"cookies": cookies,
2222
"timeout": client.get_timeout(),
23+
"verify": client.verify_ssl,
2324
}
2425

2526

end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_strings.py

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def _get_kwargs(
2020
"headers": headers,
2121
"cookies": cookies,
2222
"timeout": client.get_timeout(),
23+
"verify": client.verify_ssl,
2324
}
2425

2526

end_to_end_tests/golden-record/my_test_api_client/api/tests/get_user_list.py

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def _get_kwargs(
4444
"cookies": cookies,
4545
"timeout": client.get_timeout(),
4646
"params": params,
47+
"verify": client.verify_ssl,
4748
}
4849

4950

end_to_end_tests/golden-record/my_test_api_client/api/tests/int_enum_tests_int_enum_post.py

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def _get_kwargs(
3131
"cookies": cookies,
3232
"timeout": client.get_timeout(),
3333
"params": params,
34+
"verify": client.verify_ssl,
3435
}
3536

3637

end_to_end_tests/golden-record/my_test_api_client/api/tests/json_body_tests_json_body_post.py

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def _get_kwargs(
2626
"cookies": cookies,
2727
"timeout": client.get_timeout(),
2828
"json": json_json_body,
29+
"verify": client.verify_ssl,
2930
}
3031

3132

end_to_end_tests/golden-record/my_test_api_client/api/tests/no_response_tests_no_response_get.py

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def _get_kwargs(
2020
"headers": headers,
2121
"cookies": cookies,
2222
"timeout": client.get_timeout(),
23+
"verify": client.verify_ssl,
2324
}
2425

2526

end_to_end_tests/golden-record/my_test_api_client/api/tests/octet_stream_tests_octet_stream_get.py

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def _get_kwargs(
2121
"headers": headers,
2222
"cookies": cookies,
2323
"timeout": client.get_timeout(),
24+
"verify": client.verify_ssl,
2425
}
2526

2627

end_to_end_tests/golden-record/my_test_api_client/api/tests/post_form_data.py

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def _get_kwargs(
2323
"cookies": cookies,
2424
"timeout": client.get_timeout(),
2525
"data": form_data.to_dict(),
26+
"verify": client.verify_ssl,
2627
}
2728

2829

end_to_end_tests/golden-record/my_test_api_client/api/tests/test_inline_objects.py

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def _get_kwargs(
2626
"cookies": cookies,
2727
"timeout": client.get_timeout(),
2828
"json": json_json_body,
29+
"verify": client.verify_ssl,
2930
}
3031

3132

end_to_end_tests/golden-record/my_test_api_client/api/tests/token_with_cookie_auth_token_with_cookie_get.py

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def _get_kwargs(
2323
"headers": headers,
2424
"cookies": cookies,
2525
"timeout": client.get_timeout(),
26+
"verify": client.verify_ssl,
2627
}
2728

2829

end_to_end_tests/golden-record/my_test_api_client/api/tests/unsupported_content_tests_unsupported_content_get.py

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def _get_kwargs(
2020
"headers": headers,
2121
"cookies": cookies,
2222
"timeout": client.get_timeout(),
23+
"verify": client.verify_ssl,
2324
}
2425

2526

end_to_end_tests/golden-record/my_test_api_client/api/tests/upload_file_tests_upload_post.py

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def _get_kwargs(
3030
"cookies": cookies,
3131
"timeout": client.get_timeout(),
3232
"files": multipart_multipart_data,
33+
"verify": client.verify_ssl,
3334
}
3435

3536

end_to_end_tests/golden-record/my_test_api_client/api/tests/upload_multiple_files_tests_upload_post.py

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def _get_kwargs(
3333
"cookies": cookies,
3434
"timeout": client.get_timeout(),
3535
"files": multipart_multipart_data,
36+
"verify": client.verify_ssl,
3637
}
3738

3839

end_to_end_tests/golden-record/my_test_api_client/api/true_/false_.py

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def _get_kwargs(
2727
"cookies": cookies,
2828
"timeout": client.get_timeout(),
2929
"params": params,
30+
"verify": client.verify_ssl,
3031
}
3132

3233

end_to_end_tests/golden-record/my_test_api_client/client.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import Dict
1+
import ssl
2+
from typing import Dict, Union
23

34
import attr
45

@@ -11,6 +12,7 @@ class Client:
1112
cookies: Dict[str, str] = attr.ib(factory=dict, kw_only=True)
1213
headers: Dict[str, str] = attr.ib(factory=dict, kw_only=True)
1314
timeout: float = attr.ib(5.0, kw_only=True)
15+
verify_ssl: Union[str, bool, ssl.SSLContext] = attr.ib(True, kw_only=True)
1416

1517
def get_headers(self) -> Dict[str, str]:
1618
"""Get headers to be used in all endpoints"""

openapi_python_client/templates/README.md.jinja

+20
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,26 @@ my_data: MyDataModel = await get_my_data_model.asyncio(client=client)
4141
response: Response[MyDataModel] = await get_my_data_model.asyncio_detailed(client=client)
4242
```
4343

44+
By default, when you're calling an HTTPS API it will attempt to verify that SSL is working correctly. Using certificate verification is highly recommended most of the time, but sometimes you may need to authenticate to a server (especially an internal server) using a custom certificate bundle.
45+
46+
```python
47+
client = AuthenticatedClient(
48+
base_url="https://internal_api.example.com",
49+
token="SuperSecretToken",
50+
verify_ssl="/path/to/certificate_bundle.pem",
51+
)
52+
```
53+
54+
You can also disable certificate validation altogether, but beware that **this is a security risk**.
55+
56+
```python
57+
client = AuthenticatedClient(
58+
base_url="https://internal_api.example.com",
59+
token="SuperSecretToken",
60+
verify_ssl=False
61+
)
62+
```
63+
4464
Things to know:
4565
1. Every path/method combo becomes a Python module with four functions:
4666
1. `sync`: Blocking request that returns parsed data (if successful) or `None`

openapi_python_client/templates/client.py.jinja

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from typing import Dict
2-
1+
import ssl
2+
from typing import Dict, Union
33
import attr
44

55
@attr.s(auto_attribs=True)
@@ -10,6 +10,7 @@ class Client:
1010
cookies: Dict[str, str] = attr.ib(factory=dict, kw_only=True)
1111
headers: Dict[str, str] = attr.ib(factory=dict, kw_only=True)
1212
timeout: float = attr.ib(5.0, kw_only=True)
13+
verify_ssl: Union[str, bool, ssl.SSLContext] = attr.ib(True, kw_only=True)
1314

1415
def get_headers(self) -> Dict[str, str]:
1516
""" Get headers to be used in all endpoints """

openapi_python_client/templates/endpoint_module.py.jinja

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def _get_kwargs(
5252
{% if endpoint.query_parameters %}
5353
"params": params,
5454
{% endif %}
55+
"verify": client.verify_ssl,
5556
}
5657

5758

0 commit comments

Comments
 (0)