Skip to content

Commit

Permalink
fix gosec + refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
bermuell committed Dec 6, 2023
1 parent eb4c36e commit e441d2c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 35 deletions.
87 changes: 53 additions & 34 deletions tests/e2e/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e441d2c

Please sign in to comment.