2 // ========================LICENSE_START=================================
5 // Copyright (C) 2022: Nordix Foundation
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // ========================LICENSE_END===================================
30 log "github.com/sirupsen/logrus"
42 AuthTokenOutputFileName string
43 RefreshMarginSeconds int
46 func NewConfig() *Config {
48 CertPath: getEnv("CERT_PATH", "security/tls.crt", false),
49 KeyPath: getEnv("CERT_KEY_PATH", "security/tls.key", false),
50 CACertsPath: getEnv("ROOT_CA_CERTS_PATH", "", false),
51 LogLevel: getLogLevel(),
52 GrantType: getEnv("CREDS_GRANT_TYPE", "", false),
53 ClientSecret: getEnv("CREDS_CLIENT_SECRET", "", true),
54 ClientId: getEnv("CREDS_CLIENT_ID", "", false),
55 AuthTokenOutputFileName: getEnv("OUTPUT_FILE", "/tmp/authToken.txt", false),
56 AuthServiceUrl: getEnv("AUTH_SERVICE_URL", "https://localhost:39687/example-singlelogin-sever/login", false),
57 RefreshMarginSeconds: getEnvAsInt("REFRESH_MARGIN_SECONDS", 5, 1, 3600),
61 func validateConfiguration(configuration *Config) error {
62 if configuration.CertPath == "" || configuration.KeyPath == "" {
63 return fmt.Errorf("missing CERT_PATH and/or CERT_KEY_PATH")
66 if configuration.CACertsPath == "" {
67 log.Warn("No Root CA certs loaded, no trust validation may be performed")
73 func getEnv(key string, defaultVal string, secret bool) string {
74 if value, exists := os.LookupEnv(key); exists {
76 log.Debugf("Using value: '%v' for '%v'", value, key)
81 log.Debugf("Using default value: '%v' for '%v'", defaultVal, key)
87 func getEnvAsInt(name string, defaultVal int, min int, max int) int {
88 valueStr := getEnv(name, fmt.Sprint(defaultVal), false)
89 if value, err := strconv.Atoi(valueStr); err == nil {
90 if value < min || value > max {
91 log.Warnf("Value out of range: '%v' for variable: '%v'. Default value: '%v' will be used", valueStr, name, defaultVal)
95 } else if valueStr != "" {
96 log.Warnf("Invalid int value: '%v' for variable: '%v'. Default value: '%v' will be used", valueStr, name, defaultVal)
102 func getLogLevel() log.Level {
103 logLevelStr := getEnv("LOG_LEVEL", "Info", false)
104 if loglevel, err := log.ParseLevel(logLevelStr); err == nil {
107 log.Warnf("Invalid log level: %v. Log level will be Info!", logLevelStr)