X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=auth-token-fetch%2Fconfig.go;fp=auth-token-fetch%2Fconfig.go;h=18d610d5c17f179080b789a91fed72d3fe2dd4d4;hb=07b21181487addeb0d89ddc063a89dbc0981d1b5;hp=0000000000000000000000000000000000000000;hpb=0a882454d203e62e18f55ed1d3d5b3790801169b;p=nonrtric.git diff --git a/auth-token-fetch/config.go b/auth-token-fetch/config.go new file mode 100644 index 00000000..18d610d5 --- /dev/null +++ b/auth-token-fetch/config.go @@ -0,0 +1,100 @@ +// - +// ========================LICENSE_START================================= +// O-RAN-SC +// %% +// Copyright (C) 2022: Nordix Foundation +// %% +// 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 +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. +// ========================LICENSE_END=================================== +// + +package main + +import ( + "os" + + "strconv" + + "fmt" + + log "github.com/sirupsen/logrus" +) + +type Config struct { + LogLevel log.Level + CertPath string + KeyPath string + AuthServiceUrl string + GrantType string + ClientSecret string + ClientId string + AuthTokenOutputFileName string + RefreshMarginSeconds int +} + +func NewConfig() *Config { + return &Config{ + CertPath: getEnv("CERT_PATH", "security/tls.crt"), + KeyPath: getEnv("CERT_KEY_PATH", "security/tls.key"), + LogLevel: getLogLevel(), + GrantType: getEnv("CREDS_GRANT_TYPE", ""), + ClientSecret: getEnv("CREDS_CLIENT_SECRET", ""), + ClientId: getEnv("CREDS_CLIENT_ID", ""), + AuthTokenOutputFileName: getEnv("OUTPUT_FILE", "/tmp/authToken.txt"), + AuthServiceUrl: getEnv("AUTH_SERVICE_URL", "https://localhost:39687/example-singlelogin-sever/login"), + RefreshMarginSeconds: getEnvAsInt("REFRESH_MARGIN_SECONDS", 5, 1, 3600), + } +} + +func validateConfiguration(configuration *Config) error { + if configuration.CertPath == "" || configuration.KeyPath == "" { + return fmt.Errorf("missing CERT_PATH and/or CERT_KEY_PATH") + } + + return nil +} + +func getEnv(key string, defaultVal string) string { + if value, exists := os.LookupEnv(key); exists { + log.Debugf("Using value: '%v' for '%v'", value, key) + return value + } else { + log.Debugf("Using default value: '%v' for '%v'", defaultVal, key) + return defaultVal + } +} + +func getEnvAsInt(name string, defaultVal int, min int, max int) int { + valueStr := getEnv(name, "") + if value, err := strconv.Atoi(valueStr); err == nil { + if value < min || value > max { + log.Warnf("Value out of range: '%v' for variable: '%v'. Default value: '%v' will be used", valueStr, name, defaultVal) + return defaultVal + } + return value + } else if valueStr != "" { + log.Warnf("Invalid int value: '%v' for variable: '%v'. Default value: '%v' will be used", valueStr, name, defaultVal) + } + return defaultVal + +} + +func getLogLevel() log.Level { + logLevelStr := getEnv("LOG_LEVEL", "Info") + if loglevel, err := log.ParseLevel(logLevelStr); err == nil { + return loglevel + } else { + log.Warnf("Invalid log level: %v. Log level will be Info!", logLevelStr) + return log.InfoLevel + } +}