- Notifications
You must be signed in to change notification settings - Fork1.1k
feat: remove org flag requirement for provisioners#14722
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes from2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
74 changes: 30 additions & 44 deletionsenterprise/cli/provisionerdaemonstart.go
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
70 changes: 2 additions & 68 deletionsenterprise/cli/provisionerdaemonstart_test.go
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
1 change: 1 addition & 0 deletionsenterprise/cli/testdata/coder_provisionerd_start_--help.golden
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
80 changes: 35 additions & 45 deletionsenterprise/coderd/provisionerdaemons.go
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 |
|---|---|---|
| @@ -82,77 +82,68 @@ type provisionerDaemonAuth struct { | ||
| // authorize returns mutated tags if the given HTTP request is authorized to access the provisioner daemon | ||
| // protobuf API, and returns nil, err otherwise. | ||
| func (p *provisionerDaemonAuth) authorize(r *http.Request,org database.Organization, tags map[string]string) (uuid.UUID,uuid.UUID, map[string]string, error) { | ||
f0ssel marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| ctx := r.Context() | ||
| apiKey, apiKeyOK := httpmw.APIKeyOptional(r) | ||
| pk, pkOK := httpmw.ProvisionerKeyAuthOptional(r) | ||
| provAuth := httpmw.ProvisionerDaemonAuthenticated(r) | ||
| if !provAuth && !apiKeyOK { | ||
| return uuid.Nil,uuid.Nil,nil, xerrors.New("no API key or provisioner key provided") | ||
| } | ||
| if apiKeyOK && pkOK { | ||
| return uuid.Nil,uuid.Nil,nil, xerrors.New("Both API key and provisioner key authentication provided. Only one is allowed.") | ||
| } | ||
| // Provisioner Key Auth | ||
| if pkOK { | ||
| if tags != nil && !maps.Equal(tags, map[string]string{}) { | ||
| return uuid.Nil,uuid.Nil,nil, xerrors.New("tags are not allowed when using a provisioner key") | ||
| } | ||
| // If using provisioner key / PSK auth, the daemon is, by definition, scoped to the organization. | ||
| // Use the provisioner key tags here. | ||
| tags = provisionersdk.MutateTags(uuid.Nil, pk.Tags) | ||
| return pk.ID,pk.OrganizationID,tags, nil | ||
| } | ||
| // PSK Auth | ||
| if provAuth { | ||
| if !org.IsDefault { | ||
| return uuid.Nil, uuid.Nil, nil, xerrors.Errorf("PSK auth is only allowed for the default organization '%s'", org.Name) | ||
| } | ||
| pskKey, err := uuid.Parse(codersdk.ProvisionerKeyIDPSK) | ||
| if err != nil { | ||
| return uuid.Nil, uuid.Nil, nil, xerrors.Errorf("parse psk provisioner key id: %w", err) | ||
| } | ||
| tags = provisionersdk.MutateTags(uuid.Nil, tags) | ||
| return pskKey, org.ID, tags, nil | ||
| } | ||
| // User Auth | ||
| if !apiKeyOK { | ||
| return uuid.Nil, uuid.Nil, nil, xerrors.New("no API key provided") | ||
| } | ||
| userKey, err := uuid.Parse(codersdk.ProvisionerKeyIDUserAuth) | ||
| if err != nil { | ||
| return uuid.Nil, uuid.Nil, nil, xerrors.Errorf("parse user provisioner key id: %w", err) | ||
| } | ||
| tags = provisionersdk.MutateTags(apiKey.UserID, tags) | ||
| if tags[provisionersdk.TagScope] == provisionersdk.ScopeUser { | ||
| // Any authenticated user can create provisioner daemons scoped | ||
| // for jobs that they own, | ||
| return userKey, org.ID, tags, nil | ||
| } | ||
| ua := httpmw.UserAuthorization(r) | ||
| err = p.authorizer.Authorize(ctx, ua, policy.ActionCreate, rbac.ResourceProvisionerDaemon.InOrg(org.ID)) | ||
| if err != nil { | ||
| return uuid.Nil,uuid.Nil,nil, xerrors.New("user unauthorized") | ||
| } | ||
| return userKey, org.ID, tags, nil | ||
| } | ||
| // Serves the provisioner daemon protobuf API over a WebSocket. | ||
| @@ -166,7 +157,6 @@ func (p *provisionerDaemonAuth) authorize(r *http.Request, orgID uuid.UUID, tags | ||
| // @Router /organizations/{organization}/provisionerdaemons/serve [get] | ||
| func (api *API) provisionerDaemonServe(rw http.ResponseWriter, r *http.Request) { | ||
| ctx := r.Context() | ||
| tags := map[string]string{} | ||
| if r.URL.Query().Has("tag") { | ||
| @@ -215,7 +205,7 @@ func (api *API) provisionerDaemonServe(rw http.ResponseWriter, r *http.Request) | ||
| api.Logger.Warn(ctx, "unnamed provisioner daemon") | ||
| } | ||
| keyID,orgID,tags, err := api.provisionerDaemonAuth.authorize(r,httpmw.OrganizationParam(r), tags) | ||
| if err != nil { | ||
| api.Logger.Warn(ctx, "unauthorized provisioner daemon serve request", slog.F("tags", tags), slog.Error(err)) | ||
| httpapi.Write(ctx, rw, http.StatusForbidden, | ||
| @@ -287,7 +277,7 @@ func (api *API) provisionerDaemonServe(rw http.ResponseWriter, r *http.Request) | ||
| LastSeenAt: sql.NullTime{Time: now, Valid: true}, | ||
| Version: versionHdrVal, | ||
| APIVersion: apiVersion, | ||
| OrganizationID:orgID, | ||
| KeyID: keyID, | ||
| }) | ||
| if err != nil { | ||
| @@ -351,7 +341,7 @@ func (api *API) provisionerDaemonServe(rw http.ResponseWriter, r *http.Request) | ||
| srvCtx, | ||
| api.AccessURL, | ||
| daemon.ID, | ||
| orgID, | ||
| logger, | ||
| provisioners, | ||
| tags, | ||
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.