Support environment variables for selected config fields (#40)

* deployment time config using env variables

* check if ethereum is enabled before replacing the config value with env variable
This commit is contained in:
Tak Wai Wong 2022-09-30 13:41:29 -07:00
parent 04a78694d1
commit c2e15cfed9
No known key found for this signature in database
GPG key ID: 222E4AF2AA1F467D
2 changed files with 61 additions and 4 deletions

View file

@ -23,6 +23,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strconv"
"strings" "strings"
"github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/auth/authtypes"
@ -291,6 +292,9 @@ func LoadMatrixKey(privateKeyPath string, readFile func(string) ([]byte, error))
// Derive generates data that is derived from various values provided in // Derive generates data that is derived from various values provided in
// the config file. // the config file.
func (config *Dendrite) Derive() error { func (config *Dendrite) Derive() error {
// Replace selected config with env variables.
config.replaceWithEnvVariables()
// Determine registrations flows based off config values // Determine registrations flows based off config values
config.Derived.Registration.Params = make(map[string]interface{}) config.Derived.Registration.Params = make(map[string]interface{})
@ -576,6 +580,58 @@ func (config *Dendrite) SetupTracing(serviceName string) (closer io.Closer, err
) )
} }
/*
*
Replace selected config with environment variables
*/
func (config *Dendrite) replaceWithEnvVariables() {
// Replace selected fields with env variables
config.Global.ServerName = gomatrixserverlib.ServerName(
replaceWithEnvVariables(string(config.Global.ServerName)),
)
logrus.Infof("Matrix ServerName=%s\n", config.Global.ServerName)
config.Global.DatabaseOptions.ConnectionString = DataSource(
replaceWithEnvVariables(
string(config.Global.DatabaseOptions.ConnectionString),
),
)
// If env variable is set, convert the deployment chain IDs from the env
// variable into []int and replace the ChainIDs field.
if config.ClientAPI.PublicKeyAuthentication.Ethereum.Enabled {
deploymentChainIDs := replaceWithEnvVariables(config.ClientAPI.PublicKeyAuthentication.Ethereum.DeploymentChainIDs)
chainIds := strings.Split(deploymentChainIDs, ",")
if len(chainIds) > 0 && chainIds[0] != "" {
var ids []int
for _, id := range chainIds {
id, err := strconv.Atoi(strings.TrimSpace(id))
if err == nil {
ids = append(ids, id)
}
}
config.ClientAPI.PublicKeyAuthentication.Ethereum.ChainIDs = ids
}
logrus.Infof("Supported Ethereum chain IDs=%d\n", config.ClientAPI.PublicKeyAuthentication.Ethereum.ChainIDs)
}
}
var regexpEnvVariables = regexp.MustCompile(`\$\{(?P<Var>\w+)\}`)
var varIndex = regexpEnvVariables.SubexpIndex("Var")
func replaceWithEnvVariables(value string) string {
matches := regexpEnvVariables.FindAllStringSubmatch(value, -1)
for _, m := range matches {
if varIndex < len(m) {
envValue := os.Getenv(m[varIndex])
value = strings.ReplaceAll(value, fmt.Sprintf("${%s}", m[varIndex]), envValue)
}
}
return value
}
// logrusLogger is a small wrapper that implements jaeger.Logger using logrus. // logrusLogger is a small wrapper that implements jaeger.Logger using logrus.
type logrusLogger struct { type logrusLogger struct {
l *logrus.Logger l *logrus.Logger

View file

@ -24,6 +24,7 @@ type EthereumAuthConfig struct {
Enabled bool `yaml:"enabled"` Enabled bool `yaml:"enabled"`
Version uint `yaml:"version"` Version uint `yaml:"version"`
ChainIDs []int `yaml:"chain_ids"` ChainIDs []int `yaml:"chain_ids"`
DeploymentChainIDs string `yaml:"deployment_chain_ids"` // For deployment: use env variable strings to override the chain IDs.
EnableAuthz bool `yaml:"enable_authz"` // Flag to enable / disable authorization during development EnableAuthz bool `yaml:"enable_authz"` // Flag to enable / disable authorization during development
} }