Skip to content

Commit c7792a0

Browse files
authored
Merge pull request #119 from arthurzenika/114-add-rds-iam-authentication
Support for rds-postgres URL with rdsutils.BuildAuthToken
2 parents c842325 + 7f3fd7a commit c7792a0

7 files changed

Lines changed: 268 additions & 1 deletion

File tree

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,19 @@ environment.
212212
LOGLEVEL=info ./sql_exporter
213213
```
214214
215+
Database specific configurations
216+
--------------------------------
217+
218+
For some database backends some special functionality is available :
219+
220+
* cloudsql-postgres: a special `*` caracter can be used to query all databases
221+
accessible by the account
222+
* cloudsql-mysql : same as above
223+
* rds-postgres : this type of URL expects a working AWS configuration
224+
which will use action the equivalent of `rds generate-db-auth-token`
225+
for the password. For this driver, the `AWS_REGION` environment variable
226+
must be set.
227+
215228
Why this exporter exists
216229
========================
217230

config.yml.dist

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,18 @@ jobs:
178178
node_name,
179179
schema_name,
180180
projection_name;
181+
- name: "rds"
182+
interval: '5m'
183+
connections:
184+
- 'rds-postgres://postgres_usr:AUTHTOKEN@mypostgresql.c6c8mwvfdgv0.us-west-2.rds.amazonaws.com/db_name'
185+
queries:
186+
- name: "running_queries"
187+
help: "Number of running queries"
188+
labels:
189+
- "datname"
190+
- "usename"
191+
values:
192+
- "count"
193+
query: |
194+
SELECT datname::text, usename::text, COUNT(*)::float AS count
195+
FROM pg_stat_activity GROUP BY datname, usename;

job.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net/url"
7+
"os"
78
"regexp"
89
"strconv"
910
"strings"
@@ -23,6 +24,9 @@ import (
2324
"github.com/snowflakedb/gosnowflake"
2425
_ "github.com/vertica/vertica-sql-go" // register the Vertica driver
2526
sqladmin "google.golang.org/api/sqladmin/v1beta4"
27+
28+
"github.com/aws/aws-sdk-go/aws/session"
29+
"github.com/aws/aws-sdk-go/service/rds/rdsutils"
2630
)
2731

2832
var (
@@ -91,7 +95,6 @@ func (j *Job) updateConnections() {
9195
// parse the connection URLs and create a connection object for each
9296
if len(j.conns) < len(j.Connections) {
9397
for _, conn := range j.Connections {
94-
9598
// Check if we need to use cloudsql driver
9699
if useCloudSQL, cloudsqlDriver := isValidCloudSQLDriver(conn); useCloudSQL {
97100
// Do CloudSQL stuff
@@ -221,6 +224,27 @@ func (j *Job) updateConnections() {
221224
})
222225
continue
223226
}
227+
if strings.HasPrefix(conn, "rds-postgres://") {
228+
// reuse postgres SQLDriver by stripping rds- from connexion URL after building the RDS
229+
// authentication token
230+
conn = strings.TrimPrefix(conn, "rds-")
231+
// FIXME - parsing twice the conn url to extract host & username
232+
u, err := url.Parse(conn)
233+
if err != nil {
234+
level.Error(j.log).Log("msg", "Failed to parse URL", "url", conn, "err", err)
235+
continue
236+
}
237+
region := os.Getenv("AWS_REGION")
238+
sess := session.Must(session.NewSessionWithOptions(session.Options{
239+
SharedConfigState: session.SharedConfigEnable,
240+
}))
241+
token, err := rdsutils.BuildAuthToken(u.Host, region, u.User.Username(), sess.Config.Credentials)
242+
if err != nil {
243+
level.Error(j.log).Log("msg", "Failed to parse URL", "url", conn, "err", err)
244+
continue
245+
}
246+
conn = strings.Replace(conn, "AUTHTOKEN", url.QueryEscape(token), 1)
247+
}
224248

225249
u, err := url.Parse(conn)
226250
if err != nil {

vendor/github.com/aws/aws-sdk-go/service/rds/rdsutils/builder.go

Lines changed: 127 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/aws/aws-sdk-go/service/rds/rdsutils/connect.go

Lines changed: 67 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/aws/aws-sdk-go/service/rds/rdsutils/doc.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ github.com/aws/aws-sdk-go/private/protocol/restjson
152152
github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil
153153
github.com/aws/aws-sdk-go/service/athena
154154
github.com/aws/aws-sdk-go/service/athena/athenaiface
155+
github.com/aws/aws-sdk-go/service/rds/rdsutils
155156
github.com/aws/aws-sdk-go/service/sso
156157
github.com/aws/aws-sdk-go/service/sso/ssoiface
157158
github.com/aws/aws-sdk-go/service/ssooidc

0 commit comments

Comments
 (0)