Skip to content

Commit a10c4d5

Browse files
committed
Add endpoints for gitauth
1 parent fb5195d commit a10c4d5

18 files changed

+810
-15
lines changed

.vscode/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"cSpell.words": [
33
"apps",
4+
"ASKPASS",
45
"awsidentity",
56
"bodyclose",
67
"buildinfo",
@@ -29,6 +30,7 @@
2930
"eventsourcemock",
3031
"fatih",
3132
"Formik",
33+
"gitauth",
3234
"gitsshkey",
3335
"goarch",
3436
"gographviz",

coderd/coderd.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package coderd
33
import (
44
"crypto/tls"
55
"crypto/x509"
6+
"fmt"
67
"io"
78
"net/http"
89
"net/url"
@@ -82,6 +83,7 @@ type Options struct {
8283
Telemetry telemetry.Reporter
8384
TracerProvider trace.TracerProvider
8485
AutoImportTemplates []AutoImportTemplate
86+
GitAuthConfigs []*GitAuthConfig
8587

8688
// TLSCertificates is used to mesh DERP servers securely.
8789
TLSCertificates []tls.Certificate
@@ -260,6 +262,17 @@ func New(options *Options) *API {
260262
})
261263
})
262264

265+
r.Route("/gitauth", func(r chi.Router) {
266+
for _, gitAuthConfig := range options.GitAuthConfigs {
267+
r.Route(fmt.Sprintf("/%s", gitAuthConfig.ID), func(r chi.Router) {
268+
r.Use(
269+
httpmw.ExtractOAuth2(gitAuthConfig),
270+
apiKeyMiddleware,
271+
)
272+
r.Get("/callback", api.gitAuthCallback(gitAuthConfig))
273+
})
274+
}
275+
})
263276
r.Route("/api/v2", func(r chi.Router) {
264277
api.APIHandler = r
265278

@@ -465,6 +478,7 @@ func New(options *Options) *API {
465478
r.Get("/metadata", api.workspaceAgentMetadata)
466479
r.Post("/version", api.postWorkspaceAgentVersion)
467480
r.Post("/app-health", api.postWorkspaceAppHealth)
481+
r.Get("/gitauth", api.workspaceAgentsGitAuth)
468482
r.Get("/gitsshkey", api.agentGitSSHKey)
469483
r.Get("/coordinate", api.workspaceAgentCoordinate)
470484
r.Get("/report-stats", api.workspaceAgentReportStats)

coderd/coderdtest/coderdtest.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ type Options struct {
8484
AutobuildStats chan<- executor.Stats
8585
Auditor audit.Auditor
8686
TLSCertificates []tls.Certificate
87+
GitAuthConfigs []*coderd.GitAuthConfig
8788

8889
// IncludeProvisionerDaemon when true means to start an in-memory provisionerD
8990
IncludeProvisionerDaemon bool
@@ -231,6 +232,7 @@ func NewOptions(t *testing.T, options *Options) (func(http.Handler), context.Can
231232
Database: options.Database,
232233
Pubsub: options.Pubsub,
233234
Experimental: options.Experimental,
235+
GitAuthConfigs: options.GitAuthConfigs,
234236

235237
Auditor: options.Auditor,
236238
AWSCertificates: options.AWSCertificates,

coderd/database/databasefake/databasefake.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func New() database.Store {
3434
organizationMembers: make([]database.OrganizationMember, 0),
3535
organizations: make([]database.Organization, 0),
3636
users: make([]database.User, 0),
37+
gitAuthLinks: make([]database.GitAuthLink, 0),
3738
groups: make([]database.Group, 0),
3839
groupMembers: make([]database.GroupMember, 0),
3940
auditLogs: make([]database.AuditLog, 0),
@@ -90,6 +91,7 @@ type data struct {
9091
agentStats []database.AgentStat
9192
auditLogs []database.AuditLog
9293
files []database.File
94+
gitAuthLinks []database.GitAuthLink
9395
gitSSHKey []database.GitSSHKey
9496
groups []database.Group
9597
groupMembers []database.GroupMember
@@ -3286,3 +3288,54 @@ func (q *fakeQuerier) GetReplicasUpdatedAfter(_ context.Context, updatedAt time.
32863288
}
32873289
return replicas, nil
32883290
}
3291+
3292+
func (q *fakeQuerier) GetGitAuthLink(_ context.Context, arg database.GetGitAuthLinkParams) (database.GitAuthLink, error) {
3293+
q.mutex.RLock()
3294+
defer q.mutex.RUnlock()
3295+
for _, gitAuthLink := range q.gitAuthLinks {
3296+
if arg.UserID != gitAuthLink.UserID {
3297+
continue
3298+
}
3299+
if arg.ProviderID != gitAuthLink.ProviderID {
3300+
continue
3301+
}
3302+
return gitAuthLink, nil
3303+
}
3304+
return database.GitAuthLink{}, sql.ErrNoRows
3305+
}
3306+
3307+
func (q *fakeQuerier) InsertGitAuthLink(_ context.Context, arg database.InsertGitAuthLinkParams) (database.GitAuthLink, error) {
3308+
q.mutex.Lock()
3309+
defer q.mutex.Unlock()
3310+
// nolint:gosimple
3311+
gitAuthLink := database.GitAuthLink{
3312+
ProviderID: arg.ProviderID,
3313+
UserID: arg.UserID,
3314+
CreatedAt: arg.CreatedAt,
3315+
UpdatedAt: arg.UpdatedAt,
3316+
OAuthAccessToken: arg.OAuthAccessToken,
3317+
OAuthRefreshToken: arg.OAuthRefreshToken,
3318+
OAuthExpiry: arg.OAuthExpiry,
3319+
}
3320+
q.gitAuthLinks = append(q.gitAuthLinks, gitAuthLink)
3321+
return gitAuthLink, nil
3322+
}
3323+
3324+
func (q *fakeQuerier) UpdateGitAuthLink(_ context.Context, arg database.UpdateGitAuthLinkParams) error {
3325+
q.mutex.Lock()
3326+
defer q.mutex.Unlock()
3327+
for index, gitAuthLink := range q.gitAuthLinks {
3328+
if gitAuthLink.ProviderID != arg.ProviderID {
3329+
continue
3330+
}
3331+
if gitAuthLink.UserID != arg.UserID {
3332+
continue
3333+
}
3334+
gitAuthLink.UpdatedAt = arg.UpdatedAt
3335+
gitAuthLink.OAuthAccessToken = arg.OAuthAccessToken
3336+
gitAuthLink.OAuthRefreshToken = arg.OAuthRefreshToken
3337+
gitAuthLink.OAuthExpiry = arg.OAuthExpiry
3338+
q.gitAuthLinks[index] = gitAuthLink
3339+
}
3340+
return nil
3341+
}

coderd/database/dump.sql

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE git_auth_links;
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
CREATE TABLE IF NOT EXISTS git_provider_links (
1+
CREATE TABLE IF NOT EXISTS git_auth_links (
2+
provider_id text NOT NULL,
23
user_id uuid NOT NULL,
3-
url text NOT NULL,
44
created_at timestamptz NOT NULL,
55
updated_at timestamptz NOT NULL,
66
oauth_access_token text NOT NULL,
77
oauth_refresh_token text NOT NULL,
8-
oauth_expiry text NOT NULL
8+
oauth_expiry timestamptz NOT NULL,
9+
UNIQUE(provider_id, user_id)
910
);

coderd/database/migrations/000063_gitprovider.down.sql

Lines changed: 0 additions & 1 deletion
This file was deleted.

coderd/database/models.go

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

coderd/database/querier.go

Lines changed: 3 additions & 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: 107 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/gitauth.sql

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
-- name: GetGitAuthLink :one
2+
SELECT * FROM git_auth_links WHERE provider_id = $1 AND user_id = $2;
3+
4+
-- name: InsertGitAuthLink :one
5+
INSERT INTO git_auth_links (
6+
provider_id,
7+
user_id,
8+
created_at,
9+
updated_at,
10+
oauth_access_token,
11+
oauth_refresh_token,
12+
oauth_expiry
13+
) VALUES (
14+
$1,
15+
$2,
16+
$3,
17+
$4,
18+
$5,
19+
$6,
20+
$7
21+
) RETURNING *;
22+
23+
-- name: UpdateGitAuthLink :exec
24+
UPDATE git_auth_links SET
25+
updated_at = $3,
26+
oauth_access_token = $4,
27+
oauth_refresh_token = $5,
28+
oauth_expiry = $6
29+
WHERE provider_id = $1 AND user_id = $2;

coderd/database/unique_constraint.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.

0 commit comments

Comments
 (0)