From 03739edb2a96ba89bc21ab3d3950eed0fbcbeb2f Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 15 Aug 2014 20:49:26 -0700 Subject: [PATCH] serverinit: break WriteDefaultConfigFile up, pull out getOrMakeKeyring Change-Id: I8322f975baba0037138af30d0bb59572f852b3ed --- pkg/serverinit/genconfig.go | 48 +++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/pkg/serverinit/genconfig.go b/pkg/serverinit/genconfig.go index 2ff02a63a..e26bb885a 100644 --- a/pkg/serverinit/genconfig.go +++ b/pkg/serverinit/genconfig.go @@ -724,28 +724,12 @@ func WriteDefaultConfigFile(filePath string, useSQLite bool) error { conf.KVFile = filepath.Join(osutil.CamliVarDir(), "camli-index.kvdb") } - var keyId string - secRing := osutil.SecretRingFile() - _, err := wkfs.Stat(secRing) - switch { - case err == nil: - keyId, err = jsonsign.KeyIdFromRing(secRing) - if err != nil { - return fmt.Errorf("Could not find any keyId in file %q: %v", secRing, err) - } - log.Printf("Re-using identity with keyId %q found in file %s", keyId, secRing) - case os.IsNotExist(err): - keyId, err = jsonsign.GenerateNewSecRing(secRing) - if err != nil { - return fmt.Errorf("Could not generate new secRing at file %q: %v", secRing, err) - } - log.Printf("Generated new identity with keyId %q in file %s", keyId, secRing) - } + keyID, secretRing, err := getOrMakeKeyring() if err != nil { - return fmt.Errorf("Could not stat secret ring %q: %v", secRing, err) + return err } - conf.Identity = keyId - conf.IdentitySecretRing = secRing + conf.Identity = keyID + conf.IdentitySecretRing = secretRing confData, err := json.MarshalIndent(conf, "", " ") if err != nil { @@ -758,3 +742,27 @@ func WriteDefaultConfigFile(filePath string, useSQLite bool) error { return nil } + +func getOrMakeKeyring() (keyID, secRing string, err error) { + secRing = osutil.SecretRingFile() + _, err = wkfs.Stat(secRing) + switch { + case err == nil: + keyID, err = jsonsign.KeyIdFromRing(secRing) + if err != nil { + err = fmt.Errorf("Could not find any keyID in file %q: %v", secRing, err) + return + } + log.Printf("Re-using identity with keyID %q found in file %s", keyID, secRing) + case os.IsNotExist(err): + keyID, err = jsonsign.GenerateNewSecRing(secRing) + if err != nil { + err = fmt.Errorf("Could not generate new secRing at file %q: %v", secRing, err) + return + } + log.Printf("Generated new identity with keyID %q in file %s", keyID, secRing) + default: + err = fmt.Errorf("Could not stat secret ring %q: %v", secRing, err) + } + return +}