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

feat: add client_encryped_cert_source to ClientOptions #31

Merged
merged 2 commits into from May 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 21 additions & 2 deletions google/api_core/client_options.py
Expand Up @@ -56,12 +56,31 @@ class ClientOptions(object):
api_endpoint (str): The desired API endpoint, e.g., compute.googleapis.com
client_cert_source (Callable[[], (bytes, bytes)]): An optional callback
which returns client certificate bytes and private key bytes both in
PEM format.
PEM format. ``client_cert_source`` and ``client_encrypted_cert_source``
are mutually exclusive.
client_encrypted_cert_source (Callable[[], (str, str, bytes)]): An optional
callback which returns client certificate file path, encrypted private
key file path, and the passphrase bytes.``client_cert_source`` and
``client_encrypted_cert_source`` are mutually exclusive.

Raises:
ValueError: If both ``client_cert_source`` and ``client_encrypted_cert_source``
are provided.
"""

def __init__(self, api_endpoint=None, client_cert_source=None):
def __init__(
self,
api_endpoint=None,
client_cert_source=None,
client_encrypted_cert_source=None,
):
if client_cert_source and client_encrypted_cert_source:
raise ValueError(
"client_cert_source and client_encrypted_cert_source are mutually exclusive"
)
self.api_endpoint = api_endpoint
self.client_cert_source = client_cert_source
self.client_encrypted_cert_source = client_encrypted_cert_source

def __repr__(self):
return "ClientOptions: " + repr(self.__dict__)
Expand Down
32 changes: 30 additions & 2 deletions tests/unit/test_client_options.py
Expand Up @@ -21,6 +21,10 @@ def get_client_cert():
return b"cert", b"key"


def get_client_encrypted_cert():
return "cert_path", "key_path", b"passphrase"


def test_constructor():

options = client_options.ClientOptions(
Expand All @@ -31,6 +35,30 @@ def test_constructor():
assert options.client_cert_source() == (b"cert", b"key")


def test_constructor_with_encrypted_cert_source():

options = client_options.ClientOptions(
api_endpoint="foo.googleapis.com",
client_encrypted_cert_source=get_client_encrypted_cert,
)

assert options.api_endpoint == "foo.googleapis.com"
assert options.client_encrypted_cert_source() == (
"cert_path",
"key_path",
b"passphrase",
)


def test_constructor_with_both_cert_sources():
with pytest.raises(ValueError):
client_options.ClientOptions(
api_endpoint="foo.googleapis.com",
client_cert_source=get_client_cert,
client_encrypted_cert_source=get_client_encrypted_cert,
)


def test_from_dict():
options = client_options.from_dict(
{"api_endpoint": "foo.googleapis.com", "client_cert_source": get_client_cert}
Expand All @@ -57,6 +85,6 @@ def test_repr():

assert (
repr(options)
== "ClientOptions: {'api_endpoint': 'foo.googleapis.com', 'client_cert_source': None}"
or "ClientOptions: {'client_cert_source': None, 'api_endpoint': 'foo.googleapis.com'}"
== "ClientOptions: {'api_endpoint': 'foo.googleapis.com', 'client_cert_source': None, 'client_encrypted_cert_source': None}"
or "ClientOptions: {'client_encrypted_cert_source': None, 'client_cert_source': None, 'api_endpoint': 'foo.googleapis.com'}"
)