Skip to content

Commit

Permalink
feat(config): allow specifying upstream ip and port separately
Browse files Browse the repository at this point in the history
  • Loading branch information
tronghn committed Aug 17, 2023
1 parent 185701d commit 7987ad7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ The following flags are available:
| `sso.server-url` | string | The URL used by the proxy to point to the SSO server instance. | |
| `sso.session-cookie-name` | string | Session cookie name. Must be the same across all SSO Servers and Proxies that should share sessions. | |
| `upstream-host` | string | Address of upstream host. | `127.0.0.1:8080` |
| `upstream-ip` | string | IP of upstream host. Overrides 'upstream-host' if set. | |
| `upstream-port` | int | Port of upstream host. Overrides 'upstream-host' if set. | |

Boolean flags are by default set to `false` unless noted otherwise.

Expand Down
44 changes: 44 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type Config struct {
Ingresses []string `json:"ingress"`
Session Session `json:"session"`
UpstreamHost string `json:"upstream-host"`
UpstreamIP string `json:"upstream-ip"`
UpstreamPort int `json:"upstream-port"`

OpenID OpenID `json:"openid"`
Redis Redis `json:"redis"`
Expand Down Expand Up @@ -72,6 +74,8 @@ const (
EncryptionKey = "encryption-key"
Ingress = "ingress"
UpstreamHost = "upstream-host"
UpstreamIP = "upstream-ip"
UpstreamPort = "upstream-port"

SessionCookieName = "session.cookie-name"
SessionInactivity = "session.inactivity"
Expand Down Expand Up @@ -100,6 +104,8 @@ func Initialize() (*Config, error) {
flag.String(EncryptionKey, "", "Base64 encoded 256-bit cookie encryption key; must be identical in instances that share session store.")
flag.StringSlice(Ingress, []string{}, "Comma separated list of ingresses used to access the main application.")
flag.String(UpstreamHost, "127.0.0.1:8080", "Address of upstream host.")
flag.String(UpstreamIP, "", "IP of upstream host. Overrides 'upstream-host' if set.")
flag.Int(UpstreamPort, 0, "Port of upstream host. Overrides 'upstream-host' if set.")

flag.Bool(SessionInactivity, false, "Automatically expire user sessions if they have not refreshed their tokens within a given duration.")
flag.Duration(SessionInactivityTimeout, 30*time.Minute, "Inactivity timeout for user sessions.")
Expand Down Expand Up @@ -164,6 +170,8 @@ func Initialize() (*Config, error) {
return nil, fmt.Errorf("validating config: %w", err)
}

cfg.upstreamHostOverride()

return cfg, nil
}

Expand Down Expand Up @@ -201,5 +209,41 @@ func (c *Config) Validate() error {
}
}

if c.upstreamPortSet() {
if !c.upstreamIpSet() {
return fmt.Errorf("%q must be set when %q is set (was '%d')", UpstreamIP, UpstreamPort, c.UpstreamPort)
}
if !c.upstreamPortValid() {
return fmt.Errorf("%q must be in valid range (between '1' and '65535', was '%d')", UpstreamPort, c.UpstreamPort)
}
}

if c.upstreamIpSet() && !c.upstreamPortSet() {
return fmt.Errorf("%q must be set when %q is set (was %q)", UpstreamPort, UpstreamIP, c.UpstreamIP)
}

return nil
}

func (c *Config) upstreamIpSet() bool {
return c.UpstreamIP != ""
}

func (c *Config) upstreamPortSet() bool {
return c.UpstreamPort != 0
}

func (c *Config) upstreamPortValid() bool {
return c.UpstreamPort >= 1 && c.UpstreamPort <= 65535
}

func (c *Config) upstreamHostOverride() {
if c.upstreamIpSet() && c.upstreamPortSet() && c.upstreamPortValid() {
override := fmt.Sprintf("%s:%d", c.UpstreamIP, c.UpstreamPort)

log.WithField("logger", "wonderwall.config").
Infof("%q and %q were set; overriding %q from %q to %q", UpstreamHost, UpstreamPort, UpstreamHost, c.UpstreamHost, override)

c.UpstreamHost = override
}
}

0 comments on commit 7987ad7

Please sign in to comment.