Skip to content

Commit 34ce414

Browse files
author
Aaron Schlesinger
committed
ref(util,pkg/ssh,cmd/keys.go): move util package to pkg/ssh
1 parent 53d432e commit 34ce414

6 files changed

Lines changed: 56 additions & 56 deletions

File tree

cmd/keys.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/deis/workflow-cli/controller/api"
1111
"github.com/deis/workflow-cli/controller/client"
1212
"github.com/deis/workflow-cli/controller/models/keys"
13-
"github.com/deis/workflow-cli/util"
13+
"github.com/deis/workflow-cli/pkg/ssh"
1414
)
1515

1616
// KeysList lists a user's keys.
@@ -164,7 +164,7 @@ func getKey(filename string) (api.KeyCreateRequest, error) {
164164
}
165165

166166
backupID := strings.Split(path.Base(filename), ".")[0]
167-
keyInfo, err := util.ParseSSHPubKey(backupID, keyContents)
167+
keyInfo, err := ssh.ParsePubKey(backupID, keyContents)
168168
if err != nil {
169169
return api.KeyCreateRequest{}, fmt.Errorf("%s is not a valid ssh key", filename)
170170
}

pkg/ssh/doc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Package ssh contains SSH related utility functions that are used by both the CLI binary
2+
// and the client SDK
3+
package ssh

pkg/ssh/ssh.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package ssh
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
)
7+
8+
var (
9+
pubKeyRegex = regexp.MustCompile("^(ssh-...|ecdsa-[^ ]+) ([^ ]+) ?(.*)")
10+
)
11+
12+
// PubKeyInfo contains the information on an SSH public key
13+
type PubKeyInfo struct {
14+
ID string
15+
Public string
16+
}
17+
18+
// ErrInvalidPubKey is the error returned when an SSH public key is unrecognizable
19+
type ErrInvalidPubKey struct {
20+
pubKey []byte
21+
}
22+
23+
// Error is the error interface implementation
24+
func (e ErrInvalidPubKey) Error() string {
25+
return fmt.Sprintf("invalid SSH public key %s", string(e.pubKey))
26+
}
27+
28+
// ErrUnknownPubKeyID is the error returned when an SSH public key is not the expected format
29+
type ErrUnknownPubKeyID struct {
30+
pubKey []byte
31+
}
32+
33+
func (e ErrUnknownPubKeyID) Error() string {
34+
return fmt.Sprintf("unknown SSH public key ID for %s", string(e.pubKey))
35+
}
36+
37+
// ParsePubKey parses a byte slice representation of an SSH Public Key into an
38+
// SSHPubKeyInfo struct. If it cannot find the key ID from the pubKey byte slice itself,
39+
// it uses backupKeyID instead. Returns an appropriate error if parsing failed.
40+
func ParsePubKey(backupKeyID string, pubKey []byte) (*PubKeyInfo, error) {
41+
if !pubKeyRegex.Match(pubKey) {
42+
return nil, ErrInvalidPubKey{pubKey: pubKey}
43+
}
44+
capture := pubKeyRegex.FindStringSubmatch(string(pubKey))
45+
if len(capture) < 4 || capture[3] == "" {
46+
return &PubKeyInfo{ID: backupKeyID, Public: string(pubKey)}, nil
47+
}
48+
return &PubKeyInfo{ID: capture[3], Public: string(pubKey)}, nil
49+
}

util/ssh_test.go renamed to pkg/ssh/ssh_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package util
1+
package ssh
22

33
import (
44
"testing"
@@ -10,7 +10,7 @@ const (
1010
)
1111

1212
func TestParseSSHPubKey(t *testing.T) {
13-
info, err := ParseSSHPubKey(backupKeyID, []byte(pubKey))
13+
info, err := ParsePubKey(backupKeyID, []byte(pubKey))
1414
if err != nil {
1515
t.Fatalf("Error parsing well formed key (%s)", err)
1616
}

util/doc.go

Lines changed: 0 additions & 3 deletions
This file was deleted.

util/ssh.go

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)