2015-11-01 15:35:01 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2016-11-09 18:27:04 +00:00
|
|
|
"encoding"
|
2015-11-01 15:35:01 +00:00
|
|
|
"errors"
|
2016-06-02 13:17:04 +00:00
|
|
|
"fmt"
|
2017-01-12 13:34:54 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-01-31 21:55:02 +00:00
|
|
|
|
2017-08-25 16:22:03 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/containous/traefik/log"
|
2017-01-31 21:55:02 +00:00
|
|
|
"github.com/docker/libkv/store"
|
|
|
|
"github.com/ryanuber/go-glob"
|
2015-11-01 15:35:01 +00:00
|
|
|
)
|
|
|
|
|
2015-11-01 18:15:05 +00:00
|
|
|
// Backend holds backend configuration.
|
2015-11-01 15:35:01 +00:00
|
|
|
type Backend struct {
|
|
|
|
Servers map[string]Server `json:"servers,omitempty"`
|
|
|
|
CircuitBreaker *CircuitBreaker `json:"circuitBreaker,omitempty"`
|
|
|
|
LoadBalancer *LoadBalancer `json:"loadBalancer,omitempty"`
|
2016-04-13 08:11:36 +00:00
|
|
|
MaxConn *MaxConn `json:"maxConn,omitempty"`
|
2016-11-26 18:48:49 +00:00
|
|
|
HealthCheck *HealthCheck `json:"healthCheck,omitempty"`
|
2016-04-13 08:11:36 +00:00
|
|
|
}
|
|
|
|
|
2016-04-21 22:38:44 +00:00
|
|
|
// MaxConn holds maximum connection configuration
|
2016-04-13 08:11:36 +00:00
|
|
|
type MaxConn struct {
|
|
|
|
Amount int64 `json:"amount,omitempty"`
|
|
|
|
ExtractorFunc string `json:"extractorFunc,omitempty"`
|
2015-11-01 15:35:01 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 18:15:05 +00:00
|
|
|
// LoadBalancer holds load balancing configuration.
|
2015-11-01 15:35:01 +00:00
|
|
|
type LoadBalancer struct {
|
|
|
|
Method string `json:"method,omitempty"`
|
2016-05-13 14:22:11 +00:00
|
|
|
Sticky bool `json:"sticky,omitempty"`
|
2015-11-01 15:35:01 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 18:15:05 +00:00
|
|
|
// CircuitBreaker holds circuit breaker configuration.
|
2015-11-01 15:35:01 +00:00
|
|
|
type CircuitBreaker struct {
|
|
|
|
Expression string `json:"expression,omitempty"`
|
|
|
|
}
|
|
|
|
|
2017-01-31 21:55:02 +00:00
|
|
|
// HealthCheck holds HealthCheck configuration
|
2016-11-26 18:48:49 +00:00
|
|
|
type HealthCheck struct {
|
2017-03-14 00:22:08 +00:00
|
|
|
Path string `json:"path,omitempty"`
|
2017-02-06 21:59:50 +00:00
|
|
|
Interval string `json:"interval,omitempty"`
|
2016-11-26 18:48:49 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 18:15:05 +00:00
|
|
|
// Server holds server configuration.
|
2015-11-01 15:35:01 +00:00
|
|
|
type Server struct {
|
|
|
|
URL string `json:"url,omitempty"`
|
2016-06-06 20:30:23 +00:00
|
|
|
Weight int `json:"weight"`
|
2015-11-01 15:35:01 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 18:15:05 +00:00
|
|
|
// Route holds route configuration.
|
2015-11-01 15:35:01 +00:00
|
|
|
type Route struct {
|
2016-03-27 00:05:17 +00:00
|
|
|
Rule string `json:"rule,omitempty"`
|
2015-11-01 15:35:01 +00:00
|
|
|
}
|
|
|
|
|
2017-06-30 23:04:18 +00:00
|
|
|
//ErrorPage holds custom error page configuration
|
|
|
|
type ErrorPage struct {
|
|
|
|
Status []string `json:"status,omitempty"`
|
|
|
|
Backend string `json:"backend,omitempty"`
|
|
|
|
Query string `json:"query,omitempty"`
|
|
|
|
}
|
|
|
|
|
2017-06-13 00:48:21 +00:00
|
|
|
// Headers holds the custom header configuration
|
|
|
|
type Headers struct {
|
|
|
|
CustomRequestHeaders map[string]string `json:"customRequestHeaders,omitempty"`
|
|
|
|
CustomResponseHeaders map[string]string `json:"customResponseHeaders,omitempty"`
|
|
|
|
AllowedHosts []string `json:"allowedHosts,omitempty"`
|
|
|
|
HostsProxyHeaders []string `json:"hostsProxyHeaders,omitempty"`
|
|
|
|
SSLRedirect bool `json:"sslRedirect,omitempty"`
|
|
|
|
SSLTemporaryRedirect bool `json:"sslTemporaryRedirect,omitempty"`
|
|
|
|
SSLHost string `json:"sslHost,omitempty"`
|
|
|
|
SSLProxyHeaders map[string]string `json:"sslProxyHeaders,omitempty"`
|
|
|
|
STSSeconds int64 `json:"stsSeconds,omitempty"`
|
|
|
|
STSIncludeSubdomains bool `json:"stsIncludeSubdomains,omitempty"`
|
|
|
|
STSPreload bool `json:"stsPreload,omitempty"`
|
|
|
|
ForceSTSHeader bool `json:"forceSTSHeader,omitempty"`
|
|
|
|
FrameDeny bool `json:"frameDeny,omitempty"`
|
|
|
|
CustomFrameOptionsValue string `json:"customFrameOptionsValue,omitempty"`
|
|
|
|
ContentTypeNosniff bool `json:"contentTypeNosniff,omitempty"`
|
|
|
|
BrowserXSSFilter bool `json:"browserXssFilter,omitempty"`
|
|
|
|
ContentSecurityPolicy string `json:"contentSecurityPolicy,omitempty"`
|
|
|
|
PublicKey string `json:"publicKey,omitempty"`
|
|
|
|
ReferrerPolicy string `json:"referrerPolicy,omitempty"`
|
|
|
|
IsDevelopment bool `json:"isDevelopment,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasCustomHeadersDefined checks to see if any of the custom header elements have been set
|
|
|
|
func (h Headers) HasCustomHeadersDefined() bool {
|
|
|
|
return len(h.CustomResponseHeaders) != 0 ||
|
|
|
|
len(h.CustomRequestHeaders) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasSecureHeadersDefined checks to see if any of the secure header elements have been set
|
|
|
|
func (h Headers) HasSecureHeadersDefined() bool {
|
|
|
|
return len(h.AllowedHosts) != 0 ||
|
|
|
|
len(h.HostsProxyHeaders) != 0 ||
|
|
|
|
h.SSLRedirect ||
|
|
|
|
h.SSLTemporaryRedirect ||
|
|
|
|
h.SSLHost != "" ||
|
|
|
|
len(h.SSLProxyHeaders) != 0 ||
|
|
|
|
h.STSSeconds != 0 ||
|
|
|
|
h.STSIncludeSubdomains ||
|
|
|
|
h.STSPreload ||
|
|
|
|
h.ForceSTSHeader ||
|
|
|
|
h.FrameDeny ||
|
|
|
|
h.CustomFrameOptionsValue != "" ||
|
|
|
|
h.ContentTypeNosniff ||
|
|
|
|
h.BrowserXSSFilter ||
|
|
|
|
h.ContentSecurityPolicy != "" ||
|
|
|
|
h.PublicKey != "" ||
|
|
|
|
h.ReferrerPolicy != "" ||
|
|
|
|
h.IsDevelopment
|
|
|
|
}
|
|
|
|
|
2015-11-01 18:15:05 +00:00
|
|
|
// Frontend holds frontend configuration.
|
2015-11-01 15:35:01 +00:00
|
|
|
type Frontend struct {
|
2017-06-30 23:04:18 +00:00
|
|
|
EntryPoints []string `json:"entryPoints,omitempty"`
|
|
|
|
Backend string `json:"backend,omitempty"`
|
|
|
|
Routes map[string]Route `json:"routes,omitempty"`
|
|
|
|
PassHostHeader bool `json:"passHostHeader,omitempty"`
|
|
|
|
PassTLSCert bool `json:"passTLSCert,omitempty"`
|
|
|
|
Priority int `json:"priority"`
|
|
|
|
BasicAuth []string `json:"basicAuth"`
|
|
|
|
WhitelistSourceRange []string `json:"whitelistSourceRange,omitempty"`
|
|
|
|
Headers Headers `json:"headers,omitempty"`
|
|
|
|
Errors map[string]ErrorPage `json:"errors,omitempty"`
|
2015-11-01 15:35:01 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 18:15:05 +00:00
|
|
|
// LoadBalancerMethod holds the method of load balancing to use.
|
2015-11-01 15:35:01 +00:00
|
|
|
type LoadBalancerMethod uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Wrr (default) = Weighted Round Robin
|
|
|
|
Wrr LoadBalancerMethod = iota
|
|
|
|
// Drr = Dynamic Round Robin
|
|
|
|
Drr
|
|
|
|
)
|
|
|
|
|
|
|
|
var loadBalancerMethodNames = []string{
|
|
|
|
"Wrr",
|
|
|
|
"Drr",
|
|
|
|
}
|
|
|
|
|
2015-11-01 18:15:05 +00:00
|
|
|
// NewLoadBalancerMethod create a new LoadBalancerMethod from a given LoadBalancer.
|
2015-11-01 15:35:01 +00:00
|
|
|
func NewLoadBalancerMethod(loadBalancer *LoadBalancer) (LoadBalancerMethod, error) {
|
2017-05-10 22:34:47 +00:00
|
|
|
var method string
|
2015-11-01 15:35:01 +00:00
|
|
|
if loadBalancer != nil {
|
2017-05-10 22:34:47 +00:00
|
|
|
method = loadBalancer.Method
|
2015-11-01 15:35:01 +00:00
|
|
|
for i, name := range loadBalancerMethodNames {
|
2017-05-10 22:34:47 +00:00
|
|
|
if strings.EqualFold(name, method) {
|
2015-11-01 15:35:01 +00:00
|
|
|
return LoadBalancerMethod(i), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-10 22:34:47 +00:00
|
|
|
return Wrr, fmt.Errorf("invalid load-balancing method '%s'", method)
|
2015-11-01 15:35:01 +00:00
|
|
|
}
|
|
|
|
|
2017-08-25 14:10:03 +00:00
|
|
|
// Configurations is for currentConfigurations Map
|
|
|
|
type Configurations map[string]*Configuration
|
|
|
|
|
2015-11-01 18:15:05 +00:00
|
|
|
// Configuration of a provider.
|
2015-11-01 15:35:01 +00:00
|
|
|
type Configuration struct {
|
|
|
|
Backends map[string]*Backend `json:"backends,omitempty"`
|
|
|
|
Frontends map[string]*Frontend `json:"frontends,omitempty"`
|
|
|
|
}
|
|
|
|
|
2015-11-01 18:15:05 +00:00
|
|
|
// ConfigMessage hold configuration information exchanged between parts of traefik.
|
2015-11-01 15:35:01 +00:00
|
|
|
type ConfigMessage struct {
|
|
|
|
ProviderName string
|
|
|
|
Configuration *Configuration
|
|
|
|
}
|
2016-05-30 13:05:58 +00:00
|
|
|
|
|
|
|
// Constraint hold a parsed constraint expresssion
|
|
|
|
type Constraint struct {
|
|
|
|
Key string
|
|
|
|
// MustMatch is true if operator is "==" or false if operator is "!="
|
|
|
|
MustMatch bool
|
2016-05-31 07:54:42 +00:00
|
|
|
// TODO: support regex
|
|
|
|
Regex string
|
2016-05-30 13:05:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 15:17:38 +00:00
|
|
|
// NewConstraint receive a string and return a *Constraint, after checking syntax and parsing the constraint expression
|
2016-05-30 13:05:58 +00:00
|
|
|
func NewConstraint(exp string) (*Constraint, error) {
|
|
|
|
sep := ""
|
|
|
|
constraint := &Constraint{}
|
|
|
|
|
|
|
|
if strings.Contains(exp, "==") {
|
|
|
|
sep = "=="
|
|
|
|
constraint.MustMatch = true
|
|
|
|
} else if strings.Contains(exp, "!=") {
|
|
|
|
sep = "!="
|
|
|
|
constraint.MustMatch = false
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("Constraint expression missing valid operator: '==' or '!='")
|
|
|
|
}
|
|
|
|
|
|
|
|
kv := strings.SplitN(exp, sep, 2)
|
|
|
|
if len(kv) == 2 {
|
|
|
|
// At the moment, it only supports tags
|
|
|
|
if kv[0] != "tag" {
|
|
|
|
return nil, errors.New("Constraint must be tag-based. Syntax: tag==us-*")
|
|
|
|
}
|
|
|
|
|
|
|
|
constraint.Key = kv[0]
|
|
|
|
constraint.Regex = kv[1]
|
|
|
|
return constraint, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("Incorrect constraint expression: " + exp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Constraint) String() string {
|
|
|
|
if c.MustMatch {
|
|
|
|
return c.Key + "==" + c.Regex
|
|
|
|
}
|
|
|
|
return c.Key + "!=" + c.Regex
|
|
|
|
}
|
|
|
|
|
2016-11-09 18:27:04 +00:00
|
|
|
var _ encoding.TextUnmarshaler = (*Constraint)(nil)
|
|
|
|
|
2016-09-20 14:56:29 +00:00
|
|
|
// UnmarshalText define how unmarshal in TOML parsing
|
|
|
|
func (c *Constraint) UnmarshalText(text []byte) error {
|
|
|
|
constraint, err := NewConstraint(string(text))
|
2016-11-09 18:27:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.Key = constraint.Key
|
|
|
|
c.MustMatch = constraint.MustMatch
|
|
|
|
c.Regex = constraint.Regex
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ encoding.TextMarshaler = (*Constraint)(nil)
|
|
|
|
|
|
|
|
// MarshalText encodes the receiver into UTF-8-encoded text and returns the result.
|
|
|
|
func (c *Constraint) MarshalText() (text []byte, err error) {
|
|
|
|
return []byte(c.String()), nil
|
2016-09-20 14:56:29 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 15:17:38 +00:00
|
|
|
// MatchConstraintWithAtLeastOneTag tests a constraint for one single service
|
2016-05-30 13:05:58 +00:00
|
|
|
func (c *Constraint) MatchConstraintWithAtLeastOneTag(tags []string) bool {
|
|
|
|
for _, tag := range tags {
|
|
|
|
if glob.Glob(c.Regex, tag) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2016-06-02 13:17:04 +00:00
|
|
|
|
|
|
|
//Set []*Constraint
|
|
|
|
func (cs *Constraints) Set(str string) error {
|
|
|
|
exps := strings.Split(str, ",")
|
|
|
|
if len(exps) == 0 {
|
|
|
|
return errors.New("Bad Constraint format: " + str)
|
|
|
|
}
|
|
|
|
for _, exp := range exps {
|
|
|
|
constraint, err := NewConstraint(exp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-09 18:27:04 +00:00
|
|
|
*cs = append(*cs, constraint)
|
2016-06-02 13:17:04 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constraints holds a Constraint parser
|
2016-11-09 18:27:04 +00:00
|
|
|
type Constraints []*Constraint
|
2016-06-02 13:17:04 +00:00
|
|
|
|
|
|
|
//Get []*Constraint
|
2016-11-09 18:27:04 +00:00
|
|
|
func (cs *Constraints) Get() interface{} { return []*Constraint(*cs) }
|
2016-06-02 13:17:04 +00:00
|
|
|
|
|
|
|
//String returns []*Constraint in string
|
|
|
|
func (cs *Constraints) String() string { return fmt.Sprintf("%+v", *cs) }
|
|
|
|
|
|
|
|
//SetValue sets []*Constraint into the parser
|
|
|
|
func (cs *Constraints) SetValue(val interface{}) {
|
|
|
|
*cs = Constraints(val.(Constraints))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type exports the Constraints type as a string
|
|
|
|
func (cs *Constraints) Type() string {
|
2017-08-18 00:18:02 +00:00
|
|
|
return "constraint"
|
2016-06-02 13:17:04 +00:00
|
|
|
}
|
2016-07-20 22:29:00 +00:00
|
|
|
|
2016-08-16 17:13:18 +00:00
|
|
|
// Store holds KV store cluster config
|
|
|
|
type Store struct {
|
|
|
|
store.Store
|
|
|
|
Prefix string // like this "prefix" (without the /)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cluster holds cluster config
|
|
|
|
type Cluster struct {
|
2016-08-18 12:20:11 +00:00
|
|
|
Node string `description:"Node name"`
|
2016-08-18 11:03:10 +00:00
|
|
|
Store *Store
|
2016-08-16 17:13:18 +00:00
|
|
|
}
|
|
|
|
|
2016-07-20 22:29:00 +00:00
|
|
|
// Auth holds authentication configuration (BASIC, DIGEST, users)
|
|
|
|
type Auth struct {
|
2016-12-16 15:42:51 +00:00
|
|
|
Basic *Basic
|
|
|
|
Digest *Digest
|
2017-08-25 16:22:03 +00:00
|
|
|
Forward *Forward
|
2016-12-16 15:42:51 +00:00
|
|
|
HeaderField string
|
2016-07-20 22:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Users authentication users
|
|
|
|
type Users []string
|
|
|
|
|
|
|
|
// Basic HTTP basic authentication
|
|
|
|
type Basic struct {
|
2017-02-24 02:46:50 +00:00
|
|
|
Users `mapstructure:","`
|
|
|
|
UsersFile string
|
2016-07-20 22:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Digest HTTP authentication
|
|
|
|
type Digest struct {
|
2017-02-24 02:46:50 +00:00
|
|
|
Users `mapstructure:","`
|
|
|
|
UsersFile string
|
2016-07-20 22:29:00 +00:00
|
|
|
}
|
2016-10-14 14:04:09 +00:00
|
|
|
|
2017-08-25 16:22:03 +00:00
|
|
|
// Forward authentication
|
|
|
|
type Forward struct {
|
|
|
|
Address string `description:"Authentication server address"`
|
|
|
|
TLS *ClientTLS `description:"Enable TLS support"`
|
|
|
|
}
|
|
|
|
|
2016-10-14 14:04:09 +00:00
|
|
|
// CanonicalDomain returns a lower case domain with trim space
|
|
|
|
func CanonicalDomain(domain string) string {
|
|
|
|
return strings.ToLower(strings.TrimSpace(domain))
|
|
|
|
}
|
2016-10-21 08:36:07 +00:00
|
|
|
|
|
|
|
// Statistics provides options for monitoring request and response stats
|
|
|
|
type Statistics struct {
|
|
|
|
RecentErrors int `description:"Number of recent errors logged"`
|
|
|
|
}
|
2017-01-12 13:34:54 +00:00
|
|
|
|
|
|
|
// Metrics provides options to expose and send Traefik metrics to different third party monitoring systems
|
|
|
|
type Metrics struct {
|
|
|
|
Prometheus *Prometheus `description:"Prometheus metrics exporter type"`
|
2017-07-20 22:26:43 +00:00
|
|
|
Datadog *Datadog `description:"DataDog metrics exporter type"`
|
|
|
|
StatsD *Statsd `description:"StatsD metrics exporter type"`
|
2017-01-12 13:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prometheus can contain specific configuration used by the Prometheus Metrics exporter
|
|
|
|
type Prometheus struct {
|
|
|
|
Buckets Buckets `description:"Buckets for latency metrics"`
|
|
|
|
}
|
|
|
|
|
2017-07-20 22:26:43 +00:00
|
|
|
// Datadog contains address and metrics pushing interval configuration
|
|
|
|
type Datadog struct {
|
|
|
|
Address string `description:"DataDog's Dogstatsd address"`
|
|
|
|
PushInterval string `description:"DataDog push interval"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Statsd contains address and metrics pushing interval configuration
|
|
|
|
type Statsd struct {
|
|
|
|
Address string `description:"StatsD address"`
|
|
|
|
PushInterval string `description:"DataDog push interval"`
|
|
|
|
}
|
|
|
|
|
2017-01-12 13:34:54 +00:00
|
|
|
// Buckets holds Prometheus Buckets
|
|
|
|
type Buckets []float64
|
|
|
|
|
|
|
|
//Set adds strings elem into the the parser
|
|
|
|
//it splits str on "," and ";" and apply ParseFloat to string
|
|
|
|
func (b *Buckets) Set(str string) error {
|
|
|
|
fargs := func(c rune) bool {
|
|
|
|
return c == ',' || c == ';'
|
|
|
|
}
|
|
|
|
// get function
|
|
|
|
slice := strings.FieldsFunc(str, fargs)
|
|
|
|
for _, bucket := range slice {
|
|
|
|
bu, err := strconv.ParseFloat(bucket, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*b = append(*b, bu)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//Get []float64
|
|
|
|
func (b *Buckets) Get() interface{} { return Buckets(*b) }
|
|
|
|
|
|
|
|
//String return slice in a string
|
|
|
|
func (b *Buckets) String() string { return fmt.Sprintf("%v", *b) }
|
|
|
|
|
|
|
|
//SetValue sets []float64 into the parser
|
|
|
|
func (b *Buckets) SetValue(val interface{}) {
|
|
|
|
*b = Buckets(val.(Buckets))
|
|
|
|
}
|
2017-05-25 11:25:53 +00:00
|
|
|
|
|
|
|
// AccessLog holds the configuration settings for the access logger (middlewares/accesslog).
|
|
|
|
type AccessLog struct {
|
2017-05-30 10:06:49 +00:00
|
|
|
FilePath string `json:"file,omitempty" description:"Access log file path. Stdout is used when omitted or empty"`
|
2017-05-25 11:25:53 +00:00
|
|
|
Format string `json:"format,omitempty" description:"Access log format: json | common"`
|
|
|
|
}
|
2017-08-25 16:22:03 +00:00
|
|
|
|
|
|
|
// ClientTLS holds TLS specific configurations as client
|
|
|
|
// CA, Cert and Key can be either path or file contents
|
|
|
|
type ClientTLS struct {
|
|
|
|
CA string `description:"TLS CA"`
|
|
|
|
Cert string `description:"TLS cert"`
|
|
|
|
Key string `description:"TLS key"`
|
|
|
|
InsecureSkipVerify bool `description:"TLS insecure skip verify"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateTLSConfig creates a TLS config from ClientTLS structures
|
|
|
|
func (clientTLS *ClientTLS) CreateTLSConfig() (*tls.Config, error) {
|
|
|
|
var err error
|
|
|
|
if clientTLS == nil {
|
|
|
|
log.Warnf("clientTLS is nil")
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
caPool := x509.NewCertPool()
|
|
|
|
if clientTLS.CA != "" {
|
|
|
|
var ca []byte
|
|
|
|
if _, errCA := os.Stat(clientTLS.CA); errCA == nil {
|
|
|
|
ca, err = ioutil.ReadFile(clientTLS.CA)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to read CA. %s", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ca = []byte(clientTLS.CA)
|
|
|
|
}
|
|
|
|
caPool.AppendCertsFromPEM(ca)
|
|
|
|
}
|
|
|
|
|
|
|
|
cert := tls.Certificate{}
|
|
|
|
_, errKeyIsFile := os.Stat(clientTLS.Key)
|
|
|
|
|
|
|
|
if !clientTLS.InsecureSkipVerify && (len(clientTLS.Cert) == 0 || len(clientTLS.Key) == 0) {
|
|
|
|
return nil, fmt.Errorf("TLS Certificate or Key file must be set when TLS configuration is created")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(clientTLS.Cert) > 0 && len(clientTLS.Key) > 0 {
|
|
|
|
if _, errCertIsFile := os.Stat(clientTLS.Cert); errCertIsFile == nil {
|
|
|
|
if errKeyIsFile == nil {
|
|
|
|
cert, err = tls.LoadX509KeyPair(clientTLS.Cert, clientTLS.Key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to load TLS keypair: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("tls cert is a file, but tls key is not")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if errKeyIsFile != nil {
|
|
|
|
cert, err = tls.X509KeyPair([]byte(clientTLS.Cert), []byte(clientTLS.Key))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to load TLS keypair: %v", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("tls key is a file, but tls cert is not")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TLSConfig := &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{cert},
|
|
|
|
RootCAs: caPool,
|
|
|
|
InsecureSkipVerify: clientTLS.InsecureSkipVerify,
|
|
|
|
}
|
|
|
|
return TLSConfig, nil
|
|
|
|
}
|