forked from godaddy/asherah
-
Notifications
You must be signed in to change notification settings - Fork 0
/
partition.go
75 lines (63 loc) · 2.12 KB
/
partition.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
package appencryption
import (
"fmt"
"strings"
)
func newPartition(partition, service, product string) defaultPartition {
return defaultPartition{
id: partition,
service: service,
product: product,
}
}
// partition returns the SystemKey name and IntermediateKey name for the
// current partition id.
type partition interface {
SystemKeyID() string
IntermediateKeyID() string
IsValidIntermediateKeyID(id string) bool
}
// defaultPartition is the default implementation for partition naming.
type defaultPartition struct {
id string
service string
product string
}
// SystemKeyID returns the system key name for the product/service.
func (p defaultPartition) SystemKeyID() string {
return fmt.Sprintf("_SK_%s_%s", p.service, p.product)
}
// IntermediateKeyID returns the intermediate key name for the product/service.
func (p defaultPartition) IntermediateKeyID() string {
return fmt.Sprintf("_IK_%s_%s_%s", p.id, p.service, p.product)
}
// IsValidIntermediateKeyID ensures the given ID is a valid intermediate key ID for this partition
func (p defaultPartition) IsValidIntermediateKeyID(id string) bool {
return id == p.IntermediateKeyID()
}
func newSuffixedPartition(partition, service, product, suffix string) suffixedPartition {
return suffixedPartition{
defaultPartition: defaultPartition{
id: partition,
service: service,
product: product,
},
suffix: suffix,
}
}
type suffixedPartition struct {
defaultPartition
suffix string
}
// SystemKeyID returns the system key name for the product/service.
func (p suffixedPartition) SystemKeyID() string {
return fmt.Sprintf("_SK_%s_%s_%s", p.service, p.product, p.suffix)
}
// IntermediateKeyID returns the intermediate key name for the product/service.
func (p suffixedPartition) IntermediateKeyID() string {
return fmt.Sprintf("_IK_%s_%s_%s_%s", p.id, p.service, p.product, p.suffix)
}
// IsValidIntermediateKeyID ensures the given ID is a valid intermediate key ID for this partition
func (p suffixedPartition) IsValidIntermediateKeyID(id string) bool {
return id == p.IntermediateKeyID() || strings.Index(id, p.defaultPartition.IntermediateKeyID()) == 0
}