Repository navigation
feat(gitlab): pass an egress proxy to the runner's jobs #966
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
Open
atishay-kasliwal
wants to merge
3
commits into
redhat-developer:main
Choose a base branch
from
atishay-kasliwal:fix/953-glrunner-proxy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,98 @@ | ||
| package gitlab | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net" | ||
| "net/url" | ||
| "regexp" | ||
| "strings" | ||
| ) | ||
|
|
||
| // hostPattern matches a host name or a domain suffix such as ".example.com". Labels may | ||
| // contain underscores (_svc.internal), and a fully qualified name may end in a dot. | ||
| var hostPattern = regexp.MustCompile(`^\.?[A-Za-z0-9_]([A-Za-z0-9_-]*[A-Za-z0-9_])?(\.[A-Za-z0-9_]([A-Za-z0-9_-]*[A-Za-z0-9_])?)*\.?$`) | ||
|
|
||
| const encodeHint = "percent-encode characters such as @ : / ? # in a user name or password" | ||
|
|
||
| // ProxyEnv returns the proxy variables for the runner's job environment. Programs read | ||
| // different names, so each variable is set in upper and lower case. It returns nil when | ||
| // httpProxy is empty. | ||
| func ProxyEnv(httpProxy, noProxy string) ([]string, error) { | ||
| if httpProxy == "" { | ||
| if noProxy != "" { | ||
| return nil, fmt.Errorf("--glrunner-no-proxy needs --glrunner-http-proxy") | ||
| } | ||
| return nil, nil | ||
| } | ||
| if err := validateProxyURL(httpProxy); err != nil { | ||
| return nil, err | ||
| } | ||
| env := []string{ | ||
| "HTTP_PROXY=" + httpProxy, | ||
| "http_proxy=" + httpProxy, | ||
| "HTTPS_PROXY=" + httpProxy, | ||
| "https_proxy=" + httpProxy, | ||
| } | ||
| if noProxy != "" { | ||
| entries, err := noProxyEntries(noProxy) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| list := strings.Join(entries, ",") | ||
| env = append(env, "NO_PROXY="+list, "no_proxy="+list) | ||
| } | ||
| return env, nil | ||
| } | ||
|
|
||
| // validateProxyURL checks the proxy URL. Errors show the URL without its password, | ||
| // or not at all when it does not parse. | ||
| func validateProxyURL(raw string) error { | ||
| u, err := url.Parse(raw) | ||
| if err != nil || u.Hostname() == "" { | ||
| return fmt.Errorf("--glrunner-http-proxy must be a URL such as http://proxy.example.com:3128; %s", encodeHint) | ||
| } | ||
| if u.Scheme != "http" && u.Scheme != "https" { | ||
| return fmt.Errorf("--glrunner-http-proxy must use http or https, got %q", u.Redacted()) | ||
| } | ||
| if (u.Path != "" && u.Path != "/") || u.RawQuery != "" || u.Fragment != "" { | ||
| return fmt.Errorf("--glrunner-http-proxy must not have a path, query or fragment, got %q; %s", u.Redacted(), encodeHint) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // noProxyEntries splits and checks a NO_PROXY list. It accepts host names, domains, IP | ||
| // literals and "*". A CIDR is rejected because some clients, such as curl 7.76.1 on | ||
| // RHEL 9, ignore it without a message and send the request to the proxy. Repeated | ||
| // entries are dropped, keeping the first. | ||
| func noProxyEntries(list string) ([]string, error) { | ||
| var entries []string | ||
| seen := map[string]bool{} | ||
| for _, raw := range strings.Split(list, ",") { | ||
| entry := strings.TrimSpace(raw) | ||
| if entry == "" || seen[strings.ToLower(entry)] { | ||
| continue | ||
| } | ||
| seen[strings.ToLower(entry)] = true | ||
| if _, _, err := net.ParseCIDR(entry); err == nil { | ||
| return nil, fmt.Errorf("--glrunner-no-proxy entry %q is a CIDR range, which some clients (such as curl on RHEL 9) ignore without an error; list the host names or IP addresses instead", entry) | ||
| } | ||
| if entry != "*" && net.ParseIP(strings.Trim(entry, "[]")) == nil && !hostPattern.MatchString(entry) { | ||
| return nil, fmt.Errorf("--glrunner-no-proxy entry %q is not a host name, domain, IP address or *", entry) | ||
| } | ||
| entries = append(entries, entry) | ||
| } | ||
| if len(entries) == 0 { | ||
| return nil, fmt.Errorf("--glrunner-no-proxy has no entries, got %q", list) | ||
| } | ||
| return entries, nil | ||
| } | ||
|
|
||
| // shellQuote quotes a value for bash. | ||
| func shellQuote(v string) string { | ||
| return "'" + strings.ReplaceAll(v, "'", `'\''`) + "'" | ||
| } | ||
|
|
||
| // powershellQuote quotes a value for PowerShell. | ||
| func powershellQuote(v string) string { | ||
| return "'" + strings.ReplaceAll(v, "'", "''") + "'" | ||
| } | ||
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.
Do not include the supplied URL in validation errors.
For
http://user:12#secret@proxy.example.com:3128, parsing produces hostuser:12and fragmentsecret@proxy.example.com:3128.u.Redacted()does not remove password content parsed as a fragment. This error therefore exposes password content throughlogging.Fatalf. An unescaped?can expose password content in the query in the same way.Omit the URL from these errors. Add regression cases for both delimiters and check that neither error contains password content.