-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add default client cert source util (#486)
feat: add default client cert source util
- Loading branch information
1 parent
cc614a6
commit ed41b49
Showing
6 changed files
with
176 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Utilites for mutual TLS.""" | ||
|
||
import six | ||
|
||
from google.auth import exceptions | ||
from google.auth.transport import _mtls_helper | ||
|
||
|
||
def has_default_client_cert_source(): | ||
"""Check if default client SSL credentials exists on the device. | ||
Returns: | ||
bool: indicating if the default client cert source exists. | ||
""" | ||
metadata_path = _mtls_helper._check_dca_metadata_path( | ||
_mtls_helper.CONTEXT_AWARE_METADATA_PATH | ||
) | ||
return metadata_path is not None | ||
|
||
|
||
def default_client_cert_source(): | ||
"""Get a callback which returns the default client SSL credentials. | ||
Returns: | ||
Callable[[], [bytes, bytes]]: A callback which returns the default | ||
client certificate bytes and private key bytes, both in PEM format. | ||
Raises: | ||
google.auth.exceptions.DefaultClientCertSourceError: If the default | ||
client SSL credentials don't exist or are malformed. | ||
""" | ||
if not has_default_client_cert_source(): | ||
raise exceptions.MutualTLSChannelError( | ||
"Default client cert source doesn't exist" | ||
) | ||
|
||
def callback(): | ||
try: | ||
_, cert_bytes, key_bytes = _mtls_helper.get_client_cert_and_key() | ||
except (OSError, RuntimeError, ValueError) as caught_exc: | ||
new_exc = exceptions.MutualTLSChannelError(caught_exc) | ||
six.raise_from(new_exc, caught_exc) | ||
|
||
return cert_bytes, key_bytes | ||
|
||
return callback |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import mock | ||
import pytest | ||
|
||
from google.auth import exceptions | ||
from google.auth.transport import mtls | ||
|
||
|
||
@mock.patch( | ||
"google.auth.transport._mtls_helper._check_dca_metadata_path", autospec=True | ||
) | ||
def test_has_default_client_cert_source(check_dca_metadata_path): | ||
check_dca_metadata_path.return_value = mock.Mock() | ||
assert mtls.has_default_client_cert_source() | ||
|
||
check_dca_metadata_path.return_value = None | ||
assert not mtls.has_default_client_cert_source() | ||
|
||
|
||
@mock.patch("google.auth.transport._mtls_helper.get_client_cert_and_key", autospec=True) | ||
@mock.patch("google.auth.transport.mtls.has_default_client_cert_source", autospec=True) | ||
def test_default_client_cert_source( | ||
has_default_client_cert_source, get_client_cert_and_key | ||
): | ||
# Test default client cert source doesn't exist. | ||
has_default_client_cert_source.return_value = False | ||
with pytest.raises(exceptions.MutualTLSChannelError): | ||
mtls.default_client_cert_source() | ||
|
||
# The following tests will assume default client cert source exists. | ||
has_default_client_cert_source.return_value = True | ||
|
||
# Test good callback. | ||
get_client_cert_and_key.return_value = (True, b"cert", b"key") | ||
callback = mtls.default_client_cert_source() | ||
assert callback() == (b"cert", b"key") | ||
|
||
# Test bad callback which throws exception. | ||
get_client_cert_and_key.side_effect = ValueError() | ||
callback = mtls.default_client_cert_source() | ||
with pytest.raises(exceptions.MutualTLSChannelError): | ||
callback() |