-
Notifications
You must be signed in to change notification settings - Fork 95
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
feat: implement proxy using msal-go #142
Merged
Merged
Changes from all commits
Commits
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
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 |
---|---|---|
@@ -1,109 +1,29 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"flag" | ||
|
||
"github.com/Azure/aad-pod-managed-identity/pkg/proxy" | ||
|
||
"github.com/pkg/errors" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
const ( | ||
proxyPort = 8000 | ||
tokenExchangeURL = "https://svctokenexchange.azurewebsites.net/api/token/exchange" // #nosec | ||
var ( | ||
proxyPort int | ||
) | ||
|
||
type proxy struct{} | ||
|
||
func main() { | ||
// TODO add handler to separate default metadata and token request | ||
if err := http.ListenAndServe(fmt.Sprintf("localhost:%d", proxyPort), &proxy{}); err != nil { | ||
klog.Fatalf("failed to listen and serve, error: %+v", err) | ||
} | ||
} | ||
|
||
func (p *proxy) ServeHTTP(w http.ResponseWriter, req *http.Request) { | ||
// forward the request to the new aad endpoint | ||
resp, err := p.sendRequest(req) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusBadGateway) | ||
return | ||
} | ||
p.writeResponse(w, resp) | ||
} | ||
klog.InitFlags(nil) | ||
defer klog.Flush() | ||
|
||
func (p *proxy) sendRequest(req *http.Request) (*http.Response, error) { | ||
klog.InfoS("received request", "method", req.Method, "uri", req.RequestURI) | ||
clientID, resource := parseTokenRequest(req) | ||
if clientID == "" { | ||
return &http.Response{Status: strconv.Itoa(http.StatusBadRequest)}, errors.New("client id is not set") | ||
} | ||
if resource == "" { | ||
resource = "https://management.azure.com/" | ||
} | ||
return doTokenRequest(clientID, resource) | ||
} | ||
flag.IntVar(&proxyPort, "proxy-port", 8000, "Port for the proxy to listen on") | ||
flag.Parse() | ||
|
||
func (p *proxy) writeResponse(w http.ResponseWriter, res *http.Response) { | ||
for name, values := range res.Header { | ||
w.Header()[name] = values | ||
} | ||
// Set a special header to notify that the proxy actually serviced the request. | ||
w.Header().Set("Server", "pi-sidecar-proxy") | ||
w.WriteHeader(res.StatusCode) | ||
_, _ = io.Copy(w, res.Body) | ||
res.Body.Close() | ||
|
||
klog.InfoS("request complete", "status", res.StatusCode) | ||
} | ||
|
||
func parseTokenRequest(r *http.Request) (clientID, resource string) { | ||
vals := r.URL.Query() | ||
if vals != nil { | ||
clientID = vals.Get("client_id") | ||
resource = vals.Get("resource") | ||
} | ||
return | ||
} | ||
|
||
// TODO this will be replaced by the SDK when available | ||
func doTokenRequest(clientID, resource string) (*http.Response, error) { | ||
// get the service account jwt token | ||
tokenFile := os.Getenv("TOKEN_FILE_PATH") | ||
if tokenFile == "" { | ||
return nil, errors.New("TOKEN_FILE_PATH not set") | ||
} | ||
if _, err := os.Stat(tokenFile); err != nil { | ||
return nil, errors.New("token file not found") | ||
} | ||
token, err := os.ReadFile(tokenFile) | ||
p, err := proxy.NewProxy(proxyPort) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to get token") | ||
klog.Fatalf("failed to get proxy, error: %+v", err) | ||
} | ||
|
||
httpClient := http.Client{} | ||
|
||
reqBody, err := json.Marshal(map[string]string{ | ||
"subjectToken": strings.TrimSuffix(string(token), "\n"), | ||
"clientId": clientID, | ||
"scopes": resource, | ||
}) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to marshal req body") | ||
} | ||
|
||
req, err := http.NewRequest(http.MethodPost, tokenExchangeURL, bytes.NewBuffer(reqBody)) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to create http request") | ||
if err = p.Run(); err != nil { | ||
klog.Fatalf("failed to run proxy, error: %+v", err) | ||
} | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
resp, err := httpClient.Do(req) | ||
return resp, err | ||
} |
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
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.
Is it not possible to run e2e tests in parallel?
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 proxy test and token exchange test currently use the same service account for tests and running in parallel will cause a race. I can add trust for a new service account and enable parallel again in a follow-up PR.