-
Notifications
You must be signed in to change notification settings - Fork 11
Security for WebSockets #77
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
Draft
dmgav
wants to merge
5
commits into
bluesky:main
Choose a base branch
from
dmgav:websockets-sec
base: main
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.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c140d18
ENH: security for websockets
dmgav a96ed4c
TST: unit tests for sockets with authentication
dmgav ab7a13f
TST: unit tests for authenticated websockets
dmgav 023b08d
TST: additional test cases for websocket authentication
dmgav 2c138d0
ENH: add 'user:apikey' scopes to all default user groups
dmgav 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,175 @@ | ||
| import json | ||
| import pprint | ||
| import threading | ||
| import time as ttime | ||
|
|
||
| import pytest | ||
| from bluesky_queueserver.manager.tests.common import re_manager, re_manager_cmd # noqa F401 | ||
| from websockets.sync.client import connect | ||
|
|
||
| from .conftest import fastapi_server_fs # noqa: F401 | ||
| from .conftest import ( | ||
| SERVER_ADDRESS, | ||
| SERVER_PORT, | ||
| request_to_json, | ||
| setup_server_with_config_file, | ||
| wait_for_environment_to_be_closed, | ||
| wait_for_environment_to_be_created, | ||
| ) | ||
|
|
||
| config_toy_test = """ | ||
| authentication: | ||
| allow_anonymous_access: True | ||
| providers: | ||
| - provider: toy | ||
| authenticator: bluesky_httpserver.authenticators:DictionaryAuthenticator | ||
| args: | ||
| users_to_passwords: | ||
| bob: bob_password | ||
| alice: alice_password | ||
| cara: cara_password | ||
| tom: tom_password | ||
| api_access: | ||
| policy: bluesky_httpserver.authorization:DictionaryAPIAccessControl | ||
| args: | ||
| users: | ||
| bob: | ||
| roles: | ||
| - admin | ||
| - expert | ||
| alice: | ||
| roles: advanced | ||
| tom: | ||
| roles: user | ||
| """ | ||
|
|
||
|
|
||
| class _ReceiveSystemInfoSocket(threading.Thread): | ||
| """ | ||
| Catch streaming console output by connecting to /console_output/ws socket and | ||
| save messages to the buffer. | ||
| """ | ||
|
|
||
| def __init__(self, *, endpoint, api_key=None, token=None, **kwargs): | ||
| super().__init__(**kwargs) | ||
| self.received_data_buffer = [] | ||
| self._exit = False | ||
| self._api_key = api_key | ||
| self._token = token | ||
| self._endpoint = endpoint | ||
|
|
||
| def run(self): | ||
| websocket_uri = f"ws://{SERVER_ADDRESS}:{SERVER_PORT}/api{self._endpoint}" | ||
| if self._token is not None: | ||
| additional_headers = {"Authorization": f"Bearer {self._token}"} | ||
| elif self._api_key is not None: | ||
| additional_headers = {"Authorization": f"ApiKey {self._api_key}"} | ||
| else: | ||
| additional_headers = {} | ||
|
|
||
| try: | ||
| with connect(websocket_uri, additional_headers=additional_headers) as websocket: | ||
| while not self._exit: | ||
| try: | ||
| msg_json = websocket.recv(timeout=0.1, decode=False) | ||
| try: | ||
| msg = json.loads(msg_json) | ||
| self.received_data_buffer.append(msg) | ||
| except json.JSONDecodeError: | ||
| pass | ||
| except TimeoutError: | ||
| pass | ||
| except Exception as ex: | ||
| print(f"Failed to connect to server: {ex}") | ||
|
|
||
| def stop(self): | ||
| """ | ||
| Call this method to stop the thread. Then send a request to the server so that some output | ||
| is printed in ``stdout``. | ||
| """ | ||
| self._exit = True | ||
|
|
||
| def __del__(self): | ||
| self.stop() | ||
|
|
||
|
|
||
| # fmt: off | ||
| @pytest.mark.parametrize("ws_auth_type", ["apikey", "token", "apikey_invalid", "token_invalid", "none"]) | ||
| # fmt: on | ||
| def test_websocket_auth_01( | ||
| tmpdir, | ||
| monkeypatch, | ||
| re_manager_cmd, # noqa: F811 | ||
| fastapi_server_fs, # noqa: F811 | ||
| ws_auth_type, | ||
| ): | ||
| """ | ||
| Test authentication for websockets. The test is run only on ``/status/ws`` websocket. | ||
| The other websockets are expected to use the same authentication scheme. | ||
| """ | ||
|
|
||
| # Start RE Manager | ||
| params = ["--zmq-publish-console", "ON"] | ||
| re_manager_cmd(params) | ||
|
|
||
| setup_server_with_config_file(config_file_str=config_toy_test, tmpdir=tmpdir, monkeypatch=monkeypatch) | ||
| fastapi_server_fs() | ||
|
|
||
| resp1 = request_to_json("post", "/auth/provider/toy/token", login=("bob", "bob_password")) | ||
| assert "access_token" in pprint.pformat(resp1) | ||
| token = resp1["access_token"] | ||
|
|
||
| resp3 = request_to_json( | ||
| "post", "/auth/apikey", json={"expires_in": 900, "note": "API key for testing"}, token=token | ||
| ) | ||
| assert "secret" in resp3, pprint.pformat(resp3) | ||
| assert "note" in resp3, pprint.pformat(resp3) | ||
| assert resp3["note"] == "API key for testing" | ||
| assert resp3["scopes"] == ["inherit"] | ||
| api_key = resp3["secret"] | ||
|
|
||
| endpoint = "/status/ws" | ||
| if ws_auth_type == "none": | ||
| ws_params = {} | ||
| elif ws_auth_type == "apikey": | ||
| ws_params = {"api_key": api_key} | ||
| elif ws_auth_type == "apikey_invalid": | ||
| ws_params = {"api_key": "InvalidApiKey"} | ||
| elif ws_auth_type == "token": | ||
| ws_params = {"token": token} | ||
| elif ws_auth_type == "token_invalid": | ||
| ws_params = {"token": "InvalidToken"} | ||
| else: | ||
| assert False, f"Unknown authentication type: {ws_auth_type!r}" | ||
|
|
||
| rsc = _ReceiveSystemInfoSocket(endpoint=endpoint, **ws_params) | ||
| rsc.start() | ||
| ttime.sleep(1) # Wait until the client connects to the socket | ||
|
|
||
| resp1 = request_to_json("post", "/environment/open", api_key=api_key) | ||
| assert resp1["success"] is True, pprint.pformat(resp1) | ||
|
|
||
| assert wait_for_environment_to_be_created(timeout=10, api_key=api_key) | ||
|
|
||
| resp2b = request_to_json("post", "/environment/close", api_key=api_key) | ||
| assert resp2b["success"] is True, pprint.pformat(resp2b) | ||
|
|
||
| assert wait_for_environment_to_be_closed(timeout=10, api_key=api_key) | ||
|
|
||
| # Wait until capture is complete | ||
| ttime.sleep(2) | ||
| rsc.stop() | ||
| rsc.join() | ||
|
|
||
| buffer = rsc.received_data_buffer | ||
| if ws_auth_type in ("none", "apikey_invalid", "token_invalid"): | ||
| assert len(buffer) == 0 | ||
| elif ws_auth_type in ("apikey", "token"): | ||
| assert len(buffer) > 0 | ||
| for msg in buffer: | ||
| assert "time" in msg, msg | ||
| assert isinstance(msg["time"], float), msg | ||
| assert "msg" in msg | ||
| assert isinstance(msg["msg"], dict) | ||
| else: | ||
| assert False, f"Unknown authentication type: {ws_auth_type!r}" |
This file contains hidden or 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 hidden or 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
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.
For what it's worth, we chose not to support these in Tiled because there is no mechanism for the server to request that they be refreshed, since it cannot send HTTP response codes.
Instead, the client mints a short-lived API key and revokes it after the connection is formed.
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.
This makes sense. It should be possible to implement a refresh scheme when a token is validated by sending a plain HTTP request in case connection to a websocket fails and then refreshed if requested by the server, but it does not look like a standard approach.