2021-09-11 11:35:15 +00:00
|
|
|
/*
|
2024-05-11 20:42:21 +00:00
|
|
|
Copyright CAcert Inc.
|
2023-05-13 11:27:19 +00:00
|
|
|
SPDX-License-Identifier: Apache-2.0
|
2021-09-11 11:35:15 +00:00
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
2021-09-11 11:35:15 +00:00
|
|
|
|
2023-07-18 18:37:04 +00:00
|
|
|
https://www.apache.org/licenses/LICENSE-2.0
|
2021-09-11 11:35:15 +00:00
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
2021-09-11 11:35:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-05-11 23:07:34 +00:00
|
|
|
"log/slog"
|
2021-09-11 11:35:15 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/knadh/koanf"
|
|
|
|
"github.com/knadh/koanf/parsers/toml"
|
|
|
|
"github.com/knadh/koanf/providers/confmap"
|
|
|
|
"github.com/knadh/koanf/providers/env"
|
|
|
|
"github.com/knadh/koanf/providers/file"
|
|
|
|
"github.com/knadh/koanf/providers/posflag"
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
)
|
|
|
|
|
2023-07-29 19:06:34 +00:00
|
|
|
const (
|
|
|
|
defaultServerPort = 3000
|
|
|
|
defaultFile = "idp.toml"
|
|
|
|
)
|
2023-07-29 17:47:51 +00:00
|
|
|
|
|
|
|
var DefaultConfig = map[string]interface{}{
|
|
|
|
"server.bind_address": "",
|
|
|
|
"server.name": "idp.cacert.localhost",
|
|
|
|
"server.port": defaultServerPort,
|
|
|
|
"server.key": "idp.cacert.localhost+1-key.pem",
|
|
|
|
"server.certificate": "idp.cacert.localhost+1.pem",
|
|
|
|
"security.client.ca-file": "client_ca.pem",
|
|
|
|
"admin.url": "https://hydra.cacert.localhost:4445/",
|
|
|
|
"i18n.languages": []string{"en", "de"},
|
2023-07-29 18:32:02 +00:00
|
|
|
"log.level": "info",
|
|
|
|
"log.json": true,
|
2023-07-29 17:47:51 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
func ConfigureApplication(
|
2024-05-11 23:07:34 +00:00
|
|
|
logger *slog.Logger,
|
2021-09-11 11:35:15 +00:00
|
|
|
appName string,
|
|
|
|
defaultConfig map[string]interface{},
|
|
|
|
) (*koanf.Koanf, error) {
|
|
|
|
f := pflag.NewFlagSet("config", pflag.ContinueOnError)
|
|
|
|
f.Usage = func() {
|
2023-07-29 18:32:02 +00:00
|
|
|
logger.Info(f.FlagUsages())
|
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
f.StringSlice(
|
|
|
|
"conf",
|
|
|
|
[]string{fmt.Sprintf("%s.toml", strings.ToLower(appName))},
|
|
|
|
"path to one or more .toml files",
|
|
|
|
)
|
|
|
|
|
2023-07-29 18:32:02 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if err = f.Parse(os.Args[1:]); err != nil {
|
2024-05-11 23:07:34 +00:00
|
|
|
return nil, fmt.Errorf("could not parse command line arguments: %w", err)
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config := koanf.New(".")
|
|
|
|
|
|
|
|
_ = config.Load(confmap.Provider(defaultConfig, "."), nil)
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2023-07-29 19:06:34 +00:00
|
|
|
if err = config.Load(file.Provider(defaultFile), toml.Parser()); err != nil && !os.IsNotExist(err) {
|
2024-05-11 23:07:34 +00:00
|
|
|
logger.Error("could not load configuration from file", "file", defaultFile)
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("error loading configuration: %w", err)
|
2023-07-29 19:06:34 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
cFiles, _ := f.GetStringSlice("conf")
|
|
|
|
for _, c := range cFiles {
|
2023-07-29 18:32:02 +00:00
|
|
|
if err = config.Load(file.Provider(c), toml.Parser()); err != nil {
|
2024-05-11 23:07:34 +00:00
|
|
|
logger.Error("could not load configuration from file", "file", c)
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("error loading configuration: %w", err)
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2023-07-29 18:32:02 +00:00
|
|
|
if err = config.Load(posflag.Provider(f, ".", config), nil); err != nil {
|
2024-05-11 23:07:34 +00:00
|
|
|
return nil, fmt.Errorf("error loading configuration from command line: %w", err)
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
prefix := fmt.Sprintf("%s_", strings.ToUpper(appName))
|
2023-07-29 18:32:02 +00:00
|
|
|
|
|
|
|
if err = config.Load(env.Provider(prefix, ".", func(s string) string {
|
2023-05-13 11:27:19 +00:00
|
|
|
return strings.ReplaceAll(strings.ToLower(strings.TrimPrefix(s, prefix)), "_", ".")
|
2021-09-11 11:35:15 +00:00
|
|
|
}), nil); err != nil {
|
2024-05-11 23:07:34 +00:00
|
|
|
return nil, fmt.Errorf("error loading configuration from environment: %w", err)
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
|
|
|
return config, nil
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|