-
Notifications
You must be signed in to change notification settings - Fork 5
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
validate capi-auth-token on dqlite/remove #56
Merged
HomayoonAlimohammadi
merged 9 commits into
dqlite-remove-endpoint
from
KU-1719/validate-capi-auth-token-on-remove
Oct 11, 2024
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
55a739f
Validate capi-auth-token on dqlite/remove
HomayoonAlimohammadi d41addb
Fix lint
HomayoonAlimohammadi f5d5ad2
Address comments and issues
HomayoonAlimohammadi 22a3bb7
Address comments and issues
HomayoonAlimohammadi e711313
Fix test
HomayoonAlimohammadi 2bcaeca
Add middleware for capi auth token
HomayoonAlimohammadi 2496acf
Fix lint
HomayoonAlimohammadi 6e986af
Revert "Fix lint"
HomayoonAlimohammadi f4e5819
Revert "Add middleware for capi auth token"
HomayoonAlimohammadi 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 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,6 @@ | ||
package v2 | ||
|
||
const ( | ||
// CAPIAuthTokenHeader is the header used to pass the CAPI auth token. | ||
CAPIAuthTokenHeader = "capi-auth-token" | ||
) |
This file contains 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 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 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 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 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 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 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 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,37 @@ | ||
package snap_test | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
|
||
"github.com/canonical/microk8s-cluster-agent/pkg/snap" | ||
) | ||
|
||
func TestCAPIAuthToken(t *testing.T) { | ||
capiTestPath := "./capi-test" | ||
os.RemoveAll(capiTestPath) | ||
s := snap.NewSnap("", "", "", snap.WithCAPIPath(capiTestPath)) | ||
token := "token123" | ||
|
||
g := NewWithT(t) | ||
|
||
isValid, err := s.IsCAPIAuthTokenValid(token) | ||
g.Expect(err).To(MatchError(os.ErrNotExist)) | ||
g.Expect(isValid).To(BeFalse()) | ||
|
||
capiEtc := filepath.Join(capiTestPath, "etc") | ||
defer os.RemoveAll(capiTestPath) | ||
g.Expect(os.MkdirAll(capiEtc, 0755)).To(Succeed()) | ||
g.Expect(os.WriteFile("./capi-test/etc/token", []byte(token), 0600)).To(Succeed()) | ||
|
||
isValid, err = s.IsCAPIAuthTokenValid("random-token") | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
g.Expect(isValid).To(BeFalse()) | ||
|
||
isValid, err = s.IsCAPIAuthTokenValid(token) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
g.Expect(isValid).To(BeTrue()) | ||
} |
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.
For me, the validation of the token is a separate middleware/access handler that could potential be reused for other endpoints (similar to what we do in k8s-snap)
Disclaimer: I don't know much about the microk8s cluster agent. If this matches the other code structure I'm fine with that.
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 really like what you're saying, but IIUC the middlewares are not configurable per endpoint in cluster-agent: https://github.com/canonical/microk8s-cluster-agent/blob/main/pkg/server/server.go#L45-L46
All the v1 or v2 endpoints get registered with the same middleware.
We technically can have this
capi-auth-token
header check as a middleware, but since it's going to get applied for the other v2 endpoints as well (/image/import
and/join
) we need to make it optional, which is counter intuitive. WDYT?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.
You can simply set a new middleware on a per route basis:
https://github.com/canonical/microk8s-cluster-agent/blob/main/pkg/server/server.go#L19-L27
For this route you would add it here:
https://github.com/canonical/microk8s-cluster-agent/pull/56/files#diff-6ecd338993def4d5349db1e632bfa290c6aa5d6a7e04e6413f0f27e8983161f3R58
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.
My bad, thanks for pointing this out. Will do.
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.
@bschimke95 This is the best that I could come up with. TBH I feel like the previous implementation was more readable and understandable. Maybe I'm doing something wrong. WDYT?
2bcaeca
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'm reverting this. Not really happy with the result...
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.
ok, up to you. This was only a suggestion. If you think the current implementation is more straight-forward - go for it :)