AppEngine:为经过身份验证的GCP工作区用户设置防火墙规则”
创始人
2024-09-10 06:31:18
0

为了保护您的应用程序免受未经授权的访问,您应该为App Engine设置防火墙规则。在GCP中,您可以为经过身份验证的用户设置防火墙规则,以限制他们对应用程序的访问。

以下是示例代码,演示如何为GCP工作区中的用户设置防火墙规则:

  1. 在GCP控制台中,选择您的项目并转到'VPC网络”。

  2. 点击'防火墙规则”选项卡并选择'创建防火墙规则”。

  3. 为防火墙规则命名,并在'源过滤器”字段中输入用户的电子邮件地址。使用逗号分隔多个用户。

  4. 在'目标标记”字段中输入应用程序的标记,以表示允许用户访问应用程序。

  5. 选择所需的协议和端口,然后保存防火墙规则。

以下是一个Python代码示例,演示如何在App Engine中为经过身份验证的用户设置防火墙规则:

from google.cloud import firewall

firewall_client = firewall.FirewallServiceClient()

# Retrieve the authenticated user's email
user_email = os.environ.get('USER_EMAIL')

# Set the firewall rule name
rule_name = 'allow-{}'.format(user_email)

# Set the target and source filters
target_tags = ['allow-{}'.format(user_email)]
source_range = '{}'.format(user_email)

# Set the allowed protocols and ports
allowed_tcp = [('80', '80'), ('443', '443')]
allowed_udp = []

# Create the firewall rule
rule = firewall.FirewallRule(
    name=rule_name,
    action=firewall.FirewallRule.Action.ALLOW,
    direction=firewall.FirewallRule.Direction.INGRESS,
    target_tags=target_tags,
    source_ranges=[source_range],
    allowed=firewall.Allowed(
        ip_protocol='tcp',
        ports=allowed_tcp
    )
)

# Apply the firewall rule to the App Engine network
operation = firewall_client.update_firewall_rule(
    firewall_rule=rule,
    update_mask=firewall.field_mask.FieldMask(
        paths=[
            'allowed',
            'direction',
            'source_ranges',
            'target_tags'
        ]
    )
)

print('Firewall rule created for user: {}'.format(user_email))

这个

相关内容

热门资讯

安装apache-beam==... 出现此错误可能是因为用户的Python版本太低,而apache-beam==2.34.0需要更高的P...
避免在粘贴双引号时向VS 20... 在粘贴双引号时向VS 2022添加反斜杠的问题通常是由于编辑器的自动转义功能引起的。为了避免这个问题...
Android Recycle... 要在Android RecyclerView中实现滑动卡片效果,可以按照以下步骤进行操作:首先,在项...
omi系统和安卓系统哪个好,揭... OMI系统和安卓系统哪个好?这个问题就像是在问“苹果和橘子哪个更甜”,每个人都有自己的答案。今天,我...
原生ios和安卓系统,原生对比... 亲爱的读者们,你是否曾好奇过,为什么你的iPhone和安卓手机在操作体验上有着天壤之别?今天,就让我...
Android - 无法确定任... 这个错误通常发生在Android项目中,表示编译Debug版本的Java代码时出现了依赖关系问题。下...
Android - NDK 预... 在Android NDK的构建过程中,LOCAL_SRC_FILES只能包含一个项目。如果需要在ND...
Akka生成Actor问题 在Akka框架中,可以使用ActorSystem对象生成Actor。但是,当我们在Actor类中尝试...
Agora-RTC-React... 出现这个错误原因是因为在 React 组件中使用,import AgoraRTC from “ago...
Alertmanager在pr... 首先,在Prometheus配置文件中,确保Alertmanager URL已正确配置。例如:ale...