【问题标题】:Authenticate a local Spring Boot service with Google Cloud使用 Google Cloud 对本地 Spring Boot 服务进行身份验证
【发布时间】:2021-08-28 08:20:09
【问题描述】:

我有一个可以在本地服务器(而不是谷歌云服务器)上运行的 Spring Boot 应用程序。我计划使用服务帐户来允许应用程序使用 Google Cloud Storage and Logging。我创建了一个服务帐户和一个 api 密钥并下载了如下所示的 json 文件:

{
  "type": "service_account",
  "project_id": "...",
  "private_key_id": "....",
  "private_key": "-----BEGIN PRIVATE KEY-----\n....\n-----END PRIVATE KEY-----\n",
  "client_email": ".....iam.gserviceaccount.com",
  "client_id": ".....",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/....."
}

如果我在开发环境中启动应用程序并设置指向 json 文件的环境变量,应用程序将按预期工作 (GOOGLE_APPLICATION_CREDENTIALS)。

但是,我需要在 ubuntu 中将应用程序作为 init.d 服务运行(应用程序配置为作为可执行 jar 运行)。 我将我的 jar 文件安装为这样的服务:

sudo ln -s /home/<user>/<jarname>.jar /etc/init.d/<service-name>
sudo chmod +x /etc/init.d/<service-name>
sudo update-rc.d <service-name> defaults
sudo service <service-name> start

init.d服务看不到我设置的环境变量,所以需要换一种方式进行认证。

我安装了gcloud 并像这样设置服务帐户:

gcloud auth activate-service-account service-account-name@project-name.iam.gserviceaccount.com --key-file=credentials.json
gcloud config set project <projectId>

我还运行了gclout init,它显示了我添加的服务帐户。

当我运行应用程序时,我收到多个错误:com.google.api.gax.rpc.PermissionDeniedException: io.grpc.StatusRuntimeException: PERMISSION_DENIED: The request is missing a valid API key.。好像少了什么,但我不知道是什么。

我的logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/cloud/gcp/autoconfigure/logging/logback-appender.xml" />
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>

    <appender name="CLOUD" class="com.google.cloud.logging.logback.LoggingAppender">
        <!-- Optional : filter logs at or above a level -->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
        <log>application.log</log> <!-- Optional : default java.log -->
        <!--resourceType>gae_app</resourceType--> <!-- Optional : default: auto-detected, fallback: global -->
        <loggingEventEnhancer>....LoggingEventEnhancer</loggingEventEnhancer>
        <flushLevel>INFO</flushLevel> <!-- Optional : default ERROR -->
    </appender>


    <root level="INFO">
        <!-- If running in GCP, remove the CONSOLE appender otherwise logs will be duplicated. -->
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="CLOUD"/>

    </root>
</configuration>

在服务器上运行gcloud logging sinks list 工作正常,所以权限应该是正确的。

我知道spring.cloud.gcp.credentials.location=file:/..,但它对我不起作用。 (另外,这个:https://github.com/spring-cloud/spring-cloud-gcp/issues/315#issuecomment-359077878

【问题讨论】:

    标签: java spring-boot ubuntu google-cloud-platform init.d


    【解决方案1】:

    我使用了systemd,它允许我在服务启动时设置任何环境变量。

    1. 将可执行 jar 和 application.properties 放在一个文件夹中,例如 /opt/&lt;name&gt;/home/&lt;user&gt;/&lt;name&gt;
    2. 须藤纳米/etc/systemd/system/&lt;name&gt;.service
    3. 内容:
    [Unit]
    Description=Description text...etc
    Wants=network-online.target
    After=network.target network-online.target
    
    [Service]
    Environment="LOGPATH=/var/<name>/logs"
    Environment="GOOGLE_APPLICATION_CREDENTIALS=/path-to-google-json/google/credentials.json"
    ExecStartPre=/bin/mkdir -pm 0755 ${LOGPATH}
    ExecStart=/opt/<name>/<jar-name>.jar
    PIDFile=/run/<name>/<name>%i.pid
    Restart=on-abort
    RuntimeDirectory=<name>
    RuntimeDirectoryMode=755
    WorkingDirectory=/opt/<name>
    
    [Install]
    WantedBy=multi-user.target
    
    1. 重新加载 systemctl 配置:sudo systemctl daemon-reload
    2. 设置服务在系统启动后启动:sudo systemctl enable &lt;name&gt;.service
    3. sudo systemctl start &lt;name&gt;.service启动服务

    状态:sudo systemctl status &lt;name&gt;.service

    关注标准输出:sudo journalctl -u &lt;name&gt;.service --follow

    【讨论】:

      猜你喜欢
      • 2016-10-31
      • 2014-09-22
      • 2015-11-22
      • 1970-01-01
      • 2021-01-16
      • 2015-08-11
      • 2018-08-29
      • 2023-03-19
      • 1970-01-01
      相关资源
      最近更新 更多