summaryrefslogtreecommitdiff
path: root/internal/keyline
diff options
context:
space:
mode:
authorNick Thomas <nick@gitlab.com>2019-10-17 12:04:52 +0100
committerNick Thomas <nick@gitlab.com>2019-10-18 11:47:25 +0100
commit83d11f4deeb20b852a0af3433190a0f7250a0027 (patch)
tree1a9df18d6f9f59712c6f5c98e995a4918eb94a11 /internal/keyline
parent7d5229db263a62661653431881bef8b46984d0de (diff)
downloadgitlab-shell-83d11f4deeb20b852a0af3433190a0f7250a0027.tar.gz
Move go code up one level
Diffstat (limited to 'internal/keyline')
-rw-r--r--internal/keyline/key_line.go62
-rw-r--r--internal/keyline/key_line_test.go82
2 files changed, 144 insertions, 0 deletions
diff --git a/internal/keyline/key_line.go b/internal/keyline/key_line.go
new file mode 100644
index 0000000..f92f50b
--- /dev/null
+++ b/internal/keyline/key_line.go
@@ -0,0 +1,62 @@
+package keyline
+
+import (
+ "errors"
+ "fmt"
+ "path"
+ "regexp"
+ "strings"
+
+ "gitlab.com/gitlab-org/gitlab-shell/go/internal/executable"
+)
+
+var (
+ keyRegex = regexp.MustCompile(`\A[a-z0-9-]+\z`)
+)
+
+const (
+ PublicKeyPrefix = "key"
+ PrincipalPrefix = "username"
+ SshOptions = "no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty"
+)
+
+type KeyLine struct {
+ Id string // This can be either an ID of a Key or username
+ Value string // This can be either a public key or a principal name
+ Prefix string
+ RootDir string
+}
+
+func NewPublicKeyLine(id string, publicKey string, rootDir string) (*KeyLine, error) {
+ return newKeyLine(id, publicKey, PublicKeyPrefix, rootDir)
+}
+
+func NewPrincipalKeyLine(keyId string, principal string, rootDir string) (*KeyLine, error) {
+ return newKeyLine(keyId, principal, PrincipalPrefix, rootDir)
+}
+
+func (k *KeyLine) ToString() string {
+ command := fmt.Sprintf("%s %s-%s", path.Join(k.RootDir, executable.BinDir, executable.GitlabShell), k.Prefix, k.Id)
+
+ return fmt.Sprintf(`command="%s",%s %s`, command, SshOptions, k.Value)
+}
+
+func newKeyLine(id string, value string, prefix string, rootDir string) (*KeyLine, error) {
+ if err := validate(id, value); err != nil {
+ return nil, err
+ }
+
+ return &KeyLine{Id: id, Value: value, Prefix: prefix, RootDir: rootDir}, nil
+}
+
+func validate(id string, value string) error {
+ if !keyRegex.MatchString(id) {
+ return errors.New(fmt.Sprintf("Invalid key_id: %s", id))
+ }
+
+ if strings.Contains(value, "\n") {
+ return errors.New(fmt.Sprintf("Invalid value: %s", value))
+ }
+
+ return nil
+}
diff --git a/internal/keyline/key_line_test.go b/internal/keyline/key_line_test.go
new file mode 100644
index 0000000..c6883c0
--- /dev/null
+++ b/internal/keyline/key_line_test.go
@@ -0,0 +1,82 @@
+package keyline
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestFailingNewPublicKeyLine(t *testing.T) {
+ testCases := []struct {
+ desc string
+ id string
+ publicKey string
+ expectedError string
+ }{
+ {
+ desc: "When Id has non-alphanumeric and non-dash characters in it",
+ id: "key\n1",
+ publicKey: "public-key",
+ expectedError: "Invalid key_id: key\n1",
+ },
+ {
+ desc: "When public key has newline in it",
+ id: "key",
+ publicKey: "public\nkey",
+ expectedError: "Invalid value: public\nkey",
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.desc, func(t *testing.T) {
+ result, err := NewPublicKeyLine(tc.id, tc.publicKey, "root-dir")
+
+ require.Empty(t, result)
+ require.EqualError(t, err, tc.expectedError)
+ })
+ }
+}
+
+func TestFailingNewPrincipalKeyLine(t *testing.T) {
+ testCases := []struct {
+ desc string
+ keyId string
+ principal string
+ expectedError string
+ }{
+ {
+ desc: "When username has non-alphanumeric and non-dash characters in it",
+ keyId: "username\n1",
+ principal: "principal",
+ expectedError: "Invalid key_id: username\n1",
+ },
+ {
+ desc: "When principal has newline in it",
+ keyId: "username",
+ principal: "principal\n1",
+ expectedError: "Invalid value: principal\n1",
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.desc, func(t *testing.T) {
+ result, err := NewPrincipalKeyLine(tc.keyId, tc.principal, "root-dir")
+
+ require.Empty(t, result)
+ require.EqualError(t, err, tc.expectedError)
+ })
+ }
+}
+
+func TestToString(t *testing.T) {
+ keyLine := &KeyLine{
+ Id: "1",
+ Value: "public-key",
+ Prefix: "key",
+ RootDir: "/tmp",
+ }
+
+ result := keyLine.ToString()
+
+ require.Equal(t, `command="/tmp/bin/gitlab-shell key-1",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty public-key`, result)
+}