Skip to content

Commit

Permalink
feat: add client_encryped_cert_source to ClientOptions (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
arithmetic1728 committed May 27, 2020
1 parent a82f289 commit e4eaec0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
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'}"
)

0 comments on commit e4eaec0

Please sign in to comment.