首页 文章

Cloud SQL代理和权限不足

提问于
浏览
3

我正在尝试在Google的Container Engine(GKE)上部署我的Spring Boot / JHipster应用程序 . 我已经找到了大部分内容,但我的数据库实例(使用Google SQL Proxy在Google Cloud SQL上运行的PostgreSQL实例)存在问题 .

我按照说明herehere来设置我的应用程序 .

  • I 've set up my PostreSQL instance in cloud, and created my app' s数据库和用户 .

  • 我've created an SQL service with Cloud SQL Client role -- I grabbed the JSON key, and used it to create my cloudsql-instance-credentials. I' ve还创建了我的cloudsql-db-credentials .

  • 我've added the additional bits to my deployment yaml file. I' ve基本上从this GitHub sample克隆了yaml文件,并用我自己的Docker图像(托管在Google容器注册表中)替换了所有对wordpress的引用 . 我也更新了代理块,如下所示:

deployment.yaml片段:

- image: gcr.io/cloudsql-docker/gce-proxy:1.09
      name: cloudsql-proxy
          command: ["/cloud_sql_proxy", "--dir=/cloudsql",
                    "-instances=[my-project]:us-central1:[my-sql-instance-id]=tcp:5432",
                    "-credential_file=/secrets/cloudsql/credentials.json"]

最后,我更新了我的Spring Boot配置yaml文件,如下所示:

datasource:
    type: com.zaxxer.hikari.HikariDataSource
    url: jdbc:postgresql://google/[my-database]?socketFactory=com.google.cloud.sql.postgres.SocketFactory&socketFactoryArg=[my-project]:us-central1:[my-sql-instance-id]
    username: ${DB_USER}
    password: ${DB_PASSWORD}

当我部署我的部署时,图像会部署,但无法启动应用程序 . 这是我日志中的重要部分:

Caused by: java.lang.RuntimeException: Unable to retrieve information about Cloud SQL instance [[my-project]:us-central1:[my-sql-instance-id]]
    at com.google.cloud.sql.core.SslSocketFactory.obtainInstanceMetadata(SslSocketFactory.java:411)
    at com.google.cloud.sql.core.SslSocketFactory.fetchInstanceSslInfo(SslSocketFactory.java:284)
    at com.google.cloud.sql.core.SslSocketFactory.getInstanceSslInfo(SslSocketFactory.java:264)
    at com.google.cloud.sql.core.SslSocketFactory.createAndConfigureSocket(SslSocketFactory.java:183)
    at com.google.cloud.sql.core.SslSocketFactory.create(SslSocketFactory.java:152)
    at com.google.cloud.sql.postgres.SocketFactory.createSocket(SocketFactory.java:50)
    at org.postgresql.core.PGStream.<init>(PGStream.java:60)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:144)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:52)
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:216)
    at org.postgresql.Driver.makeConnection(Driver.java:404)
    at org.postgresql.Driver.connect(Driver.java:272)
    ... 37 common frames omitted
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
  "code" : 403,
  "errors" : [ {
    "domain" : "global",
    "message" : "Insufficient Permission",
    "reason" : "insufficientPermissions"
  } ],
  "message" : "Insufficient Permission"
}
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
    at com.google.cloud.sql.core.SslSocketFactory.obtainInstanceMetadata(SslSocketFactory.java:372)
    ... 48 common frames omitted

这个“权限不足”错误在StackOverflow上弹出很多,但我没有找到与我的情况完全相同的问题 . 这似乎是一个通用的OAuth级错误 . 我觉得我已经多次检查了我的设置与指示,我不知道在哪里可以寻找任何额外的线索 .

有任何想法吗?

Update:

感谢Vadim的指针,我已经设法超越了“权限不足”问题 . 遗憾的是,我的应用程序在启动时仍然失败,当它尝试 Build 与数据库的连接时(特别是,当Liquibase尝试开始连接到数据库以运行迁移脚本时) .

我的新错误是在驱动程序的套接字级别:

liquibase.exception.DatabaseException: org.postgresql.util.PSQLException: The connection attempt failed.
    at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:390)
    at io.github.jhipster.config.liquibase.AsyncSpringLiquibase.initDb(AsyncSpringLiquibase.java:82)
    at io.github.jhipster.config.liquibase.AsyncSpringLiquibase.afterPropertiesSet(AsyncSpringLiquibase.java:72)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
    ... 24 common frames omitted
Caused by: org.postgresql.util.PSQLException: The connection attempt failed.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:272)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:52)
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:216)
    at org.postgresql.Driver.makeConnection(Driver.java:404)
    at org.postgresql.Driver.connect(Driver.java:272)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at org.postgresql.ds.common.BaseDataSource.getConnection(BaseDataSource.java:86)
    at org.postgresql.ds.common.BaseDataSource.getConnection(BaseDataSource.java:71)
    at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:385)
    ... 28 common frames omitted
Caused by: java.net.SocketException: already connected
    at java.net.Socket.connect(Socket.java:569)
    at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:673)
    at org.postgresql.core.PGStream.<init>(PGStream.java:61)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:144)
    ... 37 common frames omitted

2 回答

  • 4

    Vadim解决了我问过的问题,但第二个问题 - 套接字已经连接问题 - 最终被我的一个同事想出来了 .

    套接字问题的根源与数据源配置有关 . 事实证明,我正在混合和匹配两种不同的机制来访问Cloud SQL环境 .

    • 使用Cloud SQL代理;和

    • 使用Google套接字工厂

    因为我已成功配置了Cloud SQL Proxy,所以在Spring Boot环境中我不需要那个奇怪的JDBC URL . 我可以使用127.0.0.1:5432连接,如下所示:

    datasource:
        type: com.zaxxer.hikari.HikariDataSource
        url: jdbc:postgresql://127.0.0.1:5432/[my-database-name]
        username: ${DB_USER}
        password: ${DB_PASSWORD}
    

    现在我已经替换了我的JDBC URL,我的应用程序连接成功 .

  • 1

    这有点旧,但使用Cloud SQL Proxy不是解决方案 .

    使用带有Spring Boot的Google Socket Factory时, java.net.SocketException: already connected 也遇到了同样的问题 .

    在将postgresql驱动程序升级到最新版本(写入时 org.postgresql:postgresql:42.2.1 )后,我能够解决这个问题 .

相关问题