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"
41 AuthTokenOutputFileName string
42 RefreshMarginSeconds int
45 func NewConfig() *Config {
47 CertPath: getEnv("CERT_PATH", "security/tls.crt"),
48 KeyPath: getEnv("CERT_KEY_PATH", "security/tls.key"),
49 LogLevel: getLogLevel(),
50 GrantType: getEnv("CREDS_GRANT_TYPE", ""),
51 ClientSecret: getEnv("CREDS_CLIENT_SECRET", ""),
52 ClientId: getEnv("CREDS_CLIENT_ID", ""),
53 AuthTokenOutputFileName: getEnv("OUTPUT_FILE", "/tmp/authToken.txt"),
54 AuthServiceUrl: getEnv("AUTH_SERVICE_URL", "https://localhost:39687/example-singlelogin-sever/login"),
55 RefreshMarginSeconds: getEnvAsInt("REFRESH_MARGIN_SECONDS", 5, 1, 3600),
59 func validateConfiguration(configuration *Config) error {
60 if configuration.CertPath == "" || configuration.KeyPath == "" {
61 return fmt.Errorf("missing CERT_PATH and/or CERT_KEY_PATH")
67 func getEnv(key string, defaultVal string) string {
68 if value, exists := os.LookupEnv(key); exists {
69 log.Debugf("Using value: '%v' for '%v'", value, key)
72 log.Debugf("Using default value: '%v' for '%v'", defaultVal, key)
77 func getEnvAsInt(name string, defaultVal int, min int, max int) int {
78 valueStr := getEnv(name, "")
79 if value, err := strconv.Atoi(valueStr); err == nil {
80 if value < min || value > max {
81 log.Warnf("Value out of range: '%v' for variable: '%v'. Default value: '%v' will be used", valueStr, name, defaultVal)
85 } else if valueStr != "" {
86 log.Warnf("Invalid int value: '%v' for variable: '%v'. Default value: '%v' will be used", valueStr, name, defaultVal)
92 func getLogLevel() log.Level {
93 logLevelStr := getEnv("LOG_LEVEL", "Info")
94 if loglevel, err := log.ParseLevel(logLevelStr); err == nil {
97 log.Warnf("Invalid log level: %v. Log level will be Info!", logLevelStr)