Fix dendrite config to use env for chain_id and enable_authz (#49)

* Fix config to support env variables
This commit is contained in:
Tak Wai Wong 2022-10-25 20:56:36 -07:00 committed by GitHub
parent 8d2033418e
commit c4afa77636
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 64 additions and 34 deletions

View file

@ -129,7 +129,7 @@ func (pk LoginPublicKeyEthereum) ValidateLoginResponse() (bool, *jsonerror.Matri
}
// Error if the chainId is not supported by the server.
if pk.config.PublicKeyAuthentication.Ethereum.ChainID != message.GetChainID() {
if pk.config.PublicKeyAuthentication.Ethereum.GetChainID() != message.GetChainID() {
return false, jsonerror.Forbidden("chainId")
}

View file

@ -18,6 +18,7 @@ import (
"context"
"fmt"
"net/http"
"strconv"
"strings"
"testing"
@ -43,9 +44,9 @@ func createLoginContext(_ *testing.T) *loginContext {
PasswordAuthenticationDisabled: true,
PublicKeyAuthentication: config.PublicKeyAuthentication{
Ethereum: config.EthereumAuthConfig{
Enabled: true,
Version: 1,
ChainID: testutil.EthereumTestNetworkId,
Enabled: true,
Version: 1,
ConfigChainID: strconv.Itoa(testutil.EthereumTestNetworkId),
},
},
}

View file

@ -10,7 +10,7 @@ import (
func NewAuthorization(cfg *config.ClientAPI, rsAPI roomserver.ClientRoomserverAPI) authorization.Authorization {
// Load authorization manager for Zion
if cfg.PublicKeyAuthentication.Ethereum.EnableAuthz {
if cfg.PublicKeyAuthentication.Ethereum.GetEnableAuthZ() {
auth, err := zion.NewZionAuthorization(cfg, rsAPI)
if err != nil {

View file

@ -19,6 +19,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"testing"
@ -48,9 +49,9 @@ func createRegisterContext(_ *testing.T) *registerContext {
PasswordAuthenticationDisabled: true,
PublicKeyAuthentication: config.PublicKeyAuthentication{
Ethereum: config.EthereumAuthConfig{
Enabled: true,
Version: 1,
ChainID: testutil.EthereumTestNetworkId,
Enabled: true,
Version: 1,
ConfigChainID: strconv.Itoa(testutil.EthereumTestNetworkId),
},
},
}

View file

@ -179,7 +179,7 @@ client_api:
enabled: false
version: 1
chain_id: 31337
networkUrl: "http://127.0.0.1:8545"
network_url: "http://127.0.0.1:8545"
# Whether to require reCAPTCHA for registration. If you have enabled registration
# then this is HIGHLY RECOMMENDED to reduce the risk of your homeserver being used

View file

@ -175,7 +175,7 @@ client_api:
enabled: false
version: 1
chain_id: 31337
networkUrl: "http://127.0.0.1:8545"
network_url: "http://127.0.0.1:8545"
# Whether to require reCAPTCHA for registration. If you have enabled registration
# then this is HIGHLY RECOMMENDED to reduce the risk of your homeserver being used

View file

@ -186,9 +186,8 @@ client_api:
ethereum:
enabled: true
version: 1
chain_id: 5
deployment_chain_id: ${CHAIN_ID}
networkUrl: ${BLOCKCHAIN_PROVIDER_URL}
chain_id: ${CHAIN_ID}
network_url: ${BLOCKCHAIN_PROVIDER_URL}
enable_authz: ${ENABLE_AUTHZ}
# Whether to require reCAPTCHA for registration.

View file

@ -23,7 +23,6 @@ import (
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/joho/godotenv"
@ -587,7 +586,9 @@ Replace selected config with environment variables
*/
func (config *Dendrite) replaceWithEnvVariables() {
// Replace selected fields with env variables
// If env variable is set, get the value from the env
// variable and replace it in each supported field.
err := godotenv.Load(".env")
if err != nil {
logrus.Errorln("error loading .env file", err)
@ -604,23 +605,21 @@ func (config *Dendrite) replaceWithEnvVariables() {
),
)
// 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 {
strChainId := replaceWithEnvVariables(config.ClientAPI.PublicKeyAuthentication.Ethereum.DeploymentChainID)
if strChainId != "" {
id, err := strconv.Atoi(strings.TrimSpace(strChainId))
if err == nil {
config.ClientAPI.PublicKeyAuthentication.Ethereum.ChainID = id
}
}
config.ClientAPI.PublicKeyAuthentication.Ethereum.ConfigChainID =
replaceWithEnvVariables(config.ClientAPI.PublicKeyAuthentication.Ethereum.ConfigChainID)
config.ClientAPI.PublicKeyAuthentication.Ethereum.NetworkUrl = replaceWithEnvVariables(config.ClientAPI.PublicKeyAuthentication.Ethereum.NetworkUrl)
config.ClientAPI.PublicKeyAuthentication.Ethereum.NetworkUrl =
replaceWithEnvVariables(config.ClientAPI.PublicKeyAuthentication.Ethereum.NetworkUrl)
config.ClientAPI.PublicKeyAuthentication.Ethereum.ConfigEnableAuthz =
replaceWithEnvVariables(config.ClientAPI.PublicKeyAuthentication.Ethereum.ConfigEnableAuthz)
logrus.Infof(
"Supported Ethereum chain ID=%d, network URL=%s",
config.ClientAPI.PublicKeyAuthentication.Ethereum.ChainID,
"Supported Ethereum chain_id=%v, network_url=%v, enable_authz=%v",
config.ClientAPI.PublicKeyAuthentication.Ethereum.ConfigChainID,
config.ClientAPI.PublicKeyAuthentication.Ethereum.NetworkUrl,
config.ClientAPI.PublicKeyAuthentication.Ethereum.ConfigEnableAuthz,
)
}
}

View file

@ -1,6 +1,9 @@
package config
import (
"strconv"
"strings"
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
)
@ -20,10 +23,37 @@ func (p EthereumAuthParams) GetParams() interface{} {
type EthereumAuthConfig struct {
Enabled bool `yaml:"enabled"`
Version uint `yaml:"version"`
ChainID int `yaml:"chain_id"`
DeploymentChainID string `yaml:"deployment_chain_id"` // For deployment: use env variable string to override the chain ID.
NetworkUrl string `yaml:"networkUrl"` // Blockchain network provider URL
EnableAuthz bool `yaml:"enable_authz"` // Flag to enable / disable authorization during development
NetworkUrl string `yaml:"network_url"` // Blockchain network provider URL
ConfigChainID string `yaml:"chain_id"` // Blockchain chain ID. Env variable can replace this property.
ConfigEnableAuthz string `yaml:"enable_authz"` // Enable / disable authorization during development. Will be removed when feature is done.
chainID int
enableAuthz bool
}
func (c *EthereumAuthConfig) GetChainID() int {
if c.ConfigChainID != "" {
v := strings.TrimSpace(c.ConfigChainID)
id, err := strconv.Atoi(v)
if err == nil {
c.chainID = id
}
// No need to do this again.
c.ConfigChainID = ""
}
return c.chainID
}
func (c *EthereumAuthConfig) GetEnableAuthZ() bool {
if c.ConfigEnableAuthz != "" {
v := strings.TrimSpace(c.ConfigEnableAuthz)
boolValue, err := strconv.ParseBool(v)
if err == nil {
c.enableAuthz = boolValue
}
// No need to do this again.
c.ConfigEnableAuthz = ""
}
return c.enableAuthz
}
type PublicKeyAuthentication struct {
@ -48,7 +78,7 @@ func (pk *PublicKeyAuthentication) GetPublicKeyRegistrationParams() map[string]i
if pk.Ethereum.Enabled {
p := EthereumAuthParams{
Version: pk.Ethereum.Version,
ChainID: pk.Ethereum.ChainID,
ChainID: pk.Ethereum.GetChainID(),
}
params[authtypes.LoginTypePublicKeyEthereum] = p
}

View file

@ -27,7 +27,7 @@ import (
"github.com/spruceid/siwe-go"
)
const EthereumTestNetworkId = 1337 // Localhost chain ID
const EthereumTestNetworkId int = 31337 // Localhost chain ID
const TestServerName = "localhost"
type EthereumTestWallet struct {

View file

@ -36,7 +36,7 @@ func NewZionAuthorization(
var auth ZionAuthorization
auth.chainId = cfg.PublicKeyAuthentication.Ethereum.ChainID
auth.chainId = cfg.PublicKeyAuthentication.Ethereum.GetChainID()
auth.store = NewStore(rsAPI)
switch auth.chainId {