Skip to content

Added support ssl cert file environment - #307

Merged
sethmlarson merged 18 commits into
encode:masterfrom
cansarigol:issue-33-ca
Sep 23, 2019
Merged

Added support ssl cert file environment#307
sethmlarson merged 18 commits into
encode:masterfrom
cansarigol:issue-33-ca

Conversation

@cansarigol

@cansarigolcansarigol commented Sep 2, 2019

Copy link
Copy Markdown
Contributor

Closes #306

Comment threadhttpx/utils.py Outdated
Comment threadtests/test_utils.py
Comment threaddocs/environment_variables.md Outdated
@sethmlarson

Copy link
Copy Markdown
Contributor

@StephenBrown2 You took the review comments right out of my mouth! 🎉 Thanks @cansarigol for tackling this issue. :)

@sethmlarson

Copy link
Copy Markdown
Contributor

Also this would benefit from #308 landing. :)

@sethmlarson

Copy link
Copy Markdown
Contributor

Btw #308 landed so I'd rebase your branch on master so that testing this is easier. :)

@cansarigol

cansarigol commented Sep 2, 2019

Copy link
Copy Markdown
ContributorAuthor

@StephenBrown2@sethmlarson thanks for review.

@lovelydinosaurlovelydinosaur left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Great stuff - nice bit of progress towards this.
I've got some thoughts and questions above that probably need addressing.

We should also review if we really want all of SSL_CERT_FILE, CURL_CA_BUNDLE, and REQUESTS_CA_BUNDLE, since that seems like quite a large exposure area to various environment variables, which I think we should prefer to avoid if possible.

Perhaps(?) we should only start with SSL_CERT_FILE support?

Comment threaddocs/environment_variables.md Outdated
Comment threaddocs/environment_variables.md Outdated
Comment threaddocs/environment_variables.md Outdated
Comment threadhttpx/config.py Outdated
@cansarigol

Copy link
Copy Markdown
ContributorAuthor

Thanks @tomchristie , I applied your comments. I got the order from this pr Bwt it wasn't merged

@sethmlarson

Copy link
Copy Markdown
Contributor

Shouldn't we be supporting (but I agree not recommending) REQUESTS_CA_BUNDLE for the sake of requests compatibility? I'm fine with dropping curls.

Comment threadhttpx/utils.py Outdated
@cansarigol

Copy link
Copy Markdown
ContributorAuthor

Hi, I dropped REQUESTS_CA_BUNDLE and CURL_CA_BUNDLE envs support. is it ok?

@cansarigol
cansarigolforce-pushed the issue-33-ca branch 2 times, most recently from 7008bf4 to 34375adCompareSeptember 3, 2019 18:59
@lovelydinosaur

Copy link
Copy Markdown
Contributor

Shouldn't we be supporting (but I agree not recommending) REQUESTS_CA_BUNDLE for the sake of requests compatibility? I'm fine with dropping curls.

I don't know. I think we should possibly make that call seperately, once we're closing in on 1.0, when we're assessing and drawing up our requests compat. guide.

My rough feel at this point, is that this is probably one of the points where we ought to allow ourselves to diverge, but it depends on a more general question of just how closely we want to align.

In summary: I'd probably rather we make that specific call within the context of a more general review of our requests compat & divergences, rather than as part of this "add the functionality" PR.

Seem reasonable?

@sethmlarsonsethmlarson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Some more review comments

Comment threadhttpx/utils.py Outdated
Comment threadtests/test_config.py Outdated
Comment threaddocs/environment_variables.md
Comment threaddocs/environment_variables.md Outdated
Comment threaddocs/environment_variables.md Outdated
Comment threaddocs/environment_variables.md Outdated
@cansarigol

Copy link
Copy Markdown
ContributorAuthor

Hi, @sethmlarson thanks for your review.

@sethmlarson

Copy link
Copy Markdown
Contributor

@cansarigol by the way we squash all commits when we merge so you don't have to amend and force push, you can just add a commits. Easier to review so we can see the incremental changes you're making.

@cansarigol

Copy link
Copy Markdown
ContributorAuthor

Ah sorry, you're right. this is my habit. I will pay attention.

Comment threadhttpx/config.py Outdated
Comment threadtests/test_config.py
context = ssl_config.load_ssl_context()
assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
assert context.check_hostname is True
assert ssl_config.verify == os.environ[config]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verify that certs are loaded via assert len(context.get_ca_certs) > 0

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

get_ca_certs returns empty if use context.load_verify_locations(capath=str(ca_bundle_path)).
To get a result, we should create an SSLSocket and call getpeercert like below

if config == "SSL_CERT_DIR":
HOST = "example.org"
PORT = 443
conn: ssl.SSLSocket = ssl_config.ssl_context.wrap_socket(
socket.socket(socket.AF_INET, socket.SOCK_STREAM), server_hostname=HOST
)
conn.connect((HOST, PORT))
conn.getpeercert()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe we can use https_server.url.host and https_server.url.port to accomplish that. Then our certificate will verify as well :)

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I couldn't verify with cert_pem_file for https_server.url. Please help :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Has this been resolved? :)

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I'm very sorry that I didn't do anything but i will asap

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is blocked by #354, just gave it a spin myself. I think we should maybe merge this now?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

#354 is closed now, I tried changing the test to look like this right now:

os.environ[config] = (
ca_cert_pem_fileifconfig.endswith("_FILE")
elsestr(Path(ca_cert_pem_file).parent)
)
ssl_config=httpx.SSLConfig(trust_env=True)
context=ssl_config.load_ssl_context()
assertcontext.verify_mode==ssl.VerifyMode.CERT_REQUIREDassertcontext.check_hostnameisTrueassertssl_config.verify==os.environ[config]
host=https_server.url.hostport=https_server.url.portconn=socket.create_connection((host, port))
context.wrap_socket(conn, server_hostname=host)
assertlen(context.get_ca_certs()) ==1

But I'm still failing on the SSL_CERT_DIR test case and I don't know why, pretty annoying!

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I added a test to be able to compare with below traceback.

tests/test_config.py ...FSSL error in data received
protocol: <asyncio.sslproto.SSLProtocol object at 0x103c044a8>
transport: <_SelectorSocketTransport closing fd=19 read=idle write=<idle, bufsize=0>>
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/sslproto.py", line 526, in data_received
ssldata, appdata = self._sslpipe.feed_ssldata(data)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/sslproto.py", line 189, in feed_ssldata
self._sslobj.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 763, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: TLSV1_ALERT_UNKNOWN_CA] tlsv1 alert unknown ca (_ssl.c:1045)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Spoke with @florimondmanca and I think we're just going to skip the SSL_CERT_DIR case here and if we run into issues with it we'll fix them then. Until then this PR is good to go. Thank you so much @cansarigol for following this one all the way through. :)

* from load_ssl_context
* to load_ssl_context_verify
@sethmlarson

Copy link
Copy Markdown
Contributor

After we get this merged I think we're ready to tag 0.7.3! @yeraydiazdiaz or @florimondmanca should do the release PR so we all get practice :)

Comment threaddocs/environment_variables.md Outdated

@sethmlarsonsethmlarson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I went through and verified all the outstanding comments and made one small change to the logic in load_ssl_context_verify because verify isn't None by that point.

Would like one more set of eyes to look and merge this PR. Thanks @cansarigol!

@florimondmancaflorimondmanca left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is looking good! :-) I think I caught a possible bug — if confirmed we definitely need to address it before merging.

Comment threaddocs/environment_variables.md Outdated
Comment threaddocs/environment_variables.md
Comment threadhttpx/utils.py Outdated
Comment threadtests/test_utils.py
@cansarigol

Copy link
Copy Markdown
ContributorAuthor

Hi @florimondmanca, thanks for your review and help.

@florimondmancaflorimondmanca left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looking good, provided the last open discussion is resolved. Great work @cansarigol!

@florimondmancaflorimondmanca added the help wanted Extra attention is needed label Sep 21, 2019
Comment threadtests/test_config.py
context = ssl_config.load_ssl_context()
assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
assert context.check_hostname is True
assert ssl_config.verify == os.environ[config]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Spoke with @florimondmanca and I think we're just going to skip the SSL_CERT_DIR case here and if we run into issues with it we'll fix them then. Until then this PR is good to go. Thank you so much @cansarigol for following this one all the way through. :)

@sethmlarson
sethmlarson merged commit c9810a7 into encode:masterSep 23, 2019
@cansarigol
cansarigol deleted the issue-33-ca branch September 23, 2019 17:12
@rehevkor5

Copy link
Copy Markdown

FYI, this was removed in #3022

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

help wantedExtra attention is needed

Development

Successfully merging this pull request may close these issues.

Add support for SSL_CERT_FILE, REQUESTS_CA_BUNDLE, and CURL_CA_BUNDLE

6 participants

@cansarigol@sethmlarson@lovelydinosaur@rehevkor5@StephenBrown2@florimondmanca