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 (
irma "github.com/privacybydesign/irmago"
"github.com/privacybydesign/irmago/server"
"github.com/privacybydesign/irmago/server/keyshare"
"github.com/sietseringers/pflag"
"github.com/spf13/cast"
"github.com/sietseringers/cobra"
......@@ -23,56 +24,56 @@ import (
func configureEmail() keyshare.EmailConfiguration {
// If username/password are specified for the email server, build an authentication object.
var emailAuth smtp.Auth
if viper.GetString("email-username") != "" {
if viper.GetString("email_username") != "" {
emailAuth = smtp.PlainAuth(
"",
viper.GetString("email-username"),
viper.GetString("email-password"),
viper.GetString("email-hostname"),
viper.GetString("email_username"),
viper.GetString("email_password"),
viper.GetString("email_hostname"),
)
}
return keyshare.EmailConfiguration{
EmailServer: viper.GetString("email-server"),
EmailServer: viper.GetString("email_server"),
EmailAuth: emailAuth,
EmailFrom: viper.GetString("email-from"),
DefaultLanguage: viper.GetString("default-language"),
EmailFrom: viper.GetString("email_from"),
DefaultLanguage: viper.GetString("default_language"),
}
}
func configureIRMAServer() *server.Configuration {
return &server.Configuration{
SchemesPath: viper.GetString("schemes-path"),
SchemesAssetsPath: viper.GetString("schemes-assets-path"),
SchemesUpdateInterval: viper.GetInt("schemes-update"),
DisableSchemesUpdate: viper.GetInt("schemes-update") == 0,
SchemesPath: viper.GetString("schemes_path"),
SchemesAssetsPath: viper.GetString("schemes_assets_path"),
SchemesUpdateInterval: viper.GetInt("schemes_update"),
DisableSchemesUpdate: viper.GetInt("schemes_update") == 0,
IssuerPrivateKeysPath: viper.GetString("privkeys"),
RevocationDBType: viper.GetString("revocation-db-type"),
RevocationDBConnStr: viper.GetString("revocation-db-str"),
RevocationDBType: viper.GetString("revocation_db_type"),
RevocationDBConnStr: viper.GetString("revocation_db_str"),
RevocationSettings: irma.RevocationSettings{},
URL: viper.GetString("url"),
DisableTLS: viper.GetBool("no-tls"),
DisableTLS: viper.GetBool("no_tls"),
Email: viper.GetString("email"),
EnableSSE: viper.GetBool("sse"),
Verbose: viper.GetInt("verbose"),
Quiet: viper.GetBool("quiet"),
LogJSON: viper.GetBool("log-json"),
LogJSON: viper.GetBool("log_json"),
Logger: logger,
Production: viper.GetBool("production"),
JwtIssuer: viper.GetString("jwt-issuer"),
JwtPrivateKey: viper.GetString("jwt-privkey"),
JwtPrivateKeyFile: viper.GetString("jwt-privkey-file"),
AllowUnsignedCallbacks: viper.GetBool("allow-unsigned-callbacks"),
AugmentClientReturnURL: viper.GetBool("augment-client-return-url"),
JwtIssuer: viper.GetString("jwt_issuer"),
JwtPrivateKey: viper.GetString("jwt_privkey"),
JwtPrivateKeyFile: viper.GetString("jwt_privkey_file"),
AllowUnsignedCallbacks: viper.GetBool("allow_unsigned_callbacks"),
AugmentClientReturnURL: viper.GetBool("augment_client_return_url"),
}
}
func configureTLS() *tls.Config {
conf, err := server.TLSConf(
viper.GetString("tls-cert"),
viper.GetString("tls-cert-file"),
viper.GetString("tls-privkey"),
viper.GetString("tls-privkey-file"))
viper.GetString("tls_cert"),
viper.GetString("tls_cert_file"),
viper.GetString("tls_privkey"),
viper.GetString("tls_privkey_file"))
if err != nil {
die("", err)
}
......@@ -82,11 +83,18 @@ func configureTLS() *tls.Config {
func readConfig(cmd *cobra.Command, name, logname string, configpaths []string, productionDefaults map[string]interface{}) {
dashReplacer := strings.NewReplacer("-", "_")
viper.SetEnvKeyReplacer(dashReplacer)
viper.SetFileKeyReplacer(dashReplacer)
viper.SetEnvPrefix(strings.ToUpper(name))
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)
}
......@@ -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
// 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
mode := "development"
......@@ -164,7 +172,7 @@ func handleMapOrString(key string, dest interface{}) error {
func handlePermission(typ string) []string {
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{}
} else {
return []string{"*"}
......
......@@ -99,24 +99,24 @@ func configureMyirmaServer(cmd *cobra.Command) (*myirmaserver.Configuration, err
Configuration: configureIRMAServer(),
EmailConfiguration: configureEmail(),
CORSAllowedOrigins: viper.GetStringSlice("cors-allowed-origins"),
CORSAllowedOrigins: viper.GetStringSlice("cors_allowed_origins"),
StaticPath: viper.GetString("static-path"),
StaticPrefix: viper.GetString("static-prefix"),
StaticPath: viper.GetString("static_path"),
StaticPrefix: viper.GetString("static_prefix"),
DBType: myirmaserver.DBType(viper.GetString("db-type")),
DBConnStr: viper.GetString("db-str"),
DBType: myirmaserver.DBType(viper.GetString("db_type")),
DBConnStr: viper.GetString("db_str"),
LoginEmailSubjects: viper.GetStringMapString("login-email-subjects"),
LoginEmailFiles: viper.GetStringMapString("login-email-files"),
LoginEmailBaseURL: viper.GetStringMapString("login-url"),
DeleteEmailFiles: viper.GetStringMapString("delete-email-files"),
DeleteEmailSubjects: viper.GetStringMapString("delete-email-subjects"),
DeleteAccountFiles: viper.GetStringMapString("delete-account-files"),
DeleteAccountSubjects: viper.GetStringMapString("delete-account-subjects"),
DeleteDelay: viper.GetInt("delete-delay"),
LoginEmailSubjects: viper.GetStringMapString("login_email_subjects"),
LoginEmailFiles: viper.GetStringMapString("login_email_files"),
LoginEmailBaseURL: viper.GetStringMapString("login_url"),
DeleteEmailFiles: viper.GetStringMapString("delete_email_files"),
DeleteEmailSubjects: viper.GetStringMapString("delete_email_subjects"),
DeleteAccountFiles: viper.GetStringMapString("delete_account_files"),
DeleteAccountSubjects: viper.GetStringMapString("delete_account_subjects"),
DeleteDelay: viper.GetInt("delete_delay"),
SessionLifetime: viper.GetInt("session-lifetime"),
SessionLifetime: viper.GetInt("session_lifetime"),
}
if conf.Production && conf.DBType != myirmaserver.DBTypePostgres {
......@@ -125,12 +125,12 @@ func configureMyirmaServer(cmd *cobra.Command) (*myirmaserver.Configuration, err
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,
irma.NewAttributeTypeIdentifier(v))
}
for _, v := range viper.GetStringSlice("email-attributes") {
for _, v := range viper.GetStringSlice("email_attributes") {
conf.EmailAttributes = append(
conf.EmailAttributes,
irma.NewAttributeTypeIdentifier(v))
......
......@@ -30,7 +30,7 @@ type stoppableServer interface {
func runServer(serv stoppableServer, logger *logrus.Logger) {
// 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
TLSConfig := configureTLS()
......
......@@ -98,22 +98,22 @@ func configureKeyshareServer(cmd *cobra.Command) (*keyshareserver.Configuration,
Configuration: configureIRMAServer(),
EmailConfiguration: configureEmail(),
DBType: keyshareserver.DBType(viper.GetString("db-type")),
DBConnStr: viper.GetString("db-str"),
JwtKeyID: viper.GetUint32("jwt-privkey-id"),
JwtPrivateKey: viper.GetString("jwt-privkey"),
JwtPrivateKeyFile: viper.GetString("jwt-privkey-file"),
JwtIssuer: viper.GetString("jwt-issuer"),
JwtPinExpiry: viper.GetInt("jwt-pin-expiry"),
StoragePrimaryKeyFile: viper.GetString("storage-primary-keyfile"),
StorageFallbackKeyFiles: viper.GetStringSlice("storage-fallback-keyfile"),
KeyshareAttribute: irma.NewAttributeTypeIdentifier(viper.GetString("keyshare-attribute")),
RegistrationEmailSubjects: viper.GetStringMapString("registration-email-subjects"),
RegistrationEmailFiles: viper.GetStringMapString("registration-email-files"),
VerificationURL: viper.GetStringMapString("verification-url"),
DBType: keyshareserver.DBType(viper.GetString("db_type")),
DBConnStr: viper.GetString("db_str"),
JwtKeyID: viper.GetUint32("jwt_privkey_id"),
JwtPrivateKey: viper.GetString("jwt_privkey"),
JwtPrivateKeyFile: viper.GetString("jwt_privkey_file"),
JwtIssuer: viper.GetString("jwt_issuer"),
JwtPinExpiry: viper.GetInt("jwt_pin_expiry"),
StoragePrimaryKeyFile: viper.GetString("storage_primary_keyfile"),
StorageFallbackKeyFiles: viper.GetStringSlice("storage_fallback_keyfile"),
KeyshareAttribute: irma.NewAttributeTypeIdentifier(viper.GetString("keyshare_attribute")),
RegistrationEmailSubjects: viper.GetStringMapString("registration_email_subjects"),
RegistrationEmailFiles: viper.GetStringMapString("registration_email_files"),
VerificationURL: viper.GetStringMapString("verification_url"),
}
if conf.Production && conf.DBType != keyshareserver.DBTypePostgres {
......
......@@ -58,17 +58,17 @@ func configureKeyshareTasks(cmd *cobra.Command) *tasks.Configuration {
return &tasks.Configuration{
EmailConfiguration: configureEmail(),
DBConnStr: viper.GetString("db-str"),
DBConnStr: viper.GetString("db_str"),
ExpiryDelay: viper.GetInt("expiry-delay"),
DeleteDelay: viper.GetInt("delete-delay"),
ExpiryDelay: viper.GetInt("expiry_delay"),
DeleteDelay: viper.GetInt("delete_delay"),
DeleteExpiredAccountSubjects: viper.GetStringMapString("expired-email-subjects"),
DeleteExpiredAccountFiles: viper.GetStringMapString("expired-email-files"),
DeleteExpiredAccountSubjects: viper.GetStringMapString("expired_email_subjects"),
DeleteExpiredAccountFiles: viper.GetStringMapString("expired_email_files"),
Verbose: viper.GetInt("verbose"),
Quiet: viper.GetBool("quiet"),
LogJSON: viper.GetBool("log-json"),
LogJSON: viper.GetBool("log_json"),
Logger: logger,
}
}
......@@ -157,8 +157,8 @@ func configureServer(cmd *cobra.Command) (*requestorserver.Configuration, error)
readConfig(cmd, "irmaserver", "irma server", []string{".", "/etc/irmaserver/", "$HOME/.irmaserver"},
map[string]interface{}{
"no-auth": false,
"no-email": false,
"no_auth": false,
"no_email": false,
"url": "",
},
)
......@@ -167,38 +167,38 @@ func configureServer(cmd *cobra.Command) (*requestorserver.Configuration, error)
conf := &requestorserver.Configuration{
Configuration: configureIRMAServer(),
Permissions: requestorserver.Permissions{
Disclosing: handlePermission("disclose-perms"),
Signing: handlePermission("sign-perms"),
Issuing: handlePermission("issue-perms"),
Revoking: handlePermission("revoke-perms"),
Disclosing: handlePermission("disclose_perms"),
Signing: handlePermission("sign_perms"),
Issuing: handlePermission("issue_perms"),
Revoking: handlePermission("revoke_perms"),
},
SkipPrivateKeysCheck: viper.GetBool("skip-private-keys-check"),
ListenAddress: viper.GetString("listen-addr"),
SkipPrivateKeysCheck: viper.GetBool("skip_private_keys_check"),
ListenAddress: viper.GetString("listen_addr"),
Port: viper.GetInt("port"),
ApiPrefix: viper.GetString("api-prefix"),
ClientListenAddress: viper.GetString("client-listen-addr"),
ClientPort: viper.GetInt("client-port"),
DisableRequestorAuthentication: viper.GetBool("no-auth"),
ApiPrefix: viper.GetString("api_prefix"),
ClientListenAddress: viper.GetString("client_listen_addr"),
ClientPort: viper.GetInt("client_port"),
DisableRequestorAuthentication: viper.GetBool("no_auth"),
Requestors: make(map[string]requestorserver.Requestor),
MaxRequestAge: viper.GetInt("max-request-age"),
StaticPath: viper.GetString("static-path"),
StaticPrefix: viper.GetString("static-prefix"),
TlsCertificate: viper.GetString("tls-cert"),
TlsCertificateFile: viper.GetString("tls-cert-file"),
TlsPrivateKey: viper.GetString("tls-privkey"),
TlsPrivateKeyFile: viper.GetString("tls-privkey-file"),
ClientTlsCertificate: viper.GetString("client-tls-cert"),
ClientTlsCertificateFile: viper.GetString("client-tls-cert-file"),
ClientTlsPrivateKey: viper.GetString("client-tls-privkey"),
ClientTlsPrivateKeyFile: viper.GetString("client-tls-privkey-file"),
MaxRequestAge: viper.GetInt("max_request_age"),
StaticPath: viper.GetString("static_path"),
StaticPrefix: viper.GetString("static_prefix"),
TlsCertificate: viper.GetString("tls_cert"),
TlsCertificateFile: viper.GetString("tls_cert_file"),
TlsPrivateKey: viper.GetString("tls_privkey"),
TlsPrivateKeyFile: viper.GetString("tls_privkey_file"),
ClientTlsCertificate: viper.GetString("client_tls_cert"),
ClientTlsCertificateFile: viper.GetString("client_tls_cert_file"),
ClientTlsPrivateKey: viper.GetString("client_tls_privkey"),
ClientTlsPrivateKeyFile: viper.GetString("client_tls_privkey_file"),
}
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.")
}
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")
}
}
......@@ -208,11 +208,11 @@ func configureServer(cmd *cobra.Command) (*requestorserver.Configuration, error)
if err = handleMapOrString("requestors", &conf.Requestors); err != nil {
return nil, err
}
if err = handleMapOrString("static-sessions", &conf.StaticSessions); err != nil {
if err = handleMapOrString("static_sessions", &conf.StaticSessions); err != nil {
return nil, err
}
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
}
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