2020-08-20 12:51:02 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2020-08-20 13:24:52 -04:00
|
|
|
type LdapConfig struct {
|
2020-08-20 12:51:02 -04:00
|
|
|
Url string
|
|
|
|
AdminUser string
|
|
|
|
UserAttr string
|
|
|
|
UserOu string
|
|
|
|
LdapDc string
|
2020-09-17 16:19:05 -04:00
|
|
|
LdapPass string
|
2020-08-20 13:24:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
2020-09-17 16:19:05 -04:00
|
|
|
Ldap *LdapConfig
|
|
|
|
Secret string
|
|
|
|
TplPath string
|
|
|
|
Tls bool
|
|
|
|
Key string
|
|
|
|
Cert string
|
|
|
|
Port string
|
2020-08-20 12:51:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func validateConfigEntry(entry string, name string) bool {
|
|
|
|
if entry == "" {
|
|
|
|
log.Printf("Error: %v unset", name)
|
2020-08-20 13:06:44 -04:00
|
|
|
return true
|
2020-08-20 12:51:02 -04:00
|
|
|
}
|
2020-08-20 13:06:44 -04:00
|
|
|
return false
|
2020-08-20 12:51:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func LoadConfig() (*Config, error) {
|
2020-08-20 13:03:43 -04:00
|
|
|
viper.SetConfigName("guildgate")
|
2020-08-20 12:51:02 -04:00
|
|
|
viper.AddConfigPath("/etc/")
|
|
|
|
viper.AddConfigPath(".")
|
|
|
|
err := viper.ReadInConfig()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("FATAL: Fatal error reading config file: %v \n", err)
|
|
|
|
}
|
|
|
|
viper.SetConfigType("yaml")
|
|
|
|
c := &Config{}
|
2020-08-20 13:24:52 -04:00
|
|
|
l := &LdapConfig{}
|
|
|
|
viper.SetDefault("port", "8080")
|
2020-08-23 17:53:48 -04:00
|
|
|
viper.SetDefault("secret", "")
|
|
|
|
viper.SetDefault("Tls", false)
|
2020-08-20 12:51:02 -04:00
|
|
|
//Load configs
|
2020-08-20 13:24:52 -04:00
|
|
|
l.Url = viper.GetString("ldapUrl")
|
|
|
|
l.AdminUser = viper.GetString("adminUser")
|
|
|
|
l.UserAttr = viper.GetString("userAttr")
|
|
|
|
l.UserOu = viper.GetString("userOu")
|
|
|
|
l.LdapDc = viper.GetString("ldapDc")
|
2020-09-17 16:19:05 -04:00
|
|
|
l.LdapPass = viper.GetString("ldapPass")
|
2020-08-20 12:51:02 -04:00
|
|
|
c.Secret = viper.GetString("secret")
|
2020-08-20 13:24:52 -04:00
|
|
|
c.Tls = viper.GetBool("tls")
|
|
|
|
c.Port = viper.GetString("port")
|
2020-08-23 17:53:48 -04:00
|
|
|
c.Key = viper.GetString("tls_key")
|
|
|
|
c.Cert = viper.GetString("tls_cert")
|
2020-09-17 16:19:05 -04:00
|
|
|
c.TplPath = viper.GetString("templates_path")
|
2020-08-20 12:51:02 -04:00
|
|
|
|
|
|
|
//Validate configs
|
2020-08-20 13:24:52 -04:00
|
|
|
if validateConfigEntry(l.Url, "ldapUrl") || validateConfigEntry(l.AdminUser, "adminUser") || validateConfigEntry(l.UserOu, "userOu") || validateConfigEntry(l.LdapDc, "ldapDc") || validateConfigEntry(l.UserAttr, "userAttr") {
|
2020-08-20 12:51:02 -04:00
|
|
|
log.Fatalf("FATAL: Error in config file, bailing")
|
|
|
|
}
|
2020-08-20 13:24:52 -04:00
|
|
|
c.Ldap = l
|
2020-08-20 12:51:02 -04:00
|
|
|
return c, nil
|
|
|
|
}
|