summaryrefslogtreecommitdiffstats
path: root/models/actions/forgejo.go
blob: 5ea77f44735f136e0712fbebf11af1c81af9b1bc (plain)
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
// SPDX-License-Identifier: MIT

package actions

import (
	"context"
	"crypto/subtle"
	"fmt"

	auth_model "code.gitea.io/gitea/models/auth"
	"code.gitea.io/gitea/models/db"
	"code.gitea.io/gitea/modules/util"

	gouuid "github.com/google/uuid"
)

func RegisterRunner(ctx context.Context, ownerID, repoID int64, token string, labels *[]string, name, version string) (*ActionRunner, error) {
	uuid, err := gouuid.FromBytes([]byte(token[:16]))
	if err != nil {
		return nil, fmt.Errorf("gouuid.FromBytes %v", err)
	}
	uuidString := uuid.String()

	var runner ActionRunner

	has, err := db.GetEngine(ctx).Where("uuid=?", uuidString).Get(&runner)
	if err != nil {
		return nil, fmt.Errorf("GetRunner %v", err)
	}

	var mustUpdateSecret bool
	if has {
		//
		// The runner exists, check if the rest of the token has changed.
		//
		mustUpdateSecret = subtle.ConstantTimeCompare(
			[]byte(runner.TokenHash),
			[]byte(auth_model.HashToken(token, runner.TokenSalt)),
		) != 1
	} else {
		//
		// The runner does not exist yet, create it
		//
		runner = ActionRunner{
			UUID:        uuidString,
			AgentLabels: []string{},
		}

		if err := runner.UpdateSecret(token); err != nil {
			return &runner, fmt.Errorf("can't set new runner's secret: %w", err)
		}

		if err := CreateRunner(ctx, &runner); err != nil {
			return &runner, fmt.Errorf("can't create new runner %w", err)
		}
	}

	//
	// Update the existing runner
	//
	name, _ = util.SplitStringAtByteN(name, 255)

	cols := []string{"name", "owner_id", "repo_id", "version"}
	runner.Name = name
	runner.OwnerID = ownerID
	runner.RepoID = repoID
	runner.Version = version
	if labels != nil {
		runner.AgentLabels = *labels
		cols = append(cols, "agent_labels")
	}
	if mustUpdateSecret {
		if err := runner.UpdateSecret(token); err != nil {
			return &runner, fmt.Errorf("can't change runner's secret: %w", err)
		}
		cols = append(cols, "token_hash", "token_salt")
	}

	if err := UpdateRunner(ctx, &runner, cols...); err != nil {
		return &runner, fmt.Errorf("can't update the runner %+v %w", runner, err)
	}

	return &runner, nil
}