Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0bab70d

Browse files
committed
ended up with a global factory funciton
1 parent eb1d073 commit 0bab70d

File tree

4 files changed

+32
-34
lines changed

4 files changed

+32
-34
lines changed

cli/server.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import (
5555

5656
"cdr.dev/slog"
5757
"cdr.dev/slog/sloggers/sloghuman"
58+
"github.com/coder/coder/v2/coderd/entitlements"
5859
"github.com/coder/coder/v2/coderd/idpsync"
5960
"github.com/coder/pretty"
6061
"github.com/coder/quartz"
@@ -108,7 +109,7 @@ import (
108109
"github.com/coder/coder/v2/tailnet"
109110
)
110111

111-
func createOIDCConfig(ctx context.Context, logger slog.Logger, vals *codersdk.DeploymentValues) (*coderd.OIDCConfig, error) {
112+
func createOIDCConfig(ctx context.Context, logger slog.Logger, entitlements *entitlements.Set, vals *codersdk.DeploymentValues) (*coderd.OIDCConfig, error) {
112113
if vals.OIDC.ClientID == "" {
113114
return nil, xerrors.Errorf("OIDC client ID must be set!")
114115
}
@@ -170,13 +171,6 @@ func createOIDCConfig(ctx context.Context, logger slog.Logger, vals *codersdk.De
170171
groupAllowList[group] = true
171172
}
172173

173-
idpSyncSetting := idpsync.SyncSettings{
174-
OrganizationField: vals.OIDC.OrganizationField.Value(),
175-
OrganizationMapping: vals.OIDC.OrganizationMapping.Value,
176-
OrganizationAssignDefault: vals.OIDC.OrganizationAssignDefault.Value(),
177-
}
178-
syncer.Configure(idpSyncSetting)
179-
180174
return &coderd.OIDCConfig{
181175
OAuth2Config: useCfg,
182176
Provider: oidcProvider,
@@ -205,7 +199,11 @@ func createOIDCConfig(ctx context.Context, logger slog.Logger, vals *codersdk.De
205199
SignupsDisabledText: vals.OIDC.SignupsDisabledText.String(),
206200
IconURL: vals.OIDC.IconURL.String(),
207201
IgnoreEmailVerified: vals.OIDC.IgnoreEmailVerified.Value(),
208-
IDPSync: syncer,
202+
IDPSync: idpsync.NewSync(logger, entitlements, idpsync.SyncSettings{
203+
OrganizationField: vals.OIDC.OrganizationField.Value(),
204+
OrganizationMapping: vals.OIDC.OrganizationMapping.Value,
205+
OrganizationAssignDefault: vals.OIDC.OrganizationAssignDefault.Value(),
206+
}),
209207
}, nil
210208
}
211209

coderd/idpsync/idpsync.go

+12-15
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,21 @@ import (
1010

1111
"cdr.dev/slog"
1212
"github.com/coder/coder/v2/coderd/database"
13+
"github.com/coder/coder/v2/coderd/entitlements"
1314
"github.com/coder/coder/v2/coderd/httpapi"
1415
"github.com/coder/coder/v2/codersdk"
1516
"github.com/coder/coder/v2/site"
1617
)
1718

19+
// NewSync is a factory function for creating an IDP sync object.
20+
// Due to the way we instantiate Coder, there is no way for the enterprise
21+
// cli wrapper to pass in the enterprise IDP sync object.
22+
// So instead, if the code is compiled with the enterprise logic, it will
23+
// override this function to return the enterprise IDP sync object.
24+
// For unit testing, the callers can specifically choose which "NewSync" to use.
25+
var NewSync = NewAGPLSync
26+
1827
type IDPSync interface {
19-
// Configure is a method on the struct only because it is easier to configure
20-
// from the AGPL initialization. For the enterprise code to get these settings,
21-
// it makes sense to have the AGPL call 'Configure' rather than duplicate
22-
// the code to create these settings.
23-
Configure(settings SyncSettings)
2428
// ParseOrganizationClaims takes claims from an OIDC provider, and returns the
2529
// organization sync params for assigning users into organizations.
2630
ParseOrganizationClaims(ctx context.Context, _ map[string]interface{}) (OrganizationParams, *HttpError)
@@ -50,20 +54,13 @@ type SyncSettings struct {
5054
OrganizationAssignDefault bool
5155
}
5256

53-
func NewSync(logger slog.Logger) *AGPLIDPSync {
57+
func NewAGPLSync(logger slog.Logger, _ *entitlements.Set, settings SyncSettings) IDPSync {
5458
return &AGPLIDPSync{
55-
Logger: logger.Named("idp-sync"),
56-
SyncSettings: SyncSettings{
57-
// A sane default
58-
OrganizationAssignDefault: true,
59-
},
59+
Logger: logger.Named("idp-sync"),
60+
SyncSettings: settings,
6061
}
6162
}
6263

63-
func (s *AGPLIDPSync) Configure(settings SyncSettings) {
64-
s.SyncSettings = settings
65-
}
66-
6764
// ParseStringSliceClaim parses the claim for groups and roles, expected []string.
6865
//
6966
// Some providers like ADFS return a single string instead of an array if there

enterprise/coderd/enidpsync/enidpsync.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ import (
77
"github.com/coder/coder/v2/coderd/idpsync"
88
)
99

10+
func init() {
11+
idpsync.NewSync = NewSync
12+
}
13+
1014
type EnterpriseIDPSync struct {
1115
entitlements *entitlements.Set
12-
agpl *idpsync.AGPLIDPSync
16+
*idpsync.AGPLIDPSync
1317
}
1418

15-
func NewSync(logger slog.Logger, entitlements *entitlements.Set) *EnterpriseIDPSync {
19+
func NewSync(logger slog.Logger, entitlements *entitlements.Set, settings idpsync.SyncSettings) idpsync.IDPSync {
1620
return &EnterpriseIDPSync{
1721
entitlements: entitlements,
18-
agpl: idpsync.NewSync(logger),
22+
AGPLIDPSync: idpsync.NewAGPLSync(logger, entitlements, settings),
1923
}
2024
}

enterprise/coderd/enidpsync/organizations.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,18 @@ import (
1313
)
1414

1515
func (e EnterpriseIDPSync) ParseOrganizationClaims(ctx context.Context, mergedClaims map[string]interface{}) (idpsync.OrganizationParams, *idpsync.HttpError) {
16-
s := e.agpl
1716
if !e.entitlements.Enabled(codersdk.FeatureMultipleOrganizations) {
1817
// Default to agpl if multi-org is not enabled
19-
return e.agpl.ParseOrganizationClaims(ctx, mergedClaims)
18+
return e.AGPLIDPSync.ParseOrganizationClaims(ctx, mergedClaims)
2019
}
2120

2221
// nolint:gocritic // all syncing is done as a system user
2322
ctx = dbauthz.AsSystemRestricted(ctx)
2423
userOrganizations := make([]uuid.UUID, 0)
2524

2625
// Pull extra organizations from the claims.
27-
if s.OrganizationField != "" {
28-
organizationRaw, ok := mergedClaims[s.OrganizationField]
26+
if e.OrganizationField != "" {
27+
organizationRaw, ok := mergedClaims[e.OrganizationField]
2928
if ok {
3029
parsedOrganizations, err := idpsync.ParseStringSliceClaim(organizationRaw)
3130
if err != nil {
@@ -41,7 +40,7 @@ func (e EnterpriseIDPSync) ParseOrganizationClaims(ctx context.Context, mergedCl
4140
// Keep track of which claims are not mapped for debugging purposes.
4241
var ignored []string
4342
for _, parsedOrg := range parsedOrganizations {
44-
if mappedOrganization, ok := s.OrganizationMapping[parsedOrg]; ok {
43+
if mappedOrganization, ok := e.OrganizationMapping[parsedOrg]; ok {
4544
// parsedOrg is in the mapping, so add the mapped organizations to the
4645
// user's organizations.
4746
userOrganizations = append(userOrganizations, mappedOrganization...)
@@ -50,7 +49,7 @@ func (e EnterpriseIDPSync) ParseOrganizationClaims(ctx context.Context, mergedCl
5049
}
5150
}
5251

53-
s.Logger.Debug(ctx, "parsed organizations from claim",
52+
e.Logger.Debug(ctx, "parsed organizations from claim",
5453
slog.F("len", len(parsedOrganizations)),
5554
slog.F("ignored", ignored),
5655
slog.F("organizations", parsedOrganizations),
@@ -60,7 +59,7 @@ func (e EnterpriseIDPSync) ParseOrganizationClaims(ctx context.Context, mergedCl
6059

6160
return idpsync.OrganizationParams{
6261
SyncEnabled: true,
63-
IncludeDefault: s.OrganizationAssignDefault,
62+
IncludeDefault: e.OrganizationAssignDefault,
6463
Organizations: userOrganizations,
6564
}, nil
6665
}

0 commit comments

Comments
 (0)