forked from cloudfoundry/bbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryption_db.go
216 lines (180 loc) · 5.52 KB
/
encryption_db.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
package sqldb
import (
"context"
"fmt"
"strings"
"code.cloudfoundry.org/bbs/db/sqldb/helpers"
"code.cloudfoundry.org/bbs/format"
"code.cloudfoundry.org/lager"
)
const EncryptionKeyID = "encryption_key_label"
func (db *SQLDB) SetEncryptionKeyLabel(ctx context.Context, logger lager.Logger, label string) error {
logger = logger.Session("db-set-encryption-key-label", lager.Data{"label": label})
logger.Debug("starting")
defer logger.Debug("complete")
return db.setConfigurationValue(ctx, logger, EncryptionKeyID, label)
}
func (db *SQLDB) EncryptionKeyLabel(ctx context.Context, logger lager.Logger) (string, error) {
logger = logger.Session("db-encryption-key-label")
logger.Debug("starting")
defer logger.Debug("complete")
return db.getConfigurationValue(ctx, logger, EncryptionKeyID)
}
func (db *SQLDB) PerformEncryption(ctx context.Context, logger lager.Logger) error {
errCh := make(chan error)
funcs := []func(){
func() {
errCh <- db.reEncrypt(ctx, logger, encryptable{
TableName: tasksTable,
PrimaryKeyNames: []string{"guid"},
Columns: []string{"task_definition"},
EncryptIfEmpty: true,
PrimaryKeyFunc: func() primaryKey { return &taskPrimaryKey{} },
})
},
func() {
errCh <- db.reEncrypt(ctx, logger, encryptable{
TableName: desiredLRPsTable,
PrimaryKeyNames: []string{"process_guid"},
Columns: []string{"run_info", "volume_placement", "routes"},
EncryptIfEmpty: true,
PrimaryKeyFunc: func() primaryKey { return &desiredLRPPrimaryKey{} },
})
},
func() {
errCh <- db.reEncrypt(ctx, logger, encryptable{
TableName: actualLRPsTable,
PrimaryKeyNames: []string{"process_guid", "instance_index", "presence"},
Columns: []string{"net_info", "internal_routes"},
EncryptIfEmpty: false,
PrimaryKeyFunc: func() primaryKey { return &actualLRPPrimaryKey{} },
})
},
}
for _, f := range funcs {
go f()
}
for range funcs {
err := <-errCh
if err != nil {
return err
}
}
return nil
}
func (db *SQLDB) reEncrypt(ctx context.Context, logger lager.Logger, toEncrypt encryptable) error {
logger = logger.WithData(
lager.Data{"table_name": toEncrypt.TableName, "primary_key": toEncrypt.PrimaryKeyNames, "blob_columns": toEncrypt.Columns},
)
rows, err := db.db.QueryContext(ctx, fmt.Sprintf("SELECT %s FROM %s", strings.Join(toEncrypt.PrimaryKeyNames, ", "), toEncrypt.TableName))
if err != nil {
return err
}
defer rows.Close()
pks := []primaryKey{}
for rows.Next() {
pk := toEncrypt.PrimaryKeyFunc()
err := pk.Scan(rows)
if err != nil {
logger.Error("failed-to-scan-primary-key", err)
continue
}
pks = append(pks, pk)
}
whereClauses := []string{}
for _, name := range toEncrypt.PrimaryKeyNames {
whereClauses = append(whereClauses, name+" = ?")
}
where := strings.Join(whereClauses, " AND ")
for _, pk := range pks {
err = db.transact(ctx, logger, func(logger lager.Logger, tx helpers.Tx) error {
blobs := make([]interface{}, len(toEncrypt.Columns))
row := db.one(ctx, logger, tx, toEncrypt.TableName, toEncrypt.Columns, helpers.LockRow, where, pk.WhereBindings()...)
for i := range toEncrypt.Columns {
var blob []byte
blobs[i] = &blob
}
err := row.Scan(blobs...)
if err != nil {
logger.Error("failed-to-scan-blob", err)
return nil
}
updatedColumnValues := map[string]interface{}{}
for columnIdx := range blobs {
// This type assertion should not fail because we set the value to be a pointer to a byte array above
blobPtr := blobs[columnIdx].(*[]byte)
blob := *blobPtr
// don't encrypt column if it doesn't contain any data, see #132626553 for more info
if !toEncrypt.EncryptIfEmpty && len(blob) == 0 {
return nil
}
encoder := format.NewEncoder(db.cryptor)
payload, err := encoder.Decode(blob)
if err != nil {
logger.Error("failed-to-decode-blob", err)
return nil
}
encryptedPayload, err := encoder.Encode(payload)
if err != nil {
logger.Error("failed-to-encode-blob", err)
return err
}
columnName := toEncrypt.Columns[columnIdx]
updatedColumnValues[columnName] = encryptedPayload
}
_, err = db.update(ctx, logger, tx, toEncrypt.TableName,
updatedColumnValues,
where, pk.WhereBindings()...,
)
if err != nil {
logger.Error("failed-to-update-blob", err)
return err
}
return nil
})
if err != nil {
return err
}
}
return nil
}
type encryptable struct {
TableName string
PrimaryKeyNames []string
Columns []string
EncryptIfEmpty bool
PrimaryKeyFunc func() primaryKey
}
type primaryKey interface {
Scan(row helpers.RowScanner) error
WhereBindings() []interface{}
}
type actualLRPPrimaryKey struct {
ProcessGuid string
InstanceIndex int32
Presence string
}
func (pk *actualLRPPrimaryKey) Scan(row helpers.RowScanner) error {
return row.Scan(&pk.ProcessGuid, &pk.InstanceIndex, &pk.Presence)
}
func (pk *actualLRPPrimaryKey) WhereBindings() []interface{} {
return []interface{}{pk.ProcessGuid, pk.InstanceIndex, pk.Presence}
}
type desiredLRPPrimaryKey struct {
ProcessGuid string
}
func (pk *desiredLRPPrimaryKey) Scan(row helpers.RowScanner) error {
return row.Scan(&pk.ProcessGuid)
}
func (pk *desiredLRPPrimaryKey) WhereBindings() []interface{} {
return []interface{}{pk.ProcessGuid}
}
type taskPrimaryKey struct {
Guid string
}
func (pk *taskPrimaryKey) Scan(row helpers.RowScanner) error {
return row.Scan(&pk.Guid)
}
func (pk *taskPrimaryKey) WhereBindings() []interface{} {
return []interface{}{pk.Guid}
}