Monorepo for Tangled
tangled.org
1package knotserver
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "log/slog"
8 "net/http"
9 "net/url"
10 "os"
11 "path/filepath"
12 "strings"
13
14 securejoin "github.com/cyphar/filepath-securejoin"
15 "github.com/go-chi/chi/v5"
16 "github.com/go-chi/chi/v5/middleware"
17 "github.com/go-git/go-git/v5/plumbing"
18 "tangled.org/core/api/tangled"
19 "tangled.org/core/hook"
20 "tangled.org/core/idresolver"
21 "tangled.org/core/knotserver/config"
22 "tangled.org/core/knotserver/db"
23 "tangled.org/core/knotserver/git"
24 "tangled.org/core/log"
25 "tangled.org/core/notifier"
26 "tangled.org/core/rbac"
27 "tangled.org/core/workflow"
28)
29
30type InternalHandle struct {
31 db *db.DB
32 c *config.Config
33 e *rbac.Enforcer
34 l *slog.Logger
35 n *notifier.Notifier
36 res *idresolver.Resolver
37}
38
39func (h *InternalHandle) PushAllowed(w http.ResponseWriter, r *http.Request) {
40 user := r.URL.Query().Get("user")
41 repo := r.URL.Query().Get("repo")
42
43 if user == "" || repo == "" {
44 w.WriteHeader(http.StatusBadRequest)
45 return
46 }
47
48 ok, err := h.e.IsPushAllowed(user, rbac.ThisServer, repo)
49 if err != nil || !ok {
50 w.WriteHeader(http.StatusForbidden)
51 return
52 }
53
54 w.WriteHeader(http.StatusNoContent)
55}
56
57func (h *InternalHandle) InternalKeys(w http.ResponseWriter, r *http.Request) {
58 keys, err := h.db.GetAllPublicKeys()
59 if err != nil {
60 writeError(w, err.Error(), http.StatusInternalServerError)
61 return
62 }
63
64 data := make([]map[string]interface{}, 0)
65 for _, key := range keys {
66 j := key.JSON()
67 data = append(data, j)
68 }
69 writeJSON(w, data)
70}
71
72// response in text/plain format
73// the body will be qualified repository path on success/push-denied
74// or an error message when process failed
75func (h *InternalHandle) Guard(w http.ResponseWriter, r *http.Request) {
76 l := h.l.With("handler", "Guard")
77
78 var (
79 incomingUser = r.URL.Query().Get("user")
80 repo = r.URL.Query().Get("repo")
81 gitCommand = r.URL.Query().Get("gitCmd")
82 )
83
84 if incomingUser == "" || repo == "" || gitCommand == "" {
85 w.WriteHeader(http.StatusBadRequest)
86 l.Error("invalid request", "incomingUser", incomingUser, "repo", repo, "gitCommand", gitCommand)
87 fmt.Fprintln(w, "invalid internal request")
88 return
89 }
90
91 components := strings.Split(strings.TrimPrefix(strings.Trim(repo, "'"), "/"), "/")
92 l.Info("command components", "components", components)
93
94 var rbacResource string
95 var diskRelative string
96
97 switch {
98 case len(components) == 1 && strings.HasPrefix(components[0], "did:"):
99 repoDid := components[0]
100 repoPath, _, _, lookupErr := h.db.ResolveRepoDIDOnDisk(h.c.Repo.ScanPath, repoDid)
101 if lookupErr != nil {
102 w.WriteHeader(http.StatusNotFound)
103 l.Error("repo DID not found", "repoDid", repoDid, "err", lookupErr)
104 fmt.Fprintln(w, "repo not found")
105 return
106 }
107 rbacResource = repoDid
108 rel, relErr := filepath.Rel(h.c.Repo.ScanPath, repoPath)
109 if relErr != nil {
110 w.WriteHeader(http.StatusInternalServerError)
111 l.Error("failed to compute relative path", "repoPath", repoPath, "err", relErr)
112 fmt.Fprintln(w, "internal error")
113 return
114 }
115 diskRelative = rel
116
117 case len(components) == 2:
118 repoOwner := components[0]
119 ownerIdent, resolveErr := h.res.ResolveAtIdentifier(r.Context(), repoOwner)
120 if resolveErr != nil {
121 l.Error("error resolving owner", "owner", repoOwner, "err", resolveErr)
122 w.WriteHeader(http.StatusInternalServerError)
123 fmt.Fprintf(w, "error resolving owner: invalid did or handle\n")
124 return
125 }
126 ownerDid := ownerIdent.DID
127 repoName := components[1]
128 repoDid, didErr := h.db.GetRepoDid(ownerDid.String(), repoName)
129 var repoPath string
130 if didErr == nil {
131 var lookupErr error
132 repoPath, _, _, lookupErr = h.db.ResolveRepoDIDOnDisk(h.c.Repo.ScanPath, repoDid)
133 if lookupErr != nil {
134 w.WriteHeader(http.StatusNotFound)
135 l.Error("repo not found on disk", "repoDid", repoDid, "err", lookupErr)
136 fmt.Fprintln(w, "repo not found")
137 return
138 }
139 rbacResource = repoDid
140 } else {
141 legacyPath, joinErr := securejoin.SecureJoin(h.c.Repo.ScanPath, filepath.Join(ownerDid.String(), repoName))
142 if joinErr != nil {
143 w.Header().Set("Content-Type", "text/plain; charset=UTF-8")
144 w.WriteHeader(http.StatusNotFound)
145 fmt.Fprint(w, "repo not found\n")
146 return
147 }
148 if _, statErr := os.Stat(legacyPath); statErr != nil {
149 l.Info("legacy repo path missing, checking rename history", "owner", ownerDid, "name", repoName)
150 w.Header().Set("Content-Type", "text/plain; charset=UTF-8")
151 w.WriteHeader(http.StatusNotFound)
152 fmt.Fprint(w, "repo not found\n")
153 return
154 }
155 repoPath = legacyPath
156 rbacResource = ownerDid.String() + "/" + repoName
157 }
158 rel, relErr := filepath.Rel(h.c.Repo.ScanPath, repoPath)
159 if relErr != nil {
160 w.WriteHeader(http.StatusInternalServerError)
161 l.Error("failed to compute relative path", "repoPath", repoPath, "err", relErr)
162 fmt.Fprintln(w, "internal error")
163 return
164 }
165 diskRelative = rel
166
167 default:
168 w.WriteHeader(http.StatusBadRequest)
169 l.Error("invalid repo format", "components", components)
170 fmt.Fprintln(w, "invalid repo format, needs <user>/<repo>, /<user>/<repo>, or <repo-did>")
171 return
172 }
173
174 if gitCommand == "git-receive-pack" {
175 ok, err := h.e.IsPushAllowed(incomingUser, rbac.ThisServer, rbacResource)
176 if err != nil || !ok {
177 w.WriteHeader(http.StatusForbidden)
178 fmt.Fprint(w, repo)
179 return
180 }
181 }
182
183 w.WriteHeader(http.StatusOK)
184 fmt.Fprint(w, diskRelative)
185}
186
187type PushOptions struct {
188 skipCi bool
189 verboseCi bool
190}
191
192func (h *InternalHandle) PostReceiveHook(w http.ResponseWriter, r *http.Request) {
193 l := h.l.With("handler", "PostReceiveHook")
194
195 gitAbsoluteDir := r.Header.Get("X-Git-Dir")
196 gitRelativeDir, err := filepath.Rel(h.c.Repo.ScanPath, gitAbsoluteDir)
197 if err != nil {
198 l.Error("failed to calculate relative git dir", "scanPath", h.c.Repo.ScanPath, "gitAbsoluteDir", gitAbsoluteDir)
199 w.WriteHeader(http.StatusInternalServerError)
200 return
201 }
202
203 var repoDid string
204 var ownerDid, repoName string
205
206 if strings.HasPrefix(gitRelativeDir, "did:") {
207 repoDid = gitRelativeDir
208 var err error
209 ownerDid, repoName, err = h.db.GetRepoKeyOwner(repoDid)
210 if err != nil {
211 l.Error("failed to resolve repo DID from git dir", "repoDid", repoDid, "err", err)
212 w.WriteHeader(http.StatusBadRequest)
213 return
214 }
215 } else {
216 components := strings.SplitN(gitRelativeDir, "/", 2)
217 if len(components) != 2 {
218 l.Error("invalid git dir, expected repo DID or owner/repo", "gitRelativeDir", gitRelativeDir)
219 w.WriteHeader(http.StatusBadRequest)
220 return
221 }
222 ownerDid = components[0]
223 repoName = components[1]
224 var didErr error
225 repoDid, didErr = h.db.GetRepoDid(ownerDid, repoName)
226 if didErr != nil {
227 l.Error("failed to resolve repo DID from legacy path", "gitRelativeDir", gitRelativeDir, "err", didErr)
228 w.WriteHeader(http.StatusBadRequest)
229 return
230 }
231 }
232
233 gitUserDid := r.Header.Get("X-Git-User-Did")
234
235 lines, err := git.ParsePostReceive(r.Body)
236 if err != nil {
237 l.Error("failed to parse post-receive payload", "err", err)
238 // non-fatal
239 }
240
241 // extract any push options
242 pushOptionsRaw := r.Header.Values("X-Git-Push-Option")
243 pushOptions := PushOptions{}
244 for _, option := range pushOptionsRaw {
245 if option == "skip-ci" || option == "ci-skip" {
246 pushOptions.skipCi = true
247 }
248 if option == "verbose-ci" || option == "ci-verbose" {
249 pushOptions.verboseCi = true
250 }
251 }
252
253 resp := hook.HookResponse{
254 Messages: make([]string, 0),
255 }
256
257 for _, line := range lines {
258 err := h.insertRefUpdate(line, gitUserDid, ownerDid, repoDid)
259 if err != nil {
260 l.Error("failed to insert op", "err", err, "line", line, "did", gitUserDid, "repo", gitRelativeDir)
261 }
262
263 err = h.emitPullRequestLink(&resp.Messages, line, ownerDid, repoName, repoDid)
264 if err != nil {
265 l.Error("failed to reply with pull request link", "err", err, "line", line, "did", gitUserDid, "repo", gitRelativeDir)
266 }
267
268 err = h.triggerPipeline(&resp.Messages, line, gitUserDid, ownerDid, repoName, repoDid, pushOptions)
269 if err != nil {
270 l.Error("failed to trigger pipeline", "err", err, "line", line, "did", gitUserDid, "repo", gitRelativeDir)
271 }
272 }
273
274 writeJSON(w, resp)
275}
276
277func (h *InternalHandle) insertRefUpdate(line git.PostReceiveLine, gitUserDid, ownerDid, repoDid string) error {
278 repoPath, _, _, resolveErr := h.db.ResolveRepoDIDOnDisk(h.c.Repo.ScanPath, repoDid)
279 if resolveErr != nil {
280 return fmt.Errorf("failed to resolve repo on disk: %w", resolveErr)
281 }
282
283 gr, err := git.Open(repoPath, line.Ref)
284 if err != nil {
285 return fmt.Errorf("failed to open git repo at ref %s: %w", line.Ref, err)
286 }
287
288 meta, err := gr.RefUpdateMeta(line)
289 if err != nil {
290 return fmt.Errorf("failed to get ref update metadata: %w", err)
291 }
292
293 metaRecord := meta.AsRecord()
294
295 refUpdate := tangled.GitRefUpdate{
296 OldSha: line.OldSha.String(),
297 NewSha: line.NewSha.String(),
298 Ref: line.Ref,
299 CommitterDid: gitUserDid,
300 OwnerDid: &ownerDid,
301 Repo: repoDid,
302 Meta: &metaRecord,
303 }
304
305 eventJson, err := json.Marshal(refUpdate)
306 if err != nil {
307 return err
308 }
309
310 event := db.Event{
311 Rkey: TID(),
312 Nsid: tangled.GitRefUpdateNSID,
313 EventJson: string(eventJson),
314 }
315
316 return h.db.InsertEvent(event, h.n)
317}
318
319func (h *InternalHandle) triggerPipeline(
320 clientMsgs *[]string,
321 line git.PostReceiveLine,
322 gitUserDid string,
323 ownerDid string,
324 repoName string,
325 repoDid string,
326 pushOptions PushOptions,
327) error {
328 if pushOptions.skipCi {
329 return nil
330 }
331
332 repoPath, _, _, resolveErr := h.db.ResolveRepoDIDOnDisk(h.c.Repo.ScanPath, repoDid)
333 if resolveErr != nil {
334 return fmt.Errorf("failed to resolve repo on disk: %w", resolveErr)
335 }
336
337 gr, err := git.Open(repoPath, line.Ref)
338 if err != nil {
339 return err
340 }
341
342 workflowDir, err := gr.FileTree(context.Background(), workflow.WorkflowDir)
343 if err != nil {
344 return err
345 }
346
347 var pipeline workflow.RawPipeline
348 for _, e := range workflowDir {
349 if !e.IsFile() {
350 continue
351 }
352
353 fpath := filepath.Join(workflow.WorkflowDir, e.Name)
354 contents, err := gr.RawContent(fpath)
355 if err != nil {
356 continue
357 }
358
359 pipeline = append(pipeline, workflow.RawWorkflow{
360 Name: e.Name,
361 Contents: contents,
362 })
363 }
364
365 defaultBranch, _ := gr.FindMainBranch()
366
367 trigger := tangled.Pipeline_PushTriggerData{
368 Ref: line.Ref,
369 OldSha: line.OldSha.String(),
370 NewSha: line.NewSha.String(),
371 }
372
373 triggerRepo := &tangled.Pipeline_TriggerRepo{
374 Did: ownerDid,
375 Knot: h.c.Server.Hostname,
376 Repo: &repoName,
377 RepoDid: &repoDid,
378 DefaultBranch: defaultBranch,
379 }
380
381 compiler := workflow.Compiler{
382 Trigger: tangled.Pipeline_TriggerMetadata{
383 Kind: string(workflow.TriggerKindPush),
384 Push: &trigger,
385 Repo: triggerRepo,
386 },
387 }
388
389 cp := compiler.Compile(compiler.Parse(pipeline))
390 eventJson, err := json.Marshal(cp)
391 if err != nil {
392 return err
393 }
394
395 for _, e := range compiler.Diagnostics.Errors {
396 *clientMsgs = append(*clientMsgs, e.String())
397 }
398
399 if pushOptions.verboseCi {
400 if compiler.Diagnostics.IsEmpty() {
401 *clientMsgs = append(*clientMsgs, "success: pipeline compiled with no diagnostics")
402 }
403
404 for _, w := range compiler.Diagnostics.Warnings {
405 *clientMsgs = append(*clientMsgs, w.String())
406 }
407 }
408
409 // do not run empty pipelines
410 if cp.Workflows == nil {
411 return nil
412 }
413
414 event := db.Event{
415 Rkey: TID(),
416 Nsid: tangled.PipelineNSID,
417 EventJson: string(eventJson),
418 }
419
420 return h.db.InsertEvent(event, h.n)
421}
422
423func (h *InternalHandle) emitPullRequestLink(
424 clientMsgs *[]string,
425 line git.PostReceiveLine,
426 ownerDid string,
427 repoName string,
428 repoDid string,
429) error {
430 if line.NewSha.IsZero() {
431 return nil
432 }
433
434 // the ref was not updated to a new hash, don't reply with the link
435 //
436 // NOTE: do we need this?
437 if line.NewSha == line.OldSha {
438 return nil
439 }
440
441 pushedRef := plumbing.ReferenceName(line.Ref)
442 if !pushedRef.IsBranch() {
443 return nil
444 }
445
446 if !line.OldSha.IsZero() {
447 return nil
448 }
449
450 repoPath, _, _, resolveErr := h.db.ResolveRepoDIDOnDisk(h.c.Repo.ScanPath, repoDid)
451 if resolveErr != nil {
452 return fmt.Errorf("failed to resolve repo on disk: %w", resolveErr)
453 }
454
455 gr, err := git.PlainOpen(repoPath)
456 if err != nil {
457 return err
458 }
459
460 defaultBranch, err := gr.FindMainBranch()
461 if err != nil {
462 return err
463 }
464
465 pushedBranch := pushedRef.Short()
466
467 // pushing to default branch
468 if pushedBranch == defaultBranch {
469 return nil
470 }
471
472 userIdent, err := h.res.ResolveIdent(context.Background(), ownerDid)
473 user := ownerDid
474 if err == nil {
475 user = userIdent.Handle.String()
476 }
477
478 query := url.Values{}
479 query.Set("source", "branch")
480 query.Set("sourceBranch", pushedBranch)
481 query.Set("targetBranch", defaultBranch)
482
483 basePath, err := url.JoinPath(h.c.AppViewEndpoint, user, repoName, "pulls", "new")
484 if err != nil {
485 return err
486 }
487 pullURL := basePath + "?" + query.Encode()
488
489 ZWS := "\u200B"
490 *clientMsgs = append(*clientMsgs, ZWS)
491 *clientMsgs = append(*clientMsgs, "→ Open pull request:")
492 *clientMsgs = append(*clientMsgs, " "+pullURL)
493 *clientMsgs = append(*clientMsgs, ZWS)
494 return nil
495}
496
497func Internal(ctx context.Context, c *config.Config, db *db.DB, e *rbac.Enforcer, n *notifier.Notifier, res *idresolver.Resolver) http.Handler {
498 r := chi.NewRouter()
499 l := log.FromContext(ctx)
500 l = log.SubLogger(l, "internal")
501
502 h := InternalHandle{
503 db: db,
504 c: c,
505 e: e,
506 l: l,
507 n: n,
508 res: res,
509 }
510
511 r.Get("/push-allowed", h.PushAllowed)
512 r.Get("/keys", h.InternalKeys)
513 r.Get("/guard", h.Guard)
514 r.Post("/hooks/post-receive", h.PostReceiveHook)
515 r.Mount("/debug", middleware.Profiler())
516
517 return r
518}