Skip to content

Commit 8f0d447

Browse files
Merge pull request #269 from gabriel-samfira/remove-update-state
Use watcher and get rid of RefreshState()
2 parents 38127af + daaca0b commit 8f0d447

23 files changed

Lines changed: 453 additions & 463 deletions

auth/instance_middleware.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,20 @@ type InstanceJWTClaims struct {
4646
jwt.RegisteredClaims
4747
}
4848

49-
func NewInstanceJWTToken(instance params.Instance, secret, entity string, poolType params.GithubEntityType, ttlMinutes uint) (string, error) {
49+
func NewInstanceTokenGetter(jwtSecret string) (InstanceTokenGetter, error) {
50+
if jwtSecret == "" {
51+
return nil, fmt.Errorf("jwt secret is required")
52+
}
53+
return &instanceToken{
54+
jwtSecret: jwtSecret,
55+
}, nil
56+
}
57+
58+
type instanceToken struct {
59+
jwtSecret string
60+
}
61+
62+
func (i *instanceToken) NewInstanceJWTToken(instance params.Instance, entity string, poolType params.GithubEntityType, ttlMinutes uint) (string, error) {
5063
// Token expiration is equal to the bootstrap timeout set on the pool plus the polling
5164
// interval garm uses to check for timed out runners. Runners that have not sent their info
5265
// by the end of this interval are most likely failed and will be reaped by garm anyway.
@@ -67,7 +80,7 @@ func NewInstanceJWTToken(instance params.Instance, secret, entity string, poolTy
6780
CreateAttempt: instance.CreateAttempt,
6881
}
6982
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
70-
tokenString, err := token.SignedString([]byte(secret))
83+
tokenString, err := token.SignedString([]byte(i.jwtSecret))
7184
if err != nil {
7285
return "", errors.Wrap(err, "signing token")
7386
}

auth/interfaces.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,17 @@
1414

1515
package auth
1616

17-
import "net/http"
17+
import (
18+
"net/http"
19+
20+
"github.com/cloudbase/garm/params"
21+
)
1822

1923
// Middleware defines an authentication middleware
2024
type Middleware interface {
2125
Middleware(next http.Handler) http.Handler
2226
}
27+
28+
type InstanceTokenGetter interface {
29+
NewInstanceJWTToken(instance params.Instance, entity string, poolType params.GithubEntityType, ttlMinutes uint) (string, error)
30+
}

cmd/garm-cli/cmd/github_credentials.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,10 @@ func parseCredentialsAddParams() (ret params.CreateGithubCredentialsParams, err
303303
func parseCredentialsUpdateParams() (params.UpdateGithubCredentialsParams, error) {
304304
var updateParams params.UpdateGithubCredentialsParams
305305

306+
if credentialsAppInstallationID != 0 || credentialsAppID != 0 || credentialsPrivateKeyPath != "" {
307+
updateParams.App = &params.GithubApp{}
308+
}
309+
306310
if credentialsName != "" {
307311
updateParams.Name = &credentialsName
308312
}
@@ -312,6 +316,9 @@ func parseCredentialsUpdateParams() (params.UpdateGithubCredentialsParams, error
312316
}
313317

314318
if credentialsOAuthToken != "" {
319+
if updateParams.PAT == nil {
320+
updateParams.PAT = &params.GithubPAT{}
321+
}
315322
updateParams.PAT.OAuth2Token = credentialsOAuthToken
316323
}
317324

database/sql/enterprise.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (s *sqlDatabase) ListEnterprises(_ context.Context) ([]params.Enterprise, e
132132
}
133133

134134
func (s *sqlDatabase) DeleteEnterprise(ctx context.Context, enterpriseID string) error {
135-
enterprise, err := s.getEnterpriseByID(ctx, s.conn, enterpriseID, "Endpoint", "Credentials")
135+
enterprise, err := s.getEnterpriseByID(ctx, s.conn, enterpriseID, "Endpoint", "Credentials", "Credentials.Endpoint")
136136
if err != nil {
137137
return errors.Wrap(err, "fetching enterprise")
138138
}
@@ -206,17 +206,13 @@ func (s *sqlDatabase) UpdateEnterprise(ctx context.Context, enterpriseID string,
206206
return errors.Wrap(q.Error, "saving enterprise")
207207
}
208208

209-
if creds.ID != 0 {
210-
enterprise.Credentials = creds
211-
}
212-
213209
return nil
214210
})
215211
if err != nil {
216212
return params.Enterprise{}, errors.Wrap(err, "updating enterprise")
217213
}
218214

219-
enterprise, err = s.getEnterpriseByID(ctx, s.conn, enterpriseID, "Endpoint", "Credentials")
215+
enterprise, err = s.getEnterpriseByID(ctx, s.conn, enterpriseID, "Endpoint", "Credentials", "Credentials.Endpoint")
220216
if err != nil {
221217
return params.Enterprise{}, errors.Wrap(err, "updating enterprise")
222218
}

database/sql/organizations.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (s *sqlDatabase) ListOrganizations(_ context.Context) ([]params.Organizatio
123123
}
124124

125125
func (s *sqlDatabase) DeleteOrganization(ctx context.Context, orgID string) (err error) {
126-
org, err := s.getOrgByID(ctx, s.conn, orgID, "Endpoint", "Credentials")
126+
org, err := s.getOrgByID(ctx, s.conn, orgID, "Endpoint", "Credentials", "Credentials.Endpoint")
127127
if err != nil {
128128
return errors.Wrap(err, "fetching org")
129129
}
@@ -198,17 +198,13 @@ func (s *sqlDatabase) UpdateOrganization(ctx context.Context, orgID string, para
198198
return errors.Wrap(q.Error, "saving org")
199199
}
200200

201-
if creds.ID != 0 {
202-
org.Credentials = creds
203-
}
204-
205201
return nil
206202
})
207203
if err != nil {
208204
return params.Organization{}, errors.Wrap(err, "saving org")
209205
}
210206

211-
org, err = s.getOrgByID(ctx, s.conn, orgID, "Endpoint", "Credentials")
207+
org, err = s.getOrgByID(ctx, s.conn, orgID, "Endpoint", "Credentials", "Credentials.Endpoint")
212208
if err != nil {
213209
return params.Organization{}, errors.Wrap(err, "updating enterprise")
214210
}

database/sql/repositories.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func (s *sqlDatabase) ListRepositories(_ context.Context) ([]params.Repository,
122122
}
123123

124124
func (s *sqlDatabase) DeleteRepository(ctx context.Context, repoID string) (err error) {
125-
repo, err := s.getRepoByID(ctx, s.conn, repoID, "Endpoint", "Credentials")
125+
repo, err := s.getRepoByID(ctx, s.conn, repoID, "Endpoint", "Credentials", "Credentials.Endpoint")
126126
if err != nil {
127127
return errors.Wrap(err, "fetching repo")
128128
}
@@ -197,16 +197,13 @@ func (s *sqlDatabase) UpdateRepository(ctx context.Context, repoID string, param
197197
return errors.Wrap(q.Error, "saving repo")
198198
}
199199

200-
if creds.ID != 0 {
201-
repo.Credentials = creds
202-
}
203200
return nil
204201
})
205202
if err != nil {
206203
return params.Repository{}, errors.Wrap(err, "saving repo")
207204
}
208205

209-
repo, err = s.getRepoByID(ctx, s.conn, repoID, "Endpoint", "Credentials")
206+
repo, err = s.getRepoByID(ctx, s.conn, repoID, "Endpoint", "Credentials", "Credentials.Endpoint")
210207
if err != nil {
211208
return params.Repository{}, errors.Wrap(err, "updating enterprise")
212209
}

database/watcher/filters.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ func WithAny(filters ...dbCommon.PayloadFilterFunc) dbCommon.PayloadFilterFunc {
3232
}
3333
}
3434

35+
// WithAll returns a filter function that returns true if all of the provided filters return true.
36+
func WithAll(filters ...dbCommon.PayloadFilterFunc) dbCommon.PayloadFilterFunc {
37+
return func(payload dbCommon.ChangePayload) bool {
38+
for _, filter := range filters {
39+
if !filter(payload) {
40+
return false
41+
}
42+
}
43+
return true
44+
}
45+
}
46+
3547
// WithEntityTypeFilter returns a filter function that filters payloads by entity type.
3648
// The filter function returns true if the payload's entity type matches the provided entity type.
3749
func WithEntityTypeFilter(entityType dbCommon.DatabaseEntityType) dbCommon.PayloadFilterFunc {
@@ -139,3 +151,17 @@ func WithEntityJobFilter(ghEntity params.GithubEntity) dbCommon.PayloadFilterFun
139151
}
140152
}
141153
}
154+
155+
// WithGithubCredentialsFilter returns a filter function that filters payloads by Github credentials.
156+
func WithGithubCredentialsFilter(creds params.GithubCredentials) dbCommon.PayloadFilterFunc {
157+
return func(payload dbCommon.ChangePayload) bool {
158+
if payload.EntityType != dbCommon.GithubCredentialsEntityType {
159+
return false
160+
}
161+
credsPayload, ok := payload.Payload.(params.GithubCredentials)
162+
if !ok {
163+
return false
164+
}
165+
return credsPayload.ID == creds.ID
166+
}
167+
}

params/params.go

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,13 @@ func (r Repository) GetEntity() (GithubEntity, error) {
419419
return GithubEntity{}, fmt.Errorf("repository has no ID")
420420
}
421421
return GithubEntity{
422-
ID: r.ID,
423-
EntityType: GithubEntityTypeRepository,
424-
Owner: r.Owner,
425-
Name: r.Name,
422+
ID: r.ID,
423+
EntityType: GithubEntityTypeRepository,
424+
Owner: r.Owner,
425+
Name: r.Name,
426+
PoolBalancerType: r.PoolBalancerType,
427+
Credentials: r.Credentials,
428+
WebhookSecret: r.WebhookSecret,
426429
}, nil
427430
}
428431

@@ -470,10 +473,12 @@ func (o Organization) GetEntity() (GithubEntity, error) {
470473
return GithubEntity{}, fmt.Errorf("organization has no ID")
471474
}
472475
return GithubEntity{
473-
ID: o.ID,
474-
EntityType: GithubEntityTypeOrganization,
475-
Owner: o.Name,
476-
WebhookSecret: o.WebhookSecret,
476+
ID: o.ID,
477+
EntityType: GithubEntityTypeOrganization,
478+
Owner: o.Name,
479+
WebhookSecret: o.WebhookSecret,
480+
PoolBalancerType: o.PoolBalancerType,
481+
Credentials: o.Credentials,
477482
}, nil
478483
}
479484

@@ -517,10 +522,12 @@ func (e Enterprise) GetEntity() (GithubEntity, error) {
517522
return GithubEntity{}, fmt.Errorf("enterprise has no ID")
518523
}
519524
return GithubEntity{
520-
ID: e.ID,
521-
EntityType: GithubEntityTypeEnterprise,
522-
Owner: e.Name,
523-
WebhookSecret: e.WebhookSecret,
525+
ID: e.ID,
526+
EntityType: GithubEntityTypeEnterprise,
527+
Owner: e.Name,
528+
WebhookSecret: e.WebhookSecret,
529+
PoolBalancerType: e.PoolBalancerType,
530+
Credentials: e.Credentials,
524531
}, nil
525532
}
526533

@@ -685,11 +692,6 @@ type Provider struct {
685692
// used by swagger client generated code
686693
type Providers []Provider
687694

688-
type UpdatePoolStateParams struct {
689-
WebhookSecret string
690-
InternalConfig *Internal
691-
}
692-
693695
type PoolManagerStatus struct {
694696
IsRunning bool `json:"running"`
695697
FailureReason string `json:"failure_reason,omitempty"`
@@ -788,15 +790,23 @@ type UpdateSystemInfoParams struct {
788790
}
789791

790792
type GithubEntity struct {
791-
Owner string `json:"owner"`
792-
Name string `json:"name"`
793-
ID string `json:"id"`
794-
EntityType GithubEntityType `json:"entity_type"`
795-
Credentials GithubCredentials `json:"credentials"`
793+
Owner string `json:"owner"`
794+
Name string `json:"name"`
795+
ID string `json:"id"`
796+
EntityType GithubEntityType `json:"entity_type"`
797+
Credentials GithubCredentials `json:"credentials"`
798+
PoolBalancerType PoolBalancerType `json:"pool_balancing_type"`
796799

797800
WebhookSecret string `json:"-"`
798801
}
799802

803+
func (g GithubEntity) GetPoolBalancerType() PoolBalancerType {
804+
if g.PoolBalancerType == "" {
805+
return PoolBalancerTypeRoundRobin
806+
}
807+
return g.PoolBalancerType
808+
}
809+
800810
func (g GithubEntity) LabelScope() string {
801811
switch g.EntityType {
802812
case GithubEntityTypeRepository:

runner/common/mocks/PoolManager.go

Lines changed: 0 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

runner/common/pool.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ type PoolManager interface {
5353
// a repo, org or enterprise, we determine the destination of that webhook, retrieve the pool manager
5454
// for it and call this function with the WorkflowJob as a parameter.
5555
HandleWorkflowJob(job params.WorkflowJob) error
56-
// RefreshState allows us to update webhook secrets and configuration for a pool manager.
57-
RefreshState(param params.UpdatePoolStateParams) error
5856

5957
// DeleteRunner will attempt to remove a runner from the pool. If forceRemove is true, any error
6058
// received from the provider will be ignored and we will proceed to remove the runner from the database.

0 commit comments

Comments
 (0)