AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1141846
Accepted
x-yuri
x-yuri
Asked: 2023-08-18 10:06:39 +0800 CST2023-08-18 10:06:39 +0800 CST 2023-08-18 10:06:39 +0800 CST

通过 Cloud SQL Auth 代理连接时是否需要密码?

  • 772

看来这是必要的,因为它要求我输入密码。但如果是这样,那么拥有 2 个凭据(凭据文件 + 密码)有什么意义呢?

如果没有,那我错过了什么?

文档对此并没有透露太多:

如果出现提示,请输入密码。

为了从本地机器上测试它,我做了:

docker-compose.yml:

services:
  app:
    build: .
    command: sleep infinity
    init: true
    volumes:
      - .:/app
    depends_on:
      - proxy

  proxy:
    image: gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.6.0
    command:
      --address 0.0.0.0
      --credentials-file /credentials.json
      --debug
      myprj:europe-central2:db
    volumes:
      - ./credentials.json:/credentials.json

Dockerfile:

FROM google/cloud-sdk:435.0.1-alpine
RUN apk add terraform postgresql15-client
WORKDIR /app

main.tf:

provider "google" {
  project = "myprj"
}

resource "google_sql_database_instance" "test-auth" {
  deletion_protection = false
  name = "db"
  region = "europe-central2"
  database_version = "POSTGRES_11"
  settings {
    tier = "db-f1-micro"
  }
}

resource "google_sql_user" "test-auth" {
  name = "postgres"
  instance = google_sql_database_instance.test-auth.name
  password = 123456
}

resource "google_service_account" "test-auth" {
  account_id = "dbaccount"
}

resource "google_project_iam_member" "test-auth" {
  project = "myprj"
  role    = "roles/cloudsql.client"
  member  = "serviceAccount:${google_service_account.test-auth.email}"
}
$ gcloud auth application-default login
$ terraform init
$ terraform apply
// create the service account key, copy to ./credentials.json, restart the container
$ psql -h proxy -U postgres
google-cloud-platform
  • 1 1 个回答
  • 61 Views

1 个回答

  • Voted
  1. Best Answer
    x-yuri
    2023-08-19T18:52:23+08:002023-08-19T18:52:23+08:00

    我缺少的是:

    • Cloud SQL 实例上默认禁用IAM 数据库身份验证,您需要设置cloudsql.iam_authentication=on
    • 应向服务帐户授予 Cloud SQL 实例用户角色 ( roles/cloudsql.instanceUser),其中包括cloudsql.instances.login权限,它允许用户登录数据库
    • 服务帐户应该有一个匹配的pg用户
    • cloud-sql-proxy 需要--auto-iam-authn,它使其使用 IAM 数据库身份验证

    还:

    • 该postgres用户不能用于 IAM 数据库身份验证,因为数据库用户名必须与服务帐户名称匹配

    这一切的结果是:

    docker-compose.yml:

    services:
      app:
        build: .
        command: sleep infinity
        init: true
        volumes:
          - .:/app
        depends_on:
          - proxy
    
      proxy:
        image: gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.6.0
        command:
          --address 0.0.0.0
          --credentials-file /credentials.json
          --auto-iam-authn
          myprj:europe-central2:db
        volumes:
          - ./credentials.json:/credentials.json
    

    Dockerfile:

    FROM google/cloud-sdk:435.0.1-alpine
    RUN apk add terraform postgresql15-client
    WORKDIR /app
    

    main.tf:

    provider "google" {
      project = "myprj"
    }
    
    resource "google_sql_database_instance" "test-auth" {
      deletion_protection = false
      name = "mydb"
      region = "europe-central2"
      database_version = "POSTGRES_11"
      settings {
        tier = "db-f1-micro"
        database_flags {
          name = "cloudsql.iam_authentication"
          value = "on"
        }
      }
    }
    
    resource "google_sql_user" "test-auth-built-in" {
      name = "postgres"
      instance = google_sql_database_instance.test-auth.name
      password = 123456
    }
    
    resource "google_service_account" "test-auth" {
      account_id = "myaccount"
    }
    
    resource "google_project_iam_member" "test-auth" {
      project = "myprj"
      role    = "roles/cloudsql.client"
      member  = "serviceAccount:${google_service_account.test-auth.email}"
    }
    
    resource "google_project_iam_member" "test-auth-instance-user" {
      project = "myprj"
      role    = "roles/cloudsql.instanceUser"
      member  = "serviceAccount:${google_service_account.test-auth.email}"
    }
    
    resource "google_sql_user" "test-auth-iam" {
      name = "[email protected]"
      instance = google_sql_database_instance.test-auth.name
      type = "CLOUD_IAM_SERVICE_ACCOUNT"
    }
    
    $ gcloud auth application-default login
    $ terraform init
    $ terraform apply
    // create the service account key, copy to ./credentials.json, restart the container
    $ psql -h proxy -U [email protected] postgres
    
    • 0

相关问题

  • 网络定价如何在云平台中准确运作?我应该如何避免专门的定价攻击?

  • 云有多大?[关闭]

  • 无需短信即可注册 Google AppEngine?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve