Skip to content

Commit b2d5609

Browse files
Add some tests
Signed-off-by: Gabriel Adrian Samfira <[email protected]>
1 parent bb798a2 commit b2d5609

File tree

9 files changed

+441
-24
lines changed

9 files changed

+441
-24
lines changed

auth/context.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,21 @@ const (
4444
instanceTokenFetched contextFlags = "tokenFetched"
4545
instanceHasJITConfig contextFlags = "hasJITConfig"
4646
instanceParams contextFlags = "instanceParams"
47+
instanceForgeTypeKey contextFlags = "forge_type"
4748
)
4849

50+
func SetInstanceForgeType(ctx context.Context, val string) context.Context {
51+
return context.WithValue(ctx, instanceForgeTypeKey, val)
52+
}
53+
54+
func InstanceForgeType(ctx context.Context) params.EndpointType {
55+
elem := ctx.Value(instanceForgeTypeKey)
56+
if elem == nil {
57+
return ""
58+
}
59+
return elem.(params.EndpointType)
60+
}
61+
4962
func SetInstanceID(ctx context.Context, id string) context.Context {
5063
return context.WithValue(ctx, instanceIDKey, id)
5164
}
@@ -159,14 +172,15 @@ func InstanceEntity(ctx context.Context) string {
159172
return elem.(string)
160173
}
161174

162-
func PopulateInstanceContext(ctx context.Context, instance params.Instance) context.Context {
175+
func PopulateInstanceContext(ctx context.Context, instance params.Instance, claims *InstanceJWTClaims) context.Context {
163176
ctx = SetInstanceID(ctx, instance.ID)
164177
ctx = SetInstanceName(ctx, instance.Name)
165178
ctx = SetInstancePoolID(ctx, instance.PoolID)
166179
ctx = SetInstanceRunnerStatus(ctx, instance.RunnerStatus)
167180
ctx = SetInstanceTokenFetched(ctx, instance.TokenFetched)
168181
ctx = SetInstanceHasJITConfig(ctx, instance.JitConfiguration)
169182
ctx = SetInstanceParams(ctx, instance)
183+
ctx = SetInstanceForgeType(ctx, claims.ForgeType)
170184
return ctx
171185
}
172186

auth/instance_middleware.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type InstanceJWTClaims struct {
4444
// Entity is the repo or org name
4545
Entity string `json:"entity"`
4646
CreateAttempt int `json:"create_attempt"`
47+
ForgeType string `json:"forge_type"`
4748
jwt.RegisteredClaims
4849
}
4950

@@ -60,7 +61,7 @@ type instanceToken struct {
6061
jwtSecret string
6162
}
6263

63-
func (i *instanceToken) NewInstanceJWTToken(instance params.Instance, entity string, entityType params.ForgeEntityType, ttlMinutes uint) (string, error) {
64+
func (i *instanceToken) NewInstanceJWTToken(instance params.Instance, entity params.ForgeEntity, entityType params.ForgeEntityType, ttlMinutes uint) (string, error) {
6465
// Token expiration is equal to the bootstrap timeout set on the pool plus the polling
6566
// interval garm uses to check for timed out runners. Runners that have not sent their info
6667
// by the end of this interval are most likely failed and will be reaped by garm anyway.
@@ -83,7 +84,8 @@ func (i *instanceToken) NewInstanceJWTToken(instance params.Instance, entity str
8384
Name: instance.Name,
8485
PoolID: instance.PoolID,
8586
Scope: entityType,
86-
Entity: entity,
87+
Entity: entity.String(),
88+
ForgeType: string(entity.Credentials.ForgeType),
8789
CreateAttempt: instance.CreateAttempt,
8890
}
8991
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
@@ -124,7 +126,7 @@ func (amw *instanceMiddleware) claimsToContext(ctx context.Context, claims *Inst
124126
return ctx, runnerErrors.ErrUnauthorized
125127
}
126128

127-
ctx = PopulateInstanceContext(ctx, instanceInfo)
129+
ctx = PopulateInstanceContext(ctx, instanceInfo, claims)
128130
return ctx, nil
129131
}
130132

auth/interfaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ type Middleware interface {
2626
}
2727

2828
type InstanceTokenGetter interface {
29-
NewInstanceJWTToken(instance params.Instance, entity string, poolType params.ForgeEntityType, ttlMinutes uint) (string, error)
29+
NewInstanceJWTToken(instance params.Instance, entity params.ForgeEntity, poolType params.ForgeEntityType, ttlMinutes uint) (string, error)
3030
}

0 commit comments

Comments
 (0)