Skip to content
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

Add Response Header Flag to zrok access private #728

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

FEATURE: Support `html_path` directive in `interstitial` stanza of public frontend configuration to support using an external HTML file for the interstitial page (https://github.com/openziti/zrok/issues/716)

FEATURE: `zrok access private` now includes a `--response-header` flag to add headers to the response for HTTP-based backends. Add flag multiple times to add multiple headers to the response. Expects `key:value` header definitions in this format: `--response-header "Access-Control-Allow-Origin: *"` (https://github.com/openziti/zrok/issues/522)

CHANGE: Update `github.com/openziti/sdk-golang` (and related dependencies) to version `v0.23.40`.

CHANGE: upgrade to ziti v1.1.7 CLI in zrok container image
Expand Down
11 changes: 7 additions & 4 deletions cmd/zrok/accessPrivate.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ func init() {
}

type accessPrivateCommand struct {
bindAddress string
headless bool
cmd *cobra.Command
bindAddress string
headless bool
responseHeaders []string
cmd *cobra.Command
}

func newAccessPrivateCommand() *accessPrivateCommand {
Expand All @@ -41,8 +42,9 @@ func newAccessPrivateCommand() *accessPrivateCommand {
}
command := &accessPrivateCommand{cmd: cmd}
cmd.Flags().BoolVar(&command.headless, "headless", false, "Disable TUI and run headless")
cmd.Run = command.run
cmd.Flags().StringVarP(&command.bindAddress, "bind", "b", "127.0.0.1:9191", "The address to bind the private frontend")
cmd.Flags().StringArrayVar(&command.responseHeaders, "response-header", []string{}, "Add a response header ('key:value')")
cmd.Run = command.run
return command
}

Expand Down Expand Up @@ -194,6 +196,7 @@ func (cmd *accessPrivateCommand) run(_ *cobra.Command, args []string) {
cfg := proxy.DefaultFrontendConfig(env.EnvironmentIdentityName())
cfg.ShrToken = shrToken
cfg.Address = cmd.bindAddress
cfg.ResponseHeaders = cmd.responseHeaders
cfg.RequestsChan = requests
fe, err := proxy.NewFrontend(cfg)
if err != nil {
Expand Down
20 changes: 15 additions & 5 deletions endpoints/proxy/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
)

type FrontendConfig struct {
IdentityName string
ShrToken string
Address string
Tls *endpoints.TlsConfig
RequestsChan chan *endpoints.Request
IdentityName string
ShrToken string
Address string
ResponseHeaders []string
Tls *endpoints.TlsConfig
RequestsChan chan *endpoints.Request
}

func DefaultFrontendConfig(identityName string) *FrontendConfig {
Expand Down Expand Up @@ -112,6 +114,14 @@ func newServiceProxy(cfg *FrontendConfig, ctx ziti.Context) (*httputil.ReversePr
req.Header.Set("X-Proxy", "zrok")
}
proxy.ModifyResponse = func(resp *http.Response) error {
for _, responseHeader := range cfg.ResponseHeaders {
tokens := strings.Split(responseHeader, ":")
if len(tokens) == 2 {
resp.Header.Set(strings.TrimSpace(tokens[0]), strings.TrimSpace(tokens[1]))
} else {
logrus.Errorf("invalid response header '%v' (expecting header:value", responseHeader)
}
}
return nil
}
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
Expand Down
Loading