@@ -20,6 +20,9 @@ import (
20
20
"github.com/coder/coder/v2/pty/ptytest"
21
21
)
22
22
23
+ // TestServerDBCrypt tests end-to-end encryption, decryption, and deletion
24
+ // of encrypted user data.
25
+ //
23
26
// nolint: paralleltest // use of t.Setenv
24
27
func TestServerDBCrypt (t * testing.T ) {
25
28
if ! dbtestutil .WillUsePostgres () {
@@ -49,6 +52,7 @@ func TestServerDBCrypt(t *testing.T) {
49
52
})
50
53
51
54
// Populate the database with some unencrypted data.
55
+ t .Logf ("Generating unencrypted data" )
52
56
users := genData (t , db )
53
57
54
58
// Setup an initial cipher A
@@ -61,6 +65,7 @@ func TestServerDBCrypt(t *testing.T) {
61
65
require .NoError (t , err )
62
66
63
67
// Populate the database with some encrypted data using cipher A.
68
+ t .Logf ("Generating data encrypted with cipher A" )
64
69
newUsers := genData (t , cryptdb )
65
70
66
71
// Validate that newly created users were encrypted with cipher A
@@ -70,6 +75,7 @@ func TestServerDBCrypt(t *testing.T) {
70
75
users = append (users , newUsers ... )
71
76
72
77
// Encrypt all the data with the initial cipher.
78
+ t .Logf ("Encrypting all data with cipher A" )
73
79
inv , _ := newCLI (t , "server" , "dbcrypt" , "rotate" ,
74
80
"--postgres-url" , connectionURL ,
75
81
"--new-key" , base64 .StdEncoding .EncodeToString ([]byte (keyA )),
@@ -90,9 +96,7 @@ func TestServerDBCrypt(t *testing.T) {
90
96
cipherBA , err := dbcrypt .NewCiphers ([]byte (keyB ), []byte (keyA ))
91
97
require .NoError (t , err )
92
98
93
- // Generate some more encrypted data using the new cipher
94
- users = append (users , genData (t , db )... )
95
-
99
+ t .Logf ("Enrypting all data with cipher B" )
96
100
inv , _ = newCLI (t , "server" , "dbcrypt" , "rotate" ,
97
101
"--postgres-url" , connectionURL ,
98
102
"--new-key" , base64 .StdEncoding .EncodeToString ([]byte (keyB )),
@@ -110,6 +114,7 @@ func TestServerDBCrypt(t *testing.T) {
110
114
}
111
115
112
116
// Assert that we can revoke the old key.
117
+ t .Logf ("Revoking cipher A" )
113
118
err = db .RevokeDBCryptKey (ctx , cipherA [0 ].HexDigest ())
114
119
require .NoError (t , err , "failed to revoke old key" )
115
120
@@ -125,13 +130,15 @@ func TestServerDBCrypt(t *testing.T) {
125
130
require .Empty (t , oldKey .ActiveKeyDigest .String , "expected the old key to not be active" )
126
131
127
132
// Revoking the new key should fail.
133
+ t .Logf ("Attempting to revoke cipher B should fail as it is still in use" )
128
134
err = db .RevokeDBCryptKey (ctx , cipherBA [0 ].HexDigest ())
129
135
require .Error (t , err , "expected to fail to revoke the new key" )
130
136
var pgErr * pq.Error
131
137
require .True (t , xerrors .As (err , & pgErr ), "expected a pg error" )
132
138
require .EqualValues (t , "23503" , pgErr .Code , "expected a foreign key constraint violation error" )
133
139
134
140
// Decrypt the data using only cipher B. This should result in the key being revoked.
141
+ t .Logf ("Decrypting with cipher B" )
135
142
inv , _ = newCLI (t , "server" , "dbcrypt" , "decrypt" ,
136
143
"--postgres-url" , connectionURL ,
137
144
"--keys" , base64 .StdEncoding .EncodeToString ([]byte (keyB )),
@@ -160,6 +167,7 @@ func TestServerDBCrypt(t *testing.T) {
160
167
cipherC , err := dbcrypt .NewCiphers ([]byte (keyC ))
161
168
require .NoError (t , err )
162
169
170
+ t .Logf ("Re-encrypting with cipher C" )
163
171
inv , _ = newCLI (t , "server" , "dbcrypt" , "rotate" ,
164
172
"--postgres-url" , connectionURL ,
165
173
"--new-key" , base64 .StdEncoding .EncodeToString ([]byte (keyC )),
@@ -177,6 +185,7 @@ func TestServerDBCrypt(t *testing.T) {
177
185
}
178
186
179
187
// Now delete all the encrypted data.
188
+ t .Logf ("Deleting all encrypted data" )
180
189
inv , _ = newCLI (t , "server" , "dbcrypt" , "delete" ,
181
190
"--postgres-url" , connectionURL ,
182
191
"--external-token-encryption-keys" , base64 .StdEncoding .EncodeToString ([]byte (keyC )),
0 commit comments