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
|
package database
import (
"context"
"fmt"
"time"
)
func AddKey(ctx context.Context, exec Executor, keyName string, key []byte) (int, error) {
row := exec.QueryRowContext(ctx, `
INSERT INTO keys (kms_key_name, encrypted_key)
VALUES ($1, $2)
RETURNING id;`, keyName, key)
var keyId int
err := row.Scan(&keyId)
if err != nil {
return 0, fmt.Errorf("unable to scan key id: %w", err)
}
return keyId, nil
}
func GetKey(ctx context.Context, exec Executor, keyId int) (*Key, error) {
row := exec.QueryRowContext(ctx, `
SELECT id, kms_key_name, encrypted_key, created_at
FROM keys
WHERE id = $1;`, keyId)
key := &Key{}
err := row.Scan(&key.Id, &key.Name, &key.Key, &key.Created)
if err != nil {
return nil, fmt.Errorf("unable to scan key: %w", err)
}
return key, nil
}
type Key struct {
Id int
Name string
Key []byte
Created time.Time
}
|