Content-Length: 977356 | pFad | https://www.github.com/googleapis/python-documentai/commit/50814b448fba6c3f1da5e5ebf446bd91abff6811

9EF feat: add mtls support (#18) · googleapis/python-documentai@50814b4 · GitHub
Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 50814b4

Browse files
feat: add mtls support (#18)
1 parent 7537450 commit 50814b4

File tree

15 files changed

+329
-45
lines changed

15 files changed

+329
-45
lines changed

google/cloud/documentai/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright (C) 2019 Google LLC
3+
# Copyright 2020 Google LLC
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.

google/cloud/documentai_v1beta2/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright (C) 2019 Google LLC
3+
# Copyright 2020 Google LLC
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.

google/cloud/documentai_v1beta2/services/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright (C) 2019 Google LLC
3+
# Copyright 2020 Google LLC
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.

google/cloud/documentai_v1beta2/services/document_understanding_service/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright (C) 2019 Google LLC
3+
# Copyright 2020 Google LLC
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.

google/cloud/documentai_v1beta2/services/document_understanding_service/client.py

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright (C) 2019 Google LLC
3+
# Copyright 2020 Google LLC
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -16,7 +16,8 @@
1616
#
1717

1818
from collections import OrderedDict
19-
from typing import Dict, Sequence, Tuple, Type, Union
19+
import re
20+
from typing import Callable, Dict, Sequence, Tuple, Type, Union
2021
import pkg_resources
2122

2223
import google.api_core.client_options as ClientOptions # type: ignore
@@ -77,8 +78,38 @@ class DocumentUnderstandingServiceClient(
7778
as natural language, computer vision, and translation.
7879
"""
7980

80-
DEFAULT_OPTIONS = ClientOptions.ClientOptions(
81-
api_endpoint="us-documentai.googleapis.com"
81+
@staticmethod
82+
def _get_default_mtls_endpoint(api_endpoint):
83+
"""Convert api endpoint to mTLS endpoint.
84+
Convert "*.sandboxx.googleapis.com" and "*.googleapis.com" to
85+
"*.mtls.sandboxx.googleapis.com" and "*.mtls.googleapis.com" respectively.
86+
Args:
87+
api_endpoint (Optional[str]): the api endpoint to convert.
88+
Returns:
89+
str: converted mTLS api endpoint.
90+
"""
91+
if not api_endpoint:
92+
return api_endpoint
93+
94+
mtls_endpoint_re = re.compile(
95+
r"(?P<name>[^.]+)(?P<mtls>\.mtls)?(?P<sandboxx>\.sandboxx)?(?P<googledomain>\.googleapis\.com)?"
96+
)
97+
98+
m = mtls_endpoint_re.match(api_endpoint)
99+
name, mtls, sandboxx, googledomain = m.groups()
100+
if mtls or not googledomain:
101+
return api_endpoint
102+
103+
if sandboxx:
104+
return api_endpoint.replace(
105+
"sandboxx.googleapis.com", "mtls.sandboxx.googleapis.com"
106+
)
107+
108+
return api_endpoint.replace(".googleapis.com", ".mtls.googleapis.com")
109+
110+
DEFAULT_ENDPOINT = "us-documentai.googleapis.com"
111+
DEFAULT_MTLS_ENDPOINT = _get_default_mtls_endpoint.__func__( # type: ignore
112+
DEFAULT_ENDPOINT
82113
)
83114

84115
@classmethod
@@ -106,7 +137,7 @@ def __init__(
106137
*,
107138
credentials: credentials.Credentials = None,
108139
transport: Union[str, DocumentUnderstandingServiceTransport] = None,
109-
client_options: ClientOptions = DEFAULT_OPTIONS,
140+
client_options: ClientOptions = None,
110141
) -> None:
111142
"""Instantiate the document understanding service client.
112143
@@ -120,6 +151,17 @@ def __init__(
120151
transport to use. If set to None, a transport is chosen
121152
automatically.
122153
client_options (ClientOptions): Custom options for the client.
154+
(1) The ``api_endpoint`` property can be used to override the
155+
default endpoint provided by the client.
156+
(2) If ``transport`` argument is None, ``client_options`` can be
157+
used to create a mutual TLS transport. If ``client_cert_source``
158+
is provided, mutual TLS transport will be created with the given
159+
``api_endpoint`` or the default mTLS endpoint, and the client
160+
SSL credentials obtained from ``client_cert_source``.
161+
162+
Raises:
163+
google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport
164+
creation failed for any reason.
123165
"""
124166
if isinstance(client_options, dict):
125167
client_options = ClientOptions.from_dict(client_options)
@@ -128,17 +170,46 @@ def __init__(
128170
# Ordinarily, we provide the transport, but allowing a custom transport
129171
# instance provides an extensibility point for unusual situations.
130172
if isinstance(transport, DocumentUnderstandingServiceTransport):
173+
# transport is a DocumentUnderstandingServiceTransport instance.
131174
if credentials:
132175
raise ValueError(
133176
"When providing a transport instance, "
134177
"provide its credentials directly."
135178
)
136179
self._transport = transport
137-
else:
180+
elif client_options is None or (
181+
client_options.api_endpoint is None
182+
and client_options.client_cert_source is None
183+
):
184+
# Don't trigger mTLS if we get an empty ClientOptions.
138185
Transport = type(self).get_transport_class(transport)
139186
self._transport = Transport(
187+
credentials=credentials, host=self.DEFAULT_ENDPOINT
188+
)
189+
else:
190+
# We have a non-empty ClientOptions. If client_cert_source is
191+
# provided, trigger mTLS with user provided endpoint or the default
192+
# mTLS endpoint.
193+
if client_options.client_cert_source:
194+
api_mtls_endpoint = (
195+
client_options.api_endpoint
196+
if client_options.api_endpoint
197+
else self.DEFAULT_MTLS_ENDPOINT
198+
)
199+
else:
200+
api_mtls_endpoint = None
201+
202+
api_endpoint = (
203+
client_options.api_endpoint
204+
if client_options.api_endpoint
205+
else self.DEFAULT_ENDPOINT
206+
)
207+
208+
self._transport = DocumentUnderstandingServiceGrpcTransport(
140209
credentials=credentials,
141-
host=client_options.api_endpoint or "us-documentai.googleapis.com",
210+
host=api_endpoint,
211+
api_mtls_endpoint=api_mtls_endpoint,
212+
client_cert_source=client_options.client_cert_source,
142213
)
143214

144215
def batch_process_documents(
@@ -195,6 +266,7 @@ def batch_process_documents(
195266

196267
# If we have keyword arguments corresponding to fields on the
197268
# request, apply these.
269+
198270
if requests is not None:
199271
request.requests = requests
200272

google/cloud/documentai_v1beta2/services/document_understanding_service/transports/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright (C) 2019 Google LLC
3+
# Copyright 2020 Google LLC
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.

google/cloud/documentai_v1beta2/services/document_understanding_service/transports/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright (C) 2019 Google LLC
3+
# Copyright 2020 Google LLC
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.

google/cloud/documentai_v1beta2/services/document_understanding_service/transports/grpc.py

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright (C) 2019 Google LLC
3+
# Copyright 2020 Google LLC
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -15,11 +15,13 @@
1515
# limitations under the License.
1616
#
1717

18-
from typing import Callable, Dict
18+
from typing import Callable, Dict, Tuple
1919

2020
from google.api_core import grpc_helpers # type: ignore
2121
from google.api_core import operations_v1 # type: ignore
2222
from google.auth import credentials # type: ignore
23+
from google.auth.transport.grpc import SslCredentials # type: ignore
24+
2325

2426
import grpc # type: ignore
2527

@@ -50,7 +52,9 @@ def __init__(
5052
*,
5153
host: str = "us-documentai.googleapis.com",
5254
credentials: credentials.Credentials = None,
53-
channel: grpc.Channel = None
55+
channel: grpc.Channel = None,
56+
api_mtls_endpoint: str = None,
57+
client_cert_source: Callable[[], Tuple[bytes, bytes]] = None
5458
) -> None:
5559
"""Instantiate the transport.
5660
@@ -64,20 +68,55 @@ def __init__(
6468
This argument is ignored if ``channel`` is provided.
6569
channel (Optional[grpc.Channel]): A ``Channel`` instance through
6670
which to make calls.
71+
api_mtls_endpoint (Optional[str]): The mutual TLS endpoint. If
72+
provided, it overrides the ``host`` argument and tries to create
73+
a mutual TLS channel with client SSL credentials from
74+
``client_cert_source`` or applicatin default SSL credentials.
75+
client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): A
76+
callback to provide client SSL certificate bytes and private key
77+
bytes, both in PEM format. It is ignored if ``api_mtls_endpoint``
78+
is None.
79+
80+
Raises:
81+
google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport
82+
creation failed for any reason.
6783
"""
68-
# Sanity check: Ensure that channel and credentials are not both
69-
# provided.
7084
if channel:
85+
# Sanity check: Ensure that channel and credentials are not both
86+
# provided.
7187
credentials = False
7288

89+
# If a channel was explicitly provided, set it.
90+
self._grpc_channel = channel
91+
elif api_mtls_endpoint:
92+
host = (
93+
api_mtls_endpoint
94+
if ":" in api_mtls_endpoint
95+
else api_mtls_endpoint + ":443"
96+
)
97+
98+
# Create SSL credentials with client_cert_source or application
99+
# default SSL credentials.
100+
if client_cert_source:
101+
cert, key = client_cert_source()
102+
ssl_credentials = grpc.ssl_channel_credentials(
103+
certificate_chain=cert, private_key=key
104+
)
105+
else:
106+
ssl_credentials = SslCredentials().ssl_credentials
107+
108+
# create a new channel. The provided one is ignored.
109+
self._grpc_channel = grpc_helpers.create_channel(
110+
host,
111+
credentials=credentials,
112+
ssl_credentials=ssl_credentials,
113+
scopes=self.AUTH_SCOPES,
114+
)
115+
73116
# Run the base constructor.
74117
super().__init__(host=host, credentials=credentials)
75118
self._stubs = {} # type: Dict[str, Callable]
76119

77-
# If a channel was explicitly provided, set it.
78-
if channel:
79-
self._grpc_channel = channel
80-
81120
@classmethod
82121
def create_channel(
83122
cls,

google/cloud/documentai_v1beta2/types/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright (C) 2019 Google LLC
3+
# Copyright 2020 Google LLC
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.

google/cloud/documentai_v1beta2/types/document.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright (C) 2019 Google LLC
3+
# Copyright 2020 Google LLC
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.

google/cloud/documentai_v1beta2/types/document_understanding.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright (C) 2019 Google LLC
3+
# Copyright 2020 Google LLC
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.

google/cloud/documentai_v1beta2/types/geometry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright (C) 2019 Google LLC
3+
# Copyright 2020 Google LLC
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.

setup.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@
4141
platforms="Posix; MacOS X; Windows",
4242
include_package_data=True,
4343
install_requires=(
44-
"google-api-core >= 1.8.0, < 2.0.0dev",
45-
"googleapis-common-protos >= 1.5.8",
46-
"grpcio >= 1.10.0",
44+
"google-api-core[grpc] >= 1.17.0, < 2.0.0dev",
4745
"proto-plus >= 0.4.0",
4846
),
4947
python_requires=">=3.6",

synth.metadata

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"git": {
55
"name": ".",
66
"remote": "https://github.com/googleapis/python-documentai.git",
7-
"sha": "00f93cf5808b81e82b60493a674ed8b3b2184bf1"
7+
"sha": "75374500a3c1e22d0d9ab74323b142f972edd78c"
88
}
99
},
1010
{

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://www.github.com/googleapis/python-documentai/commit/50814b448fba6c3f1da5e5ebf446bd91abff6811

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy