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

Check OpenSSL environment variables before defaulting to certifi #196

Merged
merged 15 commits into from
Nov 22, 2022

Conversation

aiudirog
Copy link
Contributor

Description

Updated the urllib3 and aiohttp backends to check OpenSSL environment variables SSL_CERT_FILE/DIR before defaulting to certify's CA bundle to allow environmental configuration of custom SSL certificates.

Issues Resolved

Closes #111

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@aiudirog aiudirog requested review from a team, axeoman and deztructor as code owners September 11, 2022 05:04
Signed-off-by: Roger Aiudi <aiudirog@gmail.com>
@aiudirog
Copy link
Contributor Author

I submitted the simplest implementation I could write but I do see several potential improvements depending on what people think:

  • Delaying environment variable checks to when the OpenSearch client is initialized
    • This would allow for test cases which are currently tough to do with the checks at the module level
  • Respecting other environment variables like REQUESTS_CA_BUNDLE and CURL_CA_BUNDLE for better compatibility
    • This could quickly get out of hand though
  • Moving the CA_CERTS declaration to a shared module (maybe opensearchpy.connection.base?) to DRY up the code
  • Adding this to the requests backend as well to improve consistency

@harshavamsi
Copy link
Collaborator

@wbeckler @VachaShah could you approve running the workflows?

Signed-off-by: Roger Aiudi <aiudirog@gmail.com>
@axeoman
Copy link
Collaborator

axeoman commented Sep 16, 2022

Thanks a lot for the contribution! 👍

Moving the CA_CERTS declaration to a shared module (maybe opensearchpy.connection.base?) to DRY up the code

Yep, definitely would be good to have it in a single place.

Adding this to the requests backend as well to improve consistency

Yep, good idea too.

Respecting other environment variables like REQUESTS_CA_BUNDLE and CURL_CA_BUNDLE for better compatibility
This could quickly get out of hand though

I would not go there until there are real issues reported. Are they?

Delaying environment variable checks to when the OpenSearch client is initialized
This would allow for test cases which are currently tough to do with the checks at the module level

Could you please elaborate? What tests do you have in mind?

@aiudirog
Copy link
Contributor Author

Yep, definitely would be good to have it in a single place.

Sounds good. Is putting it in the connection base module good enough or would you like it in a distinct/different module? (Distinct seems overkill to me, but I could see the benefit)

I would not go there until there are real issues reported. Are they?

Not that I know of. The only potential issue I can think of would be that enabling this for the requests backend would override requests' default behavior by forcing certifi even when the CA_BUNDLE variables are set. Probably not an issue, since using requests should really be just an implementation detail, but it's probably something to track as a definite choice.

Could you please elaborate? What tests do you have in mind?

Just basic sanity tests to ensure the environment variables are properly read and the fallback to certify still occurs in their absence. Not strictly necessary, but nice to have.

Signed-off-by: Roger Aiudi <aiudirog@gmail.com>
Signed-off-by: Roger Aiudi <aiudirog@gmail.com>
Signed-off-by: Roger Aiudi <aiudirog@gmail.com>
Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

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

LGTM. Add to CHANGELOG and USER_GUIDE?

@aiudirog
Copy link
Contributor Author

aiudirog commented Nov 7, 2022

@dblock Sorry for the delay, I've updated the docs. Please let me know if there is anything you'd like me to change.

dblock
dblock previously approved these changes Nov 9, 2022
Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

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

LGTM, I think there's an opportunity to add a test here too

Signed-off-by: Roger Aiudi <aiudirog@gmail.com>
@aiudirog
Copy link
Contributor Author

@dblock I added some tests to make sure each Connection implementation correctly handles the CA_CERTS constant, but I'm not sure how to test that SSL_CERT_FILE/DIR env vars are loaded properly without changing the implementation to delay loading the CA_CERTS until Connection instance initialization or adding much more complicated tests that either reload the related modules/start subprocesses. What's your opinion?

Signed-off-by: Roger Aiudi <aiudirog@gmail.com>
@aiudirog aiudirog force-pushed the respect_openssl_env branch from 5ff5aa6 to 4fca678 Compare November 13, 2022 07:23
dblock
dblock previously approved these changes Nov 15, 2022
@dblock
Copy link
Member

dblock commented Nov 15, 2022

@dblock I added some tests to make sure each Connection implementation correctly handles the CA_CERTS constant, but I'm not sure how to test that SSL_CERT_FILE/DIR env vars are loaded properly without changing the implementation to delay loading the CA_CERTS until Connection instance initialization or adding much more complicated tests that either reload the related modules/start subprocesses. What's your opinion?

If you move the implementation of CA_CERTS = into a method, then you can test that method by calling it again and having all the right mocks. But I also think that we're trying to be too perfect and I am A-OK with this PR as is, it's great work!

Add test cases for the different CA cert configurations

Signed-off-by: Roger Aiudi <aiudirog@gmail.com>
@aiudirog
Copy link
Contributor Author

@dblock I had the same thought originally, so I went ahead and implemented it with tests. Let me know what you think.

dblock
dblock previously approved these changes Nov 17, 2022
@dblock
Copy link
Member

dblock commented Nov 17, 2022

@dblock I had the same thought originally, so I went ahead and implemented it with tests. Let me know what you think.

Perfect. @VachaShah help CR and let's merge this?

VachaShah
VachaShah previously approved these changes Nov 18, 2022
Copy link
Collaborator

@VachaShah VachaShah left a comment

Choose a reason for hiding this comment

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

LGTM, thank you @aiudirog!

@VachaShah
Copy link
Collaborator

Workflows are failing for certain python versions for the latest ubuntu-22.04. Related issue on actions/setup-python actions/setup-python#543

Signed-off-by: Roger Aiudi <aiudirog@gmail.com>
@aiudirog aiudirog dismissed stale reviews from VachaShah and dblock via c1b2f49 November 19, 2022 18:48
@aiudirog
Copy link
Contributor Author

@VachaShah It doesn't look like those issues are going to be resolved soon, so I pushed a commit to implement the workarounds that were suggested

Signed-off-by: Roger Aiudi <aiudirog@gmail.com>
Signed-off-by: Roger Aiudi <aiudirog@gmail.com>
@aiudirog aiudirog requested review from dblock and VachaShah and removed request for dblock and VachaShah November 21, 2022 17:42
Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

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

All good with me.

Let's document SSL configuration in https://github.com/opensearch-project/opensearch-py/blob/main/USER_GUIDE.md too, can be done in a future PR.

@dblock dblock requested a review from VachaShah November 21, 2022 22:02
@dblock dblock merged commit 2672f3f into opensearch-project:main Nov 22, 2022
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.

[FEATURE] Respect SSL certificate environment variables
5 participants