Skip to content

Commit

Permalink
[Functions] Use byte array for secrets in RequestData structure (#9677)
Browse files Browse the repository at this point in the history
Co-authored-by: Morgan Kuphal <87319522+KuphJr@users.noreply.github.com>
  • Loading branch information
bolekk and KuphJr authored Jun 21, 2023
1 parent e4b4d03 commit 8931827
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
11 changes: 7 additions & 4 deletions core/services/functions/external_adapter_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func TestRunComputation_CorrectAdapterRequest(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
assert.NoError(t, err)
expectedData := `{"source":"abcd","language":0,"codeLocation":42,"secrets":"","secretsLocation":0,"args":["arg1","arg2"]}`
expectedData := `{"source":"abcd","language":7,"codeLocation":42,"secrets":"qrvM","secretsLocation":88,"args":["arg1","arg2"]}`
expectedBody := fmt.Sprintf(`{"endpoint":"lambda","requestId":"requestID1234","jobName":"TestJob","subscriptionOwner":"SubOwner","subscriptionId":1,"nodeProvidedSecrets":"secRETS","data":%s}`, expectedData)
assert.Equal(t, expectedBody, string(body))

Expand All @@ -179,9 +179,12 @@ func TestRunComputation_CorrectAdapterRequest(t *testing.T) {

ea := functions.NewExternalAdapterClient(*adapterUrl, 100_000)
reqData := &functions.RequestData{
Source: "abcd",
CodeLocation: 42,
Args: []string{"arg1", "arg2"},
Source: "abcd",
Language: 7,
CodeLocation: 42,
Secrets: []byte{0xaa, 0xbb, 0xcc}, // "qrvM" base64 encoded
SecretsLocation: 88,
Args: []string{"arg1", "arg2"},
}
_, _, _, err = ea.RunComputation(testutils.Context(t), "requestID1234", "TestJob", "SubOwner", 1, "secRETS", reqData)
assert.Error(t, err)
Expand Down
7 changes: 5 additions & 2 deletions core/services/functions/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/core/services/functions"
functions_service "github.com/smartcontractkit/chainlink/v2/core/services/functions"
functions_mocks "github.com/smartcontractkit/chainlink/v2/core/services/functions/mocks"
"github.com/smartcontractkit/chainlink/v2/core/services/job"
Expand Down Expand Up @@ -279,11 +278,14 @@ func TestFunctionsListener_HandleOracleRequestCBORParsingCorrect(t *testing.T) {
incomingData := &struct {
Source string `cbor:"source"`
Language int `cbor:"language"`
Secrets []byte `cbor:"secrets"`
Args []string `cbor:"args"`
ExtraUnwantedParam string `cbor:"extraUnwantedParam"`
// missing CodeLocation and SecretsLocation
}{
Source: "abcd",
Language: 3,
Secrets: []byte{0xaa, 0xbb},
Args: []string{"a", "b"},
ExtraUnwantedParam: "spam",
}
Expand All @@ -298,9 +300,10 @@ func TestFunctionsListener_HandleOracleRequestCBORParsingCorrect(t *testing.T) {
uni.logBroadcaster.On("MarkConsumed", mock.Anything, mock.Anything).Return(nil)
uni.bridgeAccessor.On("NewExternalAdapterClient").Return(uni.eaClient, nil)
uni.eaClient.On("RunComputation", mock.Anything, RequestIDStr, mock.Anything, SubscriptionOwner.Hex(), SubscriptionID, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
reqData := args.Get(6).(*functions.RequestData)
reqData := args.Get(6).(*functions_service.RequestData)
assert.Equal(t, incomingData.Source, reqData.Source)
assert.Equal(t, incomingData.Language, reqData.Language)
assert.Equal(t, incomingData.Secrets, reqData.Secrets)
assert.Equal(t, incomingData.Args, reqData.Args)
}).Return(ResultBytes, nil, nil, nil)
uni.pluginORM.On("SetResult", RequestID, mock.Anything, ResultBytes, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
Expand Down
8 changes: 7 additions & 1 deletion core/services/functions/request.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package functions

const (
LocationInline = 0
LocationRemote = 1
LanguageJavaScript = 0
)

type RequestData struct {
Source string `json:"source" cbor:"source"`
Language int `json:"language" cbor:"language"`
CodeLocation int `json:"codeLocation" cbor:"codeLocation"`
Secrets string `json:"secrets" cbor:"secrets"`
Secrets []byte `json:"secrets" cbor:"secrets"`
SecretsLocation int `json:"secretsLocation" cbor:"secretsLocation"`
Args []string `json:"args" cbor:"args"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ func TestIntegration_Functions_MultipleRequests_Success(t *testing.T) {
for j := 0; j < requestLenBytes; j++ {
requestSources[i][j] = byte(rnd.Uint32() % 256)
}
_, err := clientContracts[i].Contract.SendRequest(owner, hex.EncodeToString(requestSources[i]), []byte{}, []string{}, subscriptionId)
_, err := clientContracts[i].Contract.SendRequest(
owner,
hex.EncodeToString(requestSources[i]),
utils.DefaultSecretsBytes,
[]string{utils.DefaultArg1, utils.DefaultArg2},
subscriptionId)
require.NoError(t, err)
}
utils.CommitWithFinality(b)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/internal/cltest/heavyweight"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/core/services/functions"
"github.com/smartcontractkit/chainlink/v2/core/services/job"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ocr2key"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/p2pkey"
Expand All @@ -43,6 +44,13 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/utils"
)

var (
DefaultSecretsBytes = []byte{0xaa, 0xbb, 0xcc}
DefaultSecretsBase64 = "qrvM"
DefaultArg1 = "arg1"
DefaultArg2 = "arg2"
)

func ptr[T any](v T) *T { return &v }

func SetOracleConfig(t *testing.T, owner *bind.TransactOpts, oracleContract *ocr2dr_oracle.OCR2DROracle, oracles []confighelper2.OracleIdentityExtra, batchSize int) {
Expand Down Expand Up @@ -371,7 +379,16 @@ func StartNewMockEA(t *testing.T) *httptest.Server {
require.NoError(t, err)
var jsonMap map[string]any
require.NoError(t, json.Unmarshal(b, &jsonMap))
source := jsonMap["data"].(map[string]any)["source"].(string)
data := jsonMap["data"].(map[string]any)
require.Equal(t, functions.LanguageJavaScript, int(data["language"].(float64)))
require.Equal(t, functions.LocationInline, int(data["codeLocation"].(float64)))
require.Equal(t, functions.LocationRemote, int(data["secretsLocation"].(float64)))
require.Equal(t, DefaultSecretsBase64, data["secrets"].(string))
args := data["args"].([]interface{})
require.Equal(t, 2, len(args))
require.Equal(t, DefaultArg1, args[0].(string))
require.Equal(t, DefaultArg2, args[1].(string))
source := data["source"].(string)
res.WriteHeader(http.StatusOK)
// prepend "0xab" to source and return as result
_, err = res.Write([]byte(fmt.Sprintf(`{"result": "success", "statusCode": 200, "data": {"result": "0xab%s", "error": ""}}`, source)))
Expand Down

0 comments on commit 8931827

Please sign in to comment.