Skip to content

Commit

Permalink
Merge pull request #119 from arthurzenika/114-add-rds-iam-authentication
Browse files Browse the repository at this point in the history
Support for rds-postgres URL with rdsutils.BuildAuthToken
  • Loading branch information
dewey committed Mar 21, 2024
2 parents c842325 + 7f3fd7a commit c7792a0
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,19 @@ environment.
LOGLEVEL=info ./sql_exporter
```
Database specific configurations
--------------------------------
For some database backends some special functionality is available :
* cloudsql-postgres: a special `*` caracter can be used to query all databases
accessible by the account
* cloudsql-mysql : same as above
* rds-postgres : this type of URL expects a working AWS configuration
which will use action the equivalent of `rds generate-db-auth-token`
for the password. For this driver, the `AWS_REGION` environment variable
must be set.
Why this exporter exists
========================
Expand Down
15 changes: 15 additions & 0 deletions config.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,18 @@ jobs:
node_name,
schema_name,
projection_name;
- name: "rds"
interval: '5m'
connections:
- 'rds-postgres://postgres_usr:[email protected]/db_name'
queries:
- name: "running_queries"
help: "Number of running queries"
labels:
- "datname"
- "usename"
values:
- "count"
query: |
SELECT datname::text, usename::text, COUNT(*)::float AS count
FROM pg_stat_activity GROUP BY datname, usename;
26 changes: 25 additions & 1 deletion job.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/url"
"os"
"regexp"
"strconv"
"strings"
Expand All @@ -23,6 +24,9 @@ import (
"github.com/snowflakedb/gosnowflake"
_ "github.com/vertica/vertica-sql-go" // register the Vertica driver
sqladmin "google.golang.org/api/sqladmin/v1beta4"

"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/rds/rdsutils"
)

var (
Expand Down Expand Up @@ -91,7 +95,6 @@ func (j *Job) updateConnections() {
// parse the connection URLs and create a connection object for each
if len(j.conns) < len(j.Connections) {
for _, conn := range j.Connections {

// Check if we need to use cloudsql driver
if useCloudSQL, cloudsqlDriver := isValidCloudSQLDriver(conn); useCloudSQL {
// Do CloudSQL stuff
Expand Down Expand Up @@ -221,6 +224,27 @@ func (j *Job) updateConnections() {
})
continue
}
if strings.HasPrefix(conn, "rds-postgres://") {
// reuse postgres SQLDriver by stripping rds- from connexion URL after building the RDS
// authentication token
conn = strings.TrimPrefix(conn, "rds-")
// FIXME - parsing twice the conn url to extract host & username
u, err := url.Parse(conn)
if err != nil {
level.Error(j.log).Log("msg", "Failed to parse URL", "url", conn, "err", err)
continue
}
region := os.Getenv("AWS_REGION")
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
token, err := rdsutils.BuildAuthToken(u.Host, region, u.User.Username(), sess.Config.Credentials)
if err != nil {
level.Error(j.log).Log("msg", "Failed to parse URL", "url", conn, "err", err)
continue
}
conn = strings.Replace(conn, "AUTHTOKEN", url.QueryEscape(token), 1)
}

u, err := url.Parse(conn)
if err != nil {
Expand Down
127 changes: 127 additions & 0 deletions vendor/github.com/aws/aws-sdk-go/service/rds/rdsutils/builder.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions vendor/github.com/aws/aws-sdk-go/service/rds/rdsutils/connect.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions vendor/github.com/aws/aws-sdk-go/service/rds/rdsutils/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ github.com/aws/aws-sdk-go/private/protocol/restjson
github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil
github.com/aws/aws-sdk-go/service/athena
github.com/aws/aws-sdk-go/service/athena/athenaiface
github.com/aws/aws-sdk-go/service/rds/rdsutils
github.com/aws/aws-sdk-go/service/sso
github.com/aws/aws-sdk-go/service/sso/ssoiface
github.com/aws/aws-sdk-go/service/ssooidc
Expand Down

0 comments on commit c7792a0

Please sign in to comment.