GCP:具有目标App Engine HTTP的Cloud Scheduler作业无法保护与管理员的连接

问题描述

我已经创建了一个以App Engine HTTP为目标的Cloud Scheduler作业。目标URL是/ admin / task / create-documents

我有一个运行Java 8的App Engine flexible。

它工作正常,但是现在我想确保仅对GCP开发人员(通常称为“ admin”)提供对Cloud Scheduler作业调用的servlet(/ admin / task / create-documents)的访问权限。我假设Cloud Scheduler作业被视为“管理员”。

选项1-无效

我尝试针对标准环境修改web.xml文件,如here所述,但没有成功:

web.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>admin</web-resource-name>
            <url-pattern>/admin/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>admin</role-name>
        </auth-constraint>
    </security-constraint>
</web-app>

选项2-无效

我试图通过将login: admin添加到handlers部分来修改app.yaml,如文档here所述,但也没有成功。

app.yaml文件:

runtime:    java
env:        flex
threadsafe: true

runtime_config:
  jdk: openjdk8
  server: jetty9

handlers:
  - url: /admin/.*
    script: auto
    login: admin
  - url: /.*
    script: auto
    secure: always

env_variables:
  JETTY_ARGS: -Djava.util.logging.config.file=WEB-INF/logging.properties

network:
  instance_tag: no-ip
  name: my-network
  subnetwork_name: my-subnet

但是我也注意到不建议使用登录参数,如herehere所述。他们提到了IAM策略,但不确定如何配置它们。

如何将Cloud Scheduler作业端点仅保护为“管理员”?

解决方法

您不能通过配置。您需要在代码中实施检查。

您可以将静态值添加到https://my-url.appspot.com/admin/task/create-documents?key=my_secret之类的URL中,或将OIDC身份验证与Cloud Scheduler一起使用以向端点提供JWT令牌。

在两种情况下,您都必须检查机密(JWT上的内容)是否为预期的机密。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...