-
Notifications
You must be signed in to change notification settings - Fork 337
feat: add SslCredentials class for mTLS ADC #448
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
781654d
feat: add SslCredentials class for mTLS ADC (linux)
arithmetic1728 2f0aaa7
Fix JSON exception issue
arithmetic1728 e628966
Update google/auth/transport/_mtls_helper.py
arithmetic1728 ed65141
modify the code based on comments
arithmetic1728 fb36050
Merge pull request #1 from arithmetic1728/fix
arithmetic1728 f5671e4
fix docstring
arithmetic1728 a8618fe
Merge pull request #2 from arithmetic1728/fix
arithmetic1728 296977e
throw exceptions to user, add client_cert_callback
arithmetic1728 03a147f
Merge pull request #3 from arithmetic1728/fix
arithmetic1728 7d8dff7
throw exception is metadata file is not json
arithmetic1728 c49a79f
Merge pull request #5 from arithmetic1728/fix
arithmetic1728 1dc22c0
fix typo
arithmetic1728 12e69d8
update
arithmetic1728 b2e394b
update docstring
arithmetic1728 c510abe
Merge branch 'master' into master
arithmetic1728 e6f2f10
don't use ADC if client_cert_callback is provided
arithmetic1728 da135ad
update docstring
arithmetic1728 514ffa9
Merge branch 'master' into master
arithmetic1728 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,116 @@ | ||
# 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. | ||
|
||
"""Helper functions for getting mTLS cert and key, for internal use only.""" | ||
|
||
import json | ||
import logging | ||
from os import path | ||
import re | ||
import subprocess | ||
|
||
CONTEXT_AWARE_METADATA_PATH = "~/.secureConnect/context_aware_metadata.json" | ||
arithmetic1728 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_CERT_PROVIDER_COMMAND = "cert_provider_command" | ||
_CERT_REGEX = re.compile( | ||
b"-----BEGIN CERTIFICATE-----.+-----END CERTIFICATE-----\r?\n?", re.DOTALL | ||
arithmetic1728 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
# support various format of key files, e.g. | ||
# "-----BEGIN PRIVATE KEY-----...", | ||
# "-----BEGIN EC PRIVATE KEY-----...", | ||
# "-----BEGIN RSA PRIVATE KEY-----..." | ||
_KEY_REGEX = re.compile( | ||
b"-----BEGIN [A-Z ]*PRIVATE KEY-----.+-----END [A-Z ]*PRIVATE KEY-----\r?\n?", | ||
re.DOTALL, | ||
) | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def _check_dca_metadata_path(metadata_path): | ||
"""Checks for context aware metadata. If it exists, returns the absolute path; | ||
otherwise returns None. | ||
|
||
Args: | ||
metadata_path (str): context aware metadata path. | ||
|
||
Returns: | ||
str: absolute path if exists and None otherwise. | ||
""" | ||
metadata_path = path.expanduser(metadata_path) | ||
if not path.exists(metadata_path): | ||
_LOGGER.debug("%s is not found, skip client SSL authentication.", metadata_path) | ||
return None | ||
return metadata_path | ||
|
||
|
||
def _read_dca_metadata_file(metadata_path): | ||
"""Loads context aware metadata from the given path. | ||
|
||
Args: | ||
metadata_path (str): context aware metadata path. | ||
|
||
Returns: | ||
Dict[str, str]: The metadata. | ||
|
||
Raises: | ||
ValueError: If failed to parse metadata as JSON. | ||
""" | ||
with open(metadata_path) as f: | ||
metadata = json.load(f) | ||
|
||
return metadata | ||
|
||
|
||
def get_client_ssl_credentials(metadata_json): | ||
"""Returns the client side mTLS cert and key. | ||
|
||
Args: | ||
metadata_json (Dict[str, str]): metadata JSON file which contains the cert | ||
provider command. | ||
|
||
Returns: | ||
Tuple[bytes, bytes]: client certificate and key, both in PEM format. | ||
|
||
Raises: | ||
OSError: If the cert provider command failed to run. | ||
RuntimeError: If the cert provider command has a runtime error. | ||
ValueError: If the metadata json file doesn't contain the cert provider command or if the command doesn't produce both the client certificate and client key. | ||
""" | ||
# TODO: implement an in-memory cache of cert and key so we don't have to | ||
# run cert provider command every time. | ||
|
||
# Check the cert provider command existence in the metadata json file. | ||
if _CERT_PROVIDER_COMMAND not in metadata_json: | ||
raise ValueError("Cert provider command is not found") | ||
|
||
# Execute the command. It throws OsError in case of system failure. | ||
command = metadata_json[_CERT_PROVIDER_COMMAND] | ||
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
stdout, stderr = process.communicate() | ||
|
||
# Check cert provider command execution error. | ||
if process.returncode != 0: | ||
raise RuntimeError( | ||
"Cert provider command returns non-zero status code %s" % process.returncode | ||
) | ||
|
||
# Extract certificate (chain) and key. | ||
cert_match = re.findall(_CERT_REGEX, stdout) | ||
if len(cert_match) != 1: | ||
raise ValueError("Client SSL certificate is missing or invalid") | ||
key_match = re.findall(_KEY_REGEX, stdout) | ||
if len(key_match) != 1: | ||
raise ValueError("Client SSL key is missing or invalid") | ||
return cert_match[0], key_match[0] |
This file contains hidden or 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 hidden or 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,6 @@ | ||
{ | ||
"cert_provider_command":[ | ||
"/opt/google/endpoint-verification/bin/SecureConnectHelper", | ||
"--print_certificate"], | ||
"device_resource_ids":["11111111-1111-1111"] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.