Skip to content

feat: Add verify_ssl to generated Client, allowing users of generated libraries to ignore ssl verification #497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions end_to_end_tests/golden-record/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ my_data: MyDataModel = await get_my_data_model.asyncio(client=client)
response: Response[MyDataModel] = await get_my_data_model.asyncio_detailed(client=client)
```

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.

```python
client = AuthenticatedClient(
base_url="https://internal_api.example.com",
token="SuperSecretToken",
verify_ssl="/path/to/certificate_bundle.pem",
)
```

You can also disable certificate validation altogether, but beware that **this is a security risk**.

```python
client = AuthenticatedClient(
base_url="https://internal_api.example.com",
token="SuperSecretToken",
verify_ssl=False
)
```

Things to know:
1. Every path/method combo becomes a Python module with four functions:
1. `sync`: Blocking request that returns parsed data (if successful) or `None`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def _get_kwargs(
"cookies": cookies,
"timeout": client.get_timeout(),
"params": params,
"verify": client.verify_ssl,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def _get_kwargs(
"cookies": cookies,
"timeout": client.get_timeout(),
"params": params,
"verify": client.verify_ssl,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def _get_kwargs(
"cookies": cookies,
"timeout": client.get_timeout(),
"params": params,
"verify": client.verify_ssl,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def _get_kwargs(
"cookies": cookies,
"timeout": client.get_timeout(),
"params": params,
"verify": client.verify_ssl,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def _get_kwargs(
"cookies": cookies,
"timeout": client.get_timeout(),
"params": params,
"verify": client.verify_ssl,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def _get_kwargs(
"cookies": cookies,
"timeout": client.get_timeout(),
"params": params,
"verify": client.verify_ssl,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def _get_kwargs(
"headers": headers,
"cookies": cookies,
"timeout": client.get_timeout(),
"verify": client.verify_ssl,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def _get_kwargs(
"headers": headers,
"cookies": cookies,
"timeout": client.get_timeout(),
"verify": client.verify_ssl,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def _get_kwargs(
"cookies": cookies,
"timeout": client.get_timeout(),
"params": params,
"verify": client.verify_ssl,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def _get_kwargs(
"headers": headers,
"cookies": cookies,
"timeout": client.get_timeout(),
"verify": client.verify_ssl,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def _get_kwargs(
"headers": headers,
"cookies": cookies,
"timeout": client.get_timeout(),
"verify": client.verify_ssl,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def _get_kwargs(
"headers": headers,
"cookies": cookies,
"timeout": client.get_timeout(),
"verify": client.verify_ssl,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def _get_kwargs(
"headers": headers,
"cookies": cookies,
"timeout": client.get_timeout(),
"verify": client.verify_ssl,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def _get_kwargs(
"cookies": cookies,
"timeout": client.get_timeout(),
"params": params,
"verify": client.verify_ssl,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def _get_kwargs(
"cookies": cookies,
"timeout": client.get_timeout(),
"params": params,
"verify": client.verify_ssl,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def _get_kwargs(
"cookies": cookies,
"timeout": client.get_timeout(),
"json": json_json_body,
"verify": client.verify_ssl,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def _get_kwargs(
"headers": headers,
"cookies": cookies,
"timeout": client.get_timeout(),
"verify": client.verify_ssl,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def _get_kwargs(
"headers": headers,
"cookies": cookies,
"timeout": client.get_timeout(),
"verify": client.verify_ssl,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def _get_kwargs(
"cookies": cookies,
"timeout": client.get_timeout(),
"data": form_data.to_dict(),
"verify": client.verify_ssl,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def _get_kwargs(
"cookies": cookies,
"timeout": client.get_timeout(),
"json": json_json_body,
"verify": client.verify_ssl,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def _get_kwargs(
"headers": headers,
"cookies": cookies,
"timeout": client.get_timeout(),
"verify": client.verify_ssl,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def _get_kwargs(
"headers": headers,
"cookies": cookies,
"timeout": client.get_timeout(),
"verify": client.verify_ssl,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def _get_kwargs(
"cookies": cookies,
"timeout": client.get_timeout(),
"files": multipart_multipart_data,
"verify": client.verify_ssl,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def _get_kwargs(
"cookies": cookies,
"timeout": client.get_timeout(),
"files": multipart_multipart_data,
"verify": client.verify_ssl,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def _get_kwargs(
"cookies": cookies,
"timeout": client.get_timeout(),
"params": params,
"verify": client.verify_ssl,
}


Expand Down
4 changes: 3 additions & 1 deletion end_to_end_tests/golden-record/my_test_api_client/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Dict
import ssl
from typing import Dict, Union

import attr

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

def get_headers(self) -> Dict[str, str]:
"""Get headers to be used in all endpoints"""
Expand Down
20 changes: 20 additions & 0 deletions openapi_python_client/templates/README.md.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ my_data: MyDataModel = await get_my_data_model.asyncio(client=client)
response: Response[MyDataModel] = await get_my_data_model.asyncio_detailed(client=client)
```

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.

```python
client = AuthenticatedClient(
base_url="https://internal_api.example.com",
token="SuperSecretToken",
verify_ssl="/path/to/certificate_bundle.pem",
)
```

You can also disable certificate validation altogether, but beware that **this is a security risk**.

```python
client = AuthenticatedClient(
base_url="https://internal_api.example.com",
token="SuperSecretToken",
verify_ssl=False
)
```

Things to know:
1. Every path/method combo becomes a Python module with four functions:
1. `sync`: Blocking request that returns parsed data (if successful) or `None`
Expand Down
5 changes: 3 additions & 2 deletions openapi_python_client/templates/client.py.jinja
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Dict

import ssl
from typing import Dict, Union
import attr

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

def get_headers(self) -> Dict[str, str]:
""" Get headers to be used in all endpoints """
Expand Down
1 change: 1 addition & 0 deletions openapi_python_client/templates/endpoint_module.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def _get_kwargs(
{% if endpoint.query_parameters %}
"params": params,
{% endif %}
"verify": client.verify_ssl,
}


Expand Down