-
Notifications
You must be signed in to change notification settings - Fork 51
/
key.go
858 lines (725 loc) 路 18.5 KB
/
key.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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
package postgresql
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"strings"
internal_errors "github.com/bricks-cloud/bricksllm/internal/errors"
"github.com/bricks-cloud/bricksllm/internal/key"
"github.com/lib/pq"
)
func (s *Store) CreateKeysTable() error {
createTableQuery := `
CREATE TABLE IF NOT EXISTS keys (
name VARCHAR(255) NOT NULL,
created_at BIGINT NOT NULL,
updated_at BIGINT NOT NULL,
tags VARCHAR(255)[],
revoked BOOLEAN NOT NULL,
key_id VARCHAR(255) PRIMARY KEY,
key VARCHAR(255) NOT NULL,
revoked_reason VARCHAR(255),
cost_limit_in_usd FLOAT8,
cost_limit_in_usd_over_time FLOAT8,
cost_limit_in_usd_unit VARCHAR(255),
rate_limit_over_time INT,
rate_limit_unit VARCHAR(255),
ttl VARCHAR(255)
)`
ctxTimeout, cancel := context.WithTimeout(context.Background(), s.wt)
defer cancel()
_, err := s.db.ExecContext(ctxTimeout, createTableQuery)
if err != nil {
return err
}
return nil
}
func (s *Store) AlterKeysTable() error {
alterTableQuery := `
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_constraint
WHERE conname = 'key_uniqueness'
) THEN
ALTER TABLE keys
ADD CONSTRAINT key_uniqueness UNIQUE (key);
END IF;
END
$$;
ALTER TABLE keys ADD COLUMN IF NOT EXISTS setting_id VARCHAR(255), ADD COLUMN IF NOT EXISTS allowed_paths JSONB, ADD COLUMN IF NOT EXISTS setting_ids VARCHAR(255)[] NOT NULL DEFAULT ARRAY[]::VARCHAR(255)[], ADD COLUMN IF NOT EXISTS should_log_request BOOLEAN NOT NULL DEFAULT FALSE, ADD COLUMN IF NOT EXISTS should_log_response BOOLEAN NOT NULL DEFAULT FALSE, ADD COLUMN IF NOT EXISTS rotation_enabled BOOLEAN NOT NULL DEFAULT FALSE, ADD COLUMN IF NOT EXISTS policy_id VARCHAR(255) NOT NULL DEFAULT '', ADD COLUMN IF NOT EXISTS is_key_not_hashed BOOLEAN NOT NULL DEFAULT FALSE;
`
ctxTimeout, cancel := context.WithTimeout(context.Background(), s.wt)
defer cancel()
_, err := s.db.ExecContext(ctxTimeout, alterTableQuery)
if err != nil {
return err
}
return nil
}
func (s *Store) CreateCreateAtIndexForKeys() error {
createIndexQuery := `
CREATE INDEX IF NOT EXISTS created_at_idx ON keys(created_at);
`
ctxTimeout, cancel := context.WithTimeout(context.Background(), s.wt)
defer cancel()
_, err := s.db.ExecContext(ctxTimeout, createIndexQuery)
if err != nil {
return err
}
return nil
}
func (s *Store) GetKeys(tags, keyIds []string, provider string) ([]*key.ResponseKey, error) {
ctxTimeout, cancel := context.WithTimeout(context.Background(), s.rt)
defer cancel()
args := []any{}
query := ""
selectionQuery := "SELECT * FROM keys "
index := 1
if len(tags) != 0 {
args = append(args, pq.Array(tags))
index += 1
selectionQuery += "WHERE tags @> $1"
}
if len(keyIds) != 0 {
args = append(args, pq.Array(keyIds))
if index != 1 {
selectionQuery += fmt.Sprintf(" AND key_id = ANY($%d)", index)
}
if index == 1 {
selectionQuery += fmt.Sprintf("WHERE key_id = ANY($%d)", index)
}
index += 1
}
query = selectionQuery
if len(provider) != 0 {
args = append(args, provider)
query = fmt.Sprintf(`
WITH keys_table AS
(
%s
),provider_settings_table AS
(
SELECT * FROM provider_settings WHERE $%d = provider
)
SELECT DISTINCT keys_table.*
FROM keys_table
JOIN provider_settings_table
ON keys_table.setting_id = provider_settings_table.id
OR provider_settings_table.id = ANY(keys_table.setting_ids);
`, selectionQuery, index)
}
rows, err := s.db.QueryContext(ctxTimeout, query, args...)
if err != nil {
if err == sql.ErrNoRows {
return nil, internal_errors.NewNotFoundError("keys are not found")
}
return nil, err
}
defer rows.Close()
keys := []*key.ResponseKey{}
for rows.Next() {
var k key.ResponseKey
var settingId sql.NullString
var data []byte
if err := rows.Scan(
&k.Name,
&k.CreatedAt,
&k.UpdatedAt,
pq.Array(&k.Tags),
&k.Revoked,
&k.KeyId,
&k.Key,
&k.RevokedReason,
&k.CostLimitInUsd,
&k.CostLimitInUsdOverTime,
&k.CostLimitInUsdUnit,
&k.RateLimitOverTime,
&k.RateLimitUnit,
&k.Ttl,
&settingId,
&data,
pq.Array(&k.SettingIds),
&k.ShouldLogRequest,
&k.ShouldLogResponse,
&k.RotationEnabled,
&k.PolicyId,
&k.IsKeyNotHashed,
); err != nil {
return nil, err
}
pk := &k
pk.SettingId = settingId.String
if len(data) != 0 {
pathConfigs := []key.PathConfig{}
if err := json.Unmarshal(data, &pathConfigs); err != nil {
return nil, err
}
pk.AllowedPaths = pathConfigs
}
keys = append(keys, pk)
}
return keys, nil
}
func (s *Store) GetKeysV2(tags, keyIds []string, revoked *bool, limit, offset int, name, order string, returnCount bool) (*key.GetKeysResponse, error) {
ctxTimeout, cancel := context.WithTimeout(context.Background(), s.rt)
defer cancel()
args := []any{}
countQuery := "SELECT COUNT(*) FROM keys"
query := "SELECT * FROM keys"
index := 1
if len(tags) != 0 || len(keyIds) != 0 || revoked != nil || len(name) != 0 {
query += " WHERE "
countQuery += " WHERE "
}
if len(tags) != 0 {
args = append(args, pq.Array(tags))
index += 1
query += "tags @> $1"
countQuery += "tags @> $1"
}
if len(keyIds) != 0 {
if index > 1 {
query += " AND "
countQuery += " AND "
}
args = append(args, pq.Array(keyIds))
query += fmt.Sprintf("key_id = ANY($%d)", index)
countQuery += fmt.Sprintf("key_id = ANY($%d)", index)
index += 1
}
if revoked != nil {
if index > 1 {
query += " AND "
countQuery += " AND "
}
args = append(args, *revoked)
query += fmt.Sprintf("revoked = $%d", index)
countQuery += fmt.Sprintf("revoked = $%d", index)
}
if len(name) != 0 {
if index > 1 {
query += " AND "
countQuery += " AND "
}
query += fmt.Sprintf("LOWER(name) LIKE LOWER('%%%s%%')", name)
countQuery += fmt.Sprintf("LOWER(name) LIKE LOWER('%%%s%%')", name)
}
qorder := "DESC"
if strings.ToLower(order) == "asc" {
qorder = "ASC"
}
query += fmt.Sprintf(" ORDER BY created_at %s ", qorder)
if limit != 0 {
query += fmt.Sprintf("OFFSET %d LIMIT %d", offset, limit)
}
rows, err := s.db.QueryContext(ctxTimeout, query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
keys := []*key.ResponseKey{}
for rows.Next() {
var k key.ResponseKey
var settingId sql.NullString
var data []byte
if err := rows.Scan(
&k.Name,
&k.CreatedAt,
&k.UpdatedAt,
pq.Array(&k.Tags),
&k.Revoked,
&k.KeyId,
&k.Key,
&k.RevokedReason,
&k.CostLimitInUsd,
&k.CostLimitInUsdOverTime,
&k.CostLimitInUsdUnit,
&k.RateLimitOverTime,
&k.RateLimitUnit,
&k.Ttl,
&settingId,
&data,
pq.Array(&k.SettingIds),
&k.ShouldLogRequest,
&k.ShouldLogResponse,
&k.RotationEnabled,
&k.PolicyId,
&k.IsKeyNotHashed,
); err != nil {
return nil, err
}
pk := &k
pk.SettingId = settingId.String
if len(data) != 0 {
pathConfigs := []key.PathConfig{}
if err := json.Unmarshal(data, &pathConfigs); err != nil {
return nil, err
}
pk.AllowedPaths = pathConfigs
}
keys = append(keys, pk)
}
result := &key.GetKeysResponse{
Keys: keys,
}
if returnCount {
rows, err := s.db.QueryContext(ctxTimeout, countQuery, args...)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
count := 0
if err := rows.Scan(
&count,
); err != nil {
return nil, err
}
result.Count = count
}
}
return result, nil
}
func (s *Store) GetKeyByHash(hash string) (*key.ResponseKey, error) {
ctxTimeout, cancel := context.WithTimeout(context.Background(), s.rt)
defer cancel()
var k key.ResponseKey
var settingId sql.NullString
var data []byte
err := s.db.QueryRowContext(ctxTimeout, "SELECT * FROM keys WHERE key = $1", hash).Scan(
&k.Name,
&k.CreatedAt,
&k.UpdatedAt,
pq.Array(&k.Tags),
&k.Revoked,
&k.KeyId,
&k.Key,
&k.RevokedReason,
&k.CostLimitInUsd,
&k.CostLimitInUsdOverTime,
&k.CostLimitInUsdUnit,
&k.RateLimitOverTime,
&k.RateLimitUnit,
&k.Ttl,
&settingId,
&data,
pq.Array(&k.SettingIds),
&k.ShouldLogRequest,
&k.ShouldLogResponse,
&k.RotationEnabled,
&k.PolicyId,
&k.IsKeyNotHashed,
)
if err != nil {
if err == sql.ErrNoRows {
return nil, internal_errors.NewNotFoundError("key is not found using hash")
}
return nil, err
}
k.SettingId = settingId.String
if len(data) != 0 {
pathConfigs := []key.PathConfig{}
if err := json.Unmarshal(data, &pathConfigs); err != nil {
return nil, err
}
k.AllowedPaths = pathConfigs
}
return &k, nil
}
func (s *Store) GetKey(keyId string) (*key.ResponseKey, error) {
ctxTimeout, cancel := context.WithTimeout(context.Background(), s.rt)
defer cancel()
rows, err := s.db.QueryContext(ctxTimeout, "SELECT * FROM keys WHERE key_id = $1", keyId)
if err != nil {
return nil, err
}
defer rows.Close()
keys := []*key.ResponseKey{}
for rows.Next() {
var k key.ResponseKey
var settingId sql.NullString
var data []byte
if err := rows.Scan(
&k.Name,
&k.CreatedAt,
&k.UpdatedAt,
pq.Array(&k.Tags),
&k.Revoked,
&k.KeyId,
&k.Key,
&k.RevokedReason,
&k.CostLimitInUsd,
&k.CostLimitInUsdOverTime,
&k.CostLimitInUsdUnit,
&k.RateLimitOverTime,
&k.RateLimitUnit,
&k.Ttl,
&settingId,
&data,
pq.Array(&k.SettingIds),
&k.ShouldLogRequest,
&k.ShouldLogResponse,
&k.RotationEnabled,
&k.PolicyId,
&k.IsKeyNotHashed,
); err != nil {
return nil, err
}
pk := &k
pk.SettingId = settingId.String
if len(data) != 0 {
pathConfigs := []key.PathConfig{}
if err := json.Unmarshal(data, &pathConfigs); err != nil {
return nil, err
}
pk.AllowedPaths = pathConfigs
}
keys = append(keys, pk)
}
if len(keys) == 0 {
return nil, nil
}
return keys[0], nil
}
func (s *Store) GetAllKeys() ([]*key.ResponseKey, error) {
ctxTimeout, cancel := context.WithTimeout(context.Background(), s.rt)
defer cancel()
rows, err := s.db.QueryContext(ctxTimeout, "SELECT * FROM keys")
if err != nil {
return nil, err
}
defer rows.Close()
keys := []*key.ResponseKey{}
for rows.Next() {
var k key.ResponseKey
var settingId sql.NullString
var data []byte
if err := rows.Scan(
&k.Name,
&k.CreatedAt,
&k.UpdatedAt,
pq.Array(&k.Tags),
&k.Revoked,
&k.KeyId,
&k.Key,
&k.RevokedReason,
&k.CostLimitInUsd,
&k.CostLimitInUsdOverTime,
&k.CostLimitInUsdUnit,
&k.RateLimitOverTime,
&k.RateLimitUnit,
&k.Ttl,
&settingId,
&data,
pq.Array(&k.SettingIds),
&k.ShouldLogRequest,
&k.ShouldLogResponse,
&k.RotationEnabled,
&k.PolicyId,
&k.IsKeyNotHashed,
); err != nil {
return nil, err
}
pk := &k
pk.SettingId = settingId.String
if len(data) != 0 {
pathConfigs := []key.PathConfig{}
if err := json.Unmarshal(data, &pathConfigs); err != nil {
return nil, err
}
pk.AllowedPaths = pathConfigs
}
keys = append(keys, pk)
}
return keys, nil
}
func (s *Store) GetUpdatedKeys(updatedAt int64) ([]*key.ResponseKey, error) {
ctxTimeout, cancel := context.WithTimeout(context.Background(), s.rt)
defer cancel()
rows, err := s.db.QueryContext(ctxTimeout, "SELECT * FROM keys WHERE updated_at >= $1", updatedAt)
if err != nil {
return nil, err
}
defer rows.Close()
keys := []*key.ResponseKey{}
for rows.Next() {
var k key.ResponseKey
var settingId sql.NullString
var data []byte
if err := rows.Scan(
&k.Name,
&k.CreatedAt,
&k.UpdatedAt,
pq.Array(&k.Tags),
&k.Revoked,
&k.KeyId,
&k.Key,
&k.RevokedReason,
&k.CostLimitInUsd,
&k.CostLimitInUsdOverTime,
&k.CostLimitInUsdUnit,
&k.RateLimitOverTime,
&k.RateLimitUnit,
&k.Ttl,
&settingId,
&data,
pq.Array(&k.SettingIds),
&k.ShouldLogRequest,
&k.ShouldLogResponse,
&k.RotationEnabled,
&k.PolicyId,
&k.IsKeyNotHashed,
); err != nil {
return nil, err
}
pk := &k
pk.SettingId = settingId.String
if len(data) != 0 {
pathConfigs := []key.PathConfig{}
if err := json.Unmarshal(data, &pathConfigs); err != nil {
return nil, err
}
pk.AllowedPaths = pathConfigs
}
keys = append(keys, pk)
}
return keys, nil
}
func (s *Store) UpdateKey(id string, uk *key.UpdateKey) (*key.ResponseKey, error) {
fields := []string{}
counter := 2
values := []any{
id,
}
if len(uk.Name) != 0 {
values = append(values, uk.Name)
fields = append(fields, fmt.Sprintf("name = $%d", counter))
counter++
}
if uk.UpdatedAt != 0 {
values = append(values, uk.UpdatedAt)
fields = append(fields, fmt.Sprintf("updated_at = $%d", counter))
counter++
}
if len(uk.Tags) != 0 {
values = append(values, sliceToSqlStringArray(uk.Tags))
fields = append(fields, fmt.Sprintf("tags = $%d", counter))
counter++
}
if uk.Revoked != nil {
if *uk.Revoked && len(uk.RevokedReason) != 0 {
values = append(values, uk.RevokedReason)
fields = append(fields, fmt.Sprintf("revoked_reason = $%d", counter))
counter++
}
if !*uk.Revoked {
values = append(values, "")
fields = append(fields, fmt.Sprintf("revoked_reason = $%d", counter))
counter++
}
values = append(values, uk.Revoked)
fields = append(fields, fmt.Sprintf("revoked = $%d", counter))
counter++
}
if uk.CostLimitInUsd != nil {
values = append(values, *uk.CostLimitInUsd)
fields = append(fields, fmt.Sprintf("cost_limit_in_usd = $%d", counter))
counter++
}
if uk.CostLimitInUsdOverTime != nil {
values = append(values, *uk.CostLimitInUsdOverTime)
fields = append(fields, fmt.Sprintf("cost_limit_in_usd_over_time = $%d", counter))
counter++
}
if uk.CostLimitInUsdUnit != nil {
values = append(values, *uk.CostLimitInUsdUnit)
fields = append(fields, fmt.Sprintf("cost_limit_in_usd_unit = $%d", counter))
counter++
}
if uk.RateLimitOverTime != nil {
values = append(values, *uk.RateLimitOverTime)
fields = append(fields, fmt.Sprintf("rate_limit_over_time = $%d", counter))
counter++
}
if uk.RateLimitUnit != nil {
values = append(values, *uk.RateLimitUnit)
fields = append(fields, fmt.Sprintf("rate_limit_unit = $%d", counter))
counter++
}
if len(uk.SettingId) != 0 {
values = append(values, uk.SettingId)
fields = append(fields, fmt.Sprintf("setting_id = $%d", counter))
counter++
}
if len(uk.SettingIds) != 0 {
values = append(values, sliceToSqlStringArray(uk.SettingIds))
fields = append(fields, fmt.Sprintf("setting_ids = $%d", counter))
counter++
}
if uk.ShouldLogRequest != nil {
values = append(values, *uk.ShouldLogRequest)
fields = append(fields, fmt.Sprintf("should_log_request = $%d", counter))
counter++
}
if uk.ShouldLogResponse != nil {
values = append(values, *uk.ShouldLogResponse)
fields = append(fields, fmt.Sprintf("should_log_response = $%d", counter))
counter++
}
if uk.RotationEnabled != nil {
values = append(values, *uk.RotationEnabled)
fields = append(fields, fmt.Sprintf("rotation_enabled = $%d", counter))
counter++
}
if uk.AllowedPaths != nil {
data, err := json.Marshal(uk.AllowedPaths)
if err != nil {
return nil, err
}
values = append(values, data)
fields = append(fields, fmt.Sprintf("allowed_paths = $%d", counter))
}
if uk.PolicyId != nil {
values = append(values, *uk.PolicyId)
fields = append(fields, fmt.Sprintf("policy_id = $%d", counter))
counter++
}
if len(uk.Key) != 0 {
values = append(values, uk.Key)
fields = append(fields, fmt.Sprintf("key = $%d", counter))
counter++
}
query := fmt.Sprintf("UPDATE keys SET %s WHERE key_id = $1 RETURNING *;", strings.Join(fields, ","))
ctxTimeout, cancel := context.WithTimeout(context.Background(), s.wt)
defer cancel()
var k key.ResponseKey
var settingId sql.NullString
var data []byte
if err := s.db.QueryRowContext(ctxTimeout, query, values...).Scan(
&k.Name,
&k.CreatedAt,
&k.UpdatedAt,
pq.Array(&k.Tags),
&k.Revoked,
&k.KeyId,
&k.Key,
&k.RevokedReason,
&k.CostLimitInUsd,
&k.CostLimitInUsdOverTime,
&k.CostLimitInUsdUnit,
&k.RateLimitOverTime,
&k.RateLimitUnit,
&k.Ttl,
&settingId,
&data,
pq.Array(&k.SettingIds),
&k.ShouldLogRequest,
&k.ShouldLogResponse,
&k.RotationEnabled,
&k.PolicyId,
&k.IsKeyNotHashed,
); err != nil {
if err == sql.ErrNoRows {
return nil, internal_errors.NewNotFoundError(fmt.Sprintf("key not found for id: %s", id))
}
return nil, err
}
pk := &k
pk.SettingId = settingId.String
if len(data) != 0 {
pathConfigs := []key.PathConfig{}
if err := json.Unmarshal(data, &pathConfigs); err != nil {
return nil, err
}
pk.AllowedPaths = pathConfigs
}
return pk, nil
}
func (s *Store) CreateKey(rk *key.RequestKey) (*key.ResponseKey, error) {
query := `
INSERT INTO keys (name, created_at, updated_at, tags, revoked, key_id, key, revoked_reason, cost_limit_in_usd, cost_limit_in_usd_over_time, cost_limit_in_usd_unit, rate_limit_over_time, rate_limit_unit, ttl, setting_id, allowed_paths, setting_ids, should_log_request, should_log_response, rotation_enabled, policy_id, is_key_not_hashed)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22)
RETURNING *;
`
rdata, err := json.Marshal(rk.AllowedPaths)
if err != nil {
return nil, err
}
values := []any{
rk.Name,
rk.CreatedAt,
rk.UpdatedAt,
pq.Array(rk.Tags),
false,
rk.KeyId,
rk.Key,
"",
rk.CostLimitInUsd,
rk.CostLimitInUsdOverTime,
rk.CostLimitInUsdUnit,
rk.RateLimitOverTime,
rk.RateLimitUnit,
rk.Ttl,
rk.SettingId,
rdata,
sliceToSqlStringArray(rk.SettingIds),
rk.ShouldLogRequest,
rk.ShouldLogResponse,
rk.RotationEnabled,
rk.PolicyId,
rk.IsKeyNotHashed,
}
ctxTimeout, cancel := context.WithTimeout(context.Background(), s.wt)
defer cancel()
var k key.ResponseKey
var settingId sql.NullString
var data []byte
if err := s.db.QueryRowContext(ctxTimeout, query, values...).Scan(
&k.Name,
&k.CreatedAt,
&k.UpdatedAt,
pq.Array(&k.Tags),
&k.Revoked,
&k.KeyId,
&k.Key,
&k.RevokedReason,
&k.CostLimitInUsd,
&k.CostLimitInUsdOverTime,
&k.CostLimitInUsdUnit,
&k.RateLimitOverTime,
&k.RateLimitUnit,
&k.Ttl,
&settingId,
&data,
pq.Array(&k.SettingIds),
&k.ShouldLogRequest,
&k.ShouldLogResponse,
&k.RotationEnabled,
&k.PolicyId,
&k.IsKeyNotHashed,
); err != nil {
return nil, err
}
pk := &k
pk.SettingId = settingId.String
if len(data) != 0 {
pathConfigs := []key.PathConfig{}
if err := json.Unmarshal(data, &pathConfigs); err != nil {
return nil, err
}
pk.AllowedPaths = pathConfigs
}
return pk, nil
}
func (s *Store) DeleteKey(id string) error {
ctxTimeout, cancel := context.WithTimeout(context.Background(), s.wt)
defer cancel()
_, err := s.db.ExecContext(ctxTimeout, "DELETE FROM keys WHERE key_id = $1", id)
return err
}
func sliceToSqlStringArray(slice []string) string {
return "{" + strings.Join(slice, ",") + "}"
}