From f584197bbae07ba17d58126ac3b95075a6446f1c Mon Sep 17 00:00:00 2001 From: Erik Wilson Date: Thu, 25 Apr 2019 10:53:21 -0700 Subject: [PATCH] Save password as text file --- pkg/agent/config/config.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pkg/agent/config/config.go b/pkg/agent/config/config.go index 4acf6d6d5b..6f075fd37e 100644 --- a/pkg/agent/config/config.go +++ b/pkg/agent/config/config.go @@ -74,7 +74,7 @@ func getNodeNamedCrt(nodeName, nodePasswordFile string) HTTPRequester { if err != nil { return nil, err } - req.Header.Set("K3s-Node-Password", hex.EncodeToString(nodePassword)) + req.Header.Set("K3s-Node-Password", nodePassword) resp, err := client.Do(req) if err != nil { @@ -90,16 +90,18 @@ func getNodeNamedCrt(nodeName, nodePasswordFile string) HTTPRequester { } } -func ensureNodePassword(nodePasswordFile string) ([]byte, error) { +func ensureNodePassword(nodePasswordFile string) (string, error) { if _, err := os.Stat(nodePasswordFile); err == nil { - return ioutil.ReadFile(nodePasswordFile) + password, err := ioutil.ReadFile(nodePasswordFile) + return strings.TrimSpace(string(password)), err } password := make([]byte, 16, 16) _, err := cryptorand.Read(password) if err != nil { - return nil, err + return "", err } - return password, ioutil.WriteFile(nodePasswordFile, password, 0600) + nodePassword := hex.EncodeToString(password) + return nodePassword, ioutil.WriteFile(nodePasswordFile, []byte(nodePassword), 0600) } func getNodeCert(nodeName, nodeCertFile, nodeKeyFile, nodePasswordFile string, info *clientaccess.Info) (*tls.Certificate, error) { @@ -253,7 +255,7 @@ func get(envInfo *cmds.Agent) (*config.Node, error) { nodeCertFile := filepath.Join(envInfo.DataDir, "token-node.crt") nodeKeyFile := filepath.Join(envInfo.DataDir, "token-node.key") - nodePasswordFile := filepath.Join(envInfo.DataDir, "node-password.bin") + nodePasswordFile := filepath.Join(envInfo.DataDir, "node-password.txt") nodeCert, err := getNodeCert(nodeName, nodeCertFile, nodeKeyFile, nodePasswordFile, info) if err != nil {