このパッケージはJavaScriptのPostgreSQL接続に対してAzure Entra ID(旧Azure Active Directory)認証を提供します。
Node-postgres(pg)とSequelizeクライアントの両方をサポートしています。
主要なリンク:
作業の開始
現在サポートされている環境
詳細については、 サポート ポリシー を参照してください。
[前提条件]
- Azure サブスクリプション。
- Entra ID認証用に設定されたAzure Database for PostgreSQLサーバー。
@azure/postgresql-auth パッケージをインストールする
JavaScript用のAzure PostgreSQL認証クライアントライブラリを npm でインストールしてください:
npm install @azure/postgresql-auth
また、認証サポート用の @azure/identity と、お好みのPostgreSQLクライアントをインストールする必要があります:
npm install @azure/identity pg
# or
npm install @azure/identity sequelize pg
主な概念
このライブラリは、Entra ID認証をPostgreSQLと統合するための2つの機能を提供します:
-
entraTokenProvider— PostgreSQLのパスワードとして使うのに適したEntra IDアクセストークンを取得するパスワードプロバイダー関数を返します。 これを使ってpg.Poolやpg.Client。 -
configureEntraAuthentication— SequelizeインスタンスにbeforeConnectフックを登録し、新しい接続の前に自動的に新しいトークンを取得し、ユーザー名/パスワードを設定します。 -
GetEntraTokenPasswordOptions— entraTokenProviderに渡されたデフォルトのOAuthスコープ(スコーププロパティ)をオプションでオーバーライドします。 -
SequelizeBeforeConnectHook— configureEntraAuthenticationで受理された構造インターフェース;Sequelize対応のオブジェクトをハードなSequelize依存性を加えずに使えます。
両関数とも Azure TokenCredential(@azure/identity)を受け取り、Azure Database for PostgreSQLスコープに対してトークン取得を処理します。
例示
node-postgres(pg)での使用
import { DefaultAzureCredential } from "@azure/identity";
const { entraTokenProvider } = await import("@azure/postgresql-auth");
const pg = await import("pg");
const credential = new DefaultAzureCredential();
const pool = new pg.Pool({
host: process.env.PGHOST,
port: Number(process.env.PGPORT || 5432),
database: process.env.PGDATABASE,
user: process.env.PGUSER,
password: entraTokenProvider(credential),
ssl: { rejectUnauthorized: true },
});
Sequelizeでの使用
import { DefaultAzureCredential } from "@azure/identity";
const { configureEntraAuthentication } = await import("@azure/postgresql-auth");
const { Sequelize } = await import("sequelize");
const sequelize = new Sequelize({
dialect: "postgres",
host: process.env.PGHOST,
port: Number(process.env.PGPORT || 5432),
database: process.env.PGDATABASE,
});
const credential = new DefaultAzureCredential();
configureEntraAuthentication(sequelize, credential);
await sequelize.authenticate();
Troubleshooting
ロギング(記録)
ログ記録を有効にすると、エラーに関する有用な情報を明らかにするのに役立つ場合があります。 HTTP 要求と応答のログを表示するには、AZURE_LOG_LEVEL 環境変数を infoに設定します。 または、setLogLevelで @azure/logger を呼び出すことによって、実行時にログを有効にすることもできます。
import { setLogLevel } from "@azure/logger";
setLogLevel("info");
ログを有効にする方法の詳細な手順については、 @azure/logger パッケージのドキュメントを参照してください。
次のステップ
このライブラリの使用方法の詳細な例については、ディレクトリ
Contributing
このライブラリに投稿する場合は、コードをビルドしてテストする方法の詳細については、 投稿ガイド をお読みください。
Azure SDK for JavaScript