refactor: validate and parse the endpoint and proxy at program load#1267
refactor: validate and parse the endpoint and proxy at program load#1267
Conversation
cmd/src/login.go
Outdated
| if cfg.configFilePath != "" { | ||
| fmt.Fprintln(out) | ||
| fmt.Fprintf(out, "⚠️ Warning: Configuring src with a JSON file is deprecated. Please migrate to using the env vars SRC_ENDPOINT, SRC_ACCESS_TOKEN, and SRC_PROXY instead, and then remove %s. See https://github.com/sourcegraph/src-cli#readme for more information.\n", cfg.ConfigFilePath) | ||
| fmt.Fprintf(out, "⚠️ Warning: Configuring src with a JSON file is deprecated. Please migrate to using the env vars SRC_ENDPOINT, SRC_ACCESS_TOKEN, and SRC_PROXY instead, and then remove %s. See https://github.com/sourcegraph/src-cli#readme for more information.\n", cfg.configFilePath) |
There was a problem hiding this comment.
The config file has been deprecated for a long time, but if it ever is finally removed, the ability to set additional headers will go also. Do we need to keep the ability to set additional headers around? If so, we should probably introduce yet another environment variable for those and delay the config file removal. If not, let's complete the config file deprecation!
There was a problem hiding this comment.
|
This change is part of the following stack: Change managed by git-spice. |
bahrmichael
left a comment
There was a problem hiding this comment.
I think there's a bug in https://github.com/sourcegraph/src-cli/pull/1267/changes#r2903861064. But apart from that good to go, so I'll approve to unblock.
keegancsmith
left a comment
There was a problem hiding this comment.
approving, but agree with Michael's catch on the breaking change.
cab95e8 to
97c1b0a
Compare
Amp-Thread-ID: https://ampcode.com/threads/T-019cdb3f-f7de-750b-b4c3-13762c7dfc11 Co-authored-by: Amp <amp@ampcode.com>
5ccd030 to
9ad25e5
Compare
| out: os.Stdout, | ||
| apiFlags: apiFlags, | ||
| oauthClient: oauth.NewClient(oauth.DefaultClientID), | ||
| loginEndpointURL: loginEndpointURL, |
There was a problem hiding this comment.
| loginEndpointURL: loginEndpointURL, | |
| endpointURL: loginEndpointURL, |
| out io.Writer | ||
| apiFlags *api.Flags | ||
| oauthClient oauth.Client | ||
| loginEndpointURL *url.URL |
There was a problem hiding this comment.
| loginEndpointURL *url.URL | |
| endpointURL *url.URL |
| config, err := c.Discover(ctx, token.Endpoint) | ||
| endpointURL, err := token.EndpointURL() | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "invlaid token endpoint") |
There was a problem hiding this comment.
| return nil, errors.Wrap(err, "invlaid token endpoint") | |
| return nil, errors.Wrap(err, "invalid token endpoint") |
| proxyURL *url.URL | ||
| proxyPath string | ||
| configFilePath string | ||
| endpointURL *url.URL // always non-nil; defaults to https://sourcegraph.com via readConfig |
There was a problem hiding this comment.
if we are sure it will never be nil, then maybe we should just make it url.URL and not a pointer?
| func selectLoginFlow(p loginParams) (loginFlowKind, loginFlow) { | ||
| endpointArg := cleanEndpoint(p.endpoint) | ||
|
|
||
| if p.loginEndpointURL != nil && p.loginEndpointURL.String() != p.cfg.endpointURL.String() { |
There was a problem hiding this comment.
if we just make cfg the source of truth and the cli endpoint arg just overrides it we don't have to do this check at all.
| if err != nil { | ||
| printLoginProblem(p.out, fmt.Sprintf("OAuth Device flow authentication failed: %s", err)) | ||
| fmt.Fprintln(p.out, loginAccessTokenMessage(endpointArg)) | ||
| fmt.Fprintln(p.out, loginAccessTokenMessage(p.cfg.endpointURL)) |
There was a problem hiding this comment.
Note that we are preferring the configured endpointURL over the provided argument endpoint here
| return err | ||
| } | ||
| endpoint := cfg.Endpoint | ||
|
|
There was a problem hiding this comment.
I think after successful parse, we should override whatever is configured in cfg for the endpoint.
So cfg should be source of truth hence forth.
| out io.Writer | ||
| apiFlags *api.Flags | ||
| oauthClient oauth.Client | ||
| loginEndpointURL *url.URL |
There was a problem hiding this comment.
We're using cfg.EndpointURL and never use this endpointURL anymore - so we can remove it
Summary
Parse the endpoint URL at program load instead of passing around an unparsed string and needing to check/parse it in multiple places.
Parse the proxy url at program load, supporting
HTTP_PROXY,HTTPS_PROXYandNO_PROXYin addition toSRC_PROXY.SRC_PROXYtakes precedence.Changes
configstruct (cmd/src/main.go)configFromFilestruct containing the JSON elementspackage main)endpointURL-EndpointandProxyare now inconfigFromFileendpointURLinreadConfig- consumers use the parsed URLapi.ClientOpts(internal/api/api.go)Endpoint string→EndpointURL *url.URLJoinPathfor URL construction instead of string concatenation withTrimRightlogincommand (cmd/src/login.go)loginCmdnow uses the config'sendpointURLloginCmdinto the handler, where the CLI arg is parsed and compared before entering the commandcode_intel_upload(cmd/src/code_intel_upload.go)makeCodeIntelUploadURLuses a stack copy (u := *cfg.endpointURL) instead of re-parsing the URLTests
*url.URLTesting
All affected packages pass: