From 6c20c29106f1cdef03e22a25201000ed6bb0aea5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Cluseau?= Date: Fri, 6 Jul 2018 11:13:56 +1100 Subject: [PATCH] fix(secrets): key/cert generation check req --- hash.go | 19 +++++++++++++++++++ secrets.go | 13 ++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 hash.go diff --git a/hash.go b/hash.go new file mode 100644 index 0000000..7b4cf31 --- /dev/null +++ b/hash.go @@ -0,0 +1,19 @@ +package main + +import ( + "crypto/sha1" + "encoding/base64" + "encoding/json" +) + +func hash(values ...interface{}) string { + ba, err := json.Marshal(values) + if err != nil { + panic(err) // should not happen + } + + h := sha1.Sum(ba) + + enc := base64.StdEncoding.WithPadding(base64.NoPadding) + return enc.EncodeToString(h[:]) +} diff --git a/secrets.go b/secrets.go index 1e897e1..c8eb7a4 100644 --- a/secrets.go +++ b/secrets.go @@ -36,8 +36,9 @@ type CA struct { } type KeyCert struct { - Key []byte - Cert []byte + Key []byte + Cert []byte + ReqHash string } func loadSecretData(config *config.Config) (*SecretData, error) { @@ -161,8 +162,9 @@ func (sd *SecretData) KeyCert(cluster, caName, name, profile, label string, req return } + rh := hash(req) kc, ok := ca.Signed[name] - if ok { + if ok && rh == kc.ReqHash { return } @@ -190,8 +192,9 @@ func (sd *SecretData) KeyCert(cluster, caName, name, profile, label string, req } kc = &KeyCert{ - Key: key, - Cert: cert, + Key: key, + Cert: cert, + ReqHash: rh, } ca.Signed[name] = kc