Commit 7690e57b authored by Sietse Ringers's avatar Sietse Ringers
Browse files

feat: stop depending on forked viper functionality for using underscores...

feat: stop depending on forked viper functionality for using underscores instead of dashes in command configuration files
parent 84293023
...@@ -13,6 +13,7 @@ import ( ...@@ -13,6 +13,7 @@ import (
irma "github.com/privacybydesign/irmago" irma "github.com/privacybydesign/irmago"
"github.com/privacybydesign/irmago/server" "github.com/privacybydesign/irmago/server"
"github.com/privacybydesign/irmago/server/keyshare" "github.com/privacybydesign/irmago/server/keyshare"
"github.com/sietseringers/pflag"
"github.com/spf13/cast" "github.com/spf13/cast"
"github.com/sietseringers/cobra" "github.com/sietseringers/cobra"
...@@ -23,56 +24,56 @@ import ( ...@@ -23,56 +24,56 @@ import (
func configureEmail() keyshare.EmailConfiguration { func configureEmail() keyshare.EmailConfiguration {
// If username/password are specified for the email server, build an authentication object. // If username/password are specified for the email server, build an authentication object.
var emailAuth smtp.Auth var emailAuth smtp.Auth
if viper.GetString("email-username") != "" { if viper.GetString("email_username") != "" {
emailAuth = smtp.PlainAuth( emailAuth = smtp.PlainAuth(
"", "",
viper.GetString("email-username"), viper.GetString("email_username"),
viper.GetString("email-password"), viper.GetString("email_password"),
viper.GetString("email-hostname"), viper.GetString("email_hostname"),
) )
} }
return keyshare.EmailConfiguration{ return keyshare.EmailConfiguration{
EmailServer: viper.GetString("email-server"), EmailServer: viper.GetString("email_server"),
EmailAuth: emailAuth, EmailAuth: emailAuth,
EmailFrom: viper.GetString("email-from"), EmailFrom: viper.GetString("email_from"),
DefaultLanguage: viper.GetString("default-language"), DefaultLanguage: viper.GetString("default_language"),
} }
} }
func configureIRMAServer() *server.Configuration { func configureIRMAServer() *server.Configuration {
return &server.Configuration{ return &server.Configuration{
SchemesPath: viper.GetString("schemes-path"), SchemesPath: viper.GetString("schemes_path"),
SchemesAssetsPath: viper.GetString("schemes-assets-path"), SchemesAssetsPath: viper.GetString("schemes_assets_path"),
SchemesUpdateInterval: viper.GetInt("schemes-update"), SchemesUpdateInterval: viper.GetInt("schemes_update"),
DisableSchemesUpdate: viper.GetInt("schemes-update") == 0, DisableSchemesUpdate: viper.GetInt("schemes_update") == 0,
IssuerPrivateKeysPath: viper.GetString("privkeys"), IssuerPrivateKeysPath: viper.GetString("privkeys"),
RevocationDBType: viper.GetString("revocation-db-type"), RevocationDBType: viper.GetString("revocation_db_type"),
RevocationDBConnStr: viper.GetString("revocation-db-str"), RevocationDBConnStr: viper.GetString("revocation_db_str"),
RevocationSettings: irma.RevocationSettings{}, RevocationSettings: irma.RevocationSettings{},
URL: viper.GetString("url"), URL: viper.GetString("url"),
DisableTLS: viper.GetBool("no-tls"), DisableTLS: viper.GetBool("no_tls"),
Email: viper.GetString("email"), Email: viper.GetString("email"),
EnableSSE: viper.GetBool("sse"), EnableSSE: viper.GetBool("sse"),
Verbose: viper.GetInt("verbose"), Verbose: viper.GetInt("verbose"),
Quiet: viper.GetBool("quiet"), Quiet: viper.GetBool("quiet"),
LogJSON: viper.GetBool("log-json"), LogJSON: viper.GetBool("log_json"),
Logger: logger, Logger: logger,
Production: viper.GetBool("production"), Production: viper.GetBool("production"),
JwtIssuer: viper.GetString("jwt-issuer"), JwtIssuer: viper.GetString("jwt_issuer"),
JwtPrivateKey: viper.GetString("jwt-privkey"), JwtPrivateKey: viper.GetString("jwt_privkey"),
JwtPrivateKeyFile: viper.GetString("jwt-privkey-file"), JwtPrivateKeyFile: viper.GetString("jwt_privkey_file"),
AllowUnsignedCallbacks: viper.GetBool("allow-unsigned-callbacks"), AllowUnsignedCallbacks: viper.GetBool("allow_unsigned_callbacks"),
AugmentClientReturnURL: viper.GetBool("augment-client-return-url"), AugmentClientReturnURL: viper.GetBool("augment_client_return_url"),
} }
} }
func configureTLS() *tls.Config { func configureTLS() *tls.Config {
conf, err := server.TLSConf( conf, err := server.TLSConf(
viper.GetString("tls-cert"), viper.GetString("tls_cert"),
viper.GetString("tls-cert-file"), viper.GetString("tls_cert_file"),
viper.GetString("tls-privkey"), viper.GetString("tls_privkey"),
viper.GetString("tls-privkey-file")) viper.GetString("tls_privkey_file"))
if err != nil { if err != nil {
die("", err) die("", err)
} }
...@@ -82,11 +83,18 @@ func configureTLS() *tls.Config { ...@@ -82,11 +83,18 @@ func configureTLS() *tls.Config {
func readConfig(cmd *cobra.Command, name, logname string, configpaths []string, productionDefaults map[string]interface{}) { func readConfig(cmd *cobra.Command, name, logname string, configpaths []string, productionDefaults map[string]interface{}) {
dashReplacer := strings.NewReplacer("-", "_") dashReplacer := strings.NewReplacer("-", "_")
viper.SetEnvKeyReplacer(dashReplacer) viper.SetEnvKeyReplacer(dashReplacer)
viper.SetFileKeyReplacer(dashReplacer)
viper.SetEnvPrefix(strings.ToUpper(name)) viper.SetEnvPrefix(strings.ToUpper(name))
viper.AutomaticEnv() viper.AutomaticEnv()
if err := viper.BindPFlags(cmd.Flags()); err != nil { // bind cmd flags to viper, replacing dashes with underscores
f := cmd.Flags()
normalizeFunc := f.GetNormalizeFunc()
f.SetNormalizeFunc(func(fs *pflag.FlagSet, name string) pflag.NormalizedName {
result := normalizeFunc(fs, name)
name = strings.ReplaceAll(string(result), "-", "_")
return pflag.NormalizedName(name)
})
if err := viper.BindPFlags(f); err != nil {
die("", err) die("", err)
} }
...@@ -117,7 +125,7 @@ func readConfig(cmd *cobra.Command, name, logname string, configpaths []string, ...@@ -117,7 +125,7 @@ func readConfig(cmd *cobra.Command, name, logname string, configpaths []string,
err := viper.ReadInConfig() // Hold error checking until we know how much of it to log err := viper.ReadInConfig() // Hold error checking until we know how much of it to log
// Create our logger instance // Create our logger instance
logger = server.NewLogger(viper.GetInt("verbose"), viper.GetBool("quiet"), viper.GetBool("log-json")) logger = server.NewLogger(viper.GetInt("verbose"), viper.GetBool("quiet"), viper.GetBool("log_json"))
// First log output: hello, development or production mode, log level // First log output: hello, development or production mode, log level
mode := "development" mode := "development"
...@@ -164,7 +172,7 @@ func handleMapOrString(key string, dest interface{}) error { ...@@ -164,7 +172,7 @@ func handleMapOrString(key string, dest interface{}) error {
func handlePermission(typ string) []string { func handlePermission(typ string) []string {
if !viper.IsSet(typ) { if !viper.IsSet(typ) {
if typ == "revoke-perms" || (viper.GetBool("production") && typ == "issue-perms") { if typ == "revoke_perms" || (viper.GetBool("production") && typ == "issue_perms") {
return []string{} return []string{}
} else { } else {
return []string{"*"} return []string{"*"}
......
...@@ -99,24 +99,24 @@ func configureMyirmaServer(cmd *cobra.Command) (*myirmaserver.Configuration, err ...@@ -99,24 +99,24 @@ func configureMyirmaServer(cmd *cobra.Command) (*myirmaserver.Configuration, err
Configuration: configureIRMAServer(), Configuration: configureIRMAServer(),
EmailConfiguration: configureEmail(), EmailConfiguration: configureEmail(),
CORSAllowedOrigins: viper.GetStringSlice("cors-allowed-origins"), CORSAllowedOrigins: viper.GetStringSlice("cors_allowed_origins"),
StaticPath: viper.GetString("static-path"), StaticPath: viper.GetString("static_path"),
StaticPrefix: viper.GetString("static-prefix"), StaticPrefix: viper.GetString("static_prefix"),
DBType: myirmaserver.DBType(viper.GetString("db-type")), DBType: myirmaserver.DBType(viper.GetString("db_type")),
DBConnStr: viper.GetString("db-str"), DBConnStr: viper.GetString("db_str"),
LoginEmailSubjects: viper.GetStringMapString("login-email-subjects"), LoginEmailSubjects: viper.GetStringMapString("login_email_subjects"),
LoginEmailFiles: viper.GetStringMapString("login-email-files"), LoginEmailFiles: viper.GetStringMapString("login_email_files"),
LoginEmailBaseURL: viper.GetStringMapString("login-url"), LoginEmailBaseURL: viper.GetStringMapString("login_url"),
DeleteEmailFiles: viper.GetStringMapString("delete-email-files"), DeleteEmailFiles: viper.GetStringMapString("delete_email_files"),
DeleteEmailSubjects: viper.GetStringMapString("delete-email-subjects"), DeleteEmailSubjects: viper.GetStringMapString("delete_email_subjects"),
DeleteAccountFiles: viper.GetStringMapString("delete-account-files"), DeleteAccountFiles: viper.GetStringMapString("delete_account_files"),
DeleteAccountSubjects: viper.GetStringMapString("delete-account-subjects"), DeleteAccountSubjects: viper.GetStringMapString("delete_account_subjects"),
DeleteDelay: viper.GetInt("delete-delay"), DeleteDelay: viper.GetInt("delete_delay"),
SessionLifetime: viper.GetInt("session-lifetime"), SessionLifetime: viper.GetInt("session_lifetime"),
} }
if conf.Production && conf.DBType != myirmaserver.DBTypePostgres { if conf.Production && conf.DBType != myirmaserver.DBTypePostgres {
...@@ -125,12 +125,12 @@ func configureMyirmaServer(cmd *cobra.Command) (*myirmaserver.Configuration, err ...@@ -125,12 +125,12 @@ func configureMyirmaServer(cmd *cobra.Command) (*myirmaserver.Configuration, err
conf.URL = server.ReplacePortString(viper.GetString("url"), viper.GetInt("port")) conf.URL = server.ReplacePortString(viper.GetString("url"), viper.GetInt("port"))
for _, v := range viper.GetStringSlice("keyshare-attributes") { for _, v := range viper.GetStringSlice("keyshare_attributes") {
conf.KeyshareAttributes = append( conf.KeyshareAttributes = append(
conf.KeyshareAttributes, conf.KeyshareAttributes,
irma.NewAttributeTypeIdentifier(v)) irma.NewAttributeTypeIdentifier(v))
} }
for _, v := range viper.GetStringSlice("email-attributes") { for _, v := range viper.GetStringSlice("email_attributes") {
conf.EmailAttributes = append( conf.EmailAttributes = append(
conf.EmailAttributes, conf.EmailAttributes,
irma.NewAttributeTypeIdentifier(v)) irma.NewAttributeTypeIdentifier(v))
......
...@@ -30,7 +30,7 @@ type stoppableServer interface { ...@@ -30,7 +30,7 @@ type stoppableServer interface {
func runServer(serv stoppableServer, logger *logrus.Logger) { func runServer(serv stoppableServer, logger *logrus.Logger) {
// Determine full listening address. // Determine full listening address.
fullAddr := fmt.Sprintf("%s:%d", viper.GetString("listen-addr"), viper.GetInt("port")) fullAddr := fmt.Sprintf("%s:%d", viper.GetString("listen_addr"), viper.GetInt("port"))
// Load TLS configuration // Load TLS configuration
TLSConfig := configureTLS() TLSConfig := configureTLS()
......
...@@ -98,22 +98,22 @@ func configureKeyshareServer(cmd *cobra.Command) (*keyshareserver.Configuration, ...@@ -98,22 +98,22 @@ func configureKeyshareServer(cmd *cobra.Command) (*keyshareserver.Configuration,
Configuration: configureIRMAServer(), Configuration: configureIRMAServer(),
EmailConfiguration: configureEmail(), EmailConfiguration: configureEmail(),
DBType: keyshareserver.DBType(viper.GetString("db-type")), DBType: keyshareserver.DBType(viper.GetString("db_type")),
DBConnStr: viper.GetString("db-str"), DBConnStr: viper.GetString("db_str"),
JwtKeyID: viper.GetUint32("jwt-privkey-id"), JwtKeyID: viper.GetUint32("jwt_privkey_id"),
JwtPrivateKey: viper.GetString("jwt-privkey"), JwtPrivateKey: viper.GetString("jwt_privkey"),
JwtPrivateKeyFile: viper.GetString("jwt-privkey-file"), JwtPrivateKeyFile: viper.GetString("jwt_privkey_file"),
JwtIssuer: viper.GetString("jwt-issuer"), JwtIssuer: viper.GetString("jwt_issuer"),
JwtPinExpiry: viper.GetInt("jwt-pin-expiry"), JwtPinExpiry: viper.GetInt("jwt_pin_expiry"),
StoragePrimaryKeyFile: viper.GetString("storage-primary-keyfile"), StoragePrimaryKeyFile: viper.GetString("storage_primary_keyfile"),
StorageFallbackKeyFiles: viper.GetStringSlice("storage-fallback-keyfile"), StorageFallbackKeyFiles: viper.GetStringSlice("storage_fallback_keyfile"),
KeyshareAttribute: irma.NewAttributeTypeIdentifier(viper.GetString("keyshare-attribute")), KeyshareAttribute: irma.NewAttributeTypeIdentifier(viper.GetString("keyshare_attribute")),
RegistrationEmailSubjects: viper.GetStringMapString("registration-email-subjects"), RegistrationEmailSubjects: viper.GetStringMapString("registration_email_subjects"),
RegistrationEmailFiles: viper.GetStringMapString("registration-email-files"), RegistrationEmailFiles: viper.GetStringMapString("registration_email_files"),
VerificationURL: viper.GetStringMapString("verification-url"), VerificationURL: viper.GetStringMapString("verification_url"),
} }
if conf.Production && conf.DBType != keyshareserver.DBTypePostgres { if conf.Production && conf.DBType != keyshareserver.DBTypePostgres {
......
...@@ -58,17 +58,17 @@ func configureKeyshareTasks(cmd *cobra.Command) *tasks.Configuration { ...@@ -58,17 +58,17 @@ func configureKeyshareTasks(cmd *cobra.Command) *tasks.Configuration {
return &tasks.Configuration{ return &tasks.Configuration{
EmailConfiguration: configureEmail(), EmailConfiguration: configureEmail(),
DBConnStr: viper.GetString("db-str"), DBConnStr: viper.GetString("db_str"),
ExpiryDelay: viper.GetInt("expiry-delay"), ExpiryDelay: viper.GetInt("expiry_delay"),
DeleteDelay: viper.GetInt("delete-delay"), DeleteDelay: viper.GetInt("delete_delay"),
DeleteExpiredAccountSubjects: viper.GetStringMapString("expired-email-subjects"), DeleteExpiredAccountSubjects: viper.GetStringMapString("expired_email_subjects"),
DeleteExpiredAccountFiles: viper.GetStringMapString("expired-email-files"), DeleteExpiredAccountFiles: viper.GetStringMapString("expired_email_files"),
Verbose: viper.GetInt("verbose"), Verbose: viper.GetInt("verbose"),
Quiet: viper.GetBool("quiet"), Quiet: viper.GetBool("quiet"),
LogJSON: viper.GetBool("log-json"), LogJSON: viper.GetBool("log_json"),
Logger: logger, Logger: logger,
} }
} }
...@@ -157,8 +157,8 @@ func configureServer(cmd *cobra.Command) (*requestorserver.Configuration, error) ...@@ -157,8 +157,8 @@ func configureServer(cmd *cobra.Command) (*requestorserver.Configuration, error)
readConfig(cmd, "irmaserver", "irma server", []string{".", "/etc/irmaserver/", "$HOME/.irmaserver"}, readConfig(cmd, "irmaserver", "irma server", []string{".", "/etc/irmaserver/", "$HOME/.irmaserver"},
map[string]interface{}{ map[string]interface{}{
"no-auth": false, "no_auth": false,
"no-email": false, "no_email": false,
"url": "", "url": "",
}, },
) )
...@@ -167,38 +167,38 @@ func configureServer(cmd *cobra.Command) (*requestorserver.Configuration, error) ...@@ -167,38 +167,38 @@ func configureServer(cmd *cobra.Command) (*requestorserver.Configuration, error)
conf := &requestorserver.Configuration{ conf := &requestorserver.Configuration{
Configuration: configureIRMAServer(), Configuration: configureIRMAServer(),
Permissions: requestorserver.Permissions{ Permissions: requestorserver.Permissions{
Disclosing: handlePermission("disclose-perms"), Disclosing: handlePermission("disclose_perms"),
Signing: handlePermission("sign-perms"), Signing: handlePermission("sign_perms"),
Issuing: handlePermission("issue-perms"), Issuing: handlePermission("issue_perms"),
Revoking: handlePermission("revoke-perms"), Revoking: handlePermission("revoke_perms"),
}, },
SkipPrivateKeysCheck: viper.GetBool("skip-private-keys-check"), SkipPrivateKeysCheck: viper.GetBool("skip_private_keys_check"),
ListenAddress: viper.GetString("listen-addr"), ListenAddress: viper.GetString("listen_addr"),
Port: viper.GetInt("port"), Port: viper.GetInt("port"),
ApiPrefix: viper.GetString("api-prefix"), ApiPrefix: viper.GetString("api_prefix"),
ClientListenAddress: viper.GetString("client-listen-addr"), ClientListenAddress: viper.GetString("client_listen_addr"),
ClientPort: viper.GetInt("client-port"), ClientPort: viper.GetInt("client_port"),
DisableRequestorAuthentication: viper.GetBool("no-auth"), DisableRequestorAuthentication: viper.GetBool("no_auth"),
Requestors: make(map[string]requestorserver.Requestor), Requestors: make(map[string]requestorserver.Requestor),
MaxRequestAge: viper.GetInt("max-request-age"), MaxRequestAge: viper.GetInt("max_request_age"),
StaticPath: viper.GetString("static-path"), StaticPath: viper.GetString("static_path"),
StaticPrefix: viper.GetString("static-prefix"), StaticPrefix: viper.GetString("static_prefix"),
TlsCertificate: viper.GetString("tls-cert"), TlsCertificate: viper.GetString("tls_cert"),
TlsCertificateFile: viper.GetString("tls-cert-file"), TlsCertificateFile: viper.GetString("tls_cert_file"),
TlsPrivateKey: viper.GetString("tls-privkey"), TlsPrivateKey: viper.GetString("tls_privkey"),
TlsPrivateKeyFile: viper.GetString("tls-privkey-file"), TlsPrivateKeyFile: viper.GetString("tls_privkey_file"),
ClientTlsCertificate: viper.GetString("client-tls-cert"), ClientTlsCertificate: viper.GetString("client_tls_cert"),
ClientTlsCertificateFile: viper.GetString("client-tls-cert-file"), ClientTlsCertificateFile: viper.GetString("client_tls_cert_file"),
ClientTlsPrivateKey: viper.GetString("client-tls-privkey"), ClientTlsPrivateKey: viper.GetString("client_tls_privkey"),
ClientTlsPrivateKeyFile: viper.GetString("client-tls-privkey-file"), ClientTlsPrivateKeyFile: viper.GetString("client_tls_privkey_file"),
} }
if conf.Production { if conf.Production {
if !viper.GetBool("no-email") && conf.Email == "" { if !viper.GetBool("no_email") && conf.Email == "" {
return nil, errors.New("In production mode it is required to specify either an email address with the --email flag, or explicitly opting out with --no-email. See help or README for more info.") return nil, errors.New("In production mode it is required to specify either an email address with the --email flag, or explicitly opting out with --no-email. See help or README for more info.")
} }
if viper.GetBool("no-email") && conf.Email != "" { if viper.GetBool("no_email") && conf.Email != "" {
return nil, errors.New("--no-email cannot be combined with --email") return nil, errors.New("--no-email cannot be combined with --email")
} }
} }
...@@ -208,11 +208,11 @@ func configureServer(cmd *cobra.Command) (*requestorserver.Configuration, error) ...@@ -208,11 +208,11 @@ func configureServer(cmd *cobra.Command) (*requestorserver.Configuration, error)
if err = handleMapOrString("requestors", &conf.Requestors); err != nil { if err = handleMapOrString("requestors", &conf.Requestors); err != nil {
return nil, err return nil, err
} }
if err = handleMapOrString("static-sessions", &conf.StaticSessions); err != nil { if err = handleMapOrString("static_sessions", &conf.StaticSessions); err != nil {
return nil, err return nil, err
} }
var m map[string]*irma.RevocationSetting var m map[string]*irma.RevocationSetting
if err = handleMapOrString("revocation-settings", &m); err != nil { if err = handleMapOrString("revocation_settings", &m); err != nil {
return nil, err return nil, err
} }
for i, s := range m { for i, s := range m {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment