Refactor configuration

Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Sacha Al Himdani
2025-10-23 09:33:39 +02:00
parent b0f60d8de5
commit 4a476029ae
6 changed files with 91 additions and 38 deletions

View File

@@ -55,16 +55,16 @@ probod:
secret-access-key: "thisisnotasecret" secret-access-key: "thisisnotasecret"
endpoint: "http://127.0.0.1:9000" endpoint: "http://127.0.0.1:9000"
mailer: notifications:
sender-name: "Probo" mailer:
sender-email: "no-reply@notification.getprobo.com" sender-name: "Probo"
smtp: sender-email: "no-reply@notification.getprobo.com"
addr: "localhost:1025" smtp:
tls-required: false addr: "localhost:1025"
tls-required: false
slack: mailer-interval: 60
sender-interval: 60 slack:
signing-secret: "this-is-not-a-secret-for-slack-signing" sender-interval: 60
openai: openai:
api-key: "thisisnotasecret" api-key: "thisisnotasecret"
@@ -94,3 +94,5 @@ probod:
- "chat:write" - "chat:write"
- "channels:join" - "channels:join"
- "incoming-webhook" - "incoming-webhook"
settings:
signing-secret: "this-is-not-a-secret-for-slack-signing"

View File

@@ -32,9 +32,10 @@ import (
type ( type (
Mailer struct { Mailer struct {
pg *pg.Client pg *pg.Client
l *log.Logger l *log.Logger
cfg Config cfg Config
interval time.Duration
} }
Config struct { Config struct {
@@ -45,6 +46,7 @@ type (
User string User string
Password string Password string
TLSRequired bool TLSRequired bool
Interval time.Duration
} }
) )
@@ -53,7 +55,8 @@ func NewMailer(pg *pg.Client, l *log.Logger, cfg Config) *Mailer {
if cfg.Timeout == 0 { if cfg.Timeout == 0 {
cfg.Timeout = 10 * time.Second cfg.Timeout = 10 * time.Second
} }
return &Mailer{pg: pg, l: l, cfg: cfg}
return &Mailer{pg: pg, l: l, cfg: cfg, interval: cfg.Interval}
} }
func (m *Mailer) Run(ctx context.Context) error { func (m *Mailer) Run(ctx context.Context) error {
@@ -61,7 +64,7 @@ LOOP:
select { select {
case <-ctx.Done(): case <-ctx.Done():
return ctx.Err() return ctx.Err()
case <-time.After(60 * time.Second): case <-time.After(m.interval):
ctx := context.Background() ctx := context.Background()
if err := m.batchSendEmails(ctx); err != nil { if err := m.batchSendEmails(ctx); err != nil {
m.l.ErrorCtx(ctx, "cannot send email", log.Error(err)) m.l.ErrorCtx(ctx, "cannot send email", log.Error(err))

View File

@@ -15,6 +15,7 @@
package probod package probod
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"strings" "strings"
@@ -27,6 +28,7 @@ type (
Provider string `json:"provider"` Provider string `json:"provider"`
Protocol connector.ProtocolType `json:"protocol"` Protocol connector.ProtocolType `json:"protocol"`
Config connector.Connector `json:"-"` Config connector.Connector `json:"-"`
Settings any `json:"-"`
} }
connectorConfigOAuth2 struct { connectorConfigOAuth2 struct {
@@ -39,24 +41,46 @@ type (
} }
) )
func (c *config) GetSlackSigningSecret() string {
for _, conn := range c.Connectors {
if conn.Provider == "SLACK" {
if settings, ok := conn.Settings.(map[string]any); ok {
if signingSecret, ok := settings["signing-secret"].(string); ok {
return signingSecret
}
}
}
}
return ""
}
func (c *connectorConfig) UnmarshalJSON(data []byte) error { func (c *connectorConfig) UnmarshalJSON(data []byte) error {
var tmp struct { var tmp struct {
Provider string `json:"provider"` Provider string `json:"provider"`
Protocol string `json:"protocol"` Protocol string `json:"protocol"`
RawConfig json.RawMessage `json:"config"` RawConfig json.RawMessage `json:"config"`
Settings json.RawMessage `json:"settings"`
} }
if err := json.Unmarshal(data, &tmp); err != nil { if err := json.NewDecoder(bytes.NewReader(data)).Decode(&tmp); err != nil {
return fmt.Errorf("cannot unmarshal connector config: %w", err) return fmt.Errorf("cannot unmarshal connector config: %w", err)
} }
c.Provider = strings.ToUpper(tmp.Provider) c.Provider = strings.ToUpper(tmp.Provider)
c.Protocol = connector.ProtocolType(strings.ToUpper(tmp.Protocol)) c.Protocol = connector.ProtocolType(strings.ToUpper(tmp.Protocol))
if len(tmp.Settings) > 0 {
var settings map[string]any
if err := json.NewDecoder(bytes.NewReader(tmp.Settings)).Decode(&settings); err != nil {
return fmt.Errorf("cannot unmarshal settings: %w", err)
}
c.Settings = settings
}
switch c.Protocol { switch c.Protocol {
case connector.ProtocolOAuth2: case connector.ProtocolOAuth2:
var config connectorConfigOAuth2 var config connectorConfigOAuth2
if err := json.Unmarshal(tmp.RawConfig, &config); err != nil { if err := json.NewDecoder(bytes.NewReader(tmp.RawConfig)).Decode(&config); err != nil {
return fmt.Errorf("cannot unmarshal oauth2 connector config: %w", err) return fmt.Errorf("cannot unmarshal oauth2 connector config: %w", err)
} }

View File

@@ -16,9 +16,10 @@ package probod
type ( type (
mailerConfig struct { mailerConfig struct {
SenderName string `json:"sender-name"` MailerInterval int `json:"mailer-interval"`
SenderEmail string `json:"sender-email"` SenderName string `json:"sender-name"`
SMTP smtpConfig `json:"smtp"` SenderEmail string `json:"sender-email"`
SMTP smtpConfig `json:"smtp"`
} }
smtpConfig struct { smtpConfig struct {

View File

@@ -0,0 +1,20 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package probod
type notificationsConfig struct {
Mailer mailerConfig `json:"mailer"`
Slack slackConfig `json:"slack"`
}

View File

@@ -72,8 +72,7 @@ type (
TrustAuth trustAuthConfig `json:"trust-auth"` TrustAuth trustAuthConfig `json:"trust-auth"`
TrustCenter trustCenterConfig `json:"trust-center"` TrustCenter trustCenterConfig `json:"trust-center"`
AWS awsConfig `json:"aws"` AWS awsConfig `json:"aws"`
Mailer mailerConfig `json:"mailer"` Notifications notificationsConfig `json:"notifications"`
Slack slackConfig `json:"slack"`
Connectors []connectorConfig `json:"connectors"` Connectors []connectorConfig `json:"connectors"`
OpenAI openaiConfig `json:"openai"` OpenAI openaiConfig `json:"openai"`
ChromeDPAddr string `json:"chrome-dp-addr"` ChromeDPAddr string `json:"chrome-dp-addr"`
@@ -138,15 +137,18 @@ func New() *Implm {
Region: "us-east-1", Region: "us-east-1",
Bucket: "probod", Bucket: "probod",
}, },
Mailer: mailerConfig{ Notifications: notificationsConfig{
SenderEmail: "no-reply@notification.getprobo.com", Mailer: mailerConfig{
SenderName: "Probo", MailerInterval: 60,
SMTP: smtpConfig{ SenderEmail: "no-reply@notification.getprobo.com",
Addr: "localhost:1025", SenderName: "Probo",
SMTP: smtpConfig{
Addr: "localhost:1025",
},
},
Slack: slackConfig{
SenderInterval: 60,
}, },
},
Slack: slackConfig{
SenderInterval: 60,
}, },
CustomDomains: customDomainsConfig{ CustomDomains: customDomainsConfig{
RenewalInterval: 3600, RenewalInterval: 3600,
@@ -346,7 +348,7 @@ func (impl *Implm) Run(
impl.cfg.Hostname, impl.cfg.Hostname,
impl.cfg.EncryptionKey, impl.cfg.EncryptionKey,
impl.cfg.TrustAuth.TokenSecret, impl.cfg.TrustAuth.TokenSecret,
impl.cfg.Slack.SigningSecret, impl.cfg.GetSlackSigningSecret(),
authService, authService,
html2pdfConverter, html2pdfConverter,
fileManagerService, fileManagerService,
@@ -405,13 +407,14 @@ func (impl *Implm) Run(
mailerCtx, stopMailer := context.WithCancel(context.Background()) mailerCtx, stopMailer := context.WithCancel(context.Background())
mailer := mailer.NewMailer(pgClient, l, mailer.Config{ mailer := mailer.NewMailer(pgClient, l, mailer.Config{
SenderEmail: impl.cfg.Mailer.SenderEmail, SenderEmail: impl.cfg.Notifications.Mailer.SenderEmail,
SenderName: impl.cfg.Mailer.SenderName, SenderName: impl.cfg.Notifications.Mailer.SenderName,
Addr: impl.cfg.Mailer.SMTP.Addr, Addr: impl.cfg.Notifications.Mailer.SMTP.Addr,
User: impl.cfg.Mailer.SMTP.User, User: impl.cfg.Notifications.Mailer.SMTP.User,
Password: impl.cfg.Mailer.SMTP.Password, Password: impl.cfg.Notifications.Mailer.SMTP.Password,
TLSRequired: impl.cfg.Mailer.SMTP.TLSRequired, TLSRequired: impl.cfg.Notifications.Mailer.SMTP.TLSRequired,
Timeout: time.Second * 10, Timeout: time.Second * 10,
Interval: time.Duration(impl.cfg.Notifications.Mailer.MailerInterval) * time.Second,
}) })
wg.Go( wg.Go(
func() { func() {
@@ -423,7 +426,7 @@ func (impl *Implm) Run(
slackSenderCtx, stopSlackSender := context.WithCancel(context.Background()) slackSenderCtx, stopSlackSender := context.WithCancel(context.Background())
slackSender := slack.NewSender(pgClient, l.Named("slack-sender"), impl.cfg.EncryptionKey, slack.Config{ slackSender := slack.NewSender(pgClient, l.Named("slack-sender"), impl.cfg.EncryptionKey, slack.Config{
Interval: time.Duration(impl.cfg.Slack.SenderInterval) * time.Second, Interval: time.Duration(impl.cfg.Notifications.Slack.SenderInterval) * time.Second,
}) })
wg.Go( wg.Go(
func() { func() {