|
1 | 1 | package vault |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "crypto/sha256" |
4 | 5 | "encoding/hex" |
5 | 6 | "testing" |
6 | 7 |
|
| 8 | + "github.com/ethereum/go-ethereum/common" |
| 9 | + "github.com/smartcontractkit/tdh2/go/tdh2/tdh2easy" |
| 10 | + "github.com/stretchr/testify/assert" |
7 | 11 | "github.com/stretchr/testify/require" |
8 | 12 |
|
9 | 13 | vaultcommon "github.com/smartcontractkit/chainlink-common/pkg/capabilities/actions/vault" |
10 | 14 | pkgconfig "github.com/smartcontractkit/chainlink-common/pkg/config" |
11 | 15 | "github.com/smartcontractkit/chainlink-common/pkg/settings/limits" |
| 16 | + "github.com/smartcontractkit/chainlink/v2/core/capabilities/vault/vaultutils" |
12 | 17 | ) |
13 | 18 |
|
| 19 | +func generateTestKeys(t *testing.T) (*tdh2easy.PublicKey, []*tdh2easy.PrivateShare) { |
| 20 | + t.Helper() |
| 21 | + _, pk, shares, err := tdh2easy.GenerateKeys(1, 3) |
| 22 | + require.NoError(t, err) |
| 23 | + return pk, shares |
| 24 | +} |
| 25 | + |
| 26 | +func encryptWithEthAddressLabel(t *testing.T, pk *tdh2easy.PublicKey, owner string) string { |
| 27 | + t.Helper() |
| 28 | + encrypted, err := vaultutils.EncryptSecretWithWorkflowOwner("test-secret", pk, common.HexToAddress(owner)) |
| 29 | + require.NoError(t, err) |
| 30 | + return encrypted |
| 31 | +} |
| 32 | + |
| 33 | +func encryptWithOrgIDLabel(t *testing.T, pk *tdh2easy.PublicKey, orgID string) string { |
| 34 | + t.Helper() |
| 35 | + encrypted, err := vaultutils.EncryptSecretWithOrgID("test-secret", pk, orgID) |
| 36 | + require.NoError(t, err) |
| 37 | + return encrypted |
| 38 | +} |
| 39 | + |
| 40 | +func TestWorkflowOwnerToLabel(t *testing.T) { |
| 41 | + t.Run("ethereum address with 0x prefix", func(t *testing.T) { |
| 42 | + addr := "0x0001020304050607080900010203040506070809" |
| 43 | + label := vaultutils.WorkflowOwnerToLabel(addr) |
| 44 | + |
| 45 | + var expected [32]byte |
| 46 | + copy(expected[12:], common.HexToAddress(addr).Bytes()) |
| 47 | + assert.Equal(t, expected, label) |
| 48 | + }) |
| 49 | + |
| 50 | + t.Run("ethereum address without 0x prefix", func(t *testing.T) { |
| 51 | + addr := "0001020304050607080900010203040506070809" |
| 52 | + label := vaultutils.WorkflowOwnerToLabel(addr) |
| 53 | + |
| 54 | + var expected [32]byte |
| 55 | + copy(expected[12:], common.HexToAddress(addr).Bytes()) |
| 56 | + assert.Equal(t, expected, label) |
| 57 | + }) |
| 58 | + |
| 59 | + t.Run("checksummed ethereum address", func(t *testing.T) { |
| 60 | + addr := "0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B" |
| 61 | + label := vaultutils.WorkflowOwnerToLabel(addr) |
| 62 | + |
| 63 | + var expected [32]byte |
| 64 | + copy(expected[12:], common.HexToAddress(addr).Bytes()) |
| 65 | + assert.Equal(t, expected, label) |
| 66 | + }) |
| 67 | +} |
| 68 | + |
| 69 | +func TestOrgIDToLabel(t *testing.T) { |
| 70 | + t.Run("org_id produces SHA256 label", func(t *testing.T) { |
| 71 | + orgID := "org_2xAbCdEfGhIjKlMnOpQrStUvWxYz" |
| 72 | + label := vaultutils.OrgIDToLabel(orgID) |
| 73 | + |
| 74 | + expected := sha256.Sum256([]byte(orgID)) |
| 75 | + assert.Equal(t, expected, label) |
| 76 | + }) |
| 77 | + |
| 78 | + t.Run("short string", func(t *testing.T) { |
| 79 | + orgID := "my-org-id" |
| 80 | + label := vaultutils.OrgIDToLabel(orgID) |
| 81 | + |
| 82 | + expected := sha256.Sum256([]byte(orgID)) |
| 83 | + assert.Equal(t, expected, label) |
| 84 | + }) |
| 85 | +} |
| 86 | + |
| 87 | +func TestEnsureRightLabelOnSecret_WorkflowOwnerOnly(t *testing.T) { |
| 88 | + pk, _ := generateTestKeys(t) |
| 89 | + owner := "0x0001020304050607080900010203040506070809" |
| 90 | + secret := encryptWithEthAddressLabel(t, pk, owner) |
| 91 | + |
| 92 | + err := EnsureRightLabelOnSecret(pk, secret, owner, "") |
| 93 | + assert.NoError(t, err) |
| 94 | +} |
| 95 | + |
| 96 | +func TestEnsureRightLabelOnSecret_OrgIDOnly(t *testing.T) { |
| 97 | + pk, _ := generateTestKeys(t) |
| 98 | + orgID := "org_2xAbCdEfGhIjKlMnOpQrStUvWxYz" |
| 99 | + secret := encryptWithOrgIDLabel(t, pk, orgID) |
| 100 | + |
| 101 | + err := EnsureRightLabelOnSecret(pk, secret, "", orgID) |
| 102 | + assert.NoError(t, err) |
| 103 | +} |
| 104 | + |
| 105 | +func TestEnsureRightLabelOnSecret_DualMatchesWorkflowOwner(t *testing.T) { |
| 106 | + pk, _ := generateTestKeys(t) |
| 107 | + ethAddr := "0x0001020304050607080900010203040506070809" |
| 108 | + orgID := "org_2xAbCdEfGhIjKlMnOpQrStUvWxYz" |
| 109 | + secret := encryptWithEthAddressLabel(t, pk, ethAddr) |
| 110 | + |
| 111 | + err := EnsureRightLabelOnSecret(pk, secret, ethAddr, orgID) |
| 112 | + assert.NoError(t, err) |
| 113 | +} |
| 114 | + |
| 115 | +func TestEnsureRightLabelOnSecret_DualMatchesOrgID(t *testing.T) { |
| 116 | + pk, _ := generateTestKeys(t) |
| 117 | + ethAddr := "0x0001020304050607080900010203040506070809" |
| 118 | + orgID := "org_2xAbCdEfGhIjKlMnOpQrStUvWxYz" |
| 119 | + secret := encryptWithOrgIDLabel(t, pk, orgID) |
| 120 | + |
| 121 | + err := EnsureRightLabelOnSecret(pk, secret, ethAddr, orgID) |
| 122 | + assert.NoError(t, err) |
| 123 | +} |
| 124 | + |
| 125 | +func TestEnsureRightLabelOnSecret_NeitherMatches(t *testing.T) { |
| 126 | + pk, _ := generateTestKeys(t) |
| 127 | + ethAddr := "0x0001020304050607080900010203040506070809" |
| 128 | + wrongAddr := "0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" |
| 129 | + wrongOrgID := "org_wrong" |
| 130 | + secret := encryptWithEthAddressLabel(t, pk, ethAddr) |
| 131 | + |
| 132 | + err := EnsureRightLabelOnSecret(pk, secret, wrongAddr, wrongOrgID) |
| 133 | + require.Error(t, err) |
| 134 | + assert.Contains(t, err.Error(), "does not match any of the provided owner labels") |
| 135 | +} |
| 136 | + |
| 137 | +func TestEnsureRightLabelOnSecret_BothEmpty(t *testing.T) { |
| 138 | + pk, _ := generateTestKeys(t) |
| 139 | + ethAddr := "0x0001020304050607080900010203040506070809" |
| 140 | + secret := encryptWithEthAddressLabel(t, pk, ethAddr) |
| 141 | + |
| 142 | + err := EnsureRightLabelOnSecret(pk, secret, "", "") |
| 143 | + require.Error(t, err) |
| 144 | + assert.Contains(t, err.Error(), "does not match any of the provided owner labels") |
| 145 | +} |
| 146 | + |
| 147 | +func TestEnsureRightLabelOnSecret_NilPublicKey(t *testing.T) { |
| 148 | + pk, _ := generateTestKeys(t) |
| 149 | + ethAddr := "0x0001020304050607080900010203040506070809" |
| 150 | + secret := encryptWithEthAddressLabel(t, pk, ethAddr) |
| 151 | + |
| 152 | + err := EnsureRightLabelOnSecret(nil, secret, ethAddr, "") |
| 153 | + assert.NoError(t, err) |
| 154 | +} |
| 155 | + |
| 156 | +func TestEnsureRightLabelOnSecret_InvalidHexSecret(t *testing.T) { |
| 157 | + pk, _ := generateTestKeys(t) |
| 158 | + |
| 159 | + err := EnsureRightLabelOnSecret(pk, "not-valid-hex!", "0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "") |
| 160 | + require.Error(t, err) |
| 161 | + assert.Contains(t, err.Error(), "failed to decode encrypted value") |
| 162 | +} |
| 163 | + |
| 164 | +func TestEnsureRightLabelOnSecret_InvalidCiphertext(t *testing.T) { |
| 165 | + pk, _ := generateTestKeys(t) |
| 166 | + |
| 167 | + err := EnsureRightLabelOnSecret(pk, hex.EncodeToString([]byte("garbage")), "0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "") |
| 168 | + require.Error(t, err) |
| 169 | + assert.Contains(t, err.Error(), "failed to verify encrypted value") |
| 170 | +} |
| 171 | + |
| 172 | +func TestEnsureRightLabelOnSecret_WrongPublicKey(t *testing.T) { |
| 173 | + pk, _ := generateTestKeys(t) |
| 174 | + wrongPK, _ := generateTestKeys(t) |
| 175 | + ethAddr := "0x0001020304050607080900010203040506070809" |
| 176 | + secret := encryptWithEthAddressLabel(t, pk, ethAddr) |
| 177 | + |
| 178 | + err := EnsureRightLabelOnSecret(wrongPK, secret, ethAddr, "") |
| 179 | + require.Error(t, err) |
| 180 | + assert.Contains(t, err.Error(), "failed to verify encrypted value") |
| 181 | +} |
| 182 | + |
| 183 | +func TestEnsureRightLabelOnSecret_BackwardCompatSingleOwner(t *testing.T) { |
| 184 | + pk, _ := generateTestKeys(t) |
| 185 | + owner := "0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B" |
| 186 | + secret := encryptWithEthAddressLabel(t, pk, owner) |
| 187 | + |
| 188 | + err := EnsureRightLabelOnSecret(pk, secret, owner, "") |
| 189 | + assert.NoError(t, err) |
| 190 | +} |
| 191 | + |
| 192 | +func TestEnsureRightLabelOnSecret_LegacySecretReadViaNewFlow(t *testing.T) { |
| 193 | + pk, _ := generateTestKeys(t) |
| 194 | + workflowOwner := "0x0001020304050607080900010203040506070809" |
| 195 | + orgID := "org_2xAbCdEfGhIjKlMnOpQrStUvWxYz" |
| 196 | + |
| 197 | + secret := encryptWithEthAddressLabel(t, pk, workflowOwner) |
| 198 | + err := EnsureRightLabelOnSecret(pk, secret, workflowOwner, orgID) |
| 199 | + assert.NoError(t, err) |
| 200 | +} |
| 201 | + |
| 202 | +func TestEnsureRightLabelOnSecret_NewSecretReadViaNewFlow(t *testing.T) { |
| 203 | + pk, _ := generateTestKeys(t) |
| 204 | + orgID := "org_2xAbCdEfGhIjKlMnOpQrStUvWxYz" |
| 205 | + workflowOwner := "0x0001020304050607080900010203040506070809" |
| 206 | + |
| 207 | + secret := encryptWithOrgIDLabel(t, pk, orgID) |
| 208 | + err := EnsureRightLabelOnSecret(pk, secret, workflowOwner, orgID) |
| 209 | + assert.NoError(t, err) |
| 210 | +} |
| 211 | + |
14 | 212 | func TestRequestValidator_CiphertextSizeLimit(t *testing.T) { |
15 | 213 | validator := NewRequestValidator( |
16 | 214 | limits.NewUpperBoundLimiter(10), |
|
0 commit comments