Skip to content

Commit

Permalink
Issue 611 Added Custom API Driven Back end (#614)
Browse files Browse the repository at this point in the history
  • Loading branch information
galen0624 authored and aaronhurt committed Apr 4, 2019
1 parent 3de70c7 commit 3ee105f
Show file tree
Hide file tree
Showing 21 changed files with 646 additions and 49 deletions.
12 changes: 6 additions & 6 deletions auth/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ func newBasicAuth(cfg config.BasicAuth) (AuthScheme, error) {
log.Println("[WARN] Error processing a line in an htpasswd file:", err)
}

stat, err := os.Stat(cfg.File)
if err != nil {
return nil, err
}
cfg.ModTime = stat.ModTime()

secrets, err := htpasswd.New(cfg.File, htpasswd.DefaultSystems, bad)
if err != nil {
return nil, err
}

if cfg.Refresh > 0 {
stat, err := os.Stat(cfg.File)
if err != nil {
return nil, err
}
cfg.ModTime = stat.ModTime()

go func() {
ticker := time.NewTicker(cfg.Refresh).C
for range ticker {
Expand Down
12 changes: 12 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ type Registry struct {
Static Static
File File
Consul Consul
Custom Custom
Timeout time.Duration
Retry time.Duration
}
Expand Down Expand Up @@ -153,6 +154,17 @@ type Consul struct {
ServiceMonitors int
}

type Custom struct {
Host string
Path string
QueryParams string
Scheme string
CheckTLSSkipVerify bool
PollingInterval time.Duration
NoRouteHTML string
Timeout time.Duration
}

type Tracing struct {
TracingEnabled bool
CollectorType string
Expand Down
10 changes: 10 additions & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ var defaultConfig = &Config{
CheckDeregisterCriticalServiceAfter: "90m",
ChecksRequired: "one",
},
Custom: Custom{
Host: "",
Scheme: "https",
CheckTLSSkipVerify: false,
PollingInterval: 5,
NoRouteHTML: "",
Timeout: 10,
Path: "",
QueryParams: "",
},
Timeout: 10 * time.Second,
Retry: 500 * time.Millisecond,
},
Expand Down
9 changes: 9 additions & 0 deletions config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ func load(cmdline, environ, envprefix []string, props *properties.Properties) (c
f.StringVar(&cfg.Tracing.SpanHost, "tracing.SpanHost", defaultConfig.Tracing.SpanHost, "Host:Port info to add to spans")
f.BoolVar(&cfg.GlobMatchingDisabled, "glob.matching.disabled", defaultConfig.GlobMatchingDisabled, "Disable Glob Matching on routes, one of [true, false]")

f.StringVar(&cfg.Registry.Custom.Host, "registry.custom.host", defaultConfig.Registry.Custom.Host, "custom back end hostname/port")
f.StringVar(&cfg.Registry.Custom.Scheme, "registry.custom.scheme", defaultConfig.Registry.Custom.Scheme, "custom back end scheme - http/https")
f.StringVar(&cfg.Registry.Custom.NoRouteHTML, "registry.custom.noroutehtml", defaultConfig.Registry.Custom.NoRouteHTML, "path to file for HTML returned when no route is found")
f.BoolVar(&cfg.Registry.Custom.CheckTLSSkipVerify, "registry.custom.checkTLSSkipVerify", defaultConfig.Registry.Custom.CheckTLSSkipVerify, "custom back end check TLS verification")
f.DurationVar(&cfg.Registry.Custom.Timeout, "registry.custom.timeout", defaultConfig.Registry.Custom.Timeout, "timeout for API request to custom back end")
f.DurationVar(&cfg.Registry.Custom.PollingInterval, "registry.custom.pollinginterval", defaultConfig.Registry.Custom.PollingInterval, "polling interval for API request to custom back end")
f.StringVar(&cfg.Registry.Custom.Path, "registry.custom.path", defaultConfig.Registry.Custom.Path, "custom back end path in the URL")
f.StringVar(&cfg.Registry.Custom.QueryParams, "registry.custom.queryparams", defaultConfig.Registry.Custom.QueryParams, "custom back end query parameters in the URL")

// deprecated flags
var proxyLogRoutes string
f.StringVar(&proxyLogRoutes, "proxy.log.routes", "", "deprecated. use log.routes.format instead")
Expand Down
49 changes: 49 additions & 0 deletions config/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,55 @@ func TestLoad(t *testing.T) {
return cfg
},
},
{
args: []string{"-registry.custom.host", "localhost:8080"},
cfg: func(cfg *Config) *Config {
cfg.Registry.Custom.Host = "localhost:8080"
return cfg
},
},
{
args: []string{"-registry.custom.scheme", "https"},
cfg: func(cfg *Config) *Config {
cfg.Registry.Custom.Scheme = "https"
return cfg
},
},
{
args: []string{"-registry.custom.checkTLSSkipVerify", "true"},
cfg: func(cfg *Config) *Config {
cfg.Registry.Custom.CheckTLSSkipVerify = true
return cfg
},
},
{
args: []string{"-registry.custom.timeout", "5s"},
cfg: func(cfg *Config) *Config {
cfg.Registry.Custom.Timeout = 5 * time.Second
return cfg
},
},
{
args: []string{"-registry.custom.pollinginterval", "5s"},
cfg: func(cfg *Config) *Config {
cfg.Registry.Custom.PollingInterval = 5 * time.Second
return cfg
},
},
{
args: []string{"-registry.custom.path", "test"},
cfg: func(cfg *Config) *Config {
cfg.Registry.Custom.Path = "test"
return cfg
},
},
{
args: []string{"-registry.custom.queryparams", "test=1"},
cfg: func(cfg *Config) *Config {
cfg.Registry.Custom.QueryParams = "test=1"
return cfg
},
},
{
args: []string{"-log.access.format", "foobar"},
cfg: func(cfg *Config) *Config {
Expand Down
18 changes: 17 additions & 1 deletion docs/content/ref/registry.backend.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,23 @@ title: "registry.backend"
---

`registry.backend` configures which backend is used.
Supported backends are: `consul`, `static`, `file`.
Supported backends are: `consul`, `static`, `file`, `custom`. If custom is used fabio makes an api
call to a remote system expecting the below json response

```json
[
{
"cmd": "string",
"service": "string",
"src": "string",
"dest": "string",
"weight": float,
"tags": ["string"],
"opts": {"string":"string"}
}
]
```


The default is

Expand Down
9 changes: 9 additions & 0 deletions docs/content/ref/registry.custom.checkTLSSkipVerify.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: "registry.custom.checkTLSSkipVerify"
---

`registry.custom.checkTLSSkipVerify` disables the TLS validation for the API call

The default is

registry.custom.checkTLSSkipVerify = false
9 changes: 9 additions & 0 deletions docs/content/ref/registry.custom.host.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: "registry.custom.host"
---

`registry.custom.host` configures the host:port for fabio to make the API call

The default is

registry.custom.host =
15 changes: 15 additions & 0 deletions docs/content/ref/registry.custom.path.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: "registry.custom.path"
---

`registry.custom.path` is the path used in the custom back end API Call

The path does not need to contain the initial '/'

Example:

registry.custom.path = api/v1/

The default is

registry.custom.path =
9 changes: 9 additions & 0 deletions docs/content/ref/registry.custom.pollinginterval.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: "registry.custom.pollinginterval"
---

`registry.custom.pollinginterval` is the length of time between API calls

The default is

registry.custom.pollinginterval = 10s
16 changes: 16 additions & 0 deletions docs/content/ref/registry.custom.queryparams.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
title: "registry.custom.queryparams"
---
`registry.custom.queryparams` is the query parameters used in the custom back
end API Call

Multiple query parameters should be separated with an &

Example:

registry.custom.queryparams = foo=bar&bar=foo

The default is

registry.custom.queryparams =
10 changes: 10 additions & 0 deletions docs/content/ref/registry.custom.scheme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
title: "registry.custom.scheme"
---

`registry.custom.scheme` configures the scheme use to make the API call
must be one of `http`, `https`

The default is

registry.custom.scheme = https
9 changes: 9 additions & 0 deletions docs/content/ref/registry.custom.timeout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: "registry.custom.timeout"
---

`registry.custom.timeout` controls the timeout for the API call

The default is

registry.custom.timeout = 5s
9 changes: 9 additions & 0 deletions fabio.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
77 changes: 76 additions & 1 deletion fabio.properties
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,20 @@


# registry.backend configures which backend is used.
# Supported backends are: consul, static, file
# Supported backends are: consul, static, file, custom
# if custom is used fabio makes an api call to a remote system
# expecting the below json response
# [
# {
# "cmd": "string",
# "service": "string",
# "src": "string",
# "dest": "string",
# "weight": float,
# "tags": ["string"],
# "opts": {"string":"string"}
# }
# ]
#
# The default is
#
Expand Down Expand Up @@ -814,6 +827,68 @@
# registry.consul.serviceMonitors = 1


# registry.custom.host configures the host:port for fabio to make the API call
#
# The default is
#
# registry.custom.host =


# registry.custom.scheme configures the scheme use to make the API call
# must be one of http, https
#
# The default is
#
# registry.custom.scheme = https


# registry.custom.checkTLSSkipVerify disables the TLS validation for the API call
#
# The default is
#
# registry.custom.checkTLSSkipVerify = false


# registry.custom.timeout controls the timeout for the API call
#
# The default is
#
# registry.custom.timeout = 5s


# registry.custom.pollinginterval is the length of time between API calls
#
# The default is
#
#registry.custom.pollinginterval = 10s


# registry.custom.path is the path used in the custom back end API Call
#
# The path does not need to contain the initial '/'
#
# Example:
#
# registry.custom.path = api/v1/
#
# The default is
#
# registry.custom.path =

# registry.custom.queryparams is the query parameters used in the custom back
# end API Call
#
# Multiple query parameters should be separated with an &
#
# Example:
#
# registry.custom.queryparams = foo=bar&bar=foo
#
# The default is
#
# registry.custom.queryparams =


# glob.matching.disabled disables glob matching on route lookups
# If glob matching is enabled there is a performance decrease
# for every route lookup. At a large number of services (> 500) this
Expand Down
Loading

0 comments on commit 3ee105f

Please sign in to comment.