Restructured the SSH key pair's description.

Now include the key pair name (if any), and make it more "readable".
This commit is contained in:
Stephen Fox 2019-02-05 13:37:09 -05:00
parent e40aa515f2
commit 72b7d63a6a
2 changed files with 22 additions and 13 deletions

View File

@ -20,10 +20,10 @@ const (
defaultRsaBits = 4096
// Rsa is a SSH key pair of RSA type.
Rsa KeyPairType = "rsa"
Rsa KeyPairType = "RSA"
// Ecdsa is a SSH key pair of ECDSA type.
Ecdsa KeyPairType = "ecdsa"
Ecdsa KeyPairType = "ECDSA"
)
// KeyPairType represents different types of SSH key pairs.
@ -248,10 +248,20 @@ func rawPemBlock(block *pem.Block) ([]byte, error) {
return buffer.Bytes(), nil
}
// TODO: Key pair name.
// description returns a string describing a key pair.
func description(kp KeyPair) string {
return kp.Type().String() + " " + strconv.Itoa(kp.Bits())
buffer := bytes.NewBuffer(nil)
buffer.WriteString(strconv.Itoa(kp.Bits()))
buffer.WriteString(" bit ")
buffer.WriteString(kp.Type().String())
if len(kp.Name()) > 0 {
buffer.WriteString(" named ")
buffer.WriteString(kp.Name())
}
return buffer.String()
}
// publicKeyAuthorizedKeysLine returns a slice of bytes representing a SSH

View File

@ -52,10 +52,9 @@ func (o expected) matches(kp KeyPair) error {
"' - got '" + kp.Name() + "'")
}
expDescription := kp.Type().String() + " " + strconv.Itoa(o.bits)
if kp.Description() != expDescription {
if kp.Description() != o.desc {
return errors.New("key pair description should be '" +
expDescription + "' - got '" + kp.Description() + "'")
o.desc + "' - got '" + kp.Description() + "'")
}
err := o.verifyPublicKeyAuthorizedKeysFormat(kp)
@ -149,7 +148,7 @@ func TestDefaultKeyPairBuilder_Build_Default(t *testing.T) {
err = expected{
kind: Ecdsa,
bits: 521,
desc: "ecdsa 521",
desc: "521 bit ECDSA",
data: []byte(uuid.TimeOrderedUUID()),
}.matches(kp)
if err != nil {
@ -168,7 +167,7 @@ func TestDefaultKeyPairBuilder_Build_EcdsaDefault(t *testing.T) {
err = expected{
kind: Ecdsa,
bits: 521,
desc: "ecdsa 521",
desc: "521 bit ECDSA",
data: []byte(uuid.TimeOrderedUUID()),
}.matches(kp)
if err != nil {
@ -195,7 +194,7 @@ func TestDefaultKeyPairBuilder_Build_EcdsaSupportedCurves(t *testing.T) {
err = expected{
kind: Ecdsa,
bits: bits,
desc: "ecdsa " + strconv.Itoa(bits),
desc: strconv.Itoa(bits) + " bit ECDSA",
data: []byte(uuid.TimeOrderedUUID()),
}.matches(kp)
if err != nil {
@ -215,7 +214,7 @@ func TestDefaultKeyPairBuilder_Build_RsaDefault(t *testing.T) {
err = expected{
kind: Rsa,
bits: 4096,
desc: "rsa 4096",
desc: "4096 bit RSA",
data: []byte(uuid.TimeOrderedUUID()),
}.matches(kp)
if err != nil {
@ -237,7 +236,7 @@ func TestDefaultKeyPairBuilder_Build_NamedEcdsa(t *testing.T) {
err = expected{
kind: Ecdsa,
bits: 521,
desc: "ecdsa 521",
desc: "521 bit ECDSA named " + name,
data: []byte(uuid.TimeOrderedUUID()),
name: name,
}.matches(kp)
@ -260,7 +259,7 @@ func TestDefaultKeyPairBuilder_Build_NamedRsa(t *testing.T) {
err = expected{
kind: Rsa,
bits: 4096,
desc: "rsa 4096",
desc: "4096 bit RSA named " + name,
data: []byte(uuid.TimeOrderedUUID()),
name: name,
}.matches(kp)