Skip to content

Commit 0ebd435

Browse files
authored
fix: use system context for managed agent count query (#18985)
1 parent 9a6dd73 commit 0ebd435

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

enterprise/coderd/coderd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,8 @@ func (api *API) CheckBuildUsage(ctx context.Context, store database.Store, templ
961961

962962
// This check is intentionally not committed to the database. It's fine if
963963
// it's not 100% accurate or allows for minor breaches due to build races.
964-
managedAgentCount, err := store.GetManagedAgentCount(ctx, database.GetManagedAgentCountParams{
964+
// nolint:gocritic // Requires permission to read all workspaces to read managed agent count.
965+
managedAgentCount, err := store.GetManagedAgentCount(agpldbauthz.AsSystemRestricted(ctx), database.GetManagedAgentCountParams{
965966
StartTime: managedAgentLimit.UsagePeriod.Start,
966967
EndTime: managedAgentLimit.UsagePeriod.End,
967968
})

enterprise/coderd/coderd_test.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,13 +626,38 @@ func TestSCIMDisabled(t *testing.T) {
626626
func TestManagedAgentLimit(t *testing.T) {
627627
t.Parallel()
628628

629+
ctx := testutil.Context(t, testutil.WaitLong)
630+
629631
cli, _ := coderdenttest.New(t, &coderdenttest.Options{
630632
Options: &coderdtest.Options{
631633
IncludeProvisionerDaemon: true,
632634
},
633-
LicenseOptions: (&coderdenttest.LicenseOptions{}).ManagedAgentLimit(1, 1),
635+
LicenseOptions: (&coderdenttest.LicenseOptions{
636+
FeatureSet: codersdk.FeatureSetPremium,
637+
// Make it expire in the distant future so it doesn't generate
638+
// expiry warnings.
639+
GraceAt: time.Now().Add(time.Hour * 24 * 60),
640+
ExpiresAt: time.Now().Add(time.Hour * 24 * 90),
641+
}).ManagedAgentLimit(1, 1),
634642
})
635643

644+
// Get entitlements to check that the license is a-ok.
645+
entitlements, err := cli.Entitlements(ctx) //nolint:gocritic // we're not testing authz on the entitlements endpoint, so using owner is fine
646+
require.NoError(t, err)
647+
require.True(t, entitlements.HasLicense)
648+
agentLimit := entitlements.Features[codersdk.FeatureManagedAgentLimit]
649+
require.True(t, agentLimit.Enabled)
650+
require.NotNil(t, agentLimit.Limit)
651+
require.EqualValues(t, 1, *agentLimit.Limit)
652+
require.NotNil(t, agentLimit.SoftLimit)
653+
require.EqualValues(t, 1, *agentLimit.SoftLimit)
654+
require.Empty(t, entitlements.Errors)
655+
// There should be a warning since we're really close to our agent limit.
656+
require.Equal(t, entitlements.Warnings[0], "You are approaching the managed agent limit in your license. Please refer to the Deployment Licenses page for more information.")
657+
658+
// Create a fake provision response that claims there are agents in the
659+
// template and every built workspace.
660+
//
636661
// It's fine that the app ID is only used in a single successful workspace
637662
// build.
638663
appID := uuid.NewString()
@@ -693,7 +718,7 @@ func TestManagedAgentLimit(t *testing.T) {
693718

694719
// Create a second AI workspace, which should fail. This needs to be done
695720
// manually because coderdtest.CreateWorkspace expects it to succeed.
696-
_, err := cli.CreateUserWorkspace(context.Background(), codersdk.Me, codersdk.CreateWorkspaceRequest{ //nolint:gocritic // owners must still be subject to the limit
721+
_, err = cli.CreateUserWorkspace(ctx, codersdk.Me, codersdk.CreateWorkspaceRequest{ //nolint:gocritic // owners must still be subject to the limit
697722
TemplateID: aiTemplate.ID,
698723
Name: coderdtest.RandomUsername(t),
699724
AutomaticUpdates: codersdk.AutomaticUpdatesNever,

enterprise/coderd/license/license.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ func Entitlements(
9999
ReplicaCount: replicaCount,
100100
ExternalAuthCount: externalAuthCount,
101101
ManagedAgentCountFn: func(ctx context.Context, startTime time.Time, endTime time.Time) (int64, error) {
102-
return db.GetManagedAgentCount(ctx, database.GetManagedAgentCountParams{
102+
// nolint:gocritic // Requires permission to read all workspaces to read managed agent count.
103+
return db.GetManagedAgentCount(dbauthz.AsSystemRestricted(ctx), database.GetManagedAgentCountParams{
103104
StartTime: startTime,
104105
EndTime: endTime,
105106
})

0 commit comments

Comments
 (0)