-
Notifications
You must be signed in to change notification settings - Fork 44
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
ssl support #712
Open
zrvku2000
wants to merge
23
commits into
aio-libs:master
Choose a base branch
from
zrvku2000:ssl_support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+365
−37
Open
ssl support #712
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
1eb12ed
add ssl support
zrvku2000 cfd5241
tests revision
zrvku2000 9f26d69
readme revision
zrvku2000 adfefdf
amendment
zrvku2000 d3cbee4
linter and test failures correction
zrvku2000 f90bdad
livereload.js html src protocol bug, test_create_app_wrong_name bug, …
zrvku2000 fd8e41b
minor changes
zrvku2000 74891d3
rename to test_start_runserver_ssl
zrvku2000 ffba946
linter errs correction
zrvku2000 c619148
ssl-context-factory config test
zrvku2000 d8260de
Merge branch 'master' into ssl_support
Dreamsorcerer 8fb1e39
Update requirements.txt
Dreamsorcerer 60ed28b
ssl config test update
zrvku2000 63a268a
Merge branch 'ssl_support' of https://github.com/zrvku2000/aiohttp-de…
zrvku2000 e34c7ad
aux server back to http
zrvku2000 be6a637
livereload with ssl correction, test_runserver_with_ssl correction
zrvku2000 1a65338
tests correction
zrvku2000 d546a21
linter corrections
zrvku2000 0aad504
test_runserver_with_ssl correction
zrvku2000 7addb68
moved ssl tests to test_runserver_main.py
zrvku2000 39dc597
some platform dependent fork hack
zrvku2000 ebd2870
test_start_runserver_ssl amendment
zrvku2000 214e6a6
test_runserver_main.py linter error correction
zrvku2000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,11 @@ | |
import sys | ||
from importlib import import_module | ||
from pathlib import Path | ||
from typing import Awaitable, Callable, Optional, Union | ||
from typing import Awaitable, Callable, Optional, Union, Literal | ||
from types import ModuleType | ||
|
||
from aiohttp import web | ||
from ssl import SSLContext, create_default_context as create_default_ssl_context | ||
|
||
import __main__ | ||
from ..exceptions import AiohttpDevConfigError as AdevConfigError | ||
|
@@ -26,6 +28,8 @@ | |
'create_app', | ||
] | ||
|
||
DEFAULT_PORT = 8000 | ||
|
||
INFER_HOST = '<inference>' | ||
|
||
|
||
|
@@ -43,9 +47,11 @@ | |
app_factory_name: Optional[str] = None, | ||
host: str = INFER_HOST, | ||
bind_address: str = "localhost", | ||
main_port: int = 8000, | ||
main_port: Optional[int] = None, | ||
aux_port: Optional[int] = None, | ||
browser_cache: bool = False): | ||
browser_cache: bool = False, | ||
ssl_context_factory_name: Optional[str] = None, | ||
ssl_rootcert_file_path: Optional[str] = None): | ||
if root_path: | ||
self.root_path = Path(root_path).resolve() | ||
logger.debug('Root path specified: %s', self.root_path) | ||
|
@@ -83,15 +89,39 @@ | |
self.host = bind_address | ||
|
||
self.bind_address = bind_address | ||
if main_port is None: | ||
main_port = DEFAULT_PORT if ssl_context_factory_name is None else DEFAULT_PORT + 443 | ||
self.main_port = main_port | ||
self.aux_port = aux_port or (main_port + 1) | ||
if aux_port is None: | ||
aux_port = main_port + 1 if ssl_context_factory_name is None else DEFAULT_PORT + 1 | ||
self.aux_port = aux_port | ||
self.browser_cache = browser_cache | ||
self.ssl_context_factory_name = ssl_context_factory_name | ||
self.ssl_rootcert_file_path = ssl_rootcert_file_path | ||
logger.debug('config loaded:\n%s', self) | ||
|
||
@property | ||
def protocol(self) -> Literal["http", "https"]: | ||
return "http" if self.ssl_context_factory_name is None else "https" | ||
|
||
@property | ||
def static_path_str(self) -> Optional[str]: | ||
return str(self.static_path) if self.static_path else None | ||
|
||
@property | ||
def client_ssl_context(self) -> Union[SSLContext, None]: | ||
client_ssl_context = None | ||
if self.protocol == 'https': | ||
client_ssl_context = create_default_ssl_context() | ||
if self.ssl_rootcert_file_path: | ||
try: | ||
client_ssl_context.load_verify_locations(self.ssl_rootcert_file_path) | ||
except FileNotFoundError as e: | ||
raise AdevConfigError('{}: {}'.format(e.strerror, self.ssl_rootcert_file_path)) | ||
except Exception: | ||
raise AdevConfigError('invalid root cert file: {}'.format(self.ssl_rootcert_file_path)) | ||
Comment on lines
+116
to
+122
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it difficult to create a test for this code? Seems to be the only missing coverage now. |
||
return client_ssl_context | ||
|
||
def _find_app_path(self, app_path: str) -> Path: | ||
# for backwards compatibility try this first | ||
path = (self.root_path / app_path).resolve() | ||
|
@@ -136,15 +166,14 @@ | |
raise AdevConfigError('{} is not a directory'.format(path)) | ||
return path | ||
|
||
def import_app_factory(self) -> AppFactory: | ||
"""Import and return attribute/class from a python module. | ||
def import_module(self) -> ModuleType: | ||
"""Import and return python module. | ||
|
||
Raises: | ||
AdevConfigError - If the import failed. | ||
""" | ||
rel_py_file = self.py_file.relative_to(self.python_path) | ||
module_path = '.'.join(rel_py_file.with_suffix('').parts) | ||
|
||
sys.path.insert(0, str(self.python_path)) | ||
module = import_module(module_path) | ||
# Rewrite the package name, so it will appear the same as running the app. | ||
|
@@ -153,6 +182,16 @@ | |
|
||
logger.debug('successfully loaded "%s" from "%s"', module_path, self.python_path) | ||
|
||
self.watch_path = self.watch_path or Path(module.__file__ or ".").parent | ||
return module | ||
|
||
def get_app_factory(self, module: ModuleType) -> AppFactory: | ||
"""Return attribute/class from a python module. | ||
|
||
Raises: | ||
AdevConfigError - If the import failed. | ||
""" | ||
|
||
if self.app_factory_name is None: | ||
try: | ||
self.app_factory_name = next(an for an in APP_FACTORY_NAMES if hasattr(module, an)) | ||
|
@@ -179,9 +218,24 @@ | |
raise AdevConfigError("'{}.{}' should not have required arguments.".format( | ||
self.py_file.name, self.app_factory_name)) | ||
|
||
self.watch_path = self.watch_path or Path(module.__file__ or ".").parent | ||
return attr # type: ignore[no-any-return] | ||
|
||
def get_ssl_context(self, module: ModuleType) -> Union[SSLContext, None]: | ||
if self.ssl_context_factory_name is None: | ||
return None | ||
else: | ||
try: | ||
attr = getattr(module, self.ssl_context_factory_name) | ||
except AttributeError: | ||
raise AdevConfigError("Module '{}' does not define a '{}' attribute/class".format( | ||
self.py_file.name, self.ssl_context_factory_name)) | ||
ssl_context = attr() | ||
if isinstance(ssl_context, SSLContext): | ||
return ssl_context | ||
else: | ||
raise AdevConfigError("ssl-context-factory '{}' in module '{}' didn't return valid SSLContext".format( | ||
self.ssl_context_factory_name, self.py_file.name)) | ||
|
||
async def load_app(self, app_factory: AppFactory) -> web.Application: | ||
if isinstance(app_factory, web.Application): | ||
return app_factory | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
-----BEGIN CERTIFICATE----- | ||
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 | ||
-----END CERTIFICATE----- |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also wonder about catching arbitrary exceptions. I'd assume the only other errors we should expect here are ssl.SSLError exceptions.