0

我正在尝试让用于 python 的 Google Cloud Platform Data Loss Prevention (DLP) 客户端库在 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 Client python lib,我得到:

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 lib 使用相同的 CA 没有问题。任何的想法 ?

4

2 回答 2

1

您的代理正在执行TLS Interception。这会导致 Google 库不信任您的代理在访问 Google API 端点时提供的 SSL 证书。这是一个中间人问题。

解决方案是绕过 Google API 的代理。在运行应用程序的 VPC 子网中,启用 Private Google Access。这要求默认 VPC 路由规则仍然存在(或重新创建)。

私人谷歌访问

[在下面的评论后编辑]

我添加这条评论是为了吓唬蜂蜡脱离管理。

TLS 拦截非常危险,如果他们阅读以下内容,任何合理的公司都不会实施它。

本例中的场景。我是负责公司代理的 IT 人员。该公司已实施 TLS 拦截,我控制代理。我无权访问我公司的 Google Cloud 资源。我很聪明,我非常了解 Google Cloud IAM 和 OAuth。我要入侵我的公司,因为也许我没有得到加薪(发明你自己的理由)。

我正在等待一位拥有组织或项目所有者/编辑者级别权限的经理向 Google Cloud 进行身份验证。我的代理记录了所有内容的 HTTPS 标头、正文和响应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 地址,我会去离家/工作几个小时车程的星巴克或公共图书馆,然后从那里做我的事。现在找出这个黑客的位置和对象。这会让法医专家胃灼热。

https://github.com/jhanley-com/google-cloud-shell-cli-go

注意:此问题不是 Google OAuth 或 Google Cloud 的问题。这是该公司已部署的安全问题的一个示例(TLS 拦截)。这种技术适用于我所知道的几乎所有不使用 MFA 的身份验证系统。

[结束编辑]

于 2019-09-20T16:01:14.883 回答
0

概括:

  1. 用于 python 的数据丢失防护客户端库使用 gRCP。google-cloud-dlp 使用 gRPC,而 google-cloud-bigquery 和 google-cloud-storage 依赖于 JSON-over-HTTPS 的请求库。因为它是 gRPC,所以需要设置其他环境变量:

    GRPC_DEFAULT_SSL_ROOTS_FILE_PATH=path_file.pem  
    # for debugging
    RPC_TRACE=transport_security,tsi  
    GRPC_VERBOSITY=DEBUG
    

    更多细节和链接可以在这里找到链接

  2. 这并不能解决所有问题,因为它在握手(TLS 代理)后继续失败,如此处所述链接。正如@John Hanley 所解释的那样,我们应该启用 Private Google Access,这是推荐的安全方式。这在我正在使用 API 的网络区域中还没有到位,因此代理团队添加了 SSL 绕过,它现在正在工作。我正在等待启用 Private Google Access 以进行干净且安全的设置以使用 GCP API。
于 2019-09-29T09:51:04.787 回答