feat: tokens in secret data

This commit is contained in:
Mikaël Cluseau 2018-06-19 17:48:28 +11:00
parent 4d889632f6
commit d3bb8e3579

View File

@ -1,6 +1,8 @@
package main
import (
"crypto/rand"
"encoding/base32"
"encoding/json"
"errors"
"io/ioutil"
@ -23,6 +25,7 @@ type SecretData struct {
type ClusterSecrets struct {
CAs map[string]*CA
Tokens map[string]string
}
type CA struct {
@ -80,12 +83,34 @@ func (sd *SecretData) cluster(name string) (cs *ClusterSecrets) {
cs = &ClusterSecrets{
CAs: make(map[string]*CA),
Tokens: make(map[string]string),
}
sd.clusters[name] = cs
sd.changed = true
return
}
func (sd *SecretData) Token(cluster, name string) (token string, err error) {
cs := sd.cluster(cluster)
token = cs.Tokens[name]
if token != "" {
return
}
b := make([]byte, 16)
_, err = rand.Read(b)
if err != nil {
return
}
token = base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(b)
cs.Tokens[name] = token
sd.changed = true
return
}
func (sd *SecretData) CA(cluster, name string) (ca *CA, err error) {
cs := sd.cluster(cluster)