无法从 Cloud Functions NodeJS Sequelize 服务连接到 GCP CloudSQL Postgres 实例

问题描述 投票:0回答:0

简而言之,我正在尝试编写由 Cloud Functions 触发的各种迁移服务。我已经按照这里的文档https://cloud.google.com/sql/docs/postgres/connect-functions,但是 Sequelize 似乎不能很好地处理要设置的环境变量。我确实部署了我的功能并设置了服务器并能够连接到(它有一个公共 IP)。

我还尝试了这个人用他们自己的配置发现了什么GCP Cloud Run Cloud - Cloud SQL 实例“${process.env.INSTANCE_CONNECTION_NAME}”无法访问,但仍然没有运气。我收到的最常见错误是:

ConnectionError [SequelizeConnectionError]: password authentication failed for user "postgres"
    at Client._connectionCallback (/workspace/node_modules/sequelize/src/dialects/postgres/connection-manager.js:191:24)
    at Client._handleErrorWhileConnecting (/workspace/node_modules/pg/lib/client.js:316:19)
    at Client._handleErrorMessage (/workspace/node_modules/pg/lib/client.js:336:19)
    at Connection.emit (node:events:513:28)
    at Connection.emit (node:domain:552:15)
    at /workspace/node_modules/pg/lib/connection.js:119:12
    at Parser.parse (/workspace/node_modules/pg-protocol/src/parser.ts:104:9)
    at Socket.<anonymous> (/workspace/node_modules/pg-protocol/src/index.ts:7:48)
    at Socket.emit (node:events:513:28)
    at Socket.emit (node:domain:552:15) {"

但是这个错误最常出现在未配置的 postgres 用户身上,我已经通过在 VM 本身上尝试凭据验证了情况并非如此。当我在 SQL 代理上工作时,我看到了这个错误,我确实在本地工作。我试图用来连接 index.ts 的 Typescript 代码:

import 'ts-node/register';
import { Sequelize } from 'sequelize';
import { Umzug, SequelizeStorage } from 'umzug';
import * as dotenv from "dotenv";

dotenv.config();

const user = process.env.DB_USER!;
const host = process.env.DB_HOST!;
const database = process.env.DB_NAME!;
const password = process.env.DB_PASS!;
const port = process.env.DB_PORT ? process.env.DB_PORT as unknown as number: undefined;

const sequelize = new Sequelize(database, user, password, {
  host,
  port,
  dialect: 'postgres',
  logging: false,
  dialectOptions: {
    socketPath: process.env.INSTANCE_CONNECTION_NAME
  }
});

(other unrelated logic)

export const migrate_core_db_up = async (req:any, res:any) => {
  try {
    await sequelize.authenticate();
    res.send("Completed migration.");
  } catch (e) {
    console.error(e);
    res.send("Failed to complete migration.");
  }
}

我的环境变量来自 yaml:

DB_NAME: db-name
DB_HOST: /cloudsql/project-id:zone:instance
INSTANCE_UNIX_SOCKET: /cloudsql/project-id:zone:instance
INSTANCE_CONNECTION_NAME: /cloudsql/project-id:zone:instance
DB_PASS: [password]
DB_USER: postgres
DB_PORT: 5432

如果有帮助,我用来部署该功能的命令:

gcloud functions deploy migrate-core-db-up --gen2 --entry-point migrate_core_db_up --allow-unauthenticated --env-vars-file .env-functions.yaml \
     --trigger-http --runtime nodejs18 [email protected]

我切换了是否使用端口,从连接名称/主机中删除了 cloudsql 前缀,并创建并尝试了不同的用户。我真的非常感谢任何指示或建议!尝试在已部署的 Function 实例中锁定 Cloud SQL 代理是否合理?由于缺乏基于文档的节点支持,SSL 证书路由在 Cloud SQL 中似乎同样危险。提前致谢。

node.js typescript postgresql google-cloud-functions sequelize.js
© www.soinside.com 2019 - 2024. All rights reserved.