-
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 1 commit
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
Some comments aren't visible on the classic Files Changed page.
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,123 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| cleanroomv1 "github.com/buildkite/cleanroom/internal/gen/cleanroom/v1" | ||
| "github.com/buildkite/cleanroom/internal/policy" | ||
| ) | ||
|
|
||
| func normalizeBareExposeHTTPSArgs(args []string) []string { | ||
| if len(args) == 0 { | ||
| return nil | ||
| } | ||
| out := append([]string(nil), args...) | ||
| for i, arg := range out { | ||
| if arg != "--expose-https" { | ||
| continue | ||
| } | ||
| if i+1 >= len(out) || out[i+1] == "--" || strings.HasPrefix(out[i+1], "-") { | ||
| out[i] = "--expose-https=" + configuredHTTPSExposureSpec | ||
| } | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| func resolveRequestedExposures(ctx *runtimeContext, cwd, sandboxID string, requested []*cleanroomv1.PortExposure) ([]*cleanroomv1.PortExposure, error) { | ||
| if !hasConfiguredHTTPSExposure(requested) { | ||
| return requested, nil | ||
| } | ||
| if ctx == nil || ctx.Loader == nil { | ||
| return nil, 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 nil, errors.New("configured --expose-https requires expose.https in cleanroom.yaml") | ||
| } | ||
| return nil, err | ||
| } | ||
| configured, err := expandConfiguredHTTPSExposures(cfg.HTTPS, 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 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) | ||
| } | ||
| 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 | ||
| } | ||
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,139 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "errors" | ||
| "reflect" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| cleanroomv1 "github.com/buildkite/cleanroom/internal/gen/cleanroom/v1" | ||
| "github.com/buildkite/cleanroom/internal/policy" | ||
| ) | ||
|
|
||
| func TestNormalizeBareExposeHTTPSArgs(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| args []string | ||
| want []string | ||
| }{ | ||
| { | ||
| name: "bare before command separator", | ||
| args: []string{"exec", "--expose-https", "--", "npm", "run", "dev"}, | ||
| want: []string{"exec", "--expose-https=" + configuredHTTPSExposureSpec, "--", "npm", "run", "dev"}, | ||
| }, | ||
| { | ||
| name: "bare at end", | ||
| args: []string{"create", "--expose-https"}, | ||
| want: []string{"create", "--expose-https=" + configuredHTTPSExposureSpec}, | ||
| }, | ||
| { | ||
| name: "explicit value", | ||
| args: []string{"create", "--expose-https", "buildkite:3000"}, | ||
| want: []string{"create", "--expose-https", "buildkite:3000"}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| got := normalizeBareExposeHTTPSArgs(tt.args) | ||
| if !reflect.DeepEqual(got, tt.want) { | ||
| t.Fatalf("unexpected args: got %v want %v", got, tt.want) | ||
| } | ||
| if len(tt.args) > 0 && &got[0] == &tt.args[0] { | ||
| t.Fatal("expected normalized args to be a copy") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestExpandConfiguredHTTPSExposures(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| got, err := expandConfiguredHTTPSExposures(policy.ExposeHTTPSConfig{ | ||
| Base: "{sandbox_id}.localhost", | ||
| Routes: []policy.ExposeHTTPSRoute{{ | ||
| Port: 3000, | ||
| Hosts: []string{"{base}", "*.{base}", "*.*.{base}"}, | ||
| }}, | ||
| }, "Cr-123") | ||
| if err != nil { | ||
| t.Fatalf("expandConfiguredHTTPSExposures returned error: %v", err) | ||
| } | ||
|
|
||
| want := []*cleanroomv1.PortExposure{ | ||
| {Protocol: exposureProtocolHTTPS, GuestPort: 3000, Name: "cr-123.localhost"}, | ||
| {Protocol: exposureProtocolHTTPS, GuestPort: 3000, Name: "*.cr-123.localhost"}, | ||
| {Protocol: exposureProtocolHTTPS, GuestPort: 3000, Name: "*.*.cr-123.localhost"}, | ||
| } | ||
| if !reflect.DeepEqual(got, want) { | ||
| t.Fatalf("unexpected exposures: got %v want %v", got, want) | ||
| } | ||
| } | ||
|
|
||
| func TestResolveRequestedExposuresLoadsConfiguredHTTPS(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| loader := &configuredExposureLoader{ | ||
| cfg: policy.ExposeConfig{HTTPS: policy.ExposeHTTPSConfig{ | ||
| Base: "{container_id}.localhost", | ||
| Routes: []policy.ExposeHTTPSRoute{{ | ||
| Port: 3000, | ||
| Hosts: []string{"{base}", "*.{base}"}, | ||
| }}, | ||
| }}, | ||
| } | ||
| got, err := resolveRequestedExposures(&runtimeContext{Loader: loader}, "/repo", "sandbox-1", []*cleanroomv1.PortExposure{ | ||
| {Protocol: exposureProtocolTCP, HostPort: 5432, GuestPort: 5432}, | ||
| {Protocol: exposureProtocolHTTPS, Name: configuredHTTPSExposureSpec}, | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("resolveRequestedExposures returned error: %v", err) | ||
| } | ||
| if got, want := loader.cwd, "/repo"; got != want { | ||
| t.Fatalf("unexpected loader cwd: got %q want %q", got, want) | ||
| } | ||
| if got, want := len(got), 3; got != want { | ||
| t.Fatalf("unexpected exposure count: got %d want %d", got, want) | ||
| } | ||
| if got, want := got[1].GetName(), "sandbox-1.localhost"; got != want { | ||
| t.Fatalf("unexpected first configured host: got %q want %q", got, want) | ||
| } | ||
| if got, want := got[2].GetName(), "*.sandbox-1.localhost"; got != want { | ||
| t.Fatalf("unexpected second configured host: got %q want %q", got, want) | ||
| } | ||
| } | ||
|
|
||
| func TestResolveRequestedExposuresRequiresConfiguredHTTPS(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| _, err := resolveRequestedExposures(&runtimeContext{Loader: &configuredExposureLoader{}}, "/repo", "sandbox-1", []*cleanroomv1.PortExposure{ | ||
| {Protocol: exposureProtocolHTTPS, Name: configuredHTTPSExposureSpec}, | ||
| }) | ||
| if err == nil { | ||
| t.Fatal("expected error") | ||
| } | ||
| if !strings.Contains(err.Error(), "requires expose.https") { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| } | ||
|
|
||
| type configuredExposureLoader struct { | ||
| cfg policy.ExposeConfig | ||
| cwd string | ||
| } | ||
|
|
||
| func (l *configuredExposureLoader) LoadAndCompile(string) (*policy.CompiledPolicy, string, error) { | ||
| return nil, "", errors.New("LoadAndCompile should not be called") | ||
| } | ||
|
|
||
| func (l *configuredExposureLoader) LoadRepository(string) (policy.RepositoryConfig, string, error) { | ||
| return policy.RepositoryConfig{}, "", errors.New("LoadRepository should not be called") | ||
| } | ||
|
|
||
| func (l *configuredExposureLoader) LoadExpose(cwd string) (policy.ExposeConfig, string, error) { | ||
| l.cwd = cwd | ||
| return l.cfg, "/repo/cleanroom.yaml", nil | ||
| } |
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.