Skip to content

Commit ed5b96d

Browse files
committed
Remove entitlements API from AGPL coderd
1 parent 27f53aa commit ed5b96d

File tree

9 files changed

+49
-30
lines changed

9 files changed

+49
-30
lines changed

cli/root.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ func Core() []*cobra.Command {
9191
users(),
9292
versionCmd(),
9393
workspaceAgent(),
94-
features(),
9594
}
9695
}
9796

coderd/coderd.go

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -490,10 +490,6 @@ func New(options *Options) *API {
490490
r.Get("/resources", api.workspaceBuildResources)
491491
r.Get("/state", api.workspaceBuildState)
492492
})
493-
r.Route("/entitlements", func(r chi.Router) {
494-
r.Use(apiKeyMiddleware)
495-
r.Get("/", nopEntitlements)
496-
})
497493
})
498494

499495
r.NotFound(compressHandler(http.HandlerFunc(api.siteHandler.ServeHTTP)).ServeHTTP)
@@ -505,8 +501,9 @@ type API struct {
505501
Auditor *pointer.Handle[audit.Auditor]
506502
HTTPAuth *HTTPAuthorizer
507503

508-
// APIHandler serves "/api/v2" and all children routes.
509-
APIHandler chi.Router
504+
// APIHandler serves "/api/v2"
505+
APIHandler chi.Router
506+
// RootHandler serves "/"
510507
RootHandler chi.Router
511508

512509
derpServer *derp.Server
@@ -547,18 +544,3 @@ func compressHandler(h http.Handler) http.Handler {
547544

548545
return cmp.Handler(h)
549546
}
550-
551-
func nopEntitlements(rw http.ResponseWriter, _ *http.Request) {
552-
feats := make(map[string]codersdk.Feature)
553-
for _, f := range codersdk.FeatureNames {
554-
feats[f] = codersdk.Feature{
555-
Entitlement: codersdk.EntitlementNotEntitled,
556-
Enabled: false,
557-
}
558-
}
559-
httpapi.Write(rw, http.StatusOK, codersdk.Entitlements{
560-
Features: feats,
561-
Warnings: []string{},
562-
HasLicense: false,
563-
})
564-
}

coderd/pointer/pointer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"go.uber.org/atomic"
77
)
88

9+
// New constructs a Handle with an initialized value.
910
func New[T any](value T) *Handle[T] {
1011
h := &Handle[T]{
1112
key: struct{}{},

cli/features.go renamed to enterprise/cli/features.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ package cli
33
import (
44
"bytes"
55
"encoding/json"
6+
"errors"
67
"fmt"
8+
"net/http"
79
"strings"
810

911
"github.com/spf13/cobra"
1012
"golang.org/x/xerrors"
1113

14+
agpl "github.com/coder/coder/cli"
1215
"github.com/coder/coder/cli/cliui"
1316
"github.com/coder/coder/codersdk"
1417
)
@@ -36,11 +39,15 @@ func featuresList() *cobra.Command {
3639
Use: "list",
3740
Aliases: []string{"ls"},
3841
RunE: func(cmd *cobra.Command, args []string) error {
39-
client, err := CreateClient(cmd)
42+
client, err := agpl.CreateClient(cmd)
4043
if err != nil {
4144
return err
4245
}
4346
entitlements, err := client.Entitlements(cmd.Context())
47+
var apiError *codersdk.Error
48+
if errors.As(err, &apiError) && apiError.StatusCode() == http.StatusNotFound {
49+
return xerrors.New("You are on the AGPL licensed version of Coder that does not have Enterprise functionality!")
50+
}
4451
if err != nil {
4552
return err
4653
}

cli/features_test.go renamed to enterprise/cli/features_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ import (
1111
"github.com/coder/coder/cli/clitest"
1212
"github.com/coder/coder/coderd/coderdtest"
1313
"github.com/coder/coder/codersdk"
14+
"github.com/coder/coder/enterprise/cli"
15+
"github.com/coder/coder/enterprise/coderd/coderdenttest"
1416
"github.com/coder/coder/pty/ptytest"
1517
)
1618

1719
func TestFeaturesList(t *testing.T) {
1820
t.Parallel()
1921
t.Run("Table", func(t *testing.T) {
2022
t.Parallel()
21-
client := coderdtest.New(t, nil)
23+
client := coderdenttest.New(t, nil)
2224
coderdtest.CreateFirstUser(t, client)
23-
cmd, root := clitest.New(t, "features", "list")
25+
cmd, root := clitest.NewWithSubcommands(t, cli.EnterpriseSubcommands(), "features", "list")
2426
clitest.SetupConfig(t, client, root)
2527
pty := ptytest.New(t)
2628
cmd.SetIn(pty.Input())
@@ -36,9 +38,9 @@ func TestFeaturesList(t *testing.T) {
3638
t.Run("JSON", func(t *testing.T) {
3739
t.Parallel()
3840

39-
client := coderdtest.New(t, nil)
41+
client := coderdenttest.New(t, nil)
4042
coderdtest.CreateFirstUser(t, client)
41-
cmd, root := clitest.New(t, "features", "list", "-o", "json")
43+
cmd, root := clitest.NewWithSubcommands(t, cli.EnterpriseSubcommands(), "features", "list", "-o", "json")
4244
clitest.SetupConfig(t, client, root)
4345
doneChan := make(chan struct{})
4446

enterprise/cli/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func enterpriseOnly() []*cobra.Command {
2121
}
2222
return api.AGPL, nil
2323
}),
24+
features(),
2425
licenses(),
2526
}
2627
}

enterprise/coderd/coderd.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ func New(ctx context.Context, options *Options) (*API, error) {
3737
AGPL: coderd.New(options.Options),
3838
Options: options,
3939

40+
activeUsers: codersdk.Feature{
41+
Entitlement: codersdk.EntitlementNotEntitled,
42+
Enabled: false,
43+
},
4044
auditLogs: codersdk.EntitlementNotEntitled,
4145
cancelEntitlementsLoop: cancelFunc,
4246
}

site/src/api/api.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ export const hardCodedCSRFCookie = (): string => {
1616
return csrfToken
1717
}
1818

19+
// defaultEntitlements has a default set of disabled functionality.
20+
export const defaultEntitlements = (): TypesGen.Entitlements => {
21+
const features: TypesGen.Entitlements["features"] = {}
22+
for (const feature in Types.FeatureNames) {
23+
features[feature] = {
24+
enabled: false,
25+
entitlement: "not_entitled",
26+
}
27+
}
28+
return {
29+
features: features,
30+
has_license: false,
31+
warnings: [],
32+
}
33+
}
34+
1935
// Always attach CSRF token to all requests.
2036
// In puppeteer the document is undefined. In those cases, just
2137
// do nothing.
@@ -424,8 +440,15 @@ export const putWorkspaceExtension = async (
424440
}
425441

426442
export const getEntitlements = async (): Promise<TypesGen.Entitlements> => {
427-
const response = await axios.get("/api/v2/entitlements")
428-
return response.data
443+
try {
444+
const response = await axios.get("/api/v2/entitlements")
445+
return response.data
446+
} catch (error) {
447+
if (axios.isAxiosError(error) && error.response?.status === 404) {
448+
return defaultEntitlements()
449+
}
450+
throw error
451+
}
429452
}
430453

431454
interface GetAuditLogsOptions {

site/src/xServices/entitlements/entitlementsXService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export const entitlementsMachine = createMachine(
8484
}),
8585
},
8686
services: {
87-
getEntitlements: () => API.getEntitlements(),
87+
getEntitlements: API.getEntitlements,
8888
},
8989
},
9090
)

0 commit comments

Comments
 (0)