mirror of
https://git.vectorsigma.ru/public/k3s.git
synced 2026-07-29 19:19:42 +00:00
* Replace ioutil package * check integration test null pointer * Remove rotate retries Signed-off-by: Derek Nola <derek.nola@suse.com>
39 lines
673 B
Go
39 lines
673 B
Go
package token
|
|
|
|
import (
|
|
cryptorand "crypto/rand"
|
|
"encoding/hex"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func Random(size int) (string, error) {
|
|
token := make([]byte, size, size)
|
|
_, err := cryptorand.Read(token)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return hex.EncodeToString(token), err
|
|
}
|
|
|
|
func ReadFile(path string) (string, error) {
|
|
if path == "" {
|
|
return "", nil
|
|
}
|
|
|
|
for {
|
|
tokenBytes, err := os.ReadFile(path)
|
|
if err == nil {
|
|
return strings.TrimSpace(string(tokenBytes)), nil
|
|
} else if os.IsNotExist(err) {
|
|
logrus.Infof("Waiting for %s to be available\n", path)
|
|
time.Sleep(2 * time.Second)
|
|
} else {
|
|
return "", err
|
|
}
|
|
}
|
|
}
|