-
Notifications
You must be signed in to change notification settings - Fork 876
chore: implement organization sync and create idpsync package #14432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
a8d0495
chore: implement filters for the organizations query
Emyrk d788be7
chore: implement organization sync and create idpsync package
Emyrk f596da1
chore: refactor into agpl and enterprise
Emyrk d42772c
compilation fixes
Emyrk 7d3ec14
fix compile issues
Emyrk 9287fb9
trying to figure out how to initialize both AGPL and enterprise
Emyrk 9c12b18
ended up with a global factory funciton
Emyrk 210239f
fixup compile issues
Emyrk 94e05e7
fixup errors
Emyrk e2badf4
fixup some comments
Emyrk eb7e2c5
Actually enable org sync in the oidc flow
Emyrk 951a724
test: start implementing sync tests
Emyrk 8350cca
fixup duplicate assignments
Emyrk d5bf63a
linting
Emyrk b3144c0
move the config into api options
Emyrk 72b501e
change sqlc version
Emyrk 5216230
some cleanup
Emyrk e73919e
test: add full org sync tests
Emyrk e37d476
linting
Emyrk a8647ce
make gen
Emyrk 02812e4
update golden files
Emyrk e18bc8f
PR comments
Emyrk 3516008
fmt
Emyrk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package idpsync | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/golang-jwt/jwt/v4" | ||
"github.com/google/uuid" | ||
"golang.org/x/xerrors" | ||
|
||
"cdr.dev/slog" | ||
"github.com/coder/coder/v2/coderd/database" | ||
"github.com/coder/coder/v2/coderd/httpapi" | ||
"github.com/coder/coder/v2/codersdk" | ||
"github.com/coder/coder/v2/site" | ||
) | ||
|
||
// IDPSync is an interface, so we can implement this as AGPL and as enterprise, | ||
// and just swap the underlying implementation. | ||
// IDPSync exists to contain all the logic for mapping a user's external IDP | ||
// claims to the internal representation of a user in Coder. | ||
// TODO: Move group + role sync into this interface. | ||
type IDPSync interface { | ||
OrganizationSyncEnabled() bool | ||
// ParseOrganizationClaims takes claims from an OIDC provider, and returns the | ||
// organization sync params for assigning users into organizations. | ||
ParseOrganizationClaims(ctx context.Context, _ jwt.MapClaims) (OrganizationParams, *HTTPError) | ||
// SyncOrganizations assigns and removed users from organizations based on the | ||
// provided params. | ||
SyncOrganizations(ctx context.Context, tx database.Store, user database.User, params OrganizationParams) error | ||
} | ||
|
||
// AGPLIDPSync is the configuration for syncing user information from an external | ||
// IDP. All related code to syncing user information should be in this package. | ||
type AGPLIDPSync struct { | ||
Logger slog.Logger | ||
|
||
SyncSettings | ||
} | ||
|
||
type SyncSettings struct { | ||
// OrganizationField selects the claim field to be used as the created user's | ||
// organizations. If the field is the empty string, then no organization updates | ||
// will ever come from the OIDC provider. | ||
OrganizationField string | ||
// OrganizationMapping controls how organizations returned by the OIDC provider get mapped | ||
OrganizationMapping map[string][]uuid.UUID | ||
// OrganizationAssignDefault will ensure all users that authenticate will be | ||
// placed into the default organization. This is mostly a hack to support | ||
// legacy deployments. | ||
OrganizationAssignDefault bool | ||
} | ||
|
||
type OrganizationParams struct { | ||
// SyncEnabled if false will skip syncing the user's organizations. | ||
SyncEnabled bool | ||
// IncludeDefault is primarily for single org deployments. It will ensure | ||
// a user is always inserted into the default org. | ||
IncludeDefault bool | ||
// Organizations is the list of organizations the user should be a member of | ||
// assuming syncing is turned on. | ||
Organizations []uuid.UUID | ||
} | ||
|
||
func NewAGPLSync(logger slog.Logger, settings SyncSettings) *AGPLIDPSync { | ||
return &AGPLIDPSync{ | ||
Logger: logger.Named("idp-sync"), | ||
SyncSettings: settings, | ||
} | ||
} | ||
|
||
// ParseStringSliceClaim parses the claim for groups and roles, expected []string. | ||
// | ||
// Some providers like ADFS return a single string instead of an array if there | ||
// is only 1 element. So this function handles the edge cases. | ||
func ParseStringSliceClaim(claim interface{}) ([]string, error) { | ||
groups := make([]string, 0) | ||
if claim == nil { | ||
return groups, nil | ||
} | ||
|
||
// The simple case is the type is exactly what we expected | ||
asStringArray, ok := claim.([]string) | ||
if ok { | ||
return asStringArray, nil | ||
} | ||
|
||
asArray, ok := claim.([]interface{}) | ||
if ok { | ||
for i, item := range asArray { | ||
asString, ok := item.(string) | ||
if !ok { | ||
return nil, xerrors.Errorf("invalid claim type. Element %d expected a string, got: %T", i, item) | ||
} | ||
groups = append(groups, asString) | ||
} | ||
return groups, nil | ||
} | ||
|
||
asString, ok := claim.(string) | ||
if ok { | ||
if asString == "" { | ||
// Empty string should be 0 groups. | ||
return []string{}, nil | ||
} | ||
// If it is a single string, first check if it is a csv. | ||
// If a user hits this, it is likely a misconfiguration and they need | ||
// to reconfigure their IDP to send an array instead. | ||
if strings.Contains(asString, ",") { | ||
return nil, xerrors.Errorf("invalid claim type. Got a csv string (%q), change this claim to return an array of strings instead.", asString) | ||
} | ||
return []string{asString}, nil | ||
} | ||
|
||
// Not sure what the user gave us. | ||
return nil, xerrors.Errorf("invalid claim type. Expected an array of strings, got: %T", claim) | ||
} | ||
|
||
// IsHTTPError handles us being inconsistent with returning errors as values or | ||
// pointers. | ||
func IsHTTPError(err error) *HTTPError { | ||
var httpErr HTTPError | ||
if xerrors.As(err, &httpErr) { | ||
return &httpErr | ||
} | ||
|
||
var httpErrPtr *HTTPError | ||
if xerrors.As(err, &httpErrPtr) { | ||
return httpErrPtr | ||
} | ||
return nil | ||
} | ||
|
||
// HTTPError is a helper struct for returning errors from the IDP sync process. | ||
// A regular error is not sufficient because many of these errors are surfaced | ||
// to a user logging in, and the errors should be descriptive. | ||
type HTTPError struct { | ||
Code int | ||
Msg string | ||
Detail string | ||
RenderStaticPage bool | ||
RenderDetailMarkdown bool | ||
} | ||
|
||
func (e HTTPError) Write(rw http.ResponseWriter, r *http.Request) { | ||
if e.RenderStaticPage { | ||
site.RenderStaticErrorPage(rw, r, site.ErrorPageData{ | ||
Status: e.Code, | ||
HideStatus: true, | ||
Title: e.Msg, | ||
Description: e.Detail, | ||
RetryEnabled: false, | ||
DashboardURL: "/login", | ||
|
||
RenderDescriptionMarkdown: e.RenderDetailMarkdown, | ||
}) | ||
return | ||
} | ||
httpapi.Write(r.Context(), rw, e.Code, codersdk.Response{ | ||
Message: e.Msg, | ||
Detail: e.Detail, | ||
}) | ||
} | ||
|
||
func (e HTTPError) Error() string { | ||
if e.Detail != "" { | ||
return e.Detail | ||
} | ||
|
||
return e.Msg | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Is it possible for this to be just
map[string][]uuid.UUID
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain why we need this instead of solely relying on the
--oidc-organization-field
? Orgs are uniquely named right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have to use the type
serpent.Struct[map[string][]uuid.UUID]
to be compatible with all parsing.I could use names, but that assumes the IDP will use Coder organization names in their claims, which we've found with groups that this almost never the case.