-
Notifications
You must be signed in to change notification settings - Fork 487
/
postgres.go
258 lines (218 loc) · 6.79 KB
/
postgres.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
Copyright 2023 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package postgres
import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
"strconv"
"sync/atomic"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/dapr/components-contrib/bindings"
awsAuth "github.com/dapr/components-contrib/common/authentication/aws"
pgauth "github.com/dapr/components-contrib/common/authentication/postgresql"
"github.com/dapr/components-contrib/metadata"
"github.com/dapr/kit/logger"
)
// List of operations.
const (
execOperation bindings.OperationKind = "exec"
queryOperation bindings.OperationKind = "query"
closeOperation bindings.OperationKind = "close"
commandSQLKey = "sql"
commandArgsKey = "params"
)
// Postgres represents PostgreSQL output binding.
type Postgres struct {
logger logger.Logger
db *pgxpool.Pool
closed atomic.Bool
enableAzureAD bool
enableAWSIAM bool
awsAuthProvider awsAuth.Provider
}
// NewPostgres returns a new PostgreSQL output binding.
func NewPostgres(logger logger.Logger) bindings.OutputBinding {
return &Postgres{
logger: logger,
}
}
// Init initializes the PostgreSql binding.
func (p *Postgres) Init(ctx context.Context, meta bindings.Metadata) error {
if p.closed.Load() {
return errors.New("cannot initialize a previously-closed component")
}
opts := pgauth.InitWithMetadataOpts{
AzureADEnabled: p.enableAzureAD,
AWSIAMEnabled: p.enableAWSIAM,
}
m := psqlMetadata{}
if err := m.InitWithMetadata(meta.Properties); err != nil {
return err
}
var err error
poolConfig, err := m.GetPgxPoolConfig()
if err != nil {
return err
}
if opts.AWSIAMEnabled && m.UseAWSIAM {
opts, validateErr := m.BuildAwsIamOptions(p.logger, meta.Properties)
if validateErr != nil {
return fmt.Errorf("failed to validate AWS IAM authentication fields: %w", validateErr)
}
var provider awsAuth.Provider
provider, err = awsAuth.NewProvider(ctx, *opts, awsAuth.GetConfig(*opts))
if err != nil {
return err
}
p.awsAuthProvider = provider
p.awsAuthProvider.UpdatePostgres(ctx, poolConfig)
}
// This context doesn't control the lifetime of the connection pool, and is
// only scoped to postgres creating resources at init.
connCtx, connCancel := context.WithTimeout(ctx, m.Timeout)
defer connCancel()
p.db, err = pgxpool.NewWithConfig(connCtx, poolConfig)
if err != nil {
return fmt.Errorf("unable to connect to the DB: %w", err)
}
pingCtx, pingCancel := context.WithTimeout(ctx, m.Timeout)
defer pingCancel()
err = p.db.Ping(pingCtx)
if err != nil {
return fmt.Errorf("failed to ping the DB: %w", err)
}
return nil
}
// Operations returns list of operations supported by PostgreSql binding.
func (p *Postgres) Operations() []bindings.OperationKind {
return []bindings.OperationKind{
execOperation,
queryOperation,
closeOperation,
}
}
// Invoke handles all invoke operations.
func (p *Postgres) Invoke(ctx context.Context, req *bindings.InvokeRequest) (resp *bindings.InvokeResponse, err error) {
if req == nil {
return nil, errors.New("invoke request required")
}
// We let the "close" operation here succeed even if the component has been closed already
if req.Operation == closeOperation {
err = p.Close()
return nil, err
}
if p.closed.Load() {
return nil, errors.New("component is closed")
}
if req.Metadata == nil {
return nil, errors.New("metadata required")
}
// Metadata property "sql" contains the query to execute
sql := req.Metadata[commandSQLKey]
if sql == "" {
return nil, fmt.Errorf("required metadata not set: %s", commandSQLKey)
}
// Metadata property "params" contains JSON-encoded parameters, and it's optional
// If present, it must be unserializable into a []any object
var args []any
if argsStr := req.Metadata[commandArgsKey]; argsStr != "" {
err = json.Unmarshal([]byte(argsStr), &args)
if err != nil {
return nil, fmt.Errorf("invalid metadata property %s: failed to unserialize into an array: %w", commandArgsKey, err)
}
}
startTime := time.Now().UTC()
resp = &bindings.InvokeResponse{
Metadata: map[string]string{
"operation": string(req.Operation),
"sql": sql,
"start-time": startTime.Format(time.RFC3339Nano),
},
}
switch req.Operation { //nolint:exhaustive
case execOperation:
r, err := p.exec(ctx, sql, args...)
if err != nil {
return nil, err
}
resp.Metadata["rows-affected"] = strconv.FormatInt(r, 10) // 0 if error
case queryOperation:
d, err := p.query(ctx, sql, args...)
if err != nil {
return nil, err
}
resp.Data = d
default:
return nil, fmt.Errorf(
"invalid operation type: %s. Expected %s, %s, or %s",
req.Operation, execOperation, queryOperation, closeOperation,
)
}
endTime := time.Now().UTC()
resp.Metadata["end-time"] = endTime.Format(time.RFC3339Nano)
resp.Metadata["duration"] = endTime.Sub(startTime).String()
return resp, nil
}
// Close close PostgreSql instance.
func (p *Postgres) Close() error {
if !p.closed.CompareAndSwap(false, true) {
// If this failed, the component has already been closed
// We allow multiple calls to close
return nil
}
if p.db != nil {
p.db.Close()
}
p.db = nil
errs := make([]error, 1)
if p.awsAuthProvider != nil {
errs[0] = p.awsAuthProvider.Close()
}
return errors.Join(errs...)
}
func (p *Postgres) query(ctx context.Context, sql string, args ...any) (result []byte, err error) {
rows, err := p.db.Query(ctx, sql, args...)
if err != nil {
return nil, fmt.Errorf("error executing query: %w", err)
}
rs := make([]any, 0)
for rows.Next() {
val, rowErr := rows.Values()
if rowErr != nil {
return nil, fmt.Errorf("error reading result '%v': %w", rows.Err(), rowErr)
}
rs = append(rs, val) //nolint:asasalint
}
result, err = json.Marshal(rs)
if err != nil {
return nil, fmt.Errorf("error serializing results: %w", err)
}
return result, nil
}
func (p *Postgres) exec(ctx context.Context, sql string, args ...any) (result int64, err error) {
res, err := p.db.Exec(ctx, sql, args...)
if err != nil {
return 0, fmt.Errorf("error executing query: %w", err)
}
return res.RowsAffected(), nil
}
// GetComponentMetadata returns the metadata of the component.
func (p *Postgres) GetComponentMetadata() (metadataInfo metadata.MetadataMap) {
metadataStruct := psqlMetadata{}
metadata.GetMetadataInfoFromStructType(reflect.TypeOf(metadataStruct), &metadataInfo, metadata.BindingType)
return
}