-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
140 lines (114 loc) · 2.93 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"context"
"database/sql"
"fmt"
"log"
"os"
"os/signal"
"time"
"github.com/davepgreene/go-db-credential-refresh/driver"
"github.com/davepgreene/go-db-credential-refresh/examples/db"
"github.com/davepgreene/go-db-credential-refresh/store/vault"
vaultcredentials "github.com/davepgreene/go-db-credential-refresh/store/vault/credentials"
)
const (
role = "role"
host = "localhost"
username = "postgres"
password = "postgres"
dbName = "postgres"
port = 5432
)
func main() {
err := Run()
if err == nil {
return
}
if err == context.Canceled {
return
}
log.Fatal(err)
}
func Run() (err error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
infraCtx := context.Background()
dbContainer, err := setupDb(infraCtx)
if err != nil {
return err
}
defer func() {
if cleanupErr := dbContainer.Terminate(infraCtx); cleanupErr != nil {
log.Printf("error cleaning up postgres container: %v", cleanupErr)
err = cleanupErr
}
}()
driverConfig, err := generateDriverConfig(infraCtx, dbContainer)
if err != nil {
return err
}
// Set up Vault, DB backend, and Postgres configuration
client, cleanup, err := setupVault(infraCtx, "host.docker.internal", driverConfig.Port)
if err != nil {
if cleanup != nil {
if err := cleanup(); err != nil {
return err
}
}
return err
}
defer func() {
if cleanupErr := cleanup(); cleanupErr != nil {
log.Printf("error cleaning up vault container: %v", cleanupErr)
err = cleanupErr
}
}()
// Create the store
store, err := vault.NewStore(&vault.Config{
Client: client,
CredentialLocation: vaultcredentials.NewAPIDatabaseCredentials(role, ""),
})
if err != nil {
return err
}
// Create the connector which implements database/sql/driver.Connector
c, err := driver.NewConnector(store, "pgx", driverConfig)
if err != nil {
return err
}
// Use the built in database/sql package to work with the connector
database := sql.OpenDB(c)
// In order to demonstrate the creation and revocation of roles we need to set the
// connection lifetime very short. In a production environment, Vault role TTLs and
// connection lifetime should be tuned based on database performance requirements.
database.SetConnMaxLifetime(2 * time.Second)
database.SetMaxIdleConns(2)
database.SetMaxOpenConns(5)
appSignal := make(chan os.Signal, 1)
signal.Notify(appSignal, os.Interrupt)
go func() {
<-appSignal
cancel()
}()
for {
// First ping the DB to open a connection
if err := db.Ping(ctx, database); err != nil {
log.Println(err)
break
}
// Sleep long enough that the creds should expire
time.Sleep(3 * time.Second)
// Now get users
users, err := db.QueryUsers(ctx, database, map[string]bool{
username: false,
})
if err != nil {
fmt.Println(err)
break
}
fmt.Printf("Retrieving users from database: %v\n", users)
}
// err = TearDownRoles(ctx, client)
return err
}