/
getcert.go
223 lines (192 loc) · 5.03 KB
/
getcert.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package cockroach
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
// Postgres driver
_ "github.com/lib/pq"
)
const (
ssmPrefix = "/cockroach/certs/clients"
)
// DefaultConnection sets up a database connection to the provided
// host using the application name as both username and database name.
func DefaultConnection(ctx context.Context, host, application string) (*sql.DB, error) {
cc, err := NewConnectionConfig(
ctx,
application,
ConnectionOptions{
Host: host,
},
)
if err != nil {
return nil, fmt.Errorf(
"failed to set up database connection configuration: %w", err)
}
return Connect(ctx, cc, application)
}
// ConnectionOptions are used to control how we connect to the
// cluster.
type ConnectionOptions struct {
SSM *ssm.SSM
CertificateDirectory string
DatabaseParameters url.Values
Host string
}
// ConnectionConfig is a database configuration that can be used to
// create Cockroach database connection URLs.
type ConnectionConfig struct {
certDir string
user string
host string
dbParams url.Values
credentials *Credentials
}
// Credentials are the credentials used to connect to and verify the
// identity of the database cluster.
type Credentials struct {
CA string `json:"ca"`
Certificate string `json:"certificate"`
Key string `json:"key"`
}
// NewConnectionconfig creates a new configuration for a given user
// and host.
func NewConnectionConfig(
ctx context.Context,
user string,
opts ConnectionOptions,
) (*ConnectionConfig, error) {
if opts.Host == "" {
return nil, errors.New("missing database host")
}
ssmSvc := opts.SSM
if ssmSvc == nil {
sess, err := session.NewSession()
if err != nil {
return nil, fmt.Errorf(
"failed to set up AWS SDK session: %w", err)
}
ssmSvc = ssm.New(sess)
}
cred, err := fetch(ctx, ssmSvc, ssmPrefix, user)
if err != nil {
return nil, err
}
certDir := opts.CertificateDirectory
if certDir == "" {
certDir, err = os.MkdirTemp("", user)
if err != nil {
return nil, fmt.Errorf(
"failed to create temporary certificate directory: %w", err)
}
}
cc := ConnectionConfig{
certDir: certDir,
host: opts.Host,
user: user,
credentials: cred,
dbParams: opts.DatabaseParameters,
}
if err := cc.createCertDirectory(); err != nil {
return nil, err
}
return &cc, nil
}
// DatabaseURL creates a database URL for use with sql.Open.
func (cc *ConnectionConfig) DatabaseURL(database string) string {
dbValues := make(url.Values)
dbValues.Set("connect_timeout", "5")
for k, v := range cc.dbParams {
dbValues[k] = v
}
dbValues.Set("sslmode", "verify-full")
dbValues.Set("sslcert", filepath.Join(
cc.certDir, "client."+cc.user+".crt",
))
dbValues.Set("sslkey", filepath.Join(
cc.certDir, "client."+cc.user+".key",
))
dbValues.Set("sslrootcert", filepath.Join(
cc.certDir, "ca.crt",
))
dbURL := &url.URL{
Scheme: "postgresql",
User: url.User(cc.user),
Host: cc.host,
Path: database,
RawQuery: dbValues.Encode(),
}
return dbURL.String()
}
// CertificateDir returns the directory used for storing certificates.
func (cc *ConnectionConfig) CertificateDir() string {
return cc.certDir
}
func fetch(
ctx context.Context,
ssmSvc *ssm.SSM, prefix string, name string,
) (*Credentials, error) {
paramName := filepath.Join(prefix, name)
res, err := ssmSvc.GetParameterWithContext(ctx, &ssm.GetParameterInput{
Name: ¶mName,
WithDecryption: aws.Bool(true),
})
if err != nil {
return nil, fmt.Errorf(
"failed to fetch certificate: %w", err)
}
var response Credentials
value := []byte(*res.Parameter.Value)
if err := json.Unmarshal(value, &response); err != nil {
return nil, fmt.Errorf(
"failed to parse stored credentials: %w", err)
}
return &response, nil
}
func (cc *ConnectionConfig) createCertDirectory() error {
if err := os.MkdirAll(cc.certDir, 0700); err != nil && !os.IsExist(err) {
return fmt.Errorf("failed to create directory: %w", err)
}
files := map[string][]byte{
"ca.crt": []byte(cc.credentials.CA),
"client." + cc.user + ".crt": []byte(cc.credentials.Certificate),
"client." + cc.user + ".key": []byte(cc.credentials.Key),
}
for name, data := range files {
err := os.WriteFile(
filepath.Join(cc.certDir, name), data, 0600,
)
if err != nil {
return fmt.Errorf(
"failed to create file %q: %w",
name, err)
}
}
return nil
}
// Connect to the cluster using a connection configuration.
func Connect(
ctx aws.Context,
cc *ConnectionConfig, database string,
) (*sql.DB, error) {
db, err := sql.Open("postgres", cc.DatabaseURL(database))
if err != nil {
return nil, fmt.Errorf(
"failed to configure database connection: %w",
err,
)
}
if err := db.PingContext(ctx); err != nil {
return nil, fmt.Errorf(
"failed to connect to database: %w", err)
}
return db, nil
}