Skip to content

Commit 5073517

Browse files
committed
Merge branch 'main' into matifali/devcontainer
2 parents 766c7b2 + 5b9c378 commit 5073517

24 files changed

+509
-510
lines changed

coderd/audit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"cdr.dev/slog"
1818
"github.com/coder/coder/coderd/audit"
1919
"github.com/coder/coder/coderd/database"
20+
"github.com/coder/coder/coderd/database/db2sdk"
2021
"github.com/coder/coder/coderd/httpapi"
2122
"github.com/coder/coder/coderd/httpmw"
2223
"github.com/coder/coder/coderd/rbac"
@@ -193,7 +194,7 @@ func (api *API) convertAuditLog(ctx context.Context, dblog database.GetAuditLogs
193194

194195
for _, roleName := range dblog.UserRoles {
195196
rbacRole, _ := rbac.RoleByName(roleName)
196-
user.Roles = append(user.Roles, convertRole(rbacRole))
197+
user.Roles = append(user.Roles, db2sdk.Role(rbacRole))
197198
}
198199
}
199200

coderd/coderd.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,13 @@ func New(options *Options) *API {
293293
},
294294
)
295295

296-
staticHandler := site.Handler(site.FS(), binFS, binHashes)
297-
// Static file handler must be wrapped with HSTS handler if the
298-
// StrictTransportSecurityAge is set. We only need to set this header on
299-
// static files since it only affects browsers.
300-
staticHandler = httpmw.HSTS(staticHandler, options.StrictTransportSecurityCfg)
296+
staticHandler := site.New(&site.Options{
297+
BinFS: binFS,
298+
BinHashes: binHashes,
299+
Database: options.Database,
300+
SiteFS: site.FS(),
301+
})
302+
staticHandler.Experiments.Store(&experiments)
301303

302304
oauthConfigs := &httpmw.OAuth2Configs{
303305
Github: options.GithubOAuth2Config,
@@ -313,7 +315,7 @@ func New(options *Options) *API {
313315
ID: uuid.New(),
314316
Options: options,
315317
RootHandler: r,
316-
siteHandler: staticHandler,
318+
SiteHandler: staticHandler,
317319
HTTPAuth: &HTTPAuthorizer{
318320
Authorizer: options.Authorizer,
319321
Logger: options.Logger,
@@ -813,7 +815,11 @@ func New(options *Options) *API {
813815
// By default we do not add extra websocket connections to the CSP
814816
return []string{}
815817
})
816-
r.NotFound(cspMW(compressHandler(http.HandlerFunc(api.siteHandler.ServeHTTP))).ServeHTTP)
818+
819+
// Static file handler must be wrapped with HSTS handler if the
820+
// StrictTransportSecurityAge is set. We only need to set this header on
821+
// static files since it only affects browsers.
822+
r.NotFound(cspMW(compressHandler(httpmw.HSTS(api.SiteHandler, options.StrictTransportSecurityCfg))).ServeHTTP)
817823

818824
// This must be before all middleware to improve the response time.
819825
// So make a new router, and mount the old one as the root.
@@ -858,7 +864,8 @@ type API struct {
858864
// RootHandler serves "/"
859865
RootHandler chi.Router
860866

861-
siteHandler http.Handler
867+
// SiteHandler serves static files for the dashboard.
868+
SiteHandler *site.Handler
862869

863870
WebsocketWaitMutex sync.Mutex
864871
WebsocketWaitGroup sync.WaitGroup

coderd/database/db2sdk/db2sdk.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ import (
55
"encoding/json"
66
"time"
77

8+
"github.com/google/uuid"
9+
810
"github.com/coder/coder/coderd/database"
911
"github.com/coder/coder/coderd/parameter"
12+
"github.com/coder/coder/coderd/rbac"
1013
"github.com/coder/coder/codersdk"
1114
"github.com/coder/coder/provisionersdk/proto"
1215
)
@@ -100,3 +103,31 @@ func ProvisionerJobStatus(provisionerJob database.ProvisionerJob) codersdk.Provi
100103
return codersdk.ProvisionerJobRunning
101104
}
102105
}
106+
107+
func User(user database.User, organizationIDs []uuid.UUID) codersdk.User {
108+
convertedUser := codersdk.User{
109+
ID: user.ID,
110+
Email: user.Email,
111+
CreatedAt: user.CreatedAt,
112+
LastSeenAt: user.LastSeenAt,
113+
Username: user.Username,
114+
Status: codersdk.UserStatus(user.Status),
115+
OrganizationIDs: organizationIDs,
116+
Roles: make([]codersdk.Role, 0, len(user.RBACRoles)),
117+
AvatarURL: user.AvatarURL.String,
118+
}
119+
120+
for _, roleName := range user.RBACRoles {
121+
rbacRole, _ := rbac.RoleByName(roleName)
122+
convertedUser.Roles = append(convertedUser.Roles, Role(rbacRole))
123+
}
124+
125+
return convertedUser
126+
}
127+
128+
func Role(role rbac.Role) codersdk.Role {
129+
return codersdk.Role{
130+
DisplayName: role.DisplayName,
131+
Name: role.Name,
132+
}
133+
}

coderd/members.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"golang.org/x/xerrors"
1010

11+
"github.com/coder/coder/coderd/database/db2sdk"
1112
"github.com/coder/coder/coderd/rbac"
1213

1314
"github.com/coder/coder/coderd/database"
@@ -104,7 +105,7 @@ func convertOrganizationMember(mem database.OrganizationMember) codersdk.Organiz
104105

105106
for _, roleName := range mem.Roles {
106107
rbacRole, _ := rbac.RoleByName(roleName)
107-
convertedMember.Roles = append(convertedMember.Roles, convertRole(rbacRole))
108+
convertedMember.Roles = append(convertedMember.Roles, db2sdk.Role(rbacRole))
108109
}
109110
return convertedMember
110111
}

coderd/roles.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,6 @@ func (api *API) assignableOrgRoles(rw http.ResponseWriter, r *http.Request) {
5555
httpapi.Write(ctx, rw, http.StatusOK, assignableRoles(actorRoles.Actor.Roles, roles))
5656
}
5757

58-
func convertRole(role rbac.Role) codersdk.Role {
59-
return codersdk.Role{
60-
DisplayName: role.DisplayName,
61-
Name: role.Name,
62-
}
63-
}
64-
6558
func assignableRoles(actorRoles rbac.ExpandableRoles, roles []rbac.Role) []codersdk.AssignableRoles {
6659
assignable := make([]codersdk.AssignableRoles, 0)
6760
for _, role := range roles {

coderd/users.go

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/coder/coder/coderd/audit"
1616
"github.com/coder/coder/coderd/database"
17+
"github.com/coder/coder/coderd/database/db2sdk"
1718
"github.com/coder/coder/coderd/database/dbauthz"
1819
"github.com/coder/coder/coderd/gitsshkey"
1920
"github.com/coder/coder/coderd/httpapi"
@@ -401,7 +402,7 @@ func (api *API) postUser(rw http.ResponseWriter, r *http.Request) {
401402
Users: []telemetry.User{telemetry.ConvertUser(user)},
402403
})
403404

404-
httpapi.Write(ctx, rw, http.StatusCreated, convertUser(user, []uuid.UUID{req.OrganizationID}))
405+
httpapi.Write(ctx, rw, http.StatusCreated, db2sdk.User(user, []uuid.UUID{req.OrganizationID}))
405406
}
406407

407408
// @Summary Delete user
@@ -495,7 +496,7 @@ func (api *API) userByName(rw http.ResponseWriter, r *http.Request) {
495496
return
496497
}
497498

498-
httpapi.Write(ctx, rw, http.StatusOK, convertUser(user, organizationIDs))
499+
httpapi.Write(ctx, rw, http.StatusOK, db2sdk.User(user, organizationIDs))
499500
}
500501

501502
// @Summary Update user profile
@@ -580,7 +581,7 @@ func (api *API) putUserProfile(rw http.ResponseWriter, r *http.Request) {
580581
return
581582
}
582583

583-
httpapi.Write(ctx, rw, http.StatusOK, convertUser(updatedUserProfile, organizationIDs))
584+
httpapi.Write(ctx, rw, http.StatusOK, db2sdk.User(updatedUserProfile, organizationIDs))
584585
}
585586

586587
// @Summary Suspend user account
@@ -667,7 +668,7 @@ func (api *API) putUserStatus(status database.UserStatus) func(rw http.ResponseW
667668
return
668669
}
669670

670-
httpapi.Write(ctx, rw, http.StatusOK, convertUser(suspendedUser, organizations))
671+
httpapi.Write(ctx, rw, http.StatusOK, db2sdk.User(suspendedUser, organizations))
671672
}
672673
}
673674

@@ -892,7 +893,7 @@ func (api *API) putUserRoles(rw http.ResponseWriter, r *http.Request) {
892893
return
893894
}
894895

895-
httpapi.Write(ctx, rw, http.StatusOK, convertUser(updatedUser, organizationIDs))
896+
httpapi.Write(ctx, rw, http.StatusOK, db2sdk.User(updatedUser, organizationIDs))
896897
}
897898

898899
// updateSiteUserRoles will ensure only site wide roles are passed in as arguments.
@@ -1087,32 +1088,11 @@ func (api *API) CreateUser(ctx context.Context, store database.Store, req Create
10871088
}, nil)
10881089
}
10891090

1090-
func convertUser(user database.User, organizationIDs []uuid.UUID) codersdk.User {
1091-
convertedUser := codersdk.User{
1092-
ID: user.ID,
1093-
Email: user.Email,
1094-
CreatedAt: user.CreatedAt,
1095-
LastSeenAt: user.LastSeenAt,
1096-
Username: user.Username,
1097-
Status: codersdk.UserStatus(user.Status),
1098-
OrganizationIDs: organizationIDs,
1099-
Roles: make([]codersdk.Role, 0, len(user.RBACRoles)),
1100-
AvatarURL: user.AvatarURL.String,
1101-
}
1102-
1103-
for _, roleName := range user.RBACRoles {
1104-
rbacRole, _ := rbac.RoleByName(roleName)
1105-
convertedUser.Roles = append(convertedUser.Roles, convertRole(rbacRole))
1106-
}
1107-
1108-
return convertedUser
1109-
}
1110-
11111091
func convertUsers(users []database.User, organizationIDsByUserID map[uuid.UUID][]uuid.UUID) []codersdk.User {
11121092
converted := make([]codersdk.User, 0, len(users))
11131093
for _, u := range users {
11141094
userOrganizationIDs := organizationIDsByUserID[u.ID]
1115-
converted = append(converted, convertUser(u, userOrganizationIDs))
1095+
converted = append(converted, db2sdk.User(u, userOrganizationIDs))
11161096
}
11171097
return converted
11181098
}

cryptorand/errors_test.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,6 @@ func TestRandError(t *testing.T) {
3030
require.ErrorIs(t, err, io.ErrShortBuffer, "expected Int63 error")
3131
})
3232

33-
t.Run("Uint64", func(t *testing.T) {
34-
_, err := cryptorand.Uint64()
35-
require.ErrorIs(t, err, io.ErrShortBuffer, "expected Uint64 error")
36-
})
37-
38-
t.Run("Int31", func(t *testing.T) {
39-
_, err := cryptorand.Int31()
40-
require.ErrorIs(t, err, io.ErrShortBuffer, "expected Int31 error")
41-
})
42-
43-
t.Run("Int31n", func(t *testing.T) {
44-
_, err := cryptorand.Int31n(100)
45-
require.ErrorIs(t, err, io.ErrShortBuffer, "expected Int31n error")
46-
})
47-
48-
t.Run("Uint32", func(t *testing.T) {
49-
_, err := cryptorand.Uint32()
50-
require.ErrorIs(t, err, io.ErrShortBuffer, "expected Uint32 error")
51-
})
52-
53-
t.Run("Int", func(t *testing.T) {
54-
_, err := cryptorand.Int()
55-
require.ErrorIs(t, err, io.ErrShortBuffer, "expected Int error")
56-
})
57-
5833
t.Run("Intn_32bit", func(t *testing.T) {
5934
_, err := cryptorand.Intn(100)
6035
require.ErrorIs(t, err, io.ErrShortBuffer, "expected Intn error")
@@ -70,11 +45,6 @@ func TestRandError(t *testing.T) {
7045
require.ErrorIs(t, err, io.ErrShortBuffer, "expected Float64 error")
7146
})
7247

73-
t.Run("Float32", func(t *testing.T) {
74-
_, err := cryptorand.Float32()
75-
require.ErrorIs(t, err, io.ErrShortBuffer, "expected Float32 error")
76-
})
77-
7848
t.Run("StringCharset", func(t *testing.T) {
7949
_, err := cryptorand.HexString(10)
8050
require.ErrorIs(t, err, io.ErrShortBuffer, "expected HexString error")

0 commit comments

Comments
 (0)