如何配置 Google Workspace 提醒中心以向 PubSub 主题发布提醒?

问题描述 投票:0回答:2

我正在构建一个通知系统,当员工采取触发 DLP 规则的操作时,该系统会通知处于松弛状态的员工。

我想要达到的理想流程是:

员工采取行动 > 规则触发 > 警报发布到 PubSub 主题 > 触发呈现松弛消息并将其发送给用户的云函数。

我遇到的问题是让 API 警报中心将警报发布到 PubSub 主题。 我过去曾使用 Gmail API 做过类似的事情,当特定帐户收到电子邮件时发布到主题。并且有一个 watch 请求配置帐户以发布到主题。

我查看了警报中心 API 文档,没有找到监视请求或任何类似的操作。

有办法实现这一点吗?或者这根本就是不可能的?

我通过 Google 警报中心 API 参考 和警报中心控制台搜索了用于配置发布到 pubsub 主题或 Webhook 的任何选项。

publish-subscribe google-cloud-pubsub google-admin-sdk google-alerts google-alert-center-api
2个回答
1
投票

经过几个小时的反复试验,我找到了解决方案。

我们必须通过 updateSetting 来更新设置,并在正文中发送 settings 对象。

这里有一些 python 代码,使用具有域范围委托的服务帐户来执行此操作:

from google.oauth2 import service_account
from googleapiclient.discovery import build
import json

def main():

    # Authenticate the service account
    scopes = ['https://www.googleapis.com/auth/apps.alerts']
    admin_to_impersonate = '******'
    credentials = service_account.Credentials.from_service_account_file(
        'client_secrets.json', scopes=scopes, subject=admin_to_impersonate
        )
    
    # Build the service
    service = build('alertcenter', 'v1beta1', credentials=credentials)

    settings = {
        'notifications': [
            {
                'cloudPubsubTopic': {
                    'topicName': '<YOUR TOPIC NAME>',
                    'payloadFormat': 'JSON'
                }
            }
        ]
    }

    # Execute a updateSetting request
    response = service.v1beta1().updateSettings(body=settings).execute()
    print(response)

if __name__ == '__main__':
    main()

0
投票

谢谢安德烈,你的方法对我有用。

对于希望在 Cloud Functions 上部署或调用服务帐户密钥 (

'client_secrets.json'
) 不够安全的其他任何地方的任何人,以下是我的做法:

首先参考并遵循 https://github.com/GoogleCloudPlatform/professional-services/blob/e79c75a14eb20640afbcab3ee110eccdd8378720/examples/gce-to-adminsdk/README.md中的所有步骤。 忽略有关管理员组读取权限的位,而是创建一个具有对警报中心的所有访问权限的自定义管理员角色,并将其分配给虚拟用户帐户。

确保您的 VPC 子网启用了 Private Google Access(或者至少与警报中心端点有一定的连接)。我仅在 Cloud Function 中允许内部流量,并使用同一子网中的计算实例对其进行测试。

最后,无论出于何种原因,

from googleapiclient.discovery import build
导致了 Cloud Functions 中的部署错误。您可以使用
from googleapiclient import discovery
并将
build()
更改为
discovery.build()

© www.soinside.com 2019 - 2024. All rights reserved.