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