Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

feat: add topic field to Secret #80

Merged
merged 8 commits into from Mar 10, 2021
2 changes: 2 additions & 0 deletions google/cloud/secretmanager/__init__.py
Expand Up @@ -30,6 +30,7 @@
from google.cloud.secretmanager_v1.types.resources import Secret
from google.cloud.secretmanager_v1.types.resources import SecretPayload
from google.cloud.secretmanager_v1.types.resources import SecretVersion
from google.cloud.secretmanager_v1.types.resources import Topic
from google.cloud.secretmanager_v1.types.service import AccessSecretVersionRequest
from google.cloud.secretmanager_v1.types.service import AccessSecretVersionResponse
from google.cloud.secretmanager_v1.types.service import AddSecretVersionRequest
Expand Down Expand Up @@ -70,5 +71,6 @@
"SecretManagerServiceClient",
"SecretPayload",
"SecretVersion",
"Topic",
"UpdateSecretRequest",
)
2 changes: 2 additions & 0 deletions google/cloud/secretmanager_v1/__init__.py
Expand Up @@ -23,6 +23,7 @@
from .types.resources import Secret
from .types.resources import SecretPayload
from .types.resources import SecretVersion
from .types.resources import Topic
from .types.service import AccessSecretVersionRequest
from .types.service import AccessSecretVersionResponse
from .types.service import AddSecretVersionRequest
Expand Down Expand Up @@ -62,6 +63,7 @@
"Secret",
"SecretPayload",
"SecretVersion",
"Topic",
"UpdateSecretRequest",
"SecretManagerServiceClient",
)
Expand Up @@ -63,6 +63,8 @@ class SecretManagerServiceAsyncClient:
parse_secret_version_path = staticmethod(
SecretManagerServiceClient.parse_secret_version_path
)
topic_path = staticmethod(SecretManagerServiceClient.topic_path)
parse_topic_path = staticmethod(SecretManagerServiceClient.parse_topic_path)

common_billing_account_path = staticmethod(
SecretManagerServiceClient.common_billing_account_path
Expand Down Expand Up @@ -93,8 +95,36 @@ class SecretManagerServiceAsyncClient:
SecretManagerServiceClient.parse_common_location_path
)

from_service_account_info = SecretManagerServiceClient.from_service_account_info
from_service_account_file = SecretManagerServiceClient.from_service_account_file
@classmethod
def from_service_account_info(cls, info: dict, *args, **kwargs):
"""Creates an instance of this client using the provided credentials info.

Args:
info (dict): The service account private key info.
args: Additional arguments to pass to the constructor.
kwargs: Additional arguments to pass to the constructor.

Returns:
SecretManagerServiceAsyncClient: The constructed client.
"""
return SecretManagerServiceClient.from_service_account_info.__func__(SecretManagerServiceAsyncClient, info, *args, **kwargs) # type: ignore

@classmethod
def from_service_account_file(cls, filename: str, *args, **kwargs):
"""Creates an instance of this client using the provided credentials
file.

Args:
filename (str): The path to the service account private key json
file.
args: Additional arguments to pass to the constructor.
kwargs: Additional arguments to pass to the constructor.

Returns:
SecretManagerServiceAsyncClient: The constructed client.
"""
return SecretManagerServiceClient.from_service_account_file.__func__(SecretManagerServiceAsyncClient, filename, *args, **kwargs) # type: ignore

from_service_account_json = from_service_account_file

@property
Expand Down
Expand Up @@ -199,6 +199,17 @@ def parse_secret_version_path(path: str) -> Dict[str, str]:
)
return m.groupdict() if m else {}

@staticmethod
def topic_path(project: str, topic: str,) -> str:
"""Return a fully-qualified topic string."""
return "projects/{project}/topics/{topic}".format(project=project, topic=topic,)

@staticmethod
def parse_topic_path(path: str) -> Dict[str, str]:
"""Parse a topic path into its component segments."""
m = re.match(r"^projects/(?P<project>.+?)/topics/(?P<topic>.+?)$", path)
return m.groupdict() if m else {}

@staticmethod
def common_billing_account_path(billing_account: str,) -> str:
"""Return a fully-qualified billing_account string."""
Expand Down Expand Up @@ -313,21 +324,17 @@ def __init__(
util.strtobool(os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false"))
)

ssl_credentials = None
client_cert_source_func = None
is_mtls = False
if use_client_cert:
if client_options.client_cert_source:
import grpc # type: ignore

cert, key = client_options.client_cert_source()
ssl_credentials = grpc.ssl_channel_credentials(
certificate_chain=cert, private_key=key
)
is_mtls = True
client_cert_source_func = client_options.client_cert_source
else:
creds = SslCredentials()
is_mtls = creds.is_mtls
ssl_credentials = creds.ssl_credentials if is_mtls else None
is_mtls = mtls.has_default_client_cert_source()
client_cert_source_func = (
mtls.default_client_cert_source() if is_mtls else None
)

# Figure out which api endpoint to use.
if client_options.api_endpoint is not None:
Expand Down Expand Up @@ -370,7 +377,7 @@ def __init__(
credentials_file=client_options.credentials_file,
host=api_endpoint,
scopes=client_options.scopes,
ssl_channel_credentials=ssl_credentials,
client_cert_source_for_mtls=client_cert_source_func,
quota_project_id=client_options.quota_project_id,
client_info=client_info,
)
Expand Down Expand Up @@ -1488,10 +1495,13 @@ def set_iam_policy(
"""
# Create or coerce a protobuf request object.

# The request isn't a proto-plus wrapped type,
# so it must be constructed via keyword expansion.
if isinstance(request, dict):
# The request isn't a proto-plus wrapped type,
# so it must be constructed via keyword expansion.
request = iam_policy.SetIamPolicyRequest(**request)
elif not request:
# Null request, just make one.
request = iam_policy.SetIamPolicyRequest()

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
Expand Down Expand Up @@ -1593,10 +1603,13 @@ def get_iam_policy(
"""
# Create or coerce a protobuf request object.

# The request isn't a proto-plus wrapped type,
# so it must be constructed via keyword expansion.
if isinstance(request, dict):
# The request isn't a proto-plus wrapped type,
# so it must be constructed via keyword expansion.
request = iam_policy.GetIamPolicyRequest(**request)
elif not request:
# Null request, just make one.
request = iam_policy.GetIamPolicyRequest()

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
Expand Down Expand Up @@ -1648,10 +1661,13 @@ def test_iam_permissions(
"""
# Create or coerce a protobuf request object.

# The request isn't a proto-plus wrapped type,
# so it must be constructed via keyword expansion.
if isinstance(request, dict):
# The request isn't a proto-plus wrapped type,
# so it must be constructed via keyword expansion.
request = iam_policy.TestIamPermissionsRequest(**request)
elif not request:
# Null request, just make one.
request = iam_policy.TestIamPermissionsRequest()

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
Expand Down
Expand Up @@ -15,7 +15,16 @@
# limitations under the License.
#

from typing import Any, AsyncIterable, Awaitable, Callable, Iterable, Sequence, Tuple
from typing import (
Any,
AsyncIterable,
Awaitable,
Callable,
Iterable,
Sequence,
Tuple,
Optional,
)

from google.cloud.secretmanager_v1.types import resources
from google.cloud.secretmanager_v1.types import service
Expand Down
Expand Up @@ -67,6 +67,7 @@ def __init__(
api_mtls_endpoint: str = None,
client_cert_source: Callable[[], Tuple[bytes, bytes]] = None,
ssl_channel_credentials: grpc.ChannelCredentials = None,
client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None,
quota_project_id: Optional[str] = None,
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
) -> None:
Expand Down Expand Up @@ -97,6 +98,10 @@ def __init__(
``api_mtls_endpoint`` is None.
ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials
for grpc channel. It is ignored if ``channel`` is provided.
client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]):
A callback to provide client certificate bytes and private key bytes,
both in PEM format. It is used to configure mutual TLS channel. It is
ignored if ``channel`` or ``ssl_channel_credentials`` is provided.
quota_project_id (Optional[str]): An optional project to use for billing
and quota.
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
Expand All @@ -113,6 +118,11 @@ def __init__(
"""
self._ssl_channel_credentials = ssl_channel_credentials

if api_mtls_endpoint:
warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning)
if client_cert_source:
warnings.warn("client_cert_source is deprecated", DeprecationWarning)

if channel:
# Sanity check: Ensure that channel and credentials are not both
# provided.
Expand All @@ -122,11 +132,6 @@ def __init__(
self._grpc_channel = channel
self._ssl_channel_credentials = None
elif api_mtls_endpoint:
warnings.warn(
"api_mtls_endpoint and client_cert_source are deprecated",
DeprecationWarning,
)

host = (
api_mtls_endpoint
if ":" in api_mtls_endpoint
Expand Down Expand Up @@ -170,12 +175,18 @@ def __init__(
scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id
)

if client_cert_source_for_mtls and not ssl_channel_credentials:
cert, key = client_cert_source_for_mtls()
self._ssl_channel_credentials = grpc.ssl_channel_credentials(
certificate_chain=cert, private_key=key
)

# create a new channel. The provided one is ignored.
self._grpc_channel = type(self).create_channel(
host,
credentials=credentials,
credentials_file=credentials_file,
ssl_credentials=ssl_channel_credentials,
ssl_credentials=self._ssl_channel_credentials,
scopes=scopes or self.AUTH_SCOPES,
quota_project_id=quota_project_id,
options=[
Expand Down
Expand Up @@ -111,6 +111,7 @@ def __init__(
api_mtls_endpoint: str = None,
client_cert_source: Callable[[], Tuple[bytes, bytes]] = None,
ssl_channel_credentials: grpc.ChannelCredentials = None,
client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None,
quota_project_id=None,
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
) -> None:
Expand Down Expand Up @@ -142,6 +143,10 @@ def __init__(
``api_mtls_endpoint`` is None.
ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials
for grpc channel. It is ignored if ``channel`` is provided.
client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]):
A callback to provide client certificate bytes and private key bytes,
both in PEM format. It is used to configure mutual TLS channel. It is
ignored if ``channel`` or ``ssl_channel_credentials`` is provided.
quota_project_id (Optional[str]): An optional project to use for billing
and quota.
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
Expand All @@ -158,6 +163,11 @@ def __init__(
"""
self._ssl_channel_credentials = ssl_channel_credentials

if api_mtls_endpoint:
warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning)
if client_cert_source:
warnings.warn("client_cert_source is deprecated", DeprecationWarning)

if channel:
# Sanity check: Ensure that channel and credentials are not both
# provided.
Expand All @@ -167,11 +177,6 @@ def __init__(
self._grpc_channel = channel
self._ssl_channel_credentials = None
elif api_mtls_endpoint:
warnings.warn(
"api_mtls_endpoint and client_cert_source are deprecated",
DeprecationWarning,
)

host = (
api_mtls_endpoint
if ":" in api_mtls_endpoint
Expand Down Expand Up @@ -215,12 +220,18 @@ def __init__(
scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id
)

if client_cert_source_for_mtls and not ssl_channel_credentials:
cert, key = client_cert_source_for_mtls()
self._ssl_channel_credentials = grpc.ssl_channel_credentials(
certificate_chain=cert, private_key=key
)

# create a new channel. The provided one is ignored.
self._grpc_channel = type(self).create_channel(
host,
credentials=credentials,
credentials_file=credentials_file,
ssl_credentials=ssl_channel_credentials,
ssl_credentials=self._ssl_channel_credentials,
scopes=scopes or self.AUTH_SCOPES,
quota_project_id=quota_project_id,
options=[
Expand Down