|
| 1 | +--- Utility module for RDS tokens for RDS DB access. |
| 2 | +-- |
| 3 | +-- See [IAM database authentication for MariaDB, MySQL, and PostgreSQL](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.html) |
| 4 | +-- for more information on using IAM database authentication with RDS. |
| 5 | + |
1 | 6 | local pl_string = require "pl.stringx"
|
2 | 7 | local httpc = require("resty.http").new()
|
3 | 8 | local presign_awsv4_request = require("resty.aws.request.signatures.presign")
|
4 | 9 |
|
5 | 10 | local RDS_IAM_AUTH_EXPIRE_TIME = 15 * 60
|
| 11 | +local M = {} |
6 | 12 |
|
7 |
| --- Return an authorization token used as the password for a RDS DB connection. |
8 |
| --- |
9 |
| --- @param config - AWS config instance |
10 |
| --- @param endpoint - Endpoint consists of the port needed to connect to the DB. <host>:<port> |
11 |
| --- @param region - Region is the location of where the DB is |
12 |
| --- @param dbUser - User account within the database to sign in with |
13 |
| --- @return token, err - Returns the token to use as the password for the DB connection, or nil and error if any occurs |
14 |
| --- |
15 |
| --- The following example shows how to use build_auth_token to create an authentication |
| 13 | +--- Return an authorization token used as the password for a RDS DB connection. |
| 14 | +-- The example shows how to use `build_auth_token` to create an authentication |
16 | 15 | -- token for connecting to a PostgreSQL database in RDS.
|
17 |
| --- |
| 16 | +-- @tparam table config AWS config instance |
| 17 | +-- @tparam string endpoint Endpoint to connect to the DB, format `"[http(s)://]<host>:<port>"` |
| 18 | +-- (the scheme defaults to `"https://"` if omitted) |
| 19 | +-- @tparam string region The AWS region |
| 20 | +-- @tparam string db_user User account within the database to sign in with |
| 21 | +-- @return token, err - Returns the token to use as the password for the DB connection, or nil and error if an error occurs |
| 22 | +-- @usage |
18 | 23 | -- local pgmoon = require "pgmoon"
|
19 | 24 | -- local AWS = require("resty.aws")
|
20 | 25 | -- local AWS_global_config = require("resty.aws.config").global
|
@@ -48,12 +53,10 @@ local RDS_IAM_AUTH_EXPIRE_TIME = 15 * 60
|
48 | 53 | -- ngx.log(ngx.ERR, "Failed to connect to database: ", err)
|
49 | 54 | -- return
|
50 | 55 | -- end
|
| 56 | +-- |
51 | 57 | -- -- Test query
|
52 | 58 | -- assert(pg:query("select * from users where status = 'active' limit 20"))
|
53 |
| --- |
54 |
| --- See https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.html |
55 |
| --- for more information on using IAM database authentication with RDS. |
56 |
| -local function build_auth_token(config, endpoint, region, db_user) |
| 59 | +function M.build_auth_token(config, endpoint, region, db_user) |
57 | 60 | if not(pl_string.startswith(endpoint, "http://") or pl_string.startswith(endpoint, "https://")) then
|
58 | 61 | endpoint = "https://" .. endpoint
|
59 | 62 | end
|
@@ -84,6 +87,4 @@ local function build_auth_token(config, endpoint, region, db_user)
|
84 | 87 | end
|
85 | 88 |
|
86 | 89 |
|
87 |
| -return { |
88 |
| - build_auth_token = build_auth_token, |
89 |
| -} |
| 90 | +return M |
0 commit comments