mirror of
https://github.com/thomiceli/opengist.git
synced 2025-06-20 08:48:00 +02:00
Allow to define secret key & move the secret key file to parent directory (#358)
This commit is contained in:
@ -27,6 +27,8 @@ var SecretKey []byte
|
||||
// Not using nested structs because the library
|
||||
// doesn't support dot notation in this case sadly
|
||||
type config struct {
|
||||
SecretKey string `yaml:"secret-key" env:"OG_SECRET_KEY"`
|
||||
|
||||
LogLevel string `yaml:"log-level" env:"OG_LOG_LEVEL"`
|
||||
LogOutput string `yaml:"log-output" env:"OG_LOG_OUTPUT"`
|
||||
ExternalUrl string `yaml:"external-url" env:"OG_EXTERNAL_URL"`
|
||||
@ -82,6 +84,8 @@ type StaticLink struct {
|
||||
func configWithDefaults() (*config, error) {
|
||||
c := &config{}
|
||||
|
||||
c.SecretKey = ""
|
||||
|
||||
c.LogLevel = "warn"
|
||||
c.LogOutput = "stdout,file"
|
||||
c.OpengistHome = ""
|
||||
@ -138,7 +142,9 @@ func InitConfig(configPath string, out io.Writer) error {
|
||||
|
||||
C = c
|
||||
|
||||
// SecretKey = utils.GenerateSecretKey(filepath.Join(GetHomeDir(), "opengist-secret.key"))
|
||||
if err = migrateConfig(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = os.Setenv("OG_OPENGIST_HOME_INTERNAL", GetHomeDir()); err != nil {
|
||||
return err
|
||||
@ -235,6 +241,15 @@ func GetHomeDir() string {
|
||||
return filepath.Clean(absolutePath)
|
||||
}
|
||||
|
||||
func SetupSecretKey() {
|
||||
if C.SecretKey == "" {
|
||||
path := filepath.Join(GetHomeDir(), "opengist-secret.key")
|
||||
SecretKey, _ = utils.GenerateSecretKey(path)
|
||||
} else {
|
||||
SecretKey = []byte(C.SecretKey)
|
||||
}
|
||||
}
|
||||
|
||||
func loadConfigFromYaml(c *config, configPath string, out io.Writer) error {
|
||||
if configPath != "" {
|
||||
absolutePath, _ := filepath.Abs(configPath)
|
||||
|
42
internal/config/migrate.go
Normal file
42
internal/config/migrate.go
Normal file
@ -0,0 +1,42 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// auto migration for newer versions of Opengist
|
||||
func migrateConfig() error {
|
||||
configMigrations := []struct {
|
||||
Version string
|
||||
Func func() error
|
||||
}{
|
||||
{"1.8.0", v1_8_0},
|
||||
}
|
||||
|
||||
for _, fn := range configMigrations {
|
||||
err := fn.Func()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func v1_8_0() error {
|
||||
homeDir := GetHomeDir()
|
||||
moveFile(filepath.Join(filepath.Join(homeDir, "sessions"), "session-auth.key"), filepath.Join(homeDir, "opengist-secret.key"))
|
||||
return nil
|
||||
}
|
||||
|
||||
func moveFile(oldPath, newPath string) {
|
||||
if _, err := os.Stat(oldPath); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err := os.Rename(oldPath, newPath); err == nil {
|
||||
fmt.Printf("Automatically moved %s to %s\n", oldPath, newPath)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user