This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
/
key.go
76 lines (61 loc) · 3.04 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
package storage
import (
"context"
"fmt"
"github.com/abetterinternet/prio-server/key-rotator/key"
)
// Key represents a store of Prio keys, with functionality to read & write keys
// from the store.
type Key interface {
// PutBatchSigningKey writes the provided key as the batch signing key for
// the given (locality, ingestor) tuple, or returns an error on failure.
PutBatchSigningKey(ctx context.Context, locality, ingestor string, key key.Key) error
// PutPacketEncryptionKey writes the provided key as the packet encryption
// key for the given locality, or returns an error on failure.
PutPacketEncryptionKey(ctx context.Context, locality string, key key.Key) error
// GetBatchSigningKey gets the batch signing key for the given (locality,
// ingestor) pair, or returns an error on failure.
GetBatchSigningKey(ctx context.Context, locality, ingestor string) (key.Key, error)
// GetPacketEncryptionKey gets the packet encryption key for the given
// locality, or returns an error on failure.
GetPacketEncryptionKey(ctx context.Context, locality string) (key.Key, error)
}
// NewBackupKey returns a Key implementation that mirrors writes to a "backup"
// storage.Key. All reads are performed via the "main" storage.Key (the
// "backup" storage.Key will never be used to fulfill a read). To avoid the
// possiblity of writing a key to main storage without backing it up, writes
// are performed by writing to the "backup" storage first, followed by writing
// to the "main" storage.
func NewBackupKey(main, backup Key) Key { return backupKey{main, backup} }
type backupKey struct{ main, backup Key }
var _ Key = backupKey{} // verify backupKey satisfies Key
func (k backupKey) PutBatchSigningKey(ctx context.Context, locality, ingestor string, key key.Key) error {
if err := k.backup.PutBatchSigningKey(ctx, locality, ingestor, key); err != nil {
return fmt.Errorf("couldn't write to backup storage: %w", err)
}
if err := k.main.PutBatchSigningKey(ctx, locality, ingestor, key); err != nil {
return fmt.Errorf("couldn't write to main storage: %w", err)
}
return nil
}
func (k backupKey) PutPacketEncryptionKey(ctx context.Context, locality string, key key.Key) error {
if err := k.backup.PutPacketEncryptionKey(ctx, locality, key); err != nil {
return fmt.Errorf("couldn't write to backup storage: %w", err)
}
if err := k.main.PutPacketEncryptionKey(ctx, locality, key); err != nil {
return fmt.Errorf("couldn't write to main storage: %w", err)
}
return nil
}
func (k backupKey) GetBatchSigningKey(ctx context.Context, locality, ingestor string) (key.Key, error) {
return k.main.GetBatchSigningKey(ctx, locality, ingestor)
}
func (k backupKey) GetPacketEncryptionKey(ctx context.Context, locality string) (key.Key, error) {
return k.main.GetPacketEncryptionKey(ctx, locality)
}
func batchSigningKeyName(env, locality, ingestor string) string {
return fmt.Sprintf("%s-%s-%s-batch-signing-key", env, locality, ingestor)
}
func packetEncryptionKeyName(env, locality string) string {
return fmt.Sprintf("%s-%s-ingestion-packet-decryption-key", env, locality)
}