-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Add configured HTTPS expose routes #371
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
lox
wants to merge
5
commits into
main
Choose a base branch
from
lox/configured-https-expose
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
5 commits
Select commit
Hold shift + click to select a range
ddb3c6d
feat: add configured HTTPS expose routes
lox 680455f
fix: harden configured HTTPS exposure
lox 50e1647
fix: preserve guest expose-https args
lox 5cddef4
fix: harden wildcard HTTPS exposure
lox 40b7e3d
fix: validate configured HTTPS hosts
lox 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
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,152 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/buildkite/cleanroom/internal/exposure" | ||
| cleanroomv1 "github.com/buildkite/cleanroom/internal/gen/cleanroom/v1" | ||
| "github.com/buildkite/cleanroom/internal/policy" | ||
| ) | ||
|
|
||
| const configuredHTTPSPreflightSandboxID = "cr-preflight" | ||
|
|
||
| func normalizeBareExposeHTTPSArgs(args []string) []string { | ||
| if len(args) == 0 { | ||
| return nil | ||
| } | ||
| out := append([]string(nil), args...) | ||
| for i, arg := range out { | ||
| if arg == "--" { | ||
| break | ||
| } | ||
| if arg != "--expose-https" { | ||
| continue | ||
| } | ||
| if i+1 >= len(out) || out[i+1] == "--" || strings.HasPrefix(out[i+1], "-") { | ||
| out[i] = "--expose-https=" + configuredHTTPSExposureSpec | ||
|
lox marked this conversation as resolved.
|
||
| } | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| func resolveRequestedExposures(ctx *runtimeContext, cwd, sandboxID string, requested []*cleanroomv1.PortExposure) ([]*cleanroomv1.PortExposure, error) { | ||
| if !hasConfiguredHTTPSExposure(requested) { | ||
| return requested, nil | ||
| } | ||
| cfg, err := loadConfiguredHTTPSExposure(ctx, cwd) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| configured, err := expandConfiguredHTTPSExposures(cfg, sandboxID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| resolved := make([]*cleanroomv1.PortExposure, 0, len(requested)+len(configured)) | ||
| for _, req := range requested { | ||
| if isConfiguredHTTPSExposure(req) { | ||
| resolved = append(resolved, configured...) | ||
| continue | ||
| } | ||
| resolved = append(resolved, req) | ||
| } | ||
| return resolved, nil | ||
| } | ||
|
|
||
| func prevalidateConfiguredExposures(ctx *runtimeContext, cwd string, requested []*cleanroomv1.PortExposure) error { | ||
| if !hasConfiguredHTTPSExposure(requested) { | ||
| return nil | ||
| } | ||
| cfg, err := loadConfiguredHTTPSExposure(ctx, cwd) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| _, err = expandConfiguredHTTPSExposures(cfg, configuredHTTPSPreflightSandboxID) | ||
| return err | ||
| } | ||
|
|
||
| func loadConfiguredHTTPSExposure(ctx *runtimeContext, cwd string) (policy.ExposeHTTPSConfig, error) { | ||
| if ctx == nil || ctx.Loader == nil { | ||
| return policy.ExposeHTTPSConfig{}, errors.New("configured --expose-https requires a policy loader") | ||
| } | ||
| cfg, _, err := ctx.Loader.LoadExpose(cwd) | ||
| if err != nil { | ||
| if errors.Is(err, policy.ErrPolicyNotFound) { | ||
| return policy.ExposeHTTPSConfig{}, errors.New("configured --expose-https requires expose.https in cleanroom.yaml") | ||
| } | ||
| return policy.ExposeHTTPSConfig{}, err | ||
| } | ||
| return cfg.HTTPS, nil | ||
| } | ||
|
|
||
| func hasConfiguredHTTPSExposure(requested []*cleanroomv1.PortExposure) bool { | ||
| for _, req := range requested { | ||
| if isConfiguredHTTPSExposure(req) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func isConfiguredHTTPSExposure(req *cleanroomv1.PortExposure) bool { | ||
| return req != nil && | ||
| strings.TrimSpace(req.GetProtocol()) == exposureProtocolHTTPS && | ||
| strings.TrimSpace(req.GetName()) == configuredHTTPSExposureSpec && | ||
| req.GetGuestPort() == 0 | ||
| } | ||
|
|
||
| func expandConfiguredHTTPSExposures(cfg policy.ExposeHTTPSConfig, sandboxID string) ([]*cleanroomv1.PortExposure, error) { | ||
| sandboxID = strings.TrimSpace(strings.ToLower(sandboxID)) | ||
| if sandboxID == "" { | ||
| return nil, errors.New("configured --expose-https requires a sandbox id") | ||
| } | ||
| if cfg.IsZero() { | ||
| return nil, errors.New("configured --expose-https requires expose.https in cleanroom.yaml") | ||
| } | ||
| base := strings.TrimSpace(strings.ToLower(cfg.Base)) | ||
| if base != "" { | ||
| base = expandExposeTemplate(base, sandboxID, "") | ||
| base = strings.TrimSpace(strings.ToLower(base)) | ||
| } | ||
| if strings.TrimSpace(cfg.Base) != "" && base == "" { | ||
| return nil, errors.New("expose.https.base expanded to an empty host") | ||
| } | ||
|
|
||
| exposures := make([]*cleanroomv1.PortExposure, 0) | ||
| for i, route := range cfg.Routes { | ||
| if route.Port < 1 || route.Port > 65535 { | ||
| return nil, fmt.Errorf("expose.https.routes[%d].port must be in range 1-65535", i) | ||
| } | ||
| if len(route.Hosts) == 0 { | ||
| return nil, fmt.Errorf("expose.https.routes[%d].hosts must include at least one host", i) | ||
| } | ||
| for j, host := range route.Hosts { | ||
| if strings.Contains(host, "{base}") && base == "" { | ||
| return nil, fmt.Errorf("expose.https.routes[%d].hosts[%d] uses {base} but expose.https.base is empty", i, j) | ||
| } | ||
| host = expandExposeTemplate(host, sandboxID, base) | ||
| host = strings.TrimSpace(strings.ToLower(host)) | ||
| if host == "" { | ||
| return nil, fmt.Errorf("expose.https.routes[%d].hosts[%d] expanded to an empty host", i, j) | ||
| } | ||
| if err := exposure.ValidateHTTPSRouteName(host); err != nil { | ||
| return nil, fmt.Errorf("expose.https.routes[%d].hosts[%d] is invalid: %w", i, j, err) | ||
| } | ||
| exposures = append(exposures, &cleanroomv1.PortExposure{ | ||
|
lox marked this conversation as resolved.
|
||
| Protocol: exposureProtocolHTTPS, | ||
| GuestPort: int32(route.Port), | ||
| Name: host, | ||
| }) | ||
| } | ||
| } | ||
| return exposures, nil | ||
| } | ||
|
|
||
| func expandExposeTemplate(value, sandboxID, base string) string { | ||
| value = strings.ReplaceAll(value, "{sandbox_id}", sandboxID) | ||
| value = strings.ReplaceAll(value, "{container_id}", sandboxID) | ||
| value = strings.ReplaceAll(value, "{base}", base) | ||
| return value | ||
| } | ||
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.
normalizeBareExposeHTTPSArgskeeps scanning argv until a literal--, so it also rewrites--expose-httpstokens that appear after a passthrough command has already started (forexec/console, which usepassthrough:"partial"). In those cases--expose-httpsis a guest-command argument, not a Cleanroom flag, and this conversion silently changes the workload argv (for examplecleanroom exec npm --expose-httpsbecomesnpm --expose-https=__cleanroom_configured_https__).Useful? React with 👍 / 👎.