diff --git a/config/config.go b/config/config.go index 1c2aa9fd..32b09ca7 100644 --- a/config/config.go +++ b/config/config.go @@ -1,7 +1,14 @@ package config import ( + "errors" + "flag" + "os" + "path/filepath" + "time" + "github.com/ilyakaznacheev/cleanenv" + "gopkg.in/yaml.v2" ) var ConsoleConfig *Config @@ -18,14 +25,16 @@ type ( // App -. App struct { - Name string `env-required:"true" yaml:"name" env:"APP_NAME"` - Repo string `env-required:"true" yaml:"repo" env:"APP_REPO"` - Version string `env-required:"true"` - EncryptionKey string `yaml:"encryption_key" env:"APP_ENCRYPTION_KEY"` - JWTKey string `env-required:"true" yaml:"jwtKey" env:"APP_JWT_KEY"` - AuthDisabled bool `yaml:"authDisabled" env:"APP_AUTH_DISABLED"` - AdminUsername string `yaml:"adminUsername" env:"APP_ADMIN_USERNAME"` - AdminPassword string `yaml:"adminPassword" env:"APP_ADMIN_PASSWORD"` + Name string `env-required:"true" yaml:"name" env:"APP_NAME"` + Repo string `env-required:"true" yaml:"repo" env:"APP_REPO"` + Version string `env-required:"true"` + EncryptionKey string `yaml:"encryption_key" env:"APP_ENCRYPTION_KEY"` + JWTKey string `env-required:"true" yaml:"jwtKey" env:"APP_JWT_KEY"` + AuthDisabled bool `yaml:"authDisabled" env:"APP_AUTH_DISABLED"` + AdminUsername string `yaml:"adminUsername" env:"APP_ADMIN_USERNAME"` + AdminPassword string `yaml:"adminPassword" env:"APP_ADMIN_PASSWORD"` + JWTExpiration time.Duration `yaml:"jwtExpiration" env:"APP_JWT_EXPIRATION"` + RedirectionJWTExpiration time.Duration `yaml:"redirectionJWTExpiration" env:"APP_REDIRECTION_JWT_EXPIRATION"` } // HTTP -. @@ -60,13 +69,15 @@ func NewConfig() (*Config, error) { // set defaults ConsoleConfig = &Config{ App: App{ - Name: "console", - Repo: "open-amt-cloud-toolkit/console", - Version: "DEVELOPMENT", - EncryptionKey: "", - JWTKey: "your_secret_jwt_key", - AdminUsername: "standalone", - AdminPassword: "G@ppm0ym", + Name: "console", + Repo: "open-amt-cloud-toolkit/console", + Version: "DEVELOPMENT", + EncryptionKey: "", + JWTKey: "your_secret_jwt_key", + AdminUsername: "standalone", + AdminPassword: "G@ppm0ym", + JWTExpiration: 24 * time.Hour, + RedirectionJWTExpiration: 5 * time.Minute, }, HTTP: HTTP{ Host: "localhost", @@ -87,10 +98,55 @@ func NewConfig() (*Config, error) { }, } - _ = cleanenv.ReadConfig("./config/config.yml", ConsoleConfig) - // its ok to ignore the error here, as we have default values set if the config file is not found + // Define a command line flag for the config path + var configPathFlag string + if flag.Lookup("config") == nil { + flag.StringVar(&configPathFlag, "config", "", "path to config file") + } + + flag.Parse() + + // Determine the config path + var configPath string + if configPathFlag != "" { + configPath = configPathFlag + } else { + ex, err := os.Executable() + if err != nil { + panic(err) + } + + exPath := filepath.Dir(ex) + + configPath = filepath.Join(exPath, "config", "config.yml") + } + + err := cleanenv.ReadConfig(configPath, ConsoleConfig) + + var pathErr *os.PathError + + if errors.As(err, &pathErr) { + // Write config file out to disk + configDir := filepath.Dir(configPath) + if err := os.MkdirAll(configDir, os.ModePerm); err != nil { + return nil, err + } + + file, err := os.Create(configPath) + if err != nil { + return nil, err + } + defer file.Close() + + encoder := yaml.NewEncoder(file) + defer encoder.Close() + + if err := encoder.Encode(ConsoleConfig); err != nil { + return nil, err + } + } - err := cleanenv.ReadEnv(ConsoleConfig) + err = cleanenv.ReadEnv(ConsoleConfig) if err != nil { return nil, err } diff --git a/config/config.yml b/config/config.yml index 135ee890..72327178 100644 --- a/config/config.yml +++ b/config/config.yml @@ -4,6 +4,8 @@ app: jwtKey: "your_secret_jwt_key" adminUsername: "standalone" adminPassword: "G@ppm0ym" + jwtExpiration: 24h + redirectionJWTExpiration: 5m http: host: "localhost" port: "8181" diff --git a/internal/controller/http/v1/devices.go b/internal/controller/http/v1/devices.go index d7937207..0f9542f2 100644 --- a/internal/controller/http/v1/devices.go +++ b/internal/controller/http/v1/devices.go @@ -21,8 +21,6 @@ type deviceRoutes struct { var ErrValidationDevices = dto.NotValidError{Console: consoleerrors.CreateConsoleError("ProfileAPI")} -const fiveMinutes = 5 - func NewDeviceRoutes(handler *gin.RouterGroup, t devices.Feature, l logger.Interface) { r := &deviceRoutes{t, l} @@ -99,7 +97,7 @@ func (dr *deviceRoutes) LoginRedirection(c *gin.Context) { return } // Create JWT token - expirationTime := time.Now().Add(fiveMinutes * time.Minute) + expirationTime := time.Now().Add(config.ConsoleConfig.JWTExpiration) claims := jwt.RegisteredClaims{ ExpiresAt: jwt.NewNumericDate(expirationTime), } diff --git a/internal/controller/http/v1/login.go b/internal/controller/http/v1/login.go index ab1ac75f..ea58fb4e 100644 --- a/internal/controller/http/v1/login.go +++ b/internal/controller/http/v1/login.go @@ -15,8 +15,6 @@ import ( var ErrLogin = consoleerrors.CreateConsoleError("LoginHandler") -const hoursInADay = 24 - type LoginRoute struct { Config *config.Config } @@ -45,7 +43,7 @@ func (lr LoginRoute) Login(c *gin.Context) { } // Create JWT token - expirationTime := time.Now().Add(hoursInADay * time.Hour) + expirationTime := time.Now().Add(config.ConsoleConfig.JWTExpiration) claims := jwt.RegisteredClaims{ ExpiresAt: jwt.NewNumericDate(expirationTime), }