From 77423df56e1760e780d0d91b837042de8c6f0117 Mon Sep 17 00:00:00 2001 From: ehsan shariati Date: Thu, 2 Nov 2023 13:09:06 -0400 Subject: [PATCH 1/6] corrected PlugSeed --- blockchain/blockchain.go | 44 ++++++++++++++++++++++++++++++------- blockchain/keystore_test.go | 15 +++++++++++-- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/blockchain/blockchain.go b/blockchain/blockchain.go index 33f2ea95..39a51a36 100644 --- a/blockchain/blockchain.go +++ b/blockchain/blockchain.go @@ -169,8 +169,6 @@ func (bl *FxBlockchain) callBlockchain(ctx context.Context, method string, actio } } -type ReqInterface interface{} - func (bl *FxBlockchain) PlugSeedIfNeeded(ctx context.Context, action string, req interface{}) interface{} { switch action { case actionSeeded, actionAccountExists, actionPoolCreate, actionPoolJoin, actionPoolCancelJoin, actionPoolRequests, actionPoolList, actionPoolVote, actionPoolLeave, actionManifestUpload, actionManifestStore, actionManifestAvailable, actionManifestRemove, actionManifestRemoveStorer, actionManifestRemoveStored: @@ -179,17 +177,47 @@ func (bl *FxBlockchain) PlugSeedIfNeeded(ctx context.Context, action string, req log.Errorw("seed is empty", "err", err) seed = "" } - return struct { - ReqInterface - Seed string - }{ - ReqInterface: req, - Seed: seed, + log.Debugf("seed is %s", seed) + log.Debugf("request is %v", req) + + // Make sure we are dealing with a pointer to a struct + val := reflect.ValueOf(req) + if val.Kind() != reflect.Ptr || val.Elem().Kind() != reflect.Struct { + log.Error("req is not a pointer to a struct") + return req } + + // Create a new struct based on the req's type and then set the Seed field + reqVal := val.Elem() + seededReqType := reflect.StructOf([]reflect.StructField{ + { + Name: "Seed", + Type: reflect.TypeOf(""), + Tag: `json:"seed"`, + }, + }) + seededReqVal := reflect.New(seededReqType).Elem() + seededReqVal.FieldByName("Seed").SetString(seed) + + // Create a new struct that is a combination of the request struct and the Seed field + combinedReqType := reflect.StructOf(append(reflect.VisibleFields(reqVal.Type()), seededReqVal.Type().Field(0))) + combinedReq := reflect.New(combinedReqType).Elem() + + // Copy the request struct fields to the new combined struct + for i := 0; i < reqVal.NumField(); i++ { + combinedReq.Field(i).Set(reqVal.Field(i)) + } + // Set the Seed field + combinedReq.FieldByName("Seed").SetString(seed) + + log.Debugf("seeded request is %v", combinedReq.Interface()) + return combinedReq.Interface() + default: return req } } + func (bl *FxBlockchain) serve(w http.ResponseWriter, r *http.Request) { from, err := peer.Decode(r.RemoteAddr) diff --git a/blockchain/keystore_test.go b/blockchain/keystore_test.go index 78bdfb45..39eae4ff 100644 --- a/blockchain/keystore_test.go +++ b/blockchain/keystore_test.go @@ -1,10 +1,21 @@ package blockchain import ( - "fmt" + "context" "testing" ) func TestSimpleKeyStore(t *testing.T) { - fmt.Printf("test removed") + keyStore := NewSimpleKeyStorer("") + err := keyStore.SaveKey(context.Background(), "dummy") + if err != nil { + t.Errorf("while save key: %v", err) + } + key, err := keyStore.LoadKey(context.Background()) + if err != nil { + t.Errorf("while load key: %v", err) + } + if string(key) != "dummy" { + t.Errorf("error loading the stored key: %v != dummy", string(key)) + } } From 1a91e111fe2a66ec042d62322cbd726d47f79583 Mon Sep 17 00:00:00 2001 From: ehsan shariati Date: Thu, 2 Nov 2023 13:25:33 -0400 Subject: [PATCH 2/6] corrected dbPath --- .github/workflows/go-test.yml | 13 +++++++++++++ blockchain/keystore.go | 5 ++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/.github/workflows/go-test.yml b/.github/workflows/go-test.yml index 872421b4..d7082bcd 100644 --- a/.github/workflows/go-test.yml +++ b/.github/workflows/go-test.yml @@ -7,15 +7,28 @@ jobs: steps: - name: Checkout uses: actions/checkout@v3 + - name: Set up Go uses: actions/setup-go@v3 with: go-version: "1.19.x" + - name: Go information run: | go version go env + + - name: Create directory for secrets + run: | + mkdir -p ./secrets + chmod 700 ./secrets + - name: Run tests run: go test -v -shuffle=on ./... + env: + SECRETS_DIR: ${{ github.workspace }}/secrets + - name: Run tests with race detector run: go test -v -race ./... + env: + SECRETS_DIR: ${{ github.workspace }}/secrets diff --git a/blockchain/keystore.go b/blockchain/keystore.go index 6cc5ed70..89d53a78 100644 --- a/blockchain/keystore.go +++ b/blockchain/keystore.go @@ -26,7 +26,10 @@ func NewSimpleKeyStorer(dbPath string) *SimpleKeyStorer { err := os.MkdirAll(dbPath, 0755) if err != nil { // Fallback to a local directory - dbPath = "." + dbPath = os.Getenv("SECRETS_DIR") + if dbPath == "" { + dbPath = "." + } } } From a1a72ba6942f9c34fee63fd51df2e6169b225924 Mon Sep 17 00:00:00 2001 From: ehsan shariati Date: Thu, 2 Nov 2023 13:34:53 -0400 Subject: [PATCH 3/6] Update go-test.yml --- .github/workflows/go-test.yml | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/.github/workflows/go-test.yml b/.github/workflows/go-test.yml index d7082bcd..c5256f2e 100644 --- a/.github/workflows/go-test.yml +++ b/.github/workflows/go-test.yml @@ -12,17 +12,16 @@ jobs: uses: actions/setup-go@v3 with: go-version: "1.19.x" - - name: Go information run: | go version go env - - name: Create directory for secrets run: | - mkdir -p ./secrets - chmod 700 ./secrets - + mkdir -p $GITHUB_WORKSPACE/secrets + chmod 700 $GITHUB_WORKSPACE/secrets + - name: Check permissions + run: ls -lha $GITHUB_WORKSPACE/secrets - name: Run tests run: go test -v -shuffle=on ./... env: From 83d68be3181b18b6155afc5d021dd117bbea1f0e Mon Sep 17 00:00:00 2001 From: ehsan shariati Date: Thu, 2 Nov 2023 13:41:27 -0400 Subject: [PATCH 4/6] Update go-test.yml --- .github/workflows/go-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/go-test.yml b/.github/workflows/go-test.yml index c5256f2e..e85b7790 100644 --- a/.github/workflows/go-test.yml +++ b/.github/workflows/go-test.yml @@ -19,7 +19,7 @@ jobs: - name: Create directory for secrets run: | mkdir -p $GITHUB_WORKSPACE/secrets - chmod 700 $GITHUB_WORKSPACE/secrets + chmod 777 $GITHUB_WORKSPACE/secrets - name: Check permissions run: ls -lha $GITHUB_WORKSPACE/secrets - name: Run tests From eb399412efe2a326d297392da9c12d6397819974 Mon Sep 17 00:00:00 2001 From: ehsan shariati Date: Thu, 2 Nov 2023 13:44:03 -0400 Subject: [PATCH 5/6] Update go-test.yml --- .github/workflows/go-test.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/go-test.yml b/.github/workflows/go-test.yml index e85b7790..d9f822c1 100644 --- a/.github/workflows/go-test.yml +++ b/.github/workflows/go-test.yml @@ -16,6 +16,9 @@ jobs: run: | go version go env + - name: Print GITHUB_WORKSPACE + run: | + echo "GITHUB_WORKSPACE: $GITHUB_WORKSPACE" - name: Create directory for secrets run: | mkdir -p $GITHUB_WORKSPACE/secrets From a205218f92ed614f5eb72451a832a02ffbd73a5c Mon Sep 17 00:00:00 2001 From: ehsan shariati Date: Thu, 2 Nov 2023 13:51:32 -0400 Subject: [PATCH 6/6] Update keystore.go --- blockchain/keystore.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blockchain/keystore.go b/blockchain/keystore.go index 89d53a78..2a896bf4 100644 --- a/blockchain/keystore.go +++ b/blockchain/keystore.go @@ -37,7 +37,7 @@ func NewSimpleKeyStorer(dbPath string) *SimpleKeyStorer { } func (s *SimpleKeyStorer) SaveKey(ctx context.Context, key string) error { - return os.WriteFile(s.dbPath+"/secret_seed.txt", []byte(key), 0400) + return os.WriteFile(s.dbPath+"/secret_seed.txt", []byte(key), 0600) } func (s *SimpleKeyStorer) LoadKey(ctx context.Context) (string, error) {