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 if not hasattr(self, "_ignore_credentials"):
88 self._ignore_credentials: bool = False
89
90 # If no credentials are provided, then determine the appropriate
91 # defaults.
92 if credentials and credentials_file:
93 raise core_exceptions.DuplicateCredentialArgs(
94 "'credentials_file' and 'credentials' are mutually exclusive"
95 )
96
97 if credentials_file is not None:
98 credentials, _ = google.auth.load_credentials_from_file(
99 credentials_file, **scopes_kwargs, quota_project_id=quota_project_id
100 )
101 elif credentials is None and not self._ignore_credentials:
102 credentials, _ = google.auth.default(
103 **scopes_kwargs, quota_project_id=quota_project_id
104 )
105 # Don't apply audience if the credentials file passed from user.
106 if hasattr(credentials, "with_gdch_audience"):
107 credentials = credentials.with_gdch_audience(
108 api_audience if api_audience else host
109 )
110
111 # If the credentials are service account credentials, then always try to use self signed JWT.
112 if (
113 always_use_jwt_access
114 and isinstance(credentials, service_account.Credentials)
115 and hasattr(service_account.Credentials, "with_always_use_jwt_access")
116 ):
117 credentials = credentials.with_always_use_jwt_access(True)
118
119 # Save the credentials.
120 self._credentials = credentials
121
122 # Save the hostname. Default to port 443 (HTTPS) if none is specified.
123 if ":" not in host:
124 host += ":443"
125 self._host = host
126
127 @property
128 def host(self):
129 return self._host
130
131 def _prep_wrapped_messages(self, client_info):
132 # Precompute the wrapped methods.
133 self._wrapped_methods = {
134 self.list_secrets: gapic_v1.method.wrap_method(
135 self.list_secrets,
136 default_timeout=60.0,
137 client_info=client_info,
138 ),
139 self.create_secret: gapic_v1.method.wrap_method(
140 self.create_secret,
141 default_timeout=60.0,
142 client_info=client_info,
143 ),
144 self.add_secret_version: gapic_v1.method.wrap_method(
145 self.add_secret_version,
146 default_timeout=60.0,
147 client_info=client_info,
148 ),
149 self.get_secret: gapic_v1.method.wrap_method(
150 self.get_secret,
151 default_timeout=60.0,
152 client_info=client_info,
153 ),
154 self.update_secret: gapic_v1.method.wrap_method(
155 self.update_secret,
156 default_timeout=60.0,
157 client_info=client_info,
158 ),
159 self.delete_secret: gapic_v1.method.wrap_method(
160 self.delete_secret,
161 default_timeout=60.0,
162 client_info=client_info,
163 ),
164 self.list_secret_versions: gapic_v1.method.wrap_method(
165 self.list_secret_versions,
166 default_timeout=60.0,
167 client_info=client_info,
168 ),
169 self.get_secret_version: gapic_v1.method.wrap_method(
170 self.get_secret_version,
171 default_timeout=60.0,
172 client_info=client_info,
173 ),
174 self.access_secret_version: gapic_v1.method.wrap_method(
175 self.access_secret_version,
176 default_retry=retries.Retry(
177 initial=1.0,
178 maximum=60.0,
179 multiplier=1.3,
180 predicate=retries.if_exception_type(
181 core_exceptions.ServiceUnavailable,
182 core_exceptions.Unknown,
183 ),
184 deadline=60.0,
185 ),
186 default_timeout=60.0,
187 client_info=client_info,
188 ),
189 self.disable_secret_version: gapic_v1.method.wrap_method(
190 self.disable_secret_version,
191 default_timeout=60.0,
192 client_info=client_info,
193 ),
194 self.enable_secret_version: gapic_v1.method.wrap_method(
195 self.enable_secret_version,
196 default_timeout=60.0,
197 client_info=client_info,
198 ),
199 self.destroy_secret_version: gapic_v1.method.wrap_method(
200 self.destroy_secret_version,
201 default_timeout=60.0,
202 client_info=client_info,
203 ),
204 self.set_iam_policy: gapic_v1.method.wrap_method(
205 self.set_iam_policy,
206 default_timeout=60.0,
207 client_info=client_info,
208 ),
209 self.get_iam_policy: gapic_v1.method.wrap_method(
210 self.get_iam_policy,
211 default_timeout=60.0,
212 client_info=client_info,
213 ),
214 self.test_iam_permissions: gapic_v1.method.wrap_method(
215 self.test_iam_permissions,
216 default_timeout=60.0,
217 client_info=client_info,
218 ),
219 }
220
221 def close(self):
222 """Closes resources associated with the transport.
223
224 .. warning::
225 Only call this method if the transport is NOT shared
226 with other clients - this may cause errors in other clients!
227 """
228 raise NotImplementedError()
229
230 @property
231 def list_secrets(
232 self,
233 ) -> Callable[
234 [service.ListSecretsRequest],
235 Union[service.ListSecretsResponse, Awaitable[service.ListSecretsResponse]],
236 ]:
237 raise NotImplementedError()
238
239 @property
240 def create_secret(
241 self,
242 ) -> Callable[
243 [service.CreateSecretRequest],
244 Union[resources.Secret, Awaitable[resources.Secret]],
245 ]:
246 raise NotImplementedError()
247
248 @property
249 def add_secret_version(
250 self,
251 ) -> Callable[
252 [service.AddSecretVersionRequest],
253 Union[resources.SecretVersion, Awaitable[resources.SecretVersion]],
254 ]:
255 raise NotImplementedError()
256
257 @property
258 def get_secret(
259 self,
260 ) -> Callable[
261 [service.GetSecretRequest], Union[resources.Secret, Awaitable[resources.Secret]]
262 ]:
263 raise NotImplementedError()
264
265 @property
266 def update_secret(
267 self,
268 ) -> Callable[
269 [service.UpdateSecretRequest],
270 Union[resources.Secret, Awaitable[resources.Secret]],
271 ]:
272 raise NotImplementedError()
273
274 @property
275 def delete_secret(
276 self,
277 ) -> Callable[
278 [service.DeleteSecretRequest],
279 Union[empty_pb2.Empty, Awaitable[empty_pb2.Empty]],
280 ]:
281 raise NotImplementedError()
282
283 @property
284 def list_secret_versions(
285 self,
286 ) -> Callable[
287 [service.ListSecretVersionsRequest],
288 Union[
289 service.ListSecretVersionsResponse,
290 Awaitable[service.ListSecretVersionsResponse],
291 ],
292 ]:
293 raise NotImplementedError()
294
295 @property
296 def get_secret_version(
297 self,
298 ) -> Callable[
299 [service.GetSecretVersionRequest],
300 Union[resources.SecretVersion, Awaitable[resources.SecretVersion]],
301 ]:
302 raise NotImplementedError()
303
304 @property
305 def access_secret_version(
306 self,
307 ) -> Callable[
308 [service.AccessSecretVersionRequest],
309 Union[
310 service.AccessSecretVersionResponse,
311 Awaitable[service.AccessSecretVersionResponse],
312 ],
313 ]:
314 raise NotImplementedError()
315
316 @property
317 def disable_secret_version(
318 self,
319 ) -> Callable[
320 [service.DisableSecretVersionRequest],
321 Union[resources.SecretVersion, Awaitable[resources.SecretVersion]],
322 ]:
323 raise NotImplementedError()
324
325 @property
326 def enable_secret_version(
327 self,
328 ) -> Callable[
329 [service.EnableSecretVersionRequest],
330 Union[resources.SecretVersion, Awaitable[resources.SecretVersion]],
331 ]:
332 raise NotImplementedError()
333
334 @property
335 def destroy_secret_version(
336 self,
337 ) -> Callable[
338 [service.DestroySecretVersionRequest],
339 Union[resources.SecretVersion, Awaitable[resources.SecretVersion]],
340 ]:
341 raise NotImplementedError()
342
343 @property
344 def set_iam_policy(
345 self,
346 ) -> Callable[
347 [iam_policy_pb2.SetIamPolicyRequest],
348 Union[policy_pb2.Policy, Awaitable[policy_pb2.Policy]],
349 ]:
350 raise NotImplementedError()
351
352 @property
353 def get_iam_policy(
354 self,
355 ) -> Callable[
356 [iam_policy_pb2.GetIamPolicyRequest],
357 Union[policy_pb2.Policy, Awaitable[policy_pb2.Policy]],
358 ]:
359 raise NotImplementedError()
360
361 @property
362 def test_iam_permissions(
363 self,
364 ) -> Callable[
365 [iam_policy_pb2.TestIamPermissionsRequest],
366 Union[
367 iam_policy_pb2.TestIamPermissionsResponse,
368 Awaitable[iam_policy_pb2.TestIamPermissionsResponse],
369 ],
370 ]:
371 raise NotImplementedError()
372
373 @property
374 def kind(self) -> str:
375 raise NotImplementedError()
376
377
378__all__ = ("SecretManagerServiceTransport",)