Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat: added simple config validation

=18612b93c0c4
diff --git a/backend-go/internal/config/config.go b/backend-go/internal/config/config.go
index 85ef46c3b3..e923174dfc 100644
--- a/backend-go/internal/config/config.go
+++ b/backend-go/internal/config/config.go
@@ -1,6 +1,7 @@
package config
import (
+ "errors"
"fmt"
"log/slog"
"os"
@@ -12,22 +13,65 @@ import (
"github.com/knadh/koanf/v2"
)
-type AppEnv string
+type NodeEnv string
const (
- Development AppEnv = "development"
- Production AppEnv = "production"
+ NodeEnvDevelopment NodeEnv = "development"
+ NodeEnvTest NodeEnv = "test"
+ NodeEnvProduction NodeEnv = "production"
)
type Config struct {
- AppEnv AppEnv `koanf:"app_env"`
- HTTPHost string `koanf:"http_host"`
- HTTPPort int `koanf:"http_port"`
- LogLevel string `koanf:"log_level"`
+ // Server
+ NodeEnv NodeEnv `koanf:"NODE_ENV"`
+ Port int `koanf:"PORT"`
+ Host string `koanf:"HOST"`
+
+ // Logging
+ LogLevel string `koanf:"LOG_LEVEL"`
+
+ // Database
+ DBConnectionURI string `koanf:"DB_CONNECTION_URI"`
+ DBRootCert string `koanf:"DB_ROOT_CERT"`
+ DBHost string `koanf:"DB_HOST"`
+ DBPort string `koanf:"DB_PORT"`
+ DBUser string `koanf:"DB_USER"`
+ DBPassword string `koanf:"DB_PASSWORD"`
+ DBName string `koanf:"DB_NAME"`
+
+ // Redis
+ RedisURL string `koanf:"REDIS_URL"`
+
+ // Encryption
+ EncryptionKey string `koanf:"ENCRYPTION_KEY"`
+ RootEncryptionKey string `koanf:"ROOT_ENCRYPTION_KEY"`
+
+ // Auth
+ AuthSecret string `koanf:"AUTH_SECRET"`
+
+ // Site
+ SiteURL string `koanf:"SITE_URL"`
+ HTTPSEnabled bool `koanf:"HTTPS_ENABLED"`
+
+ // Telemetry
+ TelemetryEnabled bool `koanf:"TELEMETRY_ENABLED"`
+
+ // SMTP
+ SMTPHost string `koanf:"SMTP_HOST"`
+ SMTPPort int `koanf:"SMTP_PORT"`
+ SMTPUsername string `koanf:"SMTP_USERNAME"`
+ SMTPPassword string `koanf:"SMTP_PASSWORD"`
+
+ // Derived (not from env)
+ IsCloud bool
+ IsSmtpConfigured bool
+ IsRedisConfigured bool
+ IsDevelopmentMode bool
+ IsProductionMode bool
}
func (c *Config) Addr() string {
- return fmt.Sprintf("%s:%d", c.HTTPHost, c.HTTPPort)
+ return fmt.Sprintf("%s:%d", c.Host, c.Port)
}
func (c *Config) SlogLevel() slog.Level {
@@ -44,18 +88,20 @@ func (c *Config) SlogLevel() slog.Level {
}
func LoadConfig() (*Config, error) {
- // No delimiter — all keys are flat (e.g. "http_port", not nested).
k := koanf.New("")
- // Set defaults.
- k.Set("app_env", string(Development))
- k.Set("http_host", "0.0.0.0")
- k.Set("http_port", 8080)
- k.Set("log_level", "info")
+ // Defaults matching the Node.js backend.
+ k.Set("NODE_ENV", string(NodeEnvProduction))
+ k.Set("PORT", 4000)
+ k.Set("HOST", "localhost")
+ k.Set("LOG_LEVEL", "info")
+ k.Set("DB_PORT", "5432")
+ k.Set("SMTP_PORT", 587)
+ k.Set("TELEMETRY_ENABLED", true)
// In non-production, load .env file if it exists.
- appEnv := os.Getenv("INFISICAL_APP_ENV")
- if appEnv != string(Production) {
+ nodeEnv := os.Getenv("NODE_ENV")
+ if nodeEnv != string(NodeEnvProduction) {
if err := k.Load(file.Provider(".env"), dotenv.Parser()); err != nil {
if !os.IsNotExist(err) {
slog.Debug("could not load .env file", "error", err)
@@ -63,8 +109,7 @@ func LoadConfig() (*Config, error) {
}
}
- // Load environment variables with INFISICAL_ prefix.
- // INFISICAL_HTTP_PORT -> http_port
+ // Load environment variables (no prefix, matching Node.js backend).
err := k.Load(envprovider.Provider("", envprovider.Opt{}), nil)
if err != nil {
return nil, fmt.Errorf("loading env vars: %w", err)
@@ -75,5 +120,52 @@ func LoadConfig() (*Config, error) {
return nil, fmt.Errorf("unmarshalling config: %w", err)
}
+ // Validate required fields.
+ if err := cfg.validate(); err != nil {
+ return nil, fmt.Errorf("config validation: %w", err)
+ }
+
+ // Compute derived fields.
+ cfg.IsDevelopmentMode = cfg.NodeEnv == NodeEnvDevelopment
+ cfg.IsProductionMode = cfg.NodeEnv == NodeEnvProduction
+ cfg.IsRedisConfigured = cfg.RedisURL != ""
+ cfg.IsSmtpConfigured = cfg.SMTPHost != ""
+
return &cfg, nil
}
+
+func (c *Config) validate() error {
+ var errs []error
+
+ // NODE_ENV must be a known value.
+ switch c.NodeEnv {
+ case NodeEnvDevelopment, NodeEnvTest, NodeEnvProduction:
+ default:
+ errs = append(errs, fmt.Errorf("NODE_ENV must be one of: development, test, production (got %q)", c.NodeEnv))
+ }
+
+ if c.Port <= 0 || c.Port > 65535 {
+ errs = append(errs, fmt.Errorf("PORT must be between 1 and 65535 (got %d)", c.Port))
+ }
+
+ // DB_CONNECTION_URI is required unless individual DB fields are all set.
+ if c.DBConnectionURI == "" {
+ if c.DBHost == "" || c.DBUser == "" || c.DBName == "" {
+ errs = append(errs, fmt.Errorf("DB_CONNECTION_URI is required (or set DB_HOST, DB_USER, and DB_NAME)"))
+ }
+ }
+
+ if c.RedisURL == "" {
+ errs = append(errs, fmt.Errorf("REDIS_URL is required"))
+ }
+
+ if c.EncryptionKey == "" && c.RootEncryptionKey == "" {
+ errs = append(errs, fmt.Errorf("either ENCRYPTION_KEY or ROOT_ENCRYPTION_KEY is required"))
+ }
+
+ if c.AuthSecret == "" {
+ errs = append(errs, fmt.Errorf("AUTH_SECRET is required"))
+ }
+
+ return errors.Join(errs...)
+}