Skip to content
This repository has been archived by the owner on Jun 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #133 from ibuildthecloud/key
Browse files Browse the repository at this point in the history
Generate and send host rsa key
  • Loading branch information
ibuildthecloud authored Jan 25, 2017
2 parents 8400475 + f32a2b0 commit f8e2812
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
88 changes: 88 additions & 0 deletions core/hostInfo/key_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package hostInfo

import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"os"
"path"

"github.com/Sirupsen/logrus"
"github.com/pkg/errors"
"github.com/rancher/agent/utilities/config"
)

type KeyCollector struct {
key string
}

func (k KeyCollector) GetData() (map[string]interface{}, error) {
key, err := k.getKey()
return map[string]interface{}{
"data": key,
}, err
}

func (k KeyCollector) getKey() (string, error) {
if k.key != "" {
return k.key, nil
}

fileName := config.KeyFile()
bytes, err := ioutil.ReadFile(fileName)
if os.IsNotExist(err) {
bytes, err = k.genKey()
if err != nil {
return "", err
}
os.MkdirAll(path.Base(fileName), 0400)
err = ioutil.WriteFile(fileName, bytes, 0400)
if err != nil {
return "", err
}
} else if err != nil {
return "", err
}

b, _ := pem.Decode(bytes)
key, err := x509.ParsePKCS1PrivateKey(b.Bytes)
if err != nil {
return "", err
}

bytes, err = x509.MarshalPKIXPublicKey(&key.PublicKey)
if err != nil {
return "", errors.Wrap(err, "failed to marshal public key")
}

bytes = pem.EncodeToMemory(&pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: bytes,
})
k.key = string(bytes)

return k.key, nil
}

func (k KeyCollector) genKey() ([]byte, error) {
logrus.Info("Generating host key")
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, err
}
logrus.Info("Done generating host key")
return pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key),
}), nil
}

func (k KeyCollector) KeyName() string {
return "hostKey"
}

func (k KeyCollector) GetLabels(prefix string) (map[string]string, error) {
return map[string]string{}, nil
}
1 change: 1 addition & 0 deletions handlers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func initializeHandlers() *Handler {
Version: version,
},
},
hostInfo.KeyCollector{},
}
computerHandler := ComputeHandler{
dockerClient: clientWithTimeout,
Expand Down
5 changes: 5 additions & 0 deletions utilities/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ func StateDir() string {
return DefaultValue("STATE_DIR", Home())
}

func KeyFile() string {
defValue := fmt.Sprintf("%s/../etc/ssl/host.key", StateDir())
return DefaultValue("HOST_KEY_FILE", defValue)
}

func physicalHostUUIDFile() string {
defValue := fmt.Sprintf("%s/.physical_host_uuid", StateDir())
return DefaultValue("PHYSICAL_HOST_UUID_FILE", defValue)
Expand Down

0 comments on commit f8e2812

Please sign in to comment.