github.com/verrazzano/verrazzano-monitoring-operator@v0.0.30/verrazzano-backup-hook/utilities/basicUtils.go (about)

     1  // Copyright (c) 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package utilities
     5  
     6  import (
     7  	"bufio"
     8  	"crypto/rand"
     9  	"fmt"
    10  	"github.com/spf13/viper"
    11  	"github.com/verrazzano/verrazzano-monitoring-operator/verrazzano-backup-hook/constants"
    12  	"go.uber.org/zap"
    13  	"math/big"
    14  	"os"
    15  	"strings"
    16  	"time"
    17  )
    18  
    19  // CreateTempFileWithData used to create temp cloud-creds utilized for object store access
    20  func CreateTempFileWithData(data []byte) (string, error) {
    21  	file, err := os.CreateTemp(os.TempDir(), "cloud-creds-*.ini")
    22  	if err != nil {
    23  		return "", err
    24  	}
    25  	defer file.Close()
    26  	_, err = file.Write(data)
    27  	if err != nil {
    28  		return "", err
    29  	}
    30  	return file.Name(), nil
    31  }
    32  
    33  // WaitRandom generates a random number between min and max
    34  func WaitRandom(message, timeout string, log *zap.SugaredLogger) (int, error) {
    35  	randomBig, err := rand.Int(rand.Reader, big.NewInt(constants.Max))
    36  	if err != nil {
    37  		return 0, fmt.Errorf("Unable to generate random number %v", zap.Error(err))
    38  	}
    39  	randomInt := int(randomBig.Int64())
    40  	if randomInt < constants.Min {
    41  		randomInt = (constants.Min + constants.Max) / 2
    42  	}
    43  	timeParse, err := time.ParseDuration(timeout)
    44  	if err != nil {
    45  		return 0, fmt.Errorf("Unable to parse time duration %v", zap.Error(err))
    46  	}
    47  	// handle timeouts lesser that generated min!
    48  	if float64(randomInt) > timeParse.Seconds() {
    49  		randomInt = int(timeParse.Seconds())
    50  	}
    51  	log.Infof("%v . Wait for '%v' seconds ...", message, randomInt)
    52  	time.Sleep(time.Second * time.Duration(randomInt))
    53  	return randomInt, nil
    54  }
    55  
    56  // ReadTempCredsFile reads object store credentials from a temporary file for registration purpose
    57  func ReadTempCredsFile(filePath, credentialProfile string) (string, string, error) {
    58  	var awsAccessKey, awsSecretAccessKey string
    59  	pathElements := strings.Split(filePath, "/")
    60  	viper.SetConfigName(pathElements[len(pathElements)-1])
    61  	viper.SetConfigType("ini")
    62  	viper.AddConfigPath("/tmp/")
    63  	err := viper.ReadInConfig()
    64  	if err != nil {
    65  		return awsAccessKey, awsSecretAccessKey, nil
    66  	}
    67  	accessKeyString := fmt.Sprintf("%s.%s", credentialProfile, constants.AwsAccessKeyString)
    68  	secretAccessKeyString := fmt.Sprintf("%s.%s", credentialProfile, constants.AwsSecretAccessKeyString)
    69  	return fmt.Sprintf("%s", viper.Get(accessKeyString)), fmt.Sprintf("%s", viper.Get(secretAccessKeyString)), nil
    70  }
    71  
    72  // GetEnvWithDefault retrieves env variable with default value
    73  func GetEnvWithDefault(key, defaultValue string) string {
    74  	value, ok := os.LookupEnv(key)
    75  	if !ok {
    76  		return defaultValue
    77  	}
    78  	return value
    79  }
    80  
    81  // GetComponent retrieves component info from file
    82  func GetComponent(filePath string) (string, error) {
    83  	var line string
    84  	_, err := os.Stat(filePath)
    85  	if err != nil {
    86  		return "", err
    87  	}
    88  	f, err := os.Open(filePath)
    89  	if err != nil {
    90  		return "", nil
    91  	}
    92  	defer f.Close()
    93  	scanner := bufio.NewScanner(f)
    94  	for scanner.Scan() {
    95  		line = strings.TrimSpace(scanner.Text())
    96  	}
    97  	return line, nil
    98  }