diff options
author | Stan Hu <stanhu@gmail.com> | 2020-09-19 03:34:49 -0700 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2020-09-19 14:00:45 -0700 |
commit | 0590d9198f653ff2170e0f26790056bef4f056fe (patch) | |
tree | dc0d68866ea16ba4f74d441c3aa2048b12fb9e95 /internal/command/lfsauthenticate | |
parent | f100e7e83943b3bb5db232f5bf79a616fdba88f1 (diff) | |
download | gitlab-shell-sh-extract-context-from-env.tar.gz |
Make it possible to propagate correlation ID across processessh-extract-context-from-env
Previously, gitlab-shell did not pass a context through the application.
Correlation IDs were generated down the call stack, since we don't pass
the context around from the start execution.
This has several potential downsides:
1. It's easier for programming mistakes to be made in future which lead
to multiple correlation IDs being generated for a single request.
2. Correlation IDs cannot be passed in from upstream requests
3. Other advantages of context passing, such as distributed tracing is
not possible.
This commit changes the behavior:
1. Extract the correlation ID from the environment at the start of
the application.
2. If no correlation ID exists, generate a random one.
3. Pass the correlation ID to the GitLabNet API requests.
This change also enables other clients of GitLabNet (e.g. Gitaly) to
pass along the correlation ID in the internal API requests
(https://gitlab.com/gitlab-org/gitaly/-/issues/2725).
Fixes https://gitlab.com/gitlab-org/gitlab-shell/-/issues/474
Diffstat (limited to 'internal/command/lfsauthenticate')
-rw-r--r-- | internal/command/lfsauthenticate/lfsauthenticate.go | 15 | ||||
-rw-r--r-- | internal/command/lfsauthenticate/lfsauthenticate_test.go | 5 |
2 files changed, 11 insertions, 9 deletions
diff --git a/internal/command/lfsauthenticate/lfsauthenticate.go b/internal/command/lfsauthenticate/lfsauthenticate.go index 2aaac2a..dab69ab 100644 --- a/internal/command/lfsauthenticate/lfsauthenticate.go +++ b/internal/command/lfsauthenticate/lfsauthenticate.go @@ -1,6 +1,7 @@ package lfsauthenticate import ( + "context" "encoding/base64" "encoding/json" "fmt" @@ -34,7 +35,7 @@ type Payload struct { ExpiresIn int `json:"expires_in,omitempty"` } -func (c *Command) Execute() error { +func (c *Command) Execute(ctx context.Context) error { args := c.Args.SshArgs if len(args) < 3 { return disallowedcommand.Error @@ -49,12 +50,12 @@ func (c *Command) Execute() error { return err } - accessResponse, err := c.verifyAccess(action, repo) + accessResponse, err := c.verifyAccess(ctx, action, repo) if err != nil { return err } - payload, err := c.authenticate(operation, repo, accessResponse.UserId) + payload, err := c.authenticate(ctx, operation, repo, accessResponse.UserId) if err != nil { // return nothing just like Ruby's GitlabShell#lfs_authenticate does return nil @@ -80,19 +81,19 @@ func actionFromOperation(operation string) (commandargs.CommandType, error) { return action, nil } -func (c *Command) verifyAccess(action commandargs.CommandType, repo string) (*accessverifier.Response, error) { +func (c *Command) verifyAccess(ctx context.Context, action commandargs.CommandType, repo string) (*accessverifier.Response, error) { cmd := accessverifier.Command{c.Config, c.Args, c.ReadWriter} - return cmd.Verify(action, repo) + return cmd.Verify(ctx, action, repo) } -func (c *Command) authenticate(operation string, repo, userId string) ([]byte, error) { +func (c *Command) authenticate(ctx context.Context, operation string, repo, userId string) ([]byte, error) { client, err := lfsauthenticate.NewClient(c.Config, c.Args) if err != nil { return nil, err } - response, err := client.Authenticate(operation, repo, userId) + response, err := client.Authenticate(ctx, operation, repo, userId) if err != nil { return nil, err } diff --git a/internal/command/lfsauthenticate/lfsauthenticate_test.go b/internal/command/lfsauthenticate/lfsauthenticate_test.go index a1c7aec..55998ab 100644 --- a/internal/command/lfsauthenticate/lfsauthenticate_test.go +++ b/internal/command/lfsauthenticate/lfsauthenticate_test.go @@ -2,6 +2,7 @@ package lfsauthenticate import ( "bytes" + "context" "encoding/json" "io/ioutil" "net/http" @@ -54,7 +55,7 @@ func TestFailedRequests(t *testing.T) { ReadWriter: &readwriter.ReadWriter{ErrOut: output, Out: output}, } - err := cmd.Execute() + err := cmd.Execute(context.Background()) require.Error(t, err) require.Equal(t, tc.expectedOutput, err.Error()) @@ -146,7 +147,7 @@ func TestLfsAuthenticateRequests(t *testing.T) { ReadWriter: &readwriter.ReadWriter{ErrOut: output, Out: output}, } - err := cmd.Execute() + err := cmd.Execute(context.Background()) require.NoError(t, err) require.Equal(t, tc.expectedOutput, output.String()) |