|
| 1 | +package secrethub |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/secrethub/secrethub-go/internals/api" |
| 8 | + "github.com/secrethub/secrethub-go/internals/api/uuid" |
| 9 | + "github.com/secrethub/secrethub-go/internals/assert" |
| 10 | + "github.com/secrethub/secrethub-go/internals/crypto" |
| 11 | + "github.com/secrethub/secrethub-go/pkg/secrethub/internals/http" |
| 12 | +) |
| 13 | + |
| 14 | +func TestReencypter_reencrypt(t *testing.T) { |
| 15 | + fromPrivateKey, err := crypto.GenerateRSAPrivateKey(2048) |
| 16 | + assert.OK(t, err) |
| 17 | + |
| 18 | + fromPublicKey := fromPrivateKey.Public() |
| 19 | + fromEncodedPublicKey, err := fromPublicKey.Encode() |
| 20 | + assert.OK(t, err) |
| 21 | + |
| 22 | + forPrivateKey, err := crypto.GenerateRSAPrivateKey(2048) |
| 23 | + assert.OK(t, err) |
| 24 | + |
| 25 | + forPublicKey := forPrivateKey.Public() |
| 26 | + forEncodedPublicKey, err := forPublicKey.Encode() |
| 27 | + assert.OK(t, err) |
| 28 | + |
| 29 | + firstAccount := api.Account{ |
| 30 | + AccountID: uuid.New(), |
| 31 | + Name: "first-user", |
| 32 | + PublicKey: fromEncodedPublicKey, |
| 33 | + AccountType: "", |
| 34 | + CreatedAt: time.Time{}, |
| 35 | + } |
| 36 | + |
| 37 | + secondAccount := api.Account{ |
| 38 | + AccountID: uuid.New(), |
| 39 | + Name: "second-user", |
| 40 | + PublicKey: forEncodedPublicKey, |
| 41 | + AccountType: "", |
| 42 | + CreatedAt: time.Time{}, |
| 43 | + } |
| 44 | + |
| 45 | + fakeClient := Client{ |
| 46 | + httpClient: &http.Client{}, |
| 47 | + account: &secondAccount, |
| 48 | + accountKey: &forPrivateKey, |
| 49 | + } |
| 50 | + |
| 51 | + cases := map[string]struct { |
| 52 | + dirs []*api.Dir |
| 53 | + secrets []*api.Secret |
| 54 | + }{ |
| 55 | + "no directories": { |
| 56 | + dirs: nil, |
| 57 | + secrets: nil, |
| 58 | + }, |
| 59 | + "one directory": { |
| 60 | + dirs: []*api.Dir{ |
| 61 | + { |
| 62 | + DirID: uuid.New(), |
| 63 | + Name: "first-dir", |
| 64 | + }, |
| 65 | + }, |
| 66 | + secrets: nil, |
| 67 | + }, |
| 68 | + "multiple directories": { |
| 69 | + dirs: []*api.Dir{ |
| 70 | + { |
| 71 | + DirID: uuid.New(), |
| 72 | + Name: "first-dir", |
| 73 | + }, |
| 74 | + { |
| 75 | + DirID: uuid.New(), |
| 76 | + Name: "second-dir", |
| 77 | + }, |
| 78 | + { |
| 79 | + DirID: uuid.New(), |
| 80 | + Name: "third-dir", |
| 81 | + }, |
| 82 | + { |
| 83 | + DirID: uuid.New(), |
| 84 | + Name: "fourth-dir", |
| 85 | + }, |
| 86 | + { |
| 87 | + DirID: uuid.New(), |
| 88 | + Name: "fifth-dir", |
| 89 | + }, |
| 90 | + }, |
| 91 | + secrets: nil, |
| 92 | + }, |
| 93 | + } |
| 94 | + for name, tc := range cases { |
| 95 | + t.Run(name, func(t *testing.T) { |
| 96 | + fakeReencrypter := reencrypter{ |
| 97 | + dirs: make(map[uuid.UUID]api.EncryptedNameForNodeRequest), |
| 98 | + secrets: make(map[uuid.UUID]api.SecretAccessRequest), |
| 99 | + encryptFor: &secondAccount, |
| 100 | + client: &fakeClient, |
| 101 | + } |
| 102 | + |
| 103 | + encryptedTree := createEncryptedTree(t, tc.dirs, tc.secrets, fakeReencrypter, firstAccount) |
| 104 | + err = fakeReencrypter.reencrypt(&encryptedTree, &fromPrivateKey) |
| 105 | + assert.OK(t, err) |
| 106 | + |
| 107 | + count := 0 |
| 108 | + for _, dir := range fakeReencrypter.dirs { |
| 109 | + assert.Equal(t, dir.AccountID, secondAccount.AccountID) |
| 110 | + decryptedDir, err := encryptedTree.Directories[tc.dirs[count].DirID].Decrypt(&fromPrivateKey) |
| 111 | + assert.OK(t, err) |
| 112 | + assert.Equal(t, tc.dirs[count].Name, decryptedDir.Name) |
| 113 | + count++ |
| 114 | + } |
| 115 | + |
| 116 | + //TODO: In order to test for secrets encryption and decryption, a refactoring of the Client is in order, as to be able to mock the HTTP Go client. |
| 117 | + count = 0 |
| 118 | + for _, secret := range fakeReencrypter.secrets { |
| 119 | + assert.Equal(t, secret.Name.AccountID, secondAccount.AccountID) |
| 120 | + decryptedSecret, err := encryptedTree.Secrets[count].Decrypt(&fromPrivateKey) |
| 121 | + assert.OK(t, err) |
| 122 | + assert.Equal(t, tc.secrets[count].Name, decryptedSecret.Name) |
| 123 | + count++ |
| 124 | + } |
| 125 | + }) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func createEncryptedTree(t *testing.T, dirs []*api.Dir, secrets []*api.Secret, reencrypter reencrypter, account api.Account) api.EncryptedTree { |
| 130 | + encryptedTree := api.EncryptedTree{ |
| 131 | + Directories: make(map[uuid.UUID]*api.EncryptedDir), |
| 132 | + Secrets: make([]*api.EncryptedSecret, len(secrets)), |
| 133 | + } |
| 134 | + for _, dir := range dirs { |
| 135 | + request, err := reencrypter.client.encryptDirFor(dir, &account) |
| 136 | + assert.OK(t, err) |
| 137 | + encryptedTree.Directories[dir.DirID] = &api.EncryptedDir{ |
| 138 | + DirID: dir.DirID, |
| 139 | + EncryptedName: request.EncryptedName, |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + for i, secret := range secrets { |
| 144 | + request, err := reencrypter.client.encryptSecretFor(secret, &account) |
| 145 | + assert.OK(t, err) |
| 146 | + encryptedTree.Secrets[i] = &api.EncryptedSecret{ |
| 147 | + DirID: secret.DirID, |
| 148 | + EncryptedName: request.Name.EncryptedName, |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return encryptedTree |
| 153 | +} |
0 commit comments