tofu: Fix known host unmarshaling

This commit is contained in:
Adnan Maolood 2021-03-06 15:48:51 -05:00
parent 6e5c2473e7
commit a5493b708a

View File

@ -7,7 +7,6 @@ import (
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"errors"
"fmt"
"io"
"os"
@ -132,6 +131,9 @@ func (k *KnownHosts) Parse(r io.Reader) error {
if err != nil {
continue
}
if h.Algorithm != "sha256" {
continue
}
k.hosts[h.Hostname] = h
}
@ -317,32 +319,13 @@ func (h Host) String() string {
// UnmarshalText unmarshals the host from the provided text.
func (h *Host) UnmarshalText(text []byte) error {
const format = "hostname algorithm fingerprint"
parts := bytes.Split(text, []byte(" "))
if len(parts) != 3 {
return fmt.Errorf("expected the format %q", format)
}
if len(parts[0]) == 0 {
return errors.New("empty hostname")
return fmt.Errorf("expected the format 'hostname algorithm fingerprint'")
}
h.Hostname = string(parts[0])
algorithm := string(parts[1])
if algorithm != "sha256" {
return fmt.Errorf("unsupported algorithm %q", algorithm)
}
h.Algorithm = algorithm
fingerprint, err := base64.StdEncoding.DecodeString(string(parts[2]))
if err != nil {
return err
}
h.Fingerprint = string(fingerprint)
h.Algorithm = string(parts[1])
h.Fingerprint = string(parts[2])
return nil
}