Skip to content

Commit c9bce19

Browse files
authored
GET license endpoint (coder#3651)
* GET license endpoint Signed-off-by: Spike Curtis <spike@coder.com> * SDK GetLicenses -> Licenses Signed-off-by: Spike Curtis <spike@coder.com> Signed-off-by: Spike Curtis <spike@coder.com>
1 parent da54874 commit c9bce19

File tree

13 files changed

+223
-11
lines changed

13 files changed

+223
-11
lines changed

coderd/authorize.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import (
1010
"github.com/coder/coder/coderd/rbac"
1111
)
1212

13-
func AuthorizeFilter[O rbac.Objecter](api *API, r *http.Request, action rbac.Action, objects []O) ([]O, error) {
13+
func AuthorizeFilter[O rbac.Objecter](h *HTTPAuthorizer, r *http.Request, action rbac.Action, objects []O) ([]O, error) {
1414
roles := httpmw.AuthorizationUserRoles(r)
15-
objects, err := rbac.Filter(r.Context(), api.Authorizer, roles.ID.String(), roles.Roles, action, objects)
15+
objects, err := rbac.Filter(r.Context(), h.Authorizer, roles.ID.String(), roles.Roles, action, objects)
1616
if err != nil {
1717
// Log the error as Filter should not be erroring.
18-
api.Logger.Error(r.Context(), "filter failed",
18+
h.Logger.Error(r.Context(), "filter failed",
1919
slog.Error(err),
2020
slog.F("user_id", roles.ID),
2121
slog.F("username", roles.Username),

coderd/database/databasefake/databasefake.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2278,8 +2278,8 @@ func (q *fakeQuerier) GetDeploymentID(_ context.Context) (string, error) {
22782278

22792279
func (q *fakeQuerier) InsertLicense(
22802280
_ context.Context, arg database.InsertLicenseParams) (database.License, error) {
2281-
q.mutex.RLock()
2282-
defer q.mutex.RUnlock()
2281+
q.mutex.Lock()
2282+
defer q.mutex.Unlock()
22832283

22842284
l := database.License{
22852285
ID: q.lastLicenseID + 1,
@@ -2292,6 +2292,15 @@ func (q *fakeQuerier) InsertLicense(
22922292
return l, nil
22932293
}
22942294

2295+
func (q *fakeQuerier) GetLicenses(_ context.Context) ([]database.License, error) {
2296+
q.mutex.RLock()
2297+
defer q.mutex.RUnlock()
2298+
2299+
results := append([]database.License{}, q.licenses...)
2300+
sort.Slice(results, func(i, j int) bool { return results[i].ID < results[j].ID })
2301+
return results, nil
2302+
}
2303+
22952304
func (q *fakeQuerier) GetUserLinkByLinkedID(_ context.Context, id string) (database.UserLink, error) {
22962305
q.mutex.RLock()
22972306
defer q.mutex.RUnlock()

coderd/database/modelmethods.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@ func (f File) RBACObject() rbac.Object {
4343
func (User) RBACObject() rbac.Object {
4444
return rbac.ResourceUser
4545
}
46+
47+
func (License) RBACObject() rbac.Object {
48+
return rbac.ResourceLicense
49+
}

coderd/database/querier.go

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

coderd/database/queries.sql.go

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

coderd/database/queries/licenses.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ INSERT INTO
77
)
88
VALUES
99
($1, $2, $3) RETURNING *;
10+
11+
12+
-- name: GetLicenses :many
13+
SELECT *
14+
FROM licenses
15+
ORDER BY (id);

coderd/provisionerdaemons.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (api *API) provisionerDaemons(rw http.ResponseWriter, r *http.Request) {
5050
if daemons == nil {
5151
daemons = []database.ProvisionerDaemon{}
5252
}
53-
daemons, err = AuthorizeFilter(api, r, rbac.ActionRead, daemons)
53+
daemons, err = AuthorizeFilter(api.httpAuth, r, rbac.ActionRead, daemons)
5454
if err != nil {
5555
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
5656
Message: "Internal error fetching provisioner daemons.",

coderd/templates.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func (api *API) templatesByOrganization(rw http.ResponseWriter, r *http.Request)
292292
}
293293

294294
// Filter templates based on rbac permissions
295-
templates, err = AuthorizeFilter(api, r, rbac.ActionRead, templates)
295+
templates, err = AuthorizeFilter(api.httpAuth, r, rbac.ActionRead, templates)
296296
if err != nil {
297297
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
298298
Message: "Internal error fetching templates.",

coderd/users.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func (api *API) users(rw http.ResponseWriter, r *http.Request) {
158158
return
159159
}
160160

161-
users, err = AuthorizeFilter(api, r, rbac.ActionRead, users)
161+
users, err = AuthorizeFilter(api.httpAuth, r, rbac.ActionRead, users)
162162
if err != nil {
163163
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
164164
Message: "Internal error fetching users.",
@@ -503,7 +503,7 @@ func (api *API) userRoles(rw http.ResponseWriter, r *http.Request) {
503503
}
504504

505505
// Only include ones we can read from RBAC.
506-
memberships, err = AuthorizeFilter(api, r, rbac.ActionRead, memberships)
506+
memberships, err = AuthorizeFilter(api.httpAuth, r, rbac.ActionRead, memberships)
507507
if err != nil {
508508
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
509509
Message: "Internal error fetching memberships.",
@@ -631,7 +631,7 @@ func (api *API) organizationsByUser(rw http.ResponseWriter, r *http.Request) {
631631
}
632632

633633
// Only return orgs the user can read.
634-
organizations, err = AuthorizeFilter(api, r, rbac.ActionRead, organizations)
634+
organizations, err = AuthorizeFilter(api.httpAuth, r, rbac.ActionRead, organizations)
635635
if err != nil {
636636
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
637637
Message: "Internal error fetching organizations.",

coderd/workspaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (api *API) workspaces(rw http.ResponseWriter, r *http.Request) {
143143
}
144144

145145
// Only return workspaces the user can read
146-
workspaces, err = AuthorizeFilter(api, r, rbac.ActionRead, workspaces)
146+
workspaces, err = AuthorizeFilter(api.httpAuth, r, rbac.ActionRead, workspaces)
147147
if err != nil {
148148
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
149149
Message: "Internal error fetching workspaces.",

0 commit comments

Comments
 (0)