From e441d2c8fe4c7cf9b62575e12f5da9df2bea5cf9 Mon Sep 17 00:00:00 2001 From: Bernd Mueller Date: Wed, 6 Dec 2023 11:07:58 +0100 Subject: [PATCH] fix gosec + refactor --- tests/e2e/actions.go | 87 +++++++++++++++++++++++++++----------------- tests/e2e/builder.go | 2 +- 2 files changed, 54 insertions(+), 35 deletions(-) diff --git a/tests/e2e/actions.go b/tests/e2e/actions.go index 37ffa70c69..51157c9524 100644 --- a/tests/e2e/actions.go +++ b/tests/e2e/actions.go @@ -491,54 +491,73 @@ type startConsumerChainAction struct { GenesisChanges string } -func (tr *TestConfig) startConsumerChain( - action startConsumerChainAction, - verbose bool, -) { +// Transform consumer genesis content from older version +func (tr *TestConfig) transformConsumerGenesis(consumerChain ChainID, genesis []byte) []byte { + log.Print("Transforming consumer genesis") + log.Printf("Original ccv genesis: %s\n", string(genesis)) + + file, err := os.CreateTemp("", "consumer_genesis.json") + if err != nil { + panic(fmt.Sprintf("failed writing ccv consumer file : %v", err)) + } + defer file.Close() + + err = os.WriteFile(file.Name(), genesis, 0644) + if err != nil { + log.Fatalf("Failed writing consumer genesis to file: %v", err) + } + + containerInstance := tr.containerConfig.InstanceName //#nosec G204 -- Bypass linter warning for spawning subprocess with cmd arguments. - cmd := exec.Command("docker", "exec", tr.containerConfig.InstanceName, tr.chainConfigs[action.ProviderChain].BinaryName, + cmd := exec.Command("docker", "cp", file.Name(), fmt.Sprintf("%s:/tmp/%s", containerInstance, file.Name())) + genesis, err = cmd.CombinedOutput() + if err != nil { + log.Fatal(err, "\n", string(genesis)) + } + + consumerBinaryName := tr.chainConfigs[consumerChain].BinaryName + //#nosec G204 -- Bypass linter warning for spawning subprocess with cmd arguments. + cmd = exec.Command("docker", "exec", containerInstance, consumerBinaryName, + "genesis", "transform", fmt.Sprintf("/tmp/%s", file.Name())) + result, err := cmd.CombinedOutput() + if err != nil { + log.Fatal(err, "CCV consumer genesis transformation failed: %s", result) + } + log.Printf("Transformed genesis is: %v", result) + return result +} + +// Get consumer genesis from provider +func (tr *TestConfig) getConsumerGenesis(providerChain, consumerChain ChainID) string { + log.Print("Exporting consumer genesis from provider") + containerInstance := tr.containerConfig.InstanceName + providerBinaryName := tr.chainConfigs[providerChain].BinaryName + + //#nosec G204 -- Bypass linter warning for spawning subprocess with cmd arguments. + cmd := exec.Command("docker", "exec", containerInstance, providerBinaryName, "query", "provider", "consumer-genesis", - string(tr.chainConfigs[action.ConsumerChain].ChainId), + string(tr.chainConfigs[consumerChain].ChainId), - `--node`, tr.getQueryNode(action.ProviderChain), + `--node`, tr.getQueryNode(providerChain), `-o`, `json`, ) - if verbose { - log.Println("startConsumerChain cmd: ", cmd.String()) - } - bz, err := cmd.CombinedOutput() if err != nil { log.Fatal(err, "\n", string(bz)) } // only needed when consumer is running v3.3.x and later - if tr.transformGenesis { - log.Printf("Transforming consumer genesis for a newer version: %s\n", tr.consumerVersion) - log.Printf("Original ccv genesis: %s\n", string(bz)) - - file, err := os.Create("consumer_genesis.json") - if err != nil { - panic(fmt.Sprintf("failed writing ccv consumer file : %v", err)) - } - os.WriteFile(file.Name(), bz, 0644) - cmd := exec.Command("docker", "cp", file.Name(), fmt.Sprintf("%s:/tmp/%s", tr.containerConfig.InstanceName, file.Name())) - bz, err = cmd.CombinedOutput() - if err != nil { - log.Fatal(err, "\n", string(bz)) - } - cmd = exec.Command("docker", "exec", tr.containerConfig.InstanceName, tr.chainConfigs[action.ConsumerChain].BinaryName, - "genesis", "transform", fmt.Sprintf("/tmp/%s", file.Name())) - bz, err = cmd.CombinedOutput() - if err != nil { - log.Fatal(err, "CCV consumer genesis transformation failed: %s", string(bz)) - } - log.Printf("Transformed genesis is: %s", string(bz)) - } + return string(tr.transformConsumerGenesis(consumerChain, bz)) +} - consumerGenesis := ".app_state.ccvconsumer = " + string(bz) +func (tr *TestConfig) startConsumerChain( + action startConsumerChainAction, + verbose bool, +) { + log.Printf("Starting consumer chain %s", action.ConsumerChain) + consumerGenesis := ".app_state.ccvconsumer = " + tr.getConsumerGenesis(action.ProviderChain, action.ConsumerChain) consumerGenesisChanges := tr.chainConfigs[action.ConsumerChain].GenesisChanges if consumerGenesisChanges != "" { consumerGenesis = consumerGenesis + " | " + consumerGenesisChanges + " | " + action.GenesisChanges diff --git a/tests/e2e/builder.go b/tests/e2e/builder.go index 32de3d735c..dba3b4a1ee 100644 --- a/tests/e2e/builder.go +++ b/tests/e2e/builder.go @@ -67,7 +67,7 @@ func buildDockerImage(imageName string, revision string, tmpDir string) error { } log.Printf("Building docker image") - // TODO: TBD if we should use option "--no-cache" here + //#nosec G204 -- Bypass linter warning for spawning subprocess with cmd arguments. cmd := exec.Command("docker", "build", "-t", fmt.Sprintf("cosmos-ics:%s", revision), "-f", "./Dockerfile", "./") cmd.Dir = workSpace