summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAsh McKenzie <amckenzie@gitlab.com>2020-07-01 20:01:48 +1000
committerAsh McKenzie <amckenzie@gitlab.com>2020-07-02 17:40:22 +1000
commitd32959e399ff8770e67abeb80fa83cdd3c52fde9 (patch)
tree040bb403d5f965ea2f9a3a8d131e5ba760a70d4e
parent5d8d00fb7139612cbab9a3c1b0187816302d7d4a (diff)
downloadgitlab-shell-d32959e399ff8770e67abeb80fa83cdd3c52fde9.tar.gz
Include SSL_CERT_DIR env var in command
-rw-r--r--internal/keyline/key_line.go38
-rw-r--r--internal/keyline/key_line_test.go44
2 files changed, 60 insertions, 22 deletions
diff --git a/internal/keyline/key_line.go b/internal/keyline/key_line.go
index c29a320..e2abb82 100644
--- a/internal/keyline/key_line.go
+++ b/internal/keyline/key_line.go
@@ -7,6 +7,7 @@ import (
"regexp"
"strings"
+ "gitlab.com/gitlab-org/gitlab-shell/internal/config"
"gitlab.com/gitlab-org/gitlab-shell/internal/executable"
)
@@ -21,32 +22,45 @@ const (
)
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
+ 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
+ Config *config.Config
}
-func NewPublicKeyLine(id string, publicKey string, rootDir string) (*KeyLine, error) {
- return newKeyLine(id, publicKey, PublicKeyPrefix, rootDir)
+func NewPublicKeyLine(id, publicKey string, config *config.Config) (*KeyLine, error) {
+ return newKeyLine(id, publicKey, PublicKeyPrefix, config)
}
-func NewPrincipalKeyLine(keyId string, principal string, rootDir string) (*KeyLine, error) {
- return newKeyLine(keyId, principal, PrincipalPrefix, rootDir)
+func NewPrincipalKeyLine(keyId, principal string, config *config.Config) (*KeyLine, error) {
+ return newKeyLine(keyId, principal, PrincipalPrefix, config)
}
func (k *KeyLine) ToString() string {
- command := fmt.Sprintf("%s %s-%s", path.Join(k.RootDir, executable.BinDir, executable.GitlabShell), k.Prefix, k.Id)
+ sslCertDirEnvVar := k.sslCertDirEnvVar()
+ command := fmt.Sprintf("%s %s-%s", path.Join(k.Config.RootDir, executable.BinDir, executable.GitlabShell), k.Prefix, k.Id)
- return fmt.Sprintf(`command="%s",%s %s`, command, SshOptions, k.Value)
+ if sslCertDirEnvVar != "" {
+ sslCertDirEnvVar = fmt.Sprintf(`%s `, sslCertDirEnvVar)
+ }
+
+ return fmt.Sprintf(`command="%s%s",%s %s`, sslCertDirEnvVar, command, SshOptions, k.Value)
+}
+
+func (k *KeyLine) sslCertDirEnvVar() string {
+ if k.Config.SslCertDir != "" {
+ return fmt.Sprintf(`SSL_CERT_DIR=%s`, k.Config.SslCertDir)
+ }
+
+ return ""
}
-func newKeyLine(id string, value string, prefix string, rootDir string) (*KeyLine, error) {
+func newKeyLine(id, value, prefix string, config *config.Config) (*KeyLine, error) {
if err := validate(id, value); err != nil {
return nil, err
}
- return &KeyLine{Id: id, Value: value, Prefix: prefix, RootDir: rootDir}, nil
+ return &KeyLine{Id: id, Value: value, Prefix: prefix, Config: config}, nil
}
func validate(id string, value string) error {
diff --git a/internal/keyline/key_line_test.go b/internal/keyline/key_line_test.go
index c6883c0..095de78 100644
--- a/internal/keyline/key_line_test.go
+++ b/internal/keyline/key_line_test.go
@@ -4,6 +4,7 @@ import (
"testing"
"github.com/stretchr/testify/require"
+ "gitlab.com/gitlab-org/gitlab-shell/internal/config"
)
func TestFailingNewPublicKeyLine(t *testing.T) {
@@ -29,7 +30,7 @@ func TestFailingNewPublicKeyLine(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
- result, err := NewPublicKeyLine(tc.id, tc.publicKey, "root-dir")
+ result, err := NewPublicKeyLine(tc.id, tc.publicKey, &config.Config{RootDir: "/tmp", SslCertDir: "/tmp/certs"})
require.Empty(t, result)
require.EqualError(t, err, tc.expectedError)
@@ -60,7 +61,7 @@ func TestFailingNewPrincipalKeyLine(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
- result, err := NewPrincipalKeyLine(tc.keyId, tc.principal, "root-dir")
+ result, err := NewPrincipalKeyLine(tc.keyId, tc.principal, &config.Config{RootDir: "/tmp", SslCertDir: "/tmp/certs"})
require.Empty(t, result)
require.EqualError(t, err, tc.expectedError)
@@ -69,14 +70,37 @@ func TestFailingNewPrincipalKeyLine(t *testing.T) {
}
func TestToString(t *testing.T) {
- keyLine := &KeyLine{
- Id: "1",
- Value: "public-key",
- Prefix: "key",
- RootDir: "/tmp",
+ testCases := []struct {
+ desc string
+ keyLine *KeyLine
+ expectedOutput string
+ }{
+ {
+ desc: "Without SSL cert dir",
+ keyLine: &KeyLine{
+ Id: "1",
+ Value: "public-key",
+ Prefix: "key",
+ Config: &config.Config{RootDir: "/tmp"},
+ },
+ expectedOutput: `command="/tmp/bin/gitlab-shell key-1",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty public-key`,
+ },
+ {
+ desc: "With SSL cert dir",
+ keyLine: &KeyLine{
+ Id: "1",
+ Value: "public-key",
+ Prefix: "key",
+ Config: &config.Config{RootDir: "/tmp", SslCertDir: "/tmp/certs"},
+ },
+ expectedOutput: `command="SSL_CERT_DIR=/tmp/certs /tmp/bin/gitlab-shell key-1",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty public-key`,
+ },
}
- 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)
+ for _, tc := range testCases {
+ t.Run(tc.desc, func(t *testing.T) {
+ result := tc.keyLine.ToString()
+ require.Equal(t, tc.expectedOutput, result)
+ })
+ }
}