From 8459ca5405055916d57046d240ef8071ae671690 Mon Sep 17 00:00:00 2001 From: ehsan shariati Date: Thu, 2 Nov 2023 14:03:03 -0400 Subject: [PATCH] corrected PlugSeed (#172) * corrected PlugSeed * corrected dbPath * Update go-test.yml * Update go-test.yml * Update go-test.yml * Update keystore.go --- .github/workflows/go-test.yml | 15 ++++++++++++ blockchain/blockchain.go | 44 ++++++++++++++++++++++++++++------- blockchain/keystore.go | 7 ++++-- blockchain/keystore_test.go | 15 ++++++++++-- 4 files changed, 69 insertions(+), 12 deletions(-) diff --git a/.github/workflows/go-test.yml b/.github/workflows/go-test.yml index 872421b4..d9f822c1 100644 --- a/.github/workflows/go-test.yml +++ b/.github/workflows/go-test.yml @@ -7,6 +7,7 @@ jobs: steps: - name: Checkout uses: actions/checkout@v3 + - name: Set up Go uses: actions/setup-go@v3 with: @@ -15,7 +16,21 @@ 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 + chmod 777 $GITHUB_WORKSPACE/secrets + - name: Check permissions + run: ls -lha $GITHUB_WORKSPACE/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/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.go b/blockchain/keystore.go index 6cc5ed70..2a896bf4 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 = "." + } } } @@ -34,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) { 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)) + } }