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

Enforce https in search #11337

Merged
merged 8 commits into from
May 13, 2020
Merged

Enforce https in search #11337

merged 8 commits into from
May 13, 2020

Conversation

rakshith91
Copy link
Contributor

Fixes #10981

Case1: "xyz.search.windows.net"
Current Behavior: throws some random key error because it fails to parse in azure core
Changed to: "https://xyz.search.windows.net"

Case2: "https://xyz.search.windows.net"
No behavior changed - correct endpoint

Case3: "http://xyz.search.windows.net"
Behavior changed to raising value error and warn to use https

Case4: 1234 (non string)
Raises a value error

@rakshith91 rakshith91 requested review from lmazuel and mayurid as code owners May 8, 2020 17:13
@@ -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

@@ -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'):
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."

@@ -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

# 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.

For clarity I would do it:

if not endpoint.lower().startswith('http'):
    endpoint = "https://" + endpoint
elif not endpoint.lower().startswith('https'):
    raise ValueError("Bearer token authentication is not permitted for non-TLS protected (non-https) URLs.")

lmazuel
lmazuel previously approved these changes May 12, 2020
from ._indexes_client import SearchIndexesClient
from ._indexers_client import SearchIndexersClient
from ._skillsets_client import SearchSkillsetsClient
from ._synonym_maps_client import SearchSynonymMapsClient
Copy link
Member

Choose a reason for hiding this comment

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

Don't seem those imports are necessary here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

how is pylint passing? ⭕

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed these - but im shocked to see pylint not catching unused-import

:end-before: [END create_search_service_with_key]
:language: python
:dedent: 4
:caption: Creating the SearchServiceClient with an API key.
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the snippet be on this base class that users will never use directly themselves?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed these

elif not endpoint.lower().startswith('https'):
raise ValueError("Bearer token authentication is not permitted for non-TLS protected (non-https) URLs.")
except AttributeError:
raise ValueError("Endpoint must be a string.")
Copy link
Contributor

Choose a reason for hiding this comment

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

Could put this in a util function that has simple unit tests, then

self._endpoint = normalize_endpoint(endpoint)

below

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@rakshith91 rakshith91 merged commit 2d085f5 into Azure:master May 13, 2020
iscai-msft added a commit that referenced this pull request May 18, 2020
…into feature/text_analytics_v3.0

* 'master' of https://github.com/Azure/azure-sdk-for-python: (128 commits)
  add more content to index crud samples (#11443)
  Add a snippet to the Samples readme mirroring the core readme, guiding users to the currently mainline version of the lib if the end up on the wrong page. (#11420)
  20200515 run resource live test (#11454)
  Release azure mgmt eventgrid (#11431)
  Smoke Tests use new workflow for package install (#11438)
  ServiceFabric 7.1 (#11451)
  update (#11424)
  Typing for appconfiguration (#11427)
  [form recognizer] Move `get_client` method from FormRecognizer -> FormTraining (#11423)
  release customvision (#11428)
  Enforce https in search (#11337)
  [Cosmos] Remove unused files (#11388)
  Sync eng/common directory with azure-sdk-tools repository (#11417)
  [form recognizer] consistency renames for FormTrainingClient (#11390)
  Release for azure mgmt eventhub (#11403)
  Network 2020 04 01 (#11405)
  link in to reference docs for sub-clients (#11396)
  Persistent caching for interactive credentials on Linux and macOS (#11319)
  [formrecognizer] add AAD auth support (#11275)
  Search docs/readme updates (#11391)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Search must handle non https urls
3 participants