passmgr reload when turned off/on

This commit is contained in:
Pavel Vorobyov
2019-09-26 16:06:17 +03:00
parent b14f247974
commit a61050ee4b
3 changed files with 50 additions and 6 deletions

View File

@@ -440,6 +440,7 @@ func doOnOff(propName string, propRef *bool, args []string) bool {
term.Warnf("%s is %s\n", propName, value)
return false
}
prev := *propRef
switch args[0] {
case "on":
*propRef = true
@@ -449,7 +450,7 @@ func doOnOff(propName string, propRef *bool, args []string) bool {
term.Errorf("Invalid %s vaue. Please use either \"on\" or \"off\"\n", propName)
return false
}
return true
return prev != *propRef
}
func (c *Cli) setRaiseType(rt string) {

View File

@@ -9,7 +9,6 @@ import (
"syscall"
"github.com/viert/xc/passmgr"
"github.com/viert/xc/remote"
"github.com/viert/xc/term"
)
@@ -50,6 +49,7 @@ func (c *Cli) setupCmdHandlers() {
c.handlers["p_runscript"] = c.doPRunScript
c.handlers["use_password_manager"] = c.doUsePasswordManager
c.handlers["distribute_type"] = c.doDistributeType
c.handlers["_passmgr_debug"] = c.doPassmgrDebug
commands := make([]string, len(c.handlers))
i := 0
@@ -299,6 +299,9 @@ func (c *Cli) doUsePasswordManager(name string, argsLine string, args ...string)
term.Errorf("Password manager is not ready\n")
c.usePasswordMgr = false
}
if c.usePasswordMgr {
passmgr.Reload()
}
remote.SetUsePasswordManager(c.usePasswordMgr)
}
}
@@ -463,3 +466,7 @@ func (c *Cli) doCRunScript(name string, argsLine string, args ...string) {
func (c *Cli) doPRunScript(name string, argsLine string, args ...string) {
c.dorunscript(emParallel, argsLine)
}
func (c *Cli) doPassmgrDebug(name string, argsLine string, args ...string) {
passmgr.PrintDebug()
}

View File

@@ -5,6 +5,7 @@ import (
"plugin"
"github.com/viert/xc/log"
"github.com/viert/xc/term"
)
const (
@@ -14,6 +15,8 @@ const (
var (
p *plugin.Plugin
initialFilename string
initialOptions map[string]string
initialized bool
pluginInit func(map[string]string, func(string, ...interface{})) error
pluginAcquire func(string) string
@@ -22,6 +25,10 @@ var (
// Load loads a password manager library
func Load(filename string, options map[string]string) error {
var err error
initialFilename = filename
initialOptions = options
p, err = plugin.Open(filename)
if err != nil {
return err
@@ -63,3 +70,32 @@ func GetPass(hostname string) string {
func Ready() bool {
return initialized
}
// Reload reloads previously initialized plugin
func Reload() error {
term.Warnf("Reloading password manager from %s\n", initialFilename)
return Load(initialFilename, initialOptions)
}
// PrintDebug proxies a call to plugin's PrintDebug function if it exists
func PrintDebug() {
if !initialized {
term.Errorf("password manager is not initialized\n")
return
}
printsym, err := p.Lookup("PrintDebug")
if err != nil {
term.Errorf("the password manager doesn't have PrintDebug() handler\n")
return
}
printfunc, ok := printsym.(func())
if !ok {
term.Errorf("the passwordd manager PrintDebug() handler has invalid signature (must be func())\n")
return
}
term.Warnf("running password manager PrintDebug()\n")
printfunc()
fmt.Println()
}