This repository has been archived by the owner on Jun 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from ibuildthecloud/key
Generate and send host rsa key
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters