Skip to content

Commit

Permalink
config: reorganize some fields and add dry_run option
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Sep 14, 2024
1 parent dc63a4e commit 28ca69d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 44 deletions.
22 changes: 11 additions & 11 deletions cmd/meowlnir/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ type Meowlnir struct {
func (m *Meowlnir) Init(configPath string, noSaveConfig bool) {
var err error
m.Config = loadConfig(configPath, noSaveConfig)
if strings.HasPrefix(m.Config.Appservice.ManagementSecret, "sha256:") {
if strings.HasPrefix(m.Config.Meowlnir.ManagementSecret, "sha256:") {
var decoded []byte
decoded, err = hex.DecodeString(strings.TrimPrefix(m.Config.Appservice.ManagementSecret, "sha256:"))
decoded, err = hex.DecodeString(strings.TrimPrefix(m.Config.Meowlnir.ManagementSecret, "sha256:"))
if err != nil {
m.Log.WithLevel(zerolog.FatalLevel).Err(err).Msg("Failed to decode management secret hash")
os.Exit(10)
Expand All @@ -73,7 +73,7 @@ func (m *Meowlnir) Init(configPath string, noSaveConfig bool) {
}
m.ManagementSecret = [32]byte(decoded)
} else {
m.ManagementSecret = sha256.Sum256([]byte(m.Config.Appservice.ManagementSecret))
m.ManagementSecret = sha256.Sum256([]byte(m.Config.Meowlnir.ManagementSecret))
}

m.Log, err = m.Config.Logging.Compile()
Expand Down Expand Up @@ -108,10 +108,10 @@ func (m *Meowlnir) Init(configPath string, noSaveConfig bool) {
m.Log.Debug().Msg("Preparing Matrix client")
m.AS, err = appservice.CreateFull(appservice.CreateOpts{
Registration: &appservice.Registration{
ID: m.Config.Appservice.ID,
URL: m.Config.Server.Address,
AppToken: m.Config.Appservice.ASToken,
ServerToken: m.Config.Appservice.HSToken,
ID: m.Config.Meowlnir.ID,
URL: m.Config.Meowlnir.Address,
AppToken: m.Config.Meowlnir.ASToken,
ServerToken: m.Config.Meowlnir.HSToken,
RateLimited: ptr.Ptr(false),
SoruEphemeralEvents: true,
EphemeralEvents: true,
Expand All @@ -120,8 +120,8 @@ func (m *Meowlnir) Init(configPath string, noSaveConfig bool) {
HomeserverDomain: m.Config.Homeserver.Domain,
HomeserverURL: m.Config.Homeserver.Address,
HostConfig: appservice.HostConfig{
Hostname: m.Config.Server.Hostname,
Port: m.Config.Server.Port,
Hostname: m.Config.Meowlnir.Hostname,
Port: m.Config.Meowlnir.Port,
},
})
if err != nil {
Expand Down Expand Up @@ -168,7 +168,7 @@ func (m *Meowlnir) initBot(ctx context.Context, db *database.Bot) *bot.Bot {
intent := m.AS.Intent(id.NewUserID(db.Username, m.AS.HomeserverDomain))
wrapped := bot.NewBot(
db, intent, m.Log.With().Str("bot", db.Username).Logger(),
m.DB, m.EventProcessor, m.CryptoStoreDB, m.Config.Appservice.PickleKey,
m.DB, m.EventProcessor, m.CryptoStoreDB, m.Config.Meowlnir.PickleKey,
)
wrapped.Init(ctx)
wrapped.CryptoHelper.CustomPostDecrypt = m.HandleMessage
Expand All @@ -181,7 +181,7 @@ func (m *Meowlnir) initBot(ctx context.Context, db *database.Bot) *bot.Bot {
}
for _, roomID := range managementRooms {
m.EvaluatorByManagementRoom[roomID] = policyeval.NewPolicyEvaluator(
wrapped, m.PolicyStore, roomID, m.DB, m.SynapseDB, m.claimProtectedRoom,
wrapped, m.PolicyStore, roomID, m.DB, m.SynapseDB, m.claimProtectedRoom, m.Config.Meowlnir.DryRun,
)
}
return wrapped
Expand Down
20 changes: 9 additions & 11 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,23 @@ type HomeserverConfig struct {
Domain string `yaml:"domain"`
}

type AppserviceConfig struct {
ID string `yaml:"id"`
ASToken string `yaml:"as_token"`
HSToken string `yaml:"hs_token"`
PickleKey string `yaml:"pickle_key"`
type MeowlnirConfig struct {
ID string `yaml:"id"`
ASToken string `yaml:"as_token"`
HSToken string `yaml:"hs_token"`

ManagementSecret string `yaml:"management_secret"`
}

type ServerConfig struct {
Address string `yaml:"address"`
Hostname string `yaml:"hostname"`
Port uint16 `yaml:"port"`

PickleKey string `yaml:"pickle_key"`
ManagementSecret string `yaml:"management_secret"`
DryRun bool `yaml:"dry_run"`
}

type Config struct {
Homeserver HomeserverConfig `yaml:"homeserver"`
Appservice AppserviceConfig `yaml:"appservice"`
Server ServerConfig `yaml:"server"`
Meowlnir MeowlnirConfig `yaml:"meowlnir"`
Database dbutil.Config `yaml:"database"`
SynapseDB dbutil.Config `yaml:"synapse_db"`
Logging zeroconfig.Config `yaml:"logging"`
Expand Down
22 changes: 12 additions & 10 deletions config/example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,30 @@ homeserver:
# The server name of the homeserver.
domain: example.com

# Appservice settings
appservice:
# Meowlnir server settings
meowlnir:
# The unique ID for the appservice.
id: meowlnir
# Set to generate to generate random tokens.
as_token: generate
hs_token: generate
# Pickle key used for encrypting encryption keys.
# If set to generate, a random key will be generated.
pickle_key: generate
# Management secret used for the management API. If set to generate, a random secret will be generated.
# If prefixed with sha256:, the rest of the string will be hex-decoded and used as the hash of the secret.
management_secret: generate

# Configuration for Meowlnir's HTTP server. This is used for both appservice transactions and external traffic.
server:
# The address that the homeserver can use to connect to Meowlnir.
address: http://localhost:29339
# The hostname and port where Meowlnir should listen
hostname: 0.0.0.0
port: 29339

# Pickle key used for encrypting encryption keys.
# If set to generate, a random key will be generated.
pickle_key: generate
# Management secret used for the management API. If set to generate, a random secret will be generated.
# If prefixed with sha256:, the rest of the string will be hex-decoded and used as the hash of the secret.
management_secret: generate
# If dry run is set to true, meowlnir won't take any actual actions,
# but will do everything else as if it was going to take actions.
dry_run: false

# Database config for meowlnir itself.
database:
# The database type. "sqlite3-fk-wal" and "postgres" are supported.
Expand Down
23 changes: 12 additions & 11 deletions config/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ func upgradeConfig(helper up.Helper) {
helper.Copy(up.Str, "homeserver", "address")
helper.Copy(up.Str, "homeserver", "domain")

helper.Copy(up.Str, "appservice", "id")
generateOrCopy(helper, "appservice", "as_token")
generateOrCopy(helper, "appservice", "hs_token")
generateOrCopy(helper, "appservice", "pickle_key")
generateOrCopy(helper, "appservice", "management_secret")

helper.Copy(up.Str, "server", "address")
helper.Copy(up.Str, "server", "hostname")
helper.Copy(up.Int, "server", "port")
helper.Copy(up.Str, "meowlnir", "id")
generateOrCopy(helper, "meowlnir", "as_token")
generateOrCopy(helper, "meowlnir", "hs_token")
helper.Copy(up.Str, "meowlnir", "address")
helper.Copy(up.Str, "meowlnir", "hostname")
helper.Copy(up.Int, "meowlnir", "port")
generateOrCopy(helper, "meowlnir", "pickle_key")
generateOrCopy(helper, "meowlnir", "management_secret")
helper.Copy(up.Bool, "meowlnir", "dry_run")

helper.Copy(up.Str, "database", "type")
helper.Copy(up.Str, "database", "uri")
Expand All @@ -51,8 +51,9 @@ func upgradeConfig(helper up.Helper) {
}

var SpacedBlocks = [][]string{
{"appservice"},
{"server"},
{"meowlnir"},
{"meowlnir", "address"},
{"meowlnir", "pickle_key"},
{"database"},
{"synapse_db"},
{"logging"},
Expand Down
3 changes: 2 additions & 1 deletion policyeval/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func NewPolicyEvaluator(
db *database.Database,
synapseDB *synapsedb.SynapseDB,
claimProtected func(roomID id.RoomID, eval *PolicyEvaluator, claim bool) *PolicyEvaluator,
dryRun bool,
) *PolicyEvaluator {
pe := &PolicyEvaluator{
Bot: bot,
Expand All @@ -63,7 +64,7 @@ func NewPolicyEvaluator(
wantToProtect: make(map[id.RoomID]struct{}),
claimProtected: claimProtected,

DryRun: true,
DryRun: dryRun,
}
return pe
}
Expand Down

0 comments on commit 28ca69d

Please sign in to comment.