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
|
package server
import (
"context"
"fmt"
"regexp"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/gitaly"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/gitaly/vendored/gitalypb"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/module/agent_configuration"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/module/configuration_project/rpc"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/module/modshared"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/git"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/logz"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const (
// https://gitlab.com/gitlab-org/cluster-integration/gitlab-agent/-/blob/master/doc/identity_and_auth.md#agent-identity-and-name
agentNameRegex = `[a-z0-9](?:[-a-z0-9]*[a-z0-9])?`
)
var (
agentConfigFileRegex = regexp.MustCompile(fmt.Sprintf("^%s/(%s)/%s$",
regexp.QuoteMeta(agent_configuration.Directory),
agentNameRegex,
regexp.QuoteMeta(agent_configuration.FileName),
))
)
type server struct {
rpc.UnimplementedConfigurationProjectServer
gitaly gitaly.PoolInterface
}
func (s *server) ListAgentConfigFiles(ctx context.Context, req *rpc.ListAgentConfigFilesRequest) (*rpc.ListAgentConfigFilesResponse, error) {
rpcAPI := modshared.RPCAPIFromContext(ctx)
pf, err := s.gitaly.PathFetcher(ctx, req.GitalyInfo)
if err != nil {
rpcAPI.HandleProcessingError(rpcAPI.Log(), modshared.NoAgentID, "PathFetcher", err)
return nil, status.Errorf(codes.Unavailable, "PathFetcher: %v", err)
}
v := &configVisitor{}
ref := git.ExplicitRefOrHead(req.DefaultBranch)
err = pf.Visit(ctx, req.Repository.ToGitalyRepository(), []byte(ref), []byte(agent_configuration.Directory), true, v)
if err != nil {
forwardErr := func() {
log := rpcAPI.Log().With(logz.ProjectID(req.Repository.GlProjectPath))
rpcAPI.HandleProcessingError(log, modshared.NoAgentID, "PathFetcher", err)
}
switch gitaly.ErrorCodeFromError(err) { //nolint:exhaustive
case gitaly.InvalidArgument:
// We send this to Sentry anyway, because it shouldn't normally happen in this method.
forwardErr()
return nil, status.Errorf(codes.InvalidArgument, "PathFetcher: %v", err)
case gitaly.NotFound:
return &rpc.ListAgentConfigFilesResponse{}, nil
default:
forwardErr()
return nil, status.Errorf(codes.Unavailable, "PathFetcher: %v", err)
}
}
return &rpc.ListAgentConfigFilesResponse{
ConfigFiles: v.resp,
}, nil
}
type configVisitor struct {
resp []*rpc.AgentConfigFile
}
func (c *configVisitor) Entry(entry *gitalypb.TreeEntry) (bool /* download? */, int64 /* max size */, error) {
submatch := agentConfigFileRegex.FindSubmatch(entry.Path)
if submatch == nil {
return false, 0, nil
}
c.resp = append(c.resp, &rpc.AgentConfigFile{
Name: string(entry.Path),
AgentName: string(submatch[1]),
})
return false, 0, nil
}
func (c *configVisitor) StreamChunk(path []byte, data []byte) (bool, error) {
return false, nil
}
func (c *configVisitor) EntryDone(entry *gitalypb.TreeEntry, err error) {}
|