1# -*- coding: utf-8 -*-
2# Copyright 2024 Google LLC
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16import abc
17from typing import Awaitable, Callable, Dict, Optional, Sequence, Union
18
19import google.api_core
20from google.api_core import exceptions as core_exceptions
21from google.api_core import gapic_v1
22from google.api_core import retry as retries
23import google.auth # type: ignore
24from google.auth import credentials as ga_credentials # type: ignore
25from google.iam.v1 import iam_policy_pb2 # type: ignore
26from google.iam.v1 import policy_pb2 # type: ignore
27from google.oauth2 import service_account # type: ignore
28from google.protobuf import empty_pb2 # type: ignore
29
30from google.cloud.secretmanager_v1beta1 import gapic_version as package_version
31from google.cloud.secretmanager_v1beta1.types import resources, service
32
33DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
34 gapic_version=package_version.__version__
35)
36
37
38class SecretManagerServiceTransport(abc.ABC):
39 """Abstract transport class for SecretManagerService."""
40
41 AUTH_SCOPES = ("https://www.googleapis.com/auth/cloud-platform",)
42
43 DEFAULT_HOST: str = "secretmanager.googleapis.com"
44
45 def __init__(
46 self,
47 *,
48 host: str = DEFAULT_HOST,
49 credentials: Optional[ga_credentials.Credentials] = None,
50 credentials_file: Optional[str] = None,
51 scopes: Optional[Sequence[str]] = None,
52 quota_project_id: Optional[str] = None,
53 client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
54 always_use_jwt_access: Optional[bool] = False,
55 api_audience: Optional[str] = None,
56 **kwargs,
57 ) -> None:
58 """Instantiate the transport.
59
60 Args:
61 host (Optional[str]):
62 The hostname to connect to (default: 'secretmanager.googleapis.com').
63 credentials (Optional[google.auth.credentials.Credentials]): The
64 authorization credentials to attach to requests. These
65 credentials identify the application to the service; if none
66 are specified, the client will attempt to ascertain the
67 credentials from the environment.
68 credentials_file (Optional[str]): A file with credentials that can
69 be loaded with :func:`google.auth.load_credentials_from_file`.
70 This argument is mutually exclusive with credentials.
71 scopes (Optional[Sequence[str]]): A list of scopes.
72 quota_project_id (Optional[str]): An optional project to use for billing
73 and quota.
74 client_info (google.api_core.gapic_v1.client_info.ClientInfo):
75 The client info used to send a user-agent string along with
76 API requests. If ``None``, then default info will be used.
77 Generally, you only need to set this if you're developing
78 your own client library.
79 always_use_jwt_access (Optional[bool]): Whether self signed JWT should
80 be used for service account credentials.
81 """
82
83 scopes_kwargs = {"scopes": scopes, "default_scopes": self.AUTH_SCOPES}
84
85 # Save the scopes.
86 self._scopes = scopes
87
88 # If no credentials are provided, then determine the appropriate
89 # defaults.
90 if credentials and credentials_file:
91 raise core_exceptions.DuplicateCredentialArgs(
92 "'credentials_file' and 'credentials' are mutually exclusive"
93 )
94
95 if credentials_file is not None:
96 credentials, _ = google.auth.load_credentials_from_file(
97 credentials_file, **scopes_kwargs, quota_project_id=quota_project_id
98 )
99 elif credentials is None:
100 credentials, _ = google.auth.default(
101 **scopes_kwargs, quota_project_id=quota_project_id
102 )
103 # Don't apply audience if the credentials file passed from user.
104 if hasattr(credentials, "with_gdch_audience"):
105 credentials = credentials.with_gdch_audience(
106 api_audience if api_audience else host
107 )
108
109 # If the credentials are service account credentials, then always try to use self signed JWT.
110 if (
111 always_use_jwt_access
112 and isinstance(credentials, service_account.Credentials)
113 and hasattr(service_account.Credentials, "with_always_use_jwt_access")
114 ):
115 credentials = credentials.with_always_use_jwt_access(True)
116
117 # Save the credentials.
118 self._credentials = credentials
119
120 # Save the hostname. Default to port 443 (HTTPS) if none is specified.
121 if ":" not in host:
122 host += ":443"
123 self._host = host
124
125 @property
126 def host(self):
127 return self._host
128
129 def _prep_wrapped_messages(self, client_info):
130 # Precompute the wrapped methods.
131 self._wrapped_methods = {
132 self.list_secrets: gapic_v1.method.wrap_method(
133 self.list_secrets,
134 default_timeout=60.0,
135 client_info=client_info,
136 ),
137 self.create_secret: gapic_v1.method.wrap_method(
138 self.create_secret,
139 default_timeout=60.0,
140 client_info=client_info,
141 ),
142 self.add_secret_version: gapic_v1.method.wrap_method(
143 self.add_secret_version,
144 default_timeout=60.0,
145 client_info=client_info,
146 ),
147 self.get_secret: gapic_v1.method.wrap_method(
148 self.get_secret,
149 default_timeout=60.0,
150 client_info=client_info,
151 ),
152 self.update_secret: gapic_v1.method.wrap_method(
153 self.update_secret,
154 default_timeout=60.0,
155 client_info=client_info,
156 ),
157 self.delete_secret: gapic_v1.method.wrap_method(
158 self.delete_secret,
159 default_timeout=60.0,
160 client_info=client_info,
161 ),
162 self.list_secret_versions: gapic_v1.method.wrap_method(
163 self.list_secret_versions,
164 default_timeout=60.0,
165 client_info=client_info,
166 ),
167 self.get_secret_version: gapic_v1.method.wrap_method(
168 self.get_secret_version,
169 default_timeout=60.0,
170 client_info=client_info,
171 ),
172 self.access_secret_version: gapic_v1.method.wrap_method(
173 self.access_secret_version,
174 default_retry=retries.Retry(
175 initial=1.0,
176 maximum=60.0,
177 multiplier=1.3,
178 predicate=retries.if_exception_type(
179 core_exceptions.ServiceUnavailable,
180 core_exceptions.Unknown,
181 ),
182 deadline=60.0,
183 ),
184 default_timeout=60.0,
185 client_info=client_info,
186 ),
187 self.disable_secret_version: gapic_v1.method.wrap_method(
188 self.disable_secret_version,
189 default_timeout=60.0,
190 client_info=client_info,
191 ),
192 self.enable_secret_version: gapic_v1.method.wrap_method(
193 self.enable_secret_version,
194 default_timeout=60.0,
195 client_info=client_info,
196 ),
197 self.destroy_secret_version: gapic_v1.method.wrap_method(
198 self.destroy_secret_version,
199 default_timeout=60.0,
200 client_info=client_info,
201 ),
202 self.set_iam_policy: gapic_v1.method.wrap_method(
203 self.set_iam_policy,
204 default_timeout=60.0,
205 client_info=client_info,
206 ),
207 self.get_iam_policy: gapic_v1.method.wrap_method(
208 self.get_iam_policy,
209 default_timeout=60.0,
210 client_info=client_info,
211 ),
212 self.test_iam_permissions: gapic_v1.method.wrap_method(
213 self.test_iam_permissions,
214 default_timeout=60.0,
215 client_info=client_info,
216 ),
217 }
218
219 def close(self):
220 """Closes resources associated with the transport.
221
222 .. warning::
223 Only call this method if the transport is NOT shared
224 with other clients - this may cause errors in other clients!
225 """
226 raise NotImplementedError()
227
228 @property
229 def list_secrets(
230 self,
231 ) -> Callable[
232 [service.ListSecretsRequest],
233 Union[service.ListSecretsResponse, Awaitable[service.ListSecretsResponse]],
234 ]:
235 raise NotImplementedError()
236
237 @property
238 def create_secret(
239 self,
240 ) -> Callable[
241 [service.CreateSecretRequest],
242 Union[resources.Secret, Awaitable[resources.Secret]],
243 ]:
244 raise NotImplementedError()
245
246 @property
247 def add_secret_version(
248 self,
249 ) -> Callable[
250 [service.AddSecretVersionRequest],
251 Union[resources.SecretVersion, Awaitable[resources.SecretVersion]],
252 ]:
253 raise NotImplementedError()
254
255 @property
256 def get_secret(
257 self,
258 ) -> Callable[
259 [service.GetSecretRequest], Union[resources.Secret, Awaitable[resources.Secret]]
260 ]:
261 raise NotImplementedError()
262
263 @property
264 def update_secret(
265 self,
266 ) -> Callable[
267 [service.UpdateSecretRequest],
268 Union[resources.Secret, Awaitable[resources.Secret]],
269 ]:
270 raise NotImplementedError()
271
272 @property
273 def delete_secret(
274 self,
275 ) -> Callable[
276 [service.DeleteSecretRequest],
277 Union[empty_pb2.Empty, Awaitable[empty_pb2.Empty]],
278 ]:
279 raise NotImplementedError()
280
281 @property
282 def list_secret_versions(
283 self,
284 ) -> Callable[
285 [service.ListSecretVersionsRequest],
286 Union[
287 service.ListSecretVersionsResponse,
288 Awaitable[service.ListSecretVersionsResponse],
289 ],
290 ]:
291 raise NotImplementedError()
292
293 @property
294 def get_secret_version(
295 self,
296 ) -> Callable[
297 [service.GetSecretVersionRequest],
298 Union[resources.SecretVersion, Awaitable[resources.SecretVersion]],
299 ]:
300 raise NotImplementedError()
301
302 @property
303 def access_secret_version(
304 self,
305 ) -> Callable[
306 [service.AccessSecretVersionRequest],
307 Union[
308 service.AccessSecretVersionResponse,
309 Awaitable[service.AccessSecretVersionResponse],
310 ],
311 ]:
312 raise NotImplementedError()
313
314 @property
315 def disable_secret_version(
316 self,
317 ) -> Callable[
318 [service.DisableSecretVersionRequest],
319 Union[resources.SecretVersion, Awaitable[resources.SecretVersion]],
320 ]:
321 raise NotImplementedError()
322
323 @property
324 def enable_secret_version(
325 self,
326 ) -> Callable[
327 [service.EnableSecretVersionRequest],
328 Union[resources.SecretVersion, Awaitable[resources.SecretVersion]],
329 ]:
330 raise NotImplementedError()
331
332 @property
333 def destroy_secret_version(
334 self,
335 ) -> Callable[
336 [service.DestroySecretVersionRequest],
337 Union[resources.SecretVersion, Awaitable[resources.SecretVersion]],
338 ]:
339 raise NotImplementedError()
340
341 @property
342 def set_iam_policy(
343 self,
344 ) -> Callable[
345 [iam_policy_pb2.SetIamPolicyRequest],
346 Union[policy_pb2.Policy, Awaitable[policy_pb2.Policy]],
347 ]:
348 raise NotImplementedError()
349
350 @property
351 def get_iam_policy(
352 self,
353 ) -> Callable[
354 [iam_policy_pb2.GetIamPolicyRequest],
355 Union[policy_pb2.Policy, Awaitable[policy_pb2.Policy]],
356 ]:
357 raise NotImplementedError()
358
359 @property
360 def test_iam_permissions(
361 self,
362 ) -> Callable[
363 [iam_policy_pb2.TestIamPermissionsRequest],
364 Union[
365 iam_policy_pb2.TestIamPermissionsResponse,
366 Awaitable[iam_policy_pb2.TestIamPermissionsResponse],
367 ],
368 ]:
369 raise NotImplementedError()
370
371 @property
372 def kind(self) -> str:
373 raise NotImplementedError()
374
375
376__all__ = ("SecretManagerServiceTransport",)