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
|
package server
import (
"context"
"fmt"
"regexp"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/api"
"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/modserver"
"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 := modserver.RpcApiFromContext(ctx)
pf, err := s.gitaly.PathFetcher(ctx, &api.GitalyInfo{
Address: req.GitalyAddress.Address,
Token: req.GitalyAddress.Token,
//Features: nil, // TODO
})
if err != nil {
rpcApi.HandleProcessingError(rpcApi.Log(), modshared.NoAgentId, "PathFetcher", err)
return nil, status.Error(codes.Unavailable, "Unavailable")
}
r := &gitalypb.Repository{
StorageName: req.Repository.StorageName,
RelativePath: req.Repository.RelativePath,
GitObjectDirectory: req.Repository.GitObjectDirectory,
GitAlternateObjectDirectories: req.Repository.GitAlternateObjectDirectories,
GlRepository: req.Repository.GlRepository,
GlProjectPath: req.Repository.GlProjectPath,
}
v := &configVisitor{}
ref := git.ExplicitRefOrHead(req.DefaultBranch)
err = pf.Visit(ctx, r, []byte(ref), []byte(agent_configuration.Directory), true, v)
if err != nil {
log := rpcApi.Log().With(logz.ProjectId(req.Repository.GlProjectPath))
rpcApi.HandleProcessingError(log, modshared.NoAgentId, "PathFetcher", err)
return nil, status.Error(codes.Unavailable, "Unavailable")
}
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) {}
|