21416: Add comments to try and explain the options related to email better
[arvados.git] / lib / config / load.go
index 9269ddf27f59011b5dad2855edde8fb9b676ed41..d504f7796c323f12598a0af4429e289390e22e52 100644 (file)
@@ -26,6 +26,7 @@ import (
        "github.com/imdario/mergo"
        "github.com/prometheus/client_golang/prometheus"
        "github.com/sirupsen/logrus"
+       "golang.org/x/crypto/ssh"
        "golang.org/x/sys/unix"
 )
 
@@ -690,3 +691,17 @@ func (ldr *Loader) RegisterMetrics(reg *prometheus.Registry) {
        vec.WithLabelValues(hash).Set(float64(ldr.loadTimestamp.UnixNano()) / 1e9)
        reg.MustRegister(vec)
 }
+
+// Load an SSH private key from the given confvalue, which is either
+// the literal key or an absolute path to a file containing the key.
+func LoadSSHKey(confvalue string) (ssh.Signer, error) {
+       if fnm := strings.TrimPrefix(confvalue, "file://"); fnm != confvalue && strings.HasPrefix(fnm, "/") {
+               keydata, err := os.ReadFile(fnm)
+               if err != nil {
+                       return nil, err
+               }
+               return ssh.ParsePrivateKey(keydata)
+       } else {
+               return ssh.ParsePrivateKey([]byte(confvalue))
+       }
+}