|
16 | 16 | defaultRootKeyIDContext = macaroons.ContextWithRootKeyID(
|
17 | 17 | context.Background(), macaroons.DefaultRootKeyID,
|
18 | 18 | )
|
| 19 | + |
| 20 | + nonDefaultRootKeyIDContext = macaroons.ContextWithRootKeyID( |
| 21 | + context.Background(), []byte{1}, |
| 22 | + ) |
19 | 23 | )
|
20 | 24 |
|
21 | 25 | // newTestStore creates a new bolt DB in a temporary directory and then
|
@@ -131,32 +135,42 @@ func TestStore(t *testing.T) {
|
131 | 135 | require.Equal(t, rootID, id)
|
132 | 136 | }
|
133 | 137 |
|
134 |
| -// TestStoreGenerateNewRootKey tests that a root key can be replaced with a new |
135 |
| -// one in the store without changing the password. |
| 138 | +// TestStoreGenerateNewRootKey tests that root keys can be replaced with new |
| 139 | +// ones in the store without changing the password. |
136 | 140 | func TestStoreGenerateNewRootKey(t *testing.T) {
|
137 | 141 | _, store := newTestStore(t)
|
138 | 142 |
|
139 | 143 | // The store must be unlocked to replace the root key.
|
140 | 144 | err := store.GenerateNewRootKey()
|
141 | 145 | require.Equal(t, macaroons.ErrStoreLocked, err)
|
142 | 146 |
|
143 |
| - // Unlock the store and read the current key. |
| 147 | + // Unlock the store. |
144 | 148 | pw := []byte("weks")
|
145 | 149 | err = store.CreateUnlock(&pw)
|
146 | 150 | require.NoError(t, err)
|
147 |
| - oldRootKey, _, err := store.RootKey(defaultRootKeyIDContext) |
| 151 | + |
| 152 | + // Read the default root key. |
| 153 | + oldRootKey1, _, err := store.RootKey(defaultRootKeyIDContext) |
148 | 154 | require.NoError(t, err)
|
149 | 155 |
|
150 |
| - // Replace the root key with a new random key. |
| 156 | + // Read the non-default root-key. |
| 157 | + oldRootKey2, _, err := store.RootKey(nonDefaultRootKeyIDContext) |
| 158 | + require.NoError(t, err) |
| 159 | + |
| 160 | + // Replace the root keys with new random keys. |
151 | 161 | err = store.GenerateNewRootKey()
|
152 | 162 | require.NoError(t, err)
|
153 | 163 |
|
154 |
| - // Finally, read the root key from the DB and compare it to the one |
| 164 | + // Finally, read both root keys from the DB and compare them to the ones |
155 | 165 | // we got returned earlier. This makes sure that the encryption/
|
156 | 166 | // decryption of the key in the DB worked as expected too.
|
157 |
| - newRootKey, _, err := store.RootKey(defaultRootKeyIDContext) |
| 167 | + newRootKey1, _, err := store.RootKey(defaultRootKeyIDContext) |
158 | 168 | require.NoError(t, err)
|
159 |
| - require.NotEqual(t, oldRootKey, newRootKey) |
| 169 | + require.NotEqual(t, oldRootKey1, newRootKey1) |
| 170 | + |
| 171 | + newRootKey2, _, err := store.RootKey(nonDefaultRootKeyIDContext) |
| 172 | + require.NoError(t, err) |
| 173 | + require.NotEqual(t, oldRootKey2, newRootKey2) |
160 | 174 | }
|
161 | 175 |
|
162 | 176 | // TestStoreSetRootKey tests that a root key can be set to a specified value.
|
@@ -195,20 +209,25 @@ func TestStoreSetRootKey(t *testing.T) {
|
195 | 209 | }
|
196 | 210 |
|
197 | 211 | // TestStoreChangePassword tests that the password for the store can be changed
|
198 |
| -// without changing the root key. |
| 212 | +// without changing the root keys. |
199 | 213 | func TestStoreChangePassword(t *testing.T) {
|
200 | 214 | tempDir, store := newTestStore(t)
|
201 | 215 |
|
202 |
| - // The store must be unlocked to replace the root key. |
| 216 | + // The store must be unlocked to replace the root keys. |
203 | 217 | err := store.ChangePassword(nil, nil)
|
204 | 218 | require.Equal(t, macaroons.ErrStoreLocked, err)
|
205 | 219 |
|
206 |
| - // Unlock the DB and read the current root key. This will need to stay |
207 |
| - // the same after changing the password for the test to succeed. |
| 220 | + // Unlock the DB and read the current default root key and one other |
| 221 | + // non-default root key. Both of these should stay the same after |
| 222 | + // changing the password for the test to succeed. |
208 | 223 | pw := []byte("weks")
|
209 | 224 | err = store.CreateUnlock(&pw)
|
210 | 225 | require.NoError(t, err)
|
211 |
| - rootKey, _, err := store.RootKey(defaultRootKeyIDContext) |
| 226 | + |
| 227 | + rootKey1, _, err := store.RootKey(defaultRootKeyIDContext) |
| 228 | + require.NoError(t, err) |
| 229 | + |
| 230 | + rootKey2, _, err := store.RootKey(nonDefaultRootKeyIDContext) |
212 | 231 | require.NoError(t, err)
|
213 | 232 |
|
214 | 233 | // Both passwords must be set.
|
@@ -242,9 +261,13 @@ func TestStoreChangePassword(t *testing.T) {
|
242 | 261 | err = store.CreateUnlock(&newPw)
|
243 | 262 | require.NoError(t, err)
|
244 | 263 |
|
245 |
| - // Finally read the root key from the DB using the new password and |
246 |
| - // make sure the root key stayed the same. |
247 |
| - rootKeyDb, _, err := store.RootKey(defaultRootKeyIDContext) |
| 264 | + // Finally, read the root keys from the DB using the new password and |
| 265 | + // make sure that both root keys stayed the same. |
| 266 | + rootKeyDB1, _, err := store.RootKey(defaultRootKeyIDContext) |
| 267 | + require.NoError(t, err) |
| 268 | + require.Equal(t, rootKey1, rootKeyDB1) |
| 269 | + |
| 270 | + rootKeyDB2, _, err := store.RootKey(nonDefaultRootKeyIDContext) |
248 | 271 | require.NoError(t, err)
|
249 |
| - require.Equal(t, rootKey, rootKeyDb) |
| 272 | + require.Equal(t, rootKey2, rootKeyDB2) |
250 | 273 | }
|
0 commit comments