1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
package command
import (
"context"
"fmt"
"os"
"path"
"strings"
"gitlab.com/gitlab-org/gitlab-shell/v14/internal/config"
"gitlab.com/gitlab-org/labkit/correlation"
"gitlab.com/gitlab-org/labkit/tracing"
)
type Command interface {
Execute(ctx context.Context) (context.Context, error)
}
type LogMetadata struct {
Project string `json:"project,omitempty"`
RootNamespace string `json:"root_namespace,omitempty"`
}
type LogData struct {
Username string `json:"username"`
WrittenBytes int64 `json:"written_bytes"`
Meta LogMetadata `json:"meta"`
}
func CheckForVersionFlag(osArgs []string, version, buildTime string) {
// We can't use the flag library because gitlab-shell receives other arguments
// that confuse the parser.
//
// See: https://gitlab.com/gitlab-org/gitlab-shell/-/merge_requests/800#note_1459474735
if len(osArgs) == 2 && osArgs[1] == "-version" {
fmt.Printf("%s %s-%s\n", path.Base(osArgs[0]), version, buildTime)
os.Exit(0)
}
}
// Setup() initializes tracing from the configuration file and generates a
// background context from which all other contexts in the process should derive
// from, as it has a service name and initial correlation ID set.
func Setup(serviceName string, config *config.Config) (context.Context, func()) {
closer := tracing.Initialize(
tracing.WithServiceName(serviceName),
// For GitLab-Shell, we explicitly initialize tracing from a config file
// instead of the default environment variable (using GITLAB_TRACING)
// This decision was made owing to the difficulty in passing environment
// variables into GitLab-Shell processes.
//
// Processes are spawned as children of the SSH daemon, which tightly
// controls environment variables; doing this means we don't have to
// enable PermitUserEnvironment
//
// gitlab-sshd could use the standard GITLAB_TRACING envvar, but that
// would lead to inconsistencies between the two forms of operation
tracing.WithConnectionString(config.GitlabTracing),
)
ctx, finished := tracing.ExtractFromEnv(context.Background())
ctx = correlation.ContextWithClientName(ctx, serviceName)
correlationID := correlation.ExtractFromContext(ctx)
if correlationID == "" {
correlationID := correlation.SafeRandomID()
ctx = correlation.ContextWithCorrelation(ctx, correlationID)
}
return ctx, func() {
finished()
closer.Close()
}
}
func NewLogData(project, username string) LogData {
rootNameSpace := ""
if len(project) > 0 {
splitFn := func(c rune) bool {
return c == '/'
}
m := strings.FieldsFunc(project, splitFn)
if len(m) > 0 {
rootNameSpace = m[0]
}
}
return LogData{
Username: username,
WrittenBytes: 0,
Meta: LogMetadata{
Project: project,
RootNamespace: rootNameSpace,
},
}
}
|