如何为 python 配置 Google Cloud Platform Data Loss Prevention 客户端库以在 SSL 代理后面工作?
how to configure Google Cloud Platform Data Loss Prevention client library for python to work behind a SSL proxy?
我正在尝试让 Google Cloud Platform Data Loss Prevention (DLP) 客户端库用于 python 在 SSL 代理后面工作:
https://cloud.google.com/dlp/docs/libraries#client-libraries-usage-python
我正在使用文档中的代码片段:
# Import the client library
import google.cloud.dlp
import os
import subprocess
import json
import requests
import getpass
import urllib.parse
import logging
logging.basicConfig(level=logging.DEBUG)
# Instantiate a client.
dlp_client = google.cloud.dlp.DlpServiceClient()
# The string to inspect
content = 'Robert Frost'
# Construct the item to inspect.
item = {'value': content}
# The info types to search for in the content. Required.
info_types = [{'name': 'FIRST_NAME'}, {'name': 'LAST_NAME'}]
# The minimum likelihood to constitute a match. Optional.
min_likelihood = 'LIKELIHOOD_UNSPECIFIED'
# The maximum number of findings to report (0 = server maximum). Optional.
max_findings = 0
# Whether to include the matching string in the results. Optional.
include_quote = True
# Construct the configuration dictionary. Keys which are None may
# optionally be omitted entirely.
inspect_config = {
'info_types': info_types,
'min_likelihood': min_likelihood,
'include_quote': include_quote,
'limits': {'max_findings_per_request': max_findings},
}
# Convert the project id into a full resource id.
parent = dlp_client.project_path('my-project-id')
# Call the API.
response = dlp_client.inspect_content(parent, inspect_config, item)
# Print out the results.
if response.result.findings:
for finding in response.result.findings:
try:
print('Quote: {}'.format(finding.quote))
except AttributeError:
pass
print('Info type: {}'.format(finding.info_type.name))
# Convert likelihood value to string respresentation.
likelihood = (google.cloud.dlp.types.Finding.DESCRIPTOR
.fields_by_name['likelihood']
.enum_type.values_by_number[finding.likelihood]
.name)
print('Likelihood: {}'.format(likelihood))
else:
print('No findings.')
我还设置了以下 ENV 变量:
GOOGLE_APPLICATION_CREDENTIALS
当您不在 SSL 代理后面时,运行 没有问题。当我在代理后面工作时,我正在设置 3 个 ENV 变量:
REQUESTS_CA_BUNDLE
HTTP_PROXY
HTTPS_PROXY
通过这样的设置,其他 GCP 客户端 python 库在 SSL 代理后面工作正常,例如用于存储或 bigquery)。
对于 DLP 客户端 python 库,我得到:
E0920 12:21:49.931000000 24852 src/core/tsi/ssl_transport_security.cc:1229] Handshake failed with fatal error SSL_ERROR_SSL: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed.
DEBUG:google.api_core.retry:Retrying due to 503 Connect Failed, sleeping 0.0s ...
E0920 12:21:50.927000000 24852 src/core/tsi/ssl_transport_security.cc:1229] Handshake failed with fatal error SSL_ERROR_SSL: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed.
DEBUG:google.api_core.retry:Retrying due to 503 Connect Failed, sleeping 0.0s ...
我没有在文档中找到解释该库是否作为一个 GCP 客户端库与代理一起工作以及如何配置它以与 SSL 代理一起工作的说明。该库处于测试阶段,因此可能尚未实现。
好像跟CA证书和握手有关。 BigQuery 和 Storage Client python 库的同一 CA 没有问题。有什么想法吗?
您的代理正在执行 TLS Interception
。这会导致 Google 库不信任您的代理在访问 Google API 端点时提供的 SSL 证书。这是一个 man-in-the-middle 问题。
解决方案是 Google API 绕过代理。在您的应用程序 运行 所在的 VPC 子网中,启用私有 Google 访问。这需要默认的 VPC 路由规则仍然存在(或重新创建)。
[在下面评论后编辑]
我加这条评论是为了吓唬蜂蜡失控。
TLS 拦截非常危险,如果阅读以下内容,任何合理的公司都不会实施它。
本例中的场景。我是负责公司代理的 IT 人员。公司已经实施了 TLS 拦截,我控制了代理。我无权访问我公司的 Google 云资源。我很聪明,我非常了解 Google Cloud IAM 和 OAuth。我要黑掉我的公司,因为也许我没有得到加薪(发明你自己的理由)。
我正在等待一位拥有组织或项目 owner/editor 级别权限的经理使用 Google Cloud 进行身份验证。我的代理记录了 HTTPS headers、body 和对 https://www.googleapis.com/oauth2/v4/token
以及更多 URL 的所有内容的响应。
也许代理将日志存储在 Google Cloud Bucket 或 SAN 卷上,但没有实施可靠的授权。也许我只是一个软件工程师,发现代理日志文件随处可见或易于访问。
公司管理员登录他的 Google 帐户。我捕获返回的 OAuth 访问令牌。我现在可以在接下来的 3,600 秒内模拟组织管理员。此外,我捕获了 OAuth 刷新令牌。我现在可以随时重新创建 OAuth 访问令牌,直到撤销刷新令牌为止,而对于大多数公司来说,他们永远不会这样做。
对于怀疑者,请研究我的 Golang 项目,该项目展示了如何将 OAuth 访问令牌和刷新令牌保存到用于验证的任何 Google 帐户的文件中。我可以将此文件带回家并在没有任何身份验证的情况下获得授权。此代码将在它过期时重新创建访问令牌,让我几乎永远可以访问这些凭据被授权的任何帐户。您的内部 IT 资源永远不会知道我正在您的公司网络之外执行此操作。
注意:Stackdriver Audit 日志记录可以捕获 IP 地址,但是,身份将是我窃取的凭据。为了隐藏我的 IP 地址,我会去 home/job 几个小时车程的星巴克或 public 图书馆,然后在那里做我的事。现在找出这个黑客的位置和对象。这会让法医专家胃灼热。
https://github.com/jhanley-com/google-cloud-shell-cli-go
注意:此问题不是 Google OAuth 或 Google Cloud 的问题。这是公司部署的安全问题示例(TLS 拦截)。这种技术风格适用于我所知道的几乎所有不使用 MFA 的身份验证系统。
[结束编辑]
总结:
python 的数据丢失防护客户端库使用 gRCP。
google-cloud-dlp 使用 gRPC 而 google-cloud-bigquery 和
google-cloud-storage依赖requests库
JSON-通过 HTTPS。因为它是 gRPC,所以需要其他环境变量
设置:
GRPC_DEFAULT_SSL_ROOTS_FILE_PATH=path_file.pem
# for debugging
RPC_TRACE=transport_security,tsi
GRPC_VERBOSITY=DEBUG
可以在此处找到更多详细信息和链接 link
- 这并没有解决所有问题,因为它在之后继续失败
此处描述的握手(TLS 代理)link。还有
@John Hanley 解释说我们应该启用 Private Google 访问
相反,这是推荐和安全的方式。这还没有在
放置在我正在使用 API 的网络区域中,因此代理团队
添加了一个 SSL 绕过,它现在可以工作了。我正在等待 Private Google Access enbale 以进行干净安全的设置以使用 GCP API。
我正在尝试让 Google Cloud Platform Data Loss Prevention (DLP) 客户端库用于 python 在 SSL 代理后面工作: https://cloud.google.com/dlp/docs/libraries#client-libraries-usage-python
我正在使用文档中的代码片段:
# Import the client library
import google.cloud.dlp
import os
import subprocess
import json
import requests
import getpass
import urllib.parse
import logging
logging.basicConfig(level=logging.DEBUG)
# Instantiate a client.
dlp_client = google.cloud.dlp.DlpServiceClient()
# The string to inspect
content = 'Robert Frost'
# Construct the item to inspect.
item = {'value': content}
# The info types to search for in the content. Required.
info_types = [{'name': 'FIRST_NAME'}, {'name': 'LAST_NAME'}]
# The minimum likelihood to constitute a match. Optional.
min_likelihood = 'LIKELIHOOD_UNSPECIFIED'
# The maximum number of findings to report (0 = server maximum). Optional.
max_findings = 0
# Whether to include the matching string in the results. Optional.
include_quote = True
# Construct the configuration dictionary. Keys which are None may
# optionally be omitted entirely.
inspect_config = {
'info_types': info_types,
'min_likelihood': min_likelihood,
'include_quote': include_quote,
'limits': {'max_findings_per_request': max_findings},
}
# Convert the project id into a full resource id.
parent = dlp_client.project_path('my-project-id')
# Call the API.
response = dlp_client.inspect_content(parent, inspect_config, item)
# Print out the results.
if response.result.findings:
for finding in response.result.findings:
try:
print('Quote: {}'.format(finding.quote))
except AttributeError:
pass
print('Info type: {}'.format(finding.info_type.name))
# Convert likelihood value to string respresentation.
likelihood = (google.cloud.dlp.types.Finding.DESCRIPTOR
.fields_by_name['likelihood']
.enum_type.values_by_number[finding.likelihood]
.name)
print('Likelihood: {}'.format(likelihood))
else:
print('No findings.')
我还设置了以下 ENV 变量:
GOOGLE_APPLICATION_CREDENTIALS
当您不在 SSL 代理后面时,运行 没有问题。当我在代理后面工作时,我正在设置 3 个 ENV 变量:
REQUESTS_CA_BUNDLE
HTTP_PROXY
HTTPS_PROXY
通过这样的设置,其他 GCP 客户端 python 库在 SSL 代理后面工作正常,例如用于存储或 bigquery)。
对于 DLP 客户端 python 库,我得到:
E0920 12:21:49.931000000 24852 src/core/tsi/ssl_transport_security.cc:1229] Handshake failed with fatal error SSL_ERROR_SSL: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed.
DEBUG:google.api_core.retry:Retrying due to 503 Connect Failed, sleeping 0.0s ...
E0920 12:21:50.927000000 24852 src/core/tsi/ssl_transport_security.cc:1229] Handshake failed with fatal error SSL_ERROR_SSL: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed.
DEBUG:google.api_core.retry:Retrying due to 503 Connect Failed, sleeping 0.0s ...
我没有在文档中找到解释该库是否作为一个 GCP 客户端库与代理一起工作以及如何配置它以与 SSL 代理一起工作的说明。该库处于测试阶段,因此可能尚未实现。
好像跟CA证书和握手有关。 BigQuery 和 Storage Client python 库的同一 CA 没有问题。有什么想法吗?
您的代理正在执行 TLS Interception
。这会导致 Google 库不信任您的代理在访问 Google API 端点时提供的 SSL 证书。这是一个 man-in-the-middle 问题。
解决方案是 Google API 绕过代理。在您的应用程序 运行 所在的 VPC 子网中,启用私有 Google 访问。这需要默认的 VPC 路由规则仍然存在(或重新创建)。
[在下面评论后编辑]
我加这条评论是为了吓唬蜂蜡失控。
TLS 拦截非常危险,如果阅读以下内容,任何合理的公司都不会实施它。
本例中的场景。我是负责公司代理的 IT 人员。公司已经实施了 TLS 拦截,我控制了代理。我无权访问我公司的 Google 云资源。我很聪明,我非常了解 Google Cloud IAM 和 OAuth。我要黑掉我的公司,因为也许我没有得到加薪(发明你自己的理由)。
我正在等待一位拥有组织或项目 owner/editor 级别权限的经理使用 Google Cloud 进行身份验证。我的代理记录了 HTTPS headers、body 和对 https://www.googleapis.com/oauth2/v4/token
以及更多 URL 的所有内容的响应。
也许代理将日志存储在 Google Cloud Bucket 或 SAN 卷上,但没有实施可靠的授权。也许我只是一个软件工程师,发现代理日志文件随处可见或易于访问。
公司管理员登录他的 Google 帐户。我捕获返回的 OAuth 访问令牌。我现在可以在接下来的 3,600 秒内模拟组织管理员。此外,我捕获了 OAuth 刷新令牌。我现在可以随时重新创建 OAuth 访问令牌,直到撤销刷新令牌为止,而对于大多数公司来说,他们永远不会这样做。
对于怀疑者,请研究我的 Golang 项目,该项目展示了如何将 OAuth 访问令牌和刷新令牌保存到用于验证的任何 Google 帐户的文件中。我可以将此文件带回家并在没有任何身份验证的情况下获得授权。此代码将在它过期时重新创建访问令牌,让我几乎永远可以访问这些凭据被授权的任何帐户。您的内部 IT 资源永远不会知道我正在您的公司网络之外执行此操作。
注意:Stackdriver Audit 日志记录可以捕获 IP 地址,但是,身份将是我窃取的凭据。为了隐藏我的 IP 地址,我会去 home/job 几个小时车程的星巴克或 public 图书馆,然后在那里做我的事。现在找出这个黑客的位置和对象。这会让法医专家胃灼热。
https://github.com/jhanley-com/google-cloud-shell-cli-go
注意:此问题不是 Google OAuth 或 Google Cloud 的问题。这是公司部署的安全问题示例(TLS 拦截)。这种技术风格适用于我所知道的几乎所有不使用 MFA 的身份验证系统。
[结束编辑]
总结:
python 的数据丢失防护客户端库使用 gRCP。 google-cloud-dlp 使用 gRPC 而 google-cloud-bigquery 和 google-cloud-storage依赖requests库 JSON-通过 HTTPS。因为它是 gRPC,所以需要其他环境变量 设置:
GRPC_DEFAULT_SSL_ROOTS_FILE_PATH=path_file.pem # for debugging RPC_TRACE=transport_security,tsi GRPC_VERBOSITY=DEBUG
可以在此处找到更多详细信息和链接 link
- 这并没有解决所有问题,因为它在之后继续失败 此处描述的握手(TLS 代理)link。还有 @John Hanley 解释说我们应该启用 Private Google 访问 相反,这是推荐和安全的方式。这还没有在 放置在我正在使用 API 的网络区域中,因此代理团队 添加了一个 SSL 绕过,它现在可以工作了。我正在等待 Private Google Access enbale 以进行干净安全的设置以使用 GCP API。