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-01 20:01:48 +1000
commitf7ea62a01191a0f5e7bf64e2c618689c260eb19b (patch)
tree4ac3186ad46bdae15f4db1b7d6c994fbeb03c3bc
parent5d8d00fb7139612cbab9a3c1b0187816302d7d4a (diff)
downloadgitlab-shell-f7ea62a01191a0f5e7bf64e2c618689c260eb19b.tar.gz
Include SSL_CERT_DIR env var in command
-rw-r--r--internal/keyline/key_line.go36
-rw-r--r--internal/keyline/key_line_test.go44
2 files changed, 59 insertions, 21 deletions
diff --git a/internal/keyline/key_line.go b/internal/keyline/key_line.go
index c29a320..ce1d4e9 100644
--- a/internal/keyline/key_line.go
+++ b/internal/keyline/key_line.go
@@ -21,32 +21,46 @@ 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
+ RootDir string
+ SslCertDir string
}
-func NewPublicKeyLine(id string, publicKey string, rootDir string) (*KeyLine, error) {
- return newKeyLine(id, publicKey, PublicKeyPrefix, rootDir)
+func NewPublicKeyLine(id, publicKey, rootDir, sslCertDir string) (*KeyLine, error) {
+ return newKeyLine(id, publicKey, PublicKeyPrefix, rootDir, sslCertDir)
}
-func NewPrincipalKeyLine(keyId string, principal string, rootDir string) (*KeyLine, error) {
- return newKeyLine(keyId, principal, PrincipalPrefix, rootDir)
+func NewPrincipalKeyLine(keyId, principal, rootDir, sslCertDir string) (*KeyLine, error) {
+ return newKeyLine(keyId, principal, PrincipalPrefix, rootDir, sslCertDir)
}
func (k *KeyLine) ToString() string {
+ sslCertDirEnvVar := k.sslCertDirEnvVar()
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)
+ 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.SslCertDir != "" {
+ return fmt.Sprintf(`SSL_CERT_DIR=%s`, k.SslCertDir)
+ }
+
+ return ""
}
-func newKeyLine(id string, value string, prefix string, rootDir string) (*KeyLine, error) {
+func newKeyLine(id, value, prefix, rootDir, sslCertDir string) (*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, RootDir: rootDir, SslCertDir: sslCertDir}, 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..7652135 100644
--- a/internal/keyline/key_line_test.go
+++ b/internal/keyline/key_line_test.go
@@ -29,7 +29,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, "root-dir", "ssl-cert-dir")
require.Empty(t, result)
require.EqualError(t, err, tc.expectedError)
@@ -60,7 +60,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, "root-dir", "ssl-cert-dir")
require.Empty(t, result)
require.EqualError(t, err, tc.expectedError)
@@ -69,14 +69,38 @@ 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",
+ 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",
+ 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)
+ })
+ }
}