4
4
"context"
5
5
"database/sql"
6
6
"encoding/base64"
7
+ "strings"
7
8
"testing"
8
9
9
10
"github.com/google/uuid"
@@ -43,12 +44,26 @@ func TestServerDBCrypt(t *testing.T) {
43
44
44
45
// Populate the database with some unencrypted data.
45
46
users := genData (t , db , 10 )
47
+ dumpUsers (t , sqlDB , "NOT ENCRYPTED" )
46
48
47
- // Setup an initial cipher
49
+ // Setup an initial cipher A
48
50
keyA := mustString (t , 32 )
49
51
cipherA , err := dbcrypt .NewCiphers ([]byte (keyA ))
50
52
require .NoError (t , err )
51
53
54
+ // Create an encrypted database
55
+ cryptdb , err := dbcrypt .New (ctx , db , cipherA ... )
56
+ require .NoError (t , err )
57
+
58
+ // Populate the database with some encrypted data using cipher A.
59
+ users = append (users , genData (t , cryptdb , 10 )... )
60
+ dumpUsers (t , sqlDB , "PARTIALLY ENCRYPTED A" )
61
+
62
+ // Validate that newly created users were encrypted with cipher A
63
+ for _ , usr := range users [10 :] {
64
+ requireEncryptedWithCipher (ctx , t , db , cipherA [0 ], usr .ID )
65
+ }
66
+
52
67
// Encrypt all the data with the initial cipher.
53
68
inv , _ := newCLI (t , "server" , "dbcrypt" , "rotate" ,
54
69
"--postgres-url" , connectionURL ,
@@ -60,18 +75,12 @@ func TestServerDBCrypt(t *testing.T) {
60
75
err = inv .Run ()
61
76
require .NoError (t , err )
62
77
78
+ dumpUsers (t , sqlDB , "ENCRYPTED A" )
63
79
// Validate that all existing data has been encrypted with cipher A.
64
80
for _ , usr := range users {
65
81
requireEncryptedWithCipher (ctx , t , db , cipherA [0 ], usr .ID )
66
82
}
67
83
68
- // Create an encrypted database
69
- cryptdb , err := dbcrypt .New (ctx , db , cipherA ... )
70
- require .NoError (t , err )
71
-
72
- // Populate the database with some encrypted data using cipher A.
73
- users = append (users , genData (t , cryptdb , 10 )... )
74
-
75
84
// Re-encrypt all existing data with a new cipher.
76
85
keyB := mustString (t , 32 )
77
86
cipherBA , err := dbcrypt .NewCiphers ([]byte (keyB ), []byte (keyA ))
@@ -89,6 +98,7 @@ func TestServerDBCrypt(t *testing.T) {
89
98
require .NoError (t , err )
90
99
91
100
// Validate that all data has been re-encrypted with cipher B.
101
+ dumpUsers (t , sqlDB , "ENCRYPTED B" )
92
102
for _ , usr := range users {
93
103
requireEncryptedWithCipher (ctx , t , db , cipherBA [0 ], usr .ID )
94
104
}
@@ -135,6 +145,7 @@ func TestServerDBCrypt(t *testing.T) {
135
145
}
136
146
137
147
// Validate that all data has been decrypted.
148
+ dumpUsers (t , sqlDB , "DECRYPTED" )
138
149
for _ , usr := range users {
139
150
requireEncryptedWithCipher (ctx , t , db , & nullCipher {}, usr .ID )
140
151
}
@@ -156,6 +167,7 @@ func TestServerDBCrypt(t *testing.T) {
156
167
require .NoError (t , err )
157
168
158
169
// Validate that all data has been re-encrypted with cipher C.
170
+ dumpUsers (t , sqlDB , "ENCRYPTED C" )
159
171
for _ , usr := range users {
160
172
requireEncryptedWithCipher (ctx , t , db , cipherC [0 ], usr .ID )
161
173
}
@@ -172,6 +184,7 @@ func TestServerDBCrypt(t *testing.T) {
172
184
require .NoError (t , err )
173
185
174
186
// Assert that no user links remain.
187
+ dumpUsers (t , sqlDB , "DELETED" )
175
188
for _ , usr := range users {
176
189
userLinks , err := db .GetUserLinksByUserID (ctx , usr .ID )
177
190
require .NoError (t , err , "failed to get user links for user %s" , usr .ID )
@@ -215,6 +228,36 @@ func genData(t *testing.T, db database.Store, n int) []database.User {
215
228
return users
216
229
}
217
230
231
+ func dumpUsers (t * testing.T , db * sql.DB , header string ) {
232
+ t .Logf ("%s %s %s" , strings .Repeat ("=" , 20 ), header , strings .Repeat ("=" , 20 ))
233
+ rows , err := db .QueryContext (context .Background (), `select u.id, u.status, u.deleted, ul.oauth_access_token_key_id as uloatkid, ul.oauth_refresh_token_key_id as ulortkid, gal.oauth_access_token_key_id as galoatkid, gal.oauth_refresh_token_key_id as galortkid from users u left outer join user_links ul on u.id = ul.user_id left outer join git_auth_links gal on u.id = gal.user_id;` )
234
+ require .NoError (t , err )
235
+ defer rows .Close ()
236
+ for rows .Next () {
237
+ var (
238
+ id string
239
+ status string
240
+ deleted bool
241
+ UlOatKid sql.NullString
242
+ UlOrtKid sql.NullString
243
+ GalOatKid sql.NullString
244
+ GalOrtKid sql.NullString
245
+ )
246
+ require .NoError (t , rows .Scan (
247
+ & id ,
248
+ & status ,
249
+ & deleted ,
250
+ & UlOatKid ,
251
+ & UlOrtKid ,
252
+ & GalOatKid ,
253
+ & GalOrtKid ,
254
+ ))
255
+ t .Logf ("user: id:%s status:%-9s deleted:%-5t ul_kids{at:%-7s rt:%-7s} gal_kids{at:%-7s rt:%-7s}" ,
256
+ id , status , deleted , UlOatKid .String , UlOrtKid .String , GalOatKid .String , GalOrtKid .String ,
257
+ )
258
+ }
259
+ }
260
+
218
261
func mustString (t * testing.T , n int ) string {
219
262
t .Helper ()
220
263
s , err := cryptorand .String (n )
0 commit comments