Skip to content
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

Fix a bug with authentication API endpoint returning internal server error under gunicorn if auth.api_url config option was not set #4809

Merged
merged 2 commits into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion st2auth/st2auth/controllers/v1/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from st2common.router import exc
from st2common.router import Response
from st2common.util import auth as auth_utils
from st2common.util import api as api_utils
from st2common import log as logging
import st2auth.handlers as handlers

Expand Down Expand Up @@ -80,7 +81,8 @@ def post(self, request, **kwargs):

def process_successful_response(token):
resp = Response(json=token, status=http_client.CREATED)
resp.headers['X-API-URL'] = cfg.CONF.auth.api_url
# NOTE: gunicon fails and throws an error if header value is not a string (e.g. if it's None)
resp.headers['X-API-URL'] = api_utils.get_base_public_api_url()
return resp


Expand Down
17 changes: 17 additions & 0 deletions st2auth/tests/unit/controllers/v1/test_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def _test_token_post(self, path=TOKEN_V1_PATH):
actual_expiry = isotime.parse(response.json['expiry'])
self.assertLess(timestamp, actual_expiry)
self.assertLess(actual_expiry, expected_expiry)
return response

def test_token_post_unauthorized(self):
response = self.app.post_json(TOKEN_V1_PATH, {}, expect_errors=True, extra_environ={
Expand All @@ -109,6 +110,22 @@ def test_token_post_new_user(self):
def test_token_post_existing_user(self):
self._test_token_post()

@mock.patch.object(
User, 'get_by_name',
mock.MagicMock(return_value=UserDB(name=USERNAME)))
def test_token_post_success_x_api_url_header_value(self):
# auth.api_url option is explicitly set
cfg.CONF.set_override('api_url', override='https://example.com', group='auth')

resp = self._test_token_post()
self.assertEqual(resp.headers['X-API-URL'], 'https://example.com')

# auth.api_url option is not set, url is inferred from listen host and port
cfg.CONF.set_override('api_url', override=None, group='auth')

resp = self._test_token_post()
self.assertEqual(resp.headers['X-API-URL'], 'http://127.0.0.1:9101')

@mock.patch.object(
User, 'get_by_name',
mock.MagicMock(return_value=UserDB(name=USERNAME)))
Expand Down