Add key generation.

This commit is contained in:
Brendan Burns
2015-05-28 11:45:08 -07:00
committed by CJ Cullen
parent 30a89968a4
commit 5115fd5703
13 changed files with 162 additions and 5 deletions

View File

@@ -18,7 +18,9 @@ package master
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/pprof"
@@ -147,10 +149,13 @@ type Config struct {
ServiceNodePortRange util.PortRange
// Used for secure proxy. If empty, don't use secure proxy.
SSHUser string
SSHKeyfile string
SSHUser string
SSHKeyfile string
InstallSSHKey InstallSSHKey
}
type InstallSSHKey func(user string, data []byte) error
// Master contains state for a Kubernetes cluster master/api server.
type Master struct {
// "Inputs", Copied from Config
@@ -204,7 +209,8 @@ type Master struct {
InsecureHandler http.Handler
// Used for secure proxy
tunnels util.SSHTunnelList
tunnels util.SSHTunnelList
installSSHKey InstallSSHKey
}
// NewEtcdHelper returns an EtcdHelper for the provided arguments or an error if the version
@@ -486,6 +492,16 @@ func (m *Master) init(c *Config) {
var proxyDialer func(net, addr string) (net.Conn, error)
if len(c.SSHUser) > 0 {
glog.Infof("Setting up proxy: %s %s", c.SSHUser, c.SSHKeyfile)
exists, err := util.FileExists(c.SSHKeyfile)
if err != nil {
glog.Errorf("Error detecting if key exists: %v", err)
} else if !exists {
glog.Infof("Key doesn't exist, attempting to create")
err := m.generateSSHKey(c.SSHUser, c.SSHKeyfile)
if err != nil {
glog.Errorf("Failed to create key pair: %v", err)
}
}
m.setupSecureProxy(c.SSHUser, c.SSHKeyfile)
proxyDialer = m.Dial
}
@@ -801,3 +817,21 @@ func (m *Master) setupSecureProxy(user, keyfile string) {
}
}()
}
func (m *Master) generateSSHKey(user, keyfile string) error {
if m.installSSHKey == nil {
return errors.New("ssh install function is null")
}
private, public, err := util.GenerateKey(2048)
if err != nil {
return err
}
ioutil.WriteFile(keyfile, util.EncodePrivateKey(private), 0600)
data, err := util.EncodeSSHKey(public)
if err != nil {
return err
}
fmt.Printf("FOO: %s", data)
return m.installSSHKey(user, data)
}