-
Notifications
You must be signed in to change notification settings - Fork 17
Add workshop identity to workshopctl requests #1017
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
dmitry-lyfar
merged 7 commits into
secrets-implementation
from
system-sdk-secret-provider
Sep 7, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
09c1028
Add user-scoped project lookup to workshop backends
tlm 35b138a
Add workshop instance ID request middleware
tlm 5c661c1
Use current user IDs in LXD integration tests
tlm e14ee8c
Expose LXD instance IDs on workshops
tlm 85d70dc
Add workshop instance IDs to workshopctl requests
tlm 0178741
Fix workshop instance ID CI checks
tlm d7624be
Fix root LXD integration user mapping
tlm 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // Copyright (c) 2026 Canonical Ltd | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License version 3 as | ||
| // published by the Free Software Foundation. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| package daemon | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
|
|
||
| "github.com/canonical/workshop/internal/workshop" | ||
| ) | ||
|
|
||
| // workshopInstanceIDHeader identifies the workshop instance from which an API | ||
| // request originated. | ||
| const workshopInstanceIDHeader = "workshop-instance-id" | ||
|
|
||
| // withWorkshopInstanceID returns a response function that adds the workshop | ||
| // instance ID header value to the request context when the header is present, | ||
| // then calls next. | ||
| func withWorkshopInstanceID(next ResponseFunc) ResponseFunc { | ||
| return func(c *Command, r *http.Request, user *userState) Response { | ||
| instanceID := r.Header.Get(workshopInstanceIDHeader) | ||
| if instanceID != "" { | ||
| ctx := context.WithValue( | ||
| r.Context(), | ||
| workshop.ContextWorkshopInstanceID, | ||
| instanceID, | ||
| ) | ||
| r = r.WithContext(ctx) | ||
| } | ||
|
|
||
| return next(c, r, user) | ||
| } | ||
| } |
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,126 @@ | ||
| // Copyright (c) 2026 Canonical Ltd | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License version 3 as | ||
| // published by the Free Software Foundation. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| package daemon | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
|
|
||
| "gopkg.in/check.v1" | ||
|
|
||
| "github.com/canonical/workshop/internal/workshop" | ||
| ) | ||
|
|
||
| type apiRequestSuite struct{} | ||
|
|
||
| var _ = check.Suite(&apiRequestSuite{}) | ||
|
|
||
| // TestWithWorkshopInstanceIDAddsIDToContext checks that the middleware copies | ||
| // the workshop instance ID header into the request context before calling the | ||
| // next response function. It asserts that the next function's response is | ||
| // returned and that the context contains the header value. | ||
| func (apiRequestSuite) TestWithWorkshopInstanceIDAddsIDToContext(c *check.C) { | ||
| request := httptest.NewRequest(http.MethodPost, "/v1/workshopctl", nil) | ||
| request.Header.Set(workshopInstanceIDHeader, "instance-id") | ||
|
|
||
| var instanceID string | ||
| next := func(_ *Command, request *http.Request, _ *userState) Response { | ||
| instanceID, _ = request.Context(). | ||
| Value(workshop.ContextWorkshopInstanceID).(string) | ||
| return nil | ||
| } | ||
|
|
||
| response := withWorkshopInstanceID(next)(nil, request, nil) | ||
|
|
||
| c.Check(response, check.IsNil) | ||
| c.Check(instanceID, check.Equals, "instance-id") | ||
| } | ||
|
|
||
| // TestWithWorkshopInstanceIDLeavesContextUnsetWithoutHeader checks that the | ||
| // middleware calls the next response function without adding a workshop | ||
| // instance ID when the header is absent. It asserts that the next function's | ||
| // response is returned and that the context value remains unset. | ||
| func (apiRequestSuite) TestWithWorkshopInstanceIDLeavesContextUnsetWithoutHeader( | ||
| c *check.C, | ||
| ) { | ||
| request := httptest.NewRequest(http.MethodPost, "/v1/workshopctl", nil) | ||
|
|
||
| var instanceID any | ||
| next := func(_ *Command, request *http.Request, _ *userState) Response { | ||
| instanceID = request.Context().Value(workshop.ContextWorkshopInstanceID) | ||
| return nil | ||
| } | ||
|
|
||
| response := withWorkshopInstanceID(next)(nil, request, nil) | ||
|
|
||
| c.Check(response, check.IsNil) | ||
| c.Check(instanceID, check.IsNil) | ||
| } | ||
|
|
||
| // TestWithWorkshopInstanceIDCallsNextWithHeader checks that the middleware | ||
| // calls the next response function and returns its response when the workshop | ||
| // instance ID header is present. | ||
| func (apiRequestSuite) TestWithWorkshopInstanceIDCallsNextWithHeader(c *check.C) { | ||
| command := &Command{} | ||
| request := httptest.NewRequest(http.MethodPost, "/v1/workshopctl", nil) | ||
| request.Header.Set(workshopInstanceIDHeader, "instance-id") | ||
| user := &userState{} | ||
| expected := SyncResponse(nil, http.StatusAccepted) | ||
|
|
||
| called := false | ||
| next := func( | ||
| actualCommand *Command, | ||
| _ *http.Request, | ||
| actualUser *userState, | ||
| ) Response { | ||
| called = true | ||
| c.Check(actualCommand, check.Equals, command) | ||
| c.Check(actualUser, check.Equals, user) | ||
| return expected | ||
| } | ||
|
|
||
| response := withWorkshopInstanceID(next)(command, request, user) | ||
|
|
||
| c.Check(called, check.Equals, true) | ||
| c.Check(response, check.Equals, expected) | ||
| } | ||
|
|
||
| // TestWithWorkshopInstanceIDCallsNextWithoutHeader checks that the middleware | ||
| // calls the next response function and returns its response when the workshop | ||
| // instance ID header is absent. | ||
| func (apiRequestSuite) TestWithWorkshopInstanceIDCallsNextWithoutHeader(c *check.C) { | ||
| command := &Command{} | ||
| request := httptest.NewRequest(http.MethodPost, "/v1/workshopctl", nil) | ||
| user := &userState{} | ||
| expected := SyncResponse(nil, http.StatusAccepted) | ||
|
|
||
| called := false | ||
| next := func( | ||
| actualCommand *Command, | ||
| actualRequest *http.Request, | ||
| actualUser *userState, | ||
| ) Response { | ||
| called = true | ||
| c.Check(actualCommand, check.Equals, command) | ||
| c.Check(actualRequest, check.Equals, request) | ||
| c.Check(actualUser, check.Equals, user) | ||
| return expected | ||
| } | ||
|
|
||
| response := withWorkshopInstanceID(next)(command, request, user) | ||
|
|
||
| c.Check(called, check.Equals, true) | ||
| c.Check(response, check.Equals, expected) | ||
| } |
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
The interface's comment says it would return an empty slice, though, it's probably equivalent behavior here. I'm guessing the difference will show up if this would make it as a return value for
/v1/projects(not quite likely either given it would be transformed into an API level struct).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.
I would be inclined to leave it as is.
len(nil)still returns 0 and append operations still work. i.e nill is safe value for slices in go.