diff options
author | Igor Wiedler <iwiedler@gitlab.com> | 2020-11-19 14:30:30 +0100 |
---|---|---|
committer | Igor Wiedler <iwiedler@gitlab.com> | 2020-11-19 14:30:30 +0100 |
commit | cd3129c383d02aadbd8703a0483e85b444072205 (patch) | |
tree | f3b6490ff9a29d265b05fa6b4bcf205f290c3f14 | |
parent | f9384a90497bedc0002633b21076336c29a2c406 (diff) | |
download | gitlab-shell-cd3129c383d02aadbd8703a0483e85b444072205.tar.gz |
test for client identity propagation
-rw-r--r-- | internal/handler/exec_test.go | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/internal/handler/exec_test.go b/internal/handler/exec_test.go index ce672f4..326bfee 100644 --- a/internal/handler/exec_test.go +++ b/internal/handler/exec_test.go @@ -3,13 +3,16 @@ package handler import ( "context" "errors" + "os" "testing" "github.com/stretchr/testify/require" "google.golang.org/grpc" "google.golang.org/grpc/metadata" + pb "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "gitlab.com/gitlab-org/gitlab-shell/internal/config" + "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/accessverifier" ) func makeHandler(t *testing.T, err error) func(context.Context, *grpc.ClientConn) (int32, error) { @@ -83,3 +86,63 @@ func TestGetConnMetadata(t *testing.T) { }) } } + +func TestPrepareContext(t *testing.T) { + tests := []struct { + name string + gc *GitalyCommand + sshConnectionEnv string + repo *pb.Repository + response *accessverifier.Response + want map[string]string + }{ + { + name: "client_identity", + gc: &GitalyCommand{ + Config: &config.Config{}, + Address: "tcp://localhost:9999", + }, + sshConnectionEnv: "10.0.0.1 1234 127.0.0.1 5678", + repo: &pb.Repository{ + StorageName: "default", + RelativePath: "@hashed/5f/9c/5f9c4ab08cac7457e9111a30e4664920607ea2c115a1433d7be98e97e64244ca.git", + GitObjectDirectory: "path/to/git_object_directory", + GitAlternateObjectDirectories: []string{"path/to/git_alternate_object_directory"}, + GlRepository: "project-26", + GlProjectPath: "group/private", + }, + response: &accessverifier.Response{ + UserId: "6", + Username: "jane.doe", + }, + want: map[string]string{ + "remote_ip": "10.0.0.1", + "user_id": "6", + "username": "jane.doe", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctx := context.Background() + + orig := os.Getenv("SSH_CONNECTION") + defer os.Setenv("SSH_CONNECTION", orig) + os.Setenv("SSH_CONNECTION", tt.sshConnectionEnv) + + ctx, cancel := tt.gc.PrepareContext(ctx, tt.repo, tt.response, "protocol") + defer cancel() + + md, exists := metadata.FromOutgoingContext(ctx) + require.True(t, exists) + require.Equal(t, len(tt.want), md.Len()) + + for k, v := range tt.want { + values := md.Get(k) + require.Equal(t, 1, len(values)) + require.Equal(t, v, values[0]) + } + + }) + } +} |