-
Notifications
You must be signed in to change notification settings - Fork 153
/
piped.go
123 lines (103 loc) · 3.03 KB
/
piped.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
// Copyright 2023 The PipeCD Authors.
//
// Licensed 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.
package model
import (
"errors"
"fmt"
"strings"
"time"
"golang.org/x/crypto/bcrypt"
)
const (
pipedMaxKeyNum = 2
pipedKeyLength = 50
redactedMessage = "redacted"
)
type SecretManagementType string
const (
SecretManagementTypeNone SecretManagementType = "NONE"
SecretManagementTypeKeyPair SecretManagementType = "KEY_PAIR"
SecretManagementTypeGCPKMS SecretManagementType = "GCP_KMS"
SecretManagementTypeAWSKMS SecretManagementType = "AWS_KMS"
)
func (t SecretManagementType) String() string {
return string(t)
}
// GeneratePipedKey generates a new key for piped.
// This returns raw key value for used by piped and
// a hash value of the key for storing in datastore.
func GeneratePipedKey() (key, hash string, err error) {
var encoded []byte
key = GenerateRandomString(pipedKeyLength)
encoded, err = bcrypt.GenerateFromPassword([]byte(key), bcrypt.DefaultCost)
if err != nil {
return
}
hash = string(encoded)
return
}
// CheckKey checks if the give key is one of the stored keys.
func (p *Piped) CheckKey(key string) (err error) {
if len(p.Keys) == 0 {
return errors.New("piped does not contain any key")
}
for _, k := range p.Keys {
err = bcrypt.CompareHashAndPassword([]byte(k.Hash), []byte(key))
if err == nil {
return nil
}
}
return
}
// AddKey adds a new key to the list.
// A piped can hold a maximum of "pipedMaxKeyNum" keys.
func (p *Piped) AddKey(hash, creator string, createdAt time.Time) error {
if len(p.Keys) == pipedMaxKeyNum {
return fmt.Errorf("number of keys for each piped must be less than or equal to %d, you may need to delete the old keys before adding a new one", pipedMaxKeyNum)
}
k := &PipedKey{
Hash: hash,
Creator: creator,
CreatedAt: createdAt.Unix(),
}
keys := make([]*PipedKey, 0, len(p.Keys)+1)
keys = append(keys, k)
keys = append(keys, p.Keys...)
p.Keys = keys
return nil
}
// DeleteOldPipedKeys removes all old keys to keep only the latest one.
func (p *Piped) DeleteOldPipedKeys() {
if len(p.Keys) < 2 {
return
}
var latest *PipedKey
for i := range p.Keys {
if latest == nil || latest.CreatedAt < p.Keys[i].CreatedAt {
latest = p.Keys[i]
}
}
p.Keys = []*PipedKey{latest}
}
func (p *Piped) RedactSensitiveData() {
for i := range p.Keys {
p.Keys[i].Hash = redactedMessage
}
}
func (p *Piped) SetUpdatedAt(t int64) {
p.UpdatedAt = t
}
func MakePipedURL(baseURL, pipedID string) string {
return fmt.Sprintf("%s/settings/piped", strings.TrimSuffix(baseURL, "/"))
}