Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for using env vars in config #10

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,25 @@ chains:
grpc: 'grpc-axelar.endpoint.xyz:9090'
```

## Example Using Environment Variables
Environment variables can be used to inject values into the configuration file.
For this a simple template variable marked by `${}` (e.g. `${SUPER_SECRET_ENV_VAR}`) can be added to the config file.

```yaml
monikers: ['all']

chains:
- display_name: 'cosmos'
chain_id: cosmoshub-4
tracking_addresses:
- 'cosmos1clpqr4nrk4khgkxj78fcwwh6dl3uw4ep4tgu9q'
- '${THIS_IS_ALSO_AN_ENV_VAR}'
nodes:
- rpc: 'https://rpc-cosmos.endpoint.xyz/${SUPER_SECRET_TOKEN}'
api: 'https://lcd-cosmos.endpoint.xyz/${ANOTHER_ENV_VAR}'
grpc: 'grpc-cosmos.endpoint.xyz:9090'
```

## Example: Supporting Custom Chain

For devents, testnets, localnet even if unsupported mainnets, Use `custom_chains.yaml` for CVMS
Expand Down
23 changes: 23 additions & 0 deletions internal/helper/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"fmt"
"os"
"regexp"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -31,13 +32,35 @@ type NodeEndPoint struct {
GRPC string `yaml:"grpc"`
}

func replacePlaceholders(byteString []byte) []byte {
// Simple function to replace placeholders in the config file with their corresponding environment variables.
// Variables take the form of ${VAR_NAME}

// Convert the byte slice to a string
cfg := string(byteString)

// Regular expression to match placeholders in the format ${VAR_NAME}
re := regexp.MustCompile(`\$\{([a-zA-Z_][a-zA-Z0-9_]*)\}`)

cfg = re.ReplaceAllStringFunc(cfg, func(match string) string {
varName := re.FindStringSubmatch(match)[1]
if value, exists := os.LookupEnv(varName); exists {
return value
}
return match // return original placeholder if not found in environment variables
})
return []byte(cfg)
}

// TODO: ignore failed chains
func GetConfig(path string) (*MonitoringConfig, error) {
dataBytes, err := os.ReadFile(path)
if err != nil {
return nil, errors.Wrapf(err, "failed to read config file")
}

dataBytes = replacePlaceholders(dataBytes)

cfg := &MonitoringConfig{}
err = yaml.Unmarshal(dataBytes, cfg)
if err != nil {
Expand Down
46 changes: 46 additions & 0 deletions internal/helper/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,49 @@ func TestGetSupportChainConfigForConsumerChains(t *testing.T) {
}
}
}

func TestReplacePlaceholders(t *testing.T) {
yamlCfg := `
chains:
- display_name: "band"
chain_id: "laozi-mainnet"
monikers:
- "test"
nodes:
- rpc: "${RPC_NODE1}"
api: "${API_NODE1}"
grpc: "${GRPC_NODE1}"
- rpc: "${RPC_NODE2}"
api: "${API_NODE2}"
grpc: "${GRPC_NODE2}"
`
envVars := map[string]string{
"RPC_NODE1": "https://rpc.bandchain.org",
"API_NODE1": "https://api.bandchain.org",
"GRPC_NODE1": "https://grpc.bandchain."}

envVarsNotSet := []string{
"RPC_NODE2",
"API_NODE2",
"GRPC_NODE2",
}

for key, value := range envVars {
os.Setenv(key, value)
}

replaceCfg := replacePlaceholders([]byte(yamlCfg))

replaceCfgStr := string(replaceCfg)

// test if unset palceholders were not replaced
for _, key := range envVarsNotSet {
assert.Contains(t, replaceCfgStr, fmt.Sprintf("${%s}", key))
}

// test if set palceholders were replaced
for key, value := range envVars {
assert.NotContains(t, replaceCfgStr, fmt.Sprintf("%s: %s", key, value))
}

}