func (c *SSHTunneler) generateSSHKey(user, privateKeyfile, publicKeyfile string) error { // TODO: user is not used. Consider removing it as an input to the function. private, public, err := util.GenerateKey(2048) if err != nil { return err } // If private keyfile already exists, we must have only made it halfway // through last time, so delete it. exists, err := util.FileExists(privateKeyfile) if err != nil { glog.Errorf("Error detecting if private key exists: %v", err) } else if exists { glog.Infof("Private key exists, but public key does not") if err := os.Remove(privateKeyfile); err != nil { glog.Errorf("Failed to remove stale private key: %v", err) } } if err := ioutil.WriteFile(privateKeyfile, util.EncodePrivateKey(private), 0600); err != nil { return err } publicKeyBytes, err := util.EncodePublicKey(public) if err != nil { return err } if err := ioutil.WriteFile(publicKeyfile+".tmp", publicKeyBytes, 0600); err != nil { return err } return os.Rename(publicKeyfile+".tmp", publicKeyfile) }
// Run establishes tunnel loops and returns func (c *SSHTunneler) Run(getAddresses AddressFunc) { if c.stopChan != nil { return } c.stopChan = make(chan struct{}) // Save the address getter if getAddresses != nil { c.getAddresses = getAddresses } // Usernames are capped @ 32 if len(c.SSHUser) > 32 { glog.Warning("SSH User is too long, truncating to 32 chars") c.SSHUser = c.SSHUser[0:32] } glog.Infof("Setting up proxy: %s %s", c.SSHUser, c.SSHKeyfile) // public keyfile is written last, so check for that. publicKeyFile := c.SSHKeyfile + ".pub" exists, err := util.FileExists(publicKeyFile) 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 := c.generateSSHKey(c.SSHUser, c.SSHKeyfile, publicKeyFile) if err != nil { glog.Errorf("Failed to create key pair: %v", err) } } c.tunnels = &util.SSHTunnelList{} c.setupSecureProxy(c.SSHUser, c.SSHKeyfile, publicKeyFile) c.lastSync = c.clock.Now().Unix() }
func TestDeleter(t *testing.T) { tempPath := fmt.Sprintf("/tmp/hostpath/%s", util.NewUUID()) defer os.RemoveAll(tempPath) err := os.MkdirAll(tempPath, 0750) if err != nil { t.Fatal("Failed to create tmp directory for deleter: %v", err) } plugMgr := volume.VolumePluginMgr{} plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volume.NewFakeVolumeHost("/tmp/fake", nil, nil)) spec := &volume.Spec{PersistentVolume: &api.PersistentVolume{Spec: api.PersistentVolumeSpec{PersistentVolumeSource: api.PersistentVolumeSource{HostPath: &api.HostPathVolumeSource{Path: tempPath}}}}} plug, err := plugMgr.FindDeletablePluginBySpec(spec) if err != nil { t.Errorf("Can't find the plugin by name") } deleter, err := plug.NewDeleter(spec) if err != nil { t.Errorf("Failed to make a new Deleter: %v", err) } if deleter.GetPath() != tempPath { t.Errorf("Expected %s but got %s", tempPath, deleter.GetPath()) } if err := deleter.Delete(); err != nil { t.Errorf("Mock Recycler expected to return nil but got %s", err) } if exists, _ := util.FileExists("foo"); exists { t.Errorf("Temp path expected to be deleted, but was found at %s", tempPath) } }