@@ -30,6 +30,7 @@ import (
30
30
"github.com/google/uuid"
31
31
"github.com/pion/turn/v2"
32
32
"github.com/pion/webrtc/v3"
33
+ "github.com/prometheus/client_golang/prometheus"
33
34
"github.com/prometheus/client_golang/prometheus/promhttp"
34
35
"github.com/spf13/afero"
35
36
"github.com/spf13/cobra"
@@ -53,6 +54,7 @@ import (
53
54
"github.com/coder/coder/coderd/database/databasefake"
54
55
"github.com/coder/coder/coderd/devtunnel"
55
56
"github.com/coder/coder/coderd/gitsshkey"
57
+ "github.com/coder/coder/coderd/prometheusmetrics"
56
58
"github.com/coder/coder/coderd/telemetry"
57
59
"github.com/coder/coder/coderd/tracing"
58
60
"github.com/coder/coder/coderd/turnconn"
@@ -85,6 +87,7 @@ func server() *cobra.Command {
85
87
oauth2GithubAllowedOrganizations []string
86
88
oauth2GithubAllowedTeams []string
87
89
oauth2GithubAllowSignups bool
90
+ oauth2GithubEnterpriseBaseURL string
88
91
oidcAllowSignups bool
89
92
oidcClientID string
90
93
oidcClientSecret string
@@ -284,7 +287,7 @@ func server() *cobra.Command {
284
287
}
285
288
286
289
if oauth2GithubClientSecret != "" {
287
- options .GithubOAuth2Config , err = configureGithubOAuth2 (accessURLParsed , oauth2GithubClientID , oauth2GithubClientSecret , oauth2GithubAllowSignups , oauth2GithubAllowedOrganizations , oauth2GithubAllowedTeams )
290
+ options .GithubOAuth2Config , err = configureGithubOAuth2 (accessURLParsed , oauth2GithubClientID , oauth2GithubClientSecret , oauth2GithubAllowSignups , oauth2GithubAllowedOrganizations , oauth2GithubAllowedTeams , oauth2GithubEnterpriseBaseURL )
288
291
if err != nil {
289
292
return xerrors .Errorf ("configure github oauth2: %w" , err )
290
293
}
@@ -392,6 +395,32 @@ func server() *cobra.Command {
392
395
defer options .Telemetry .Close ()
393
396
}
394
397
398
+ // This prevents the pprof import from being accidentally deleted.
399
+ _ = pprof .Handler
400
+ if pprofEnabled {
401
+ //nolint:revive
402
+ defer serveHandler (ctx , logger , nil , pprofAddress , "pprof" )()
403
+ }
404
+ if promEnabled {
405
+ options .PrometheusRegistry = prometheus .NewRegistry ()
406
+ closeUsersFunc , err := prometheusmetrics .ActiveUsers (ctx , options .PrometheusRegistry , options .Database , 0 )
407
+ if err != nil {
408
+ return xerrors .Errorf ("register active users prometheus metric: %w" , err )
409
+ }
410
+ defer closeUsersFunc ()
411
+
412
+ closeWorkspacesFunc , err := prometheusmetrics .Workspaces (ctx , options .PrometheusRegistry , options .Database , 0 )
413
+ if err != nil {
414
+ return xerrors .Errorf ("register workspaces prometheus metric: %w" , err )
415
+ }
416
+ defer closeWorkspacesFunc ()
417
+
418
+ //nolint:revive
419
+ defer serveHandler (ctx , logger , promhttp .InstrumentMetricHandler (
420
+ options .PrometheusRegistry , promhttp .HandlerFor (options .PrometheusRegistry , promhttp.HandlerOpts {}),
421
+ ), promAddress , "prometheus" )()
422
+ }
423
+
395
424
coderAPI := coderd .New (options )
396
425
defer coderAPI .Close ()
397
426
@@ -406,17 +435,6 @@ func server() *cobra.Command {
406
435
}
407
436
}
408
437
409
- // This prevents the pprof import from being accidentally deleted.
410
- _ = pprof .Handler
411
- if pprofEnabled {
412
- //nolint:revive
413
- defer serveHandler (ctx , logger , nil , pprofAddress , "pprof" )()
414
- }
415
- if promEnabled {
416
- //nolint:revive
417
- defer serveHandler (ctx , logger , promhttp .Handler (), promAddress , "prometheus" )()
418
- }
419
-
420
438
// Since errCh only has one buffered slot, all routines
421
439
// sending on it must be wrapped in a select/default to
422
440
// avoid leaving dangling goroutines waiting for the
@@ -678,6 +696,8 @@ func server() *cobra.Command {
678
696
"Specifies teams inside organizations the user must be a member of to authenticate with GitHub. Formatted as: <organization-name>/<team-slug>." )
679
697
cliflag .BoolVarP (root .Flags (), & oauth2GithubAllowSignups , "oauth2-github-allow-signups" , "" , "CODER_OAUTH2_GITHUB_ALLOW_SIGNUPS" , false ,
680
698
"Specifies whether new users can sign up with GitHub." )
699
+ cliflag .StringVarP (root .Flags (), & oauth2GithubEnterpriseBaseURL , "oauth2-github-enterprise-base-url" , "" , "CODER_OAUTH2_GITHUB_ENTERPRISE_BASE_URL" , "" ,
700
+ "Specifies the base URL of a GitHub Enterprise instance to use for oauth2." )
681
701
cliflag .BoolVarP (root .Flags (), & oidcAllowSignups , "oidc-allow-signups" , "" , "CODER_OIDC_ALLOW_SIGNUPS" , true ,
682
702
"Specifies whether new users can sign up with OIDC." )
683
703
cliflag .StringVarP (root .Flags (), & oidcClientID , "oidc-client-id" , "" , "CODER_OIDC_CLIENT_ID" , "" ,
@@ -955,7 +975,7 @@ func configureTLS(listener net.Listener, tlsMinVersion, tlsClientAuth, tlsCertFi
955
975
return tls .NewListener (listener , tlsConfig ), nil
956
976
}
957
977
958
- func configureGithubOAuth2 (accessURL * url.URL , clientID , clientSecret string , allowSignups bool , allowOrgs []string , rawTeams []string ) (* coderd.GithubOAuth2Config , error ) {
978
+ func configureGithubOAuth2 (accessURL * url.URL , clientID , clientSecret string , allowSignups bool , allowOrgs []string , rawTeams []string , enterpriseBaseURL string ) (* coderd.GithubOAuth2Config , error ) {
959
979
redirectURL , err := accessURL .Parse ("/api/v2/users/oauth2/github/callback" )
960
980
if err != nil {
961
981
return nil , xerrors .Errorf ("parse github oauth callback url: %w" , err )
@@ -971,11 +991,38 @@ func configureGithubOAuth2(accessURL *url.URL, clientID, clientSecret string, al
971
991
Slug : parts [1 ],
972
992
})
973
993
}
994
+ createClient := func (client * http.Client ) (* github.Client , error ) {
995
+ if enterpriseBaseURL != "" {
996
+ return github .NewEnterpriseClient (enterpriseBaseURL , "" , client )
997
+ }
998
+ return github .NewClient (client ), nil
999
+ }
1000
+
1001
+ endpoint := xgithub .Endpoint
1002
+ if enterpriseBaseURL != "" {
1003
+ enterpriseURL , err := url .Parse (enterpriseBaseURL )
1004
+ if err != nil {
1005
+ return nil , xerrors .Errorf ("parse enterprise base url: %w" , err )
1006
+ }
1007
+ authURL , err := enterpriseURL .Parse ("/login/oauth/authorize" )
1008
+ if err != nil {
1009
+ return nil , xerrors .Errorf ("parse enterprise auth url: %w" , err )
1010
+ }
1011
+ tokenURL , err := enterpriseURL .Parse ("/login/oauth/access_token" )
1012
+ if err != nil {
1013
+ return nil , xerrors .Errorf ("parse enterprise token url: %w" , err )
1014
+ }
1015
+ endpoint = oauth2.Endpoint {
1016
+ AuthURL : authURL .String (),
1017
+ TokenURL : tokenURL .String (),
1018
+ }
1019
+ }
1020
+
974
1021
return & coderd.GithubOAuth2Config {
975
1022
OAuth2Config : & oauth2.Config {
976
1023
ClientID : clientID ,
977
1024
ClientSecret : clientSecret ,
978
- Endpoint : xgithub . Endpoint ,
1025
+ Endpoint : endpoint ,
979
1026
RedirectURL : redirectURL .String (),
980
1027
Scopes : []string {
981
1028
"read:user" ,
@@ -987,15 +1034,27 @@ func configureGithubOAuth2(accessURL *url.URL, clientID, clientSecret string, al
987
1034
AllowOrganizations : allowOrgs ,
988
1035
AllowTeams : allowTeams ,
989
1036
AuthenticatedUser : func (ctx context.Context , client * http.Client ) (* github.User , error ) {
990
- user , _ , err := github .NewClient (client ).Users .Get (ctx , "" )
1037
+ api , err := createClient (client )
1038
+ if err != nil {
1039
+ return nil , err
1040
+ }
1041
+ user , _ , err := api .Users .Get (ctx , "" )
991
1042
return user , err
992
1043
},
993
1044
ListEmails : func (ctx context.Context , client * http.Client ) ([]* github.UserEmail , error ) {
994
- emails , _ , err := github .NewClient (client ).Users .ListEmails (ctx , & github.ListOptions {})
1045
+ api , err := createClient (client )
1046
+ if err != nil {
1047
+ return nil , err
1048
+ }
1049
+ emails , _ , err := api .Users .ListEmails (ctx , & github.ListOptions {})
995
1050
return emails , err
996
1051
},
997
1052
ListOrganizationMemberships : func (ctx context.Context , client * http.Client ) ([]* github.Membership , error ) {
998
- memberships , _ , err := github .NewClient (client ).Organizations .ListOrgMemberships (ctx , & github.ListOrgMembershipsOptions {
1053
+ api , err := createClient (client )
1054
+ if err != nil {
1055
+ return nil , err
1056
+ }
1057
+ memberships , _ , err := api .Organizations .ListOrgMemberships (ctx , & github.ListOrgMembershipsOptions {
999
1058
State : "active" ,
1000
1059
ListOptions : github.ListOptions {
1001
1060
PerPage : 100 ,
@@ -1004,7 +1063,11 @@ func configureGithubOAuth2(accessURL *url.URL, clientID, clientSecret string, al
1004
1063
return memberships , err
1005
1064
},
1006
1065
TeamMembership : func (ctx context.Context , client * http.Client , org , teamSlug , username string ) (* github.Membership , error ) {
1007
- team , _ , err := github .NewClient (client ).Teams .GetTeamMembershipBySlug (ctx , org , teamSlug , username )
1066
+ api , err := createClient (client )
1067
+ if err != nil {
1068
+ return nil , err
1069
+ }
1070
+ team , _ , err := api .Teams .GetTeamMembershipBySlug (ctx , org , teamSlug , username )
1008
1071
return team , err
1009
1072
},
1010
1073
}, nil
0 commit comments