-
Notifications
You must be signed in to change notification settings - Fork 6
Add capi-auth-token header to /dqlite/remove request #68
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
HomayoonAlimohammadi
merged 4 commits into
dqlite-remove-endpoint
from
KU-1719/use-auth-token-in-request-header
Oct 11, 2024
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8b97586
Refactor cluster-agent client
HomayoonAlimohammadi 0ed95de
Add capi-auth-token header to dqlite/remove request
HomayoonAlimohammadi 5e39e7f
Add test for token pkg
HomayoonAlimohammadi cc5b188
Remove from dqlite only for 3 machines and more
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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package clusteragent | ||
|
|
||
| const ( | ||
| AuthTokenHeader = "capi-auth-token" | ||
| ) |
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,62 @@ | ||
| package httptest | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| ) | ||
|
|
||
| type serverMock struct { | ||
| Method string | ||
| Path string | ||
| Response any | ||
| Request map[string]any | ||
| Header http.Header | ||
| Srv *httptest.Server | ||
| } | ||
|
|
||
| // NewServerMock creates a test server that responds with the given response when called with the given method and path. | ||
| // Make sure to close the server after the test is done. | ||
| // Server will try to decode the request body into a map[string]any. | ||
| func NewServerMock(method string, path string, response any) *serverMock { | ||
| req := make(map[string]any) | ||
| header := make(map[string][]string) | ||
| ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| if r.URL.Path != path { | ||
| http.NotFound(w, r) | ||
| return | ||
| } | ||
| if r.Method != method { | ||
| w.WriteHeader(http.StatusMethodNotAllowed) | ||
| return | ||
| } | ||
|
|
||
| for k, vv := range map[string][]string(r.Header) { | ||
| header[k] = vv | ||
| } | ||
|
|
||
| if err := json.NewDecoder(r.Body).Decode(&req); err != nil { | ||
| w.WriteHeader(http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| if response != nil { | ||
| if err := json.NewEncoder(w).Encode(response); err != nil { | ||
| w.WriteHeader(http.StatusInternalServerError) | ||
| return | ||
| } | ||
| } | ||
| w.WriteHeader(http.StatusOK) | ||
| })) | ||
|
|
||
| fmt.Println("header:", header) | ||
| return &serverMock{ | ||
| Method: method, | ||
| Path: path, | ||
| Response: response, | ||
| Header: header, | ||
| Request: req, | ||
| Srv: ts, | ||
| } | ||
| } |
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,47 @@ | ||
| package token | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| ) | ||
|
|
||
| const ( | ||
| AuthTokenNameSuffix = "capi-auth-token" | ||
| ) | ||
|
|
||
| // Lookup retrieves the token for the given cluster. | ||
| func Lookup(ctx context.Context, c client.Client, clusterKey client.ObjectKey) (string, error) { | ||
| secret, err := getSecret(ctx, c, clusterKey) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to get secret: %w", err) | ||
| } | ||
|
|
||
| v, ok := secret.Data["token"] | ||
| if !ok { | ||
| return "", fmt.Errorf("token not found in secret") | ||
| } | ||
|
|
||
| return string(v), nil | ||
| } | ||
|
|
||
| // getSecret retrieves the token secret for the given cluster. | ||
| func getSecret(ctx context.Context, c client.Client, clusterKey client.ObjectKey) (*corev1.Secret, error) { | ||
| s := &corev1.Secret{} | ||
| key := client.ObjectKey{ | ||
| Name: authTokenName(clusterKey.Name), | ||
| Namespace: clusterKey.Namespace, | ||
| } | ||
| if err := c.Get(ctx, key, s); err != nil { | ||
| return nil, fmt.Errorf("failed to get secret: %w", err) | ||
| } | ||
|
|
||
| return s, nil | ||
| } | ||
|
|
||
| // name returns the name of the token secret, computed by convention using the name of the cluster. | ||
| func authTokenName(clusterName string) string { | ||
| return fmt.Sprintf("%s-%s", clusterName, AuthTokenNameSuffix) | ||
| } | ||
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 @@ | ||
| package token_test | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. left over?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nah thanks for pointing this out. Forgot to add the tests. Add the tests both here and in the bootstrap provider. |
||
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.