Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ class SearchServiceClient(HeadersMixin): # pylint: disable=too-many-public-meth
def __init__(self, endpoint, credential, **kwargs):
# type: (str, AzureKeyCredential, **Any) -> None

try:
if endpoint.lower().startswith('http') and not endpoint.lower().startswith('https'):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there scenario were a valid endpoint would not start by http?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

service only supports https

raise ValueError("Endpoint should be secure. Use https.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use a wording more consistent with Azure-core:

"Bearer token authentication is not permitted for non-TLS protected (non-https) URLs."

if not endpoint.lower().startswith('http'):
endpoint = "https://" + endpoint
except AttributeError:
raise ValueError("Endpoint must be a string")

self._endpoint = endpoint # type: str
self._credential = credential # type: AzureKeyCredential
self._client = _SearchServiceClient(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ class SearchServiceClient(HeadersMixin): # pylint: disable=too-many-public-meth
def __init__(self, endpoint, credential, **kwargs):
# type: (str, AzureKeyCredential, **Any) -> None

try:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a shame we have to duplicate this code, should we have a SeachServiceClientBase?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was think about the same - Will use this PR to do that

if endpoint.lower().startswith('http') and not endpoint.lower().startswith('https'):
raise ValueError("Endpoint should be secure. Use https.")
if not endpoint.lower().startswith('http'):
endpoint = "https://" + endpoint
except AttributeError:
raise ValueError("Endpoint must be a string")

self._endpoint = endpoint # type: str
self._credential = credential # type: AzureKeyCredential
self._client = _SearchServiceClient(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_credential_roll(self):
def test_repr(self):
client = SearchServiceClient("endpoint", CREDENTIAL)
assert repr(client) == "<SearchServiceClient [endpoint={}]>".format(
repr("endpoint")
repr("https://endpoint")
)

@mock.patch(
Expand All @@ -52,3 +52,17 @@ def test_get_service_statistics(self, mock_get_stats):
assert mock_get_stats.called
assert mock_get_stats.call_args[0] == ()
assert mock_get_stats.call_args[1] == {"headers": client._headers}

def test_endpoint_https(self):
credential = AzureKeyCredential(key="old_api_key")
client = SearchServiceClient("endpoint", credential)
assert client._endpoint.startswith('https')

client = SearchServiceClient("https://endpoint", credential)
assert client._endpoint.startswith('https')

with pytest.raises(ValueError):
client = SearchServiceClient("http://endpoint", credential)

with pytest.raises(ValueError):
client = SearchServiceClient(12345, credential)