-
Notifications
You must be signed in to change notification settings - Fork 341
/
postgres.go
747 lines (687 loc) · 22.1 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
package main
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
import (
"database/sql"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"github.com/apache/trafficcontrol/lib/go-log"
"github.com/apache/trafficcontrol/lib/go-tc"
util "github.com/apache/trafficcontrol/lib/go-util"
_ "github.com/lib/pq"
)
// PGConfig represents the configuration options available to the PG backend.
type PGConfig struct {
Host string `json:"host"`
Port string `json:"port"`
User string `json:"user"`
Password string `json:"password"`
SSLMode string `json:"sslmode"`
Database string `json:"database"`
KeyBase64 string `json:"aesKey"`
AESKey []byte
}
// PGBackend is the Postgres implementation of TVBackend.
type PGBackend struct {
sslKey pgSSLKeyTable
dnssec pgDNSSecTable
uriSigningKeys pgURISignKeyTable
urlSigKeys pgURLSigKeyTable
cfg PGConfig
db *sql.DB
}
// String returns a high level overview of the backend and its keys.
func (pg *PGBackend) String() string {
data := fmt.Sprintf("PG server %s@%s:%s\n", pg.cfg.User, pg.cfg.Host, pg.cfg.Port)
data += fmt.Sprintf("\tSSL Keys: %d\n", len(pg.sslKey.Records))
data += fmt.Sprintf("\tDNSSec Keys: %d\n", len(pg.dnssec.Records))
data += fmt.Sprintf("\tURI Signing Keys: %d\n", len(pg.uriSigningKeys.Records))
data += fmt.Sprintf("\tURL Sig Keys: %d\n", len(pg.urlSigKeys.Records))
return data
}
// Name returns the name for this backend.
func (pg *PGBackend) Name() string {
return "PG"
}
// ReadConfigFile takes in a filename and will read it into the backends config.
func (pg *PGBackend) ReadConfigFile(configFile string) error {
var err error
if err = UnmarshalConfig(configFile, &pg.cfg); err != nil {
return err
}
if pg.cfg.AESKey, err = base64.StdEncoding.DecodeString(pg.cfg.KeyBase64); err != nil {
return fmt.Errorf("unable to decode PG AESKey '%s': %w", pg.cfg.KeyBase64, err)
}
if err = util.ValidateAESKey(pg.cfg.AESKey); err != nil {
return fmt.Errorf("unable to validate PG AESKey '%s'", pg.cfg.KeyBase64)
}
return nil
}
// Insert takes the current keys and inserts them into the backend DB.
func (pg *PGBackend) Insert() error {
if err := pg.sslKey.insertKeys(pg.db); err != nil {
return err
}
if err := pg.dnssec.insertKeys(pg.db); err != nil {
return err
}
if err := pg.urlSigKeys.insertKeys(pg.db); err != nil {
return err
}
if err := pg.uriSigningKeys.insertKeys(pg.db); err != nil {
return err
}
return nil
}
// Start initiates the connection to the backend DB.
func (pg *PGBackend) Start() error {
sqlStr := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=%s", pg.cfg.User, pg.cfg.Password, pg.cfg.Host, pg.cfg.Port, pg.cfg.Database, pg.cfg.SSLMode)
db, err := sql.Open("postgres", sqlStr)
if err != nil {
sqlStr = strings.Replace(sqlStr, pg.cfg.Password, "*", 1)
return fmt.Errorf("unable to start PG client with connection string '%s': %w", sqlStr, err)
}
pg.db = db
pg.sslKey = pgSSLKeyTable{}
pg.dnssec = pgDNSSecTable{}
pg.urlSigKeys = pgURLSigKeyTable{}
pg.uriSigningKeys = pgURISignKeyTable{}
return nil
}
// ValidateKey validates that the keys are valid (in most cases, certain fields are not null).
func (pg *PGBackend) ValidateKey() []string {
var allErrs []string
if errs := pg.sslKey.validate(); errs != nil {
allErrs = append(allErrs, errs...)
}
if errs := pg.dnssec.validate(); errs != nil {
allErrs = append(allErrs, errs...)
}
if errs := pg.uriSigningKeys.validate(); errs != nil {
allErrs = append(allErrs, errs...)
}
if errs := pg.urlSigKeys.validate(); errs != nil {
allErrs = append(allErrs, errs...)
}
return allErrs
}
// Close terminates the connection to the backend DB.
func (pg *PGBackend) Close() error {
return pg.db.Close()
}
// Ping checks the connection to the backend DB.
func (pg *PGBackend) Ping() error {
return pg.db.Ping()
}
// Fetch gets all of the keys from the backend DB.
func (pg *PGBackend) Fetch() error {
if err := pg.sslKey.gatherKeys(pg.db); err != nil {
return err
}
if err := pg.dnssec.gatherKeys(pg.db); err != nil {
return err
}
if err := pg.urlSigKeys.gatherKeys(pg.db); err != nil {
return err
}
if err := pg.uriSigningKeys.gatherKeys(pg.db); err != nil {
return err
}
return nil
}
// GetSSLKeys converts the backends internal key representation into the common representation (SSLKey).
func (pg *PGBackend) GetSSLKeys() ([]SSLKey, error) {
if err := pg.sslKey.decrypt(pg.cfg.AESKey); err != nil {
return nil, err
}
return pg.sslKey.toGeneric(), nil
}
// SetSSLKeys takes in keys and converts & encrypts the data into the backends internal format.
func (pg *PGBackend) SetSSLKeys(keys []SSLKey) error {
pg.sslKey.fromGeneric(keys)
return pg.sslKey.encrypt(pg.cfg.AESKey)
}
// GetDNSSecKeys converts the backends internal key representation into the common representation (DNSSecKey).
func (pg *PGBackend) GetDNSSecKeys() ([]DNSSecKey, error) {
if err := pg.dnssec.decrypt(pg.cfg.AESKey); err != nil {
return nil, err
}
return pg.dnssec.toGeneric(), nil
}
// SetDNSSecKeys takes in keys and converts & encrypts the data into the backends internal format.
func (pg *PGBackend) SetDNSSecKeys(keys []DNSSecKey) error {
pg.dnssec.fromGeneric(keys)
return pg.dnssec.encrypt(pg.cfg.AESKey)
}
// GetURISignKeys converts the pg internal key representation into the common representation (URISignKey).
func (pg *PGBackend) GetURISignKeys() ([]URISignKey, error) {
if err := pg.uriSigningKeys.decrypt(pg.cfg.AESKey); err != nil {
return nil, err
}
return pg.uriSigningKeys.toGeneric(), nil
}
// SetURISignKeys takes in keys and converts & encrypts the data into the backends internal format.
func (pg *PGBackend) SetURISignKeys(keys []URISignKey) error {
pg.uriSigningKeys.fromGeneric(keys)
return pg.uriSigningKeys.encrypt(pg.cfg.AESKey)
}
// GetURLSigKeys converts the backends internal key representation into the common representation (URLSigKey).
func (pg *PGBackend) GetURLSigKeys() ([]URLSigKey, error) {
if err := pg.urlSigKeys.decrypt(pg.cfg.AESKey); err != nil {
return nil, err
}
return pg.urlSigKeys.toGeneric(), nil
}
// SetURLSigKeys takes in keys and converts & encrypts the data into the backends internal format.
func (pg *PGBackend) SetURLSigKeys(keys []URLSigKey) error {
pg.urlSigKeys.fromGeneric(keys)
return pg.urlSigKeys.encrypt(pg.cfg.AESKey)
}
type pgCommonRecord struct {
DataEncrypted []byte
}
type pgDNSSecRecord struct {
Key tc.DNSSECKeysTrafficVault
CDN string
pgCommonRecord
}
type pgDNSSecTable struct {
Records []pgDNSSecRecord
}
func (tbl *pgDNSSecTable) gatherKeys(db *sql.DB) error {
sz, err := getSize(db, "dnssec")
if err != nil {
log.Errorln("PGDNSSec gatherKeys: unable to determine size of dnssec table")
}
tbl.Records = make([]pgDNSSecRecord, sz)
query := "SELECT cdn, data from dnssec"
rows, err := db.Query(query)
if err != nil {
return fmt.Errorf("PGDNSSec gatherKeys: unable to run query '%s': %w", query, err)
}
defer log.Close(rows, "closing dnssec query")
i := 0
for rows.Next() {
if i > len(tbl.Records)-1 {
return fmt.Errorf("PGDNSSec gatherKeys got more results than expected %d", len(tbl.Records))
}
if err := rows.Scan(&tbl.Records[i].CDN, &tbl.Records[i].DataEncrypted); err != nil {
return fmt.Errorf("PGDNSSec gatherKeys unable to scan row: %w", err)
}
i += 1
}
return nil
}
func (tbl *pgDNSSecTable) decrypt(aesKey []byte) error {
for i, _ := range tbl.Records {
if err := decryptInto(aesKey, tbl.Records[i].DataEncrypted, &tbl.Records[i].Key); err != nil {
return fmt.Errorf("unable to decrypt into keys: %w", err)
}
}
return nil
}
func (tbl *pgDNSSecTable) encrypt(aesKey []byte) error {
for i, dns := range tbl.Records {
data, err := json.Marshal(&dns.Key)
if err != nil {
return fmt.Errorf("encrypt issue marshalling keys: %w", err)
}
dat, err := encrypt(data, aesKey)
if err != nil {
return fmt.Errorf("encrypt error: %w", err)
}
tbl.Records[i].DataEncrypted = dat
}
return nil
}
func (tbl *pgDNSSecTable) toGeneric() []DNSSecKey {
keys := make([]DNSSecKey, len(tbl.Records))
for i, record := range tbl.Records {
keys[i] = DNSSecKey{
CDN: record.CDN,
DNSSECKeysTrafficVault: record.Key,
}
}
return keys
}
func (tbl *pgDNSSecTable) fromGeneric(keys []DNSSecKey) {
tbl.Records = make([]pgDNSSecRecord, len(keys))
for i, key := range keys {
tbl.Records[i] = pgDNSSecRecord{
Key: key.DNSSECKeysTrafficVault,
CDN: key.CDN,
pgCommonRecord: pgCommonRecord{
DataEncrypted: nil,
},
}
}
}
func (tbl *pgDNSSecTable) validate() []string {
for _, record := range tbl.Records {
if record.DataEncrypted == nil && len(record.Key) > 0 {
return []string{fmt.Sprintf("DNSSEC Key CDN '%s': DataEncrypted is blank!", record.CDN)}
}
}
return nil
}
func (tbl *pgDNSSecTable) insertKeys(db *sql.DB) error {
queryBase := "INSERT INTO dnssec (cdn, data) VALUES %s ON CONFLICT (cdn) DO UPDATE SET data = EXCLUDED.data"
stride := 2
queryArgs := make([]interface{}, len(tbl.Records)*stride)
for i, record := range tbl.Records {
j := i * stride
queryArgs[j] = record.CDN
queryArgs[j+1] = record.DataEncrypted
}
return insertIntoTable(db, queryBase, stride, queryArgs)
}
type pgSSLKeyRecord struct {
Keys tc.DeliveryServiceSSLKeys
pgCommonRecord
// These records are stored on the table but are duplicated
DeliveryService string
CDN string
Version string
}
type pgSSLKeyTable struct {
Records []pgSSLKeyRecord
}
func (tbl *pgSSLKeyTable) insertKeys(db *sql.DB) error {
queryBase := "INSERT INTO sslkey (deliveryservice, data, cdn, version) VALUES %s ON CONFLICT (deliveryservice,cdn,version) DO UPDATE SET data = EXCLUDED.data"
stride := 4
queryArgs := make([]interface{}, len(tbl.Records)*stride)
for i, record := range tbl.Records {
j := i * stride
queryArgs[j] = record.DeliveryService
queryArgs[j+1] = record.DataEncrypted
queryArgs[j+2] = record.CDN
queryArgs[j+3] = record.Version
}
return insertIntoTable(db, queryBase, 4, queryArgs)
}
func (tbl *pgSSLKeyTable) gatherKeys(db *sql.DB) error {
sz, err := getSize(db, "sslkey")
if err != nil {
return fmt.Errorf("PGSSLKey gatherKeys unable to determine size of sslkey table: %w", err)
}
tbl.Records = make([]pgSSLKeyRecord, sz)
query := "SELECT data, deliveryservice, cdn, version from sslkey"
rows, err := db.Query(query)
if err != nil {
return fmt.Errorf("PGSSLKey gatherKeys unable to run query '%s': %w", query, err)
}
defer log.Close(rows, "closing sslkey query")
i := 0
for rows.Next() {
if i > len(tbl.Records)-1 {
return fmt.Errorf("PGSSLKey gatherKeys: got more results than expected")
}
if err := rows.Scan(&tbl.Records[i].DataEncrypted, &tbl.Records[i].DeliveryService, &tbl.Records[i].CDN, &tbl.Records[i].Version); err != nil {
return fmt.Errorf("PGSSLKey gatherKeys unable to scan %d row: %w", i, err)
}
i += 1
}
return nil
}
func (tbl *pgSSLKeyTable) decrypt(aesKey []byte) error {
for i, key := range tbl.Records {
if err := decryptInto(aesKey, key.DataEncrypted, &tbl.Records[i].Keys); err != nil {
return fmt.Errorf("unable to decrypt into keys: %w", err)
}
}
return nil
}
func (tbl *pgSSLKeyTable) encrypt(aesKey []byte) error {
for i, key := range tbl.Records {
data, err := json.Marshal(key.Keys)
if err != nil {
return fmt.Errorf("encrypt issue marshalling keys: %w", err)
}
dat, err := encrypt(data, aesKey)
if err != nil {
return fmt.Errorf("encrypt error: %w", err)
}
tbl.Records[i].DataEncrypted = dat
}
return nil
}
func (tbl *pgSSLKeyTable) toGeneric() []SSLKey {
keys := make([]SSLKey, len(tbl.Records))
for i, record := range tbl.Records {
keys[i] = SSLKey{
DeliveryServiceSSLKeys: record.Keys,
Version: record.Version,
}
}
return keys
}
func (tbl *pgSSLKeyTable) fromGeneric(keys []SSLKey) {
tbl.Records = make([]pgSSLKeyRecord, len(keys))
for i, key := range keys {
tbl.Records[i] = pgSSLKeyRecord{
Keys: key.DeliveryServiceSSLKeys,
pgCommonRecord: pgCommonRecord{
DataEncrypted: nil,
},
DeliveryService: key.DeliveryService,
CDN: key.CDN,
Version: key.Version,
}
}
}
func (tbl *pgSSLKeyTable) validate() []string {
defaultKey := tc.DeliveryServiceSSLKeys{}
var errs []string
fmtStr := "SSL Key DS '%s': %s"
for _, record := range tbl.Records {
if record.Keys.DeliveryService == "" {
errs = append(errs, fmt.Sprintf(fmtStr, record.DeliveryService, "DS is blank!"))
} else if record.Keys == defaultKey {
errs = append(errs, fmt.Sprintf(fmtStr, record.DeliveryService, "DS SSL Keys are default!"))
} else if record.Keys.Key == "" {
errs = append(errs, fmt.Sprintf(fmtStr, record.DeliveryService, "Key is blank!"))
} else if record.Keys.CDN == "" {
errs = append(errs, fmt.Sprintf(fmtStr, record.DeliveryService, "CDN is blank!"))
} else if record.DataEncrypted == nil {
errs = append(errs, fmt.Sprintf(fmtStr, record.DeliveryService, "DataEncrypted is blank!"))
} else if record.Version == "" {
errs = append(errs, fmt.Sprintf(fmtStr, record.DeliveryService, "Version is blank!"))
}
}
return errs
}
type pgURLSigKeyRecord struct {
Keys tc.URLSigKeys
DeliveryService string
pgCommonRecord
}
type pgURLSigKeyTable struct {
Records []pgURLSigKeyRecord
}
func (tbl *pgURLSigKeyTable) insertKeys(db *sql.DB) error {
queryBase := "INSERT INTO url_sig_key (deliveryservice, data) VALUES %s ON CONFLICT (deliveryservice) DO UPDATE set data = EXCLUDED.data"
stride := 2
queryArgs := make([]interface{}, len(tbl.Records)*stride)
for i, record := range tbl.Records {
j := i * stride
queryArgs[j] = record.DeliveryService
queryArgs[j+1] = record.DataEncrypted
}
return insertIntoTable(db, queryBase, stride, queryArgs)
}
func (tbl *pgURLSigKeyTable) gatherKeys(db *sql.DB) error {
sz, err := getSize(db, "url_sig_key")
if err != nil {
log.Errorln("PGURLSigKey gatherKeys: unable to determine url_sig_key table size")
}
tbl.Records = make([]pgURLSigKeyRecord, sz)
query := "SELECT deliveryservice, data from url_sig_key"
rows, err := db.Query(query)
if err != nil {
return fmt.Errorf("PGURLSigKey gatherKeys error running query '%s': %w", query, err)
}
defer log.Close(rows, "closing url_sig_key query")
i := 0
for rows.Next() {
if i > len(tbl.Records)-1 {
return fmt.Errorf("PGURLSigKey gatherKeys: got more results than expected %d", len(tbl.Records))
}
if err := rows.Scan(&tbl.Records[i].DeliveryService, &tbl.Records[i].DataEncrypted); err != nil {
return fmt.Errorf("PGURLSigKey gatherKeys: unable to scan row: %w", err)
}
i += 1
}
return nil
}
func (tbl *pgURLSigKeyTable) decrypt(aesKey []byte) error {
for i, sig := range tbl.Records {
if err := decryptInto(aesKey, sig.DataEncrypted, &tbl.Records[i].Keys); err != nil {
return fmt.Errorf("unable to decrypt into keys: %w", err)
}
}
return nil
}
func (tbl *pgURLSigKeyTable) encrypt(aesKey []byte) error {
for i, sig := range tbl.Records {
data, err := json.Marshal(&sig.Keys)
if err != nil {
return fmt.Errorf("encrypt issue marshalling keys: %w", err)
}
dat, err := encrypt(data, aesKey)
if err != nil {
return fmt.Errorf("encrypt error: %w", err)
}
tbl.Records[i].DataEncrypted = dat
}
return nil
}
func (tbl *pgURLSigKeyTable) toGeneric() []URLSigKey {
keys := make([]URLSigKey, len(tbl.Records))
for i, record := range tbl.Records {
keys[i] = URLSigKey{
DeliveryService: record.DeliveryService,
URLSigKeys: record.Keys,
}
}
return keys
}
func (tbl *pgURLSigKeyTable) fromGeneric(keys []URLSigKey) {
tbl.Records = make([]pgURLSigKeyRecord, len(keys))
for i, key := range keys {
tbl.Records[i] = pgURLSigKeyRecord{
Keys: key.URLSigKeys,
DeliveryService: key.DeliveryService,
pgCommonRecord: pgCommonRecord{
DataEncrypted: nil,
},
}
}
}
func (tbl *pgURLSigKeyTable) validate() []string {
for _, record := range tbl.Records {
if record.DataEncrypted == nil && len(record.Keys) > 0 {
return []string{fmt.Sprintf("URL Sig Key DS '%s': DataEncrypted is blank!", record.DeliveryService)}
}
}
return nil
}
type pgURISignKeyRecord struct {
Keys map[string]tc.URISignerKeyset
DeliveryService string
pgCommonRecord
}
type pgURISignKeyTable struct {
Records []pgURISignKeyRecord
}
func (tbl *pgURISignKeyTable) insertKeys(db *sql.DB) error {
queryBase := "INSERT INTO uri_signing_key (deliveryservice, data) VALUES %s ON CONFLICT (deliveryservice) DO UPDATE SET data = EXCLUDED.data"
stride := 2
queryArgs := make([]interface{}, len(tbl.Records)*stride)
for i, record := range tbl.Records {
j := i * stride
queryArgs[j] = record.DeliveryService
queryArgs[j+1] = record.DataEncrypted
}
return insertIntoTable(db, queryBase, stride, queryArgs)
}
func (tbl *pgURISignKeyTable) gatherKeys(db *sql.DB) error {
sz, err := getSize(db, "uri_signing_key")
if err != nil {
log.Errorln("PGURISignKey gatherKeys: unable to determine size of uri_signing_key table")
}
tbl.Records = make([]pgURISignKeyRecord, sz)
query := "SELECT deliveryservice, data from uri_signing_key"
rows, err := db.Query(query)
if err != nil {
return fmt.Errorf("PGURISignKey gatherKeys error while running query '%s': %w", query, err)
}
defer log.Close(rows, "closing uri_signing_key table")
i := 0
for rows.Next() {
if i > len(tbl.Records)-1 {
return fmt.Errorf("PGURISignKey gatherKeys: got more results than expected %d", len(tbl.Records))
}
if err := rows.Scan(&tbl.Records[i].DeliveryService, &tbl.Records[i].DataEncrypted); err != nil {
return fmt.Errorf("PGURISignKey gatherKeys: unable to scan row: %w", err)
}
i += 1
}
return nil
}
func (tbl *pgURISignKeyTable) decrypt(aesKey []byte) error {
for i, sign := range tbl.Records {
if err := decryptInto(aesKey, sign.DataEncrypted, &tbl.Records[i].Keys); err != nil {
return fmt.Errorf("unable to decrypt into keys: %w", err)
}
}
return nil
}
func (tbl *pgURISignKeyTable) encrypt(aesKey []byte) error {
for i, sign := range tbl.Records {
data, err := json.Marshal(sign.Keys)
if err != nil {
return fmt.Errorf("encrypt issue marshalling keys: %w", err)
}
dat, err := encrypt(data, aesKey)
if err != nil {
return fmt.Errorf("encrypt error: %w", err)
}
tbl.Records[i].DataEncrypted = dat
}
return nil
}
func (tbl *pgURISignKeyTable) toGeneric() []URISignKey {
keys := make([]URISignKey, len(tbl.Records))
for i, record := range tbl.Records {
keys[i] = URISignKey{
DeliveryService: record.DeliveryService,
Keys: record.Keys,
}
}
return keys
}
func (tbl *pgURISignKeyTable) fromGeneric(keys []URISignKey) {
tbl.Records = make([]pgURISignKeyRecord, len(keys))
for i, key := range keys {
tbl.Records[i] = pgURISignKeyRecord{
Keys: key.Keys,
DeliveryService: key.DeliveryService,
pgCommonRecord: pgCommonRecord{
DataEncrypted: nil,
},
}
}
}
func (tbl *pgURISignKeyTable) validate() []string {
for _, record := range tbl.Records {
if record.DataEncrypted == nil && len(record.Keys) > 0 {
return []string{fmt.Sprintf("URI Signing Key DS '%s': DataEncrypted is blank!", record.DeliveryService)}
}
}
return nil
}
func getSize(db *sql.DB, table string) (int64, error) {
rows, err := db.Query("SELECT COUNT(*) FROM " + table)
if err != nil {
return 0, err
}
var numRows int64
if !rows.Next() {
return 0, errors.New("no results returned for: " + table)
}
if err = rows.Scan(&numRows); err != nil {
return 0, fmt.Errorf("error reading number of results for %s: %w", table, err)
}
return numRows, nil
}
func decrypt(record []byte, aesKey []byte) ([]byte, error) {
unencrypted, err := util.AESDecrypt(record, aesKey)
if err != nil {
return nil, fmt.Errorf("unable to decrypt: %w", err)
}
return unencrypted, nil
}
func encrypt(record []byte, aesKey []byte) ([]byte, error) {
encrypted, err := util.AESEncrypt(record, aesKey)
if err != nil {
return nil, err
}
return encrypted, nil
}
func decryptInto(aesKey []byte, encData []byte, value interface{}) error {
data, err := decrypt(encData, aesKey)
if err != nil {
return err
}
if len(data) == 0 {
return errors.New("decrypted data is empty")
}
if err = json.Unmarshal(data, &value); err != nil {
return err
}
return nil
}
func insertIntoTable(db *sql.DB, queryBase string, stride int, queryArgs []interface{}) error {
if len(queryArgs) == 0 {
return nil
}
rows := len(queryArgs) / stride
workStr := ""
queryValueStr := make([]string, rows)
for i, _ := range queryArgs {
rowIndex := i % stride
rowGroup := i / stride
if rowIndex == 0 && i > 0 {
queryValueStr[rowGroup-1] = "(" + workStr + ")"
workStr = ""
}
if rowIndex == 0 {
workStr += "$"
} else {
workStr += ",$"
}
workStr += strconv.Itoa(i + 1)
}
queryValueStr[len(queryValueStr)-1] = "(" + workStr + ")"
query := fmt.Sprintf(queryBase, strings.Join(queryValueStr, ","))
tx, err := db.Begin()
if err != nil {
return fmt.Errorf("unable to open db transaction: %w", err)
}
res, err := tx.Exec(query, queryArgs...)
if err != nil {
return rollback(tx, fmt.Errorf("error executing query '%s': %w", query, err))
}
if rows, err := res.RowsAffected(); err != nil {
return rollback(tx, fmt.Errorf("error getting rows affected: %w", err))
} else if rows != int64(len(queryValueStr)) {
return rollback(tx, fmt.Errorf("wanted to insert %d rows, but inserted %d\n", len(queryValueStr), rows))
}
return tx.Commit()
}
func rollback(tx *sql.Tx, addError error) error {
if err := tx.Rollback(); err != nil {
return fmt.Errorf("encountered error rolling back transaction %w %s", err, addError.Error())
}
return addError
}