Skip to content

Commit

Permalink
less opinionated ssh-readiness check
Browse files Browse the repository at this point in the history
  • Loading branch information
femnad committed Dec 13, 2023
1 parent 7981923 commit ccb6a42
Showing 1 changed file with 27 additions and 56 deletions.
83 changes: 27 additions & 56 deletions precheck/fact.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ const (

var (
batteryDeviceRegex = regexp.MustCompile(batteryDevicePattern)
pkgMgrToOs = map[string][]string{
caps = map[string]func() (bool, error){
"laptop": isLaptop,
"ssh": sshReady,
}
pkgMgrToOs = map[string][]string{
"apt": {"debian", "ubuntu"},
"dnf": {"fedora"},
}
Expand All @@ -46,69 +50,18 @@ func isLaptop() (bool, error) {
return false, nil
}

func hasEnv(env string) (bool, error) {
val := os.Getenv(env)
return val != "", nil
}

func isOs(osId string) (bool, error) {
foundOsId, err := GetOsId()
if err != nil {
return false, fmt.Errorf("error getting OS ID %v", err)
}

return foundOsId == osId, nil
}

func sshReady() (bool, error) {
_, err := os.Stat(internal.ExpandUser(onepasswordSSHSocket))
if err == nil {
return true, nil
}

resp, err := marecmd.RunFormatError(marecmd.Input{Command: "ssh-add -l"})
if err != nil {
internal.Log.Debugf("error checking ssh-add output: %v", err)
return false, nil
}

output := strings.TrimSpace(resp.Stdout)
if output == "" {
return false, nil
}

for _, line := range strings.Split(output, "\n") {
fields := strings.Split(line, " ")
if len(fields) != 4 {
return false, fmt.Errorf("unexpected SSH agent output: %s", output)
}

hostname, err := os.Hostname()
if err != nil {
return false, err
}

// Third field could be <user>@<hostname> or <hostname>.
if strings.HasSuffix(fields[2], hostname) {
return true, nil
}
}

return false, nil
return hasOutput("ssh-add -l")
}

var caps = map[string]func() (bool, error){
"laptop": isLaptop,
"ssh": sshReady,
}

func isOk(cap string) (bool, error) {
capFn, ok := caps[cap]
if !ok {
return false, fmt.Errorf("no such capability check: %s", cap)
}

return capFn()
func hasEnv(env string) (bool, error) {
val := os.Getenv(env)
return val != "", nil
}

func hasOutput(cmd string) (bool, error) {
Expand Down Expand Up @@ -139,6 +92,24 @@ func hasPkgMgr(pkgMgr string) (bool, error) {
return false, nil
}

func isOk(cap string) (bool, error) {
capFn, ok := caps[cap]
if !ok {
return false, fmt.Errorf("no such capability check: %s", cap)
}

return capFn()
}

func isOs(osId string) (bool, error) {
foundOsId, err := GetOsId()
if err != nil {
return false, fmt.Errorf("error getting OS ID %v", err)
}

return foundOsId == osId, nil
}

var FactFns = template.FuncMap{
"env": hasEnv,
"pkg": hasPkgMgr,
Expand Down

0 comments on commit ccb6a42

Please sign in to comment.