tofu: Fix known host unmarshaling
This commit is contained in:
parent
6e5c2473e7
commit
a5493b708a
29
tofu/tofu.go
29
tofu/tofu.go
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user