github.com/argoproj/argo-cd@v1.8.7/util/util.go (about)

     1  package util
     2  
     3  import (
     4  	"crypto/rand"
     5  	"encoding/base64"
     6  	"time"
     7  )
     8  
     9  // Wait takes a check interval and timeout and waits for a function to return `true`.
    10  // Wait will return `true` on success and `false` on timeout.
    11  // The passed function, in turn, should pass `true` (or anything, really) to the channel when it's done.
    12  // Pass `0` as the timeout to run infinitely until completion.
    13  func Wait(timeout uint, f func(chan<- bool)) bool {
    14  	done := make(chan bool)
    15  	go f(done)
    16  
    17  	// infinite
    18  	if timeout == 0 {
    19  		return <-done
    20  	}
    21  
    22  	timedOut := time.After(time.Duration(timeout) * time.Second)
    23  	for {
    24  		select {
    25  		case <-done:
    26  			return true
    27  		case <-timedOut:
    28  			return false
    29  		}
    30  	}
    31  }
    32  
    33  // MakeSignature generates a cryptographically-secure pseudo-random token, based on a given number of random bytes, for signing purposes.
    34  func MakeSignature(size int) ([]byte, error) {
    35  	b := make([]byte, size)
    36  	_, err := rand.Read(b)
    37  	if err != nil {
    38  		b = nil
    39  	}
    40  	// base64 encode it so signing key can be typed into validation utilities
    41  	b = []byte(base64.StdEncoding.EncodeToString(b))
    42  	return b, err
    43  }