diff --git a/CHANGELOG.md b/CHANGELOG.md index 62ef5602b..ab2a1245e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,30 @@ # Change Log +## [v1.7.3](https://github.com/containous/traefik/tree/v1.7.3) (2018-10-15) +[All Commits](https://github.com/containous/traefik/compare/v1.7.2...v1.7.3) + +**Enhancements:** +- Improve the CLI help ([#3996](https://github.com/containous/traefik/pull/3996) by [dduportal](https://github.com/dduportal)) + +**Bug fixes:** +- **[acme]** DNS challenge Cloudflare auth zone ([#4042](https://github.com/containous/traefik/pull/4042) by [ldez](https://github.com/ldez)) +- **[acme]** ACME DNS challenges ([#3998](https://github.com/containous/traefik/pull/3998) by [ldez](https://github.com/ldez)) +- **[acme]** Don't initalize ACME provider if storage is empty ([#3988](https://github.com/containous/traefik/pull/3988) by [nmengin](https://github.com/nmengin)) +- **[acme]** Fix: acme DNS providers ([#4021](https://github.com/containous/traefik/pull/4021) by [ldez](https://github.com/ldez)) +- **[acme]** Prevent some malformed errors in LE. ([#4015](https://github.com/containous/traefik/pull/4015) by [ldez](https://github.com/ldez)) +- **[authentication,consulcatalog,docker,ecs,etcd,kv,marathon,mesos,rancher]** Add the AuthResponseHeaders to the labels ([#3973](https://github.com/containous/traefik/pull/3973) by [Crypto89](https://github.com/Crypto89)) +- **[docker]** usebindportip can fall back on the container ip / port ([#4018](https://github.com/containous/traefik/pull/4018) by [geraldcroes](https://github.com/geraldcroes)) +- **[k8s]** Avoid flapping of multiple Ingress definitions ([#3862](https://github.com/containous/traefik/pull/3862) by [rtreffer](https://github.com/rtreffer)) +- **[middleware,server]** Log stack on panic ([#4033](https://github.com/containous/traefik/pull/4033) by [ldez](https://github.com/ldez)) +- **[middleware,server]** Fix recover from panic handler ([#4031](https://github.com/containous/traefik/pull/4031) by [mmatur](https://github.com/mmatur)) +- **[server,websocket]** Fix update oxy ([#4009](https://github.com/containous/traefik/pull/4009) by [mmatur](https://github.com/mmatur)) + +**Documentation:** +- **[docker]** Add tags label to Docker provider documentation ([#3896](https://github.com/containous/traefik/pull/3896) by [artheus](https://github.com/artheus)) +- **[docker]** Added two examples with labels in docker-compose.yml ([#3891](https://github.com/containous/traefik/pull/3891) by [pascalandy](https://github.com/pascalandy)) +- **[k8s]** Move buffering annotation documentation to service ([#3991](https://github.com/containous/traefik/pull/3991) by [ldez](https://github.com/ldez)) +- Fix a typo ([#3995](https://github.com/containous/traefik/pull/3995) by [arnydo](https://github.com/arnydo)) + ## [v1.7.2](https://github.com/containous/traefik/tree/v1.7.2) (2018-10-04) [All Commits](https://github.com/containous/traefik/compare/v1.7.1...v1.7.2) diff --git a/Gopkg.lock b/Gopkg.lock index 2aedc1b4d..ec392456e 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -258,6 +258,12 @@ packages = ["."] revision = "2ea60e5f094469f9e65adb9cd103795b73ae743e" +[[projects]] + name = "github.com/cloudflare/cloudflare-go" + packages = ["."] + revision = "1f9007fbecae20711133c60519338c41cef1ffb4" + version = "v0.8.5" + [[projects]] branch = "master" name = "github.com/codahale/hdrhistogram" @@ -276,8 +282,8 @@ ".", "parse" ] - revision = "b4c2f060875361c070ed2bc300c5929b82f5fa2e" - version = "v1.1.2" + revision = "aad81c7ac7f49671a59b9ede8ab22436e132a302" + version = "v1.3.0" [[projects]] branch = "master" @@ -626,6 +632,12 @@ packages = ["."] revision = "1d0bd113de87027671077d3c71eb3ac5d7dbba72" +[[projects]] + name = "github.com/go-resty/resty" + packages = ["."] + revision = "d4920dcf5b7689548a6db640278a9b35a5b48ec6" + version = "v1.9.1" + [[projects]] name = "github.com/go-stack/stack" packages = ["."] @@ -802,7 +814,6 @@ revision = "9b66602d496a139e4722bdde32f0f1ac1c12d4a8" [[projects]] - branch = "master" name = "github.com/jjcollinge/servicefabric" packages = ["."] revision = "8eebe170fa1ba25d3dfb928b3f86a7313b13b9fe" @@ -862,6 +873,12 @@ packages = ["."] revision = "1113af38e5916529ad7317b0fe12e273e6e92af5" +[[projects]] + name = "github.com/linode/linodego" + packages = ["."] + revision = "d0d31d8ca62fa3f7e4526ca0ce95de81e4ed001e" + version = "v0.5.1" + [[projects]] name = "github.com/mailgun/minheap" packages = ["."] @@ -1295,7 +1312,7 @@ "roundrobin", "utils" ] - revision = "fe51048067db50958154cd4040da878b10002a3a" + revision = "7d94d212f808222b72fd0b8bb171bfcd4e27ffca" [[projects]] name = "github.com/vulcand/predicate" @@ -1339,6 +1356,7 @@ "providers/dns/dnsimple", "providers/dns/dnsmadeeasy", "providers/dns/dnspod", + "providers/dns/dreamhost", "providers/dns/duckdns", "providers/dns/dyn", "providers/dns/exec", @@ -1353,6 +1371,7 @@ "providers/dns/iij", "providers/dns/lightsail", "providers/dns/linode", + "providers/dns/linodev4", "providers/dns/namecheap", "providers/dns/namedotcom", "providers/dns/netcup", @@ -1365,10 +1384,11 @@ "providers/dns/rfc2136", "providers/dns/route53", "providers/dns/sakuracloud", + "providers/dns/stackpath", "providers/dns/vegadns", "providers/dns/vultr" ] - revision = "621237d07213aa6dead90bdf6fd46251220fa669" + revision = "160d6fe60303699067faad57dc0b1e147ac499ef" [[projects]] branch = "master" @@ -1403,6 +1423,7 @@ "ipv4", "ipv6", "proxy", + "publicsuffix", "trace", "websocket" ] @@ -1413,6 +1434,7 @@ name = "golang.org/x/oauth2" packages = [ ".", + "clientcredentials", "google", "internal", "jws", @@ -1789,6 +1811,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "8eb11befb583dfb89320dd7b7aea9e54b40e0523e60e66ed418788394b873d44" + inputs-digest = "37e89a543fca153d166cc70fd7fed689f06d894140bf617f69f5f664ffee621e" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index 6e1a4eaab..cdb1c4781 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -54,7 +54,7 @@ [[constraint]] name = "github.com/containous/flaeg" - version = "1.0.1" + version = "1.3.0" [[constraint]] branch = "master" diff --git a/acme/acme.go b/acme/acme.go index b1e56f65f..73f20b8ae 100644 --- a/acme/acme.go +++ b/acme/acme.go @@ -692,16 +692,25 @@ func searchUncheckedDomains(domains []string, certs map[string]*tls.Certificate) } func (a *ACME) getDomainsCertificates(domains []string) (*Certificate, error) { - domains = fun.Map(types.CanonicalDomain, domains).([]string) - log.Debugf("Loading ACME certificates %s...", domains) + var cleanDomains []string + for _, domain := range domains { + canonicalDomain := types.CanonicalDomain(domain) + cleanDomain := acme.UnFqdn(canonicalDomain) + if canonicalDomain != cleanDomain { + log.Warnf("FQDN detected, please remove the trailing dot: %s", canonicalDomain) + } + cleanDomains = append(cleanDomains, cleanDomain) + } + + log.Debugf("Loading ACME certificates %s...", cleanDomains) bundle := true - certificate, err := a.client.ObtainCertificate(domains, bundle, nil, OSCPMustStaple) + certificate, err := a.client.ObtainCertificate(cleanDomains, bundle, nil, OSCPMustStaple) if err != nil { return nil, fmt.Errorf("cannot obtain certificates: %+v", err) } - log.Debugf("Loaded ACME certificates %s", domains) + log.Debugf("Loaded ACME certificates %s", cleanDomains) return &Certificate{ Domain: certificate.Domain, CertURL: certificate.CertURL, diff --git a/autogen/gentemplates/gen.go b/autogen/gentemplates/gen.go index 168c7c653..588fdebd2 100644 --- a/autogen/gentemplates/gen.go +++ b/autogen/gentemplates/gen.go @@ -162,6 +162,11 @@ var _templatesConsul_catalogTmpl = []byte(`[backends] [frontends."frontend-{{ $service.ServiceName }}".auth.forward] address = "{{ $auth.Forward.Address }}" trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }} + {{if $auth.Forward.AuthResponseHeaders }} + authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }} + "{{.}}", + {{end}}] + {{end}} {{if $auth.Forward.TLS }} [frontends."frontend-{{ $service.ServiceName }}".auth.forward.tls] @@ -431,6 +436,11 @@ var _templatesDockerTmpl = []byte(`{{$backendServers := .Servers}} [frontends."frontend-{{ $frontendName }}".auth.forward] address = "{{ $auth.Forward.Address }}" trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }} + {{if $auth.Forward.AuthResponseHeaders }} + authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }} + "{{.}}", + {{end}}] + {{end}} {{if $auth.Forward.TLS }} [frontends."frontend-{{ $frontendName }}".auth.forward.tls] @@ -703,6 +713,11 @@ var _templatesEcsTmpl = []byte(`[backends] [frontends."frontend-{{ $frontendName }}".auth.forward] address = "{{ $auth.Forward.Address }}" trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }} + {{if $auth.Forward.AuthResponseHeaders }} + authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }} + "{{.}}", + {{end}}] + {{end}} {{if $auth.Forward.TLS }} [frontends."frontend-{{ $frontendName }}".auth.forward.tls] @@ -1230,6 +1245,11 @@ var _templatesKvTmpl = []byte(`[backends] [frontends."{{ $frontendName }}".auth.forward] address = "{{ $auth.Forward.Address }}" trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }} + {{if $auth.Forward.AuthResponseHeaders }} + authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }} + "{{.}}", + {{end}}] + {{end}} {{if $auth.Forward.TLS }} [frontends."{{ $frontendName }}".auth.forward.tls] @@ -1516,6 +1536,11 @@ var _templatesMarathonTmpl = []byte(`{{ $apps := .Applications }} [frontends."{{ $frontendName }}".auth.forward] address = "{{ $auth.Forward.Address }}" trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }} + {{if $auth.Forward.AuthResponseHeaders }} + authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }} + "{{.}}", + {{end}}] + {{end}} {{if $auth.Forward.TLS }} [frontends."{{ $frontendName }}".auth.forward.tls] @@ -1787,6 +1812,11 @@ var _templatesMesosTmpl = []byte(`[backends] [frontends."frontend-{{ $frontendName }}".auth.forward] address = "{{ $auth.Forward.Address }}" trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }} + {{if $auth.Forward.AuthResponseHeaders }} + authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }} + "{{.}}", + {{end}}] + {{end}} {{if $auth.Forward.TLS }} [frontends."frontend-{{ $frontendName }}".auth.forward.tls] @@ -2080,6 +2110,11 @@ var _templatesRancherTmpl = []byte(`{{ $backendServers := .Backends }} [frontends."frontend-{{ $frontendName }}".auth.forward] address = "{{ $auth.Forward.Address }}" trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }} + {{if $auth.Forward.AuthResponseHeaders }} + authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }} + "{{.}}", + {{end}}] + {{end}} {{if $auth.Forward.TLS }} [frontends."frontend-{{ $frontendName }}".auth.forward.tls] diff --git a/build.Dockerfile b/build.Dockerfile index d7a279b5d..52b1b8bab 100644 --- a/build.Dockerfile +++ b/build.Dockerfile @@ -5,7 +5,7 @@ RUN apk --update upgrade \ && rm -rf /var/cache/apk/* RUN go get github.com/containous/go-bindata/... \ -&& go get github.com/golang/lint/golint \ +&& go get golang.org/x/lint/golint \ && go get github.com/kisielk/errcheck \ && go get github.com/client9/misspell/cmd/misspell diff --git a/configuration/configuration.go b/configuration/configuration.go index b20075042..4c3bb9ce3 100644 --- a/configuration/configuration.go +++ b/configuration/configuration.go @@ -34,6 +34,7 @@ import ( "github.com/containous/traefik/tls" "github.com/containous/traefik/types" "github.com/pkg/errors" + lego "github.com/xenolf/lego/acme" ) const ( @@ -260,6 +261,17 @@ func (gc *GlobalConfiguration) initACMEProvider() { gc.ACME.HTTPChallenge = nil } + for _, domain := range gc.ACME.Domains { + if domain.Main != lego.UnFqdn(domain.Main) { + log.Warnf("FQDN detected, please remove the trailing dot: %s", domain.Main) + } + for _, san := range domain.SANs { + if san != lego.UnFqdn(san) { + log.Warnf("FQDN detected, please remove the trailing dot: %s", san) + } + } + } + if len(gc.ACME.DNSProvider) > 0 { log.Warn("ACME.DNSProvider is deprecated, use ACME.DNSChallenge instead") gc.ACME.DNSChallenge = &acmeprovider.DNSChallenge{Provider: gc.ACME.DNSProvider, DelayBeforeCheck: gc.ACME.DelayDontCheckDNS} diff --git a/docs/configuration/acme.md b/docs/configuration/acme.md index a4ae9d1b8..1a50f64c0 100644 --- a/docs/configuration/acme.md +++ b/docs/configuration/acme.md @@ -257,19 +257,20 @@ Here is a list of supported `provider`s, that can automate the DNS verification, | [Auroradns](https://www.pcextreme.com/aurora/dns) | `auroradns` | `AURORA_USER_ID`, `AURORA_KEY`, `AURORA_ENDPOINT` | Not tested yet | | [Azure](https://azure.microsoft.com/services/dns/) | `azure` | `AZURE_CLIENT_ID`, `AZURE_CLIENT_SECRET`, `AZURE_SUBSCRIPTION_ID`, `AZURE_TENANT_ID`, `AZURE_RESOURCE_GROUP` | Not tested yet | | [Blue Cat](https://www.bluecatnetworks.com/) | `bluecat` | `BLUECAT_SERVER_URL`, `BLUECAT_USER_NAME`, `BLUECAT_PASSWORD`, `BLUECAT_CONFIG_NAME`, `BLUECAT_DNS_VIEW` | Not tested yet | -| [Cloudflare](https://www.cloudflare.com) | `cloudflare` | `CLOUDFLARE_EMAIL`, `CLOUDFLARE_API_KEY` - The `Global API Key` needs to be used, not the `Origin CA Key` | YES | +| [Cloudflare](https://www.cloudflare.com) | `cloudflare` | `CF_API_EMAIL`, `CF_API_KEY` - The `Global API Key` needs to be used, not the `Origin CA Key` | YES | | [CloudXNS](https://www.cloudxns.net) | `cloudxns` | `CLOUDXNS_API_KEY`, `CLOUDXNS_SECRET_KEY` | Not tested yet | | [DigitalOcean](https://www.digitalocean.com) | `digitalocean` | `DO_AUTH_TOKEN` | YES | | [DNSimple](https://dnsimple.com) | `dnsimple` | `DNSIMPLE_OAUTH_TOKEN`, `DNSIMPLE_BASE_URL` | Not tested yet | | [DNS Made Easy](https://dnsmadeeasy.com) | `dnsmadeeasy` | `DNSMADEEASY_API_KEY`, `DNSMADEEASY_API_SECRET`, `DNSMADEEASY_SANDBOX` | Not tested yet | | [DNSPod](http://www.dnspod.net/) | `dnspod` | `DNSPOD_API_KEY` | Not tested yet | +| [DreamHost](https://www.dreamhost.com/) | `dreamhost` | `DREAMHOST_API_KEY` | YES | | [Duck DNS](https://www.duckdns.org/) | `duckdns` | `DUCKDNS_TOKEN` | Not tested yet | | [Dyn](https://dyn.com) | `dyn` | `DYN_CUSTOMER_NAME`, `DYN_USER_NAME`, `DYN_PASSWORD` | Not tested yet | | External Program | `exec` | `EXEC_PATH` | Not tested yet | | [Exoscale](https://www.exoscale.ch) | `exoscale` | `EXOSCALE_API_KEY`, `EXOSCALE_API_SECRET`, `EXOSCALE_ENDPOINT` | YES | | [Fast DNS](https://www.akamai.com/) | `fastdns` | `AKAMAI_CLIENT_TOKEN`, `AKAMAI_CLIENT_SECRET`, `AKAMAI_ACCESS_TOKEN` | Not tested yet | | [Gandi](https://www.gandi.net) | `gandi` | `GANDI_API_KEY` | Not tested yet | -| [Gandi V5](http://doc.livedns.gandi.net) | `gandiv5` | `GANDIV5_API_KEY` | YES | +| [Gandi v5](http://doc.livedns.gandi.net) | `gandiv5` | `GANDIV5_API_KEY` | YES | | [Glesys](https://glesys.com/) | `glesys` | `GLESYS_API_USER`, `GLESYS_API_KEY`, `GLESYS_DOMAIN` | Not tested yet | | [GoDaddy](https://godaddy.com/domains) | `godaddy` | `GODADDY_API_KEY`, `GODADDY_API_SECRET` | Not tested yet | | [Google Cloud DNS](https://cloud.google.com/dns/docs/) | `gcloud` | `GCE_PROJECT`, `GCE_SERVICE_ACCOUNT_FILE` | YES | @@ -277,6 +278,7 @@ Here is a list of supported `provider`s, that can automate the DNS verification, | [IIJ](https://www.iij.ad.jp/) | `iij` | `IIJ_API_ACCESS_KEY`, `IIJ_API_SECRET_KEY`, `IIJ_DO_SERVICE_CODE` | Not tested yet | | [Lightsail](https://aws.amazon.com/lightsail/) | `lightsail` | `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `DNS_ZONE` | Not tested yet | | [Linode](https://www.linode.com) | `linode` | `LINODE_API_KEY` | Not tested yet | +| [Linode v4](https://www.linode.com) | `linodev4` | `LINODE_TOKEN` | Not tested yet | | manual | - | none, but you need to run Træfik interactively, turn on `acmeLogging` to see instructions and press Enter. | YES | | [Namecheap](https://www.namecheap.com) | `namecheap` | `NAMECHEAP_API_USER`, `NAMECHEAP_API_KEY` | YES | | [name.com](https://www.name.com/) | `namedotcom` | `NAMECOM_USERNAME`, `NAMECOM_API_TOKEN`, `NAMECOM_SERVER` | Not tested yet | @@ -290,6 +292,7 @@ Here is a list of supported `provider`s, that can automate the DNS verification, | [RFC2136](https://tools.ietf.org/html/rfc2136) | `rfc2136` | `RFC2136_TSIG_KEY`, `RFC2136_TSIG_SECRET`, `RFC2136_TSIG_ALGORITHM`, `RFC2136_NAMESERVER` | Not tested yet | | [Route 53](https://aws.amazon.com/route53/) | `route53` | `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `[AWS_REGION]`, `[AWS_HOSTED_ZONE_ID]` or a configured user/instance IAM profile. | YES | | [Sakura Cloud](https://cloud.sakura.ad.jp/) | `sakuracloud` | `SAKURACLOUD_ACCESS_TOKEN`, `SAKURACLOUD_ACCESS_TOKEN_SECRET` | Not tested yet | +| [Stackpath](https://www.stackpath.com/) | `stackpath` | `STACKPATH_CLIENT_ID`, `STACKPATH_CLIENT_SECRET`, `STACKPATH_STACK_ID` | Not tested yet | | [VegaDNS](https://github.com/shupp/VegaDNS-API) | `vegadns` | `SECRET_VEGADNS_KEY`, `SECRET_VEGADNS_SECRET`, `VEGADNS_URL` | Not tested yet | | [VULTR](https://www.vultr.com) | `vultr` | `VULTR_API_KEY` | Not tested yet | diff --git a/docs/configuration/backends/consulcatalog.md b/docs/configuration/backends/consulcatalog.md index 09b201baa..170a089e6 100644 --- a/docs/configuration/backends/consulcatalog.md +++ b/docs/configuration/backends/consulcatalog.md @@ -125,6 +125,7 @@ Additional settings can be defined using Consul Catalog tags. | `.frontend.auth.digest.users=EXPR` | Sets digest authentication to this frontend in CSV format: `User:Realm:Hash,User:Realm:Hash`. | | `.frontend.auth.digest.usersfile=/path/.htdigest` | Sets digest authentication with an external file; if users and usersFile are provided, both are merged, with external file contents having precedence. | | `.frontend.auth.forward.address=https://example.com` | Sets the URL of the authentication server. | +| `.frontend.auth.forward.authResponseHeaders=EXPR` | Sets the forward authentication authResponseHeaders in CSV format: `X-Auth-User,X-Auth-Header` | | `.frontend.auth.forward.tls.ca=/path/ca.pem` | Sets the Certificate Authority (CA) for the TLS connection with the authentication server. | | `.frontend.auth.forward.tls.caOptional=true` | Checks the certificates if present but do not force to be signed by a specified Certificate Authority (CA). | | `.frontend.auth.forward.tls.cert=/path/server.pem` | Sets the Certificate for the TLS connection with the authentication server. | diff --git a/docs/configuration/backends/docker.md b/docs/configuration/backends/docker.md index 5fbb161d5..80555307e 100644 --- a/docs/configuration/backends/docker.md +++ b/docs/configuration/backends/docker.md @@ -57,7 +57,9 @@ watch = true exposedByDefault = true # Use the IP address from the binded port instead of the inner network one. -# For specific use-case :) +# +# In case no IP address is attached to the binded port (or in case +# there is no bind), the inner network one will be used as a fallback. # # Optional # Default: false @@ -213,6 +215,7 @@ Labels can be used on containers to override default behavior. | `traefik.domain` | Sets the default base domain for the frontend rules. For more information, check the [Container Labels section's of the user guide "Let's Encrypt & Docker"](/user-guide/docker-and-lets-encrypt/#container-labels) | | `traefik.enable=false` | Disables this container in Træfik. | | `traefik.port=80` | Registers this port. Useful when the container exposes multiples ports. | +| `traefik.tags=foo,bar,myTag` | Adds Træfik tags to the Docker container/service to be used in [constraints](/configuration/commons/#constraints). | | `traefik.protocol=https` | Overrides the default `http` protocol | | `traefik.weight=10` | Assigns this weight to the container | | `traefik.backend=foo` | Gives the name `foo` to the generated backend for this container. | @@ -244,6 +247,7 @@ Labels can be used on containers to override default behavior. | `traefik.frontend.auth.digest.users=EXPR` | Sets the digest authentication to this frontend in CSV format: `User:Realm:Hash,User:Realm:Hash`. | | `traefik.frontend.auth.digest.usersFile=/path/.htdigest` | Sets the digest authentication with an external file; if users and usersFile are provided, both are merged, with external file contents having precedence. | | `traefik.frontend.auth.forward.address=https://example.com` | Sets the URL of the authentication server. | +| `traefik.frontend.auth.forward.authResponseHeaders=EXPR` | Sets the forward authentication authResponseHeaders in CSV format: `X-Auth-User,X-Auth-Header` | | `traefik.frontend.auth.forward.tls.ca=/path/ca.pem` | Sets the Certificate Authority (CA) for the TLS connection with the authentication server. | | `traefik.frontend.auth.forward.tls.caOptional=true` | Checks the certificates if present but do not force to be signed by a specified Certificate Authority (CA). | | `traefik.frontend.auth.forward.tls.cert=/path/server.pem` | Sets the Certificate for the TLS connection with the authentication server. | @@ -347,6 +351,7 @@ Segment labels override the default behavior. | `traefik..frontend.auth.digest.users=EXPR` | Same as `traefik.frontend.auth.digest.users` | | `traefik..frontend.auth.digest.usersFile=/path/.htdigest` | Same as `traefik.frontend.auth.digest.usersFile` | | `traefik..frontend.auth.forward.address=https://example.com` | Same as `traefik.frontend.auth.forward.address` | +| `traefik..frontend.auth.forward.authResponseHeaders=EXPR` | Same as `traefik.frontend.auth.forward.authResponseHeaders` | | `traefik..frontend.auth.forward.tls.ca=/path/ca.pem` | Same as `traefik.frontend.auth.forward.tls.ca` | | `traefik..frontend.auth.forward.tls.caOptional=true` | Same as `traefik.frontend.auth.forward.tls.caOptional` | | `traefik..frontend.auth.forward.tls.cert=/path/server.pem` | Same as `traefik.frontend.auth.forward.tls.cert` | @@ -428,3 +433,25 @@ Segment labels override the default behavior. When running inside a container, Træfik will need network access through: `docker network connect ` + +## usebindportip + +The default behavior of Træfik is to route requests to the IP/Port of the matching container. +When setting `usebindportip` to true, you tell Træfik to use the IP/Port attached to the container's binding instead of the inner network IP/Port. + +When used in conjunction with the `traefik.port` label (that tells Træfik to route requests to a specific port), Træfik tries to find a binding with `traefik.port` port to select the container. If it can't find such a binding, Træfik falls back on the internal network IP of the container, but still uses the `traefik.port` that is set in the label. + +Below is a recap of the behavior of `usebindportip` in different situations. + +| traefik.port label | Container's binding | Routes to | +|--------------------|----------------------------------------------------|----------------| +| - | - | IntIP:IntPort | +| - | ExtPort:IntPort | IntIP:IntPort | +| - | ExtIp:ExtPort:IntPort | ExtIp:ExtPort | +| LblPort | - | IntIp:LblPort | +| LblPort | ExtIp:ExtPort:LblPort | ExtIp:ExtPort | +| LblPort | ExtIp:ExtPort:OtherPort | IntIp:LblPort | +| LblPort | ExtIp1:ExtPort1:IntPort1 & ExtIp2:LblPort:IntPort2 | ExtIp2:LblPort | + +!!! note + In the above table, ExtIp stands for "external IP found in the binding", IntIp stands for "internal network container's IP", ExtPort stands for "external Port found in the binding", and IntPort stands for "internal network container's port." \ No newline at end of file diff --git a/docs/configuration/backends/ecs.md b/docs/configuration/backends/ecs.md index b5cc87f0c..aa57ce71b 100644 --- a/docs/configuration/backends/ecs.md +++ b/docs/configuration/backends/ecs.md @@ -164,6 +164,7 @@ Labels can be used on task containers to override default behavior: | `traefik.frontend.auth.digest.users=EXPR` | Sets digest authentication to this frontend in CSV format: `User:Realm:Hash,User:Realm:Hash`. | | `traefik.frontend.auth.digest.usersFile=/path/.htdigest` | Sets digest authentication with an external file; if users and usersFile are provided, both are merged, with external file contents having precedence. | | `traefik.frontend.auth.forward.address=https://example.com` | Sets the URL of the authentication server. | +| `traefik.frontend.auth.forward.authResponseHeaders=EXPR` | Sets the forward authentication authResponseHeaders in CSV format: `X-Auth-User,X-Auth-Header` | | `traefik.frontend.auth.forward.tls.ca=/path/ca.pem` | Sets the Certificate Authority (CA) for the TLS connection with the authentication server. | | `traefik.frontend.auth.forward.tls.caOptional=true` | Checks the certificates if present but do not force to be signed by a specified Certificate Authority (CA). | | `traefik.frontend.auth.forward.tls.cert=/path/server.pem` | Sets the Certificate for the TLS connection with the authentication server. | @@ -258,6 +259,7 @@ Segment labels override the default behavior. | `traefik..frontend.auth.digest.users=EXPR` | Same as `traefik.frontend.auth.digest.users` | | `traefik..frontend.auth.digest.usersFile=/path/.htdigest` | Same as `traefik.frontend.auth.digest.usersFile` | | `traefik..frontend.auth.forward.address=https://example.com` | Same as `traefik.frontend.auth.forward.address` | +| `traefik..frontend.auth.forward.authResponseHeaders=EXPR` | Same as `traefik.frontend.auth.forward.authResponseHeaders` | | `traefik..frontend.auth.forward.tls.ca=/path/ca.pem` | Same as `traefik.frontend.auth.forward.tls.ca` | | `traefik..frontend.auth.forward.tls.caOptional=true` | Same as `traefik.frontend.auth.forward.tls.caOptional` | | `traefik..frontend.auth.forward.tls.cert=/path/server.pem` | Same as `traefik.frontend.auth.forward.tls.cert` | diff --git a/docs/configuration/backends/kubernetes.md b/docs/configuration/backends/kubernetes.md index b1ca37196..b2d6ee142 100644 --- a/docs/configuration/backends/kubernetes.md +++ b/docs/configuration/backends/kubernetes.md @@ -170,7 +170,6 @@ The following general annotations are applicable on the Ingress object: | `traefik.ingress.kubernetes.io/service-weights: ` | Set ingress backend weights specified as percentage or decimal numbers in YAML. (5) | | `ingress.kubernetes.io/protocol: ` | Set the protocol Traefik will use to communicate with pods. | - <1> `traefik.ingress.kubernetes.io/error-pages` example: ```yaml @@ -202,15 +201,8 @@ rateset: burst: 18 ``` -<3> `traefik.ingress.kubernetes.io/buffering` example: - -```yaml -maxrequestbodybytes: 10485760 -memrequestbodybytes: 2097153 -maxresponsebodybytes: 10485761 -memresponsebodybytes: 2097152 -retryexpression: IsNetworkError() && Attempts() <= 2 -``` +<3> `traefik.ingress.kubernetes.io/rule-type` +Note: `ReplacePath` is deprecated in this annotation, use the `traefik.ingress.kubernetes.io/request-modifier` annotation instead. Default: `PathPrefix`. <4> `traefik.ingress.kubernetes.io/app-root`: Non-root paths will not be affected by this annotation and handled normally. @@ -259,13 +251,24 @@ The following annotations are applicable on the Service object associated with a | Annotation | Description | |--------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `traefik.ingress.kubernetes.io/buffering: ` | (1) See the [buffering](/configuration/commons/#buffering) section. | | `traefik.ingress.kubernetes.io/affinity: "true"` | Enable backend sticky sessions. | | `traefik.ingress.kubernetes.io/circuit-breaker-expression: ` | Set the circuit breaker expression for the backend. | | `traefik.ingress.kubernetes.io/load-balancer-method: drr` | Override the default `wrr` load balancer algorithm. | -| `traefik.ingress.kubernetes.io/max-conn-amount: "10"` | Set a maximum number of connections to the backend.
Must be used in conjunction with the below label to take effect. | +| `traefik.ingress.kubernetes.io/max-conn-amount: "10"` | Sets the maximum number of simultaneous connections to the backend.
Must be used in conjunction with the label below to take effect. | | `traefik.ingress.kubernetes.io/max-conn-extractor-func: client.ip` | Set the function to be used against the request to determine what to limit maximum connections to the backend by.
Must be used in conjunction with the above label to take effect. | | `traefik.ingress.kubernetes.io/session-cookie-name: ` | Manually set the cookie name for sticky sessions. | +<1> `traefik.ingress.kubernetes.io/buffering` example: + +```yaml +maxrequestbodybytes: 10485760 +memrequestbodybytes: 2097153 +maxresponsebodybytes: 10485761 +memresponsebodybytes: 2097152 +retryexpression: IsNetworkError() && Attempts() <= 2 +``` + !!! note `traefik.ingress.kubernetes.io/` and `ingress.kubernetes.io/` are supported prefixes. diff --git a/docs/configuration/backends/marathon.md b/docs/configuration/backends/marathon.md index 2549963b3..df4e76da0 100644 --- a/docs/configuration/backends/marathon.md +++ b/docs/configuration/backends/marathon.md @@ -228,6 +228,7 @@ The following labels can be defined on Marathon applications. They adjust the be | `traefik.frontend.auth.digest.users=EXPR` | Sets digest authentication to this frontend in CSV format: `User:Realm:Hash,User:Realm:Hash`. | | `traefik.frontend.auth.digest.usersFile=/path/.htdigest` | Sets digest authentication with an external file; if users and usersFile are provided, both are merged, with external file contents having precedence. | | `traefik.frontend.auth.forward.address=https://example.com` | Sets the URL of the authentication server. | +| `traefik.frontend.auth.forward.authResponseHeaders=EXPR` | Sets the forward authentication authResponseHeaders in CSV format: `X-Auth-User,X-Auth-Header` | | `traefik.frontend.auth.forward.tls.ca=/path/ca.pem` | Sets the Certificate Authority (CA) for the TLS connection with the authentication server. | | `traefik.frontend.auth.forward.tls.caOptional=true` | Checks the certificates if present but do not force to be signed by a specified Certificate Authority (CA). | | `traefik.frontend.auth.forward.tls.cert=/path/server.pem` | Sets the Certificate for the TLS connection with the authentication server. | @@ -308,60 +309,61 @@ You can define as many segments as ports exposed in an application. Segment labels override the default behavior. -| Label | Description | -|------------------------------------------------------------------------------ |----------------------------------------------------------------| -| `traefik..backend=BACKEND` | Same as `traefik.backend` | -| `traefik..domain=DOMAIN` | Same as `traefik.domain` | -| `traefik..portIndex=1` | Same as `traefik.portIndex` | -| `traefik..port=PORT` | Same as `traefik.port` | -| `traefik..protocol=http` | Same as `traefik.protocol` | -| `traefik..weight=10` | Same as `traefik.weight` | -| `traefik..frontend.auth.basic=EXPR` | Same as `traefik.frontend.auth.basic` | -| `traefik..frontend.auth.basic.removeHeader=true` | Same as `traefik.frontend.auth.basic.removeHeader` | -| `traefik..frontend.auth.basic.users=EXPR` | Same as `traefik.frontend.auth.basic.users` | -| `traefik..frontend.auth.basic.usersFile=/path/.htpasswd` | Same as `traefik.frontend.auth.basic.usersFile` | -| `traefik..frontend.auth.digest.removeHeader=true` | Same as `traefik.frontend.auth.digest.removeHeader` | -| `traefik..frontend.auth.digest.users=EXPR` | Same as `traefik.frontend.auth.digest.users` | -| `traefik..frontend.auth.digest.usersFile=/path/.htdigest` | Same as `traefik.frontend.auth.digest.usersFile` | -| `traefik..frontend.auth.forward.address=https://example.com` | Same as `traefik.frontend.auth.forward.address` | -| `traefik..frontend.auth.forward.tls.ca=/path/ca.pem` | Same as `traefik.frontend.auth.forward.tls.ca` | -| `traefik..frontend.auth.forward.tls.caOptional=true` | Same as `traefik.frontend.auth.forward.tls.caOptional` | -| `traefik..frontend.auth.forward.tls.cert=/path/server.pem` | Same as `traefik.frontend.auth.forward.tls.cert` | -| `traefik..frontend.auth.forward.tls.insecureSkipVerify=true` | Same as `traefik.frontend.auth.forward.tls.insecureSkipVerify` | -| `traefik..frontend.auth.forward.tls.key=/path/server.key` | Same as `traefik.frontend.auth.forward.tls.key` | -| `traefik..frontend.auth.forward.trustForwardHeader=true` | Same as `traefik.frontend.auth.forward.trustForwardHeader` | -| `traefik..frontend.auth.headerField=X-WebAuth-User` | Same as `traefik.frontend.auth.headerField` | -| `traefik..frontend.auth.removeHeader=true` | Same as `traefik.frontend.auth.removeHeader` | -| `traefik..frontend.entryPoints=https` | Same as `traefik.frontend.entryPoints` | -| `traefik..frontend.errors..backend=NAME` | Same as `traefik.frontend.errors..backend` | -| `traefik..frontend.errors..query=PATH` | Same as `traefik.frontend.errors..query` | -| `traefik..frontend.errors..status=RANGE` | Same as `traefik.frontend.errors..status` | -| `traefik..frontend.passHostHeader=true` | Same as `traefik.frontend.passHostHeader` | -| `traefik..frontend.passTLSClientCert.infos.notAfter=true` | Same as `traefik.frontend.passTLSClientCert.infos.notAfter` | -| `traefik..frontend.passTLSClientCert.infos.notBefore=true` | Same as `traefik.frontend.passTLSClientCert.infos.notBefore` | -| `traefik..frontend.passTLSClientCert.infos.sans=true` | Same as `traefik.frontend.passTLSClientCert.infos.sans` | -| `traefik..frontend.passTLSClientCert.infos.subject.commonName=true` | Same as `traefik.frontend.passTLSClientCert.infos.subject.commonName` | -| `traefik..frontend.passTLSClientCert.infos.subject.country=true` | Same as `traefik.frontend.passTLSClientCert.infos.subject.country` | -| `traefik..frontend.passTLSClientCert.infos.subject.locality=true` | Same as `traefik.frontend.passTLSClientCert.infos.subject.locality` | -| `traefik..frontend.passTLSClientCert.infos.subject.organization=true`| Same as `traefik.frontend.passTLSClientCert.infos.subject.organization`| -| `traefik..frontend.passTLSClientCert.infos.subject.province=true` | Same as `traefik.frontend.passTLSClientCert.infos.subject.province` | -| `traefik..frontend.passTLSClientCert.infos.subject.serialNumber=true`| Same as `traefik.frontend.passTLSClientCert.infos.subject.serialNumber`| -| `traefik..frontend.passTLSClientCert.pem=true` | Same as `traefik.frontend.passTLSClientCert.infos.pem` | -| `traefik..frontend.passTLSCert=true` | Same as `traefik.frontend.passTLSCert` | -| `traefik..frontend.priority=10` | Same as `traefik.frontend.priority` | -| `traefik..frontend.rateLimit.extractorFunc=EXP` | Same as `traefik.frontend.rateLimit.extractorFunc` | -| `traefik..frontend.rateLimit.rateSet..period=6` | Same as `traefik.frontend.rateLimit.rateSet..period` | -| `traefik..frontend.rateLimit.rateSet..average=6` | Same as `traefik.frontend.rateLimit.rateSet..average` | -| `traefik..frontend.rateLimit.rateSet..burst=6` | Same as `traefik.frontend.rateLimit.rateSet..burst` | -| `traefik..frontend.redirect.entryPoint=https` | Same as `traefik.frontend.redirect.entryPoint` | -| `traefik..frontend.redirect.regex=^http://localhost/(.*)` | Same as `traefik.frontend.redirect.regex` | -| `traefik..frontend.redirect.replacement=http://mydomain/$1` | Same as `traefik.frontend.redirect.replacement` | -| `traefik..frontend.redirect.permanent=true` | Same as `traefik.frontend.redirect.permanent` | -| `traefik..frontend.rule=EXP` | Same as `traefik.frontend.rule` | -| `traefik..frontend.whiteList.sourceRange=RANGE` | Same as `traefik.frontend.whiteList.sourceRange` | -| `traefik..frontend.whiteList.ipStrategy=true` | Same as `traefik.frontend.whiteList.ipStrategy` | -| `traefik..frontend.whiteList.ipStrategy.depth=5` | Same as `traefik.frontend.whiteList.ipStrategy.depth` | -| `traefik..frontend.whiteList.ipStrategy.excludedIPs=127.0.0.1` | Same as `traefik.frontend.whiteList.ipStrategy.excludedIPs` | +|| Label | Description | + |------------------------------------------------------------------------------------|------------------------------------------------------------------------| + | `traefik..backend=BACKEND` | Same as `traefik.backend` | + | `traefik..domain=DOMAIN` | Same as `traefik.domain` | + | `traefik..portIndex=1` | Same as `traefik.portIndex` | + | `traefik..port=PORT` | Same as `traefik.port` | + | `traefik..protocol=http` | Same as `traefik.protocol` | + | `traefik..weight=10` | Same as `traefik.weight` | + | `traefik..frontend.auth.basic=EXPR` | Same as `traefik.frontend.auth.basic` | + | `traefik..frontend.auth.basic.removeHeader=true` | Same as `traefik.frontend.auth.basic.removeHeader` | + | `traefik..frontend.auth.basic.users=EXPR` | Same as `traefik.frontend.auth.basic.users` | + | `traefik..frontend.auth.basic.usersFile=/path/.htpasswd` | Same as `traefik.frontend.auth.basic.usersFile` | + | `traefik..frontend.auth.digest.removeHeader=true` | Same as `traefik.frontend.auth.digest.removeHeader` | + | `traefik..frontend.auth.digest.users=EXPR` | Same as `traefik.frontend.auth.digest.users` | + | `traefik..frontend.auth.digest.usersFile=/path/.htdigest` | Same as `traefik.frontend.auth.digest.usersFile` | + | `traefik..frontend.auth.forward.address=https://example.com` | Same as `traefik.frontend.auth.forward.address` | + | `traefik..frontend.auth.forward.authResponseHeaders=EXPR` | Same as `traefik.frontend.auth.forward.authResponseHeaders` | + | `traefik..frontend.auth.forward.tls.ca=/path/ca.pem` | Same as `traefik.frontend.auth.forward.tls.ca` | + | `traefik..frontend.auth.forward.tls.caOptional=true` | Same as `traefik.frontend.auth.forward.tls.caOptional` | + | `traefik..frontend.auth.forward.tls.cert=/path/server.pem` | Same as `traefik.frontend.auth.forward.tls.cert` | + | `traefik..frontend.auth.forward.tls.insecureSkipVerify=true` | Same as `traefik.frontend.auth.forward.tls.insecureSkipVerify` | + | `traefik..frontend.auth.forward.tls.key=/path/server.key` | Same as `traefik.frontend.auth.forward.tls.key` | + | `traefik..frontend.auth.forward.trustForwardHeader=true` | Same as `traefik.frontend.auth.forward.trustForwardHeader` | + | `traefik..frontend.auth.headerField=X-WebAuth-User` | Same as `traefik.frontend.auth.headerField` | + | `traefik..frontend.auth.removeHeader=true` | Same as `traefik.frontend.auth.removeHeader` | + | `traefik..frontend.entryPoints=https` | Same as `traefik.frontend.entryPoints` | + | `traefik..frontend.errors..backend=NAME` | Same as `traefik.frontend.errors..backend` | + | `traefik..frontend.errors..query=PATH` | Same as `traefik.frontend.errors..query` | + | `traefik..frontend.errors..status=RANGE` | Same as `traefik.frontend.errors..status` | + | `traefik..frontend.passHostHeader=true` | Same as `traefik.frontend.passHostHeader` | + | `traefik..frontend.passTLSClientCert.infos.notAfter=true` | Same as `traefik.frontend.passTLSClientCert.infos.notAfter` | + | `traefik..frontend.passTLSClientCert.infos.notBefore=true` | Same as `traefik.frontend.passTLSClientCert.infos.notBefore` | + | `traefik..frontend.passTLSClientCert.infos.sans=true` | Same as `traefik.frontend.passTLSClientCert.infos.sans` | + | `traefik..frontend.passTLSClientCert.infos.subject.commonName=true` | Same as `traefik.frontend.passTLSClientCert.infos.subject.commonName` | + | `traefik..frontend.passTLSClientCert.infos.subject.country=true` | Same as `traefik.frontend.passTLSClientCert.infos.subject.country` | + | `traefik..frontend.passTLSClientCert.infos.subject.locality=true` | Same as `traefik.frontend.passTLSClientCert.infos.subject.locality` | + | `traefik..frontend.passTLSClientCert.infos.subject.organization=true`| Same as `traefik.frontend.passTLSClientCert.infos.subject.organization`| + | `traefik..frontend.passTLSClientCert.infos.subject.province=true` | Same as `traefik.frontend.passTLSClientCert.infos.subject.province` | + | `traefik..frontend.passTLSClientCert.infos.subject.serialNumber=true`| Same as `traefik.frontend.passTLSClientCert.infos.subject.serialNumber`| + | `traefik..frontend.passTLSClientCert.pem=true` | Same as `traefik.frontend.passTLSClientCert.infos.pem` | + | `traefik..frontend.passTLSCert=true` | Same as `traefik.frontend.passTLSCert` | + | `traefik..frontend.priority=10` | Same as `traefik.frontend.priority` | + | `traefik..frontend.rateLimit.extractorFunc=EXP` | Same as `traefik.frontend.rateLimit.extractorFunc` | + | `traefik..frontend.rateLimit.rateSet..period=6` | Same as `traefik.frontend.rateLimit.rateSet..period` | + | `traefik..frontend.rateLimit.rateSet..average=6` | Same as `traefik.frontend.rateLimit.rateSet..average` | + | `traefik..frontend.rateLimit.rateSet..burst=6` | Same as `traefik.frontend.rateLimit.rateSet..burst` | + | `traefik..frontend.redirect.entryPoint=https` | Same as `traefik.frontend.redirect.entryPoint` | + | `traefik..frontend.redirect.regex=^http://localhost/(.*)` | Same as `traefik.frontend.redirect.regex` | + | `traefik..frontend.redirect.replacement=http://mydomain/$1` | Same as `traefik.frontend.redirect.replacement` | + | `traefik..frontend.redirect.permanent=true` | Same as `traefik.frontend.redirect.permanent` | + | `traefik..frontend.rule=EXP` | Same as `traefik.frontend.rule` | + | `traefik..frontend.whiteList.sourceRange=RANGE` | Same as `traefik.frontend.whiteList.sourceRange` | + | `traefik..frontend.whiteList.ipStrategy=true` | Same as `traefik.frontend.whiteList.ipStrategy` | + | `traefik..frontend.whiteList.ipStrategy.depth=5` | Same as `traefik.frontend.whiteList.ipStrategy.depth` | + | `traefik..frontend.whiteList.ipStrategy.excludedIPs=127.0.0.1` | Same as `traefik.frontend.whiteList.ipStrategy.excludedIPs` | #### Custom Headers diff --git a/docs/configuration/backends/mesos.md b/docs/configuration/backends/mesos.md index 1939b30e9..86ae4ccd1 100644 --- a/docs/configuration/backends/mesos.md +++ b/docs/configuration/backends/mesos.md @@ -106,55 +106,56 @@ domain = "mesos.localhost" The following labels can be defined on Mesos tasks. They adjust the behavior for the entire application. -| Label | Description | -|-----------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| `traefik.domain` | Sets the default domain for the frontend rules. | -| `traefik.enable=false` | Disables this container in Træfik. | -| `traefik.port=80` | Registers this port. Useful when the application exposes multiple ports. | -| `traefik.portName=web` | Registers port by name in the application's ports array. Useful when the application exposes multiple ports. | -| `traefik.portIndex=1` | Registers port by index in the application's ports array. Useful when the application exposes multiple ports. | -| `traefik.protocol=https` | Overrides the default `http` protocol | -| `traefik.weight=10` | Assigns this weight to the container | -| `traefik.backend=foo` | Gives the name `foo` to the generated backend for this container. | -| `traefik.backend.buffering.maxRequestBodyBytes=0` | See [buffering](/configuration/commons/#buffering) section. | -| `traefik.backend.buffering.maxResponseBodyBytes=0` | See [buffering](/configuration/commons/#buffering) section. | -| `traefik.backend.buffering.memRequestBodyBytes=0` | See [buffering](/configuration/commons/#buffering) section. | -| `traefik.backend.buffering.memResponseBodyBytes=0` | See [buffering](/configuration/commons/#buffering) section. | -| `traefik.backend.buffering.retryExpression=EXPR` | See [buffering](/configuration/commons/#buffering) section. | -| `traefik.backend.circuitbreaker.expression=EXPR` | Creates a [circuit breaker](/basics/#backends) to be used against the backend | -| `traefik.backend.healthcheck.path=/health` | Enables health check for the backend, hitting the container at `path`. | -| `traefik.backend.healthcheck.interval=5s` | Defines the health check interval. (Default: 30s) | -| `traefik.backend.healthcheck.timeout=3s` | Defines the health check request timeout. (Default: 5s) | -| `traefik.backend.healthcheck.scheme=http` | Overrides the server URL scheme. | -| `traefik.backend.healthcheck.port=8080` | Sets a different port for the health check. | -| `traefik.backend.healthcheck.hostname=foobar.com` | Defines the health check hostname. | -| `traefik.backend.healthcheck.headers=EXPR` | Defines the health check request headers
Format: HEADER:value||HEADER2:value2 | -| `traefik.backend.loadbalancer.method=drr` | Overrides the default `wrr` load balancer algorithm | -| `traefik.backend.loadbalancer.stickiness=true` | Enables backend sticky sessions | -| `traefik.backend.loadbalancer.stickiness.cookieName=NAME` | Sets the cookie manually name for sticky sessions | -| `traefik.backend.maxconn.amount=10` | Sets a maximum number of connections to the backend.
Must be used in conjunction with the below label to take effect. | -| `traefik.backend.maxconn.extractorfunc=client.ip` | Sets the function to be used against the request to determine what to limit maximum connections to the backend by.
Must be used in conjunction with the above label to take effect. | -| `traefik.frontend.auth.basic=EXPR` | Sets basic authentication to this frontend in CSV format: `User:Hash,User:Hash` (DEPRECATED). | -| `traefik.frontend.auth.basic.users=EXPR` | Sets basic authentication to this frontend in CSV format: `User:Hash,User:Hash`. | -| `traefik.frontend.auth.basic.removeHeader=true` | If set to `true`, removes the `Authorization` header. | -| `traefik.frontend.auth.basic.usersFile=/path/.htpasswd` | Sets basic authentication with an external file; if users and usersFile are provided, both are merged, with external file contents having precedence. | -| `traefik.frontend.auth.digest.removeHeader=true` | If set to `true`, removes the `Authorization` header. | -| `traefik.frontend.auth.digest.users=EXPR` | Sets digest authentication to this frontend in CSV format: `User:Realm:Hash,User:Realm:Hash`. | -| `traefik.frontend.auth.digest.usersFile=/path/.htdigest` | Sets digest authentication with an external file; if users and usersFile are provided, both are merged, with external file contents having precedence. | -| `traefik.frontend.auth.forward.address=https://example.com` | Sets the URL of the authentication server. | -| `traefik.frontend.auth.forward.tls.ca=/path/ca.pem` | Sets the Certificate Authority (CA) for the TLS connection with the authentication server. | -| `traefik.frontend.auth.forward.tls.caOptional=true` | Checks the certificates if present but do not force to be signed by a specified Certificate Authority (CA). | -| `traefik.frontend.auth.forward.tls.cert=/path/server.pem` | Sets the Certificate for the TLS connection with the authentication server. | -| `traefik.frontend.auth.forward.tls.insecureSkipVerify=true` | If set to true invalid SSL certificates are accepted. | -| `traefik.frontend.auth.forward.tls.key=/path/server.key` | Sets the Certificate for the TLS connection with the authentication server. | -| `traefik.frontend.auth.forward.trustForwardHeader=true` | Trusts X-Forwarded-* headers. | -| `traefik.frontend.auth.headerField=X-WebAuth-User` | Sets the header used to pass the authenticated user to the application. | -| `traefik.frontend.auth.removeHeader=true` | If set to true, removes the Authorization header. | -| `traefik.frontend.entryPoints=http,https` | Assigns this frontend to entry points `http` and `https`.
Overrides `defaultEntryPoints` | -| `traefik.frontend.errors..backend=NAME` | See [custom error pages](/configuration/commons/#custom-error-pages) section. | -| `traefik.frontend.errors..query=PATH` | See [custom error pages](/configuration/commons/#custom-error-pages) section. | -| `traefik.frontend.errors..status=RANGE` | See [custom error pages](/configuration/commons/#custom-error-pages) section. | -| `traefik.frontend.passHostHeader=true` | Forwards client `Host` header to the backend. | +| Label | Description | +|---------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `traefik.domain` | Sets the default domain for the frontend rules. | +| `traefik.enable=false` | Disables this container in Træfik. | +| `traefik.port=80` | Registers this port. Useful when the application exposes multiple ports. | +| `traefik.portName=web` | Registers port by name in the application's ports array. Useful when the application exposes multiple ports. | +| `traefik.portIndex=1` | Registers port by index in the application's ports array. Useful when the application exposes multiple ports. | +| `traefik.protocol=https` | Overrides the default `http` protocol | +| `traefik.weight=10` | Assigns this weight to the container | +| `traefik.backend=foo` | Gives the name `foo` to the generated backend for this container. | +| `traefik.backend.buffering.maxRequestBodyBytes=0` | See [buffering](/configuration/commons/#buffering) section. | +| `traefik.backend.buffering.maxResponseBodyBytes=0` | See [buffering](/configuration/commons/#buffering) section. | +| `traefik.backend.buffering.memRequestBodyBytes=0` | See [buffering](/configuration/commons/#buffering) section. | +| `traefik.backend.buffering.memResponseBodyBytes=0` | See [buffering](/configuration/commons/#buffering) section. | +| `traefik.backend.buffering.retryExpression=EXPR` | See [buffering](/configuration/commons/#buffering) section. | +| `traefik.backend.circuitbreaker.expression=EXPR` | Creates a [circuit breaker](/basics/#backends) to be used against the backend | +| `traefik.backend.healthcheck.path=/health` | Enables health check for the backend, hitting the container at `path`. | +| `traefik.backend.healthcheck.interval=5s` | Defines the health check interval. (Default: 30s) | +| `traefik.backend.healthcheck.timeout=3s` | Defines the health check request timeout. (Default: 5s) | +| `traefik.backend.healthcheck.scheme=http` | Overrides the server URL scheme. | +| `traefik.backend.healthcheck.port=8080` | Sets a different port for the health check. | +| `traefik.backend.healthcheck.hostname=foobar.com` | Defines the health check hostname. | +| `traefik.backend.healthcheck.headers=EXPR` | Defines the health check request headers
Format: HEADER:value||HEADER2:value2 | +| `traefik.backend.loadbalancer.method=drr` | Overrides the default `wrr` load balancer algorithm | +| `traefik.backend.loadbalancer.stickiness=true` | Enables backend sticky sessions | +| `traefik.backend.loadbalancer.stickiness.cookieName=NAME` | Sets the cookie manually name for sticky sessions | +| `traefik.backend.maxconn.amount=10` | Sets a maximum number of connections to the backend.
Must be used in conjunction with the below label to take effect. | +| `traefik.backend.maxconn.extractorfunc=client.ip` | Sets the function to be used against the request to determine what to limit maximum connections to the backend by.
Must be used in conjunction with the above label to take effect. | +| `traefik.frontend.auth.basic=EXPR` | Sets basic authentication to this frontend in CSV format: `User:Hash,User:Hash` (DEPRECATED). | +| `traefik.frontend.auth.basic.users=EXPR` | Sets basic authentication to this frontend in CSV format: `User:Hash,User:Hash`. | +| `traefik.frontend.auth.basic.removeHeader=true` | If set to `true`, removes the `Authorization` header. | +| `traefik.frontend.auth.basic.usersFile=/path/.htpasswd` | Sets basic authentication with an external file; if users and usersFile are provided, both are merged, with external file contents having precedence. | +| `traefik.frontend.auth.digest.removeHeader=true` | If set to `true`, removes the `Authorization` header. | +| `traefik.frontend.auth.digest.users=EXPR` | Sets digest authentication to this frontend in CSV format: `User:Realm:Hash,User:Realm:Hash`. | +| `traefik.frontend.auth.digest.usersFile=/path/.htdigest` | Sets digest authentication with an external file; if users and usersFile are provided, both are merged, with external file contents having precedence. | +| `traefik.frontend.auth.forward.address=https://example.com` | Sets the URL of the authentication server. | +| `traefik.frontend.auth.forward.authResponseHeaders=EXPR` | Sets the forward authentication authResponseHeaders in CSV format: `X-Auth-User,X-Auth-Header` | +| `traefik.frontend.auth.forward.tls.ca=/path/ca.pem` | Sets the Certificate Authority (CA) for the TLS connection with the authentication server. | +| `traefik.frontend.auth.forward.tls.caOptional=true` | Checks the certificates if present but do not force to be signed by a specified Certificate Authority (CA). | +| `traefik.frontend.auth.forward.tls.cert=/path/server.pem` | Sets the Certificate for the TLS connection with the authentication server. | +| `traefik.frontend.auth.forward.tls.insecureSkipVerify=true` | If set to true invalid SSL certificates are accepted. | +| `traefik.frontend.auth.forward.tls.key=/path/server.key` | Sets the Certificate for the TLS connection with the authentication server. | +| `traefik.frontend.auth.forward.trustForwardHeader=true` | Trusts X-Forwarded-* headers. | +| `traefik.frontend.auth.headerField=X-WebAuth-User` | Sets the header used to pass the authenticated user to the application. | +| `traefik.frontend.auth.removeHeader=true` | If set to true, removes the Authorization header. | +| `traefik.frontend.entryPoints=http,https` | Assigns this frontend to entry points `http` and `https`.
Overrides `defaultEntryPoints` | +| `traefik.frontend.errors..backend=NAME` | See [custom error pages](/configuration/commons/#custom-error-pages) section. | +| `traefik.frontend.errors..query=PATH` | See [custom error pages](/configuration/commons/#custom-error-pages) section. | +| `traefik.frontend.errors..status=RANGE` | See [custom error pages](/configuration/commons/#custom-error-pages) section. | +| `traefik.frontend.passHostHeader=true` | Forwards client `Host` header to the backend. | | `traefik.frontend.passTLSClientCert.infos.notAfter=true` | Add the noAfter field in a escaped client infos in the `X-Forwarded-Ssl-Client-Cert-Infos` header. | | `traefik.frontend.passTLSClientCert.infos.notBefore=true` | Add the noBefore field in a escaped client infos in the `X-Forwarded-Ssl-Client-Cert-Infos` header. | | `traefik.frontend.passTLSClientCert.infos.sans=true` | Add the sans field in a escaped client infos in the `X-Forwarded-Ssl-Client-Cert-Infos` header. | @@ -165,21 +166,21 @@ The following labels can be defined on Mesos tasks. They adjust the behavior for | `traefik.frontend.passTLSClientCert.infos.subject.province=true` | Add the subject.province field in a escaped client infos in the `X-Forwarded-Ssl-Client-Cert-Infos` header. | | `traefik.frontend.passTLSClientCert.infos.subject.serialNumber=true`| Add the subject.serialNumber field in a escaped client infos in the `X-Forwarded-Ssl-Client-Cert-Infos` header. | | `traefik.frontend.passTLSClientCert.pem=true` | Pass the escaped pem in the `X-Forwarded-Ssl-Client-Cert` header. | -| `traefik.frontend.passTLSCert=true` | Forwards TLS Client certificates to the backend. | -| `traefik.frontend.priority=10` | Overrides default frontend priority | -| `traefik.frontend.rateLimit.extractorFunc=EXP` | See [rate limiting](/configuration/commons/#rate-limiting) section. | -| `traefik.frontend.rateLimit.rateSet..period=6` | See [rate limiting](/configuration/commons/#rate-limiting) section. | -| `traefik.frontend.rateLimit.rateSet..average=6` | See [rate limiting](/configuration/commons/#rate-limiting) section. | -| `traefik.frontend.rateLimit.rateSet..burst=6` | See [rate limiting](/configuration/commons/#rate-limiting) section. | -| `traefik.frontend.redirect.entryPoint=https` | Enables Redirect to another entryPoint to this frontend (e.g. HTTPS) | -| `traefik.frontend.redirect.regex=^http://localhost/(.*)` | Redirects to another URL to this frontend.
Must be set with `traefik.frontend.redirect.replacement`. | -| `traefik.frontend.redirect.replacement=http://mydomain/$1` | Redirects to another URL to this frontend.
Must be set with `traefik.frontend.redirect.regex`. | -| `traefik.frontend.redirect.permanent=true` | Returns 301 instead of 302. | -| `traefik.frontend.rule=EXPR` | Overrides the default frontend rule. Default: `Host:{discovery_name}.{domain}`. | -| `traefik.frontend.whiteList.sourceRange=RANGE` | Sets a list of IP-Ranges which are allowed to access.
An unset or empty list allows all Source-IPs to access. If one of the Net-Specifications are invalid, the whole list is invalid and allows all Source-IPs to access. | -| `traefik.frontend.whiteList.ipStrategy=true` | Uses the default IPStrategy.
Can be used when there is an existing `clientIPStrategy` but you want the remote address for whitelisting. | -| `traefik.frontend.whiteList.ipStrategy.depth=5` | See [whitelist](/configuration/entrypoints/#white-listing) | -| `traefik.frontend.whiteList.ipStrategy.excludedIPs=127.0.0.1` | See [whitelist](/configuration/entrypoints/#white-listing) | +| `traefik.frontend.passTLSCert=true` | Forwards TLS Client certificates to the backend. | +| `traefik.frontend.priority=10` | Overrides default frontend priority | +| `traefik.frontend.rateLimit.extractorFunc=EXP` | See [rate limiting](/configuration/commons/#rate-limiting) section. | +| `traefik.frontend.rateLimit.rateSet..period=6` | See [rate limiting](/configuration/commons/#rate-limiting) section. | +| `traefik.frontend.rateLimit.rateSet..average=6` | See [rate limiting](/configuration/commons/#rate-limiting) section. | +| `traefik.frontend.rateLimit.rateSet..burst=6` | See [rate limiting](/configuration/commons/#rate-limiting) section. | +| `traefik.frontend.redirect.entryPoint=https` | Enables Redirect to another entryPoint to this frontend (e.g. HTTPS) | +| `traefik.frontend.redirect.regex=^http://localhost/(.*)` | Redirects to another URL to this frontend.
Must be set with `traefik.frontend.redirect.replacement`. | +| `traefik.frontend.redirect.replacement=http://mydomain/$1` | Redirects to another URL to this frontend.
Must be set with `traefik.frontend.redirect.regex`. | +| `traefik.frontend.redirect.permanent=true` | Returns 301 instead of 302. | +| `traefik.frontend.rule=EXPR` | Overrides the default frontend rule. Default: `Host:{discovery_name}.{domain}`. | +| `traefik.frontend.whiteList.sourceRange=RANGE` | Sets a list of IP-Ranges which are allowed to access.
An unset or empty list allows all Source-IPs to access. If one of the Net-Specifications are invalid, the whole list is invalid and allows all Source-IPs to access. | +| `traefik.frontend.whiteList.ipStrategy=true` | Uses the default IPStrategy.
Can be used when there is an existing `clientIPStrategy` but you want the remote address for whitelisting. | +| `traefik.frontend.whiteList.ipStrategy.depth=5` | See [whitelist](/configuration/entrypoints/#white-listing) | +| `traefik.frontend.whiteList.ipStrategy.excludedIPs=127.0.0.1` | See [whitelist](/configuration/entrypoints/#white-listing) | ### Custom Headers @@ -223,36 +224,37 @@ Additionally, if a segment name matches a named port, that port will be used unl Segment labels override the default behavior. -| Label | Description | -|------------------------------------------------------------------------------|----------------------------------------------------------------| -| `traefik..backend=BACKEND` | Same as `traefik.backend` | -| `traefik..domain=DOMAIN` | Same as `traefik.domain` | -| `traefik..portIndex=1` | Same as `traefik.portIndex` | -| `traefik..portName=web` | Same as `traefik.portName` | -| `traefik..port=PORT` | Same as `traefik.port` | -| `traefik..protocol=http` | Same as `traefik.protocol` | -| `traefik..weight=10` | Same as `traefik.weight` | -| `traefik..frontend.auth.basic=EXPR` | Same as `traefik.frontend.auth.basic` | -| `traefik..frontend.auth.basic.removeHeader=true` | Same as `traefik.frontend.auth.basic.removeHeader` | -| `traefik..frontend.auth.basic.users=EXPR` | Same as `traefik.frontend.auth.basic.users` | -| `traefik..frontend.auth.basic.usersFile=/path/.htpasswd` | Same as `traefik.frontend.auth.basic.usersFile` | -| `traefik..frontend.auth.digest.removeHeader=true` | Same as `traefik.frontend.auth.digest.removeHeader` | -| `traefik..frontend.auth.digest.users=EXPR` | Same as `traefik.frontend.auth.digest.users` | -| `traefik..frontend.auth.digest.usersFile=/path/.htdigest` | Same as `traefik.frontend.auth.digest.usersFile` | -| `traefik..frontend.auth.forward.address=https://example.com` | Same as `traefik.frontend.auth.forward.address` | -| `traefik..frontend.auth.forward.tls.ca=/path/ca.pem` | Same as `traefik.frontend.auth.forward.tls.ca` | -| `traefik..frontend.auth.forward.tls.caOptional=true` | Same as `traefik.frontend.auth.forward.tls.caOptional` | -| `traefik..frontend.auth.forward.tls.cert=/path/server.pem` | Same as `traefik.frontend.auth.forward.tls.cert` | -| `traefik..frontend.auth.forward.tls.insecureSkipVerify=true` | Same as `traefik.frontend.auth.forward.tls.insecureSkipVerify` | -| `traefik..frontend.auth.forward.tls.key=/path/server.key` | Same as `traefik.frontend.auth.forward.tls.key` | -| `traefik..frontend.auth.forward.trustForwardHeader=true` | Same as `traefik.frontend.auth.forward.trustForwardHeader` | -| `traefik..frontend.auth.headerField=X-WebAuth-User` | Same as `traefik.frontend.auth.headerField` | -| `traefik..frontend.auth.removeHeader=true` | Same as `traefik.frontend.auth.removeHeader` | -| `traefik..frontend.entryPoints=https` | Same as `traefik.frontend.entryPoints` | -| `traefik..frontend.errors..backend=NAME` | Same as `traefik.frontend.errors..backend` | -| `traefik..frontend.errors..query=PATH` | Same as `traefik.frontend.errors..query` | -| `traefik..frontend.errors..status=RANGE` | Same as `traefik.frontend.errors..status` | -| `traefik..frontend.passHostHeader=true` | Same as `traefik.frontend.passHostHeader` | +| Label | Description | +|------------------------------------------------------------------------------------|------------------------------------------------------------------------| +| `traefik..backend=BACKEND` | Same as `traefik.backend` | +| `traefik..domain=DOMAIN` | Same as `traefik.domain` | +| `traefik..portIndex=1` | Same as `traefik.portIndex` | +| `traefik..portName=web` | Same as `traefik.portName` | +| `traefik..port=PORT` | Same as `traefik.port` | +| `traefik..protocol=http` | Same as `traefik.protocol` | +| `traefik..weight=10` | Same as `traefik.weight` | +| `traefik..frontend.auth.basic=EXPR` | Same as `traefik.frontend.auth.basic` | +| `traefik..frontend.auth.basic.removeHeader=true` | Same as `traefik.frontend.auth.basic.removeHeader` | +| `traefik..frontend.auth.basic.users=EXPR` | Same as `traefik.frontend.auth.basic.users` | +| `traefik..frontend.auth.basic.usersFile=/path/.htpasswd` | Same as `traefik.frontend.auth.basic.usersFile` | +| `traefik..frontend.auth.digest.removeHeader=true` | Same as `traefik.frontend.auth.digest.removeHeader` | +| `traefik..frontend.auth.digest.users=EXPR` | Same as `traefik.frontend.auth.digest.users` | +| `traefik..frontend.auth.digest.usersFile=/path/.htdigest` | Same as `traefik.frontend.auth.digest.usersFile` | +| `traefik..frontend.auth.forward.address=https://example.com` | Same as `traefik.frontend.auth.forward.address` | +| `traefik..frontend.auth.forward.authResponseHeaders=EXPR` | Same as `traefik.frontend.auth.forward.authResponseHeaders` | +| `traefik..frontend.auth.forward.tls.ca=/path/ca.pem` | Same as `traefik.frontend.auth.forward.tls.ca` | +| `traefik..frontend.auth.forward.tls.caOptional=true` | Same as `traefik.frontend.auth.forward.tls.caOptional` | +| `traefik..frontend.auth.forward.tls.cert=/path/server.pem` | Same as `traefik.frontend.auth.forward.tls.cert` | +| `traefik..frontend.auth.forward.tls.insecureSkipVerify=true` | Same as `traefik.frontend.auth.forward.tls.insecureSkipVerify` | +| `traefik..frontend.auth.forward.tls.key=/path/server.key` | Same as `traefik.frontend.auth.forward.tls.key` | +| `traefik..frontend.auth.forward.trustForwardHeader=true` | Same as `traefik.frontend.auth.forward.trustForwardHeader` | +| `traefik..frontend.auth.headerField=X-WebAuth-User` | Same as `traefik.frontend.auth.headerField` | +| `traefik..frontend.auth.removeHeader=true` | Same as `traefik.frontend.auth.removeHeader` | +| `traefik..frontend.entryPoints=https` | Same as `traefik.frontend.entryPoints` | +| `traefik..frontend.errors..backend=NAME` | Same as `traefik.frontend.errors..backend` | +| `traefik..frontend.errors..query=PATH` | Same as `traefik.frontend.errors..query` | +| `traefik..frontend.errors..status=RANGE` | Same as `traefik.frontend.errors..status` | +| `traefik..frontend.passHostHeader=true` | Same as `traefik.frontend.passHostHeader` | | `traefik..frontend.passTLSClientCert.infos.notAfter=true` | Same as `traefik.frontend.passTLSClientCert.infos.notAfter` | | `traefik..frontend.passTLSClientCert.infos.notBefore=true` | Same as `traefik.frontend.passTLSClientCert.infos.notBefore` | | `traefik..frontend.passTLSClientCert.infos.sans=true` | Same as `traefik.frontend.passTLSClientCert.infos.sans` | @@ -263,21 +265,21 @@ Segment labels override the default behavior. | `traefik..frontend.passTLSClientCert.infos.subject.province=true` | Same as `traefik.frontend.passTLSClientCert.infos.subject.province` | | `traefik..frontend.passTLSClientCert.infos.subject.serialNumber=true`| Same as `traefik.frontend.passTLSClientCert.infos.subject.serialNumber`| | `traefik..frontend.passTLSClientCert.pem=true` | Same as `traefik.frontend.passTLSClientCert.infos.pem` | -| `traefik..frontend.passTLSCert=true` | Same as `traefik.frontend.passTLSCert` | -| `traefik..frontend.priority=10` | Same as `traefik.frontend.priority` | -| `traefik..frontend.rateLimit.extractorFunc=EXP` | Same as `traefik.frontend.rateLimit.extractorFunc` | -| `traefik..frontend.rateLimit.rateSet..period=6` | Same as `traefik.frontend.rateLimit.rateSet..period` | -| `traefik..frontend.rateLimit.rateSet..average=6` | Same as `traefik.frontend.rateLimit.rateSet..average` | -| `traefik..frontend.rateLimit.rateSet..burst=6` | Same as `traefik.frontend.rateLimit.rateSet..burst` | -| `traefik..frontend.redirect.entryPoint=https` | Same as `traefik.frontend.redirect.entryPoint` | -| `traefik..frontend.redirect.regex=^http://localhost/(.*)` | Same as `traefik.frontend.redirect.regex` | -| `traefik..frontend.redirect.replacement=http://mydomain/$1` | Same as `traefik.frontend.redirect.replacement` | -| `traefik..frontend.redirect.permanent=true` | Same as `traefik.frontend.redirect.permanent` | -| `traefik..frontend.rule=EXP` | Same as `traefik.frontend.rule` | -| `traefik..frontend.whiteList.sourceRange=RANGE` | Same as `traefik.frontend.whiteList.sourceRange` | -| `traefik..frontend.whiteList.ipStrategy=true` | Same as `traefik.frontend.whiteList.ipStrategy` | -| `traefik..frontend.whiteList.ipStrategy.depth=5` | Same as `traefik.frontend.whiteList.ipStrategy.depth` | -| `traefik..frontend.whiteList.ipStrategy.excludedIPs=127.0.0.1` | Same as `traefik.frontend.whiteList.ipStrategy.excludedIPs` | +| `traefik..frontend.passTLSCert=true` | Same as `traefik.frontend.passTLSCert` | +| `traefik..frontend.priority=10` | Same as `traefik.frontend.priority` | +| `traefik..frontend.rateLimit.extractorFunc=EXP` | Same as `traefik.frontend.rateLimit.extractorFunc` | +| `traefik..frontend.rateLimit.rateSet..period=6` | Same as `traefik.frontend.rateLimit.rateSet..period` | +| `traefik..frontend.rateLimit.rateSet..average=6` | Same as `traefik.frontend.rateLimit.rateSet..average` | +| `traefik..frontend.rateLimit.rateSet..burst=6` | Same as `traefik.frontend.rateLimit.rateSet..burst` | +| `traefik..frontend.redirect.entryPoint=https` | Same as `traefik.frontend.redirect.entryPoint` | +| `traefik..frontend.redirect.regex=^http://localhost/(.*)` | Same as `traefik.frontend.redirect.regex` | +| `traefik..frontend.redirect.replacement=http://mydomain/$1` | Same as `traefik.frontend.redirect.replacement` | +| `traefik..frontend.redirect.permanent=true` | Same as `traefik.frontend.redirect.permanent` | +| `traefik..frontend.rule=EXP` | Same as `traefik.frontend.rule` | +| `traefik..frontend.whiteList.sourceRange=RANGE` | Same as `traefik.frontend.whiteList.sourceRange` | +| `traefik..frontend.whiteList.ipStrategy=true` | Same as `traefik.frontend.whiteList.ipStrategy` | +| `traefik..frontend.whiteList.ipStrategy.depth=5` | Same as `traefik.frontend.whiteList.ipStrategy.depth` | +| `traefik..frontend.whiteList.ipStrategy.excludedIPs=127.0.0.1` | Same as `traefik.frontend.whiteList.ipStrategy.excludedIPs` | #### Custom Headers diff --git a/docs/configuration/backends/rancher.md b/docs/configuration/backends/rancher.md index f9aeb2b2e..634f12914 100644 --- a/docs/configuration/backends/rancher.md +++ b/docs/configuration/backends/rancher.md @@ -172,6 +172,7 @@ Labels can be used on task containers to override default behavior: | `traefik.frontend.auth.digest.users=EXPR` | Sets the digest authentication to this frontend in CSV format: `User:Realm:Hash,User:Realm:Hash`. | | `traefik.frontend.auth.digest.usersFile=/path/.htdigest` | Sets the digest authentication with an external file; if users and usersFile are provided, both are merged, with external file contents having precedence. | | `traefik.frontend.auth.forward.address=https://example.com` | Sets the URL of the authentication server. | +| `traefik.frontend.auth.forward.authResponseHeaders=EXPR` | Sets the forward authentication authResponseHeaders in CSV format: `X-Auth-User,X-Auth-Header` | | `traefik.frontend.auth.forward.tls.ca=/path/ca.pem` | Sets the Certificate Authority (CA) for the TLS connection with the authentication server. | | `traefik.frontend.auth.forward.tls.caOptional=true` | Checks the certificates if present but do not force to be signed by a specified Certificate Authority (CA). | | `traefik.frontend.auth.forward.tls.cert=/path/server.pem` | Sets the Certificate for the TLS connection with the authentication server. | @@ -265,6 +266,7 @@ Segment labels override the default behavior. | `traefik..frontend.auth.digest.users=EXPR` | Same as `traefik.frontend.auth.digest.users` | | `traefik..frontend.auth.digest.usersFile=/path/.htdigest` | Same as `traefik.frontend.auth.digest.usersFile` | | `traefik..frontend.auth.forward.address=https://example.com` | Same as `traefik.frontend.auth.forward.address` | +| `traefik..frontend.auth.forward.authResponseHeaders=EXPR` | Same as `traefik.frontend.auth.forward.authResponseHeaders` | | `traefik..frontend.auth.forward.tls.ca=/path/ca.pem` | Same as `traefik.frontend.auth.forward.tls.ca` | | `traefik..frontend.auth.forward.tls.caOptional=true` | Same as `traefik.frontend.auth.forward.tls.caOptional` | | `traefik..frontend.auth.forward.tls.cert=/path/server.pem` | Same as `traefik.frontend.auth.forward.tls.cert` | diff --git a/docs/index.md b/docs/index.md index 50c180572..a24a20dd2 100644 --- a/docs/index.md +++ b/docs/index.md @@ -87,7 +87,7 @@ services: ``` !!! warning - Enabling the Web UI with the `--api` flag might exposes configuration elements. You can read more about this on the [API/Dashboard's Security section](/configuration/api#security). + Enabling the Web UI with the `--api` flag might expose configuration elements. You can read more about this on the [API/Dashboard's Security section](/configuration/api#security). **That's it. Now you can launch Træfik!** @@ -218,4 +218,4 @@ Reported vulnerabilities can be found on ### Report a Vulnerability We want to keep Træfik safe for everyone. -If you've discovered a security vulnerability in Træfik, we appreciate your help in disclosing it to us in a responsible manner, using [this form](https://security.traefik.io). \ No newline at end of file +If you've discovered a security vulnerability in Træfik, we appreciate your help in disclosing it to us in a responsible manner, using [this form](https://security.traefik.io). diff --git a/docs/user-guide/examples.md b/docs/user-guide/examples.md index 2b24194a0..d8a8e8698 100644 --- a/docs/user-guide/examples.md +++ b/docs/user-guide/examples.md @@ -329,3 +329,87 @@ providersThrottleDuration = "5s" [respondingTimeouts] idleTimeout = "360s" ``` + +## Using labels in docker-compose.yml + +Pay attention to the **labels** section: + +``` +home: +image: abiosoft/caddy:0.10.14 +networks: + - ntw_front +volumes: + - ./www/home/srv/:/srv/ +deploy: + mode: replicated + replicas: 2 + #placement: + # constraints: [node.role==manager] + restart_policy: + condition: on-failure + max_attempts: 5 + resources: + limits: + cpus: '0.20' + memory: 9M + reservations: + cpus: '0.05' + memory: 9M + labels: + - "traefik.frontend.rule=PathPrefixStrip:/" + - "traefik.backend=home" + - "traefik.port=2015" + - "traefik.weight=10" + - "traefik.enable=true" + - "traefik.passHostHeader=true" + - "traefik.docker.network=ntw_front" + - "traefik.frontend.entryPoints=http" + - "traefik.backend.loadbalancer.swarm=true" + - "traefik.backend.loadbalancer.method=drr" +``` + +Something more tricky using `regex`. + +In this case a slash is added to `siteexample.io/portainer` and redirect to `siteexample.io/portainer/`. For more details: https://github.com/containous/traefik/issues/563 + +The double sign `$$` are variables managed by the docker compose file ([documentation](https://docs.docker.com/compose/compose-file/#variable-substitution)). + +``` +portainer: +image: portainer/portainer:1.16.5 +networks: + - ntw_front +volumes: + - /var/run/docker.sock:/var/run/docker.sock +deploy: + mode: replicated + replicas: 1 + placement: + constraints: [node.role==manager] + restart_policy: + condition: on-failure + max_attempts: 5 + resources: + limits: + cpus: '0.33' + memory: 20M + reservations: + cpus: '0.05' + memory: 10M + labels: + - "traefik.frontend.rule=PathPrefixStrip:/portainer" + - "traefik.backend=portainer" + - "traefik.port=9000" + - "traefik.weight=10" + - "traefik.enable=true" + - "traefik.passHostHeader=true" + - "traefik.docker.network=ntw_front" + - "traefik.frontend.entryPoints=http" + - "traefik.backend.loadbalancer.swarm=true" + - "traefik.backend.loadbalancer.method=drr" + # https://github.com/containous/traefik/issues/563#issuecomment-421360934 + - "traefik.frontend.redirect.regex=^(.*)/portainer$$" + - "traefik.frontend.redirect.replacement=$$1/portainer/" + - "traefik.frontend.rule=PathPrefix:/portainer;ReplacePathRegex: ^/portainer/(.*) /$$1" +``` diff --git a/integration/fixtures/websocket/config.toml b/integration/fixtures/websocket/config.toml index 965f3d641..480ca01fc 100644 --- a/integration/fixtures/websocket/config.toml +++ b/integration/fixtures/websocket/config.toml @@ -19,5 +19,6 @@ logLevel = "DEBUG" [frontends] [frontends.frontend1] backend = "backend1" + passHostHeader = true [frontends.frontend1.routes.test_1] rule = "PathPrefix:/ws" diff --git a/integration/fixtures/websocket/config_https.toml b/integration/fixtures/websocket/config_https.toml index d1954a578..8f9c01c01 100644 --- a/integration/fixtures/websocket/config_https.toml +++ b/integration/fixtures/websocket/config_https.toml @@ -24,5 +24,6 @@ insecureSkipVerify=true [frontends] [frontends.frontend1] backend = "backend1" + passHostHeader = true [frontends.frontend1.routes.test_1] rule = "Path:/echo,/ws" diff --git a/middlewares/recover.go b/middlewares/recover.go index 79af9eee4..88a98629d 100644 --- a/middlewares/recover.go +++ b/middlewares/recover.go @@ -2,6 +2,7 @@ package middlewares import ( "net/http" + "runtime" "github.com/containous/traefik/log" "github.com/urfave/negroni" @@ -10,7 +11,7 @@ import ( // RecoverHandler recovers from a panic in http handlers func RecoverHandler(next http.Handler) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { - defer recoverFunc(w) + defer recoverFunc(w, r) next.ServeHTTP(w, r) } return http.HandlerFunc(fn) @@ -19,15 +20,32 @@ func RecoverHandler(next http.Handler) http.Handler { // NegroniRecoverHandler recovers from a panic in negroni handlers func NegroniRecoverHandler() negroni.Handler { fn := func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { - defer recoverFunc(w) + defer recoverFunc(w, r) next.ServeHTTP(w, r) } return negroni.HandlerFunc(fn) } -func recoverFunc(w http.ResponseWriter) { +func recoverFunc(w http.ResponseWriter, r *http.Request) { if err := recover(); err != nil { - log.Errorf("Recovered from panic in http handler: %+v", err) + if !shouldLogPanic(err) { + log.Debugf("Request has been aborted [%s - %s]: %v", r.RemoteAddr, r.URL, err) + return + } + + log.Errorf("Recovered from panic in HTTP handler [%s - %s]: %+v", r.RemoteAddr, r.URL, err) + + const size = 64 << 10 + buf := make([]byte, size) + buf = buf[:runtime.Stack(buf, false)] + log.Errorf("Stack: %s", buf) + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) } } + +// https://github.com/golang/go/blob/a0d6420d8be2ae7164797051ec74fa2a2df466a1/src/net/http/server.go#L1761-L1775 +// https://github.com/golang/go/blob/c33153f7b416c03983324b3e8f869ce1116d84bc/src/net/http/httputil/reverseproxy.go#L284 +func shouldLogPanic(panicValue interface{}) bool { + return panicValue != nil && panicValue != http.ErrAbortHandler +} diff --git a/provider/acme/provider.go b/provider/acme/provider.go index 348ab0626..e1c8c3d54 100644 --- a/provider/acme/provider.go +++ b/provider/acme/provider.go @@ -12,7 +12,6 @@ import ( "sync" "time" - "github.com/BurntSushi/ty/fun" "github.com/cenk/backoff" "github.com/containous/flaeg/parse" "github.com/containous/traefik/log" @@ -762,8 +761,17 @@ func (p *Provider) getValidDomains(domain types.Domain, wildcardAllowed bool) ([ } } - domains = fun.Map(types.CanonicalDomain, domains).([]string) - return domains, nil + var cleanDomains []string + for _, domain := range domains { + canonicalDomain := types.CanonicalDomain(domain) + cleanDomain := acme.UnFqdn(canonicalDomain) + if canonicalDomain != cleanDomain { + log.Warnf("FQDN detected, please remove the trailing dot: %s", canonicalDomain) + } + cleanDomains = append(cleanDomains, cleanDomain) + } + + return cleanDomains, nil } func isDomainAlreadyChecked(domainToCheck string, existentDomains []string) bool { diff --git a/provider/consulcatalog/config_test.go b/provider/consulcatalog/config_test.go index 8545b8d9c..6d133f4c3 100644 --- a/provider/consulcatalog/config_test.go +++ b/provider/consulcatalog/config_test.go @@ -329,6 +329,7 @@ func TestProviderBuildConfiguration(t *testing.T) { Attributes: []string{ "random.foo=bar", label.TraefikFrontendAuthForwardAddress + "=auth.server", + label.TraefikFrontendAuthForwardAuthResponseHeaders + "=X-Auth-User,X-Auth-Token", label.TraefikFrontendAuthForwardTrustForwardHeader + "=true", label.TraefikFrontendAuthForwardTLSCa + "=ca.crt", label.TraefikFrontendAuthForwardTLSCaOptional + "=true", @@ -371,8 +372,7 @@ func TestProviderBuildConfiguration(t *testing.T) { Auth: &types.Auth{ HeaderField: "X-WebAuth-User", Forward: &types.Forward{ - Address: "auth.server", - TrustForwardHeader: true, + Address: "auth.server", TLS: &types.ClientTLS{ CA: "ca.crt", CAOptional: true, @@ -380,6 +380,8 @@ func TestProviderBuildConfiguration(t *testing.T) { Cert: "server.crt", Key: "server.key", }, + TrustForwardHeader: true, + AuthResponseHeaders: []string{"X-Auth-User", "X-Auth-Token"}, }, }, EntryPoints: []string{}, @@ -443,6 +445,7 @@ func TestProviderBuildConfiguration(t *testing.T) { label.TraefikFrontendAuthDigestUsers + "=test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", label.TraefikFrontendAuthDigestUsersFile + "=.htpasswd", label.TraefikFrontendAuthForwardAddress + "=auth.server", + label.TraefikFrontendAuthForwardAuthResponseHeaders + "=X-Auth-User,X-Auth-Token", label.TraefikFrontendAuthForwardTrustForwardHeader + "=true", label.TraefikFrontendAuthForwardTLSCa + "=ca.crt", label.TraefikFrontendAuthForwardTLSCaOptional + "=true", diff --git a/provider/docker/config.go b/provider/docker/config.go index 3c6381869..86aa25924 100644 --- a/provider/docker/config.go +++ b/provider/docker/config.go @@ -337,21 +337,22 @@ func (p *Provider) getPortBinding(container dockerData) (*nat.PortBinding, error func (p *Provider) getIPPort(container dockerData) (string, string, error) { var ip, port string + usedBound := false if p.UseBindPortIP { portBinding, err := p.getPortBinding(container) if err != nil { - return "", "", fmt.Errorf("unable to find a binding for the container %q: ignoring server", container.Name) + log.Infof("Unable to find a binding for container %q, falling back on its internal IP/Port.", container.Name) + } else if (portBinding.HostIP == "0.0.0.0") || (len(portBinding.HostIP) == 0) { + log.Infof("Cannot determine the IP address (got %q) for %q's binding, falling back on its internal IP/Port.", portBinding.HostIP, container.Name) + } else { + ip = portBinding.HostIP + port = portBinding.HostPort + usedBound = true } + } - if portBinding.HostIP == "0.0.0.0" { - return "", "", fmt.Errorf("cannot determine the IP address (got 0.0.0.0) for the container %q: ignoring server", container.Name) - } - - ip = portBinding.HostIP - port = portBinding.HostPort - - } else { + if !usedBound { ip = p.getIPAddress(container) port = getPort(container) } @@ -359,6 +360,7 @@ func (p *Provider) getIPPort(container dockerData) (string, string, error) { if len(ip) == 0 { return "", "", fmt.Errorf("unable to find the IP address for the container %q: the server is ignored", container.Name) } + return ip, port, nil } diff --git a/provider/docker/config_container_docker_test.go b/provider/docker/config_container_docker_test.go index 8f3731a3c..39cfe36a1 100644 --- a/provider/docker/config_container_docker_test.go +++ b/provider/docker/config_container_docker_test.go @@ -63,56 +63,6 @@ func TestDockerBuildConfiguration(t *testing.T) { }, }, }, - { - desc: "when frontend basic auth", - containers: []docker.ContainerJSON{ - containerJSON( - name("test"), - labels(map[string]string{ - label.TraefikFrontendAuthBasicUsers: "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", - label.TraefikFrontendAuthBasicUsersFile: ".htpasswd", - label.TraefikFrontendAuthBasicRemoveHeader: "true", - label.TraefikFrontendAuthBasicRealm: "myRealm", - }), - ports(nat.PortMap{ - "80/tcp": {}, - }), - withNetwork("bridge", ipv4("127.0.0.1")), - ), - }, - expectedFrontends: map[string]*types.Frontend{ - "frontend-Host-test-docker-localhost-0": { - Backend: "backend-test", - PassHostHeader: true, - EntryPoints: []string{}, - Auth: &types.Auth{ - Basic: &types.Basic{ - RemoveHeader: true, - Realm: "myRealm", - Users: []string{"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/", - "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"}, - UsersFile: ".htpasswd", - }, - }, - Routes: map[string]types.Route{ - "route-frontend-Host-test-docker-localhost-0": { - Rule: "Host:test.docker.localhost", - }, - }, - }, - }, - expectedBackends: map[string]*types.Backend{ - "backend-test": { - Servers: map[string]types.Server{ - "server-test-842895ca2aca17f6ee36ddb2f621194d": { - URL: "http://127.0.0.1:80", - Weight: label.DefaultWeight, - }, - }, - CircuitBreaker: nil, - }, - }, - }, { desc: "when pass tls client certificate", containers: []docker.ContainerJSON{ @@ -175,6 +125,55 @@ func TestDockerBuildConfiguration(t *testing.T) { CircuitBreaker: nil, }, }, + }, { + desc: "when frontend basic auth", + containers: []docker.ContainerJSON{ + containerJSON( + name("test"), + labels(map[string]string{ + label.TraefikFrontendAuthBasicUsers: "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", + label.TraefikFrontendAuthBasicUsersFile: ".htpasswd", + label.TraefikFrontendAuthBasicRemoveHeader: "true", + label.TraefikFrontendAuthBasicRealm: "myRealm", + }), + ports(nat.PortMap{ + "80/tcp": {}, + }), + withNetwork("bridge", ipv4("127.0.0.1")), + ), + }, + expectedFrontends: map[string]*types.Frontend{ + "frontend-Host-test-docker-localhost-0": { + Backend: "backend-test", + PassHostHeader: true, + EntryPoints: []string{}, + Auth: &types.Auth{ + Basic: &types.Basic{ + RemoveHeader: true, + Realm: "myRealm", + Users: []string{"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/", + "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"}, + UsersFile: ".htpasswd", + }, + }, + Routes: map[string]types.Route{ + "route-frontend-Host-test-docker-localhost-0": { + Rule: "Host:test.docker.localhost", + }, + }, + }, + }, + expectedBackends: map[string]*types.Backend{ + "backend-test": { + Servers: map[string]types.Server{ + "server-test-842895ca2aca17f6ee36ddb2f621194d": { + URL: "http://127.0.0.1:80", + Weight: label.DefaultWeight, + }, + }, + CircuitBreaker: nil, + }, + }, }, { desc: "when frontend basic auth backward compatibility", @@ -281,6 +280,7 @@ func TestDockerBuildConfiguration(t *testing.T) { label.TraefikFrontendAuthForwardTLSCert: "server.crt", label.TraefikFrontendAuthForwardTLSKey: "server.key", label.TraefikFrontendAuthForwardTLSInsecureSkipVerify: "true", + label.TraefikFrontendAuthForwardAuthResponseHeaders: "X-Auth-User,X-Auth-Token", }), ports(nat.PortMap{ "80/tcp": {}, @@ -295,8 +295,7 @@ func TestDockerBuildConfiguration(t *testing.T) { EntryPoints: []string{}, Auth: &types.Auth{ Forward: &types.Forward{ - Address: "auth.server", - TrustForwardHeader: true, + Address: "auth.server", TLS: &types.ClientTLS{ CA: "ca.crt", CAOptional: true, @@ -304,6 +303,8 @@ func TestDockerBuildConfiguration(t *testing.T) { Cert: "server.crt", Key: "server.key", }, + TrustForwardHeader: true, + AuthResponseHeaders: []string{"X-Auth-User", "X-Auth-Token"}, }, }, Routes: map[string]types.Route{ @@ -1398,6 +1399,31 @@ func TestDockerGetIPPort(t *testing.T) { ip, port string expectsError bool }{ + { + desc: "label traefik.port not set, no binding, falling back on the container's IP/Port", + container: containerJSON( + ports(nat.PortMap{ + "8080/tcp": {}, + }), + withNetwork("testnet", ipv4("10.11.12.13"))), + ip: "10.11.12.13", + port: "8080", + }, + { + desc: "label traefik.port not set, single binding with port only, falling back on the container's IP/Port", + container: containerJSON( + withNetwork("testnet", ipv4("10.11.12.13")), + ports(nat.PortMap{ + "80/tcp": []nat.PortBinding{ + { + HostPort: "8082", + }, + }, + }), + ), + ip: "10.11.12.13", + port: "80", + }, { desc: "label traefik.port not set, binding with ip:port should create a route to the bound ip:port", container: containerJSON( @@ -1413,6 +1439,52 @@ func TestDockerGetIPPort(t *testing.T) { ip: "1.2.3.4", port: "8081", }, + { + desc: "label traefik.port set, no binding, falling back on the container's IP/traefik.port", + container: containerJSON( + labels(map[string]string{ + label.TraefikPort: "80", + }), + withNetwork("testnet", ipv4("10.11.12.13"))), + ip: "10.11.12.13", + port: "80", + }, + { + desc: "label traefik.port set, single binding with ip:port for the label, creates the route", + container: containerJSON( + labels(map[string]string{ + label.TraefikPort: "443", + }), + ports(nat.PortMap{ + "443/tcp": []nat.PortBinding{ + { + HostIP: "5.6.7.8", + HostPort: "8082", + }, + }, + }), + withNetwork("testnet", ipv4("10.11.12.13"))), + ip: "5.6.7.8", + port: "8082", + }, + { + desc: "label traefik.port set, no binding on the corresponding port, falling back on the container's IP/label.port", + container: containerJSON( + labels(map[string]string{ + label.TraefikPort: "80", + }), + ports(nat.PortMap{ + "443/tcp": []nat.PortBinding{ + { + HostIP: "5.6.7.8", + HostPort: "8082", + }, + }, + }), + withNetwork("testnet", ipv4("10.11.12.13"))), + ip: "10.11.12.13", + port: "80", + }, { desc: "label traefik.port set, multiple bindings on different ports, uses the label to select the correct (first) binding", container: containerJSON( @@ -1461,69 +1533,6 @@ func TestDockerGetIPPort(t *testing.T) { ip: "5.6.7.8", port: "8082", }, - { - desc: "label traefik.port set, single binding with ip:port for the label, creates the route", - container: containerJSON( - labels(map[string]string{ - label.TraefikPort: "443", - }), - ports(nat.PortMap{ - "443/tcp": []nat.PortBinding{ - { - HostIP: "5.6.7.8", - HostPort: "8082", - }, - }, - }), - withNetwork("testnet", ipv4("10.11.12.13"))), - ip: "5.6.7.8", - port: "8082", - }, - { - desc: "label traefik.port not set, single binding with port only, server ignored", - container: containerJSON( - ports(nat.PortMap{ - "80/tcp": []nat.PortBinding{ - { - HostPort: "8082", - }, - }, - }), - withNetwork("testnet", ipv4("10.11.12.13"))), - expectsError: true, - }, - { - desc: "label traefik.port not set, no binding, server ignored", - container: containerJSON( - withNetwork("testnet", ipv4("10.11.12.13"))), - expectsError: true, - }, - { - desc: "label traefik.port set, no binding on the corresponding port, server ignored", - container: containerJSON( - labels(map[string]string{ - label.TraefikPort: "80", - }), - ports(nat.PortMap{ - "443/tcp": []nat.PortBinding{ - { - HostIP: "5.6.7.8", - HostPort: "8082", - }, - }, - }), - withNetwork("testnet", ipv4("10.11.12.13"))), - expectsError: true, - }, - { - desc: "label traefik.port set, no binding, server ignored", - container: containerJSON( - labels(map[string]string{ - label.TraefikPort: "80", - }), - withNetwork("testnet", ipv4("10.11.12.13"))), - expectsError: true, - }, } for _, test := range testCases { @@ -1536,7 +1545,7 @@ func TestDockerGetIPPort(t *testing.T) { dData.SegmentLabels = segmentProperties[""] provider := &Provider{ - Network: "webnet", + Network: "testnet", UseBindPortIP: true, } diff --git a/provider/docker/config_container_swarm_test.go b/provider/docker/config_container_swarm_test.go index 830a354af..4938a1b25 100644 --- a/provider/docker/config_container_swarm_test.go +++ b/provider/docker/config_container_swarm_test.go @@ -324,6 +324,7 @@ func TestSwarmBuildConfiguration(t *testing.T) { label.TraefikFrontendAuthForwardTLSCert: "server.crt", label.TraefikFrontendAuthForwardTLSKey: "server.key", label.TraefikFrontendAuthForwardTLSInsecureSkipVerify: "true", + label.TraefikFrontendAuthForwardAuthResponseHeaders: "X-Auth-User,X-Auth-Token", }), withEndpointSpec(modeVIP), withEndpoint(virtualIP("1", "127.0.0.1/24")), @@ -336,8 +337,7 @@ func TestSwarmBuildConfiguration(t *testing.T) { EntryPoints: []string{}, Auth: &types.Auth{ Forward: &types.Forward{ - Address: "auth.server", - TrustForwardHeader: true, + Address: "auth.server", TLS: &types.ClientTLS{ CA: "ca.crt", CAOptional: true, @@ -345,6 +345,8 @@ func TestSwarmBuildConfiguration(t *testing.T) { Key: "server.key", InsecureSkipVerify: true, }, + TrustForwardHeader: true, + AuthResponseHeaders: []string{"X-Auth-User", "X-Auth-Token"}, }, }, Routes: map[string]types.Route{ diff --git a/provider/docker/config_segment_test.go b/provider/docker/config_segment_test.go index 009e753c5..810e0e6a1 100644 --- a/provider/docker/config_segment_test.go +++ b/provider/docker/config_segment_test.go @@ -298,6 +298,7 @@ func TestSegmentBuildConfiguration(t *testing.T) { label.Prefix + "sauternes." + label.SuffixFrontendAuthForwardTLSCert: "server.crt", label.Prefix + "sauternes." + label.SuffixFrontendAuthForwardTLSKey: "server.key", label.Prefix + "sauternes." + label.SuffixFrontendAuthForwardTLSInsecureSkipVerify: "true", + label.Prefix + "sauternes." + label.SuffixFrontendAuthForwardAuthResponseHeaders: "X-Auth-User,X-Auth-Token", }), ports(nat.PortMap{ "80/tcp": {}, @@ -318,8 +319,7 @@ func TestSegmentBuildConfiguration(t *testing.T) { Auth: &types.Auth{ HeaderField: "X-WebAuth-User", Forward: &types.Forward{ - Address: "auth.server", - TrustForwardHeader: true, + Address: "auth.server", TLS: &types.ClientTLS{ CA: "ca.crt", CAOptional: true, @@ -327,6 +327,8 @@ func TestSegmentBuildConfiguration(t *testing.T) { Key: "server.key", InsecureSkipVerify: true, }, + TrustForwardHeader: true, + AuthResponseHeaders: []string{"X-Auth-User", "X-Auth-Token"}, }, }, }, diff --git a/provider/ecs/config_segment_test.go b/provider/ecs/config_segment_test.go index bfcb80080..80246ec3c 100644 --- a/provider/ecs/config_segment_test.go +++ b/provider/ecs/config_segment_test.go @@ -247,6 +247,7 @@ func TestSegmentBuildConfiguration(t *testing.T) { label.Prefix + "sauternes." + label.SuffixFrontendAuthForwardTLSCert: "server.crt", label.Prefix + "sauternes." + label.SuffixFrontendAuthForwardTLSKey: "server.key", label.Prefix + "sauternes." + label.SuffixFrontendAuthForwardTLSInsecureSkipVerify: "true", + label.Prefix + "sauternes." + label.SuffixFrontendAuthForwardAuthResponseHeaders: "X-Auth-User,X-Auth-Token", }), iMachine( mState(ec2.InstanceStateNameRunning), @@ -270,8 +271,7 @@ func TestSegmentBuildConfiguration(t *testing.T) { Auth: &types.Auth{ HeaderField: "X-WebAuth-User", Forward: &types.Forward{ - Address: "auth.server", - TrustForwardHeader: true, + Address: "auth.server", TLS: &types.ClientTLS{ CA: "ca.crt", CAOptional: true, @@ -279,6 +279,8 @@ func TestSegmentBuildConfiguration(t *testing.T) { Key: "server.key", InsecureSkipVerify: true, }, + TrustForwardHeader: true, + AuthResponseHeaders: []string{"X-Auth-User", "X-Auth-Token"}, }, }, }, diff --git a/provider/ecs/config_test.go b/provider/ecs/config_test.go index 6af3ae455..826678acd 100644 --- a/provider/ecs/config_test.go +++ b/provider/ecs/config_test.go @@ -278,7 +278,9 @@ func TestBuildConfiguration(t *testing.T) { label.TraefikFrontendAuthForwardTLSCaOptional: aws.String("true"), label.TraefikFrontendAuthForwardTLSCert: aws.String("server.crt"), label.TraefikFrontendAuthForwardTLSKey: aws.String("server.key"), - label.TraefikFrontendAuthForwardTLSInsecureSkipVerify: aws.String("true"), label.TraefikFrontendAuthHeaderField: aws.String("X-WebAuth-User"), + label.TraefikFrontendAuthForwardTLSInsecureSkipVerify: aws.String("true"), + label.TraefikFrontendAuthHeaderField: aws.String("X-WebAuth-User"), + label.TraefikFrontendAuthForwardAuthResponseHeaders: aws.String("X-Auth-User,X-Auth-Token"), }), iMachine( mState(ec2.InstanceStateNameRunning), @@ -311,8 +313,7 @@ func TestBuildConfiguration(t *testing.T) { Auth: &types.Auth{ HeaderField: "X-WebAuth-User", Forward: &types.Forward{ - Address: "auth.server", - TrustForwardHeader: true, + Address: "auth.server", TLS: &types.ClientTLS{ CA: "ca.crt", CAOptional: true, @@ -320,6 +321,8 @@ func TestBuildConfiguration(t *testing.T) { Cert: "server.crt", Key: "server.key", }, + TrustForwardHeader: true, + AuthResponseHeaders: []string{"X-Auth-User", "X-Auth-Token"}, }, }, PassHostHeader: true, diff --git a/provider/kv/keynames.go b/provider/kv/keynames.go index 48ed83542..150bdee43 100644 --- a/provider/kv/keynames.go +++ b/provider/kv/keynames.go @@ -49,6 +49,7 @@ const ( pathFrontendWhiteListIPStrategyExcludedIPs = pathFrontendWhiteListIPStrategy + "/excludedips" pathFrontendAuth = "/auth/" + pathFrontendAuthHeaderField = pathFrontendAuth + "headerfield" pathFrontendAuthBasic = pathFrontendAuth + "basic/" pathFrontendAuthBasicRemoveHeader = pathFrontendAuthBasic + "removeheader" pathFrontendAuthBasicUsers = pathFrontendAuthBasic + "users" @@ -59,6 +60,7 @@ const ( pathFrontendAuthDigestUsersFile = pathFrontendAuthDigest + "usersfile" pathFrontendAuthForward = pathFrontendAuth + "forward/" pathFrontendAuthForwardAddress = pathFrontendAuthForward + "address" + pathFrontendAuthForwardAuthResponseHeaders = pathFrontendAuthForward + ".authresponseheaders" pathFrontendAuthForwardTLS = pathFrontendAuthForward + "tls/" pathFrontendAuthForwardTLSCa = pathFrontendAuthForwardTLS + "ca" pathFrontendAuthForwardTLSCaOptional = pathFrontendAuthForwardTLS + "caoptional" @@ -66,7 +68,6 @@ const ( pathFrontendAuthForwardTLSInsecureSkipVerify = pathFrontendAuthForwardTLS + "insecureskipverify" pathFrontendAuthForwardTLSKey = pathFrontendAuthForwardTLS + "key" pathFrontendAuthForwardTrustForwardHeader = pathFrontendAuthForward + "trustforwardheader" - pathFrontendAuthHeaderField = pathFrontendAuth + "headerfield" pathFrontendEntryPoints = "/entrypoints" pathFrontendRedirectEntryPoint = "/redirect/entrypoint" diff --git a/provider/kv/kv_config.go b/provider/kv/kv_config.go index 0e680abe7..64e70bd46 100644 --- a/provider/kv/kv_config.go +++ b/provider/kv/kv_config.go @@ -411,8 +411,9 @@ func (p *Provider) getAuthDigest(rootPath string) *types.Digest { // getAuthForward Create Forward Auth from path func (p *Provider) getAuthForward(rootPath string) *types.Forward { forwardAuth := &types.Forward{ - Address: p.get("", rootPath, pathFrontendAuthForwardAddress), - TrustForwardHeader: p.getBool(false, rootPath, pathFrontendAuthForwardTrustForwardHeader), + Address: p.get("", rootPath, pathFrontendAuthForwardAddress), + TrustForwardHeader: p.getBool(false, rootPath, pathFrontendAuthForwardTrustForwardHeader), + AuthResponseHeaders: p.getList(rootPath, pathFrontendAuthForwardAuthResponseHeaders), } // TLS configuration diff --git a/provider/kv/kv_config_test.go b/provider/kv/kv_config_test.go index 94ba889fa..6f4533b75 100644 --- a/provider/kv/kv_config_test.go +++ b/provider/kv/kv_config_test.go @@ -181,6 +181,7 @@ func TestProviderBuildConfiguration(t *testing.T) { withPair(pathFrontendAuthForwardTLSCert, "server.crt"), withPair(pathFrontendAuthForwardTLSKey, "server.key"), withPair(pathFrontendAuthForwardTLSInsecureSkipVerify, "true"), + withPair(pathFrontendAuthForwardAuthResponseHeaders, "X-Auth-User,X-Auth-Token"), ), backend("backend"), ), @@ -200,8 +201,7 @@ func TestProviderBuildConfiguration(t *testing.T) { Auth: &types.Auth{ HeaderField: "X-WebAuth-User", Forward: &types.Forward{ - Address: "auth.server", - TrustForwardHeader: true, + Address: "auth.server", TLS: &types.ClientTLS{ CA: "ca.crt", CAOptional: true, @@ -209,6 +209,8 @@ func TestProviderBuildConfiguration(t *testing.T) { Cert: "server.crt", Key: "server.key", }, + TrustForwardHeader: true, + AuthResponseHeaders: []string{"X-Auth-User", "X-Auth-Token"}, }, }, }, diff --git a/provider/label/names.go b/provider/label/names.go index 516720ea3..35c40eff5 100644 --- a/provider/label/names.go +++ b/provider/label/names.go @@ -47,6 +47,7 @@ const ( SuffixFrontendAuthDigestUsersFile = SuffixFrontendAuthDigest + ".usersFile" SuffixFrontendAuthForward = SuffixFrontendAuth + ".forward" SuffixFrontendAuthForwardAddress = SuffixFrontendAuthForward + ".address" + SuffixFrontendAuthForwardAuthResponseHeaders = SuffixFrontendAuthForward + ".authResponseHeaders" SuffixFrontendAuthForwardTLS = SuffixFrontendAuthForward + ".tls" SuffixFrontendAuthForwardTLSCa = SuffixFrontendAuthForwardTLS + ".ca" SuffixFrontendAuthForwardTLSCaOptional = SuffixFrontendAuthForwardTLS + ".caOptional" @@ -150,6 +151,7 @@ const ( TraefikFrontendAuthDigestUsersFile = Prefix + SuffixFrontendAuthDigestUsersFile TraefikFrontendAuthForward = Prefix + SuffixFrontendAuthForward TraefikFrontendAuthForwardAddress = Prefix + SuffixFrontendAuthForwardAddress + TraefikFrontendAuthForwardAuthResponseHeaders = Prefix + SuffixFrontendAuthForwardAuthResponseHeaders TraefikFrontendAuthForwardTLS = Prefix + SuffixFrontendAuthForwardTLS TraefikFrontendAuthForwardTLSCa = Prefix + SuffixFrontendAuthForwardTLSCa TraefikFrontendAuthForwardTLSCaOptional = Prefix + SuffixFrontendAuthForwardTLSCaOptional diff --git a/provider/label/partial.go b/provider/label/partial.go index 472e4cda1..64be029dd 100644 --- a/provider/label/partial.go +++ b/provider/label/partial.go @@ -147,8 +147,9 @@ func getAuthDigest(labels map[string]string) *types.Digest { // getAuthForward Create Forward Auth from labels func getAuthForward(labels map[string]string) *types.Forward { forwardAuth := &types.Forward{ - Address: GetStringValue(labels, TraefikFrontendAuthForwardAddress, ""), - TrustForwardHeader: GetBoolValue(labels, TraefikFrontendAuthForwardTrustForwardHeader, false), + Address: GetStringValue(labels, TraefikFrontendAuthForwardAddress, ""), + AuthResponseHeaders: GetSliceStringValue(labels, TraefikFrontendAuthForwardAuthResponseHeaders), + TrustForwardHeader: GetBoolValue(labels, TraefikFrontendAuthForwardTrustForwardHeader, false), } // TLS configuration diff --git a/provider/marathon/config_segment_test.go b/provider/marathon/config_segment_test.go new file mode 100644 index 000000000..dfaaf98d0 --- /dev/null +++ b/provider/marathon/config_segment_test.go @@ -0,0 +1,370 @@ +package marathon + +import ( + "testing" + "time" + + "github.com/containous/flaeg/parse" + "github.com/containous/traefik/provider/label" + "github.com/containous/traefik/types" + "github.com/gambol99/go-marathon" + "github.com/stretchr/testify/assert" +) + +func TestBuildConfigurationSegments(t *testing.T) { + testCases := []struct { + desc string + applications *marathon.Applications + expectedFrontends map[string]*types.Frontend + expectedBackends map[string]*types.Backend + }{ + { + desc: "multiple ports with segments", + applications: withApplications( + application( + appID("/app"), + appPorts(80, 81), + withTasks(localhostTask(taskPorts(80, 81))), + + withLabel(label.TraefikBackendMaxConnAmount, "1000"), + withLabel(label.TraefikBackendMaxConnExtractorFunc, "client.ip"), + withSegmentLabel(label.TraefikPort, "80", "web"), + withSegmentLabel(label.TraefikPort, "81", "admin"), + withLabel("traefik..port", "82"), // This should be ignored, as it fails to match the segmentPropertiesRegexp regex. + withSegmentLabel(label.TraefikFrontendRule, "Host:web.app.marathon.localhost", "web"), + withSegmentLabel(label.TraefikFrontendRule, "Host:admin.app.marathon.localhost", "admin"), + )), + expectedFrontends: map[string]*types.Frontend{ + "frontend-app-service-web": { + Backend: "backend-app-service-web", + Routes: map[string]types.Route{ + `route-host-app-service-web`: { + Rule: "Host:web.app.marathon.localhost", + }, + }, + PassHostHeader: true, + EntryPoints: []string{}, + }, + "frontend-app-service-admin": { + Backend: "backend-app-service-admin", + Routes: map[string]types.Route{ + `route-host-app-service-admin`: { + Rule: "Host:admin.app.marathon.localhost", + }, + }, + PassHostHeader: true, + EntryPoints: []string{}, + }, + }, + expectedBackends: map[string]*types.Backend{ + "backend-app-service-web": { + Servers: map[string]types.Server{ + "server-app-taskID-service-web": { + URL: "http://localhost:80", + Weight: label.DefaultWeight, + }, + }, + MaxConn: &types.MaxConn{ + Amount: 1000, + ExtractorFunc: "client.ip", + }, + }, + "backend-app-service-admin": { + Servers: map[string]types.Server{ + "server-app-taskID-service-admin": { + URL: "http://localhost:81", + Weight: label.DefaultWeight, + }, + }, + MaxConn: &types.MaxConn{ + Amount: 1000, + ExtractorFunc: "client.ip", + }, + }, + }, + }, + { + desc: "when all labels are set", + applications: withApplications( + application( + appID("/app"), + appPorts(80, 81), + withTasks(localhostTask(taskPorts(80, 81))), + + // withLabel(label.TraefikBackend, "foobar"), + + withLabel(label.TraefikBackendCircuitBreakerExpression, "NetworkErrorRatio() > 0.5"), + withLabel(label.TraefikBackendHealthCheckPath, "/health"), + withLabel(label.TraefikBackendHealthCheckPort, "880"), + withLabel(label.TraefikBackendHealthCheckInterval, "6"), + withLabel(label.TraefikBackendHealthCheckTimeout, "3"), + withLabel(label.TraefikBackendLoadBalancerMethod, "drr"), + withLabel(label.TraefikBackendLoadBalancerStickiness, "true"), + withLabel(label.TraefikBackendLoadBalancerStickinessCookieName, "chocolate"), + withLabel(label.TraefikBackendMaxConnAmount, "666"), + withLabel(label.TraefikBackendMaxConnExtractorFunc, "client.ip"), + withLabel(label.TraefikBackendBufferingMaxResponseBodyBytes, "10485760"), + withLabel(label.TraefikBackendBufferingMemResponseBodyBytes, "2097152"), + withLabel(label.TraefikBackendBufferingMaxRequestBodyBytes, "10485760"), + withLabel(label.TraefikBackendBufferingMemRequestBodyBytes, "2097152"), + withLabel(label.TraefikBackendBufferingRetryExpression, "IsNetworkError() && Attempts() <= 2"), + + withSegmentLabel(label.TraefikPort, "80", "containous"), + withSegmentLabel(label.TraefikProtocol, "https", "containous"), + withSegmentLabel(label.TraefikWeight, "12", "containous"), + + withSegmentLabel(label.TraefikFrontendPassTLSClientCertPem, "true", "containous"), + withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosNotBefore, "true", "containous"), + withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosNotAfter, "true", "containous"), + withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSans, "true", "containous"), + withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectCommonName, "true", "containous"), + withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectCountry, "true", "containous"), + withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectLocality, "true", "containous"), + withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectOrganization, "true", "containous"), + withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectProvince, "true", "containous"), + withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectSerialNumber, "true", "containous"), + + withSegmentLabel(label.TraefikFrontendAuthBasic, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", "containous"), + withSegmentLabel(label.TraefikFrontendAuthBasicRemoveHeader, "true", "containous"), + withSegmentLabel(label.TraefikFrontendAuthBasicUsers, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", "containous"), + withSegmentLabel(label.TraefikFrontendAuthBasicUsersFile, ".htpasswd", "containous"), + withSegmentLabel(label.TraefikFrontendAuthDigestRemoveHeader, "true", "containous"), + withSegmentLabel(label.TraefikFrontendAuthDigestUsers, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", "containous"), + withSegmentLabel(label.TraefikFrontendAuthDigestUsersFile, ".htpasswd", "containous"), + withSegmentLabel(label.TraefikFrontendAuthForwardAddress, "auth.server", "containous"), + withSegmentLabel(label.TraefikFrontendAuthForwardTrustForwardHeader, "true", "containous"), + withSegmentLabel(label.TraefikFrontendAuthForwardTLSCa, "ca.crt", "containous"), + withSegmentLabel(label.TraefikFrontendAuthForwardTLSCaOptional, "true", "containous"), + withSegmentLabel(label.TraefikFrontendAuthForwardTLSCert, "server.crt", "containous"), + withSegmentLabel(label.TraefikFrontendAuthForwardTLSKey, "server.key", "containous"), + withSegmentLabel(label.TraefikFrontendAuthForwardTLSInsecureSkipVerify, "true", "containous"), + withSegmentLabel(label.TraefikFrontendAuthHeaderField, "X-WebAuth-User", "containous"), + + withSegmentLabel(label.TraefikFrontendEntryPoints, "http,https", "containous"), + withSegmentLabel(label.TraefikFrontendPassHostHeader, "true", "containous"), + withSegmentLabel(label.TraefikFrontendPassTLSCert, "true", "containous"), + withSegmentLabel(label.TraefikFrontendPriority, "666", "containous"), + withSegmentLabel(label.TraefikFrontendRedirectEntryPoint, "https", "containous"), + withSegmentLabel(label.TraefikFrontendRedirectRegex, "nope", "containous"), + withSegmentLabel(label.TraefikFrontendRedirectReplacement, "nope", "containous"), + withSegmentLabel(label.TraefikFrontendRedirectPermanent, "true", "containous"), + withSegmentLabel(label.TraefikFrontendRule, "Host:traefik.io", "containous"), + withSegmentLabel(label.TraefikFrontendWhiteListSourceRange, "10.10.10.10", "containous"), + + withSegmentLabel(label.TraefikFrontendRequestHeaders, "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8", "containous"), + withSegmentLabel(label.TraefikFrontendResponseHeaders, "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8", "containous"), + withSegmentLabel(label.TraefikFrontendSSLProxyHeaders, "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8", "containous"), + withSegmentLabel(label.TraefikFrontendAllowedHosts, "foo,bar,bor", "containous"), + withSegmentLabel(label.TraefikFrontendHostsProxyHeaders, "foo,bar,bor", "containous"), + withSegmentLabel(label.TraefikFrontendSSLForceHost, "true", "containous"), + withSegmentLabel(label.TraefikFrontendSSLHost, "foo", "containous"), + withSegmentLabel(label.TraefikFrontendCustomFrameOptionsValue, "foo", "containous"), + withSegmentLabel(label.TraefikFrontendContentSecurityPolicy, "foo", "containous"), + withSegmentLabel(label.TraefikFrontendPublicKey, "foo", "containous"), + withSegmentLabel(label.TraefikFrontendReferrerPolicy, "foo", "containous"), + withSegmentLabel(label.TraefikFrontendCustomBrowserXSSValue, "foo", "containous"), + withSegmentLabel(label.TraefikFrontendSTSSeconds, "666", "containous"), + withSegmentLabel(label.TraefikFrontendSSLRedirect, "true", "containous"), + withSegmentLabel(label.TraefikFrontendSSLTemporaryRedirect, "true", "containous"), + withSegmentLabel(label.TraefikFrontendSTSIncludeSubdomains, "true", "containous"), + withSegmentLabel(label.TraefikFrontendSTSPreload, "true", "containous"), + withSegmentLabel(label.TraefikFrontendForceSTSHeader, "true", "containous"), + withSegmentLabel(label.TraefikFrontendFrameDeny, "true", "containous"), + withSegmentLabel(label.TraefikFrontendContentTypeNosniff, "true", "containous"), + withSegmentLabel(label.TraefikFrontendBrowserXSSFilter, "true", "containous"), + withSegmentLabel(label.TraefikFrontendIsDevelopment, "true", "containous"), + + withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"foo."+label.SuffixErrorPageStatus, "404"), + withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"foo."+label.SuffixErrorPageBackend, "foobar"), + withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"foo."+label.SuffixErrorPageQuery, "foo_query"), + withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"bar."+label.SuffixErrorPageStatus, "500,600"), + withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"bar."+label.SuffixErrorPageBackend, "foobar"), + withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"bar."+label.SuffixErrorPageQuery, "bar_query"), + + withSegmentLabel(label.TraefikFrontendRateLimitExtractorFunc, "client.ip", "containous"), + withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"foo."+label.SuffixRateLimitPeriod, "6"), + withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"foo."+label.SuffixRateLimitAverage, "12"), + withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"foo."+label.SuffixRateLimitBurst, "18"), + withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"bar."+label.SuffixRateLimitPeriod, "3"), + withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"bar."+label.SuffixRateLimitAverage, "6"), + withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"bar."+label.SuffixRateLimitBurst, "9"), + )), + expectedFrontends: map[string]*types.Frontend{ + "frontend-app-service-containous": { + EntryPoints: []string{ + "http", + "https", + }, + Backend: "backend-app-service-containous", + Routes: map[string]types.Route{ + "route-host-app-service-containous": { + Rule: "Host:traefik.io", + }, + }, + PassHostHeader: true, + PassTLSCert: true, + Priority: 666, + PassTLSClientCert: &types.TLSClientHeaders{ + PEM: true, + Infos: &types.TLSClientCertificateInfos{ + NotBefore: true, + Sans: true, + NotAfter: true, + Subject: &types.TLSCLientCertificateSubjectInfos{ + CommonName: true, + Country: true, + Locality: true, + Organization: true, + Province: true, + SerialNumber: true, + }, + }, + }, + Auth: &types.Auth{ + HeaderField: "X-WebAuth-User", + Basic: &types.Basic{ + RemoveHeader: true, + Users: []string{"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/", + "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"}, + UsersFile: ".htpasswd", + }, + }, + WhiteList: &types.WhiteList{ + SourceRange: []string{"10.10.10.10"}, + }, + Headers: &types.Headers{ + CustomRequestHeaders: map[string]string{ + "Access-Control-Allow-Methods": "POST,GET,OPTIONS", + "Content-Type": "application/json; charset=utf-8", + }, + CustomResponseHeaders: map[string]string{ + "Access-Control-Allow-Methods": "POST,GET,OPTIONS", + "Content-Type": "application/json; charset=utf-8", + }, + AllowedHosts: []string{ + "foo", + "bar", + "bor", + }, + HostsProxyHeaders: []string{ + "foo", + "bar", + "bor", + }, + SSLRedirect: true, + SSLTemporaryRedirect: true, + SSLForceHost: true, + SSLHost: "foo", + SSLProxyHeaders: map[string]string{ + "Access-Control-Allow-Methods": "POST,GET,OPTIONS", + "Content-Type": "application/json; charset=utf-8", + }, + STSSeconds: 666, + STSIncludeSubdomains: true, + STSPreload: true, + ForceSTSHeader: true, + FrameDeny: true, + CustomFrameOptionsValue: "foo", + ContentTypeNosniff: true, + BrowserXSSFilter: true, + CustomBrowserXSSValue: "foo", + ContentSecurityPolicy: "foo", + PublicKey: "foo", + ReferrerPolicy: "foo", + IsDevelopment: true, + }, + Errors: map[string]*types.ErrorPage{ + "bar": { + Status: []string{ + "500", + "600", + }, + Backend: "backendfoobar", + Query: "bar_query", + }, + "foo": { + Status: []string{ + "404", + }, + Backend: "backendfoobar", + Query: "foo_query", + }, + }, + RateLimit: &types.RateLimit{ + RateSet: map[string]*types.Rate{ + "bar": { + Period: parse.Duration(3 * time.Second), + Average: 6, + Burst: 9, + }, + "foo": { + Period: parse.Duration(6 * time.Second), + Average: 12, + Burst: 18, + }, + }, + ExtractorFunc: "client.ip", + }, + Redirect: &types.Redirect{ + EntryPoint: "https", + Permanent: true, + }, + }, + }, + expectedBackends: map[string]*types.Backend{ + "backend-app-service-containous": { + Servers: map[string]types.Server{ + "server-app-taskID-service-containous": { + URL: "https://localhost:80", + Weight: 12, + }, + }, + CircuitBreaker: &types.CircuitBreaker{ + Expression: "NetworkErrorRatio() > 0.5", + }, + LoadBalancer: &types.LoadBalancer{ + Method: "drr", + Stickiness: &types.Stickiness{ + CookieName: "chocolate", + }, + }, + MaxConn: &types.MaxConn{ + Amount: 666, + ExtractorFunc: "client.ip", + }, + HealthCheck: &types.HealthCheck{ + Path: "/health", + Port: 880, + Interval: "6", + Timeout: "3", + }, + Buffering: &types.Buffering{ + MaxResponseBodyBytes: 10485760, + MemResponseBodyBytes: 2097152, + MaxRequestBodyBytes: 10485760, + MemRequestBodyBytes: 2097152, + RetryExpression: "IsNetworkError() && Attempts() <= 2", + }, + }, + }, + }, + } + + for _, test := range testCases { + test := test + t.Run(test.desc, func(t *testing.T) { + t.Parallel() + + p := &Provider{ + Domain: "marathon.localhost", + ExposedByDefault: true, + } + + actualConfig := p.buildConfiguration(test.applications) + + assert.NotNil(t, actualConfig) + assert.Equal(t, test.expectedBackends, actualConfig.Backends) + assert.Equal(t, test.expectedFrontends, actualConfig.Frontends) + }) + } +} diff --git a/provider/marathon/config_test.go b/provider/marathon/config_test.go index d0cfe5f62..8da674907 100644 --- a/provider/marathon/config_test.go +++ b/provider/marathon/config_test.go @@ -299,6 +299,7 @@ func TestBuildConfiguration(t *testing.T) { withLabel(label.TraefikFrontendAuthForwardTLSCert, "server.crt"), withLabel(label.TraefikFrontendAuthForwardTLSKey, "server.key"), withLabel(label.TraefikFrontendAuthForwardTLSInsecureSkipVerify, "true"), + withLabel(label.TraefikFrontendAuthForwardAuthResponseHeaders, "X-Auth-User,X-Auth-Token"), withTasks(localhostTask(taskPorts(80))), )), @@ -313,8 +314,7 @@ func TestBuildConfiguration(t *testing.T) { Auth: &types.Auth{ HeaderField: "X-WebAuth-User", Forward: &types.Forward{ - Address: "auth.server", - TrustForwardHeader: true, + Address: "auth.server", TLS: &types.ClientTLS{ CA: "ca.crt", CAOptional: true, @@ -322,6 +322,8 @@ func TestBuildConfiguration(t *testing.T) { Cert: "server.crt", Key: "server.key", }, + TrustForwardHeader: true, + AuthResponseHeaders: []string{"X-Auth-User", "X-Auth-Token"}, }, }, PassHostHeader: true, @@ -695,364 +697,6 @@ func TestBuildConfiguration(t *testing.T) { } } -func TestBuildConfigurationSegments(t *testing.T) { - testCases := []struct { - desc string - applications *marathon.Applications - expectedFrontends map[string]*types.Frontend - expectedBackends map[string]*types.Backend - }{ - { - desc: "multiple ports with segments", - applications: withApplications( - application( - appID("/app"), - appPorts(80, 81), - withTasks(localhostTask(taskPorts(80, 81))), - - withLabel(label.TraefikBackendMaxConnAmount, "1000"), - withLabel(label.TraefikBackendMaxConnExtractorFunc, "client.ip"), - withSegmentLabel(label.TraefikPort, "80", "web"), - withSegmentLabel(label.TraefikPort, "81", "admin"), - withLabel("traefik..port", "82"), // This should be ignored, as it fails to match the segmentPropertiesRegexp regex. - withSegmentLabel(label.TraefikFrontendRule, "Host:web.app.marathon.localhost", "web"), - withSegmentLabel(label.TraefikFrontendRule, "Host:admin.app.marathon.localhost", "admin"), - )), - expectedFrontends: map[string]*types.Frontend{ - "frontend-app-service-web": { - Backend: "backend-app-service-web", - Routes: map[string]types.Route{ - `route-host-app-service-web`: { - Rule: "Host:web.app.marathon.localhost", - }, - }, - PassHostHeader: true, - EntryPoints: []string{}, - }, - "frontend-app-service-admin": { - Backend: "backend-app-service-admin", - Routes: map[string]types.Route{ - `route-host-app-service-admin`: { - Rule: "Host:admin.app.marathon.localhost", - }, - }, - PassHostHeader: true, - EntryPoints: []string{}, - }, - }, - expectedBackends: map[string]*types.Backend{ - "backend-app-service-web": { - Servers: map[string]types.Server{ - "server-app-taskID-service-web": { - URL: "http://localhost:80", - Weight: label.DefaultWeight, - }, - }, - MaxConn: &types.MaxConn{ - Amount: 1000, - ExtractorFunc: "client.ip", - }, - }, - "backend-app-service-admin": { - Servers: map[string]types.Server{ - "server-app-taskID-service-admin": { - URL: "http://localhost:81", - Weight: label.DefaultWeight, - }, - }, - MaxConn: &types.MaxConn{ - Amount: 1000, - ExtractorFunc: "client.ip", - }, - }, - }, - }, - { - desc: "when all labels are set", - applications: withApplications( - application( - appID("/app"), - appPorts(80, 81), - withTasks(localhostTask(taskPorts(80, 81))), - - // withLabel(label.TraefikBackend, "foobar"), - - withLabel(label.TraefikBackendCircuitBreakerExpression, "NetworkErrorRatio() > 0.5"), - withLabel(label.TraefikBackendHealthCheckPath, "/health"), - withLabel(label.TraefikBackendHealthCheckPort, "880"), - withLabel(label.TraefikBackendHealthCheckInterval, "6"), - withLabel(label.TraefikBackendHealthCheckTimeout, "3"), - withLabel(label.TraefikBackendLoadBalancerMethod, "drr"), - withLabel(label.TraefikBackendLoadBalancerStickiness, "true"), - withLabel(label.TraefikBackendLoadBalancerStickinessCookieName, "chocolate"), - withLabel(label.TraefikBackendMaxConnAmount, "666"), - withLabel(label.TraefikBackendMaxConnExtractorFunc, "client.ip"), - withLabel(label.TraefikBackendBufferingMaxResponseBodyBytes, "10485760"), - withLabel(label.TraefikBackendBufferingMemResponseBodyBytes, "2097152"), - withLabel(label.TraefikBackendBufferingMaxRequestBodyBytes, "10485760"), - withLabel(label.TraefikBackendBufferingMemRequestBodyBytes, "2097152"), - withLabel(label.TraefikBackendBufferingRetryExpression, "IsNetworkError() && Attempts() <= 2"), - - withSegmentLabel(label.TraefikPort, "80", "containous"), - withSegmentLabel(label.TraefikProtocol, "https", "containous"), - withSegmentLabel(label.TraefikWeight, "12", "containous"), - - withSegmentLabel(label.TraefikFrontendPassTLSClientCertPem, "true", "containous"), - withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosNotBefore, "true", "containous"), - withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosNotAfter, "true", "containous"), - withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSans, "true", "containous"), - withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectCommonName, "true", "containous"), - withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectCountry, "true", "containous"), - withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectLocality, "true", "containous"), - withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectOrganization, "true", "containous"), - withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectProvince, "true", "containous"), - withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectSerialNumber, "true", "containous"), - - withSegmentLabel(label.TraefikFrontendAuthBasic, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", "containous"), - withSegmentLabel(label.TraefikFrontendAuthBasicRemoveHeader, "true", "containous"), - withSegmentLabel(label.TraefikFrontendAuthBasicUsers, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", "containous"), - withSegmentLabel(label.TraefikFrontendAuthBasicUsersFile, ".htpasswd", "containous"), - withSegmentLabel(label.TraefikFrontendAuthDigestRemoveHeader, "true", "containous"), - withSegmentLabel(label.TraefikFrontendAuthDigestUsers, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", "containous"), - withSegmentLabel(label.TraefikFrontendAuthDigestUsersFile, ".htpasswd", "containous"), - withSegmentLabel(label.TraefikFrontendAuthForwardAddress, "auth.server", "containous"), - withSegmentLabel(label.TraefikFrontendAuthForwardTrustForwardHeader, "true", "containous"), - withSegmentLabel(label.TraefikFrontendAuthForwardTLSCa, "ca.crt", "containous"), - withSegmentLabel(label.TraefikFrontendAuthForwardTLSCaOptional, "true", "containous"), - withSegmentLabel(label.TraefikFrontendAuthForwardTLSCert, "server.crt", "containous"), - withSegmentLabel(label.TraefikFrontendAuthForwardTLSKey, "server.key", "containous"), - withSegmentLabel(label.TraefikFrontendAuthForwardTLSInsecureSkipVerify, "true", "containous"), - withSegmentLabel(label.TraefikFrontendAuthHeaderField, "X-WebAuth-User", "containous"), - - withSegmentLabel(label.TraefikFrontendEntryPoints, "http,https", "containous"), - withSegmentLabel(label.TraefikFrontendPassHostHeader, "true", "containous"), - withSegmentLabel(label.TraefikFrontendPassTLSCert, "true", "containous"), - withSegmentLabel(label.TraefikFrontendPriority, "666", "containous"), - withSegmentLabel(label.TraefikFrontendRedirectEntryPoint, "https", "containous"), - withSegmentLabel(label.TraefikFrontendRedirectRegex, "nope", "containous"), - withSegmentLabel(label.TraefikFrontendRedirectReplacement, "nope", "containous"), - withSegmentLabel(label.TraefikFrontendRedirectPermanent, "true", "containous"), - withSegmentLabel(label.TraefikFrontendRule, "Host:traefik.io", "containous"), - withSegmentLabel(label.TraefikFrontendWhiteListSourceRange, "10.10.10.10", "containous"), - - withSegmentLabel(label.TraefikFrontendRequestHeaders, "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8", "containous"), - withSegmentLabel(label.TraefikFrontendResponseHeaders, "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8", "containous"), - withSegmentLabel(label.TraefikFrontendSSLProxyHeaders, "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8", "containous"), - withSegmentLabel(label.TraefikFrontendAllowedHosts, "foo,bar,bor", "containous"), - withSegmentLabel(label.TraefikFrontendHostsProxyHeaders, "foo,bar,bor", "containous"), - withSegmentLabel(label.TraefikFrontendSSLForceHost, "true", "containous"), - withSegmentLabel(label.TraefikFrontendSSLHost, "foo", "containous"), - withSegmentLabel(label.TraefikFrontendCustomFrameOptionsValue, "foo", "containous"), - withSegmentLabel(label.TraefikFrontendContentSecurityPolicy, "foo", "containous"), - withSegmentLabel(label.TraefikFrontendPublicKey, "foo", "containous"), - withSegmentLabel(label.TraefikFrontendReferrerPolicy, "foo", "containous"), - withSegmentLabel(label.TraefikFrontendCustomBrowserXSSValue, "foo", "containous"), - withSegmentLabel(label.TraefikFrontendSTSSeconds, "666", "containous"), - withSegmentLabel(label.TraefikFrontendSSLRedirect, "true", "containous"), - withSegmentLabel(label.TraefikFrontendSSLTemporaryRedirect, "true", "containous"), - withSegmentLabel(label.TraefikFrontendSTSIncludeSubdomains, "true", "containous"), - withSegmentLabel(label.TraefikFrontendSTSPreload, "true", "containous"), - withSegmentLabel(label.TraefikFrontendForceSTSHeader, "true", "containous"), - withSegmentLabel(label.TraefikFrontendFrameDeny, "true", "containous"), - withSegmentLabel(label.TraefikFrontendContentTypeNosniff, "true", "containous"), - withSegmentLabel(label.TraefikFrontendBrowserXSSFilter, "true", "containous"), - withSegmentLabel(label.TraefikFrontendIsDevelopment, "true", "containous"), - - withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"foo."+label.SuffixErrorPageStatus, "404"), - withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"foo."+label.SuffixErrorPageBackend, "foobar"), - withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"foo."+label.SuffixErrorPageQuery, "foo_query"), - withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"bar."+label.SuffixErrorPageStatus, "500,600"), - withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"bar."+label.SuffixErrorPageBackend, "foobar"), - withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"bar."+label.SuffixErrorPageQuery, "bar_query"), - - withSegmentLabel(label.TraefikFrontendRateLimitExtractorFunc, "client.ip", "containous"), - withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"foo."+label.SuffixRateLimitPeriod, "6"), - withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"foo."+label.SuffixRateLimitAverage, "12"), - withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"foo."+label.SuffixRateLimitBurst, "18"), - withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"bar."+label.SuffixRateLimitPeriod, "3"), - withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"bar."+label.SuffixRateLimitAverage, "6"), - withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"bar."+label.SuffixRateLimitBurst, "9"), - )), - expectedFrontends: map[string]*types.Frontend{ - "frontend-app-service-containous": { - EntryPoints: []string{ - "http", - "https", - }, - Backend: "backend-app-service-containous", - Routes: map[string]types.Route{ - "route-host-app-service-containous": { - Rule: "Host:traefik.io", - }, - }, - PassHostHeader: true, - PassTLSCert: true, - Priority: 666, - PassTLSClientCert: &types.TLSClientHeaders{ - PEM: true, - Infos: &types.TLSClientCertificateInfos{ - NotBefore: true, - Sans: true, - NotAfter: true, - Subject: &types.TLSCLientCertificateSubjectInfos{ - CommonName: true, - Country: true, - Locality: true, - Organization: true, - Province: true, - SerialNumber: true, - }, - }, - }, - Auth: &types.Auth{ - HeaderField: "X-WebAuth-User", - Basic: &types.Basic{ - RemoveHeader: true, - Users: []string{"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/", - "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"}, - UsersFile: ".htpasswd", - }, - }, - WhiteList: &types.WhiteList{ - SourceRange: []string{"10.10.10.10"}, - }, - Headers: &types.Headers{ - CustomRequestHeaders: map[string]string{ - "Access-Control-Allow-Methods": "POST,GET,OPTIONS", - "Content-Type": "application/json; charset=utf-8", - }, - CustomResponseHeaders: map[string]string{ - "Access-Control-Allow-Methods": "POST,GET,OPTIONS", - "Content-Type": "application/json; charset=utf-8", - }, - AllowedHosts: []string{ - "foo", - "bar", - "bor", - }, - HostsProxyHeaders: []string{ - "foo", - "bar", - "bor", - }, - SSLRedirect: true, - SSLTemporaryRedirect: true, - SSLForceHost: true, - SSLHost: "foo", - SSLProxyHeaders: map[string]string{ - "Access-Control-Allow-Methods": "POST,GET,OPTIONS", - "Content-Type": "application/json; charset=utf-8", - }, - STSSeconds: 666, - STSIncludeSubdomains: true, - STSPreload: true, - ForceSTSHeader: true, - FrameDeny: true, - CustomFrameOptionsValue: "foo", - ContentTypeNosniff: true, - BrowserXSSFilter: true, - CustomBrowserXSSValue: "foo", - ContentSecurityPolicy: "foo", - PublicKey: "foo", - ReferrerPolicy: "foo", - IsDevelopment: true, - }, - Errors: map[string]*types.ErrorPage{ - "bar": { - Status: []string{ - "500", - "600", - }, - Backend: "backendfoobar", - Query: "bar_query", - }, - "foo": { - Status: []string{ - "404", - }, - Backend: "backendfoobar", - Query: "foo_query", - }, - }, - RateLimit: &types.RateLimit{ - RateSet: map[string]*types.Rate{ - "bar": { - Period: parse.Duration(3 * time.Second), - Average: 6, - Burst: 9, - }, - "foo": { - Period: parse.Duration(6 * time.Second), - Average: 12, - Burst: 18, - }, - }, - ExtractorFunc: "client.ip", - }, - Redirect: &types.Redirect{ - EntryPoint: "https", - Permanent: true, - }, - }, - }, - expectedBackends: map[string]*types.Backend{ - "backend-app-service-containous": { - Servers: map[string]types.Server{ - "server-app-taskID-service-containous": { - URL: "https://localhost:80", - Weight: 12, - }, - }, - CircuitBreaker: &types.CircuitBreaker{ - Expression: "NetworkErrorRatio() > 0.5", - }, - LoadBalancer: &types.LoadBalancer{ - Method: "drr", - Stickiness: &types.Stickiness{ - CookieName: "chocolate", - }, - }, - MaxConn: &types.MaxConn{ - Amount: 666, - ExtractorFunc: "client.ip", - }, - HealthCheck: &types.HealthCheck{ - Path: "/health", - Port: 880, - Interval: "6", - Timeout: "3", - }, - Buffering: &types.Buffering{ - MaxResponseBodyBytes: 10485760, - MemResponseBodyBytes: 2097152, - MaxRequestBodyBytes: 10485760, - MemRequestBodyBytes: 2097152, - RetryExpression: "IsNetworkError() && Attempts() <= 2", - }, - }, - }, - }, - } - - for _, test := range testCases { - test := test - t.Run(test.desc, func(t *testing.T) { - t.Parallel() - - p := &Provider{ - Domain: "marathon.localhost", - ExposedByDefault: true, - } - - actualConfig := p.buildConfiguration(test.applications) - - assert.NotNil(t, actualConfig) - assert.Equal(t, test.expectedBackends, actualConfig.Backends) - assert.Equal(t, test.expectedFrontends, actualConfig.Frontends) - }) - } -} - func TestApplicationFilterConstraints(t *testing.T) { testCases := []struct { desc string diff --git a/provider/mesos/config_segment_test.go b/provider/mesos/config_segment_test.go new file mode 100644 index 000000000..a5ef74da5 --- /dev/null +++ b/provider/mesos/config_segment_test.go @@ -0,0 +1,392 @@ +package mesos + +import ( + "testing" + "time" + + "github.com/containous/flaeg/parse" + "github.com/containous/traefik/provider/label" + "github.com/containous/traefik/types" + "github.com/mesosphere/mesos-dns/records/state" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestBuildConfigurationSegments(t *testing.T) { + p := &Provider{ + Domain: "mesos.localhost", + ExposedByDefault: true, + IPSources: "host", + } + + testCases := []struct { + desc string + tasks []state.Task + expectedFrontends map[string]*types.Frontend + expectedBackends map[string]*types.Backend + }{ + { + desc: "multiple ports with segments", + tasks: []state.Task{ + aTask("app-taskID", + withIP("127.0.0.1"), + withInfo("/app", + withPorts( + withPort("TCP", 80, "web"), + withPort("TCP", 81, "admin"), + ), + ), + withStatus(withHealthy(true), withState("TASK_RUNNING")), + withLabel(label.TraefikBackendMaxConnAmount, "1000"), + withLabel(label.TraefikBackendMaxConnExtractorFunc, "client.ip"), + withSegmentLabel(label.TraefikPort, "80", "web"), + withSegmentLabel(label.TraefikPort, "81", "admin"), + withLabel("traefik..port", "82"), // This should be ignored, as it fails to match the segmentPropertiesRegexp regex. + withSegmentLabel(label.TraefikFrontendRule, "Host:web.app.mesos.localhost", "web"), + withSegmentLabel(label.TraefikFrontendRule, "Host:admin.app.mesos.localhost", "admin"), + ), + }, + expectedFrontends: map[string]*types.Frontend{ + "frontend-app-taskID-service-web": { + Backend: "backend-app-service-web", + Routes: map[string]types.Route{ + `route-host-app-taskID-service-web`: { + Rule: "Host:web.app.mesos.localhost", + }, + }, + PassHostHeader: true, + EntryPoints: []string{}, + }, + "frontend-app-taskID-service-admin": { + Backend: "backend-app-service-admin", + Routes: map[string]types.Route{ + `route-host-app-taskID-service-admin`: { + Rule: "Host:admin.app.mesos.localhost", + }, + }, + PassHostHeader: true, + EntryPoints: []string{}, + }, + }, + expectedBackends: map[string]*types.Backend{ + "backend-app-service-web": { + Servers: map[string]types.Server{ + "server-app-taskID-service-web": { + URL: "http://127.0.0.1:80", + Weight: label.DefaultWeight, + }, + }, + MaxConn: &types.MaxConn{ + Amount: 1000, + ExtractorFunc: "client.ip", + }, + }, + "backend-app-service-admin": { + Servers: map[string]types.Server{ + "server-app-taskID-service-admin": { + URL: "http://127.0.0.1:81", + Weight: label.DefaultWeight, + }, + }, + MaxConn: &types.MaxConn{ + Amount: 1000, + ExtractorFunc: "client.ip", + }, + }, + }, + }, + { + desc: "when all labels are set", + tasks: []state.Task{ + aTask("app-taskID", + withIP("127.0.0.1"), + withInfo("/app", + withPorts( + withPort("TCP", 80, "web"), + withPort("TCP", 81, "admin"), + ), + ), + withStatus(withHealthy(true), withState("TASK_RUNNING")), + + withLabel(label.TraefikBackendCircuitBreakerExpression, "NetworkErrorRatio() > 0.5"), + withLabel(label.TraefikBackendHealthCheckScheme, "http"), + withLabel(label.TraefikBackendHealthCheckPath, "/health"), + withLabel(label.TraefikBackendHealthCheckPort, "880"), + withLabel(label.TraefikBackendHealthCheckInterval, "6"), + withLabel(label.TraefikBackendHealthCheckTimeout, "3"), + withLabel(label.TraefikBackendHealthCheckHostname, "foo.com"), + withLabel(label.TraefikBackendHealthCheckHeaders, "Foo:bar || Bar:foo"), + withLabel(label.TraefikBackendLoadBalancerMethod, "drr"), + withLabel(label.TraefikBackendLoadBalancerStickiness, "true"), + withLabel(label.TraefikBackendLoadBalancerStickinessCookieName, "chocolate"), + withLabel(label.TraefikBackendMaxConnAmount, "666"), + withLabel(label.TraefikBackendMaxConnExtractorFunc, "client.ip"), + withLabel(label.TraefikBackendBufferingMaxResponseBodyBytes, "10485760"), + withLabel(label.TraefikBackendBufferingMemResponseBodyBytes, "2097152"), + withLabel(label.TraefikBackendBufferingMaxRequestBodyBytes, "10485760"), + withLabel(label.TraefikBackendBufferingMemRequestBodyBytes, "2097152"), + withLabel(label.TraefikBackendBufferingRetryExpression, "IsNetworkError() && Attempts() <= 2"), + + withSegmentLabel(label.TraefikPort, "80", "containous"), + withSegmentLabel(label.TraefikPortName, "web", "containous"), + withSegmentLabel(label.TraefikProtocol, "https", "containous"), + withSegmentLabel(label.TraefikWeight, "12", "containous"), + + withSegmentLabel(label.TraefikFrontendPassTLSClientCertPem, "true", "containous"), + withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosNotBefore, "true", "containous"), + withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosNotAfter, "true", "containous"), + withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSans, "true", "containous"), + withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectCommonName, "true", "containous"), + withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectCountry, "true", "containous"), + withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectLocality, "true", "containous"), + withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectOrganization, "true", "containous"), + withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectProvince, "true", "containous"), + withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectSerialNumber, "true", "containous"), + + withSegmentLabel(label.TraefikFrontendAuthBasic, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", "containous"), + withSegmentLabel(label.TraefikFrontendAuthBasicRemoveHeader, "true", "containous"), + withSegmentLabel(label.TraefikFrontendAuthBasicUsers, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", "containous"), + withSegmentLabel(label.TraefikFrontendAuthBasicUsersFile, ".htpasswd", "containous"), + withSegmentLabel(label.TraefikFrontendAuthDigestRemoveHeader, "true", "containous"), + withSegmentLabel(label.TraefikFrontendAuthDigestUsers, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", "containous"), + withSegmentLabel(label.TraefikFrontendAuthDigestUsersFile, ".htpasswd", "containous"), + withSegmentLabel(label.TraefikFrontendAuthForwardAddress, "auth.server", "containous"), + withSegmentLabel(label.TraefikFrontendAuthForwardTrustForwardHeader, "true", "containous"), + withSegmentLabel(label.TraefikFrontendAuthForwardTLSCa, "ca.crt", "containous"), + withSegmentLabel(label.TraefikFrontendAuthForwardTLSCaOptional, "true", "containous"), + withSegmentLabel(label.TraefikFrontendAuthForwardTLSCert, "server.crt", "containous"), + withSegmentLabel(label.TraefikFrontendAuthForwardTLSKey, "server.key", "containous"), + withSegmentLabel(label.TraefikFrontendAuthForwardTLSInsecureSkipVerify, "true", "containous"), + withSegmentLabel(label.TraefikFrontendAuthHeaderField, "X-WebAuth-User", "containous"), + + withSegmentLabel(label.TraefikFrontendEntryPoints, "http,https", "containous"), + withSegmentLabel(label.TraefikFrontendPassHostHeader, "true", "containous"), + withSegmentLabel(label.TraefikFrontendPassTLSCert, "true", "containous"), + withSegmentLabel(label.TraefikFrontendPriority, "666", "containous"), + withSegmentLabel(label.TraefikFrontendRedirectEntryPoint, "https", "containous"), + withSegmentLabel(label.TraefikFrontendRedirectRegex, "nope", "containous"), + withSegmentLabel(label.TraefikFrontendRedirectReplacement, "nope", "containous"), + withSegmentLabel(label.TraefikFrontendRedirectPermanent, "true", "containous"), + withSegmentLabel(label.TraefikFrontendRule, "Host:traefik.io", "containous"), + withSegmentLabel(label.TraefikFrontendWhiteListSourceRange, "10.10.10.10", "containous"), + + withSegmentLabel(label.TraefikFrontendRequestHeaders, "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8", "containous"), + withSegmentLabel(label.TraefikFrontendResponseHeaders, "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8", "containous"), + withSegmentLabel(label.TraefikFrontendSSLProxyHeaders, "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8", "containous"), + withSegmentLabel(label.TraefikFrontendAllowedHosts, "foo,bar,bor", "containous"), + withSegmentLabel(label.TraefikFrontendHostsProxyHeaders, "foo,bar,bor", "containous"), + withSegmentLabel(label.TraefikFrontendSSLForceHost, "true", "containous"), + withSegmentLabel(label.TraefikFrontendSSLHost, "foo", "containous"), + withSegmentLabel(label.TraefikFrontendCustomFrameOptionsValue, "foo", "containous"), + withSegmentLabel(label.TraefikFrontendContentSecurityPolicy, "foo", "containous"), + withSegmentLabel(label.TraefikFrontendPublicKey, "foo", "containous"), + withSegmentLabel(label.TraefikFrontendReferrerPolicy, "foo", "containous"), + withSegmentLabel(label.TraefikFrontendCustomBrowserXSSValue, "foo", "containous"), + withSegmentLabel(label.TraefikFrontendSTSSeconds, "666", "containous"), + withSegmentLabel(label.TraefikFrontendSSLRedirect, "true", "containous"), + withSegmentLabel(label.TraefikFrontendSSLTemporaryRedirect, "true", "containous"), + withSegmentLabel(label.TraefikFrontendSTSIncludeSubdomains, "true", "containous"), + withSegmentLabel(label.TraefikFrontendSTSPreload, "true", "containous"), + withSegmentLabel(label.TraefikFrontendForceSTSHeader, "true", "containous"), + withSegmentLabel(label.TraefikFrontendFrameDeny, "true", "containous"), + withSegmentLabel(label.TraefikFrontendContentTypeNosniff, "true", "containous"), + withSegmentLabel(label.TraefikFrontendBrowserXSSFilter, "true", "containous"), + withSegmentLabel(label.TraefikFrontendIsDevelopment, "true", "containous"), + + withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"foo."+label.SuffixErrorPageStatus, "404"), + withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"foo."+label.SuffixErrorPageBackend, "foobar"), + withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"foo."+label.SuffixErrorPageQuery, "foo_query"), + withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"bar."+label.SuffixErrorPageStatus, "500,600"), + withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"bar."+label.SuffixErrorPageBackend, "foobar"), + withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"bar."+label.SuffixErrorPageQuery, "bar_query"), + + withSegmentLabel(label.TraefikFrontendRateLimitExtractorFunc, "client.ip", "containous"), + withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"foo."+label.SuffixRateLimitPeriod, "6"), + withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"foo."+label.SuffixRateLimitAverage, "12"), + withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"foo."+label.SuffixRateLimitBurst, "18"), + withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"bar."+label.SuffixRateLimitPeriod, "3"), + withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"bar."+label.SuffixRateLimitAverage, "6"), + withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"bar."+label.SuffixRateLimitBurst, "9"), + ), + }, + expectedFrontends: map[string]*types.Frontend{ + "frontend-app-taskID-service-containous": { + EntryPoints: []string{ + "http", + "https", + }, + Backend: "backend-app-service-containous", + Routes: map[string]types.Route{ + "route-host-app-taskID-service-containous": { + Rule: "Host:traefik.io", + }, + }, + PassHostHeader: true, + PassTLSCert: true, + Priority: 666, + PassTLSClientCert: &types.TLSClientHeaders{ + PEM: true, + Infos: &types.TLSClientCertificateInfos{ + NotBefore: true, + Sans: true, + NotAfter: true, + Subject: &types.TLSCLientCertificateSubjectInfos{ + CommonName: true, + Country: true, + Locality: true, + Organization: true, + Province: true, + SerialNumber: true, + }, + }, + }, + Auth: &types.Auth{ + HeaderField: "X-WebAuth-User", + Basic: &types.Basic{ + RemoveHeader: true, + Users: []string{"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/", + "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"}, + UsersFile: ".htpasswd", + }, + }, + + WhiteList: &types.WhiteList{ + SourceRange: []string{"10.10.10.10"}, + }, + Headers: &types.Headers{ + CustomRequestHeaders: map[string]string{ + "Access-Control-Allow-Methods": "POST,GET,OPTIONS", + "Content-Type": "application/json; charset=utf-8", + }, + CustomResponseHeaders: map[string]string{ + "Access-Control-Allow-Methods": "POST,GET,OPTIONS", + "Content-Type": "application/json; charset=utf-8", + }, + AllowedHosts: []string{ + "foo", + "bar", + "bor", + }, + HostsProxyHeaders: []string{ + "foo", + "bar", + "bor", + }, + SSLRedirect: true, + SSLTemporaryRedirect: true, + SSLForceHost: true, + SSLHost: "foo", + SSLProxyHeaders: map[string]string{ + "Access-Control-Allow-Methods": "POST,GET,OPTIONS", + "Content-Type": "application/json; charset=utf-8", + }, + STSSeconds: 666, + STSIncludeSubdomains: true, + STSPreload: true, + ForceSTSHeader: true, + FrameDeny: true, + CustomFrameOptionsValue: "foo", + ContentTypeNosniff: true, + BrowserXSSFilter: true, + CustomBrowserXSSValue: "foo", + ContentSecurityPolicy: "foo", + PublicKey: "foo", + ReferrerPolicy: "foo", + IsDevelopment: true, + }, + Errors: map[string]*types.ErrorPage{ + "bar": { + Status: []string{ + "500", + "600", + }, + Backend: "backend-foobar", + Query: "bar_query", + }, + "foo": { + Status: []string{ + "404", + }, + Backend: "backend-foobar", + Query: "foo_query", + }, + }, + RateLimit: &types.RateLimit{ + RateSet: map[string]*types.Rate{ + "bar": { + Period: parse.Duration(3 * time.Second), + Average: 6, + Burst: 9, + }, + "foo": { + Period: parse.Duration(6 * time.Second), + Average: 12, + Burst: 18, + }, + }, + ExtractorFunc: "client.ip", + }, + Redirect: &types.Redirect{ + EntryPoint: "https", + Permanent: true, + }, + }, + }, + expectedBackends: map[string]*types.Backend{ + "backend-app-service-containous": { + Servers: map[string]types.Server{ + "server-app-taskID-service-containous": { + URL: "https://127.0.0.1:80", + Weight: 12, + }, + }, + CircuitBreaker: &types.CircuitBreaker{ + Expression: "NetworkErrorRatio() > 0.5", + }, + LoadBalancer: &types.LoadBalancer{ + Method: "drr", + Stickiness: &types.Stickiness{ + CookieName: "chocolate", + }, + }, + MaxConn: &types.MaxConn{ + Amount: 666, + ExtractorFunc: "client.ip", + }, + HealthCheck: &types.HealthCheck{ + Scheme: "http", + Path: "/health", + Port: 880, + Interval: "6", + Timeout: "3", + Hostname: "foo.com", + Headers: map[string]string{ + "Bar": "foo", + "Foo": "bar", + }, + }, + Buffering: &types.Buffering{ + MaxResponseBodyBytes: 10485760, + MemResponseBodyBytes: 2097152, + MaxRequestBodyBytes: 10485760, + MemRequestBodyBytes: 2097152, + RetryExpression: "IsNetworkError() && Attempts() <= 2", + }, + }, + }, + }, + } + + for _, test := range testCases { + test := test + t.Run(test.desc, func(t *testing.T) { + t.Parallel() + + actualConfig := p.buildConfiguration(test.tasks) + + require.NotNil(t, actualConfig) + assert.Equal(t, test.expectedBackends, actualConfig.Backends) + assert.Equal(t, test.expectedFrontends, actualConfig.Frontends) + }) + } +} diff --git a/provider/mesos/config_test.go b/provider/mesos/config_test.go index 3431d8675..d5af11249 100644 --- a/provider/mesos/config_test.go +++ b/provider/mesos/config_test.go @@ -262,6 +262,7 @@ func TestBuildConfiguration(t *testing.T) { withLabel(label.TraefikFrontendAuthForwardTLSKey, "server.key"), withLabel(label.TraefikFrontendAuthForwardTLSInsecureSkipVerify, "true"), withLabel(label.TraefikFrontendAuthHeaderField, "X-WebAuth-User"), + withLabel(label.TraefikFrontendAuthForwardAuthResponseHeaders, "X-Auth-User,X-Auth-Token"), ), }, expectedFrontends: map[string]*types.Frontend{ @@ -277,8 +278,7 @@ func TestBuildConfiguration(t *testing.T) { Auth: &types.Auth{ HeaderField: "X-WebAuth-User", Forward: &types.Forward{ - Address: "auth.server", - TrustForwardHeader: true, + Address: "auth.server", TLS: &types.ClientTLS{ CA: "ca.crt", CAOptional: true, @@ -286,6 +286,8 @@ func TestBuildConfiguration(t *testing.T) { Cert: "server.crt", Key: "server.key", }, + TrustForwardHeader: true, + AuthResponseHeaders: []string{"X-Auth-User", "X-Auth-Token"}, }, }, }, @@ -597,385 +599,6 @@ func TestBuildConfiguration(t *testing.T) { } } -func TestBuildConfigurationSegments(t *testing.T) { - p := &Provider{ - Domain: "mesos.localhost", - ExposedByDefault: true, - IPSources: "host", - } - - testCases := []struct { - desc string - tasks []state.Task - expectedFrontends map[string]*types.Frontend - expectedBackends map[string]*types.Backend - }{ - { - desc: "multiple ports with segments", - tasks: []state.Task{ - aTask("app-taskID", - withIP("127.0.0.1"), - withInfo("/app", - withPorts( - withPort("TCP", 80, "web"), - withPort("TCP", 81, "admin"), - ), - ), - withStatus(withHealthy(true), withState("TASK_RUNNING")), - withLabel(label.TraefikBackendMaxConnAmount, "1000"), - withLabel(label.TraefikBackendMaxConnExtractorFunc, "client.ip"), - withSegmentLabel(label.TraefikPort, "80", "web"), - withSegmentLabel(label.TraefikPort, "81", "admin"), - withLabel("traefik..port", "82"), // This should be ignored, as it fails to match the segmentPropertiesRegexp regex. - withSegmentLabel(label.TraefikFrontendRule, "Host:web.app.mesos.localhost", "web"), - withSegmentLabel(label.TraefikFrontendRule, "Host:admin.app.mesos.localhost", "admin"), - ), - }, - expectedFrontends: map[string]*types.Frontend{ - "frontend-app-taskID-service-web": { - Backend: "backend-app-service-web", - Routes: map[string]types.Route{ - `route-host-app-taskID-service-web`: { - Rule: "Host:web.app.mesos.localhost", - }, - }, - PassHostHeader: true, - EntryPoints: []string{}, - }, - "frontend-app-taskID-service-admin": { - Backend: "backend-app-service-admin", - Routes: map[string]types.Route{ - `route-host-app-taskID-service-admin`: { - Rule: "Host:admin.app.mesos.localhost", - }, - }, - PassHostHeader: true, - EntryPoints: []string{}, - }, - }, - expectedBackends: map[string]*types.Backend{ - "backend-app-service-web": { - Servers: map[string]types.Server{ - "server-app-taskID-service-web": { - URL: "http://127.0.0.1:80", - Weight: label.DefaultWeight, - }, - }, - MaxConn: &types.MaxConn{ - Amount: 1000, - ExtractorFunc: "client.ip", - }, - }, - "backend-app-service-admin": { - Servers: map[string]types.Server{ - "server-app-taskID-service-admin": { - URL: "http://127.0.0.1:81", - Weight: label.DefaultWeight, - }, - }, - MaxConn: &types.MaxConn{ - Amount: 1000, - ExtractorFunc: "client.ip", - }, - }, - }, - }, - { - desc: "when all labels are set", - tasks: []state.Task{ - aTask("app-taskID", - withIP("127.0.0.1"), - withInfo("/app", - withPorts( - withPort("TCP", 80, "web"), - withPort("TCP", 81, "admin"), - ), - ), - withStatus(withHealthy(true), withState("TASK_RUNNING")), - - withLabel(label.TraefikBackendCircuitBreakerExpression, "NetworkErrorRatio() > 0.5"), - withLabel(label.TraefikBackendHealthCheckScheme, "http"), - withLabel(label.TraefikBackendHealthCheckPath, "/health"), - withLabel(label.TraefikBackendHealthCheckPort, "880"), - withLabel(label.TraefikBackendHealthCheckInterval, "6"), - withLabel(label.TraefikBackendHealthCheckTimeout, "3"), - withLabel(label.TraefikBackendHealthCheckHostname, "foo.com"), - withLabel(label.TraefikBackendHealthCheckHeaders, "Foo:bar || Bar:foo"), - withLabel(label.TraefikBackendLoadBalancerMethod, "drr"), - withLabel(label.TraefikBackendLoadBalancerStickiness, "true"), - withLabel(label.TraefikBackendLoadBalancerStickinessCookieName, "chocolate"), - withLabel(label.TraefikBackendMaxConnAmount, "666"), - withLabel(label.TraefikBackendMaxConnExtractorFunc, "client.ip"), - withLabel(label.TraefikBackendBufferingMaxResponseBodyBytes, "10485760"), - withLabel(label.TraefikBackendBufferingMemResponseBodyBytes, "2097152"), - withLabel(label.TraefikBackendBufferingMaxRequestBodyBytes, "10485760"), - withLabel(label.TraefikBackendBufferingMemRequestBodyBytes, "2097152"), - withLabel(label.TraefikBackendBufferingRetryExpression, "IsNetworkError() && Attempts() <= 2"), - - withSegmentLabel(label.TraefikPort, "80", "containous"), - withSegmentLabel(label.TraefikPortName, "web", "containous"), - withSegmentLabel(label.TraefikProtocol, "https", "containous"), - withSegmentLabel(label.TraefikWeight, "12", "containous"), - - withSegmentLabel(label.TraefikFrontendPassTLSClientCertPem, "true", "containous"), - withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosNotBefore, "true", "containous"), - withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosNotAfter, "true", "containous"), - withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSans, "true", "containous"), - withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectCommonName, "true", "containous"), - withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectCountry, "true", "containous"), - withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectLocality, "true", "containous"), - withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectOrganization, "true", "containous"), - withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectProvince, "true", "containous"), - withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectSerialNumber, "true", "containous"), - - withSegmentLabel(label.TraefikFrontendAuthBasic, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", "containous"), - withSegmentLabel(label.TraefikFrontendAuthBasicRemoveHeader, "true", "containous"), - withSegmentLabel(label.TraefikFrontendAuthBasicUsers, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", "containous"), - withSegmentLabel(label.TraefikFrontendAuthBasicUsersFile, ".htpasswd", "containous"), - withSegmentLabel(label.TraefikFrontendAuthDigestRemoveHeader, "true", "containous"), - withSegmentLabel(label.TraefikFrontendAuthDigestUsers, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", "containous"), - withSegmentLabel(label.TraefikFrontendAuthDigestUsersFile, ".htpasswd", "containous"), - withSegmentLabel(label.TraefikFrontendAuthForwardAddress, "auth.server", "containous"), - withSegmentLabel(label.TraefikFrontendAuthForwardTrustForwardHeader, "true", "containous"), - withSegmentLabel(label.TraefikFrontendAuthForwardTLSCa, "ca.crt", "containous"), - withSegmentLabel(label.TraefikFrontendAuthForwardTLSCaOptional, "true", "containous"), - withSegmentLabel(label.TraefikFrontendAuthForwardTLSCert, "server.crt", "containous"), - withSegmentLabel(label.TraefikFrontendAuthForwardTLSKey, "server.key", "containous"), - withSegmentLabel(label.TraefikFrontendAuthForwardTLSInsecureSkipVerify, "true", "containous"), - withSegmentLabel(label.TraefikFrontendAuthHeaderField, "X-WebAuth-User", "containous"), - - withSegmentLabel(label.TraefikFrontendEntryPoints, "http,https", "containous"), - withSegmentLabel(label.TraefikFrontendPassHostHeader, "true", "containous"), - withSegmentLabel(label.TraefikFrontendPassTLSCert, "true", "containous"), - withSegmentLabel(label.TraefikFrontendPriority, "666", "containous"), - withSegmentLabel(label.TraefikFrontendRedirectEntryPoint, "https", "containous"), - withSegmentLabel(label.TraefikFrontendRedirectRegex, "nope", "containous"), - withSegmentLabel(label.TraefikFrontendRedirectReplacement, "nope", "containous"), - withSegmentLabel(label.TraefikFrontendRedirectPermanent, "true", "containous"), - withSegmentLabel(label.TraefikFrontendRule, "Host:traefik.io", "containous"), - withSegmentLabel(label.TraefikFrontendWhiteListSourceRange, "10.10.10.10", "containous"), - - withSegmentLabel(label.TraefikFrontendRequestHeaders, "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8", "containous"), - withSegmentLabel(label.TraefikFrontendResponseHeaders, "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8", "containous"), - withSegmentLabel(label.TraefikFrontendSSLProxyHeaders, "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8", "containous"), - withSegmentLabel(label.TraefikFrontendAllowedHosts, "foo,bar,bor", "containous"), - withSegmentLabel(label.TraefikFrontendHostsProxyHeaders, "foo,bar,bor", "containous"), - withSegmentLabel(label.TraefikFrontendSSLForceHost, "true", "containous"), - withSegmentLabel(label.TraefikFrontendSSLHost, "foo", "containous"), - withSegmentLabel(label.TraefikFrontendCustomFrameOptionsValue, "foo", "containous"), - withSegmentLabel(label.TraefikFrontendContentSecurityPolicy, "foo", "containous"), - withSegmentLabel(label.TraefikFrontendPublicKey, "foo", "containous"), - withSegmentLabel(label.TraefikFrontendReferrerPolicy, "foo", "containous"), - withSegmentLabel(label.TraefikFrontendCustomBrowserXSSValue, "foo", "containous"), - withSegmentLabel(label.TraefikFrontendSTSSeconds, "666", "containous"), - withSegmentLabel(label.TraefikFrontendSSLRedirect, "true", "containous"), - withSegmentLabel(label.TraefikFrontendSSLTemporaryRedirect, "true", "containous"), - withSegmentLabel(label.TraefikFrontendSTSIncludeSubdomains, "true", "containous"), - withSegmentLabel(label.TraefikFrontendSTSPreload, "true", "containous"), - withSegmentLabel(label.TraefikFrontendForceSTSHeader, "true", "containous"), - withSegmentLabel(label.TraefikFrontendFrameDeny, "true", "containous"), - withSegmentLabel(label.TraefikFrontendContentTypeNosniff, "true", "containous"), - withSegmentLabel(label.TraefikFrontendBrowserXSSFilter, "true", "containous"), - withSegmentLabel(label.TraefikFrontendIsDevelopment, "true", "containous"), - - withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"foo."+label.SuffixErrorPageStatus, "404"), - withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"foo."+label.SuffixErrorPageBackend, "foobar"), - withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"foo."+label.SuffixErrorPageQuery, "foo_query"), - withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"bar."+label.SuffixErrorPageStatus, "500,600"), - withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"bar."+label.SuffixErrorPageBackend, "foobar"), - withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"bar."+label.SuffixErrorPageQuery, "bar_query"), - - withSegmentLabel(label.TraefikFrontendRateLimitExtractorFunc, "client.ip", "containous"), - withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"foo."+label.SuffixRateLimitPeriod, "6"), - withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"foo."+label.SuffixRateLimitAverage, "12"), - withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"foo."+label.SuffixRateLimitBurst, "18"), - withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"bar."+label.SuffixRateLimitPeriod, "3"), - withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"bar."+label.SuffixRateLimitAverage, "6"), - withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"bar."+label.SuffixRateLimitBurst, "9"), - ), - }, - expectedFrontends: map[string]*types.Frontend{ - "frontend-app-taskID-service-containous": { - EntryPoints: []string{ - "http", - "https", - }, - Backend: "backend-app-service-containous", - Routes: map[string]types.Route{ - "route-host-app-taskID-service-containous": { - Rule: "Host:traefik.io", - }, - }, - PassHostHeader: true, - PassTLSCert: true, - Priority: 666, - PassTLSClientCert: &types.TLSClientHeaders{ - PEM: true, - Infos: &types.TLSClientCertificateInfos{ - NotBefore: true, - Sans: true, - NotAfter: true, - Subject: &types.TLSCLientCertificateSubjectInfos{ - CommonName: true, - Country: true, - Locality: true, - Organization: true, - Province: true, - SerialNumber: true, - }, - }, - }, - Auth: &types.Auth{ - HeaderField: "X-WebAuth-User", - Basic: &types.Basic{ - RemoveHeader: true, - Users: []string{"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/", - "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"}, - UsersFile: ".htpasswd", - }, - }, - - WhiteList: &types.WhiteList{ - SourceRange: []string{"10.10.10.10"}, - }, - Headers: &types.Headers{ - CustomRequestHeaders: map[string]string{ - "Access-Control-Allow-Methods": "POST,GET,OPTIONS", - "Content-Type": "application/json; charset=utf-8", - }, - CustomResponseHeaders: map[string]string{ - "Access-Control-Allow-Methods": "POST,GET,OPTIONS", - "Content-Type": "application/json; charset=utf-8", - }, - AllowedHosts: []string{ - "foo", - "bar", - "bor", - }, - HostsProxyHeaders: []string{ - "foo", - "bar", - "bor", - }, - SSLRedirect: true, - SSLTemporaryRedirect: true, - SSLForceHost: true, - SSLHost: "foo", - SSLProxyHeaders: map[string]string{ - "Access-Control-Allow-Methods": "POST,GET,OPTIONS", - "Content-Type": "application/json; charset=utf-8", - }, - STSSeconds: 666, - STSIncludeSubdomains: true, - STSPreload: true, - ForceSTSHeader: true, - FrameDeny: true, - CustomFrameOptionsValue: "foo", - ContentTypeNosniff: true, - BrowserXSSFilter: true, - CustomBrowserXSSValue: "foo", - ContentSecurityPolicy: "foo", - PublicKey: "foo", - ReferrerPolicy: "foo", - IsDevelopment: true, - }, - Errors: map[string]*types.ErrorPage{ - "bar": { - Status: []string{ - "500", - "600", - }, - Backend: "backend-foobar", - Query: "bar_query", - }, - "foo": { - Status: []string{ - "404", - }, - Backend: "backend-foobar", - Query: "foo_query", - }, - }, - RateLimit: &types.RateLimit{ - RateSet: map[string]*types.Rate{ - "bar": { - Period: parse.Duration(3 * time.Second), - Average: 6, - Burst: 9, - }, - "foo": { - Period: parse.Duration(6 * time.Second), - Average: 12, - Burst: 18, - }, - }, - ExtractorFunc: "client.ip", - }, - Redirect: &types.Redirect{ - EntryPoint: "https", - Permanent: true, - }, - }, - }, - expectedBackends: map[string]*types.Backend{ - "backend-app-service-containous": { - Servers: map[string]types.Server{ - "server-app-taskID-service-containous": { - URL: "https://127.0.0.1:80", - Weight: 12, - }, - }, - CircuitBreaker: &types.CircuitBreaker{ - Expression: "NetworkErrorRatio() > 0.5", - }, - LoadBalancer: &types.LoadBalancer{ - Method: "drr", - Stickiness: &types.Stickiness{ - CookieName: "chocolate", - }, - }, - MaxConn: &types.MaxConn{ - Amount: 666, - ExtractorFunc: "client.ip", - }, - HealthCheck: &types.HealthCheck{ - Scheme: "http", - Path: "/health", - Port: 880, - Interval: "6", - Timeout: "3", - Hostname: "foo.com", - Headers: map[string]string{ - "Bar": "foo", - "Foo": "bar", - }, - }, - Buffering: &types.Buffering{ - MaxResponseBodyBytes: 10485760, - MemResponseBodyBytes: 2097152, - MaxRequestBodyBytes: 10485760, - MemRequestBodyBytes: 2097152, - RetryExpression: "IsNetworkError() && Attempts() <= 2", - }, - }, - }, - }, - } - - for _, test := range testCases { - test := test - t.Run(test.desc, func(t *testing.T) { - t.Parallel() - - actualConfig := p.buildConfiguration(test.tasks) - - require.NotNil(t, actualConfig) - assert.Equal(t, test.expectedBackends, actualConfig.Backends) - assert.Equal(t, test.expectedFrontends, actualConfig.Frontends) - }) - } -} - func TestTaskFilter(t *testing.T) { testCases := []struct { desc string diff --git a/provider/rancher/config_test.go b/provider/rancher/config_test.go index 4cf393ae1..2f82c14c3 100644 --- a/provider/rancher/config_test.go +++ b/provider/rancher/config_test.go @@ -683,6 +683,7 @@ func TestProviderBuildConfiguration(t *testing.T) { label.TraefikFrontendAuthForwardTLSKey: "server.key", label.TraefikFrontendAuthForwardTLSInsecureSkipVerify: "true", label.TraefikFrontendAuthHeaderField: "X-WebAuth-User", + label.TraefikFrontendAuthForwardAuthResponseHeaders: "X-Auth-User,X-Auth-Token", }, Health: "healthy", Containers: []string{"127.0.0.1"}, @@ -696,8 +697,7 @@ func TestProviderBuildConfiguration(t *testing.T) { Auth: &types.Auth{ HeaderField: "X-WebAuth-User", Forward: &types.Forward{ - Address: "auth.server", - TrustForwardHeader: true, + Address: "auth.server", TLS: &types.ClientTLS{ CA: "ca.crt", CAOptional: true, @@ -705,6 +705,8 @@ func TestProviderBuildConfiguration(t *testing.T) { Cert: "server.crt", Key: "server.key", }, + TrustForwardHeader: true, + AuthResponseHeaders: []string{"X-Auth-User", "X-Auth-Token"}, }, }, Priority: 0, diff --git a/safe/routine.go b/safe/routine.go index bb2f4fec8..9e810f621 100644 --- a/safe/routine.go +++ b/safe/routine.go @@ -145,7 +145,7 @@ func GoWithRecover(goroutine func(), customRecover func(err interface{})) { func defaultRecoverGoroutine(err interface{}) { log.Errorf("Error in Go routine: %s", err) - debug.PrintStack() + log.Errorf("Stack: %s", debug.Stack()) } // OperationWithRecover wrap a backoff operation in a Recover diff --git a/safe/routine_test.go b/safe/routine_test.go index 6d207dac3..288b9c9b0 100644 --- a/safe/routine_test.go +++ b/safe/routine_test.go @@ -13,9 +13,12 @@ import ( func TestNewPoolContext(t *testing.T) { type testKeyType string testKey := testKeyType("test") + ctx := context.WithValue(context.Background(), testKey, "test") p := NewPool(ctx) + retCtx := p.Ctx() + retCtxVal, ok := retCtx.Value(testKey).(string) if !ok || retCtxVal != "test" { t.Errorf("Pool.Ctx() did not return a derived context, got %#v, expected context with test value", retCtx) @@ -52,7 +55,8 @@ func (tr *fakeRoutine) routine(stop chan bool) { func TestPoolWithCtx(t *testing.T) { testRoutine := newFakeRoutine() - tt := []struct { + + testCases := []struct { desc string fn func(*Pool) }{ @@ -70,19 +74,20 @@ func TestPoolWithCtx(t *testing.T) { }, }, } - for _, tc := range tt { - tc := tc - t.Run(tc.desc, func(t *testing.T) { + + for _, test := range testCases { + test := test + t.Run(test.desc, func(t *testing.T) { // These subtests cannot be run in parallel, since the testRoutine // is shared across the subtests. p := NewPool(context.Background()) timer := time.NewTimer(500 * time.Millisecond) defer timer.Stop() - tc.fn(p) + test.fn(p) defer p.Cleanup() if len(p.routinesCtx) != 1 { - t.Fatalf("After %s, Pool did have %d goroutineCtxs, expected 1", tc.desc, len(p.routinesCtx)) + t.Fatalf("After %s, Pool did have %d goroutineCtxs, expected 1", test.desc, len(p.routinesCtx)) } testDone := make(chan bool, 1) @@ -91,6 +96,7 @@ func TestPoolWithCtx(t *testing.T) { p.Cleanup() testDone <- true }() + select { case <-timer.C: testRoutine.Lock() @@ -105,8 +111,9 @@ func TestPoolWithCtx(t *testing.T) { func TestPoolWithStopChan(t *testing.T) { testRoutine := newFakeRoutine() - ctx := context.Background() - p := NewPool(ctx) + + p := NewPool(context.Background()) + timer := time.NewTimer(500 * time.Millisecond) defer timer.Stop() @@ -121,6 +128,7 @@ func TestPoolWithStopChan(t *testing.T) { p.Cleanup() testDone <- true }() + select { case <-timer.C: testRoutine.Lock() @@ -133,8 +141,9 @@ func TestPoolWithStopChan(t *testing.T) { func TestPoolStartWithStopChan(t *testing.T) { testRoutine := newFakeRoutine() - ctx := context.Background() - p := NewPool(ctx) + + p := NewPool(context.Background()) + timer := time.NewTimer(500 * time.Millisecond) defer timer.Stop() diff --git a/safe/safe_test.go b/safe/safe_test.go index b5f6f30c3..242ce5509 100644 --- a/safe/safe_test.go +++ b/safe/safe_test.go @@ -5,19 +5,25 @@ import "testing" func TestSafe(t *testing.T) { const ts1 = "test1" const ts2 = "test2" + s := New(ts1) + result, ok := s.Get().(string) if !ok { t.Fatalf("Safe.Get() failed, got type '%T', expected string", s.Get()) } + if result != ts1 { t.Errorf("Safe.Get() failed, got '%s', expected '%s'", result, ts1) } + s.Set(ts2) + result, ok = s.Get().(string) if !ok { t.Fatalf("Safe.Get() after Safe.Set() failed, got type '%T', expected string", s.Get()) } + if result != ts2 { t.Errorf("Safe.Get() after Safe.Set() failed, got '%s', expected '%s'", result, ts2) } diff --git a/script/prune-dep.sh b/script/prune-dep.sh index 0ca0861ee..701d8a97e 100755 --- a/script/prune-dep.sh +++ b/script/prune-dep.sh @@ -32,7 +32,7 @@ find vendor -type f \( ! -iname 'licen[cs]e*' \ -a ! -iname '*.hxx' \ -a ! -iname '*.s' \) -exec rm -f {} + -find -type d \( -iname '*Godeps*' \) -exec rm -rf {} + +find . -type d \( -iname '*Godeps*' \) -exec rm -rf {} + find vendor -type l \( ! -iname 'licen[cs]e*' \ -a ! -iname '*notice*' \ @@ -55,4 +55,4 @@ find vendor -type l \( ! -iname 'licen[cs]e*' \ -a ! -iname '*.hh' \ -a ! -iname '*.hpp' \ -a ! -iname '*.hxx' \ - -a ! -iname '*.s' \) -exec rm -f {} + \ No newline at end of file + -a ! -iname '*.s' \) -exec rm -f {} + diff --git a/templates/consul_catalog.tmpl b/templates/consul_catalog.tmpl index 7f11887c8..fd8f6416e 100644 --- a/templates/consul_catalog.tmpl +++ b/templates/consul_catalog.tmpl @@ -106,6 +106,11 @@ [frontends."frontend-{{ $service.ServiceName }}".auth.forward] address = "{{ $auth.Forward.Address }}" trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }} + {{if $auth.Forward.AuthResponseHeaders }} + authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }} + "{{.}}", + {{end}}] + {{end}} {{if $auth.Forward.TLS }} [frontends."frontend-{{ $service.ServiceName }}".auth.forward.tls] diff --git a/templates/docker.tmpl b/templates/docker.tmpl index e41a4c57d..e10893277 100644 --- a/templates/docker.tmpl +++ b/templates/docker.tmpl @@ -107,6 +107,11 @@ [frontends."frontend-{{ $frontendName }}".auth.forward] address = "{{ $auth.Forward.Address }}" trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }} + {{if $auth.Forward.AuthResponseHeaders }} + authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }} + "{{.}}", + {{end}}] + {{end}} {{if $auth.Forward.TLS }} [frontends."frontend-{{ $frontendName }}".auth.forward.tls] diff --git a/templates/ecs.tmpl b/templates/ecs.tmpl index 6c485e612..f9c8f3675 100644 --- a/templates/ecs.tmpl +++ b/templates/ecs.tmpl @@ -108,6 +108,11 @@ [frontends."frontend-{{ $frontendName }}".auth.forward] address = "{{ $auth.Forward.Address }}" trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }} + {{if $auth.Forward.AuthResponseHeaders }} + authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }} + "{{.}}", + {{end}}] + {{end}} {{if $auth.Forward.TLS }} [frontends."frontend-{{ $frontendName }}".auth.forward.tls] diff --git a/templates/kv.tmpl b/templates/kv.tmpl index 1904b16fe..01191f0b8 100644 --- a/templates/kv.tmpl +++ b/templates/kv.tmpl @@ -106,6 +106,11 @@ [frontends."{{ $frontendName }}".auth.forward] address = "{{ $auth.Forward.Address }}" trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }} + {{if $auth.Forward.AuthResponseHeaders }} + authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }} + "{{.}}", + {{end}}] + {{end}} {{if $auth.Forward.TLS }} [frontends."{{ $frontendName }}".auth.forward.tls] diff --git a/templates/marathon.tmpl b/templates/marathon.tmpl index c36b20a35..071d91383 100644 --- a/templates/marathon.tmpl +++ b/templates/marathon.tmpl @@ -109,6 +109,11 @@ [frontends."{{ $frontendName }}".auth.forward] address = "{{ $auth.Forward.Address }}" trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }} + {{if $auth.Forward.AuthResponseHeaders }} + authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }} + "{{.}}", + {{end}}] + {{end}} {{if $auth.Forward.TLS }} [frontends."{{ $frontendName }}".auth.forward.tls] diff --git a/templates/mesos.tmpl b/templates/mesos.tmpl index 04a9cef79..0854e76e5 100644 --- a/templates/mesos.tmpl +++ b/templates/mesos.tmpl @@ -109,6 +109,11 @@ [frontends."frontend-{{ $frontendName }}".auth.forward] address = "{{ $auth.Forward.Address }}" trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }} + {{if $auth.Forward.AuthResponseHeaders }} + authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }} + "{{.}}", + {{end}}] + {{end}} {{if $auth.Forward.TLS }} [frontends."frontend-{{ $frontendName }}".auth.forward.tls] diff --git a/templates/rancher.tmpl b/templates/rancher.tmpl index 0749d93e1..9dce141e1 100644 --- a/templates/rancher.tmpl +++ b/templates/rancher.tmpl @@ -107,6 +107,11 @@ [frontends."frontend-{{ $frontendName }}".auth.forward] address = "{{ $auth.Forward.Address }}" trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }} + {{if $auth.Forward.AuthResponseHeaders }} + authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }} + "{{.}}", + {{end}}] + {{end}} {{if $auth.Forward.TLS }} [frontends."frontend-{{ $frontendName }}".auth.forward.tls] diff --git a/vendor/github.com/cloudflare/cloudflare-go/LICENSE b/vendor/github.com/cloudflare/cloudflare-go/LICENSE new file mode 100644 index 000000000..c035bb48d --- /dev/null +++ b/vendor/github.com/cloudflare/cloudflare-go/LICENSE @@ -0,0 +1,26 @@ +Copyright (c) 2015-2016, Cloudflare. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this +list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, +this list of conditions and the following disclaimer in the documentation and/or +other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors +may be used to endorse or promote products derived from this software without +specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/vendor/github.com/cloudflare/cloudflare-go/cloudflare.go b/vendor/github.com/cloudflare/cloudflare-go/cloudflare.go new file mode 100644 index 000000000..818cdf8ae --- /dev/null +++ b/vendor/github.com/cloudflare/cloudflare-go/cloudflare.go @@ -0,0 +1,318 @@ +// Package cloudflare implements the Cloudflare v4 API. +package cloudflare + +import ( + "bytes" + "context" + "encoding/json" + "io" + "io/ioutil" + "log" + "math" + "net/http" + "strings" + "time" + + "github.com/pkg/errors" + "golang.org/x/time/rate" +) + +const apiURL = "https://api.cloudflare.com/client/v4" +const ( + // AuthKeyEmail specifies that we should authenticate with API key and email address + AuthKeyEmail = 1 << iota + // AuthUserService specifies that we should authenticate with a User-Service key + AuthUserService +) + +// API holds the configuration for the current API client. A client should not +// be modified concurrently. +type API struct { + APIKey string + APIEmail string + APIUserServiceKey string + BaseURL string + organizationID string + headers http.Header + httpClient *http.Client + authType int + rateLimiter *rate.Limiter + retryPolicy RetryPolicy + logger Logger +} + +// New creates a new Cloudflare v4 API client. +func New(key, email string, opts ...Option) (*API, error) { + if key == "" || email == "" { + return nil, errors.New(errEmptyCredentials) + } + + silentLogger := log.New(ioutil.Discard, "", log.LstdFlags) + + api := &API{ + APIKey: key, + APIEmail: email, + BaseURL: apiURL, + headers: make(http.Header), + authType: AuthKeyEmail, + rateLimiter: rate.NewLimiter(rate.Limit(4), 1), // 4rps equates to default api limit (1200 req/5 min) + retryPolicy: RetryPolicy{ + MaxRetries: 3, + MinRetryDelay: time.Duration(1) * time.Second, + MaxRetryDelay: time.Duration(30) * time.Second, + }, + logger: silentLogger, + } + + err := api.parseOptions(opts...) + if err != nil { + return nil, errors.Wrap(err, "options parsing failed") + } + + // Fall back to http.DefaultClient if the package user does not provide + // their own. + if api.httpClient == nil { + api.httpClient = http.DefaultClient + } + + return api, nil +} + +// SetAuthType sets the authentication method (AuthyKeyEmail or AuthUserService). +func (api *API) SetAuthType(authType int) { + api.authType = authType +} + +// ZoneIDByName retrieves a zone's ID from the name. +func (api *API) ZoneIDByName(zoneName string) (string, error) { + res, err := api.ListZones(zoneName) + if err != nil { + return "", errors.Wrap(err, "ListZones command failed") + } + for _, zone := range res { + if zone.Name == zoneName { + return zone.ID, nil + } + } + return "", errors.New("Zone could not be found") +} + +// makeRequest makes a HTTP request and returns the body as a byte slice, +// closing it before returnng. params will be serialized to JSON. +func (api *API) makeRequest(method, uri string, params interface{}) ([]byte, error) { + return api.makeRequestWithAuthType(method, uri, params, api.authType) +} + +func (api *API) makeRequestWithAuthType(method, uri string, params interface{}, authType int) ([]byte, error) { + // Replace nil with a JSON object if needed + var jsonBody []byte + var err error + if params != nil { + jsonBody, err = json.Marshal(params) + if err != nil { + return nil, errors.Wrap(err, "error marshalling params to JSON") + } + } else { + jsonBody = nil + } + + var resp *http.Response + var respErr error + var reqBody io.Reader + var respBody []byte + for i := 0; i <= api.retryPolicy.MaxRetries; i++ { + if jsonBody != nil { + reqBody = bytes.NewReader(jsonBody) + } + + if i > 0 { + // expect the backoff introduced here on errored requests to dominate the effect of rate limiting + // dont need a random component here as the rate limiter should do something similar + // nb time duration could truncate an arbitrary float. Since our inputs are all ints, we should be ok + sleepDuration := time.Duration(math.Pow(2, float64(i-1)) * float64(api.retryPolicy.MinRetryDelay)) + + if sleepDuration > api.retryPolicy.MaxRetryDelay { + sleepDuration = api.retryPolicy.MaxRetryDelay + } + // useful to do some simple logging here, maybe introduce levels later + api.logger.Printf("Sleeping %s before retry attempt number %d for request %s %s", sleepDuration.String(), i, method, uri) + time.Sleep(sleepDuration) + } + api.rateLimiter.Wait(context.TODO()) + if err != nil { + return nil, errors.Wrap(err, "Error caused by request rate limiting") + } + resp, respErr = api.request(method, uri, reqBody, authType) + + // retry if the server is rate limiting us or if it failed + // assumes server operations are rolled back on failure + if respErr != nil || resp.StatusCode == http.StatusTooManyRequests || resp.StatusCode >= 500 { + // if we got a valid http response, try to read body so we can reuse the connection + // see https://golang.org/pkg/net/http/#Client.Do + if respErr == nil { + respBody, err = ioutil.ReadAll(resp.Body) + resp.Body.Close() + + respErr = errors.Wrap(err, "could not read response body") + + api.logger.Printf("Request: %s %s got an error response %d: %s\n", method, uri, resp.StatusCode, + strings.Replace(strings.Replace(string(respBody), "\n", "", -1), "\t", "", -1)) + } else { + api.logger.Printf("Error performing request: %s %s : %s \n", method, uri, respErr.Error()) + } + continue + } else { + respBody, err = ioutil.ReadAll(resp.Body) + defer resp.Body.Close() + if err != nil { + return nil, errors.Wrap(err, "could not read response body") + } + break + } + } + if respErr != nil { + return nil, respErr + } + + switch { + case resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices: + case resp.StatusCode == http.StatusUnauthorized: + return nil, errors.Errorf("HTTP status %d: invalid credentials", resp.StatusCode) + case resp.StatusCode == http.StatusForbidden: + return nil, errors.Errorf("HTTP status %d: insufficient permissions", resp.StatusCode) + case resp.StatusCode == http.StatusServiceUnavailable, + resp.StatusCode == http.StatusBadGateway, + resp.StatusCode == http.StatusGatewayTimeout, + resp.StatusCode == 522, + resp.StatusCode == 523, + resp.StatusCode == 524: + return nil, errors.Errorf("HTTP status %d: service failure", resp.StatusCode) + default: + var s string + if respBody != nil { + s = string(respBody) + } + return nil, errors.Errorf("HTTP status %d: content %q", resp.StatusCode, s) + } + + return respBody, nil +} + +// request makes a HTTP request to the given API endpoint, returning the raw +// *http.Response, or an error if one occurred. The caller is responsible for +// closing the response body. +func (api *API) request(method, uri string, reqBody io.Reader, authType int) (*http.Response, error) { + req, err := http.NewRequest(method, api.BaseURL+uri, reqBody) + if err != nil { + return nil, errors.Wrap(err, "HTTP request creation failed") + } + + // Apply any user-defined headers first. + req.Header = cloneHeader(api.headers) + if authType&AuthKeyEmail != 0 { + req.Header.Set("X-Auth-Key", api.APIKey) + req.Header.Set("X-Auth-Email", api.APIEmail) + } + if authType&AuthUserService != 0 { + req.Header.Set("X-Auth-User-Service-Key", api.APIUserServiceKey) + } + + if req.Header.Get("Content-Type") == "" { + req.Header.Set("Content-Type", "application/json") + } + + resp, err := api.httpClient.Do(req) + if err != nil { + return nil, errors.Wrap(err, "HTTP request failed") + } + + return resp, nil +} + +// Returns the base URL to use for API endpoints that exist for both accounts and organizations. +// If an Organization option was used when creating the API instance, returns the org URL. +// +// accountBase is the base URL for endpoints referring to the current user. It exists as a +// parameter because it is not consistent across APIs. +func (api *API) userBaseURL(accountBase string) string { + if api.organizationID != "" { + return "/organizations/" + api.organizationID + } + return accountBase +} + +// cloneHeader returns a shallow copy of the header. +// copied from https://godoc.org/github.com/golang/gddo/httputil/header#Copy +func cloneHeader(header http.Header) http.Header { + h := make(http.Header) + for k, vs := range header { + h[k] = vs + } + return h +} + +// ResponseInfo contains a code and message returned by the API as errors or +// informational messages inside the response. +type ResponseInfo struct { + Code int `json:"code"` + Message string `json:"message"` +} + +// Response is a template. There will also be a result struct. There will be a +// unique response type for each response, which will include this type. +type Response struct { + Success bool `json:"success"` + Errors []ResponseInfo `json:"errors"` + Messages []ResponseInfo `json:"messages"` +} + +// ResultInfo contains metadata about the Response. +type ResultInfo struct { + Page int `json:"page"` + PerPage int `json:"per_page"` + TotalPages int `json:"total_pages"` + Count int `json:"count"` + Total int `json:"total_count"` +} + +// RawResponse keeps the result as JSON form +type RawResponse struct { + Response + Result json.RawMessage `json:"result"` +} + +// Raw makes a HTTP request with user provided params and returns the +// result as untouched JSON. +func (api *API) Raw(method, endpoint string, data interface{}) (json.RawMessage, error) { + res, err := api.makeRequest(method, endpoint, data) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + var r RawResponse + if err := json.Unmarshal(res, &r); err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// PaginationOptions can be passed to a list request to configure paging +// These values will be defaulted if omitted, and PerPage has min/max limits set by resource +type PaginationOptions struct { + Page int `json:"page,omitempty"` + PerPage int `json:"per_page,omitempty"` +} + +// RetryPolicy specifies number of retries and min/max retry delays +// This config is used when the client exponentially backs off after errored requests +type RetryPolicy struct { + MaxRetries int + MinRetryDelay time.Duration + MaxRetryDelay time.Duration +} + +// Logger defines the interface this library needs to use logging +// This is a subset of the methods implemented in the log package +type Logger interface { + Printf(format string, v ...interface{}) +} diff --git a/vendor/github.com/cloudflare/cloudflare-go/cpage.go b/vendor/github.com/cloudflare/cloudflare-go/cpage.go new file mode 100644 index 000000000..87e50ce84 --- /dev/null +++ b/vendor/github.com/cloudflare/cloudflare-go/cpage.go @@ -0,0 +1,29 @@ +package cloudflare + +import "time" + +// CustomPage represents a custom page configuration. +type CustomPage struct { + CreatedOn string `json:"created_on"` + ModifiedOn time.Time `json:"modified_on"` + URL string `json:"url"` + State string `json:"state"` + RequiredTokens []string `json:"required_tokens"` + PreviewTarget string `json:"preview_target"` + Description string `json:"description"` +} + +// CustomPageResponse represents the response from the custom pages endpoint. +type CustomPageResponse struct { + Response + Result []CustomPage `json:"result"` +} + +// https://api.cloudflare.com/#custom-pages-for-a-zone-available-custom-pages +// GET /zones/:zone_identifier/custom_pages + +// https://api.cloudflare.com/#custom-pages-for-a-zone-custom-page-details +// GET /zones/:zone_identifier/custom_pages/:identifier + +// https://api.cloudflare.com/#custom-pages-for-a-zone-update-custom-page-url +// PUT /zones/:zone_identifier/custom_pages/:identifier diff --git a/vendor/github.com/cloudflare/cloudflare-go/custom_hostname.go b/vendor/github.com/cloudflare/cloudflare-go/custom_hostname.go new file mode 100644 index 000000000..44dbc4068 --- /dev/null +++ b/vendor/github.com/cloudflare/cloudflare-go/custom_hostname.go @@ -0,0 +1,149 @@ +package cloudflare + +import ( + "encoding/json" + "net/url" + "strconv" + + "github.com/pkg/errors" +) + +// CustomHostnameSSL represents the SSL section in a given custom hostname. +type CustomHostnameSSL struct { + Status string `json:"status,omitempty"` + Method string `json:"method,omitempty"` + Type string `json:"type,omitempty"` + CnameTarget string `json:"cname_target,omitempty"` + CnameName string `json:"cname_name,omitempty"` +} + +// CustomMetadata defines custom metadata for the hostname. This requires logic to be implemented by Cloudflare to act on the data provided. +type CustomMetadata map[string]interface{} + +// CustomHostname represents a custom hostname in a zone. +type CustomHostname struct { + ID string `json:"id,omitempty"` + Hostname string `json:"hostname,omitempty"` + SSL CustomHostnameSSL `json:"ssl,omitempty"` + CustomMetadata CustomMetadata `json:"custom_metadata,omitempty"` +} + +// CustomHostNameResponse represents a response from the Custom Hostnames endpoints. +type CustomHostnameResponse struct { + Result CustomHostname `json:"result"` + Response +} + +// CustomHostnameListResponse represents a response from the Custom Hostnames endpoints. +type CustomHostnameListResponse struct { + Result []CustomHostname `json:"result"` + Response + ResultInfo `json:"result_info"` +} + +// Modify SSL configuration for the given custom hostname in the given zone. +// +// API reference: https://api.cloudflare.com/#custom-hostname-for-a-zone-update-custom-hostname-configuration +func (api *API) UpdateCustomHostnameSSL(zoneID string, customHostnameID string, ssl CustomHostnameSSL) (CustomHostname, error) { + return CustomHostname{}, errors.New("Not implemented") +} + +// Delete a custom hostname (and any issued SSL certificates) +// +// API reference: https://api.cloudflare.com/#custom-hostname-for-a-zone-delete-a-custom-hostname-and-any-issued-ssl-certificates- +func (api *API) DeleteCustomHostname(zoneID string, customHostnameID string) error { + uri := "/zones/" + zoneID + "/custom_hostnames/" + customHostnameID + res, err := api.makeRequest("DELETE", uri, nil) + if err != nil { + return errors.Wrap(err, errMakeRequestError) + } + + var response *CustomHostnameResponse + err = json.Unmarshal(res, &response) + if err != nil { + return errors.Wrap(err, errUnmarshalError) + } + + return nil +} + +// CreateCustomHostname creates a new custom hostname and requests that an SSL certificate be issued for it. +// +// API reference: https://api.cloudflare.com/#custom-hostname-for-a-zone-create-custom-hostname +func (api *API) CreateCustomHostname(zoneID string, ch CustomHostname) (*CustomHostnameResponse, error) { + uri := "/zones/" + zoneID + "/custom_hostnames" + res, err := api.makeRequest("POST", uri, ch) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + var response *CustomHostnameResponse + err = json.Unmarshal(res, &response) + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + + return response, nil +} + +// CustomHostnames fetches custom hostnames for the given zone, +// by applying filter.Hostname if not empty and scoping the result to page'th 50 items. +// +// The returned ResultInfo can be used to implement pagination. +// +// API reference: https://api.cloudflare.com/#custom-hostname-for-a-zone-list-custom-hostnames +func (api *API) CustomHostnames(zoneID string, page int, filter CustomHostname) ([]CustomHostname, ResultInfo, error) { + v := url.Values{} + v.Set("per_page", "50") + v.Set("page", strconv.Itoa(page)) + if filter.Hostname != "" { + v.Set("hostname", filter.Hostname) + } + query := "?" + v.Encode() + + uri := "/zones/" + zoneID + "/custom_hostnames" + query + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return []CustomHostname{}, ResultInfo{}, errors.Wrap(err, errMakeRequestError) + } + var customHostnameListResponse CustomHostnameListResponse + err = json.Unmarshal(res, &customHostnameListResponse) + if err != nil { + return []CustomHostname{}, ResultInfo{}, errors.Wrap(err, errMakeRequestError) + } + + return customHostnameListResponse.Result, customHostnameListResponse.ResultInfo, nil +} + +// CustomHostname inspects the given custom hostname in the given zone. +// +// API reference: https://api.cloudflare.com/#custom-hostname-for-a-zone-custom-hostname-configuration-details +func (api *API) CustomHostname(zoneID string, customHostnameID string) (CustomHostname, error) { + uri := "/zones/" + zoneID + "/custom_hostnames/" + customHostnameID + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return CustomHostname{}, errors.Wrap(err, errMakeRequestError) + } + + var response CustomHostnameResponse + err = json.Unmarshal(res, &response) + if err != nil { + return CustomHostname{}, errors.Wrap(err, errUnmarshalError) + } + + return response.Result, nil +} + +// CustomHostnameIDByName retrieves the ID for the given hostname in the given zone. +func (api *API) CustomHostnameIDByName(zoneID string, hostname string) (string, error) { + customHostnames, _, err := api.CustomHostnames(zoneID, 1, CustomHostname{Hostname: hostname}) + if err != nil { + return "", errors.Wrap(err, "CustomHostnames command failed") + } + for _, ch := range customHostnames { + if ch.Hostname == hostname { + return ch.ID, nil + } + } + return "", errors.New("CustomHostname could not be found") +} diff --git a/vendor/github.com/cloudflare/cloudflare-go/dns.go b/vendor/github.com/cloudflare/cloudflare-go/dns.go new file mode 100644 index 000000000..3303e9d02 --- /dev/null +++ b/vendor/github.com/cloudflare/cloudflare-go/dns.go @@ -0,0 +1,174 @@ +package cloudflare + +import ( + "encoding/json" + "net/url" + "strconv" + "time" + + "github.com/pkg/errors" +) + +// DNSRecord represents a DNS record in a zone. +type DNSRecord struct { + ID string `json:"id,omitempty"` + Type string `json:"type,omitempty"` + Name string `json:"name,omitempty"` + Content string `json:"content,omitempty"` + Proxiable bool `json:"proxiable,omitempty"` + Proxied bool `json:"proxied,omitempty"` + TTL int `json:"ttl,omitempty"` + Locked bool `json:"locked,omitempty"` + ZoneID string `json:"zone_id,omitempty"` + ZoneName string `json:"zone_name,omitempty"` + CreatedOn time.Time `json:"created_on,omitempty"` + ModifiedOn time.Time `json:"modified_on,omitempty"` + Data interface{} `json:"data,omitempty"` // data returned by: SRV, LOC + Meta interface{} `json:"meta,omitempty"` + Priority int `json:"priority,omitempty"` +} + +// DNSRecordResponse represents the response from the DNS endpoint. +type DNSRecordResponse struct { + Result DNSRecord `json:"result"` + Response + ResultInfo `json:"result_info"` +} + +// DNSListResponse represents the response from the list DNS records endpoint. +type DNSListResponse struct { + Result []DNSRecord `json:"result"` + Response + ResultInfo `json:"result_info"` +} + +// CreateDNSRecord creates a DNS record for the zone identifier. +// +// API reference: https://api.cloudflare.com/#dns-records-for-a-zone-create-dns-record +func (api *API) CreateDNSRecord(zoneID string, rr DNSRecord) (*DNSRecordResponse, error) { + uri := "/zones/" + zoneID + "/dns_records" + res, err := api.makeRequest("POST", uri, rr) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + var recordResp *DNSRecordResponse + err = json.Unmarshal(res, &recordResp) + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + + return recordResp, nil +} + +// DNSRecords returns a slice of DNS records for the given zone identifier. +// +// This takes a DNSRecord to allow filtering of the results returned. +// +// API reference: https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records +func (api *API) DNSRecords(zoneID string, rr DNSRecord) ([]DNSRecord, error) { + // Construct a query string + v := url.Values{} + // Request as many records as possible per page - API max is 50 + v.Set("per_page", "50") + if rr.Name != "" { + v.Set("name", rr.Name) + } + if rr.Type != "" { + v.Set("type", rr.Type) + } + if rr.Content != "" { + v.Set("content", rr.Content) + } + + var query string + var records []DNSRecord + page := 1 + + // Loop over makeRequest until what we've fetched all records + for { + v.Set("page", strconv.Itoa(page)) + query = "?" + v.Encode() + uri := "/zones/" + zoneID + "/dns_records" + query + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return []DNSRecord{}, errors.Wrap(err, errMakeRequestError) + } + var r DNSListResponse + err = json.Unmarshal(res, &r) + if err != nil { + return []DNSRecord{}, errors.Wrap(err, errUnmarshalError) + } + records = append(records, r.Result...) + if r.ResultInfo.Page >= r.ResultInfo.TotalPages { + break + } + // Loop around and fetch the next page + page++ + } + return records, nil +} + +// DNSRecord returns a single DNS record for the given zone & record +// identifiers. +// +// API reference: https://api.cloudflare.com/#dns-records-for-a-zone-dns-record-details +func (api *API) DNSRecord(zoneID, recordID string) (DNSRecord, error) { + uri := "/zones/" + zoneID + "/dns_records/" + recordID + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return DNSRecord{}, errors.Wrap(err, errMakeRequestError) + } + var r DNSRecordResponse + err = json.Unmarshal(res, &r) + if err != nil { + return DNSRecord{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// UpdateDNSRecord updates a single DNS record for the given zone & record +// identifiers. +// +// API reference: https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record +func (api *API) UpdateDNSRecord(zoneID, recordID string, rr DNSRecord) error { + rec, err := api.DNSRecord(zoneID, recordID) + if err != nil { + return err + } + // Populate the record name from the existing one if the update didn't + // specify it. + if rr.Name == "" { + rr.Name = rec.Name + } + rr.Type = rec.Type + uri := "/zones/" + zoneID + "/dns_records/" + recordID + res, err := api.makeRequest("PUT", uri, rr) + if err != nil { + return errors.Wrap(err, errMakeRequestError) + } + var r DNSRecordResponse + err = json.Unmarshal(res, &r) + if err != nil { + return errors.Wrap(err, errUnmarshalError) + } + return nil +} + +// DeleteDNSRecord deletes a single DNS record for the given zone & record +// identifiers. +// +// API reference: https://api.cloudflare.com/#dns-records-for-a-zone-delete-dns-record +func (api *API) DeleteDNSRecord(zoneID, recordID string) error { + uri := "/zones/" + zoneID + "/dns_records/" + recordID + res, err := api.makeRequest("DELETE", uri, nil) + if err != nil { + return errors.Wrap(err, errMakeRequestError) + } + var r DNSRecordResponse + err = json.Unmarshal(res, &r) + if err != nil { + return errors.Wrap(err, errUnmarshalError) + } + return nil +} diff --git a/vendor/github.com/cloudflare/cloudflare-go/errors.go b/vendor/github.com/cloudflare/cloudflare-go/errors.go new file mode 100644 index 000000000..2fff3db30 --- /dev/null +++ b/vendor/github.com/cloudflare/cloudflare-go/errors.go @@ -0,0 +1,48 @@ +package cloudflare + +// Error messages +const ( + errEmptyCredentials = "invalid credentials: key & email must not be empty" + errMakeRequestError = "error from makeRequest" + errUnmarshalError = "error unmarshalling the JSON response" + errRequestNotSuccessful = "error reported by API" +) + +var _ Error = &UserError{} + +// Error represents an error returned from this library. +type Error interface { + error + // Raised when user credentials or configuration is invalid. + User() bool + // Raised when a parsing error (e.g. JSON) occurs. + Parse() bool + // Raised when a network error occurs. + Network() bool + // Contains the most recent error. +} + +// UserError represents a user-generated error. +type UserError struct { + Err error +} + +// User is a user-caused error. +func (e *UserError) User() bool { + return true +} + +// Network error. +func (e *UserError) Network() bool { + return false +} + +// Parse error. +func (e *UserError) Parse() bool { + return true +} + +// Error wraps the underlying error. +func (e *UserError) Error() string { + return e.Err.Error() +} diff --git a/vendor/github.com/cloudflare/cloudflare-go/firewall.go b/vendor/github.com/cloudflare/cloudflare-go/firewall.go new file mode 100644 index 000000000..9246b7e65 --- /dev/null +++ b/vendor/github.com/cloudflare/cloudflare-go/firewall.go @@ -0,0 +1,241 @@ +package cloudflare + +import ( + "encoding/json" + "net/url" + "strconv" + "time" + + "github.com/pkg/errors" +) + +// AccessRule represents a firewall access rule. +type AccessRule struct { + ID string `json:"id,omitempty"` + Notes string `json:"notes,omitempty"` + AllowedModes []string `json:"allowed_modes,omitempty"` + Mode string `json:"mode,omitempty"` + Configuration AccessRuleConfiguration `json:"configuration,omitempty"` + Scope AccessRuleScope `json:"scope,omitempty"` + CreatedOn time.Time `json:"created_on,omitempty"` + ModifiedOn time.Time `json:"modified_on,omitempty"` +} + +// AccessRuleConfiguration represents the configuration of a firewall +// access rule. +type AccessRuleConfiguration struct { + Target string `json:"target,omitempty"` + Value string `json:"value,omitempty"` +} + +// AccessRuleScope represents the scope of a firewall access rule. +type AccessRuleScope struct { + ID string `json:"id,omitempty"` + Email string `json:"email,omitempty"` + Name string `json:"name,omitempty"` + Type string `json:"type,omitempty"` +} + +// AccessRuleResponse represents the response from the firewall access +// rule endpoint. +type AccessRuleResponse struct { + Result AccessRule `json:"result"` + Response + ResultInfo `json:"result_info"` +} + +// AccessRuleListResponse represents the response from the list access rules +// endpoint. +type AccessRuleListResponse struct { + Result []AccessRule `json:"result"` + Response + ResultInfo `json:"result_info"` +} + +// ListUserAccessRules returns a slice of access rules for the logged-in user. +// +// This takes an AccessRule to allow filtering of the results returned. +// +// API reference: https://api.cloudflare.com/#user-level-firewall-access-rule-list-access-rules +func (api *API) ListUserAccessRules(accessRule AccessRule, page int) (*AccessRuleListResponse, error) { + return api.listAccessRules("/user", accessRule, page) +} + +// CreateUserAccessRule creates a firewall access rule for the logged-in user. +// +// API reference: https://api.cloudflare.com/#user-level-firewall-access-rule-create-access-rule +func (api *API) CreateUserAccessRule(accessRule AccessRule) (*AccessRuleResponse, error) { + return api.createAccessRule("/user", accessRule) +} + +// UpdateUserAccessRule updates a single access rule for the logged-in user & +// given access rule identifier. +// +// API reference: https://api.cloudflare.com/#user-level-firewall-access-rule-update-access-rule +func (api *API) UpdateUserAccessRule(accessRuleID string, accessRule AccessRule) (*AccessRuleResponse, error) { + return api.updateAccessRule("/user", accessRuleID, accessRule) +} + +// DeleteUserAccessRule deletes a single access rule for the logged-in user and +// access rule identifiers. +// +// API reference: https://api.cloudflare.com/#user-level-firewall-access-rule-update-access-rule +func (api *API) DeleteUserAccessRule(accessRuleID string) (*AccessRuleResponse, error) { + return api.deleteAccessRule("/user", accessRuleID) +} + +// ListZoneAccessRules returns a slice of access rules for the given zone +// identifier. +// +// This takes an AccessRule to allow filtering of the results returned. +// +// API reference: https://api.cloudflare.com/#firewall-access-rule-for-a-zone-list-access-rules +func (api *API) ListZoneAccessRules(zoneID string, accessRule AccessRule, page int) (*AccessRuleListResponse, error) { + return api.listAccessRules("/zones/"+zoneID, accessRule, page) +} + +// CreateZoneAccessRule creates a firewall access rule for the given zone +// identifier. +// +// API reference: https://api.cloudflare.com/#firewall-access-rule-for-a-zone-create-access-rule +func (api *API) CreateZoneAccessRule(zoneID string, accessRule AccessRule) (*AccessRuleResponse, error) { + return api.createAccessRule("/zones/"+zoneID, accessRule) +} + +// UpdateZoneAccessRule updates a single access rule for the given zone & +// access rule identifiers. +// +// API reference: https://api.cloudflare.com/#firewall-access-rule-for-a-zone-update-access-rule +func (api *API) UpdateZoneAccessRule(zoneID, accessRuleID string, accessRule AccessRule) (*AccessRuleResponse, error) { + return api.updateAccessRule("/zones/"+zoneID, accessRuleID, accessRule) +} + +// DeleteZoneAccessRule deletes a single access rule for the given zone and +// access rule identifiers. +// +// API reference: https://api.cloudflare.com/#firewall-access-rule-for-a-zone-delete-access-rule +func (api *API) DeleteZoneAccessRule(zoneID, accessRuleID string) (*AccessRuleResponse, error) { + return api.deleteAccessRule("/zones/"+zoneID, accessRuleID) +} + +// ListOrganizationAccessRules returns a slice of access rules for the given +// organization identifier. +// +// This takes an AccessRule to allow filtering of the results returned. +// +// API reference: https://api.cloudflare.com/#organization-level-firewall-access-rule-list-access-rules +func (api *API) ListOrganizationAccessRules(organizationID string, accessRule AccessRule, page int) (*AccessRuleListResponse, error) { + return api.listAccessRules("/organizations/"+organizationID, accessRule, page) +} + +// CreateOrganizationAccessRule creates a firewall access rule for the given +// organization identifier. +// +// API reference: https://api.cloudflare.com/#organization-level-firewall-access-rule-create-access-rule +func (api *API) CreateOrganizationAccessRule(organizationID string, accessRule AccessRule) (*AccessRuleResponse, error) { + return api.createAccessRule("/organizations/"+organizationID, accessRule) +} + +// UpdateOrganizationAccessRule updates a single access rule for the given +// organization & access rule identifiers. +// +// API reference: https://api.cloudflare.com/#organization-level-firewall-access-rule-update-access-rule +func (api *API) UpdateOrganizationAccessRule(organizationID, accessRuleID string, accessRule AccessRule) (*AccessRuleResponse, error) { + return api.updateAccessRule("/organizations/"+organizationID, accessRuleID, accessRule) +} + +// DeleteOrganizationAccessRule deletes a single access rule for the given +// organization and access rule identifiers. +// +// API reference: https://api.cloudflare.com/#organization-level-firewall-access-rule-delete-access-rule +func (api *API) DeleteOrganizationAccessRule(organizationID, accessRuleID string) (*AccessRuleResponse, error) { + return api.deleteAccessRule("/organizations/"+organizationID, accessRuleID) +} + +func (api *API) listAccessRules(prefix string, accessRule AccessRule, page int) (*AccessRuleListResponse, error) { + // Construct a query string + v := url.Values{} + if page <= 0 { + page = 1 + } + v.Set("page", strconv.Itoa(page)) + // Request as many rules as possible per page - API max is 100 + v.Set("per_page", "100") + if accessRule.Notes != "" { + v.Set("notes", accessRule.Notes) + } + if accessRule.Mode != "" { + v.Set("mode", accessRule.Mode) + } + if accessRule.Scope.Type != "" { + v.Set("scope_type", accessRule.Scope.Type) + } + if accessRule.Configuration.Value != "" { + v.Set("configuration_value", accessRule.Configuration.Value) + } + if accessRule.Configuration.Target != "" { + v.Set("configuration_target", accessRule.Configuration.Target) + } + v.Set("page", strconv.Itoa(page)) + query := "?" + v.Encode() + + uri := prefix + "/firewall/access_rules/rules" + query + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + response := &AccessRuleListResponse{} + err = json.Unmarshal(res, &response) + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + return response, nil +} + +func (api *API) createAccessRule(prefix string, accessRule AccessRule) (*AccessRuleResponse, error) { + uri := prefix + "/firewall/access_rules/rules" + res, err := api.makeRequest("POST", uri, accessRule) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + response := &AccessRuleResponse{} + err = json.Unmarshal(res, &response) + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + + return response, nil +} + +func (api *API) updateAccessRule(prefix, accessRuleID string, accessRule AccessRule) (*AccessRuleResponse, error) { + uri := prefix + "/firewall/access_rules/rules/" + accessRuleID + res, err := api.makeRequest("PATCH", uri, accessRule) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + response := &AccessRuleResponse{} + err = json.Unmarshal(res, &response) + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + return response, nil +} + +func (api *API) deleteAccessRule(prefix, accessRuleID string) (*AccessRuleResponse, error) { + uri := prefix + "/firewall/access_rules/rules/" + accessRuleID + res, err := api.makeRequest("DELETE", uri, nil) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + response := &AccessRuleResponse{} + err = json.Unmarshal(res, &response) + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + + return response, nil +} diff --git a/vendor/github.com/cloudflare/cloudflare-go/ips.go b/vendor/github.com/cloudflare/cloudflare-go/ips.go new file mode 100644 index 000000000..72b5fcfbc --- /dev/null +++ b/vendor/github.com/cloudflare/cloudflare-go/ips.go @@ -0,0 +1,44 @@ +package cloudflare + +import ( + "encoding/json" + "io/ioutil" + "net/http" + + "github.com/pkg/errors" +) + +// IPRanges contains lists of IPv4 and IPv6 CIDRs. +type IPRanges struct { + IPv4CIDRs []string `json:"ipv4_cidrs"` + IPv6CIDRs []string `json:"ipv6_cidrs"` +} + +// IPsResponse is the API response containing a list of IPs. +type IPsResponse struct { + Response + Result IPRanges `json:"result"` +} + +// IPs gets a list of Cloudflare's IP ranges. +// +// This does not require logging in to the API. +// +// API reference: https://api.cloudflare.com/#cloudflare-ips +func IPs() (IPRanges, error) { + resp, err := http.Get(apiURL + "/ips") + if err != nil { + return IPRanges{}, errors.Wrap(err, "HTTP request failed") + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return IPRanges{}, errors.Wrap(err, "Response body could not be read") + } + var r IPsResponse + err = json.Unmarshal(body, &r) + if err != nil { + return IPRanges{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} diff --git a/vendor/github.com/cloudflare/cloudflare-go/keyless.go b/vendor/github.com/cloudflare/cloudflare-go/keyless.go new file mode 100644 index 000000000..c5cc83914 --- /dev/null +++ b/vendor/github.com/cloudflare/cloudflare-go/keyless.go @@ -0,0 +1,52 @@ +package cloudflare + +import "time" + +// KeylessSSL represents Keyless SSL configuration. +type KeylessSSL struct { + ID string `json:"id"` + Name string `json:"name"` + Host string `json:"host"` + Port int `json:"port"` + Status string `json:"success"` + Enabled bool `json:"enabled"` + Permissions []string `json:"permissions"` + CreatedOn time.Time `json:"created_on"` + ModifiedOn time.Time `json:"modifed_on"` +} + +// KeylessSSLResponse represents the response from the Keyless SSL endpoint. +type KeylessSSLResponse struct { + Response + Result []KeylessSSL `json:"result"` +} + +// CreateKeyless creates a new Keyless SSL configuration for the zone. +// +// API reference: https://api.cloudflare.com/#keyless-ssl-for-a-zone-create-a-keyless-ssl-configuration +func (api *API) CreateKeyless() { +} + +// ListKeyless lists Keyless SSL configurations for a zone. +// +// API reference: https://api.cloudflare.com/#keyless-ssl-for-a-zone-list-keyless-ssls +func (api *API) ListKeyless() { +} + +// Keyless provides the configuration for a given Keyless SSL identifier. +// +// API reference: https://api.cloudflare.com/#keyless-ssl-for-a-zone-keyless-ssl-details +func (api *API) Keyless() { +} + +// UpdateKeyless updates an existing Keyless SSL configuration. +// +// API reference: https://api.cloudflare.com/#keyless-ssl-for-a-zone-update-keyless-configuration +func (api *API) UpdateKeyless() { +} + +// DeleteKeyless deletes an existing Keyless SSL configuration. +// +// API reference: https://api.cloudflare.com/#keyless-ssl-for-a-zone-delete-keyless-configuration +func (api *API) DeleteKeyless() { +} diff --git a/vendor/github.com/cloudflare/cloudflare-go/load_balancing.go b/vendor/github.com/cloudflare/cloudflare-go/load_balancing.go new file mode 100644 index 000000000..e1af07b57 --- /dev/null +++ b/vendor/github.com/cloudflare/cloudflare-go/load_balancing.go @@ -0,0 +1,330 @@ +package cloudflare + +import ( + "encoding/json" + "time" + + "github.com/pkg/errors" +) + +// LoadBalancerPool represents a load balancer pool's properties. +type LoadBalancerPool struct { + ID string `json:"id,omitempty"` + CreatedOn *time.Time `json:"created_on,omitempty"` + ModifiedOn *time.Time `json:"modified_on,omitempty"` + Description string `json:"description"` + Name string `json:"name"` + Enabled bool `json:"enabled"` + MinimumOrigins int `json:"minimum_origins,omitempty"` + Monitor string `json:"monitor,omitempty"` + Origins []LoadBalancerOrigin `json:"origins"` + NotificationEmail string `json:"notification_email,omitempty"` + + // CheckRegions defines the geographic region(s) from where to run health-checks from - e.g. "WNAM", "WEU", "SAF", "SAM". + // Providing a null/empty value means "all regions", which may not be available to all plan types. + CheckRegions []string `json:"check_regions"` +} + +type LoadBalancerOrigin struct { + Name string `json:"name"` + Address string `json:"address"` + Enabled bool `json:"enabled"` + Weight float64 `json:"weight"` +} + +// LoadBalancerMonitor represents a load balancer monitor's properties. +type LoadBalancerMonitor struct { + ID string `json:"id,omitempty"` + CreatedOn *time.Time `json:"created_on,omitempty"` + ModifiedOn *time.Time `json:"modified_on,omitempty"` + Type string `json:"type"` + Description string `json:"description"` + Method string `json:"method"` + Path string `json:"path"` + Header map[string][]string `json:"header"` + Timeout int `json:"timeout"` + Retries int `json:"retries"` + Interval int `json:"interval"` + ExpectedBody string `json:"expected_body"` + ExpectedCodes string `json:"expected_codes"` +} + +// LoadBalancer represents a load balancer's properties. +type LoadBalancer struct { + ID string `json:"id,omitempty"` + CreatedOn *time.Time `json:"created_on,omitempty"` + ModifiedOn *time.Time `json:"modified_on,omitempty"` + Description string `json:"description"` + Name string `json:"name"` + TTL int `json:"ttl,omitempty"` + FallbackPool string `json:"fallback_pool"` + DefaultPools []string `json:"default_pools"` + RegionPools map[string][]string `json:"region_pools"` + PopPools map[string][]string `json:"pop_pools"` + Proxied bool `json:"proxied"` + Persistence string `json:"session_affinity,omitempty"` +} + +// loadBalancerPoolResponse represents the response from the load balancer pool endpoints. +type loadBalancerPoolResponse struct { + Response + Result LoadBalancerPool `json:"result"` +} + +// loadBalancerPoolListResponse represents the response from the List Pools endpoint. +type loadBalancerPoolListResponse struct { + Response + Result []LoadBalancerPool `json:"result"` + ResultInfo ResultInfo `json:"result_info"` +} + +// loadBalancerMonitorResponse represents the response from the load balancer monitor endpoints. +type loadBalancerMonitorResponse struct { + Response + Result LoadBalancerMonitor `json:"result"` +} + +// loadBalancerMonitorListResponse represents the response from the List Monitors endpoint. +type loadBalancerMonitorListResponse struct { + Response + Result []LoadBalancerMonitor `json:"result"` + ResultInfo ResultInfo `json:"result_info"` +} + +// loadBalancerResponse represents the response from the load balancer endpoints. +type loadBalancerResponse struct { + Response + Result LoadBalancer `json:"result"` +} + +// loadBalancerListResponse represents the response from the List Load Balancers endpoint. +type loadBalancerListResponse struct { + Response + Result []LoadBalancer `json:"result"` + ResultInfo ResultInfo `json:"result_info"` +} + +// CreateLoadBalancerPool creates a new load balancer pool. +// +// API reference: https://api.cloudflare.com/#load-balancer-pools-create-a-pool +func (api *API) CreateLoadBalancerPool(pool LoadBalancerPool) (LoadBalancerPool, error) { + uri := api.userBaseURL("/user") + "/load_balancers/pools" + res, err := api.makeRequest("POST", uri, pool) + if err != nil { + return LoadBalancerPool{}, errors.Wrap(err, errMakeRequestError) + } + var r loadBalancerPoolResponse + if err := json.Unmarshal(res, &r); err != nil { + return LoadBalancerPool{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// ListLoadBalancerPools lists load balancer pools connected to an account. +// +// API reference: https://api.cloudflare.com/#load-balancer-pools-list-pools +func (api *API) ListLoadBalancerPools() ([]LoadBalancerPool, error) { + uri := api.userBaseURL("/user") + "/load_balancers/pools" + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + var r loadBalancerPoolListResponse + if err := json.Unmarshal(res, &r); err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// LoadBalancerPoolDetails returns the details for a load balancer pool. +// +// API reference: https://api.cloudflare.com/#load-balancer-pools-pool-details +func (api *API) LoadBalancerPoolDetails(poolID string) (LoadBalancerPool, error) { + uri := api.userBaseURL("/user") + "/load_balancers/pools/" + poolID + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return LoadBalancerPool{}, errors.Wrap(err, errMakeRequestError) + } + var r loadBalancerPoolResponse + if err := json.Unmarshal(res, &r); err != nil { + return LoadBalancerPool{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// DeleteLoadBalancerPool disables and deletes a load balancer pool. +// +// API reference: https://api.cloudflare.com/#load-balancer-pools-delete-a-pool +func (api *API) DeleteLoadBalancerPool(poolID string) error { + uri := api.userBaseURL("/user") + "/load_balancers/pools/" + poolID + if _, err := api.makeRequest("DELETE", uri, nil); err != nil { + return errors.Wrap(err, errMakeRequestError) + } + return nil +} + +// ModifyLoadBalancerPool modifies a configured load balancer pool. +// +// API reference: https://api.cloudflare.com/#load-balancer-pools-modify-a-pool +func (api *API) ModifyLoadBalancerPool(pool LoadBalancerPool) (LoadBalancerPool, error) { + uri := api.userBaseURL("/user") + "/load_balancers/pools/" + pool.ID + res, err := api.makeRequest("PUT", uri, pool) + if err != nil { + return LoadBalancerPool{}, errors.Wrap(err, errMakeRequestError) + } + var r loadBalancerPoolResponse + if err := json.Unmarshal(res, &r); err != nil { + return LoadBalancerPool{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// CreateLoadBalancerMonitor creates a new load balancer monitor. +// +// API reference: https://api.cloudflare.com/#load-balancer-monitors-create-a-monitor +func (api *API) CreateLoadBalancerMonitor(monitor LoadBalancerMonitor) (LoadBalancerMonitor, error) { + uri := api.userBaseURL("/user") + "/load_balancers/monitors" + res, err := api.makeRequest("POST", uri, monitor) + if err != nil { + return LoadBalancerMonitor{}, errors.Wrap(err, errMakeRequestError) + } + var r loadBalancerMonitorResponse + if err := json.Unmarshal(res, &r); err != nil { + return LoadBalancerMonitor{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// ListLoadBalancerMonitors lists load balancer monitors connected to an account. +// +// API reference: https://api.cloudflare.com/#load-balancer-monitors-list-monitors +func (api *API) ListLoadBalancerMonitors() ([]LoadBalancerMonitor, error) { + uri := api.userBaseURL("/user") + "/load_balancers/monitors" + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + var r loadBalancerMonitorListResponse + if err := json.Unmarshal(res, &r); err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// LoadBalancerMonitorDetails returns the details for a load balancer monitor. +// +// API reference: https://api.cloudflare.com/#load-balancer-monitors-monitor-details +func (api *API) LoadBalancerMonitorDetails(monitorID string) (LoadBalancerMonitor, error) { + uri := api.userBaseURL("/user") + "/load_balancers/monitors/" + monitorID + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return LoadBalancerMonitor{}, errors.Wrap(err, errMakeRequestError) + } + var r loadBalancerMonitorResponse + if err := json.Unmarshal(res, &r); err != nil { + return LoadBalancerMonitor{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// DeleteLoadBalancerMonitor disables and deletes a load balancer monitor. +// +// API reference: https://api.cloudflare.com/#load-balancer-monitors-delete-a-monitor +func (api *API) DeleteLoadBalancerMonitor(monitorID string) error { + uri := api.userBaseURL("/user") + "/load_balancers/monitors/" + monitorID + if _, err := api.makeRequest("DELETE", uri, nil); err != nil { + return errors.Wrap(err, errMakeRequestError) + } + return nil +} + +// ModifyLoadBalancerMonitor modifies a configured load balancer monitor. +// +// API reference: https://api.cloudflare.com/#load-balancer-monitors-modify-a-monitor +func (api *API) ModifyLoadBalancerMonitor(monitor LoadBalancerMonitor) (LoadBalancerMonitor, error) { + uri := api.userBaseURL("/user") + "/load_balancers/monitors/" + monitor.ID + res, err := api.makeRequest("PUT", uri, monitor) + if err != nil { + return LoadBalancerMonitor{}, errors.Wrap(err, errMakeRequestError) + } + var r loadBalancerMonitorResponse + if err := json.Unmarshal(res, &r); err != nil { + return LoadBalancerMonitor{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// CreateLoadBalancer creates a new load balancer. +// +// API reference: https://api.cloudflare.com/#load-balancers-create-a-load-balancer +func (api *API) CreateLoadBalancer(zoneID string, lb LoadBalancer) (LoadBalancer, error) { + uri := "/zones/" + zoneID + "/load_balancers" + res, err := api.makeRequest("POST", uri, lb) + if err != nil { + return LoadBalancer{}, errors.Wrap(err, errMakeRequestError) + } + var r loadBalancerResponse + if err := json.Unmarshal(res, &r); err != nil { + return LoadBalancer{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// ListLoadBalancers lists load balancers configured on a zone. +// +// API reference: https://api.cloudflare.com/#load-balancers-list-load-balancers +func (api *API) ListLoadBalancers(zoneID string) ([]LoadBalancer, error) { + uri := "/zones/" + zoneID + "/load_balancers" + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + var r loadBalancerListResponse + if err := json.Unmarshal(res, &r); err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// LoadBalancerDetails returns the details for a load balancer. +// +// API reference: https://api.cloudflare.com/#load-balancers-load-balancer-details +func (api *API) LoadBalancerDetails(zoneID, lbID string) (LoadBalancer, error) { + uri := "/zones/" + zoneID + "/load_balancers/" + lbID + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return LoadBalancer{}, errors.Wrap(err, errMakeRequestError) + } + var r loadBalancerResponse + if err := json.Unmarshal(res, &r); err != nil { + return LoadBalancer{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// DeleteLoadBalancer disables and deletes a load balancer. +// +// API reference: https://api.cloudflare.com/#load-balancers-delete-a-load-balancer +func (api *API) DeleteLoadBalancer(zoneID, lbID string) error { + uri := "/zones/" + zoneID + "/load_balancers/" + lbID + if _, err := api.makeRequest("DELETE", uri, nil); err != nil { + return errors.Wrap(err, errMakeRequestError) + } + return nil +} + +// ModifyLoadBalancer modifies a configured load balancer. +// +// API reference: https://api.cloudflare.com/#load-balancers-modify-a-load-balancer +func (api *API) ModifyLoadBalancer(zoneID string, lb LoadBalancer) (LoadBalancer, error) { + uri := "/zones/" + zoneID + "/load_balancers/" + lb.ID + res, err := api.makeRequest("PUT", uri, lb) + if err != nil { + return LoadBalancer{}, errors.Wrap(err, errMakeRequestError) + } + var r loadBalancerResponse + if err := json.Unmarshal(res, &r); err != nil { + return LoadBalancer{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} diff --git a/vendor/github.com/cloudflare/cloudflare-go/lockdown.go b/vendor/github.com/cloudflare/cloudflare-go/lockdown.go new file mode 100644 index 000000000..3dba81c21 --- /dev/null +++ b/vendor/github.com/cloudflare/cloudflare-go/lockdown.go @@ -0,0 +1,150 @@ +package cloudflare + +import ( + "encoding/json" + "net/url" + "strconv" + + "github.com/pkg/errors" +) + +// ZoneLockdown represents a Zone Lockdown rule. A rule only permits access to +// the provided URL pattern(s) from the given IP address(es) or subnet(s). +type ZoneLockdown struct { + ID string `json:"id"` + Description string `json:"description"` + URLs []string `json:"urls"` + Configurations []ZoneLockdownConfig `json:"configurations"` + Paused bool `json:"paused"` +} + +// ZoneLockdownConfig represents a Zone Lockdown config, which comprises +// a Target ("ip" or "ip_range") and a Value (an IP address or IP+mask, +// respectively.) +type ZoneLockdownConfig struct { + Target string `json:"target"` + Value string `json:"value"` +} + +// ZoneLockdownResponse represents a response from the Zone Lockdown endpoint. +type ZoneLockdownResponse struct { + Result ZoneLockdown `json:"result"` + Response + ResultInfo `json:"result_info"` +} + +// ZoneLockdownListResponse represents a response from the List Zone Lockdown +// endpoint. +type ZoneLockdownListResponse struct { + Result []ZoneLockdown `json:"result"` + Response + ResultInfo `json:"result_info"` +} + +// CreateZoneLockdown creates a Zone ZoneLockdown rule for the given zone ID. +// +// API reference: https://api.cloudflare.com/#zone-ZoneLockdown-create-a-ZoneLockdown-rule +func (api *API) CreateZoneLockdown(zoneID string, ld ZoneLockdown) (*ZoneLockdownResponse, error) { + uri := "/zones/" + zoneID + "/firewall/lockdowns" + res, err := api.makeRequest("POST", uri, ld) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + response := &ZoneLockdownResponse{} + err = json.Unmarshal(res, &response) + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + + return response, nil +} + +// UpdateZoneLockdown updates a Zone ZoneLockdown rule (based on the ID) for the +// given zone ID. +// +// API reference: https://api.cloudflare.com/#zone-ZoneLockdown-update-ZoneLockdown-rule +func (api *API) UpdateZoneLockdown(zoneID string, id string, ld ZoneLockdown) (*ZoneLockdownResponse, error) { + uri := "/zones/" + zoneID + "/firewall/lockdowns" + res, err := api.makeRequest("PUT", uri, ld) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + response := &ZoneLockdownResponse{} + err = json.Unmarshal(res, &response) + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + + return response, nil +} + +// DeleteZoneLockdown deletes a Zone ZoneLockdown rule (based on the ID) for the +// given zone ID. +// +// API reference: https://api.cloudflare.com/#zone-ZoneLockdown-delete-ZoneLockdown-rule +func (api *API) DeleteZoneLockdown(zoneID string, id string) (*ZoneLockdownResponse, error) { + uri := "/zones/" + zoneID + "/firewall/lockdowns/" + id + res, err := api.makeRequest("DELETE", uri, nil) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + response := &ZoneLockdownResponse{} + err = json.Unmarshal(res, &response) + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + + return response, nil +} + +// ZoneLockdown retrieves a Zone ZoneLockdown rule (based on the ID) for the +// given zone ID. +// +// API reference: https://api.cloudflare.com/#zone-ZoneLockdown-ZoneLockdown-rule-details +func (api *API) ZoneLockdown(zoneID string, id string) (*ZoneLockdownResponse, error) { + uri := "/zones/" + zoneID + "/firewall/lockdowns/" + id + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + response := &ZoneLockdownResponse{} + err = json.Unmarshal(res, &response) + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + + return response, nil +} + +// ListZoneLockdowns retrieves a list of Zone ZoneLockdown rules for a given +// zone ID by page number. +// +// API reference: https://api.cloudflare.com/#zone-ZoneLockdown-list-ZoneLockdown-rules +func (api *API) ListZoneLockdowns(zoneID string, page int) (*ZoneLockdownListResponse, error) { + v := url.Values{} + if page <= 0 { + page = 1 + } + + v.Set("page", strconv.Itoa(page)) + v.Set("per_page", strconv.Itoa(100)) + query := "?" + v.Encode() + + uri := "/zones/" + zoneID + "/firewall/lockdowns" + query + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + response := &ZoneLockdownListResponse{} + err = json.Unmarshal(res, &response) + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + + return response, nil +} diff --git a/vendor/github.com/cloudflare/cloudflare-go/options.go b/vendor/github.com/cloudflare/cloudflare-go/options.go new file mode 100644 index 000000000..1a113ee10 --- /dev/null +++ b/vendor/github.com/cloudflare/cloudflare-go/options.go @@ -0,0 +1,90 @@ +package cloudflare + +import ( + "net/http" + + "time" + + "golang.org/x/time/rate" +) + +// Option is a functional option for configuring the API client. +type Option func(*API) error + +// HTTPClient accepts a custom *http.Client for making API calls. +func HTTPClient(client *http.Client) Option { + return func(api *API) error { + api.httpClient = client + return nil + } +} + +// Headers allows you to set custom HTTP headers when making API calls (e.g. for +// satisfying HTTP proxies, or for debugging). +func Headers(headers http.Header) Option { + return func(api *API) error { + api.headers = headers + return nil + } +} + +// Organization allows you to apply account-level changes (Load Balancing, Railguns) +// to an organization instead. +func UsingOrganization(orgID string) Option { + return func(api *API) error { + api.organizationID = orgID + return nil + } +} + +// UsingRateLimit applies a non-default rate limit to client API requests +// If not specified the default of 4rps will be applied +func UsingRateLimit(rps float64) Option { + return func(api *API) error { + // because ratelimiter doesnt do any windowing + // setting burst makes it difficult to enforce a fixed rate + // so setting it equal to 1 this effectively disables bursting + // this doesn't check for sensible values, ultimately the api will enforce that the value is ok + api.rateLimiter = rate.NewLimiter(rate.Limit(rps), 1) + return nil + } +} + +// UsingRetryPolicy applies a non-default number of retries and min/max retry delays +// This will be used when the client exponentially backs off after errored requests +func UsingRetryPolicy(maxRetries int, minRetryDelaySecs int, maxRetryDelaySecs int) Option { + // seconds is very granular for a minimum delay - but this is only in case of failure + return func(api *API) error { + api.retryPolicy = RetryPolicy{ + MaxRetries: maxRetries, + MinRetryDelay: time.Duration(minRetryDelaySecs) * time.Second, + MaxRetryDelay: time.Duration(maxRetryDelaySecs) * time.Second, + } + return nil + } +} + +// UsingLogger can be set if you want to get log output from this API instance +// By default no log output is emitted +func UsingLogger(logger Logger) Option { + return func(api *API) error { + api.logger = logger + return nil + } +} + +// parseOptions parses the supplied options functions and returns a configured +// *API instance. +func (api *API) parseOptions(opts ...Option) error { + // Range over each options function and apply it to our API type to + // configure it. Options functions are applied in order, with any + // conflicting options overriding earlier calls. + for _, option := range opts { + err := option(api) + if err != nil { + return err + } + } + + return nil +} diff --git a/vendor/github.com/cloudflare/cloudflare-go/organizations.go b/vendor/github.com/cloudflare/cloudflare-go/organizations.go new file mode 100644 index 000000000..97a67e778 --- /dev/null +++ b/vendor/github.com/cloudflare/cloudflare-go/organizations.go @@ -0,0 +1,185 @@ +package cloudflare + +import ( + "encoding/json" + "time" + + "github.com/pkg/errors" +) + +// Organization represents a multi-user organization. +type Organization struct { + ID string `json:"id,omitempty"` + Name string `json:"name,omitempty"` + Status string `json:"status,omitempty"` + Permissions []string `json:"permissions,omitempty"` + Roles []string `json:"roles,omitempty"` +} + +// organizationResponse represents the response from the Organization endpoint. +type organizationResponse struct { + Response + Result []Organization `json:"result"` + ResultInfo `json:"result_info"` +} + +// OrganizationMember has details on a member. +type OrganizationMember struct { + ID string `json:"id,omitempty"` + Name string `json:"name,omitempty"` + Email string `json:"email,omitempty"` + Status string `json:"status,omitempty"` + Roles []OrganizationRole `json:"roles,omitempty"` +} + +// OrganizationInvite has details on an invite. +type OrganizationInvite struct { + ID string `json:"id,omitempty"` + InvitedMemberID string `json:"invited_member_id,omitempty"` + InvitedMemberEmail string `json:"invited_member_email,omitempty"` + OrganizationID string `json:"organization_id,omitempty"` + OrganizationName string `json:"organization_name,omitempty"` + Roles []OrganizationRole `json:"roles,omitempty"` + InvitedBy string `json:"invited_by,omitempty"` + InvitedOn *time.Time `json:"invited_on,omitempty"` + ExpiresOn *time.Time `json:"expires_on,omitempty"` + Status string `json:"status,omitempty"` +} + +// OrganizationRole has details on a role. +type OrganizationRole struct { + ID string `json:"id,omitempty"` + Name string `json:"name,omitempty"` + Description string `json:"description,omitempty"` + Permissions []string `json:"permissions,omitempty"` +} + +// OrganizationDetails represents details of an organization. +type OrganizationDetails struct { + ID string `json:"id,omitempty"` + Name string `json:"name,omitempty"` + Members []OrganizationMember `json:"members"` + Invites []OrganizationInvite `json:"invites"` + Roles []OrganizationRole `json:"roles,omitempty"` +} + +// organizationDetailsResponse represents the response from the OrganizationDetails endpoint. +type organizationDetailsResponse struct { + Response + Result OrganizationDetails `json:"result"` +} + +// ListOrganizations lists organizations of the logged-in user. +// +// API reference: https://api.cloudflare.com/#user-s-organizations-list-organizations +func (api *API) ListOrganizations() ([]Organization, ResultInfo, error) { + var r organizationResponse + res, err := api.makeRequest("GET", "/user/organizations", nil) + if err != nil { + return []Organization{}, ResultInfo{}, errors.Wrap(err, errMakeRequestError) + } + + err = json.Unmarshal(res, &r) + if err != nil { + return []Organization{}, ResultInfo{}, errors.Wrap(err, errUnmarshalError) + } + + return r.Result, r.ResultInfo, nil +} + +// OrganizationDetails returns details for the specified organization of the logged-in user. +// +// API reference: https://api.cloudflare.com/#organizations-organization-details +func (api *API) OrganizationDetails(organizationID string) (OrganizationDetails, error) { + var r organizationDetailsResponse + uri := "/organizations/" + organizationID + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return OrganizationDetails{}, errors.Wrap(err, errMakeRequestError) + } + + err = json.Unmarshal(res, &r) + if err != nil { + return OrganizationDetails{}, errors.Wrap(err, errUnmarshalError) + } + + return r.Result, nil +} + +// organizationMembersResponse represents the response from the Organization members endpoint. +type organizationMembersResponse struct { + Response + Result []OrganizationMember `json:"result"` + ResultInfo `json:"result_info"` +} + +// OrganizationMembers returns list of members for specified organization of the logged-in user. +// +// API reference: https://api.cloudflare.com/#organization-members-list-members +func (api *API) OrganizationMembers(organizationID string) ([]OrganizationMember, ResultInfo, error) { + var r organizationMembersResponse + uri := "/organizations/" + organizationID + "/members" + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return []OrganizationMember{}, ResultInfo{}, errors.Wrap(err, errMakeRequestError) + } + + err = json.Unmarshal(res, &r) + if err != nil { + return []OrganizationMember{}, ResultInfo{}, errors.Wrap(err, errUnmarshalError) + } + + return r.Result, r.ResultInfo, nil +} + +// organizationInvitesResponse represents the response from the Organization invites endpoint. +type organizationInvitesResponse struct { + Response + Result []OrganizationInvite `json:"result"` + ResultInfo `json:"result_info"` +} + +// OrganizationMembers returns list of invites for specified organization of the logged-in user. +// +// API reference: https://api.cloudflare.com/#organization-invites +func (api *API) OrganizationInvites(organizationID string) ([]OrganizationInvite, ResultInfo, error) { + var r organizationInvitesResponse + uri := "/organizations/" + organizationID + "/invites" + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return []OrganizationInvite{}, ResultInfo{}, errors.Wrap(err, errMakeRequestError) + } + + err = json.Unmarshal(res, &r) + if err != nil { + return []OrganizationInvite{}, ResultInfo{}, errors.Wrap(err, errUnmarshalError) + } + + return r.Result, r.ResultInfo, nil +} + +// organizationRolesResponse represents the response from the Organization roles endpoint. +type organizationRolesResponse struct { + Response + Result []OrganizationRole `json:"result"` + ResultInfo `json:"result_info"` +} + +// OrganizationRoles returns list of roles for specified organization of the logged-in user. +// +// API reference: https://api.cloudflare.com/#organization-roles-list-roles +func (api *API) OrganizationRoles(organizationID string) ([]OrganizationRole, ResultInfo, error) { + var r organizationRolesResponse + uri := "/organizations/" + organizationID + "/roles" + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return []OrganizationRole{}, ResultInfo{}, errors.Wrap(err, errMakeRequestError) + } + + err = json.Unmarshal(res, &r) + if err != nil { + return []OrganizationRole{}, ResultInfo{}, errors.Wrap(err, errUnmarshalError) + } + + return r.Result, r.ResultInfo, nil +} diff --git a/vendor/github.com/cloudflare/cloudflare-go/origin_ca.go b/vendor/github.com/cloudflare/cloudflare-go/origin_ca.go new file mode 100644 index 000000000..486033afd --- /dev/null +++ b/vendor/github.com/cloudflare/cloudflare-go/origin_ca.go @@ -0,0 +1,168 @@ +package cloudflare + +import ( + "encoding/json" + "net/url" + "time" + + "github.com/pkg/errors" +) + +// OriginCACertificate represents a Cloudflare-issued certificate. +// +// API reference: https://api.cloudflare.com/#cloudflare-ca +type OriginCACertificate struct { + ID string `json:"id"` + Certificate string `json:"certificate"` + Hostnames []string `json:"hostnames"` + ExpiresOn time.Time `json:"expires_on"` + RequestType string `json:"request_type"` + RequestValidity int `json:"requested_validity"` + CSR string `json:"csr"` +} + +// OriginCACertificateListOptions represents the parameters used to list Cloudflare-issued certificates. +type OriginCACertificateListOptions struct { + ZoneID string +} + +// OriginCACertificateID represents the ID of the revoked certificate from the Revoke Certificate endpoint. +type OriginCACertificateID struct { + ID string `json:"id"` +} + +// originCACertificateResponse represents the response from the Create Certificate and the Certificate Details endpoints. +type originCACertificateResponse struct { + Response + Result OriginCACertificate `json:"result"` +} + +// originCACertificateResponseList represents the response from the List Certificates endpoint. +type originCACertificateResponseList struct { + Response + Result []OriginCACertificate `json:"result"` + ResultInfo ResultInfo `json:"result_info"` +} + +// originCACertificateResponseRevoke represents the response from the Revoke Certificate endpoint. +type originCACertificateResponseRevoke struct { + Response + Result OriginCACertificateID `json:"result"` +} + +// CreateOriginCertificate creates a Cloudflare-signed certificate. +// +// This function requires api.APIUserServiceKey be set to your Certificates API key. +// +// API reference: https://api.cloudflare.com/#cloudflare-ca-create-certificate +func (api *API) CreateOriginCertificate(certificate OriginCACertificate) (*OriginCACertificate, error) { + uri := "/certificates" + res, err := api.makeRequestWithAuthType("POST", uri, certificate, AuthUserService) + + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + var originResponse *originCACertificateResponse + + err = json.Unmarshal(res, &originResponse) + + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + + if !originResponse.Success { + return nil, errors.New(errRequestNotSuccessful) + } + + return &originResponse.Result, nil +} + +// OriginCertificates lists all Cloudflare-issued certificates. +// +// This function requires api.APIUserServiceKey be set to your Certificates API key. +// +// API reference: https://api.cloudflare.com/#cloudflare-ca-list-certificates +func (api *API) OriginCertificates(options OriginCACertificateListOptions) ([]OriginCACertificate, error) { + v := url.Values{} + if options.ZoneID != "" { + v.Set("zone_id", options.ZoneID) + } + uri := "/certificates" + "?" + v.Encode() + res, err := api.makeRequestWithAuthType("GET", uri, nil, AuthUserService) + + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + var originResponse *originCACertificateResponseList + + err = json.Unmarshal(res, &originResponse) + + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + + if !originResponse.Success { + return nil, errors.New(errRequestNotSuccessful) + } + + return originResponse.Result, nil +} + +// OriginCertificate returns the details for a Cloudflare-issued certificate. +// +// This function requires api.APIUserServiceKey be set to your Certificates API key. +// +// API reference: https://api.cloudflare.com/#cloudflare-ca-certificate-details +func (api *API) OriginCertificate(certificateID string) (*OriginCACertificate, error) { + uri := "/certificates/" + certificateID + res, err := api.makeRequestWithAuthType("GET", uri, nil, AuthUserService) + + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + var originResponse *originCACertificateResponse + + err = json.Unmarshal(res, &originResponse) + + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + + if !originResponse.Success { + return nil, errors.New(errRequestNotSuccessful) + } + + return &originResponse.Result, nil +} + +// RevokeOriginCertificate revokes a created certificate for a zone. +// +// This function requires api.APIUserServiceKey be set to your Certificates API key. +// +// API reference: https://api.cloudflare.com/#cloudflare-ca-revoke-certificate +func (api *API) RevokeOriginCertificate(certificateID string) (*OriginCACertificateID, error) { + uri := "/certificates/" + certificateID + res, err := api.makeRequestWithAuthType("DELETE", uri, nil, AuthUserService) + + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + var originResponse *originCACertificateResponseRevoke + + err = json.Unmarshal(res, &originResponse) + + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + + if !originResponse.Success { + return nil, errors.New(errRequestNotSuccessful) + } + + return &originResponse.Result, nil + +} diff --git a/vendor/github.com/cloudflare/cloudflare-go/page_rules.go b/vendor/github.com/cloudflare/cloudflare-go/page_rules.go new file mode 100644 index 000000000..8749b2c51 --- /dev/null +++ b/vendor/github.com/cloudflare/cloudflare-go/page_rules.go @@ -0,0 +1,206 @@ +package cloudflare + +import ( + "encoding/json" + "time" + + "github.com/pkg/errors" +) + +// PageRuleTarget is the target to evaluate on a request. +// +// Currently Target must always be "url" and Operator must be "matches". Value +// is the URL pattern to match against. +type PageRuleTarget struct { + Target string `json:"target"` + Constraint struct { + Operator string `json:"operator"` + Value string `json:"value"` + } `json:"constraint"` +} + +/* +PageRuleAction is the action to take when the target is matched. + +Valid IDs are: + + always_online + always_use_https + browser_cache_ttl + browser_check + cache_level + disable_apps + disable_performance + disable_railgun + disable_security + edge_cache_ttl + email_obfuscation + forwarding_url + ip_geolocation + mirage + rocket_loader + security_level + server_side_exclude + smart_errors + ssl + waf +*/ +type PageRuleAction struct { + ID string `json:"id"` + Value interface{} `json:"value"` +} + +// PageRuleActions maps API action IDs to human-readable strings. +var PageRuleActions = map[string]string{ + "always_online": "Always Online", // Value of type string + "always_use_https": "Always Use HTTPS", // Value of type interface{} + "browser_cache_ttl": "Browser Cache TTL", // Value of type int + "browser_check": "Browser Integrity Check", // Value of type string + "cache_level": "Cache Level", // Value of type string + "disable_apps": "Disable Apps", // Value of type interface{} + "disable_performance": "Disable Performance", // Value of type interface{} + "disable_railgun": "Disable Railgun", // Value of type string + "disable_security": "Disable Security", // Value of type interface{} + "edge_cache_ttl": "Edge Cache TTL", // Value of type int + "email_obfuscation": "Email Obfuscation", // Value of type string + "forwarding_url": "Forwarding URL", // Value of type map[string]interface + "ip_geolocation": "IP Geolocation Header", // Value of type string + "mirage": "Mirage", // Value of type string + "rocket_loader": "Rocker Loader", // Value of type string + "security_level": "Security Level", // Value of type string + "server_side_exclude": "Server Side Excludes", // Value of type string + "smart_errors": "Smart Errors", // Value of type string + "ssl": "SSL", // Value of type string + "waf": "Web Application Firewall", // Value of type string +} + +// PageRule describes a Page Rule. +type PageRule struct { + ID string `json:"id,omitempty"` + Targets []PageRuleTarget `json:"targets"` + Actions []PageRuleAction `json:"actions"` + Priority int `json:"priority"` + Status string `json:"status"` // can be: active, paused + ModifiedOn time.Time `json:"modified_on,omitempty"` + CreatedOn time.Time `json:"created_on,omitempty"` +} + +// PageRuleDetailResponse is the API response, containing a single PageRule. +type PageRuleDetailResponse struct { + Success bool `json:"success"` + Errors []string `json:"errors"` + Messages []string `json:"messages"` + Result PageRule `json:"result"` +} + +// PageRulesResponse is the API response, containing an array of PageRules. +type PageRulesResponse struct { + Success bool `json:"success"` + Errors []string `json:"errors"` + Messages []string `json:"messages"` + Result []PageRule `json:"result"` +} + +// CreatePageRule creates a new Page Rule for a zone. +// +// API reference: https://api.cloudflare.com/#page-rules-for-a-zone-create-a-page-rule +func (api *API) CreatePageRule(zoneID string, rule PageRule) (*PageRule, error) { + uri := "/zones/" + zoneID + "/pagerules" + res, err := api.makeRequest("POST", uri, rule) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + var r PageRuleDetailResponse + err = json.Unmarshal(res, &r) + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + return &r.Result, nil +} + +// ListPageRules returns all Page Rules for a zone. +// +// API reference: https://api.cloudflare.com/#page-rules-for-a-zone-list-page-rules +func (api *API) ListPageRules(zoneID string) ([]PageRule, error) { + uri := "/zones/" + zoneID + "/pagerules" + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return []PageRule{}, errors.Wrap(err, errMakeRequestError) + } + var r PageRulesResponse + err = json.Unmarshal(res, &r) + if err != nil { + return []PageRule{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// PageRule fetches detail about one Page Rule for a zone. +// +// API reference: https://api.cloudflare.com/#page-rules-for-a-zone-page-rule-details +func (api *API) PageRule(zoneID, ruleID string) (PageRule, error) { + uri := "/zones/" + zoneID + "/pagerules/" + ruleID + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return PageRule{}, errors.Wrap(err, errMakeRequestError) + } + var r PageRuleDetailResponse + err = json.Unmarshal(res, &r) + if err != nil { + return PageRule{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// ChangePageRule lets you change individual settings for a Page Rule. This is +// in contrast to UpdatePageRule which replaces the entire Page Rule. +// +// API reference: https://api.cloudflare.com/#page-rules-for-a-zone-change-a-page-rule +func (api *API) ChangePageRule(zoneID, ruleID string, rule PageRule) error { + uri := "/zones/" + zoneID + "/pagerules/" + ruleID + res, err := api.makeRequest("PATCH", uri, rule) + if err != nil { + return errors.Wrap(err, errMakeRequestError) + } + var r PageRuleDetailResponse + err = json.Unmarshal(res, &r) + if err != nil { + return errors.Wrap(err, errUnmarshalError) + } + return nil +} + +// UpdatePageRule lets you replace a Page Rule. This is in contrast to +// ChangePageRule which lets you change individual settings. +// +// API reference: https://api.cloudflare.com/#page-rules-for-a-zone-update-a-page-rule +func (api *API) UpdatePageRule(zoneID, ruleID string, rule PageRule) error { + uri := "/zones/" + zoneID + "/pagerules/" + ruleID + res, err := api.makeRequest("PUT", uri, nil) + if err != nil { + return errors.Wrap(err, errMakeRequestError) + } + var r PageRuleDetailResponse + err = json.Unmarshal(res, &r) + if err != nil { + return errors.Wrap(err, errUnmarshalError) + } + return nil +} + +// DeletePageRule deletes a Page Rule for a zone. +// +// API reference: https://api.cloudflare.com/#page-rules-for-a-zone-delete-a-page-rule +func (api *API) DeletePageRule(zoneID, ruleID string) error { + uri := "/zones/" + zoneID + "/pagerules/" + ruleID + res, err := api.makeRequest("DELETE", uri, nil) + if err != nil { + return errors.Wrap(err, errMakeRequestError) + } + var r PageRuleDetailResponse + err = json.Unmarshal(res, &r) + if err != nil { + return errors.Wrap(err, errUnmarshalError) + } + return nil +} diff --git a/vendor/github.com/cloudflare/cloudflare-go/railgun.go b/vendor/github.com/cloudflare/cloudflare-go/railgun.go new file mode 100644 index 000000000..72d228691 --- /dev/null +++ b/vendor/github.com/cloudflare/cloudflare-go/railgun.go @@ -0,0 +1,297 @@ +package cloudflare + +import ( + "encoding/json" + "net/url" + "time" + + "github.com/pkg/errors" +) + +// Railgun represents a Railgun's properties. +type Railgun struct { + ID string `json:"id"` + Name string `json:"name"` + Status string `json:"status"` + Enabled bool `json:"enabled"` + ZonesConnected int `json:"zones_connected"` + Build string `json:"build"` + Version string `json:"version"` + Revision string `json:"revision"` + ActivationKey string `json:"activation_key"` + ActivatedOn time.Time `json:"activated_on"` + CreatedOn time.Time `json:"created_on"` + ModifiedOn time.Time `json:"modified_on"` + UpgradeInfo struct { + LatestVersion string `json:"latest_version"` + DownloadLink string `json:"download_link"` + } `json:"upgrade_info"` +} + +// RailgunListOptions represents the parameters used to list railguns. +type RailgunListOptions struct { + Direction string +} + +// railgunResponse represents the response from the Create Railgun and the Railgun Details endpoints. +type railgunResponse struct { + Response + Result Railgun `json:"result"` +} + +// railgunsResponse represents the response from the List Railguns endpoint. +type railgunsResponse struct { + Response + Result []Railgun `json:"result"` +} + +// CreateRailgun creates a new Railgun. +// +// API reference: https://api.cloudflare.com/#railgun-create-railgun +func (api *API) CreateRailgun(name string) (Railgun, error) { + uri := api.userBaseURL("") + "/railguns" + params := struct { + Name string `json:"name"` + }{ + Name: name, + } + res, err := api.makeRequest("POST", uri, params) + if err != nil { + return Railgun{}, errors.Wrap(err, errMakeRequestError) + } + var r railgunResponse + if err := json.Unmarshal(res, &r); err != nil { + return Railgun{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// ListRailguns lists Railguns connected to an account. +// +// API reference: https://api.cloudflare.com/#railgun-list-railguns +func (api *API) ListRailguns(options RailgunListOptions) ([]Railgun, error) { + v := url.Values{} + if options.Direction != "" { + v.Set("direction", options.Direction) + } + uri := api.userBaseURL("") + "/railguns" + "?" + v.Encode() + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + var r railgunsResponse + if err := json.Unmarshal(res, &r); err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// RailgunDetails returns the details for a Railgun. +// +// API reference: https://api.cloudflare.com/#railgun-railgun-details +func (api *API) RailgunDetails(railgunID string) (Railgun, error) { + uri := api.userBaseURL("") + "/railguns/" + railgunID + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return Railgun{}, errors.Wrap(err, errMakeRequestError) + } + var r railgunResponse + if err := json.Unmarshal(res, &r); err != nil { + return Railgun{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// RailgunZones returns the zones that are currently using a Railgun. +// +// API reference: https://api.cloudflare.com/#railgun-get-zones-connected-to-a-railgun +func (api *API) RailgunZones(railgunID string) ([]Zone, error) { + uri := api.userBaseURL("") + "/railguns/" + railgunID + "/zones" + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + var r ZonesResponse + if err := json.Unmarshal(res, &r); err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// enableRailgun enables (true) or disables (false) a Railgun for all zones connected to it. +// +// API reference: https://api.cloudflare.com/#railgun-enable-or-disable-a-railgun +func (api *API) enableRailgun(railgunID string, enable bool) (Railgun, error) { + uri := api.userBaseURL("") + "/railguns/" + railgunID + params := struct { + Enabled bool `json:"enabled"` + }{ + Enabled: enable, + } + res, err := api.makeRequest("PATCH", uri, params) + if err != nil { + return Railgun{}, errors.Wrap(err, errMakeRequestError) + } + var r railgunResponse + if err := json.Unmarshal(res, &r); err != nil { + return Railgun{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// EnableRailgun enables a Railgun for all zones connected to it. +// +// API reference: https://api.cloudflare.com/#railgun-enable-or-disable-a-railgun +func (api *API) EnableRailgun(railgunID string) (Railgun, error) { + return api.enableRailgun(railgunID, true) +} + +// DisableRailgun enables a Railgun for all zones connected to it. +// +// API reference: https://api.cloudflare.com/#railgun-enable-or-disable-a-railgun +func (api *API) DisableRailgun(railgunID string) (Railgun, error) { + return api.enableRailgun(railgunID, false) +} + +// DeleteRailgun disables and deletes a Railgun. +// +// API reference: https://api.cloudflare.com/#railgun-delete-railgun +func (api *API) DeleteRailgun(railgunID string) error { + uri := api.userBaseURL("") + "/railguns/" + railgunID + if _, err := api.makeRequest("DELETE", uri, nil); err != nil { + return errors.Wrap(err, errMakeRequestError) + } + return nil +} + +// ZoneRailgun represents the status of a Railgun on a zone. +type ZoneRailgun struct { + ID string `json:"id"` + Name string `json:"name"` + Enabled bool `json:"enabled"` + Connected bool `json:"connected"` +} + +// zoneRailgunResponse represents the response from the Zone Railgun Details endpoint. +type zoneRailgunResponse struct { + Response + Result ZoneRailgun `json:"result"` +} + +// zoneRailgunsResponse represents the response from the Zone Railgun endpoint. +type zoneRailgunsResponse struct { + Response + Result []ZoneRailgun `json:"result"` +} + +// RailgunDiagnosis represents the test results from testing railgun connections +// to a zone. +type RailgunDiagnosis struct { + Method string `json:"method"` + HostName string `json:"host_name"` + HTTPStatus int `json:"http_status"` + Railgun string `json:"railgun"` + URL string `json:"url"` + ResponseStatus string `json:"response_status"` + Protocol string `json:"protocol"` + ElapsedTime string `json:"elapsed_time"` + BodySize string `json:"body_size"` + BodyHash string `json:"body_hash"` + MissingHeaders string `json:"missing_headers"` + ConnectionClose bool `json:"connection_close"` + Cloudflare string `json:"cloudflare"` + CFRay string `json:"cf-ray"` + // NOTE: Cloudflare's online API documentation does not yet have definitions + // for the following fields. See: https://api.cloudflare.com/#railgun-connections-for-a-zone-test-railgun-connection/ + CFWANError string `json:"cf-wan-error"` + CFCacheStatus string `json:"cf-cache-status"` +} + +// railgunDiagnosisResponse represents the response from the Test Railgun Connection enpoint. +type railgunDiagnosisResponse struct { + Response + Result RailgunDiagnosis `json:"result"` +} + +// ZoneRailguns returns the available Railguns for a zone. +// +// API reference: https://api.cloudflare.com/#railguns-for-a-zone-get-available-railguns +func (api *API) ZoneRailguns(zoneID string) ([]ZoneRailgun, error) { + uri := "/zones/" + zoneID + "/railguns" + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + var r zoneRailgunsResponse + if err := json.Unmarshal(res, &r); err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// ZoneRailgunDetails returns the configuration for a given Railgun. +// +// API reference: https://api.cloudflare.com/#railguns-for-a-zone-get-railgun-details +func (api *API) ZoneRailgunDetails(zoneID, railgunID string) (ZoneRailgun, error) { + uri := "/zones/" + zoneID + "/railguns/" + railgunID + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return ZoneRailgun{}, errors.Wrap(err, errMakeRequestError) + } + var r zoneRailgunResponse + if err := json.Unmarshal(res, &r); err != nil { + return ZoneRailgun{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// TestRailgunConnection tests a Railgun connection for a given zone. +// +// API reference: https://api.cloudflare.com/#railgun-connections-for-a-zone-test-railgun-connection +func (api *API) TestRailgunConnection(zoneID, railgunID string) (RailgunDiagnosis, error) { + uri := "/zones/" + zoneID + "/railguns/" + railgunID + "/diagnose" + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return RailgunDiagnosis{}, errors.Wrap(err, errMakeRequestError) + } + var r railgunDiagnosisResponse + if err := json.Unmarshal(res, &r); err != nil { + return RailgunDiagnosis{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// connectZoneRailgun connects (true) or disconnects (false) a Railgun for a given zone. +// +// API reference: https://api.cloudflare.com/#railguns-for-a-zone-connect-or-disconnect-a-railgun +func (api *API) connectZoneRailgun(zoneID, railgunID string, connect bool) (ZoneRailgun, error) { + uri := "/zones/" + zoneID + "/railguns/" + railgunID + params := struct { + Connected bool `json:"connected"` + }{ + Connected: connect, + } + res, err := api.makeRequest("PATCH", uri, params) + if err != nil { + return ZoneRailgun{}, errors.Wrap(err, errMakeRequestError) + } + var r zoneRailgunResponse + if err := json.Unmarshal(res, &r); err != nil { + return ZoneRailgun{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// ConnectZoneRailgun connects a Railgun for a given zone. +// +// API reference: https://api.cloudflare.com/#railguns-for-a-zone-connect-or-disconnect-a-railgun +func (api *API) ConnectZoneRailgun(zoneID, railgunID string) (ZoneRailgun, error) { + return api.connectZoneRailgun(zoneID, railgunID, true) +} + +// DisconnectZoneRailgun disconnects a Railgun for a given zone. +// +// API reference: https://api.cloudflare.com/#railguns-for-a-zone-connect-or-disconnect-a-railgun +func (api *API) DisconnectZoneRailgun(zoneID, railgunID string) (ZoneRailgun, error) { + return api.connectZoneRailgun(zoneID, railgunID, false) +} diff --git a/vendor/github.com/cloudflare/cloudflare-go/rate_limiting.go b/vendor/github.com/cloudflare/cloudflare-go/rate_limiting.go new file mode 100644 index 000000000..96ad70ee9 --- /dev/null +++ b/vendor/github.com/cloudflare/cloudflare-go/rate_limiting.go @@ -0,0 +1,195 @@ +package cloudflare + +import ( + "encoding/json" + "net/url" + "strconv" + + "github.com/pkg/errors" +) + +// RateLimit is a policy than can be applied to limit traffic within a customer domain +type RateLimit struct { + ID string `json:"id,omitempty"` + Disabled bool `json:"disabled,omitempty"` + Description string `json:"description,omitempty"` + Match RateLimitTrafficMatcher `json:"match"` + Bypass []RateLimitKeyValue `json:"bypass,omitempty"` + Threshold int `json:"threshold"` + Period int `json:"period"` + Action RateLimitAction `json:"action"` +} + +// RateLimitTrafficMatcher contains the rules that will be used to apply a rate limit to traffic +type RateLimitTrafficMatcher struct { + Request RateLimitRequestMatcher `json:"request"` + Response RateLimitResponseMatcher `json:"response"` +} + +// RateLimitRequestMatcher contains the matching rules pertaining to requests +type RateLimitRequestMatcher struct { + Methods []string `json:"methods,omitempty"` + Schemes []string `json:"schemes,omitempty"` + URLPattern string `json:"url,omitempty"` +} + +// RateLimitResponseMatcher contains the matching rules pertaining to responses +type RateLimitResponseMatcher struct { + Statuses []int `json:"status,omitempty"` + OriginTraffic *bool `json:"origin_traffic,omitempty"` // api defaults to true so we need an explicit empty value +} + +// RateLimitKeyValue is k-v formatted as expected in the rate limit description +type RateLimitKeyValue struct { + Name string `json:"name"` + Value string `json:"value"` +} + +// RateLimitAction is the action that will be taken when the rate limit threshold is reached +type RateLimitAction struct { + Mode string `json:"mode"` + Timeout int `json:"timeout"` + Response *RateLimitActionResponse `json:"response"` +} + +// RateLimitActionResponse is the response that will be returned when rate limit action is triggered +type RateLimitActionResponse struct { + ContentType string `json:"content_type"` + Body string `json:"body"` +} + +type rateLimitResponse struct { + Response + Result RateLimit `json:"result"` +} + +type rateLimitListResponse struct { + Response + Result []RateLimit `json:"result"` + ResultInfo ResultInfo `json:"result_info"` +} + +// CreateRateLimit creates a new rate limit for a zone. +// +// API reference: https://api.cloudflare.com/#rate-limits-for-a-zone-create-a-ratelimit +func (api *API) CreateRateLimit(zoneID string, limit RateLimit) (RateLimit, error) { + uri := "/zones/" + zoneID + "/rate_limits" + res, err := api.makeRequest("POST", uri, limit) + if err != nil { + return RateLimit{}, errors.Wrap(err, errMakeRequestError) + } + var r rateLimitResponse + if err := json.Unmarshal(res, &r); err != nil { + return RateLimit{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// ListRateLimits returns Rate Limits for a zone, paginated according to the provided options +// +// API reference: https://api.cloudflare.com/#rate-limits-for-a-zone-list-rate-limits +func (api *API) ListRateLimits(zoneID string, pageOpts PaginationOptions) ([]RateLimit, ResultInfo, error) { + v := url.Values{} + if pageOpts.PerPage > 0 { + v.Set("per_page", strconv.Itoa(pageOpts.PerPage)) + } + if pageOpts.Page > 0 { + v.Set("page", strconv.Itoa(pageOpts.Page)) + } + + uri := "/zones/" + zoneID + "/rate_limits" + if len(v) > 0 { + uri = uri + "?" + v.Encode() + } + + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return []RateLimit{}, ResultInfo{}, errors.Wrap(err, errMakeRequestError) + } + + var r rateLimitListResponse + err = json.Unmarshal(res, &r) + if err != nil { + return []RateLimit{}, ResultInfo{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, r.ResultInfo, nil +} + +// ListAllRateLimits returns all Rate Limits for a zone. +// +// API reference: https://api.cloudflare.com/#rate-limits-for-a-zone-list-rate-limits +func (api *API) ListAllRateLimits(zoneID string) ([]RateLimit, error) { + pageOpts := PaginationOptions{ + PerPage: 100, // this is the max page size allowed + Page: 1, + } + + allRateLimits := make([]RateLimit, 0) + for { + rateLimits, resultInfo, err := api.ListRateLimits(zoneID, pageOpts) + if err != nil { + return []RateLimit{}, err + } + allRateLimits = append(allRateLimits, rateLimits...) + // total pages is not returned on this call + // if number of records is less than the max, this must be the last page + // in case TotalCount % PerPage = 0, the last request will return an empty list + if resultInfo.Count < resultInfo.PerPage { + break + } + // continue with the next page + pageOpts.Page = pageOpts.Page + 1 + } + + return allRateLimits, nil +} + +// RateLimit fetches detail about one Rate Limit for a zone. +// +// API reference: https://api.cloudflare.com/#rate-limits-for-a-zone-rate-limit-details +func (api *API) RateLimit(zoneID, limitID string) (RateLimit, error) { + uri := "/zones/" + zoneID + "/rate_limits/" + limitID + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return RateLimit{}, errors.Wrap(err, errMakeRequestError) + } + var r rateLimitResponse + err = json.Unmarshal(res, &r) + if err != nil { + return RateLimit{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// UpdateRateLimit lets you replace a Rate Limit for a zone. +// +// API reference: https://api.cloudflare.com/#rate-limits-for-a-zone-update-rate-limit +func (api *API) UpdateRateLimit(zoneID, limitID string, limit RateLimit) (RateLimit, error) { + uri := "/zones/" + zoneID + "/rate_limits/" + limitID + res, err := api.makeRequest("PUT", uri, limit) + if err != nil { + return RateLimit{}, errors.Wrap(err, errMakeRequestError) + } + var r rateLimitResponse + if err := json.Unmarshal(res, &r); err != nil { + return RateLimit{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// DeleteRateLimit deletes a Rate Limit for a zone. +// +// API reference: https://api.cloudflare.com/#rate-limits-for-a-zone-delete-rate-limit +func (api *API) DeleteRateLimit(zoneID, limitID string) error { + uri := "/zones/" + zoneID + "/rate_limits/" + limitID + res, err := api.makeRequest("DELETE", uri, nil) + if err != nil { + return errors.Wrap(err, errMakeRequestError) + } + var r rateLimitResponse + err = json.Unmarshal(res, &r) + if err != nil { + return errors.Wrap(err, errUnmarshalError) + } + return nil +} diff --git a/vendor/github.com/cloudflare/cloudflare-go/ssl.go b/vendor/github.com/cloudflare/cloudflare-go/ssl.go new file mode 100644 index 000000000..def81ea01 --- /dev/null +++ b/vendor/github.com/cloudflare/cloudflare-go/ssl.go @@ -0,0 +1,148 @@ +package cloudflare + +import ( + "encoding/json" + "time" + + "github.com/pkg/errors" +) + +// ZoneCustomSSL represents custom SSL certificate metadata. +type ZoneCustomSSL struct { + ID string `json:"id"` + Hosts []string `json:"hosts"` + Issuer string `json:"issuer"` + Signature string `json:"signature"` + Status string `json:"status"` + BundleMethod string `json:"bundle_method"` + ZoneID string `json:"zone_id"` + UploadedOn time.Time `json:"uploaded_on"` + ModifiedOn time.Time `json:"modified_on"` + ExpiresOn time.Time `json:"expires_on"` + Priority int `json:"priority"` + KeylessServer KeylessSSL `json:"keyless_server"` +} + +// zoneCustomSSLResponse represents the response from the zone SSL details endpoint. +type zoneCustomSSLResponse struct { + Response + Result ZoneCustomSSL `json:"result"` +} + +// zoneCustomSSLsResponse represents the response from the zone SSL list endpoint. +type zoneCustomSSLsResponse struct { + Response + Result []ZoneCustomSSL `json:"result"` +} + +// ZoneCustomSSLOptions represents the parameters to create or update an existing +// custom SSL configuration. +type ZoneCustomSSLOptions struct { + Certificate string `json:"certificate"` + PrivateKey string `json:"private_key"` + BundleMethod string `json:"bundle_method,omitempty"` +} + +// ZoneCustomSSLPriority represents a certificate's ID and priority. It is a +// subset of ZoneCustomSSL used for patch requests. +type ZoneCustomSSLPriority struct { + ID string `json:"ID"` + Priority int `json:"priority"` +} + +// CreateSSL allows you to add a custom SSL certificate to the given zone. +// +// API reference: https://api.cloudflare.com/#custom-ssl-for-a-zone-create-ssl-configuration +func (api *API) CreateSSL(zoneID string, options ZoneCustomSSLOptions) (ZoneCustomSSL, error) { + uri := "/zones/" + zoneID + "/custom_certificates" + res, err := api.makeRequest("POST", uri, options) + if err != nil { + return ZoneCustomSSL{}, errors.Wrap(err, errMakeRequestError) + } + var r zoneCustomSSLResponse + if err := json.Unmarshal(res, &r); err != nil { + return ZoneCustomSSL{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// ListSSL lists the custom certificates for the given zone. +// +// API reference: https://api.cloudflare.com/#custom-ssl-for-a-zone-list-ssl-configurations +func (api *API) ListSSL(zoneID string) ([]ZoneCustomSSL, error) { + uri := "/zones/" + zoneID + "/custom_certificates" + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + var r zoneCustomSSLsResponse + if err := json.Unmarshal(res, &r); err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// SSLDetails returns the configuration details for a custom SSL certificate. +// +// API reference: https://api.cloudflare.com/#custom-ssl-for-a-zone-ssl-configuration-details +func (api *API) SSLDetails(zoneID, certificateID string) (ZoneCustomSSL, error) { + uri := "/zones/" + zoneID + "/custom_certificates/" + certificateID + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return ZoneCustomSSL{}, errors.Wrap(err, errMakeRequestError) + } + var r zoneCustomSSLResponse + if err := json.Unmarshal(res, &r); err != nil { + return ZoneCustomSSL{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// UpdateSSL updates (replaces) a custom SSL certificate. +// +// API reference: https://api.cloudflare.com/#custom-ssl-for-a-zone-update-ssl-configuration +func (api *API) UpdateSSL(zoneID, certificateID string, options ZoneCustomSSLOptions) (ZoneCustomSSL, error) { + uri := "/zones/" + zoneID + "/custom_certificates/" + certificateID + res, err := api.makeRequest("PATCH", uri, options) + if err != nil { + return ZoneCustomSSL{}, errors.Wrap(err, errMakeRequestError) + } + var r zoneCustomSSLResponse + if err := json.Unmarshal(res, &r); err != nil { + return ZoneCustomSSL{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// ReprioritizeSSL allows you to change the priority (which is served for a given +// request) of custom SSL certificates associated with the given zone. +// +// API reference: https://api.cloudflare.com/#custom-ssl-for-a-zone-re-prioritize-ssl-certificates +func (api *API) ReprioritizeSSL(zoneID string, p []ZoneCustomSSLPriority) ([]ZoneCustomSSL, error) { + uri := "/zones/" + zoneID + "/custom_certificates/prioritize" + params := struct { + Certificates []ZoneCustomSSLPriority `json:"certificates"` + }{ + Certificates: p, + } + res, err := api.makeRequest("PUT", uri, params) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + var r zoneCustomSSLsResponse + if err := json.Unmarshal(res, &r); err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// DeleteSSL deletes a custom SSL certificate from the given zone. +// +// API reference: https://api.cloudflare.com/#custom-ssl-for-a-zone-delete-an-ssl-certificate +func (api *API) DeleteSSL(zoneID, certificateID string) error { + uri := "/zones/" + zoneID + "/custom_certificates/" + certificateID + if _, err := api.makeRequest("DELETE", uri, nil); err != nil { + return errors.Wrap(err, errMakeRequestError) + } + return nil +} diff --git a/vendor/github.com/cloudflare/cloudflare-go/user.go b/vendor/github.com/cloudflare/cloudflare-go/user.go new file mode 100644 index 000000000..b42641e92 --- /dev/null +++ b/vendor/github.com/cloudflare/cloudflare-go/user.go @@ -0,0 +1,113 @@ +package cloudflare + +import ( + "encoding/json" + "time" + + "github.com/pkg/errors" +) + +// User describes a user account. +type User struct { + ID string `json:"id,omitempty"` + Email string `json:"email,omitempty"` + FirstName string `json:"first_name,omitempty"` + LastName string `json:"last_name,omitempty"` + Username string `json:"username,omitempty"` + Telephone string `json:"telephone,omitempty"` + Country string `json:"country,omitempty"` + Zipcode string `json:"zipcode,omitempty"` + CreatedOn *time.Time `json:"created_on,omitempty"` + ModifiedOn *time.Time `json:"modified_on,omitempty"` + APIKey string `json:"api_key,omitempty"` + TwoFA bool `json:"two_factor_authentication_enabled,omitempty"` + Betas []string `json:"betas,omitempty"` + Organizations []Organization `json:"organizations,omitempty"` +} + +// UserResponse wraps a response containing User accounts. +type UserResponse struct { + Response + Result User `json:"result"` +} + +// userBillingProfileResponse wraps a response containing Billing Profile information. +type userBillingProfileResponse struct { + Response + Result UserBillingProfile +} + +// UserBillingProfile contains Billing Profile information. +type UserBillingProfile struct { + ID string `json:"id,omitempty"` + FirstName string `json:"first_name,omitempty"` + LastName string `json:"last_name,omitempty"` + Address string `json:"address,omitempty"` + Address2 string `json:"address2,omitempty"` + Company string `json:"company,omitempty"` + City string `json:"city,omitempty"` + State string `json:"state,omitempty"` + ZipCode string `json:"zipcode,omitempty"` + Country string `json:"country,omitempty"` + Telephone string `json:"telephone,omitempty"` + CardNumber string `json:"card_number,omitempty"` + CardExpiryYear int `json:"card_expiry_year,omitempty"` + CardExpiryMonth int `json:"card_expiry_month,omitempty"` + VAT string `json:"vat,omitempty"` + CreatedOn *time.Time `json:"created_on,omitempty"` + EditedOn *time.Time `json:"edited_on,omitempty"` +} + +// UserDetails provides information about the logged-in user. +// +// API reference: https://api.cloudflare.com/#user-user-details +func (api *API) UserDetails() (User, error) { + var r UserResponse + res, err := api.makeRequest("GET", "/user", nil) + if err != nil { + return User{}, errors.Wrap(err, errMakeRequestError) + } + + err = json.Unmarshal(res, &r) + if err != nil { + return User{}, errors.Wrap(err, errUnmarshalError) + } + + return r.Result, nil +} + +// UpdateUser updates the properties of the given user. +// +// API reference: https://api.cloudflare.com/#user-update-user +func (api *API) UpdateUser(user *User) (User, error) { + var r UserResponse + res, err := api.makeRequest("PATCH", "/user", user) + if err != nil { + return User{}, errors.Wrap(err, errMakeRequestError) + } + + err = json.Unmarshal(res, &r) + if err != nil { + return User{}, errors.Wrap(err, errUnmarshalError) + } + + return r.Result, nil +} + +// UserBillingProfile returns the billing profile of the user. +// +// API reference: https://api.cloudflare.com/#user-billing-profile +func (api *API) UserBillingProfile() (UserBillingProfile, error) { + var r userBillingProfileResponse + res, err := api.makeRequest("GET", "/user/billing/profile", nil) + if err != nil { + return UserBillingProfile{}, errors.Wrap(err, errMakeRequestError) + } + + err = json.Unmarshal(res, &r) + if err != nil { + return UserBillingProfile{}, errors.Wrap(err, errUnmarshalError) + } + + return r.Result, nil +} diff --git a/vendor/github.com/cloudflare/cloudflare-go/user_agent.go b/vendor/github.com/cloudflare/cloudflare-go/user_agent.go new file mode 100644 index 000000000..6d75f3a1d --- /dev/null +++ b/vendor/github.com/cloudflare/cloudflare-go/user_agent.go @@ -0,0 +1,149 @@ +package cloudflare + +import ( + "encoding/json" + "net/url" + "strconv" + + "github.com/pkg/errors" +) + +// UserAgentRule represents a User-Agent Block. These rules can be used to +// challenge, block or whitelist specific User-Agents for a given zone. +type UserAgentRule struct { + ID string `json:"id"` + Description string `json:"description"` + Mode string `json:"mode"` + Configuration UserAgentRuleConfig `json:"configuration"` + Paused bool `json:"paused"` +} + +// UserAgentRuleConfig represents a Zone Lockdown config, which comprises +// a Target ("ip" or "ip_range") and a Value (an IP address or IP+mask, +// respectively.) +type UserAgentRuleConfig ZoneLockdownConfig + +// UserAgentRuleResponse represents a response from the Zone Lockdown endpoint. +type UserAgentRuleResponse struct { + Result UserAgentRule `json:"result"` + Response + ResultInfo `json:"result_info"` +} + +// UserAgentRuleListResponse represents a response from the List Zone Lockdown endpoint. +type UserAgentRuleListResponse struct { + Result []UserAgentRule `json:"result"` + Response + ResultInfo `json:"result_info"` +} + +// CreateUserAgentRule creates a User-Agent Block rule for the given zone ID. +// +// API reference: https://api.cloudflare.com/#user-agent-blocking-rules-create-a-useragent-rule +func (api *API) CreateUserAgentRule(zoneID string, ld UserAgentRule) (*UserAgentRuleResponse, error) { + switch ld.Mode { + case "block", "challenge", "js_challenge", "whitelist": + break + default: + return nil, errors.New(`the User-Agent Block rule mode must be one of "block", "challenge", "js_challenge", "whitelist"`) + } + + uri := "/zones/" + zoneID + "/firewall/ua_rules" + res, err := api.makeRequest("POST", uri, ld) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + response := &UserAgentRuleResponse{} + err = json.Unmarshal(res, &response) + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + + return response, nil +} + +// UpdateUserAgentRule updates a User-Agent Block rule (based on the ID) for the given zone ID. +// +// API reference: https://api.cloudflare.com/#user-agent-blocking-rules-update-useragent-rule +func (api *API) UpdateUserAgentRule(zoneID string, id string, ld UserAgentRule) (*UserAgentRuleResponse, error) { + uri := "/zones/" + zoneID + "/firewall/ua_rules/" + id + res, err := api.makeRequest("PUT", uri, ld) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + response := &UserAgentRuleResponse{} + err = json.Unmarshal(res, &response) + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + + return response, nil +} + +// DeleteUserAgentRule deletes a User-Agent Block rule (based on the ID) for the given zone ID. +// +// API reference: https://api.cloudflare.com/#user-agent-blocking-rules-delete-useragent-rule +func (api *API) DeleteUserAgentRule(zoneID string, id string) (*UserAgentRuleResponse, error) { + uri := "/zones/" + zoneID + "/firewall/ua_rules/" + id + res, err := api.makeRequest("DELETE", uri, nil) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + response := &UserAgentRuleResponse{} + err = json.Unmarshal(res, &response) + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + + return response, nil +} + +// UserAgentRule retrieves a User-Agent Block rule (based on the ID) for the given zone ID. +// +// API reference: https://api.cloudflare.com/#user-agent-blocking-rules-useragent-rule-details +func (api *API) UserAgentRule(zoneID string, id string) (*UserAgentRuleResponse, error) { + uri := "/zones/" + zoneID + "/firewall/ua_rules/" + id + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + response := &UserAgentRuleResponse{} + err = json.Unmarshal(res, &response) + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + + return response, nil +} + +// ListUserAgentRules retrieves a list of User-Agent Block rules for a given zone ID by page number. +// +// API reference: https://api.cloudflare.com/#user-agent-blocking-rules-list-useragent-rules +func (api *API) ListUserAgentRules(zoneID string, page int) (*UserAgentRuleListResponse, error) { + v := url.Values{} + if page <= 0 { + page = 1 + } + + v.Set("page", strconv.Itoa(page)) + v.Set("per_page", strconv.Itoa(100)) + query := "?" + v.Encode() + + uri := "/zones/" + zoneID + "/firewall/ua_rules" + query + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + response := &UserAgentRuleListResponse{} + err = json.Unmarshal(res, &response) + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + + return response, nil +} diff --git a/vendor/github.com/cloudflare/cloudflare-go/virtualdns.go b/vendor/github.com/cloudflare/cloudflare-go/virtualdns.go new file mode 100644 index 000000000..6eb7629f0 --- /dev/null +++ b/vendor/github.com/cloudflare/cloudflare-go/virtualdns.go @@ -0,0 +1,125 @@ +package cloudflare + +import ( + "encoding/json" + + "github.com/pkg/errors" +) + +// VirtualDNS represents a Virtual DNS configuration. +type VirtualDNS struct { + ID string `json:"id"` + Name string `json:"name"` + OriginIPs []string `json:"origin_ips"` + VirtualDNSIPs []string `json:"virtual_dns_ips"` + MinimumCacheTTL uint `json:"minimum_cache_ttl"` + MaximumCacheTTL uint `json:"maximum_cache_ttl"` + DeprecateAnyRequests bool `json:"deprecate_any_requests"` + ModifiedOn string `json:"modified_on"` +} + +// VirtualDNSResponse represents a Virtual DNS response. +type VirtualDNSResponse struct { + Response + Result *VirtualDNS `json:"result"` +} + +// VirtualDNSListResponse represents an array of Virtual DNS responses. +type VirtualDNSListResponse struct { + Response + Result []*VirtualDNS `json:"result"` +} + +// CreateVirtualDNS creates a new Virtual DNS cluster. +// +// API reference: https://api.cloudflare.com/#virtual-dns-users--create-a-virtual-dns-cluster +func (api *API) CreateVirtualDNS(v *VirtualDNS) (*VirtualDNS, error) { + res, err := api.makeRequest("POST", "/user/virtual_dns", v) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + response := &VirtualDNSResponse{} + err = json.Unmarshal(res, &response) + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + + return response.Result, nil +} + +// VirtualDNS fetches a single virtual DNS cluster. +// +// API reference: https://api.cloudflare.com/#virtual-dns-users--get-a-virtual-dns-cluster +func (api *API) VirtualDNS(virtualDNSID string) (*VirtualDNS, error) { + uri := "/user/virtual_dns/" + virtualDNSID + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + response := &VirtualDNSResponse{} + err = json.Unmarshal(res, &response) + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + + return response.Result, nil +} + +// ListVirtualDNS lists the virtual DNS clusters associated with an account. +// +// API reference: https://api.cloudflare.com/#virtual-dns-users--get-virtual-dns-clusters +func (api *API) ListVirtualDNS() ([]*VirtualDNS, error) { + res, err := api.makeRequest("GET", "/user/virtual_dns", nil) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + response := &VirtualDNSListResponse{} + err = json.Unmarshal(res, &response) + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + + return response.Result, nil +} + +// UpdateVirtualDNS updates a Virtual DNS cluster. +// +// API reference: https://api.cloudflare.com/#virtual-dns-users--modify-a-virtual-dns-cluster +func (api *API) UpdateVirtualDNS(virtualDNSID string, vv VirtualDNS) error { + uri := "/user/virtual_dns/" + virtualDNSID + res, err := api.makeRequest("PUT", uri, vv) + if err != nil { + return errors.Wrap(err, errMakeRequestError) + } + + response := &VirtualDNSResponse{} + err = json.Unmarshal(res, &response) + if err != nil { + return errors.Wrap(err, errUnmarshalError) + } + + return nil +} + +// DeleteVirtualDNS deletes a Virtual DNS cluster. Note that this cannot be +// undone, and will stop all traffic to that cluster. +// +// API reference: https://api.cloudflare.com/#virtual-dns-users--delete-a-virtual-dns-cluster +func (api *API) DeleteVirtualDNS(virtualDNSID string) error { + uri := "/user/virtual_dns/" + virtualDNSID + res, err := api.makeRequest("DELETE", uri, nil) + if err != nil { + return errors.Wrap(err, errMakeRequestError) + } + + response := &VirtualDNSResponse{} + err = json.Unmarshal(res, &response) + if err != nil { + return errors.Wrap(err, errUnmarshalError) + } + + return nil +} diff --git a/vendor/github.com/cloudflare/cloudflare-go/waf.go b/vendor/github.com/cloudflare/cloudflare-go/waf.go new file mode 100644 index 000000000..4ed156236 --- /dev/null +++ b/vendor/github.com/cloudflare/cloudflare-go/waf.go @@ -0,0 +1,97 @@ +package cloudflare + +import ( + "encoding/json" + + "github.com/pkg/errors" +) + +// WAFPackage represents a WAF package configuration. +type WAFPackage struct { + ID string `json:"id"` + Name string `json:"name"` + Description string `json:"description"` + ZoneID string `json:"zone_id"` + DetectionMode string `json:"detection_mode"` + Sensitivity string `json:"sensitivity"` + ActionMode string `json:"action_mode"` +} + +// WAFPackagesResponse represents the response from the WAF packages endpoint. +type WAFPackagesResponse struct { + Response + Result []WAFPackage `json:"result"` + ResultInfo ResultInfo `json:"result_info"` +} + +// WAFRule represents a WAF rule. +type WAFRule struct { + ID string `json:"id"` + Description string `json:"description"` + Priority string `json:"priority"` + PackageID string `json:"package_id"` + Group struct { + ID string `json:"id"` + Name string `json:"name"` + } `json:"group"` + Mode string `json:"mode"` + DefaultMode string `json:"default_mode"` + AllowedModes []string `json:"allowed_modes"` +} + +// WAFRulesResponse represents the response from the WAF rule endpoint. +type WAFRulesResponse struct { + Response + Result []WAFRule `json:"result"` + ResultInfo ResultInfo `json:"result_info"` +} + +// ListWAFPackages returns a slice of the WAF packages for the given zone. +func (api *API) ListWAFPackages(zoneID string) ([]WAFPackage, error) { + var p WAFPackagesResponse + var packages []WAFPackage + var res []byte + var err error + uri := "/zones/" + zoneID + "/firewall/waf/packages" + res, err = api.makeRequest("GET", uri, nil) + if err != nil { + return []WAFPackage{}, errors.Wrap(err, errMakeRequestError) + } + err = json.Unmarshal(res, &p) + if err != nil { + return []WAFPackage{}, errors.Wrap(err, errUnmarshalError) + } + if !p.Success { + // TODO: Provide an actual error message instead of always returning nil + return []WAFPackage{}, err + } + for pi := range p.Result { + packages = append(packages, p.Result[pi]) + } + return packages, nil +} + +// ListWAFRules returns a slice of the WAF rules for the given WAF package. +func (api *API) ListWAFRules(zoneID, packageID string) ([]WAFRule, error) { + var r WAFRulesResponse + var rules []WAFRule + var res []byte + var err error + uri := "/zones/" + zoneID + "/firewall/waf/packages/" + packageID + "/rules" + res, err = api.makeRequest("GET", uri, nil) + if err != nil { + return []WAFRule{}, errors.Wrap(err, errMakeRequestError) + } + err = json.Unmarshal(res, &r) + if err != nil { + return []WAFRule{}, errors.Wrap(err, errUnmarshalError) + } + if !r.Success { + // TODO: Provide an actual error message instead of always returning nil + return []WAFRule{}, err + } + for ri := range r.Result { + rules = append(rules, r.Result[ri]) + } + return rules, nil +} diff --git a/vendor/github.com/cloudflare/cloudflare-go/zone.go b/vendor/github.com/cloudflare/cloudflare-go/zone.go new file mode 100644 index 000000000..fe222d33e --- /dev/null +++ b/vendor/github.com/cloudflare/cloudflare-go/zone.go @@ -0,0 +1,587 @@ +package cloudflare + +import ( + "encoding/json" + "fmt" + "net/url" + "time" + + "github.com/pkg/errors" +) + +// Owner describes the resource owner. +type Owner struct { + ID string `json:"id"` + Email string `json:"email"` + OwnerType string `json:"owner_type"` +} + +// Zone describes a Cloudflare zone. +type Zone struct { + ID string `json:"id"` + Name string `json:"name"` + // DevMode contains the time in seconds until development expires (if + // positive) or since it expired (if negative). It will be 0 if never used. + DevMode int `json:"development_mode"` + OriginalNS []string `json:"original_name_servers"` + OriginalRegistrar string `json:"original_registrar"` + OriginalDNSHost string `json:"original_dnshost"` + CreatedOn time.Time `json:"created_on"` + ModifiedOn time.Time `json:"modified_on"` + NameServers []string `json:"name_servers"` + Owner Owner `json:"owner"` + Permissions []string `json:"permissions"` + Plan ZoneRatePlan `json:"plan"` + PlanPending ZoneRatePlan `json:"plan_pending,omitempty"` + Status string `json:"status"` + Paused bool `json:"paused"` + Type string `json:"type"` + Host struct { + Name string + Website string + } `json:"host"` + VanityNS []string `json:"vanity_name_servers"` + Betas []string `json:"betas"` + DeactReason string `json:"deactivation_reason"` + Meta ZoneMeta `json:"meta"` +} + +// ZoneMeta describes metadata about a zone. +type ZoneMeta struct { + // custom_certificate_quota is broken - sometimes it's a string, sometimes a number! + // CustCertQuota int `json:"custom_certificate_quota"` + PageRuleQuota int `json:"page_rule_quota"` + WildcardProxiable bool `json:"wildcard_proxiable"` + PhishingDetected bool `json:"phishing_detected"` +} + +// ZoneRatePlan contains the plan information for a zone. +type ZoneRatePlan struct { + ID string `json:"id"` + Name string `json:"name,omitempty"` + Price int `json:"price,omitempty"` + Currency string `json:"currency,omitempty"` + Duration int `json:"duration,omitempty"` + Frequency string `json:"frequency,omitempty"` + Components []zoneRatePlanComponents `json:"components,omitempty"` +} + +type zoneRatePlanComponents struct { + Name string `json:"name"` + Default int `json:"Default"` + UnitPrice int `json:"unit_price"` +} + +// ZoneID contains only the zone ID. +type ZoneID struct { + ID string `json:"id"` +} + +// ZoneResponse represents the response from the Zone endpoint containing a single zone. +type ZoneResponse struct { + Response + Result Zone `json:"result"` +} + +// ZonesResponse represents the response from the Zone endpoint containing an array of zones. +type ZonesResponse struct { + Response + Result []Zone `json:"result"` +} + +// ZoneIDResponse represents the response from the Zone endpoint, containing only a zone ID. +type ZoneIDResponse struct { + Response + Result ZoneID `json:"result"` +} + +// AvailableZoneRatePlansResponse represents the response from the Available Rate Plans endpoint. +type AvailableZoneRatePlansResponse struct { + Response + Result []ZoneRatePlan `json:"result"` + ResultInfo +} + +// ZoneRatePlanResponse represents the response from the Plan Details endpoint. +type ZoneRatePlanResponse struct { + Response + Result ZoneRatePlan `json:"result"` +} + +// ZoneSetting contains settings for a zone. +type ZoneSetting struct { + ID string `json:"id"` + Editable bool `json:"editable"` + ModifiedOn string `json:"modified_on"` + Value interface{} `json:"value"` + TimeRemaining int `json:"time_remaining"` +} + +// ZoneSettingResponse represents the response from the Zone Setting endpoint. +type ZoneSettingResponse struct { + Response + Result []ZoneSetting `json:"result"` +} + +// ZoneSSLSetting contains ssl setting for a zone. +type ZoneSSLSetting struct { + ID string `json:"id"` + Editable bool `json:"editable"` + ModifiedOn string `json:"modified_on"` + Value string `json:"value"` + CertificateStatus string `json:"certificate_status"` +} + +// ZoneSettingResponse represents the response from the Zone SSL Setting endpoint. +type ZoneSSLSettingResponse struct { + Response + Result ZoneSSLSetting `json:"result"` +} + +// ZoneAnalyticsData contains totals and timeseries analytics data for a zone. +type ZoneAnalyticsData struct { + Totals ZoneAnalytics `json:"totals"` + Timeseries []ZoneAnalytics `json:"timeseries"` +} + +// zoneAnalyticsDataResponse represents the response from the Zone Analytics Dashboard endpoint. +type zoneAnalyticsDataResponse struct { + Response + Result ZoneAnalyticsData `json:"result"` +} + +// ZoneAnalyticsColocation contains analytics data by datacenter. +type ZoneAnalyticsColocation struct { + ColocationID string `json:"colo_id"` + Timeseries []ZoneAnalytics `json:"timeseries"` +} + +// zoneAnalyticsColocationResponse represents the response from the Zone Analytics By Co-location endpoint. +type zoneAnalyticsColocationResponse struct { + Response + Result []ZoneAnalyticsColocation `json:"result"` +} + +// ZoneAnalytics contains analytics data for a zone. +type ZoneAnalytics struct { + Since time.Time `json:"since"` + Until time.Time `json:"until"` + Requests struct { + All int `json:"all"` + Cached int `json:"cached"` + Uncached int `json:"uncached"` + ContentType map[string]int `json:"content_type"` + Country map[string]int `json:"country"` + SSL struct { + Encrypted int `json:"encrypted"` + Unencrypted int `json:"unencrypted"` + } `json:"ssl"` + HTTPStatus map[string]int `json:"http_status"` + } `json:"requests"` + Bandwidth struct { + All int `json:"all"` + Cached int `json:"cached"` + Uncached int `json:"uncached"` + ContentType map[string]int `json:"content_type"` + Country map[string]int `json:"country"` + SSL struct { + Encrypted int `json:"encrypted"` + Unencrypted int `json:"unencrypted"` + } `json:"ssl"` + } `json:"bandwidth"` + Threats struct { + All int `json:"all"` + Country map[string]int `json:"country"` + Type map[string]int `json:"type"` + } `json:"threats"` + Pageviews struct { + All int `json:"all"` + SearchEngines map[string]int `json:"search_engines"` + } `json:"pageviews"` + Uniques struct { + All int `json:"all"` + } +} + +// ZoneAnalyticsOptions represents the optional parameters in Zone Analytics +// endpoint requests. +type ZoneAnalyticsOptions struct { + Since *time.Time + Until *time.Time + Continuous *bool +} + +// PurgeCacheRequest represents the request format made to the purge endpoint. +type PurgeCacheRequest struct { + Everything bool `json:"purge_everything,omitempty"` + // Purge by filepath (exact match). Limit of 30 + Files []string `json:"files,omitempty"` + // Purge by Tag (Enterprise only): + // https://support.cloudflare.com/hc/en-us/articles/206596608-How-to-Purge-Cache-Using-Cache-Tags-Enterprise-only- + Tags []string `json:"tags,omitempty"` + // Purge by hostname - e.g. "assets.example.com" + Hosts []string `json:"hosts,omitempty"` +} + +// PurgeCacheResponse represents the response from the purge endpoint. +type PurgeCacheResponse struct { + Response + Result struct { + ID string `json:"id"` + } `json:"result"` +} + +// newZone describes a new zone. +type newZone struct { + Name string `json:"name"` + JumpStart bool `json:"jump_start"` + // We use a pointer to get a nil type when the field is empty. + // This allows us to completely omit this with json.Marshal(). + Organization *Organization `json:"organization,omitempty"` +} + +// CreateZone creates a zone on an account. +// +// Setting jumpstart to true will attempt to automatically scan for existing +// DNS records. Setting this to false will create the zone with no DNS records. +// +// If Organization is non-empty, it must have at least the ID field populated. +// This will add the new zone to the specified multi-user organization. +// +// API reference: https://api.cloudflare.com/#zone-create-a-zone +func (api *API) CreateZone(name string, jumpstart bool, org Organization) (Zone, error) { + var newzone newZone + newzone.Name = name + newzone.JumpStart = jumpstart + if org.ID != "" { + newzone.Organization = &org + } + + res, err := api.makeRequest("POST", "/zones", newzone) + if err != nil { + return Zone{}, errors.Wrap(err, errMakeRequestError) + } + + var r ZoneResponse + err = json.Unmarshal(res, &r) + if err != nil { + return Zone{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// ZoneActivationCheck initiates another zone activation check for newly-created zones. +// +// API reference: https://api.cloudflare.com/#zone-initiate-another-zone-activation-check +func (api *API) ZoneActivationCheck(zoneID string) (Response, error) { + res, err := api.makeRequest("PUT", "/zones/"+zoneID+"/activation_check", nil) + if err != nil { + return Response{}, errors.Wrap(err, errMakeRequestError) + } + var r Response + err = json.Unmarshal(res, &r) + if err != nil { + return Response{}, errors.Wrap(err, errUnmarshalError) + } + return r, nil +} + +// ListZones lists zones on an account. Optionally takes a list of zone names +// to filter against. +// +// API reference: https://api.cloudflare.com/#zone-list-zones +func (api *API) ListZones(z ...string) ([]Zone, error) { + v := url.Values{} + var res []byte + var r ZonesResponse + var zones []Zone + var err error + if len(z) > 0 { + for _, zone := range z { + v.Set("name", zone) + res, err = api.makeRequest("GET", "/zones?"+v.Encode(), nil) + if err != nil { + return []Zone{}, errors.Wrap(err, errMakeRequestError) + } + err = json.Unmarshal(res, &r) + if err != nil { + return []Zone{}, errors.Wrap(err, errUnmarshalError) + } + if !r.Success { + // TODO: Provide an actual error message instead of always returning nil + return []Zone{}, err + } + for zi := range r.Result { + zones = append(zones, r.Result[zi]) + } + } + } else { + // TODO: Paginate here. We only grab the first page of results. + // Could do this concurrently after the first request by creating a + // sync.WaitGroup or just a channel + workers. + res, err = api.makeRequest("GET", "/zones", nil) + if err != nil { + return []Zone{}, errors.Wrap(err, errMakeRequestError) + } + err = json.Unmarshal(res, &r) + if err != nil { + return []Zone{}, errors.Wrap(err, errUnmarshalError) + } + zones = r.Result + } + + return zones, nil +} + +// ZoneDetails fetches information about a zone. +// +// API reference: https://api.cloudflare.com/#zone-zone-details +func (api *API) ZoneDetails(zoneID string) (Zone, error) { + res, err := api.makeRequest("GET", "/zones/"+zoneID, nil) + if err != nil { + return Zone{}, errors.Wrap(err, errMakeRequestError) + } + var r ZoneResponse + err = json.Unmarshal(res, &r) + if err != nil { + return Zone{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// ZoneOptions is a subset of Zone, for editable options. +type ZoneOptions struct { + Paused *bool `json:"paused,omitempty"` + VanityNS []string `json:"vanity_name_servers,omitempty"` + Plan *ZoneRatePlan `json:"plan,omitempty"` +} + +// ZoneSetPaused pauses Cloudflare service for the entire zone, sending all +// traffic direct to the origin. +func (api *API) ZoneSetPaused(zoneID string, paused bool) (Zone, error) { + zoneopts := ZoneOptions{Paused: &paused} + zone, err := api.EditZone(zoneID, zoneopts) + if err != nil { + return Zone{}, err + } + + return zone, nil +} + +// ZoneSetVanityNS sets custom nameservers for the zone. +// These names must be within the same zone. +func (api *API) ZoneSetVanityNS(zoneID string, ns []string) (Zone, error) { + zoneopts := ZoneOptions{VanityNS: ns} + zone, err := api.EditZone(zoneID, zoneopts) + if err != nil { + return Zone{}, err + } + + return zone, nil +} + +// ZoneSetRatePlan changes the zone plan. +func (api *API) ZoneSetRatePlan(zoneID string, plan ZoneRatePlan) (Zone, error) { + zoneopts := ZoneOptions{Plan: &plan} + zone, err := api.EditZone(zoneID, zoneopts) + if err != nil { + return Zone{}, err + } + + return zone, nil +} + +// EditZone edits the given zone. +// +// This is usually called by ZoneSetPaused, ZoneSetVanityNS or ZoneSetPlan. +// +// API reference: https://api.cloudflare.com/#zone-edit-zone-properties +func (api *API) EditZone(zoneID string, zoneOpts ZoneOptions) (Zone, error) { + res, err := api.makeRequest("PATCH", "/zones/"+zoneID, zoneOpts) + if err != nil { + return Zone{}, errors.Wrap(err, errMakeRequestError) + } + var r ZoneResponse + err = json.Unmarshal(res, &r) + if err != nil { + return Zone{}, errors.Wrap(err, errUnmarshalError) + } + + return r.Result, nil +} + +// PurgeEverything purges the cache for the given zone. +// +// Note: this will substantially increase load on the origin server for that +// zone if there is a high cached vs. uncached request ratio. +// +// API reference: https://api.cloudflare.com/#zone-purge-all-files +func (api *API) PurgeEverything(zoneID string) (PurgeCacheResponse, error) { + uri := "/zones/" + zoneID + "/purge_cache" + res, err := api.makeRequest("DELETE", uri, PurgeCacheRequest{true, nil, nil, nil}) + if err != nil { + return PurgeCacheResponse{}, errors.Wrap(err, errMakeRequestError) + } + var r PurgeCacheResponse + err = json.Unmarshal(res, &r) + if err != nil { + return PurgeCacheResponse{}, errors.Wrap(err, errUnmarshalError) + } + return r, nil +} + +// PurgeCache purges the cache using the given PurgeCacheRequest (zone/url/tag). +// +// API reference: https://api.cloudflare.com/#zone-purge-individual-files-by-url-and-cache-tags +func (api *API) PurgeCache(zoneID string, pcr PurgeCacheRequest) (PurgeCacheResponse, error) { + uri := "/zones/" + zoneID + "/purge_cache" + res, err := api.makeRequest("DELETE", uri, pcr) + if err != nil { + return PurgeCacheResponse{}, errors.Wrap(err, errMakeRequestError) + } + var r PurgeCacheResponse + err = json.Unmarshal(res, &r) + if err != nil { + return PurgeCacheResponse{}, errors.Wrap(err, errUnmarshalError) + } + return r, nil +} + +// DeleteZone deletes the given zone. +// +// API reference: https://api.cloudflare.com/#zone-delete-a-zone +func (api *API) DeleteZone(zoneID string) (ZoneID, error) { + res, err := api.makeRequest("DELETE", "/zones/"+zoneID, nil) + if err != nil { + return ZoneID{}, errors.Wrap(err, errMakeRequestError) + } + var r ZoneIDResponse + err = json.Unmarshal(res, &r) + if err != nil { + return ZoneID{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// AvailableZoneRatePlans returns information about all plans available to the specified zone. +// +// API reference: https://api.cloudflare.com/#zone-plan-available-plans +func (api *API) AvailableZoneRatePlans(zoneID string) ([]ZoneRatePlan, error) { + uri := "/zones/" + zoneID + "/available_rate_plans" + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return []ZoneRatePlan{}, errors.Wrap(err, errMakeRequestError) + } + var r AvailableZoneRatePlansResponse + err = json.Unmarshal(res, &r) + if err != nil { + return []ZoneRatePlan{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// encode encodes non-nil fields into URL encoded form. +func (o ZoneAnalyticsOptions) encode() string { + v := url.Values{} + if o.Since != nil { + v.Set("since", (*o.Since).Format(time.RFC3339)) + } + if o.Until != nil { + v.Set("until", (*o.Until).Format(time.RFC3339)) + } + if o.Continuous != nil { + v.Set("continuous", fmt.Sprintf("%t", *o.Continuous)) + } + return v.Encode() +} + +// ZoneAnalyticsDashboard returns zone analytics information. +// +// API reference: https://api.cloudflare.com/#zone-analytics-dashboard +func (api *API) ZoneAnalyticsDashboard(zoneID string, options ZoneAnalyticsOptions) (ZoneAnalyticsData, error) { + uri := "/zones/" + zoneID + "/analytics/dashboard" + "?" + options.encode() + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return ZoneAnalyticsData{}, errors.Wrap(err, errMakeRequestError) + } + var r zoneAnalyticsDataResponse + err = json.Unmarshal(res, &r) + if err != nil { + return ZoneAnalyticsData{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// ZoneAnalyticsByColocation returns zone analytics information by datacenter. +// +// API reference: https://api.cloudflare.com/#zone-analytics-analytics-by-co-locations +func (api *API) ZoneAnalyticsByColocation(zoneID string, options ZoneAnalyticsOptions) ([]ZoneAnalyticsColocation, error) { + uri := "/zones/" + zoneID + "/analytics/colos" + "?" + options.encode() + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + var r zoneAnalyticsColocationResponse + err = json.Unmarshal(res, &r) + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} + +// ZoneSettings returns all of the settings for a given zone. +// +// API reference: https://api.cloudflare.com/#zone-settings-get-all-zone-settings +func (api *API) ZoneSettings(zoneID string) (*ZoneSettingResponse, error) { + uri := "/zones/" + zoneID + "/settings" + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + response := &ZoneSettingResponse{} + err = json.Unmarshal(res, &response) + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + + return response, nil +} + +// UpdateZoneSettings updates the settings for a given zone. +// +// API reference: https://api.cloudflare.com/#zone-settings-edit-zone-settings-info +func (api *API) UpdateZoneSettings(zoneID string, settings []ZoneSetting) (*ZoneSettingResponse, error) { + uri := "/zones/" + zoneID + "/settings" + res, err := api.makeRequest("PATCH", uri, struct { + Items []ZoneSetting `json:"items"` + }{settings}) + if err != nil { + return nil, errors.Wrap(err, errMakeRequestError) + } + + response := &ZoneSettingResponse{} + err = json.Unmarshal(res, &response) + if err != nil { + return nil, errors.Wrap(err, errUnmarshalError) + } + + return response, nil +} + +// ZoneSSLSettings returns information about SSL setting to the specified zone. +// +// API reference: https://api.cloudflare.com/#zone-settings-get-ssl-setting +func (api *API) ZoneSSLSettings(zoneID string) (ZoneSSLSetting, error) { + uri := "/zones/" + zoneID + "/settings/ssl" + res, err := api.makeRequest("GET", uri, nil) + if err != nil { + return ZoneSSLSetting{}, errors.Wrap(err, errMakeRequestError) + } + var r ZoneSSLSettingResponse + err = json.Unmarshal(res, &r) + if err != nil { + return ZoneSSLSetting{}, errors.Wrap(err, errUnmarshalError) + } + return r.Result, nil +} diff --git a/vendor/github.com/containous/flaeg/flaeg.go b/vendor/github.com/containous/flaeg/flaeg.go index 8b1421d31..0ae2252da 100644 --- a/vendor/github.com/containous/flaeg/flaeg.go +++ b/vendor/github.com/containous/flaeg/flaeg.go @@ -396,6 +396,7 @@ type Command struct { DefaultPointersConfig interface{} // TODO: case DefaultPointersConfig is nil Run func() error Metadata map[string]string + HideHelp bool } // LoadWithCommand initializes config : struct fields given by reference, with args : arguments. @@ -437,13 +438,16 @@ func PrintHelpWithCommand(flagMap map[string]reflect.StructField, defaultValMap // Using POSXE STD : http://pubs.opengroup.org/onlinepubs/9699919799/ const helper = `{{if .ProgDescription}}{{.ProgDescription}} -{{end}}Usage: {{.ProgName}} [--flag=flag_argument] [-f[flag_argument]] ... set flag_argument to flag(s) - or: {{.ProgName}} [--flag[=true|false| ]] [-f[true|false| ]] ... set true/false to boolean flag(s) +{{end}}Usage: {{.ProgName}} [flags] [] + +Use "{{.ProgName}} --help" for help on any command. {{if .SubCommands}} -Available Commands:{{range $subCmdName, $subCmdDesc := .SubCommands}} +Commands:{{range $subCmdName, $subCmdDesc := .SubCommands}} {{printf "\t%-50s %s" $subCmdName $subCmdDesc}}{{end}} -Use "{{.ProgName}} [command] --help" for more information about a command. {{end}} +Flag's usage: {{.ProgName}} [--flag=flag_argument] [-f[flag_argument]] ... set flag_argument to flag(s) + or: {{.ProgName}} [--flag[=true|false| ]] [-f[true|false| ]] ... set true/false to boolean flag(s) + Flags: ` // Use a struct to give data to template @@ -453,13 +457,15 @@ Flags: SubCommands map[string]string } tempStruct := TempStruct{} - if cmd != nil { + if cmd != nil && !cmd.HideHelp { tempStruct.ProgName = cmd.Name tempStruct.ProgDescription = cmd.Description tempStruct.SubCommands = map[string]string{} if len(subCmd) > 1 && cmd == subCmd[0] { for _, c := range subCmd[1:] { - tempStruct.SubCommands[c.Name] = c.Description + if !c.HideHelp { + tempStruct.SubCommands[c.Name] = c.Description + } } } } else { @@ -528,7 +534,7 @@ func printFlagsDescriptionsDefaultValues(flagMap map[string]reflect.StructField, } } - //add help flag + // add help flag shortFlagsWithDash = append(shortFlagsWithDash, "-h,") flagsWithDash = append(flagsWithDash, "--help") descriptions = append(descriptions, "Print Help (this message) and exit") @@ -536,6 +542,7 @@ func printFlagsDescriptionsDefaultValues(flagMap map[string]reflect.StructField, return displayTab(output, shortFlagsWithDash, flagsWithDash, descriptions, defaultValues) } + func split(str string, width int) []string { if len(str) > width { index := strings.LastIndex(str[:width], " ") @@ -584,7 +591,7 @@ func PrintErrorWithCommand(err error, flagMap map[string]reflect.StructField, de // a map of custom parsers could be use type Flaeg struct { calledCommand *Command - commands []*Command ///rootCommand is th fist one in this slice + commands []*Command // rootCommand is th fist one in this slice args []string commandArgs []string customParsers map[reflect.Type]parse.Parser @@ -654,7 +661,7 @@ func (f *Flaeg) findCommandWithCommandArgs() (*Command, []string, error) { commandName, f.commandArgs = splitArgs(f.args) if len(commandName) > 0 { for _, command := range f.commands { - if commandName == command.Name { + if commandName == command.Name && !command.HideHelp { f.calledCommand = command return f.calledCommand, f.commandArgs, nil } diff --git a/vendor/github.com/go-resty/resty/LICENSE b/vendor/github.com/go-resty/resty/LICENSE new file mode 100644 index 000000000..6ae89ae63 --- /dev/null +++ b/vendor/github.com/go-resty/resty/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015-2018 Jeevanandam M., https://myjeeva.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/github.com/go-resty/resty/client.go b/vendor/github.com/go-resty/resty/client.go new file mode 100644 index 000000000..8e63ef9f0 --- /dev/null +++ b/vendor/github.com/go-resty/resty/client.go @@ -0,0 +1,900 @@ +// Copyright (c) 2015-2018 Jeevanandam M (jeeva@myjeeva.com), All rights reserved. +// resty source code and usage is governed by a MIT style +// license that can be found in the LICENSE file. + +package resty + +import ( + "bytes" + "compress/gzip" + "crypto/tls" + "crypto/x509" + "errors" + "fmt" + "io" + "io/ioutil" + "log" + "net/http" + "net/url" + "reflect" + "regexp" + "strings" + "sync" + "time" +) + +const ( + // MethodGet HTTP method + MethodGet = "GET" + + // MethodPost HTTP method + MethodPost = "POST" + + // MethodPut HTTP method + MethodPut = "PUT" + + // MethodDelete HTTP method + MethodDelete = "DELETE" + + // MethodPatch HTTP method + MethodPatch = "PATCH" + + // MethodHead HTTP method + MethodHead = "HEAD" + + // MethodOptions HTTP method + MethodOptions = "OPTIONS" +) + +var ( + hdrUserAgentKey = http.CanonicalHeaderKey("User-Agent") + hdrAcceptKey = http.CanonicalHeaderKey("Accept") + hdrContentTypeKey = http.CanonicalHeaderKey("Content-Type") + hdrContentLengthKey = http.CanonicalHeaderKey("Content-Length") + hdrContentEncodingKey = http.CanonicalHeaderKey("Content-Encoding") + hdrAuthorizationKey = http.CanonicalHeaderKey("Authorization") + + plainTextType = "text/plain; charset=utf-8" + jsonContentType = "application/json; charset=utf-8" + formContentType = "application/x-www-form-urlencoded" + + jsonCheck = regexp.MustCompile(`(?i:(application|text)/(json|.*\+json)(;|$))`) + xmlCheck = regexp.MustCompile(`(?i:(application|text)/(xml|.*\+xml)(;|$))`) + + hdrUserAgentValue = "go-resty/%s (https://github.com/go-resty/resty)" + bufPool = &sync.Pool{New: func() interface{} { return &bytes.Buffer{} }} +) + +// Client type is used for HTTP/RESTful global values +// for all request raised from the client +type Client struct { + HostURL string + QueryParam url.Values + FormData url.Values + Header http.Header + UserInfo *User + Token string + Cookies []*http.Cookie + Error reflect.Type + Debug bool + DisableWarn bool + AllowGetMethodPayload bool + Log *log.Logger + RetryCount int + RetryWaitTime time.Duration + RetryMaxWaitTime time.Duration + RetryConditions []RetryConditionFunc + JSONMarshal func(v interface{}) ([]byte, error) + JSONUnmarshal func(data []byte, v interface{}) error + + jsonEscapeHTML bool + httpClient *http.Client + setContentLength bool + isHTTPMode bool + outputDirectory string + scheme string + proxyURL *url.URL + closeConnection bool + notParseResponse bool + debugBodySizeLimit int64 + logPrefix string + pathParams map[string]string + beforeRequest []func(*Client, *Request) error + udBeforeRequest []func(*Client, *Request) error + preReqHook func(*Client, *Request) error + afterResponse []func(*Client, *Response) error +} + +// User type is to hold an username and password information +type User struct { + Username, Password string +} + +//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ +// Client methods +//___________________________________ + +// SetHostURL method is to set Host URL in the client instance. It will be used with request +// raised from this client with relative URL +// // Setting HTTP address +// resty.SetHostURL("http://myjeeva.com") +// +// // Setting HTTPS address +// resty.SetHostURL("https://myjeeva.com") +// +func (c *Client) SetHostURL(url string) *Client { + c.HostURL = strings.TrimRight(url, "/") + return c +} + +// SetHeader method sets a single header field and its value in the client instance. +// These headers will be applied to all requests raised from this client instance. +// Also it can be overridden at request level header options, see `resty.R().SetHeader` +// or `resty.R().SetHeaders`. +// +// Example: To set `Content-Type` and `Accept` as `application/json` +// +// resty. +// SetHeader("Content-Type", "application/json"). +// SetHeader("Accept", "application/json") +// +func (c *Client) SetHeader(header, value string) *Client { + c.Header.Set(header, value) + return c +} + +// SetHeaders method sets multiple headers field and its values at one go in the client instance. +// These headers will be applied to all requests raised from this client instance. Also it can be +// overridden at request level headers options, see `resty.R().SetHeaders` or `resty.R().SetHeader`. +// +// Example: To set `Content-Type` and `Accept` as `application/json` +// +// resty.SetHeaders(map[string]string{ +// "Content-Type": "application/json", +// "Accept": "application/json", +// }) +// +func (c *Client) SetHeaders(headers map[string]string) *Client { + for h, v := range headers { + c.Header.Set(h, v) + } + + return c +} + +// SetCookieJar method sets custom http.CookieJar in the resty client. Its way to override default. +// Example: sometimes we don't want to save cookies in api contacting, we can remove the default +// CookieJar in resty client. +// +// resty.SetCookieJar(nil) +// +func (c *Client) SetCookieJar(jar http.CookieJar) *Client { + c.httpClient.Jar = jar + return c +} + +// SetCookie method appends a single cookie in the client instance. +// These cookies will be added to all the request raised from this client instance. +// resty.SetCookie(&http.Cookie{ +// Name:"go-resty", +// Value:"This is cookie value", +// Path: "/", +// Domain: "sample.com", +// MaxAge: 36000, +// HttpOnly: true, +// Secure: false, +// }) +// +func (c *Client) SetCookie(hc *http.Cookie) *Client { + c.Cookies = append(c.Cookies, hc) + return c +} + +// SetCookies method sets an array of cookies in the client instance. +// These cookies will be added to all the request raised from this client instance. +// cookies := make([]*http.Cookie, 0) +// +// cookies = append(cookies, &http.Cookie{ +// Name:"go-resty-1", +// Value:"This is cookie 1 value", +// Path: "/", +// Domain: "sample.com", +// MaxAge: 36000, +// HttpOnly: true, +// Secure: false, +// }) +// +// cookies = append(cookies, &http.Cookie{ +// Name:"go-resty-2", +// Value:"This is cookie 2 value", +// Path: "/", +// Domain: "sample.com", +// MaxAge: 36000, +// HttpOnly: true, +// Secure: false, +// }) +// +// // Setting a cookies into resty +// resty.SetCookies(cookies) +// +func (c *Client) SetCookies(cs []*http.Cookie) *Client { + c.Cookies = append(c.Cookies, cs...) + return c +} + +// SetQueryParam method sets single parameter and its value in the client instance. +// It will be formed as query string for the request. For example: `search=kitchen%20papers&size=large` +// in the URL after `?` mark. These query params will be added to all the request raised from +// this client instance. Also it can be overridden at request level Query Param options, +// see `resty.R().SetQueryParam` or `resty.R().SetQueryParams`. +// resty. +// SetQueryParam("search", "kitchen papers"). +// SetQueryParam("size", "large") +// +func (c *Client) SetQueryParam(param, value string) *Client { + c.QueryParam.Set(param, value) + return c +} + +// SetQueryParams method sets multiple parameters and their values at one go in the client instance. +// It will be formed as query string for the request. For example: `search=kitchen%20papers&size=large` +// in the URL after `?` mark. These query params will be added to all the request raised from this +// client instance. Also it can be overridden at request level Query Param options, +// see `resty.R().SetQueryParams` or `resty.R().SetQueryParam`. +// resty.SetQueryParams(map[string]string{ +// "search": "kitchen papers", +// "size": "large", +// }) +// +func (c *Client) SetQueryParams(params map[string]string) *Client { + for p, v := range params { + c.SetQueryParam(p, v) + } + + return c +} + +// SetFormData method sets Form parameters and their values in the client instance. +// It's applicable only HTTP method `POST` and `PUT` and requets content type would be set as +// `application/x-www-form-urlencoded`. These form data will be added to all the request raised from +// this client instance. Also it can be overridden at request level form data, see `resty.R().SetFormData`. +// resty.SetFormData(map[string]string{ +// "access_token": "BC594900-518B-4F7E-AC75-BD37F019E08F", +// "user_id": "3455454545", +// }) +// +func (c *Client) SetFormData(data map[string]string) *Client { + for k, v := range data { + c.FormData.Set(k, v) + } + + return c +} + +// SetBasicAuth method sets the basic authentication header in the HTTP request. Example: +// Authorization: Basic +// +// Example: To set the header for username "go-resty" and password "welcome" +// resty.SetBasicAuth("go-resty", "welcome") +// +// This basic auth information gets added to all the request rasied from this client instance. +// Also it can be overridden or set one at the request level is supported, see `resty.R().SetBasicAuth`. +// +func (c *Client) SetBasicAuth(username, password string) *Client { + c.UserInfo = &User{Username: username, Password: password} + return c +} + +// SetAuthToken method sets bearer auth token header in the HTTP request. Example: +// Authorization: Bearer +// +// Example: To set auth token BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F +// +// resty.SetAuthToken("BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F") +// +// This bearer auth token gets added to all the request rasied from this client instance. +// Also it can be overridden or set one at the request level is supported, see `resty.R().SetAuthToken`. +// +func (c *Client) SetAuthToken(token string) *Client { + c.Token = token + return c +} + +// R method creates a request instance, its used for Get, Post, Put, Delete, Patch, Head and Options. +func (c *Client) R() *Request { + r := &Request{ + QueryParam: url.Values{}, + FormData: url.Values{}, + Header: http.Header{}, + + client: c, + multipartFiles: []*File{}, + multipartFields: []*multipartField{}, + pathParams: map[string]string{}, + jsonEscapeHTML: true, + } + + return r +} + +// NewRequest is an alias for R(). Creates a request instance, its used for +// Get, Post, Put, Delete, Patch, Head and Options. +func (c *Client) NewRequest() *Request { + return c.R() +} + +// OnBeforeRequest method appends request middleware into the before request chain. +// Its gets applied after default `go-resty` request middlewares and before request +// been sent from `go-resty` to host server. +// resty.OnBeforeRequest(func(c *resty.Client, r *resty.Request) error { +// // Now you have access to Client and Request instance +// // manipulate it as per your need +// +// return nil // if its success otherwise return error +// }) +// +func (c *Client) OnBeforeRequest(m func(*Client, *Request) error) *Client { + c.udBeforeRequest = append(c.udBeforeRequest, m) + return c +} + +// OnAfterResponse method appends response middleware into the after response chain. +// Once we receive response from host server, default `go-resty` response middleware +// gets applied and then user assigened response middlewares applied. +// resty.OnAfterResponse(func(c *resty.Client, r *resty.Response) error { +// // Now you have access to Client and Response instance +// // manipulate it as per your need +// +// return nil // if its success otherwise return error +// }) +// +func (c *Client) OnAfterResponse(m func(*Client, *Response) error) *Client { + c.afterResponse = append(c.afterResponse, m) + return c +} + +// SetPreRequestHook method sets the given pre-request function into resty client. +// It is called right before the request is fired. +// +// Note: Only one pre-request hook can be registered. Use `resty.OnBeforeRequest` for mutilple. +func (c *Client) SetPreRequestHook(h func(*Client, *Request) error) *Client { + if c.preReqHook != nil { + c.Log.Printf("Overwriting an existing pre-request hook: %s", functionName(h)) + } + c.preReqHook = h + return c +} + +// SetDebug method enables the debug mode on `go-resty` client. Client logs details of every request and response. +// For `Request` it logs information such as HTTP verb, Relative URL path, Host, Headers, Body if it has one. +// For `Response` it logs information such as Status, Response Time, Headers, Body if it has one. +// resty.SetDebug(true) +// +func (c *Client) SetDebug(d bool) *Client { + c.Debug = d + return c +} + +// SetDebugBodyLimit sets the maximum size for which the response body will be logged in debug mode. +// resty.SetDebugBodyLimit(1000000) +// +func (c *Client) SetDebugBodyLimit(sl int64) *Client { + c.debugBodySizeLimit = sl + return c +} + +// SetDisableWarn method disables the warning message on `go-resty` client. +// For example: go-resty warns the user when BasicAuth used on HTTP mode. +// resty.SetDisableWarn(true) +// +func (c *Client) SetDisableWarn(d bool) *Client { + c.DisableWarn = d + return c +} + +// SetAllowGetMethodPayload method allows the GET method with payload on `go-resty` client. +// For example: go-resty allows the user sends request with a payload on HTTP GET method. +// resty.SetAllowGetMethodPayload(true) +// +func (c *Client) SetAllowGetMethodPayload(a bool) *Client { + c.AllowGetMethodPayload = a + return c +} + +// SetLogger method sets given writer for logging go-resty request and response details. +// Default is os.Stderr +// file, _ := os.OpenFile("/Users/jeeva/go-resty.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) +// +// resty.SetLogger(file) +// +func (c *Client) SetLogger(w io.Writer) *Client { + c.Log = getLogger(w) + return c +} + +// SetContentLength method enables the HTTP header `Content-Length` value for every request. +// By default go-resty won't set `Content-Length`. +// resty.SetContentLength(true) +// +// Also you have an option to enable for particular request. See `resty.R().SetContentLength` +// +func (c *Client) SetContentLength(l bool) *Client { + c.setContentLength = l + return c +} + +// SetTimeout method sets timeout for request raised from client. +// resty.SetTimeout(time.Duration(1 * time.Minute)) +// +func (c *Client) SetTimeout(timeout time.Duration) *Client { + c.httpClient.Timeout = timeout + return c +} + +// SetError method is to register the global or client common `Error` object into go-resty. +// It is used for automatic unmarshalling if response status code is greater than 399 and +// content type either JSON or XML. Can be pointer or non-pointer. +// resty.SetError(&Error{}) +// // OR +// resty.SetError(Error{}) +// +func (c *Client) SetError(err interface{}) *Client { + c.Error = typeOf(err) + return c +} + +// SetRedirectPolicy method sets the client redirect poilicy. go-resty provides ready to use +// redirect policies. Wanna create one for yourself refer `redirect.go`. +// +// resty.SetRedirectPolicy(FlexibleRedirectPolicy(20)) +// +// // Need multiple redirect policies together +// resty.SetRedirectPolicy(FlexibleRedirectPolicy(20), DomainCheckRedirectPolicy("host1.com", "host2.net")) +// +func (c *Client) SetRedirectPolicy(policies ...interface{}) *Client { + for _, p := range policies { + if _, ok := p.(RedirectPolicy); !ok { + c.Log.Printf("ERORR: %v does not implement resty.RedirectPolicy (missing Apply method)", + functionName(p)) + } + } + + c.httpClient.CheckRedirect = func(req *http.Request, via []*http.Request) error { + for _, p := range policies { + if err := p.(RedirectPolicy).Apply(req, via); err != nil { + return err + } + } + return nil // looks good, go ahead + } + + return c +} + +// SetRetryCount method enables retry on `go-resty` client and allows you +// to set no. of retry count. Resty uses a Backoff mechanism. +func (c *Client) SetRetryCount(count int) *Client { + c.RetryCount = count + return c +} + +// SetRetryWaitTime method sets default wait time to sleep before retrying +// request. +// Default is 100 milliseconds. +func (c *Client) SetRetryWaitTime(waitTime time.Duration) *Client { + c.RetryWaitTime = waitTime + return c +} + +// SetRetryMaxWaitTime method sets max wait time to sleep before retrying +// request. +// Default is 2 seconds. +func (c *Client) SetRetryMaxWaitTime(maxWaitTime time.Duration) *Client { + c.RetryMaxWaitTime = maxWaitTime + return c +} + +// AddRetryCondition method adds a retry condition function to array of functions +// that are checked to determine if the request is retried. The request will +// retry if any of the functions return true and error is nil. +func (c *Client) AddRetryCondition(condition RetryConditionFunc) *Client { + c.RetryConditions = append(c.RetryConditions, condition) + return c +} + +// SetHTTPMode method sets go-resty mode to 'http' +func (c *Client) SetHTTPMode() *Client { + return c.SetMode("http") +} + +// SetRESTMode method sets go-resty mode to 'rest' +func (c *Client) SetRESTMode() *Client { + return c.SetMode("rest") +} + +// SetMode method sets go-resty client mode to given value such as 'http' & 'rest'. +// 'rest': +// - No Redirect +// - Automatic response unmarshal if it is JSON or XML +// 'http': +// - Up to 10 Redirects +// - No automatic unmarshall. Response will be treated as `response.String()` +// +// If you want more redirects, use FlexibleRedirectPolicy +// resty.SetRedirectPolicy(FlexibleRedirectPolicy(20)) +// +func (c *Client) SetMode(mode string) *Client { + // HTTP + if mode == "http" { + c.isHTTPMode = true + c.SetRedirectPolicy(FlexibleRedirectPolicy(10)) + c.afterResponse = []func(*Client, *Response) error{ + responseLogger, + saveResponseIntoFile, + } + return c + } + + // RESTful + c.isHTTPMode = false + c.SetRedirectPolicy(NoRedirectPolicy()) + c.afterResponse = []func(*Client, *Response) error{ + responseLogger, + parseResponseBody, + saveResponseIntoFile, + } + return c +} + +// Mode method returns the current client mode. Typically its a "http" or "rest". +// Default is "rest" +func (c *Client) Mode() string { + if c.isHTTPMode { + return "http" + } + return "rest" +} + +// SetTLSClientConfig method sets TLSClientConfig for underling client Transport. +// +// Example: +// // One can set custom root-certificate. Refer: http://golang.org/pkg/crypto/tls/#example_Dial +// resty.SetTLSClientConfig(&tls.Config{ RootCAs: roots }) +// +// // or One can disable security check (https) +// resty.SetTLSClientConfig(&tls.Config{ InsecureSkipVerify: true }) +// Note: This method overwrites existing `TLSClientConfig`. +// +func (c *Client) SetTLSClientConfig(config *tls.Config) *Client { + transport, err := c.getTransport() + if err != nil { + c.Log.Printf("ERROR %v", err) + return c + } + transport.TLSClientConfig = config + return c +} + +// SetProxy method sets the Proxy URL and Port for resty client. +// resty.SetProxy("http://proxyserver:8888") +// +// Alternatives: At request level proxy, see `Request.SetProxy`. OR Without this `SetProxy` method, +// you can also set Proxy via environment variable. By default `Go` uses setting from `HTTP_PROXY`. +// +func (c *Client) SetProxy(proxyURL string) *Client { + transport, err := c.getTransport() + if err != nil { + c.Log.Printf("ERROR %v", err) + return c + } + + if pURL, err := url.Parse(proxyURL); err == nil { + c.proxyURL = pURL + transport.Proxy = http.ProxyURL(c.proxyURL) + } else { + c.Log.Printf("ERROR %v", err) + c.RemoveProxy() + } + return c +} + +// RemoveProxy method removes the proxy configuration from resty client +// resty.RemoveProxy() +// +func (c *Client) RemoveProxy() *Client { + transport, err := c.getTransport() + if err != nil { + c.Log.Printf("ERROR %v", err) + return c + } + c.proxyURL = nil + transport.Proxy = nil + return c +} + +// SetCertificates method helps to set client certificates into resty conveniently. +// +func (c *Client) SetCertificates(certs ...tls.Certificate) *Client { + config, err := c.getTLSConfig() + if err != nil { + c.Log.Printf("ERROR %v", err) + return c + } + config.Certificates = append(config.Certificates, certs...) + return c +} + +// SetRootCertificate method helps to add one or more root certificates into resty client +// resty.SetRootCertificate("/path/to/root/pemFile.pem") +// +func (c *Client) SetRootCertificate(pemFilePath string) *Client { + rootPemData, err := ioutil.ReadFile(pemFilePath) + if err != nil { + c.Log.Printf("ERROR %v", err) + return c + } + + config, err := c.getTLSConfig() + if err != nil { + c.Log.Printf("ERROR %v", err) + return c + } + if config.RootCAs == nil { + config.RootCAs = x509.NewCertPool() + } + + config.RootCAs.AppendCertsFromPEM(rootPemData) + + return c +} + +// SetOutputDirectory method sets output directory for saving HTTP response into file. +// If the output directory not exists then resty creates one. This setting is optional one, +// if you're planning using absoule path in `Request.SetOutput` and can used together. +// resty.SetOutputDirectory("/save/http/response/here") +// +func (c *Client) SetOutputDirectory(dirPath string) *Client { + c.outputDirectory = dirPath + return c +} + +// SetTransport method sets custom `*http.Transport` or any `http.RoundTripper` +// compatible interface implementation in the resty client. +// +// NOTE: +// +// - If transport is not type of `*http.Transport` then you may not be able to +// take advantage of some of the `resty` client settings. +// +// - It overwrites the resty client transport instance and it's configurations. +// +// transport := &http.Transport{ +// // somthing like Proxying to httptest.Server, etc... +// Proxy: func(req *http.Request) (*url.URL, error) { +// return url.Parse(server.URL) +// }, +// } +// +// resty.SetTransport(transport) +// +func (c *Client) SetTransport(transport http.RoundTripper) *Client { + if transport != nil { + c.httpClient.Transport = transport + } + return c +} + +// SetScheme method sets custom scheme in the resty client. It's way to override default. +// resty.SetScheme("http") +// +func (c *Client) SetScheme(scheme string) *Client { + if !IsStringEmpty(scheme) { + c.scheme = scheme + } + + return c +} + +// SetCloseConnection method sets variable `Close` in http request struct with the given +// value. More info: https://golang.org/src/net/http/request.go +func (c *Client) SetCloseConnection(close bool) *Client { + c.closeConnection = close + return c +} + +// SetDoNotParseResponse method instructs `Resty` not to parse the response body automatically. +// Resty exposes the raw response body as `io.ReadCloser`. Also do not forget to close the body, +// otherwise you might get into connection leaks, no connection reuse. +// +// Please Note: Response middlewares are not applicable, if you use this option. Basically you have +// taken over the control of response parsing from `Resty`. +func (c *Client) SetDoNotParseResponse(parse bool) *Client { + c.notParseResponse = parse + return c +} + +// SetLogPrefix method sets the Resty logger prefix value. +func (c *Client) SetLogPrefix(prefix string) *Client { + c.logPrefix = prefix + c.Log.SetPrefix(prefix) + return c +} + +// SetPathParams method sets multiple URL path key-value pairs at one go in the +// resty client instance. +// resty.SetPathParams(map[string]string{ +// "userId": "sample@sample.com", +// "subAccountId": "100002", +// }) +// +// Result: +// URL - /v1/users/{userId}/{subAccountId}/details +// Composed URL - /v1/users/sample@sample.com/100002/details +// It replace the value of the key while composing request URL. Also it can be +// overridden at request level Path Params options, see `Request.SetPathParams`. +func (c *Client) SetPathParams(params map[string]string) *Client { + for p, v := range params { + c.pathParams[p] = v + } + return c +} + +// SetJSONEscapeHTML method is to enable/disable the HTML escape on JSON marshal. +// +// NOTE: This option only applicable to standard JSON Marshaller. +func (c *Client) SetJSONEscapeHTML(b bool) *Client { + c.jsonEscapeHTML = b + return c +} + +// IsProxySet method returns the true if proxy is set on client otherwise false. +func (c *Client) IsProxySet() bool { + return c.proxyURL != nil +} + +// GetClient method returns the current http.Client used by the resty client. +func (c *Client) GetClient() *http.Client { + return c.httpClient +} + +//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ +// Client Unexported methods +//___________________________________ + +// executes the given `Request` object and returns response +func (c *Client) execute(req *Request) (*Response, error) { + defer releaseBuffer(req.bodyBuf) + // Apply Request middleware + var err error + + // user defined on before request methods + // to modify the *resty.Request object + for _, f := range c.udBeforeRequest { + if err = f(c, req); err != nil { + return nil, err + } + } + + // resty middlewares + for _, f := range c.beforeRequest { + if err = f(c, req); err != nil { + return nil, err + } + } + + // call pre-request if defined + if c.preReqHook != nil { + if err = c.preReqHook(c, req); err != nil { + return nil, err + } + } + + if hostHeader := req.Header.Get("Host"); hostHeader != "" { + req.RawRequest.Host = hostHeader + } + + req.Time = time.Now() + resp, err := c.httpClient.Do(req.RawRequest) + + response := &Response{ + Request: req, + RawResponse: resp, + receivedAt: time.Now(), + } + + if err != nil || req.notParseResponse || c.notParseResponse { + return response, err + } + + if !req.isSaveResponse { + defer closeq(resp.Body) + body := resp.Body + + // GitHub #142 + if strings.EqualFold(resp.Header.Get(hdrContentEncodingKey), "gzip") && resp.ContentLength > 0 { + if _, ok := body.(*gzip.Reader); !ok { + body, err = gzip.NewReader(body) + if err != nil { + return response, err + } + defer closeq(body) + } + } + + if response.body, err = ioutil.ReadAll(body); err != nil { + return response, err + } + + response.size = int64(len(response.body)) + } + + // Apply Response middleware + for _, f := range c.afterResponse { + if err = f(c, response); err != nil { + break + } + } + + return response, err +} + +// enables a log prefix +func (c *Client) enableLogPrefix() { + c.Log.SetFlags(log.LstdFlags) + c.Log.SetPrefix(c.logPrefix) +} + +// disables a log prefix +func (c *Client) disableLogPrefix() { + c.Log.SetFlags(0) + c.Log.SetPrefix("") +} + +// getting TLS client config if not exists then create one +func (c *Client) getTLSConfig() (*tls.Config, error) { + transport, err := c.getTransport() + if err != nil { + return nil, err + } + if transport.TLSClientConfig == nil { + transport.TLSClientConfig = &tls.Config{} + } + return transport.TLSClientConfig, nil +} + +// returns `*http.Transport` currently in use or error +// in case currently used `transport` is not an `*http.Transport` +func (c *Client) getTransport() (*http.Transport, error) { + if c.httpClient.Transport == nil { + c.SetTransport(new(http.Transport)) + } + + if transport, ok := c.httpClient.Transport.(*http.Transport); ok { + return transport, nil + } + return nil, errors.New("current transport is not an *http.Transport instance") +} + +// +// File +// + +// File represent file information for multipart request +type File struct { + Name string + ParamName string + io.Reader +} + +// String returns string value of current file details +func (f *File) String() string { + return fmt.Sprintf("ParamName: %v; FileName: %v", f.ParamName, f.Name) +} + +// multipartField represent custom data part for multipart request +type multipartField struct { + Param string + FileName string + ContentType string + io.Reader +} diff --git a/vendor/github.com/go-resty/resty/default.go b/vendor/github.com/go-resty/resty/default.go new file mode 100644 index 000000000..4fc7b2dc4 --- /dev/null +++ b/vendor/github.com/go-resty/resty/default.go @@ -0,0 +1,328 @@ +// Copyright (c) 2015-2018 Jeevanandam M (jeeva@myjeeva.com), All rights reserved. +// resty source code and usage is governed by a MIT style +// license that can be found in the LICENSE file. + +package resty + +import ( + "crypto/tls" + "encoding/json" + "io" + "math" + "net/http" + "net/http/cookiejar" + "net/url" + "os" + "time" + + "golang.org/x/net/publicsuffix" +) + +// DefaultClient of resty +var DefaultClient *Client + +// New method creates a new go-resty client. +func New() *Client { + cookieJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List}) + return createClient(&http.Client{Jar: cookieJar}) +} + +// NewWithClient method create a new go-resty client with given `http.Client`. +func NewWithClient(hc *http.Client) *Client { + return createClient(hc) +} + +// R creates a new resty request object, it is used form a HTTP/RESTful request +// such as GET, POST, PUT, DELETE, HEAD, PATCH and OPTIONS. +func R() *Request { + return DefaultClient.R() +} + +// NewRequest is an alias for R(). Creates a new resty request object, it is used form a HTTP/RESTful request +// such as GET, POST, PUT, DELETE, HEAD, PATCH and OPTIONS. +func NewRequest() *Request { + return R() +} + +// SetHostURL sets Host URL. See `Client.SetHostURL for more information. +func SetHostURL(url string) *Client { + return DefaultClient.SetHostURL(url) +} + +// SetHeader sets single header. See `Client.SetHeader` for more information. +func SetHeader(header, value string) *Client { + return DefaultClient.SetHeader(header, value) +} + +// SetHeaders sets multiple headers. See `Client.SetHeaders` for more information. +func SetHeaders(headers map[string]string) *Client { + return DefaultClient.SetHeaders(headers) +} + +// SetCookieJar sets custom http.CookieJar. See `Client.SetCookieJar` for more information. +func SetCookieJar(jar http.CookieJar) *Client { + return DefaultClient.SetCookieJar(jar) +} + +// SetCookie sets single cookie object. See `Client.SetCookie` for more information. +func SetCookie(hc *http.Cookie) *Client { + return DefaultClient.SetCookie(hc) +} + +// SetCookies sets multiple cookie object. See `Client.SetCookies` for more information. +func SetCookies(cs []*http.Cookie) *Client { + return DefaultClient.SetCookies(cs) +} + +// SetQueryParam method sets single parameter and its value. See `Client.SetQueryParam` for more information. +func SetQueryParam(param, value string) *Client { + return DefaultClient.SetQueryParam(param, value) +} + +// SetQueryParams method sets multiple parameters and its value. See `Client.SetQueryParams` for more information. +func SetQueryParams(params map[string]string) *Client { + return DefaultClient.SetQueryParams(params) +} + +// SetFormData method sets Form parameters and its values. See `Client.SetFormData` for more information. +func SetFormData(data map[string]string) *Client { + return DefaultClient.SetFormData(data) +} + +// SetBasicAuth method sets the basic authentication header. See `Client.SetBasicAuth` for more information. +func SetBasicAuth(username, password string) *Client { + return DefaultClient.SetBasicAuth(username, password) +} + +// SetAuthToken method sets bearer auth token header. See `Client.SetAuthToken` for more information. +func SetAuthToken(token string) *Client { + return DefaultClient.SetAuthToken(token) +} + +// OnBeforeRequest method sets request middleware. See `Client.OnBeforeRequest` for more information. +func OnBeforeRequest(m func(*Client, *Request) error) *Client { + return DefaultClient.OnBeforeRequest(m) +} + +// OnAfterResponse method sets response middleware. See `Client.OnAfterResponse` for more information. +func OnAfterResponse(m func(*Client, *Response) error) *Client { + return DefaultClient.OnAfterResponse(m) +} + +// SetPreRequestHook method sets the pre-request hook. See `Client.SetPreRequestHook` for more information. +func SetPreRequestHook(h func(*Client, *Request) error) *Client { + return DefaultClient.SetPreRequestHook(h) +} + +// SetDebug method enables the debug mode. See `Client.SetDebug` for more information. +func SetDebug(d bool) *Client { + return DefaultClient.SetDebug(d) +} + +// SetDebugBodyLimit method sets the response body limit for debug mode. See `Client.SetDebugBodyLimit` for more information. +func SetDebugBodyLimit(sl int64) *Client { + return DefaultClient.SetDebugBodyLimit(sl) +} + +// SetAllowGetMethodPayload method allows the GET method with payload. See `Client.SetAllowGetMethodPayload` for more information. +func SetAllowGetMethodPayload(a bool) *Client { + return DefaultClient.SetAllowGetMethodPayload(a) +} + +// SetRetryCount method sets the retry count. See `Client.SetRetryCount` for more information. +func SetRetryCount(count int) *Client { + return DefaultClient.SetRetryCount(count) +} + +// SetRetryWaitTime method sets the retry wait time. See `Client.SetRetryWaitTime` for more information. +func SetRetryWaitTime(waitTime time.Duration) *Client { + return DefaultClient.SetRetryWaitTime(waitTime) +} + +// SetRetryMaxWaitTime method sets the retry max wait time. See `Client.SetRetryMaxWaitTime` for more information. +func SetRetryMaxWaitTime(maxWaitTime time.Duration) *Client { + return DefaultClient.SetRetryMaxWaitTime(maxWaitTime) +} + +// AddRetryCondition method appends check function for retry. See `Client.AddRetryCondition` for more information. +func AddRetryCondition(condition RetryConditionFunc) *Client { + return DefaultClient.AddRetryCondition(condition) +} + +// SetDisableWarn method disables warning comes from `go-resty` client. See `Client.SetDisableWarn` for more information. +func SetDisableWarn(d bool) *Client { + return DefaultClient.SetDisableWarn(d) +} + +// SetLogger method sets given writer for logging. See `Client.SetLogger` for more information. +func SetLogger(w io.Writer) *Client { + return DefaultClient.SetLogger(w) +} + +// SetContentLength method enables `Content-Length` value. See `Client.SetContentLength` for more information. +func SetContentLength(l bool) *Client { + return DefaultClient.SetContentLength(l) +} + +// SetError method is to register the global or client common `Error` object. See `Client.SetError` for more information. +func SetError(err interface{}) *Client { + return DefaultClient.SetError(err) +} + +// SetRedirectPolicy method sets the client redirect poilicy. See `Client.SetRedirectPolicy` for more information. +func SetRedirectPolicy(policies ...interface{}) *Client { + return DefaultClient.SetRedirectPolicy(policies...) +} + +// SetHTTPMode method sets go-resty mode into HTTP. See `Client.SetMode` for more information. +func SetHTTPMode() *Client { + return DefaultClient.SetHTTPMode() +} + +// SetRESTMode method sets go-resty mode into RESTful. See `Client.SetMode` for more information. +func SetRESTMode() *Client { + return DefaultClient.SetRESTMode() +} + +// Mode method returns the current client mode. See `Client.Mode` for more information. +func Mode() string { + return DefaultClient.Mode() +} + +// SetTLSClientConfig method sets TLSClientConfig for underling client Transport. See `Client.SetTLSClientConfig` for more information. +func SetTLSClientConfig(config *tls.Config) *Client { + return DefaultClient.SetTLSClientConfig(config) +} + +// SetTimeout method sets timeout for request. See `Client.SetTimeout` for more information. +func SetTimeout(timeout time.Duration) *Client { + return DefaultClient.SetTimeout(timeout) +} + +// SetProxy method sets Proxy for request. See `Client.SetProxy` for more information. +func SetProxy(proxyURL string) *Client { + return DefaultClient.SetProxy(proxyURL) +} + +// RemoveProxy method removes the proxy configuration. See `Client.RemoveProxy` for more information. +func RemoveProxy() *Client { + return DefaultClient.RemoveProxy() +} + +// SetCertificates method helps to set client certificates into resty conveniently. +// See `Client.SetCertificates` for more information and example. +func SetCertificates(certs ...tls.Certificate) *Client { + return DefaultClient.SetCertificates(certs...) +} + +// SetRootCertificate method helps to add one or more root certificates into resty client. +// See `Client.SetRootCertificate` for more information. +func SetRootCertificate(pemFilePath string) *Client { + return DefaultClient.SetRootCertificate(pemFilePath) +} + +// SetOutputDirectory method sets output directory. See `Client.SetOutputDirectory` for more information. +func SetOutputDirectory(dirPath string) *Client { + return DefaultClient.SetOutputDirectory(dirPath) +} + +// SetTransport method sets custom `*http.Transport` or any `http.RoundTripper` +// compatible interface implementation in the resty client. +// See `Client.SetTransport` for more information. +func SetTransport(transport http.RoundTripper) *Client { + return DefaultClient.SetTransport(transport) +} + +// SetScheme method sets custom scheme in the resty client. +// See `Client.SetScheme` for more information. +func SetScheme(scheme string) *Client { + return DefaultClient.SetScheme(scheme) +} + +// SetCloseConnection method sets close connection value in the resty client. +// See `Client.SetCloseConnection` for more information. +func SetCloseConnection(close bool) *Client { + return DefaultClient.SetCloseConnection(close) +} + +// SetDoNotParseResponse method instructs `Resty` not to parse the response body automatically. +// See `Client.SetDoNotParseResponse` for more information. +func SetDoNotParseResponse(parse bool) *Client { + return DefaultClient.SetDoNotParseResponse(parse) +} + +// SetPathParams method sets the Request path parameter key-value pairs. See +// `Client.SetPathParams` for more information. +func SetPathParams(params map[string]string) *Client { + return DefaultClient.SetPathParams(params) +} + +// IsProxySet method returns the true if proxy is set on client otherwise false. +// See `Client.IsProxySet` for more information. +func IsProxySet() bool { + return DefaultClient.IsProxySet() +} + +// GetClient method returns the current `http.Client` used by the default resty client. +func GetClient() *http.Client { + return DefaultClient.httpClient +} + +//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ +// Unexported methods +//___________________________________ + +func createClient(hc *http.Client) *Client { + c := &Client{ + HostURL: "", + QueryParam: url.Values{}, + FormData: url.Values{}, + Header: http.Header{}, + UserInfo: nil, + Token: "", + Cookies: make([]*http.Cookie, 0), + Debug: false, + Log: getLogger(os.Stderr), + RetryCount: 0, + RetryWaitTime: defaultWaitTime, + RetryMaxWaitTime: defaultMaxWaitTime, + JSONMarshal: json.Marshal, + JSONUnmarshal: json.Unmarshal, + jsonEscapeHTML: true, + httpClient: hc, + debugBodySizeLimit: math.MaxInt32, + pathParams: make(map[string]string), + } + + // Log Prefix + c.SetLogPrefix("RESTY ") + + // Default redirect policy + c.SetRedirectPolicy(NoRedirectPolicy()) + + // default before request middlewares + c.beforeRequest = []func(*Client, *Request) error{ + parseRequestURL, + parseRequestHeader, + parseRequestBody, + createHTTPRequest, + addCredentials, + requestLogger, + } + + // user defined request middlewares + c.udBeforeRequest = []func(*Client, *Request) error{} + + // default after response middlewares + c.afterResponse = []func(*Client, *Response) error{ + responseLogger, + parseResponseBody, + saveResponseIntoFile, + } + + return c +} + +func init() { + DefaultClient = New() +} diff --git a/vendor/github.com/go-resty/resty/middleware.go b/vendor/github.com/go-resty/resty/middleware.go new file mode 100644 index 000000000..7b7ceb726 --- /dev/null +++ b/vendor/github.com/go-resty/resty/middleware.go @@ -0,0 +1,453 @@ +// Copyright (c) 2015-2018 Jeevanandam M (jeeva@myjeeva.com), All rights reserved. +// resty source code and usage is governed by a MIT style +// license that can be found in the LICENSE file. + +package resty + +import ( + "bytes" + "encoding/xml" + "errors" + "fmt" + "io" + "mime/multipart" + "net/http" + "net/url" + "os" + "path/filepath" + "reflect" + "strings" + "time" +) + +//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ +// Request Middleware(s) +//___________________________________ + +func parseRequestURL(c *Client, r *Request) error { + // GitHub #103 Path Params + if len(r.pathParams) > 0 { + for p, v := range r.pathParams { + r.URL = strings.Replace(r.URL, "{"+p+"}", v, -1) + } + } + if len(c.pathParams) > 0 { + for p, v := range c.pathParams { + r.URL = strings.Replace(r.URL, "{"+p+"}", v, -1) + } + } + + // Parsing request URL + reqURL, err := url.Parse(r.URL) + if err != nil { + return err + } + + // If Request.URL is relative path then added c.HostURL into + // the request URL otherwise Request.URL will be used as-is + if !reqURL.IsAbs() { + r.URL = reqURL.String() + if len(r.URL) > 0 && r.URL[0] != '/' { + r.URL = "/" + r.URL + } + + reqURL, err = url.Parse(c.HostURL + r.URL) + if err != nil { + return err + } + } + + // Adding Query Param + query := make(url.Values) + for k, v := range c.QueryParam { + for _, iv := range v { + query.Add(k, iv) + } + } + + for k, v := range r.QueryParam { + // remove query param from client level by key + // since overrides happens for that key in the request + query.Del(k) + + for _, iv := range v { + query.Add(k, iv) + } + } + + // GitHub #123 Preserve query string order partially. + // Since not feasible in `SetQuery*` resty methods, because + // standard package `url.Encode(...)` sorts the query params + // alphabetically + if len(query) > 0 { + if IsStringEmpty(reqURL.RawQuery) { + reqURL.RawQuery = query.Encode() + } else { + reqURL.RawQuery = reqURL.RawQuery + "&" + query.Encode() + } + } + + r.URL = reqURL.String() + + return nil +} + +func parseRequestHeader(c *Client, r *Request) error { + hdr := make(http.Header) + for k := range c.Header { + hdr[k] = append(hdr[k], c.Header[k]...) + } + + for k := range r.Header { + hdr.Del(k) + hdr[k] = append(hdr[k], r.Header[k]...) + } + + if IsStringEmpty(hdr.Get(hdrUserAgentKey)) { + hdr.Set(hdrUserAgentKey, fmt.Sprintf(hdrUserAgentValue, Version)) + } + + ct := hdr.Get(hdrContentTypeKey) + if IsStringEmpty(hdr.Get(hdrAcceptKey)) && !IsStringEmpty(ct) && + (IsJSONType(ct) || IsXMLType(ct)) { + hdr.Set(hdrAcceptKey, hdr.Get(hdrContentTypeKey)) + } + + r.Header = hdr + + return nil +} + +func parseRequestBody(c *Client, r *Request) (err error) { + if isPayloadSupported(r.Method, c.AllowGetMethodPayload) { + // Handling Multipart + if r.isMultiPart && !(r.Method == MethodPatch) { + if err = handleMultipart(c, r); err != nil { + return + } + + goto CL + } + + // Handling Form Data + if len(c.FormData) > 0 || len(r.FormData) > 0 { + handleFormData(c, r) + + goto CL + } + + // Handling Request body + if r.Body != nil { + handleContentType(c, r) + + if err = handleRequestBody(c, r); err != nil { + return + } + } + } + +CL: + // by default resty won't set content length, you can if you want to :) + if (c.setContentLength || r.setContentLength) && r.bodyBuf != nil { + r.Header.Set(hdrContentLengthKey, fmt.Sprintf("%d", r.bodyBuf.Len())) + } + + return +} + +func createHTTPRequest(c *Client, r *Request) (err error) { + if r.bodyBuf == nil { + if reader, ok := r.Body.(io.Reader); ok { + r.RawRequest, err = http.NewRequest(r.Method, r.URL, reader) + } else { + r.RawRequest, err = http.NewRequest(r.Method, r.URL, nil) + } + } else { + r.RawRequest, err = http.NewRequest(r.Method, r.URL, r.bodyBuf) + } + + if err != nil { + return + } + + // Assign close connection option + r.RawRequest.Close = c.closeConnection + + // Add headers into http request + r.RawRequest.Header = r.Header + + // Add cookies into http request + for _, cookie := range c.Cookies { + r.RawRequest.AddCookie(cookie) + } + + // it's for non-http scheme option + if r.RawRequest.URL != nil && r.RawRequest.URL.Scheme == "" { + r.RawRequest.URL.Scheme = c.scheme + r.RawRequest.URL.Host = r.URL + } + + // Use context if it was specified + r.addContextIfAvailable() + + return +} + +func addCredentials(c *Client, r *Request) error { + var isBasicAuth bool + // Basic Auth + if r.UserInfo != nil { // takes precedence + r.RawRequest.SetBasicAuth(r.UserInfo.Username, r.UserInfo.Password) + isBasicAuth = true + } else if c.UserInfo != nil { + r.RawRequest.SetBasicAuth(c.UserInfo.Username, c.UserInfo.Password) + isBasicAuth = true + } + + if !c.DisableWarn { + if isBasicAuth && !strings.HasPrefix(r.URL, "https") { + c.Log.Println("WARNING - Using Basic Auth in HTTP mode is not secure.") + } + } + + // Token Auth + if !IsStringEmpty(r.Token) { // takes precedence + r.RawRequest.Header.Set(hdrAuthorizationKey, "Bearer "+r.Token) + } else if !IsStringEmpty(c.Token) { + r.RawRequest.Header.Set(hdrAuthorizationKey, "Bearer "+c.Token) + } + + return nil +} + +func requestLogger(c *Client, r *Request) error { + if c.Debug { + rr := r.RawRequest + reqLog := "\n---------------------- REQUEST LOG -----------------------\n" + + fmt.Sprintf("%s %s %s\n", r.Method, rr.URL.RequestURI(), rr.Proto) + + fmt.Sprintf("HOST : %s\n", rr.URL.Host) + + fmt.Sprintf("HEADERS:\n") + + composeHeaders(rr.Header) + "\n" + + fmt.Sprintf("BODY :\n%v\n", r.fmtBodyString()) + + "----------------------------------------------------------\n" + + c.Log.Print(reqLog) + } + + return nil +} + +//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ +// Response Middleware(s) +//___________________________________ + +func responseLogger(c *Client, res *Response) error { + if c.Debug { + resLog := "\n---------------------- RESPONSE LOG -----------------------\n" + + fmt.Sprintf("STATUS : %s\n", res.Status()) + + fmt.Sprintf("RECEIVED AT : %v\n", res.ReceivedAt().Format(time.RFC3339Nano)) + + fmt.Sprintf("RESPONSE TIME : %v\n", res.Time()) + + "HEADERS:\n" + + composeHeaders(res.Header()) + "\n" + + if res.Request.isSaveResponse { + resLog += fmt.Sprintf("BODY :\n***** RESPONSE WRITTEN INTO FILE *****\n") + } else { + resLog += fmt.Sprintf("BODY :\n%v\n", res.fmtBodyString(c.debugBodySizeLimit)) + } + resLog += "----------------------------------------------------------\n" + + c.Log.Print(resLog) + } + + return nil +} + +func parseResponseBody(c *Client, res *Response) (err error) { + // Handles only JSON or XML content type + ct := firstNonEmpty(res.Header().Get(hdrContentTypeKey), res.Request.fallbackContentType) + if IsJSONType(ct) || IsXMLType(ct) { + // HTTP status code > 199 and < 300, considered as Result + if res.IsSuccess() { + if res.Request.Result != nil { + err = Unmarshalc(c, ct, res.body, res.Request.Result) + return + } + } + + // HTTP status code > 399, considered as Error + if res.IsError() { + // global error interface + if res.Request.Error == nil && c.Error != nil { + res.Request.Error = reflect.New(c.Error).Interface() + } + + if res.Request.Error != nil { + err = Unmarshalc(c, ct, res.body, res.Request.Error) + } + } + } + + return +} + +func handleMultipart(c *Client, r *Request) (err error) { + r.bodyBuf = acquireBuffer() + w := multipart.NewWriter(r.bodyBuf) + + for k, v := range c.FormData { + for _, iv := range v { + if err = w.WriteField(k, iv); err != nil { + return err + } + } + } + + for k, v := range r.FormData { + for _, iv := range v { + if strings.HasPrefix(k, "@") { // file + err = addFile(w, k[1:], iv) + if err != nil { + return + } + } else { // form value + if err = w.WriteField(k, iv); err != nil { + return err + } + } + } + } + + // #21 - adding io.Reader support + if len(r.multipartFiles) > 0 { + for _, f := range r.multipartFiles { + err = addFileReader(w, f) + if err != nil { + return + } + } + } + + // GitHub #130 adding multipart field support with content type + if len(r.multipartFields) > 0 { + for _, mf := range r.multipartFields { + if err = addMultipartFormField(w, mf); err != nil { + return + } + } + } + + r.Header.Set(hdrContentTypeKey, w.FormDataContentType()) + err = w.Close() + + return +} + +func handleFormData(c *Client, r *Request) { + formData := url.Values{} + + for k, v := range c.FormData { + for _, iv := range v { + formData.Add(k, iv) + } + } + + for k, v := range r.FormData { + // remove form data field from client level by key + // since overrides happens for that key in the request + formData.Del(k) + + for _, iv := range v { + formData.Add(k, iv) + } + } + + r.bodyBuf = bytes.NewBuffer([]byte(formData.Encode())) + r.Header.Set(hdrContentTypeKey, formContentType) + r.isFormData = true +} + +func handleContentType(c *Client, r *Request) { + contentType := r.Header.Get(hdrContentTypeKey) + if IsStringEmpty(contentType) { + contentType = DetectContentType(r.Body) + r.Header.Set(hdrContentTypeKey, contentType) + } +} + +func handleRequestBody(c *Client, r *Request) (err error) { + var bodyBytes []byte + contentType := r.Header.Get(hdrContentTypeKey) + kind := kindOf(r.Body) + r.bodyBuf = nil + + if reader, ok := r.Body.(io.Reader); ok { + if c.setContentLength || r.setContentLength { // keep backward compability + r.bodyBuf = acquireBuffer() + _, err = r.bodyBuf.ReadFrom(reader) + r.Body = nil + } else { + // Otherwise buffer less processing for `io.Reader`, sounds good. + return + } + } else if b, ok := r.Body.([]byte); ok { + bodyBytes = b + } else if s, ok := r.Body.(string); ok { + bodyBytes = []byte(s) + } else if IsJSONType(contentType) && + (kind == reflect.Struct || kind == reflect.Map || kind == reflect.Slice) { + bodyBytes, err = jsonMarshal(c, r, r.Body) + } else if IsXMLType(contentType) && (kind == reflect.Struct) { + bodyBytes, err = xml.Marshal(r.Body) + } + + if bodyBytes == nil && r.bodyBuf == nil { + err = errors.New("unsupported 'Body' type/value") + } + + // if any errors during body bytes handling, return it + if err != nil { + return + } + + // []byte into Buffer + if bodyBytes != nil && r.bodyBuf == nil { + r.bodyBuf = acquireBuffer() + _, _ = r.bodyBuf.Write(bodyBytes) + } + + return +} + +func saveResponseIntoFile(c *Client, res *Response) error { + if res.Request.isSaveResponse { + file := "" + + if len(c.outputDirectory) > 0 && !filepath.IsAbs(res.Request.outputFile) { + file += c.outputDirectory + string(filepath.Separator) + } + + file = filepath.Clean(file + res.Request.outputFile) + if err := createDirectory(filepath.Dir(file)); err != nil { + return err + } + + outFile, err := os.Create(file) + if err != nil { + return err + } + defer closeq(outFile) + + // io.Copy reads maximum 32kb size, it is perfect for large file download too + defer closeq(res.RawResponse.Body) + + written, err := io.Copy(outFile, res.RawResponse.Body) + if err != nil { + return err + } + + res.size = written + } + + return nil +} diff --git a/vendor/github.com/go-resty/resty/redirect.go b/vendor/github.com/go-resty/resty/redirect.go new file mode 100644 index 000000000..ac362cc44 --- /dev/null +++ b/vendor/github.com/go-resty/resty/redirect.go @@ -0,0 +1,99 @@ +// Copyright (c) 2015-2018 Jeevanandam M (jeeva@myjeeva.com), All rights reserved. +// resty source code and usage is governed by a MIT style +// license that can be found in the LICENSE file. + +package resty + +import ( + "errors" + "fmt" + "net" + "net/http" + "strings" +) + +type ( + // RedirectPolicy to regulate the redirects in the resty client. + // Objects implementing the RedirectPolicy interface can be registered as + // + // Apply function should return nil to continue the redirect jounery, otherwise + // return error to stop the redirect. + RedirectPolicy interface { + Apply(req *http.Request, via []*http.Request) error + } + + // The RedirectPolicyFunc type is an adapter to allow the use of ordinary functions as RedirectPolicy. + // If f is a function with the appropriate signature, RedirectPolicyFunc(f) is a RedirectPolicy object that calls f. + RedirectPolicyFunc func(*http.Request, []*http.Request) error +) + +// Apply calls f(req, via). +func (f RedirectPolicyFunc) Apply(req *http.Request, via []*http.Request) error { + return f(req, via) +} + +// NoRedirectPolicy is used to disable redirects in the HTTP client +// resty.SetRedirectPolicy(NoRedirectPolicy()) +func NoRedirectPolicy() RedirectPolicy { + return RedirectPolicyFunc(func(req *http.Request, via []*http.Request) error { + return errors.New("auto redirect is disabled") + }) +} + +// FlexibleRedirectPolicy is convenient method to create No of redirect policy for HTTP client. +// resty.SetRedirectPolicy(FlexibleRedirectPolicy(20)) +func FlexibleRedirectPolicy(noOfRedirect int) RedirectPolicy { + return RedirectPolicyFunc(func(req *http.Request, via []*http.Request) error { + if len(via) >= noOfRedirect { + return fmt.Errorf("stopped after %d redirects", noOfRedirect) + } + + checkHostAndAddHeaders(req, via[0]) + + return nil + }) +} + +// DomainCheckRedirectPolicy is convenient method to define domain name redirect rule in resty client. +// Redirect is allowed for only mentioned host in the policy. +// resty.SetRedirectPolicy(DomainCheckRedirectPolicy("host1.com", "host2.org", "host3.net")) +func DomainCheckRedirectPolicy(hostnames ...string) RedirectPolicy { + hosts := make(map[string]bool) + for _, h := range hostnames { + hosts[strings.ToLower(h)] = true + } + + fn := RedirectPolicyFunc(func(req *http.Request, via []*http.Request) error { + if ok := hosts[getHostname(req.URL.Host)]; !ok { + return errors.New("redirect is not allowed as per DomainCheckRedirectPolicy") + } + + return nil + }) + + return fn +} + +func getHostname(host string) (hostname string) { + if strings.Index(host, ":") > 0 { + host, _, _ = net.SplitHostPort(host) + } + hostname = strings.ToLower(host) + return +} + +// By default Golang will not redirect request headers +// after go throughing various discussion comments from thread +// https://github.com/golang/go/issues/4800 +// go-resty will add all the headers during a redirect for the same host +func checkHostAndAddHeaders(cur *http.Request, pre *http.Request) { + curHostname := getHostname(cur.URL.Host) + preHostname := getHostname(pre.URL.Host) + if strings.EqualFold(curHostname, preHostname) { + for key, val := range pre.Header { + cur.Header[key] = val + } + } else { // only library User-Agent header is added + cur.Header.Set(hdrUserAgentKey, fmt.Sprintf(hdrUserAgentValue, Version)) + } +} diff --git a/vendor/github.com/go-resty/resty/request.go b/vendor/github.com/go-resty/resty/request.go new file mode 100644 index 000000000..91ffdc7e6 --- /dev/null +++ b/vendor/github.com/go-resty/resty/request.go @@ -0,0 +1,566 @@ +// Copyright (c) 2015-2018 Jeevanandam M (jeeva@myjeeva.com), All rights reserved. +// resty source code and usage is governed by a MIT style +// license that can be found in the LICENSE file. + +package resty + +import ( + "encoding/base64" + "encoding/json" + "encoding/xml" + "fmt" + "io" + "net" + "net/url" + "reflect" + "strings" +) + +// SRVRecord holds the data to query the SRV record for the following service +type SRVRecord struct { + Service string + Domain string +} + +// SetHeader method is to set a single header field and its value in the current request. +// Example: To set `Content-Type` and `Accept` as `application/json`. +// resty.R(). +// SetHeader("Content-Type", "application/json"). +// SetHeader("Accept", "application/json") +// +// Also you can override header value, which was set at client instance level. +// +func (r *Request) SetHeader(header, value string) *Request { + r.Header.Set(header, value) + return r +} + +// SetHeaders method sets multiple headers field and its values at one go in the current request. +// Example: To set `Content-Type` and `Accept` as `application/json` +// +// resty.R(). +// SetHeaders(map[string]string{ +// "Content-Type": "application/json", +// "Accept": "application/json", +// }) +// Also you can override header value, which was set at client instance level. +// +func (r *Request) SetHeaders(headers map[string]string) *Request { + for h, v := range headers { + r.SetHeader(h, v) + } + + return r +} + +// SetQueryParam method sets single parameter and its value in the current request. +// It will be formed as query string for the request. +// Example: `search=kitchen%20papers&size=large` in the URL after `?` mark. +// resty.R(). +// SetQueryParam("search", "kitchen papers"). +// SetQueryParam("size", "large") +// Also you can override query params value, which was set at client instance level +// +func (r *Request) SetQueryParam(param, value string) *Request { + r.QueryParam.Set(param, value) + return r +} + +// SetQueryParams method sets multiple parameters and its values at one go in the current request. +// It will be formed as query string for the request. +// Example: `search=kitchen%20papers&size=large` in the URL after `?` mark. +// resty.R(). +// SetQueryParams(map[string]string{ +// "search": "kitchen papers", +// "size": "large", +// }) +// Also you can override query params value, which was set at client instance level +// +func (r *Request) SetQueryParams(params map[string]string) *Request { + for p, v := range params { + r.SetQueryParam(p, v) + } + + return r +} + +// SetMultiValueQueryParams method appends multiple parameters with multi-value +// at one go in the current request. It will be formed as query string for the request. +// Example: `status=pending&status=approved&status=open` in the URL after `?` mark. +// resty.R(). +// SetMultiValueQueryParams(url.Values{ +// "status": []string{"pending", "approved", "open"}, +// }) +// Also you can override query params value, which was set at client instance level +// +func (r *Request) SetMultiValueQueryParams(params url.Values) *Request { + for p, v := range params { + for _, pv := range v { + r.QueryParam.Add(p, pv) + } + } + + return r +} + +// SetQueryString method provides ability to use string as an input to set URL query string for the request. +// +// Using String as an input +// resty.R(). +// SetQueryString("productId=232&template=fresh-sample&cat=resty&source=google&kw=buy a lot more") +// +func (r *Request) SetQueryString(query string) *Request { + params, err := url.ParseQuery(strings.TrimSpace(query)) + if err == nil { + for p, v := range params { + for _, pv := range v { + r.QueryParam.Add(p, pv) + } + } + } else { + r.client.Log.Printf("ERROR %v", err) + } + return r +} + +// SetFormData method sets Form parameters and their values in the current request. +// It's applicable only HTTP method `POST` and `PUT` and requests content type would be set as +// `application/x-www-form-urlencoded`. +// resty.R(). +// SetFormData(map[string]string{ +// "access_token": "BC594900-518B-4F7E-AC75-BD37F019E08F", +// "user_id": "3455454545", +// }) +// Also you can override form data value, which was set at client instance level +// +func (r *Request) SetFormData(data map[string]string) *Request { + for k, v := range data { + r.FormData.Set(k, v) + } + + return r +} + +// SetMultiValueFormData method appends multiple form parameters with multi-value +// at one go in the current request. +// resty.R(). +// SetMultiValueFormData(url.Values{ +// "search_criteria": []string{"book", "glass", "pencil"}, +// }) +// Also you can override form data value, which was set at client instance level +// +func (r *Request) SetMultiValueFormData(params url.Values) *Request { + for k, v := range params { + for _, kv := range v { + r.FormData.Add(k, kv) + } + } + + return r +} + +// SetBody method sets the request body for the request. It supports various realtime need easy. +// We can say its quite handy or powerful. Supported request body data types is `string`, `[]byte`, +// `struct` and `map`. Body value can be pointer or non-pointer. Automatic marshalling +// for JSON and XML content type, if it is `struct` or `map`. +// +// Example: +// +// Struct as a body input, based on content type, it will be marshalled. +// resty.R(). +// SetBody(User{ +// Username: "jeeva@myjeeva.com", +// Password: "welcome2resty", +// }) +// +// Map as a body input, based on content type, it will be marshalled. +// resty.R(). +// SetBody(map[string]interface{}{ +// "username": "jeeva@myjeeva.com", +// "password": "welcome2resty", +// "address": &Address{ +// Address1: "1111 This is my street", +// Address2: "Apt 201", +// City: "My City", +// State: "My State", +// ZipCode: 00000, +// }, +// }) +// +// String as a body input. Suitable for any need as a string input. +// resty.R(). +// SetBody(`{ +// "username": "jeeva@getrightcare.com", +// "password": "admin" +// }`) +// +// []byte as a body input. Suitable for raw request such as file upload, serialize & deserialize, etc. +// resty.R(). +// SetBody([]byte("This is my raw request, sent as-is")) +// +func (r *Request) SetBody(body interface{}) *Request { + r.Body = body + return r +} + +// SetResult method is to register the response `Result` object for automatic unmarshalling in the RESTful mode +// if response status code is between 200 and 299 and content type either JSON or XML. +// +// Note: Result object can be pointer or non-pointer. +// resty.R().SetResult(&AuthToken{}) +// // OR +// resty.R().SetResult(AuthToken{}) +// +// Accessing a result value +// response.Result().(*AuthToken) +// +func (r *Request) SetResult(res interface{}) *Request { + r.Result = getPointer(res) + return r +} + +// SetError method is to register the request `Error` object for automatic unmarshalling in the RESTful mode +// if response status code is greater than 399 and content type either JSON or XML. +// +// Note: Error object can be pointer or non-pointer. +// resty.R().SetError(&AuthError{}) +// // OR +// resty.R().SetError(AuthError{}) +// +// Accessing a error value +// response.Error().(*AuthError) +// +func (r *Request) SetError(err interface{}) *Request { + r.Error = getPointer(err) + return r +} + +// SetFile method is to set single file field name and its path for multipart upload. +// resty.R(). +// SetFile("my_file", "/Users/jeeva/Gas Bill - Sep.pdf") +// +func (r *Request) SetFile(param, filePath string) *Request { + r.isMultiPart = true + r.FormData.Set("@"+param, filePath) + + return r +} + +// SetFiles method is to set multiple file field name and its path for multipart upload. +// resty.R(). +// SetFiles(map[string]string{ +// "my_file1": "/Users/jeeva/Gas Bill - Sep.pdf", +// "my_file2": "/Users/jeeva/Electricity Bill - Sep.pdf", +// "my_file3": "/Users/jeeva/Water Bill - Sep.pdf", +// }) +// +func (r *Request) SetFiles(files map[string]string) *Request { + r.isMultiPart = true + + for f, fp := range files { + r.FormData.Set("@"+f, fp) + } + + return r +} + +// SetFileReader method is to set single file using io.Reader for multipart upload. +// resty.R(). +// SetFileReader("profile_img", "my-profile-img.png", bytes.NewReader(profileImgBytes)). +// SetFileReader("notes", "user-notes.txt", bytes.NewReader(notesBytes)) +// +func (r *Request) SetFileReader(param, fileName string, reader io.Reader) *Request { + r.isMultiPart = true + + r.multipartFiles = append(r.multipartFiles, &File{ + Name: fileName, + ParamName: param, + Reader: reader, + }) + + return r +} + +// SetMultipartField method is to set custom data using io.Reader for multipart upload. +func (r *Request) SetMultipartField(param, fileName, contentType string, reader io.Reader) *Request { + r.isMultiPart = true + + r.multipartFields = append(r.multipartFields, &multipartField{ + Param: param, + FileName: fileName, + ContentType: contentType, + Reader: reader, + }) + + return r +} + +// SetContentLength method sets the HTTP header `Content-Length` value for current request. +// By default go-resty won't set `Content-Length`. Also you have an option to enable for every +// request. See `resty.SetContentLength` +// resty.R().SetContentLength(true) +// +func (r *Request) SetContentLength(l bool) *Request { + r.setContentLength = true + + return r +} + +// SetBasicAuth method sets the basic authentication header in the current HTTP request. +// For Header example: +// Authorization: Basic +// +// To set the header for username "go-resty" and password "welcome" +// resty.R().SetBasicAuth("go-resty", "welcome") +// +// This method overrides the credentials set by method `resty.SetBasicAuth`. +// +func (r *Request) SetBasicAuth(username, password string) *Request { + r.UserInfo = &User{Username: username, Password: password} + return r +} + +// SetAuthToken method sets bearer auth token header in the current HTTP request. Header example: +// Authorization: Bearer +// +// Example: To set auth token BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F +// +// resty.R().SetAuthToken("BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F") +// +// This method overrides the Auth token set by method `resty.SetAuthToken`. +// +func (r *Request) SetAuthToken(token string) *Request { + r.Token = token + return r +} + +// SetOutput method sets the output file for current HTTP request. Current HTTP response will be +// saved into given file. It is similar to `curl -o` flag. Absolute path or relative path can be used. +// If is it relative path then output file goes under the output directory, as mentioned +// in the `Client.SetOutputDirectory`. +// resty.R(). +// SetOutput("/Users/jeeva/Downloads/ReplyWithHeader-v5.1-beta.zip"). +// Get("http://bit.ly/1LouEKr") +// +// Note: In this scenario `Response.Body` might be nil. +func (r *Request) SetOutput(file string) *Request { + r.outputFile = file + r.isSaveResponse = true + return r +} + +// SetSRV method sets the details to query the service SRV record and execute the +// request. +// resty.R(). +// SetSRV(SRVRecord{"web", "testservice.com"}). +// Get("/get") +func (r *Request) SetSRV(srv *SRVRecord) *Request { + r.SRV = srv + return r +} + +// SetDoNotParseResponse method instructs `Resty` not to parse the response body automatically. +// Resty exposes the raw response body as `io.ReadCloser`. Also do not forget to close the body, +// otherwise you might get into connection leaks, no connection reuse. +// +// Please Note: Response middlewares are not applicable, if you use this option. Basically you have +// taken over the control of response parsing from `Resty`. +func (r *Request) SetDoNotParseResponse(parse bool) *Request { + r.notParseResponse = parse + return r +} + +// SetPathParams method sets multiple URL path key-value pairs at one go in the +// resty current request instance. +// resty.R().SetPathParams(map[string]string{ +// "userId": "sample@sample.com", +// "subAccountId": "100002", +// }) +// +// Result: +// URL - /v1/users/{userId}/{subAccountId}/details +// Composed URL - /v1/users/sample@sample.com/100002/details +// It replace the value of the key while composing request URL. Also you can +// override Path Params value, which was set at client instance level. +func (r *Request) SetPathParams(params map[string]string) *Request { + for p, v := range params { + r.pathParams[p] = v + } + return r +} + +// ExpectContentType method allows to provide fallback `Content-Type` for automatic unmarshalling +// when `Content-Type` response header is unavailable. +func (r *Request) ExpectContentType(contentType string) *Request { + r.fallbackContentType = contentType + return r +} + +// SetJSONEscapeHTML method is to enable/disable the HTML escape on JSON marshal. +// +// NOTE: This option only applicable to standard JSON Marshaller. +func (r *Request) SetJSONEscapeHTML(b bool) *Request { + r.jsonEscapeHTML = b + return r +} + +//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ +// HTTP verb method starts here +//___________________________________ + +// Get method does GET HTTP request. It's defined in section 4.3.1 of RFC7231. +func (r *Request) Get(url string) (*Response, error) { + return r.Execute(MethodGet, url) +} + +// Head method does HEAD HTTP request. It's defined in section 4.3.2 of RFC7231. +func (r *Request) Head(url string) (*Response, error) { + return r.Execute(MethodHead, url) +} + +// Post method does POST HTTP request. It's defined in section 4.3.3 of RFC7231. +func (r *Request) Post(url string) (*Response, error) { + return r.Execute(MethodPost, url) +} + +// Put method does PUT HTTP request. It's defined in section 4.3.4 of RFC7231. +func (r *Request) Put(url string) (*Response, error) { + return r.Execute(MethodPut, url) +} + +// Delete method does DELETE HTTP request. It's defined in section 4.3.5 of RFC7231. +func (r *Request) Delete(url string) (*Response, error) { + return r.Execute(MethodDelete, url) +} + +// Options method does OPTIONS HTTP request. It's defined in section 4.3.7 of RFC7231. +func (r *Request) Options(url string) (*Response, error) { + return r.Execute(MethodOptions, url) +} + +// Patch method does PATCH HTTP request. It's defined in section 2 of RFC5789. +func (r *Request) Patch(url string) (*Response, error) { + return r.Execute(MethodPatch, url) +} + +// Execute method performs the HTTP request with given HTTP method and URL +// for current `Request`. +// resp, err := resty.R().Execute(resty.GET, "http://httpbin.org/get") +// +func (r *Request) Execute(method, url string) (*Response, error) { + var addrs []*net.SRV + var err error + + if r.isMultiPart && !(method == MethodPost || method == MethodPut) { + return nil, fmt.Errorf("multipart content is not allowed in HTTP verb [%v]", method) + } + + if r.SRV != nil { + _, addrs, err = net.LookupSRV(r.SRV.Service, "tcp", r.SRV.Domain) + if err != nil { + return nil, err + } + } + + r.Method = method + r.URL = r.selectAddr(addrs, url, 0) + + if r.client.RetryCount == 0 { + return r.client.execute(r) + } + + var resp *Response + attempt := 0 + _ = Backoff( + func() (*Response, error) { + attempt++ + + r.URL = r.selectAddr(addrs, url, attempt) + + resp, err = r.client.execute(r) + if err != nil { + r.client.Log.Printf("ERROR %v, Attempt %v", err, attempt) + if r.isContextCancelledIfAvailable() { + // stop Backoff from retrying request if request has been + // canceled by context + return resp, nil + } + } + + return resp, err + }, + Retries(r.client.RetryCount), + WaitTime(r.client.RetryWaitTime), + MaxWaitTime(r.client.RetryMaxWaitTime), + RetryConditions(r.client.RetryConditions), + ) + + return resp, err +} + +//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ +// Request Unexported methods +//___________________________________ + +func (r *Request) fmtBodyString() (body string) { + body = "***** NO CONTENT *****" + if isPayloadSupported(r.Method, r.client.AllowGetMethodPayload) { + if _, ok := r.Body.(io.Reader); ok { + body = "***** BODY IS io.Reader *****" + return + } + + // multipart or form-data + if r.isMultiPart || r.isFormData { + body = r.bodyBuf.String() + return + } + + // request body data + if r.Body == nil { + return + } + var prtBodyBytes []byte + var err error + + contentType := r.Header.Get(hdrContentTypeKey) + kind := kindOf(r.Body) + if canJSONMarshal(contentType, kind) { + prtBodyBytes, err = json.MarshalIndent(&r.Body, "", " ") + } else if IsXMLType(contentType) && (kind == reflect.Struct) { + prtBodyBytes, err = xml.MarshalIndent(&r.Body, "", " ") + } else if b, ok := r.Body.(string); ok { + if IsJSONType(contentType) { + bodyBytes := []byte(b) + out := acquireBuffer() + defer releaseBuffer(out) + if err = json.Indent(out, bodyBytes, "", " "); err == nil { + prtBodyBytes = out.Bytes() + } + } else { + body = b + return + } + } else if b, ok := r.Body.([]byte); ok { + body = base64.StdEncoding.EncodeToString(b) + } + + if prtBodyBytes != nil && err == nil { + body = string(prtBodyBytes) + } + } + + return +} + +func (r *Request) selectAddr(addrs []*net.SRV, path string, attempt int) string { + if addrs == nil { + return path + } + + idx := attempt % len(addrs) + domain := strings.TrimRight(addrs[idx].Target, ".") + path = strings.TrimLeft(path, "/") + + return fmt.Sprintf("%s://%s:%d/%s", r.client.scheme, domain, addrs[idx].Port, path) +} diff --git a/vendor/github.com/go-resty/resty/request16.go b/vendor/github.com/go-resty/resty/request16.go new file mode 100644 index 000000000..999ba80ae --- /dev/null +++ b/vendor/github.com/go-resty/resty/request16.go @@ -0,0 +1,63 @@ +// +build !go1.7 + +// Copyright (c) 2015-2018 Jeevanandam M (jeeva@myjeeva.com) +// 2016 Andrew Grigorev (https://github.com/ei-grad) +// All rights reserved. +// resty source code and usage is governed by a MIT style +// license that can be found in the LICENSE file. + +package resty + +import ( + "bytes" + "encoding/json" + "net/http" + "net/url" + "time" +) + +// Request type is used to compose and send individual request from client +// go-resty is provide option override client level settings such as +// Auth Token, Basic Auth credentials, Header, Query Param, Form Data, Error object +// and also you can add more options for that particular request +type Request struct { + URL string + Method string + Token string + QueryParam url.Values + FormData url.Values + Header http.Header + Time time.Time + Body interface{} + Result interface{} + Error interface{} + RawRequest *http.Request + SRV *SRVRecord + UserInfo *User + + isMultiPart bool + isFormData bool + setContentLength bool + isSaveResponse bool + notParseResponse bool + jsonEscapeHTML bool + outputFile string + fallbackContentType string + pathParams map[string]string + client *Client + bodyBuf *bytes.Buffer + multipartFiles []*File + multipartFields []*multipartField +} + +func (r *Request) addContextIfAvailable() { + // nothing to do for golang<1.7 +} + +func (r *Request) isContextCancelledIfAvailable() bool { + // just always return false golang<1.7 + return false +} + +// for !go1.7 +var noescapeJSONMarshal = json.Marshal diff --git a/vendor/github.com/go-resty/resty/request17.go b/vendor/github.com/go-resty/resty/request17.go new file mode 100644 index 000000000..c81cae715 --- /dev/null +++ b/vendor/github.com/go-resty/resty/request17.go @@ -0,0 +1,87 @@ +// +build go1.7 go1.8 + +// Copyright (c) 2015-2018 Jeevanandam M (jeeva@myjeeva.com) +// 2016 Andrew Grigorev (https://github.com/ei-grad) +// All rights reserved. +// resty source code and usage is governed by a MIT style +// license that can be found in the LICENSE file. + +package resty + +import ( + "bytes" + "context" + "encoding/json" + "net/http" + "net/url" + "time" +) + +// Request type is used to compose and send individual request from client +// go-resty is provide option override client level settings such as +// Auth Token, Basic Auth credentials, Header, Query Param, Form Data, Error object +// and also you can add more options for that particular request +type Request struct { + URL string + Method string + Token string + QueryParam url.Values + FormData url.Values + Header http.Header + Time time.Time + Body interface{} + Result interface{} + Error interface{} + RawRequest *http.Request + SRV *SRVRecord + UserInfo *User + + isMultiPart bool + isFormData bool + setContentLength bool + isSaveResponse bool + notParseResponse bool + jsonEscapeHTML bool + outputFile string + fallbackContentType string + ctx context.Context + pathParams map[string]string + client *Client + bodyBuf *bytes.Buffer + multipartFiles []*File + multipartFields []*multipartField +} + +// SetContext method sets the context.Context for current Request. It allows +// to interrupt the request execution if ctx.Done() channel is closed. +// See https://blog.golang.org/context article and the "context" package +// documentation. +func (r *Request) SetContext(ctx context.Context) *Request { + r.ctx = ctx + return r +} + +func (r *Request) addContextIfAvailable() { + if r.ctx != nil { + r.RawRequest = r.RawRequest.WithContext(r.ctx) + } +} + +func (r *Request) isContextCancelledIfAvailable() bool { + if r.ctx != nil { + if r.ctx.Err() != nil { + return true + } + } + return false +} + +// for go1.7+ +var noescapeJSONMarshal = func(v interface{}) ([]byte, error) { + buf := acquireBuffer() + defer releaseBuffer(buf) + encoder := json.NewEncoder(buf) + encoder.SetEscapeHTML(false) + err := encoder.Encode(v) + return buf.Bytes(), err +} diff --git a/vendor/github.com/go-resty/resty/response.go b/vendor/github.com/go-resty/resty/response.go new file mode 100644 index 000000000..f54e90557 --- /dev/null +++ b/vendor/github.com/go-resty/resty/response.go @@ -0,0 +1,150 @@ +// Copyright (c) 2015-2018 Jeevanandam M (jeeva@myjeeva.com), All rights reserved. +// resty source code and usage is governed by a MIT style +// license that can be found in the LICENSE file. + +package resty + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "strings" + "time" +) + +// Response is an object represents executed request and its values. +type Response struct { + Request *Request + RawResponse *http.Response + + body []byte + size int64 + receivedAt time.Time +} + +// Body method returns HTTP response as []byte array for the executed request. +// Note: `Response.Body` might be nil, if `Request.SetOutput` is used. +func (r *Response) Body() []byte { + if r.RawResponse == nil { + return []byte{} + } + return r.body +} + +// Status method returns the HTTP status string for the executed request. +// Example: 200 OK +func (r *Response) Status() string { + if r.RawResponse == nil { + return "" + } + + return r.RawResponse.Status +} + +// StatusCode method returns the HTTP status code for the executed request. +// Example: 200 +func (r *Response) StatusCode() int { + if r.RawResponse == nil { + return 0 + } + + return r.RawResponse.StatusCode +} + +// Result method returns the response value as an object if it has one +func (r *Response) Result() interface{} { + return r.Request.Result +} + +// Error method returns the error object if it has one +func (r *Response) Error() interface{} { + return r.Request.Error +} + +// Header method returns the response headers +func (r *Response) Header() http.Header { + if r.RawResponse == nil { + return http.Header{} + } + + return r.RawResponse.Header +} + +// Cookies method to access all the response cookies +func (r *Response) Cookies() []*http.Cookie { + if r.RawResponse == nil { + return make([]*http.Cookie, 0) + } + + return r.RawResponse.Cookies() +} + +// String method returns the body of the server response as String. +func (r *Response) String() string { + if r.body == nil { + return "" + } + + return strings.TrimSpace(string(r.body)) +} + +// Time method returns the time of HTTP response time that from request we sent and received a request. +// See `response.ReceivedAt` to know when client recevied response and see `response.Request.Time` to know +// when client sent a request. +func (r *Response) Time() time.Duration { + return r.receivedAt.Sub(r.Request.Time) +} + +// ReceivedAt method returns when response got recevied from server for the request. +func (r *Response) ReceivedAt() time.Time { + return r.receivedAt +} + +// Size method returns the HTTP response size in bytes. Ya, you can relay on HTTP `Content-Length` header, +// however it won't be good for chucked transfer/compressed response. Since Resty calculates response size +// at the client end. You will get actual size of the http response. +func (r *Response) Size() int64 { + return r.size +} + +// RawBody method exposes the HTTP raw response body. Use this method in-conjunction with `SetDoNotParseResponse` +// option otherwise you get an error as `read err: http: read on closed response body`. +// +// Do not forget to close the body, otherwise you might get into connection leaks, no connection reuse. +// Basically you have taken over the control of response parsing from `Resty`. +func (r *Response) RawBody() io.ReadCloser { + if r.RawResponse == nil { + return nil + } + return r.RawResponse.Body +} + +// IsSuccess method returns true if HTTP status code >= 200 and <= 299 otherwise false. +func (r *Response) IsSuccess() bool { + return r.StatusCode() > 199 && r.StatusCode() < 300 +} + +// IsError method returns true if HTTP status code >= 400 otherwise false. +func (r *Response) IsError() bool { + return r.StatusCode() > 399 +} + +func (r *Response) fmtBodyString(sl int64) string { + if r.body != nil { + if int64(len(r.body)) > sl { + return fmt.Sprintf("***** RESPONSE TOO LARGE (size - %d) *****", len(r.body)) + } + ct := r.Header().Get(hdrContentTypeKey) + if IsJSONType(ct) { + out := acquireBuffer() + defer releaseBuffer(out) + if err := json.Indent(out, r.body, "", " "); err == nil { + return out.String() + } + } + return r.String() + } + + return "***** NO CONTENT *****" +} diff --git a/vendor/github.com/go-resty/resty/resty.go b/vendor/github.com/go-resty/resty/resty.go new file mode 100644 index 000000000..8cb6e3e80 --- /dev/null +++ b/vendor/github.com/go-resty/resty/resty.go @@ -0,0 +1,9 @@ +// Copyright (c) 2015-2018 Jeevanandam M (jeeva@myjeeva.com), All rights reserved. +// resty source code and usage is governed by a MIT style +// license that can be found in the LICENSE file. + +// Package resty provides simple HTTP and REST client for Go inspired by Ruby rest-client. +package resty + +// Version # of resty +const Version = "1.9.1" diff --git a/vendor/github.com/go-resty/resty/retry.go b/vendor/github.com/go-resty/resty/retry.go new file mode 100644 index 000000000..7b88cb73f --- /dev/null +++ b/vendor/github.com/go-resty/resty/retry.go @@ -0,0 +1,114 @@ +// Copyright (c) 2015-2018 Jeevanandam M (jeeva@myjeeva.com), All rights reserved. +// resty source code and usage is governed by a MIT style +// license that can be found in the LICENSE file. + +package resty + +import ( + "math" + "math/rand" + "time" +) + +const ( + defaultMaxRetries = 3 + defaultWaitTime = time.Duration(100) * time.Millisecond + defaultMaxWaitTime = time.Duration(2000) * time.Millisecond +) + +type ( + // Option is to create convenient retry options like wait time, max retries, etc. + Option func(*Options) + + // RetryConditionFunc type is for retry condition function + RetryConditionFunc func(*Response) (bool, error) + + // Options to hold go-resty retry values + Options struct { + maxRetries int + waitTime time.Duration + maxWaitTime time.Duration + retryConditions []RetryConditionFunc + } +) + +// Retries sets the max number of retries +func Retries(value int) Option { + return func(o *Options) { + o.maxRetries = value + } +} + +// WaitTime sets the default wait time to sleep between requests +func WaitTime(value time.Duration) Option { + return func(o *Options) { + o.waitTime = value + } +} + +// MaxWaitTime sets the max wait time to sleep between requests +func MaxWaitTime(value time.Duration) Option { + return func(o *Options) { + o.maxWaitTime = value + } +} + +// RetryConditions sets the conditions that will be checked for retry. +func RetryConditions(conditions []RetryConditionFunc) Option { + return func(o *Options) { + o.retryConditions = conditions + } +} + +// Backoff retries with increasing timeout duration up until X amount of retries +// (Default is 3 attempts, Override with option Retries(n)) +func Backoff(operation func() (*Response, error), options ...Option) error { + // Defaults + opts := Options{ + maxRetries: defaultMaxRetries, + waitTime: defaultWaitTime, + maxWaitTime: defaultMaxWaitTime, + retryConditions: []RetryConditionFunc{}, + } + + for _, o := range options { + o(&opts) + } + + var ( + resp *Response + err error + ) + base := float64(opts.waitTime) // Time to wait between each attempt + capLevel := float64(opts.maxWaitTime) // Maximum amount of wait time for the retry + for attempt := 0; attempt < opts.maxRetries; attempt++ { + resp, err = operation() + + var needsRetry bool + var conditionErr error + for _, condition := range opts.retryConditions { + needsRetry, conditionErr = condition(resp) + if needsRetry || conditionErr != nil { + break + } + } + + // If the operation returned no error, there was no condition satisfied and + // there was no error caused by the conditional functions. + if err == nil && !needsRetry && conditionErr == nil { + return nil + } + // Adding capped exponential backup with jitter + // See the following article... + // http://www.awsarchitectureblog.com/2015/03/backoff.html + temp := math.Min(capLevel, base*math.Exp2(float64(attempt))) + sleepDuration := time.Duration(int(temp/2) + rand.Intn(int(temp/2))) + + if sleepDuration < opts.waitTime { + sleepDuration = opts.waitTime + } + time.Sleep(sleepDuration) + } + + return err +} diff --git a/vendor/github.com/go-resty/resty/util.go b/vendor/github.com/go-resty/resty/util.go new file mode 100644 index 000000000..e72374ed8 --- /dev/null +++ b/vendor/github.com/go-resty/resty/util.go @@ -0,0 +1,253 @@ +// Copyright (c) 2015-2018 Jeevanandam M (jeeva@myjeeva.com), All rights reserved. +// resty source code and usage is governed by a MIT style +// license that can be found in the LICENSE file. + +package resty + +import ( + "bytes" + "encoding/json" + "encoding/xml" + "fmt" + "io" + "log" + "mime/multipart" + "net/http" + "net/textproto" + "os" + "path/filepath" + "reflect" + "runtime" + "sort" + "strings" +) + +//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ +// Package Helper methods +//___________________________________ + +// IsStringEmpty method tells whether given string is empty or not +func IsStringEmpty(str string) bool { + return len(strings.TrimSpace(str)) == 0 +} + +// DetectContentType method is used to figure out `Request.Body` content type for request header +func DetectContentType(body interface{}) string { + contentType := plainTextType + kind := kindOf(body) + switch kind { + case reflect.Struct, reflect.Map: + contentType = jsonContentType + case reflect.String: + contentType = plainTextType + default: + if b, ok := body.([]byte); ok { + contentType = http.DetectContentType(b) + } else if kind == reflect.Slice { + contentType = jsonContentType + } + } + + return contentType +} + +// IsJSONType method is to check JSON content type or not +func IsJSONType(ct string) bool { + return jsonCheck.MatchString(ct) +} + +// IsXMLType method is to check XML content type or not +func IsXMLType(ct string) bool { + return xmlCheck.MatchString(ct) +} + +// Unmarshal content into object from JSON or XML +// Deprecated: kept for backward compatibility +func Unmarshal(ct string, b []byte, d interface{}) (err error) { + if IsJSONType(ct) { + err = json.Unmarshal(b, d) + } else if IsXMLType(ct) { + err = xml.Unmarshal(b, d) + } + + return +} + +// Unmarshalc content into object from JSON or XML +func Unmarshalc(c *Client, ct string, b []byte, d interface{}) (err error) { + if IsJSONType(ct) { + err = c.JSONUnmarshal(b, d) + } else if IsXMLType(ct) { + err = xml.Unmarshal(b, d) + } + + return +} + +// way to disable the HTML escape as opt-in +func jsonMarshal(c *Client, r *Request, d interface{}) ([]byte, error) { + if !r.jsonEscapeHTML { + return noescapeJSONMarshal(d) + } else if !c.jsonEscapeHTML { + return noescapeJSONMarshal(d) + } + return c.JSONMarshal(d) +} + +//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ +// Package Unexported methods +//___________________________________ + +func firstNonEmpty(v ...string) string { + for _, s := range v { + if !IsStringEmpty(s) { + return s + } + } + return "" +} + +func getLogger(w io.Writer) *log.Logger { + return log.New(w, "RESTY ", log.LstdFlags) +} + +var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"") + +func escapeQuotes(s string) string { + return quoteEscaper.Replace(s) +} + +func createMultipartHeader(param, fileName, contentType string) textproto.MIMEHeader { + hdr := make(textproto.MIMEHeader) + hdr.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`, + escapeQuotes(param), escapeQuotes(fileName))) + hdr.Set("Content-Type", contentType) + return hdr +} + +func addMultipartFormField(w *multipart.Writer, mf *multipartField) error { + partWriter, err := w.CreatePart(createMultipartHeader(mf.Param, mf.FileName, mf.ContentType)) + if err != nil { + return err + } + + _, err = io.Copy(partWriter, mf.Reader) + return err +} + +func writeMultipartFormFile(w *multipart.Writer, fieldName, fileName string, r io.Reader) error { + // Auto detect actual multipart content type + cbuf := make([]byte, 512) + size, err := r.Read(cbuf) + if err != nil { + return err + } + + partWriter, err := w.CreatePart(createMultipartHeader(fieldName, fileName, http.DetectContentType(cbuf))) + if err != nil { + return err + } + + if _, err = partWriter.Write(cbuf[:size]); err != nil { + return err + } + + _, err = io.Copy(partWriter, r) + return err +} + +func addFile(w *multipart.Writer, fieldName, path string) error { + file, err := os.Open(path) + if err != nil { + return err + } + defer closeq(file) + return writeMultipartFormFile(w, fieldName, filepath.Base(path), file) +} + +func addFileReader(w *multipart.Writer, f *File) error { + return writeMultipartFormFile(w, f.ParamName, f.Name, f.Reader) +} + +func getPointer(v interface{}) interface{} { + vv := valueOf(v) + if vv.Kind() == reflect.Ptr { + return v + } + return reflect.New(vv.Type()).Interface() +} + +func isPayloadSupported(m string, allowMethodGet bool) bool { + return !(m == MethodHead || m == MethodOptions || (m == MethodGet && !allowMethodGet)) +} + +func typeOf(i interface{}) reflect.Type { + return indirect(valueOf(i)).Type() +} + +func valueOf(i interface{}) reflect.Value { + return reflect.ValueOf(i) +} + +func indirect(v reflect.Value) reflect.Value { + return reflect.Indirect(v) +} + +func kindOf(v interface{}) reflect.Kind { + return typeOf(v).Kind() +} + +func createDirectory(dir string) (err error) { + if _, err = os.Stat(dir); err != nil { + if os.IsNotExist(err) { + if err = os.MkdirAll(dir, 0755); err != nil { + return + } + } + } + return +} + +func canJSONMarshal(contentType string, kind reflect.Kind) bool { + return IsJSONType(contentType) && (kind == reflect.Struct || kind == reflect.Map) +} + +func functionName(i interface{}) string { + return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name() +} + +func acquireBuffer() *bytes.Buffer { + return bufPool.Get().(*bytes.Buffer) +} + +func releaseBuffer(buf *bytes.Buffer) { + if buf != nil { + buf.Reset() + bufPool.Put(buf) + } +} + +func closeq(v interface{}) { + if c, ok := v.(io.Closer); ok { + sliently(c.Close()) + } +} + +func sliently(_ ...interface{}) {} + +func composeHeaders(hdrs http.Header) string { + var str []string + for _, k := range sortHeaderKeys(hdrs) { + str = append(str, fmt.Sprintf("%25s: %s", k, strings.Join(hdrs[k], ", "))) + } + return strings.Join(str, "\n") +} + +func sortHeaderKeys(hdrs http.Header) []string { + var keys []string + for key := range hdrs { + keys = append(keys, key) + } + sort.Strings(keys) + return keys +} diff --git a/vendor/github.com/linode/linodego/LICENSE b/vendor/github.com/linode/linodego/LICENSE new file mode 100644 index 000000000..6b0e6bace --- /dev/null +++ b/vendor/github.com/linode/linodego/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 Christopher "Chief" Najewicz + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/github.com/linode/linodego/account.go b/vendor/github.com/linode/linodego/account.go new file mode 100644 index 000000000..5d6774caa --- /dev/null +++ b/vendor/github.com/linode/linodego/account.go @@ -0,0 +1,44 @@ +package linodego + +import "context" + +// Account associated with the token in use +type Account struct { + FirstName string `json:"first_name"` + LastName string `json:"last_name"` + Email string `json:"email"` + Company string `json:"company"` + Address1 string `json:"address1"` + Address2 string `json:"address2"` + Balance float32 `json:"balance"` + City string `json:"city"` + State string `json:"state"` + Zip string `json:"zip"` + Country string `json:"country"` + TaxID string `json:"tax_id"` + CreditCard *CreditCard `json:"credit_card"` +} + +// CreditCard information associated with the Account. +type CreditCard struct { + LastFour string `json:"last_four"` + Expiry string `json:"expiry"` +} + +// fixDates converts JSON timestamps to Go time.Time values +func (v *Account) fixDates() *Account { + return v +} + +// GetAccount gets the contact and billing information related to the Account +func (c *Client) GetAccount(ctx context.Context) (*Account, error) { + e, err := c.Account.Endpoint() + if err != nil { + return nil, err + } + r, err := coupleAPIErrors(c.R(ctx).SetResult(&Account{}).Get(e)) + if err != nil { + return nil, err + } + return r.Result().(*Account).fixDates(), nil +} diff --git a/vendor/github.com/linode/linodego/account_events.go b/vendor/github.com/linode/linodego/account_events.go new file mode 100644 index 000000000..e2365c35b --- /dev/null +++ b/vendor/github.com/linode/linodego/account_events.go @@ -0,0 +1,277 @@ +package linodego + +import ( + "context" + "encoding/json" + "fmt" + "log" + "strconv" + "strings" + "time" +) + +// Event represents an action taken on the Account. +type Event struct { + CreatedStr string `json:"created"` + + // The unique ID of this Event. + ID int `json:"id"` + + // Current status of the Event, Enum: "failed" "finished" "notification" "scheduled" "started" + Status EventStatus `json:"status"` + + // The action that caused this Event. New actions may be added in the future. + Action EventAction `json:"action"` + + // A percentage estimating the amount of time remaining for an Event. Returns null for notification events. + PercentComplete int `json:"percent_complete"` + + // The rate of completion of the Event. Only some Events will return rate; for example, migration and resize Events. + Rate *string `json:"rate"` + + // If this Event has been read. + Read bool `json:"read"` + + // If this Event has been seen. + Seen bool `json:"seen"` + + // The estimated time remaining until the completion of this Event. This value is only returned for in-progress events. + TimeRemainingMsg json.RawMessage `json:"time_remaining"` + TimeRemaining *int `json:"-"` + + // The username of the User who caused the Event. + Username string `json:"username"` + + // Detailed information about the Event's entity, including ID, type, label, and URL used to access it. + Entity *EventEntity `json:"entity"` + + // When this Event was created. + Created *time.Time `json:"-"` +} + +// EventAction constants start with Action and include all known Linode API Event Actions. +type EventAction string + +// EventAction constants represent the actions that cause an Event. New actions may be added in the future. +const ( + ActionBackupsEnable EventAction = "backups_enable" + ActionBackupsCancel EventAction = "backups_cancel" + ActionBackupsRestore EventAction = "backups_restore" + ActionCommunityQuestionReply EventAction = "community_question_reply" + ActionCreateCardUpdated EventAction = "credit_card_updated" + ActionDiskCreate EventAction = "disk_create" + ActionDiskDelete EventAction = "disk_delete" + ActionDiskDuplicate EventAction = "disk_duplicate" + ActionDiskImagize EventAction = "disk_imagize" + ActionDiskResize EventAction = "disk_resize" + ActionDNSRecordCreate EventAction = "dns_record_create" + ActionDNSRecordDelete EventAction = "dns_record_delete" + ActionDNSZoneCreate EventAction = "dns_zone_create" + ActionDNSZoneDelete EventAction = "dns_zone_delete" + ActionImageDelete EventAction = "image_delete" + ActionLinodeAddIP EventAction = "linode_addip" + ActionLinodeBoot EventAction = "linode_boot" + ActionLinodeClone EventAction = "linode_clone" + ActionLinodeCreate EventAction = "linode_create" + ActionLinodeDelete EventAction = "linode_delete" + ActionLinodeDeleteIP EventAction = "linode_deleteip" + ActionLinodeMigrate EventAction = "linode_migrate" + ActionLinodeMutate EventAction = "linode_mutate" + ActionLinodeReboot EventAction = "linode_reboot" + ActionLinodeRebuild EventAction = "linode_rebuild" + ActionLinodeResize EventAction = "linode_resize" + ActionLinodeShutdown EventAction = "linode_shutdown" + ActionLinodeSnapshot EventAction = "linode_snapshot" + ActionLongviewClientCreate EventAction = "longviewclient_create" + ActionLongviewClientDelete EventAction = "longviewclient_delete" + ActionManagedDisabled EventAction = "managed_disabled" + ActionManagedEnabled EventAction = "managed_enabled" + ActionManagedServiceCreate EventAction = "managed_service_create" + ActionManagedServiceDelete EventAction = "managed_service_delete" + ActionNodebalancerCreate EventAction = "nodebalancer_create" + ActionNodebalancerDelete EventAction = "nodebalancer_delete" + ActionNodebalancerConfigCreate EventAction = "nodebalancer_config_create" + ActionNodebalancerConfigDelete EventAction = "nodebalancer_config_delete" + ActionPasswordReset EventAction = "password_reset" + ActionPaymentSubmitted EventAction = "payment_submitted" + ActionStackScriptCreate EventAction = "stackscript_create" + ActionStackScriptDelete EventAction = "stackscript_delete" + ActionStackScriptPublicize EventAction = "stackscript_publicize" + ActionStackScriptRevise EventAction = "stackscript_revise" + ActionTFADisabled EventAction = "tfa_disabled" + ActionTFAEnabled EventAction = "tfa_enabled" + ActionTicketAttachmentUpload EventAction = "ticket_attachment_upload" + ActionTicketCreate EventAction = "ticket_create" + ActionTicketReply EventAction = "ticket_reply" + ActionVolumeAttach EventAction = "volume_attach" + ActionVolumeClone EventAction = "volume_clone" + ActionVolumeCreate EventAction = "volume_create" + ActionVolumeDelte EventAction = "volume_delete" + ActionVolumeDetach EventAction = "volume_detach" + ActionVolumeResize EventAction = "volume_resize" +) + +// EntityType constants start with Entity and include Linode API Event Entity Types +type EntityType string + +// EntityType contants are the entities an Event can be related to +const ( + EntityLinode EntityType = "linode" + EntityDisk EntityType = "disk" +) + +// EventStatus constants start with Event and include Linode API Event Status values +type EventStatus string + +// EventStatus constants reflect the current status of an Event +const ( + EventFailed EventStatus = "failed" + EventFinished EventStatus = "finished" + EventNotification EventStatus = "notification" + EventScheduled EventStatus = "scheduled" + EventStarted EventStatus = "started" +) + +// EventEntity provides detailed information about the Event's +// associated entity, including ID, Type, Label, and a URL that +// can be used to access it. +type EventEntity struct { + // ID may be a string or int, it depends on the EntityType + ID interface{} `json:"id"` + Label string `json:"label"` + Type EntityType `json:"type"` + URL string `json:"url"` +} + +// EventsPagedResponse represents a paginated Events API response +type EventsPagedResponse struct { + *PageOptions + Data []Event `json:"data"` +} + +// endpoint gets the endpoint URL for Event +func (EventsPagedResponse) endpoint(c *Client) string { + endpoint, err := c.Events.Endpoint() + if err != nil { + panic(err) + } + return endpoint +} + +// endpointWithID gets the endpoint URL for a specific Event +func (e Event) endpointWithID(c *Client) string { + endpoint, err := c.Events.Endpoint() + if err != nil { + panic(err) + } + endpoint = fmt.Sprintf("%s/%d", endpoint, e.ID) + return endpoint +} + +// appendData appends Events when processing paginated Event responses +func (resp *EventsPagedResponse) appendData(r *EventsPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListEvents gets a collection of Event objects representing actions taken +// on the Account. The Events returned depend on the token grants and the grants +// of the associated user. +func (c *Client) ListEvents(ctx context.Context, opts *ListOptions) ([]Event, error) { + response := EventsPagedResponse{} + err := c.listHelper(ctx, &response, opts) + for i := range response.Data { + response.Data[i].fixDates() + } + if err != nil { + return nil, err + } + return response.Data, nil +} + +// GetEvent gets the Event with the Event ID +func (c *Client) GetEvent(ctx context.Context, id int) (*Event, error) { + e, err := c.Events.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, id) + r, err := c.R(ctx).SetResult(&Event{}).Get(e) + if err != nil { + return nil, err + } + return r.Result().(*Event).fixDates(), nil +} + +// fixDates converts JSON timestamps to Go time.Time values +func (e *Event) fixDates() *Event { + e.Created, _ = parseDates(e.CreatedStr) + e.TimeRemaining = unmarshalTimeRemaining(e.TimeRemainingMsg) + return e +} + +// MarkEventRead marks a single Event as read. +func (c *Client) MarkEventRead(ctx context.Context, event *Event) error { + e := event.endpointWithID(c) + e = fmt.Sprintf("%s/read", e) + + _, err := coupleAPIErrors(c.R(ctx).Post(e)) + + return err +} + +// MarkEventsSeen marks all Events up to and including this Event by ID as seen. +func (c *Client) MarkEventsSeen(ctx context.Context, event *Event) error { + e := event.endpointWithID(c) + e = fmt.Sprintf("%s/seen", e) + + _, err := coupleAPIErrors(c.R(ctx).Post(e)) + + return err +} + +func unmarshalTimeRemaining(m json.RawMessage) *int { + jsonBytes, err := m.MarshalJSON() + if err != nil { + panic(jsonBytes) + } + + if len(jsonBytes) == 4 && string(jsonBytes) == "null" { + return nil + } + + var timeStr string + if err := json.Unmarshal(jsonBytes, &timeStr); err == nil && len(timeStr) > 0 { + if dur, err := durationToSeconds(timeStr); err != nil { + panic(err) + } else { + return &dur + } + } else { + var intPtr int + if err := json.Unmarshal(jsonBytes, &intPtr); err == nil { + return &intPtr + } + } + + log.Println("[WARN] Unexpected unmarshalTimeRemaining value: ", jsonBytes) + return nil +} + +// durationToSeconds takes a hh:mm:ss string and returns the number of seconds +func durationToSeconds(s string) (int, error) { + multipliers := [3]int{60 * 60, 60, 1} + segs := strings.Split(s, ":") + if len(segs) > len(multipliers) { + return 0, fmt.Errorf("too many ':' separators in time duration: %s", s) + } + var d int + l := len(segs) + for i := 0; i < l; i++ { + m, err := strconv.Atoi(segs[i]) + if err != nil { + return 0, err + } + d += m * multipliers[i+len(multipliers)-l] + } + return d, nil +} diff --git a/vendor/github.com/linode/linodego/account_invoices.go b/vendor/github.com/linode/linodego/account_invoices.go new file mode 100644 index 000000000..75ca2f73d --- /dev/null +++ b/vendor/github.com/linode/linodego/account_invoices.go @@ -0,0 +1,125 @@ +package linodego + +import ( + "context" + "fmt" + "time" +) + +// Invoice structs reflect an invoice for billable activity on the account. +type Invoice struct { + DateStr string `json:"date"` + + ID int `json:"id"` + Label string `json:"label"` + Total float32 `json:"total"` + Date *time.Time `json:"-"` +} + +// InvoiceItem structs reflect an single billable activity associate with an Invoice +type InvoiceItem struct { + FromStr string `json:"from"` + ToStr string `json:"to"` + + Label string `json:"label"` + Type string `json:"type"` + UnitPrice int `json:"unitprice"` + Quantity int `json:"quantity"` + Amount float32 `json:"amount"` + From *time.Time `json:"-"` + To *time.Time `json:"-"` +} + +// InvoicesPagedResponse represents a paginated Invoice API response +type InvoicesPagedResponse struct { + *PageOptions + Data []Invoice `json:"data"` +} + +// endpoint gets the endpoint URL for Invoice +func (InvoicesPagedResponse) endpoint(c *Client) string { + endpoint, err := c.Invoices.Endpoint() + if err != nil { + panic(err) + } + return endpoint +} + +// appendData appends Invoices when processing paginated Invoice responses +func (resp *InvoicesPagedResponse) appendData(r *InvoicesPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListInvoices gets a paginated list of Invoices against the Account +func (c *Client) ListInvoices(ctx context.Context, opts *ListOptions) ([]Invoice, error) { + response := InvoicesPagedResponse{} + err := c.listHelper(ctx, &response, opts) + for i := range response.Data { + response.Data[i].fixDates() + } + if err != nil { + return nil, err + } + return response.Data, nil +} + +// fixDates converts JSON timestamps to Go time.Time values +func (v *Invoice) fixDates() *Invoice { + v.Date, _ = parseDates(v.DateStr) + return v +} + +// fixDates converts JSON timestamps to Go time.Time values +func (v *InvoiceItem) fixDates() *InvoiceItem { + v.From, _ = parseDates(v.FromStr) + v.To, _ = parseDates(v.ToStr) + return v +} + +// GetInvoice gets the a single Invoice matching the provided ID +func (c *Client) GetInvoice(ctx context.Context, id int) (*Invoice, error) { + e, err := c.Invoices.Endpoint() + if err != nil { + return nil, err + } + + e = fmt.Sprintf("%s/%d", e, id) + r, err := coupleAPIErrors(c.R(ctx).SetResult(&Invoice{}).Get(e)) + if err != nil { + return nil, err + } + return r.Result().(*Invoice).fixDates(), nil +} + +// InvoiceItemsPagedResponse represents a paginated Invoice Item API response +type InvoiceItemsPagedResponse struct { + *PageOptions + Data []InvoiceItem `json:"data"` +} + +// endpointWithID gets the endpoint URL for InvoiceItems associated with a specific Invoice +func (InvoiceItemsPagedResponse) endpointWithID(c *Client, id int) string { + endpoint, err := c.InvoiceItems.endpointWithID(id) + if err != nil { + panic(err) + } + return endpoint +} + +// appendData appends InvoiceItems when processing paginated Invoice Item responses +func (resp *InvoiceItemsPagedResponse) appendData(r *InvoiceItemsPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListInvoiceItems gets the invoice items associated with a specific Invoice +func (c *Client) ListInvoiceItems(ctx context.Context, id int, opts *ListOptions) ([]InvoiceItem, error) { + response := InvoiceItemsPagedResponse{} + err := c.listHelperWithID(ctx, &response, id, opts) + for i := range response.Data { + response.Data[i].fixDates() + } + if err != nil { + return nil, err + } + return response.Data, nil +} diff --git a/vendor/github.com/linode/linodego/account_notifications.go b/vendor/github.com/linode/linodego/account_notifications.go new file mode 100644 index 000000000..6b63e3850 --- /dev/null +++ b/vendor/github.com/linode/linodego/account_notifications.go @@ -0,0 +1,73 @@ +package linodego + +import ( + "context" + "time" +) + +// Notification represents a notification on an Account +type Notification struct { + UntilStr string `json:"until"` + WhenStr string `json:"when"` + + Label string `json:"label"` + Message string `json:"message"` + Type string `json:"type"` + Severity string `json:"severity"` + Entity *NotificationEntity `json:"entity"` + Until *time.Time `json:"-"` + When *time.Time `json:"-"` +} + +// NotificationEntity adds detailed information about the Notification. +// This could refer to the ticket that triggered the notification, for example. +type NotificationEntity struct { + ID int `json:"id"` + Label string `json:"label"` + Type string `json:"type"` + URL string `json:"url"` +} + +// NotificationsPagedResponse represents a paginated Notifications API response +type NotificationsPagedResponse struct { + *PageOptions + Data []Notification `json:"data"` +} + +// endpoint gets the endpoint URL for Notification +func (NotificationsPagedResponse) endpoint(c *Client) string { + endpoint, err := c.Notifications.Endpoint() + if err != nil { + panic(err) + } + return endpoint +} + +// appendData appends Notifications when processing paginated Notification responses +func (resp *NotificationsPagedResponse) appendData(r *NotificationsPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListNotifications gets a collection of Notification objects representing important, +// often time-sensitive items related to the Account. An account cannot interact directly with +// Notifications, and a Notification will disappear when the circumstances causing it +// have been resolved. For example, if the account has an important Ticket open, a response +// to the Ticket will dismiss the Notification. +func (c *Client) ListNotifications(ctx context.Context, opts *ListOptions) ([]Notification, error) { + response := NotificationsPagedResponse{} + err := c.listHelper(ctx, &response, opts) + for i := range response.Data { + response.Data[i].fixDates() + } + if err != nil { + return nil, err + } + return response.Data, nil +} + +// fixDates converts JSON timestamps to Go time.Time values +func (v *Notification) fixDates() *Notification { + v.Until, _ = parseDates(v.UntilStr) + v.When, _ = parseDates(v.WhenStr) + return v +} diff --git a/vendor/github.com/linode/linodego/client.go b/vendor/github.com/linode/linodego/client.go new file mode 100644 index 000000000..81c51f692 --- /dev/null +++ b/vendor/github.com/linode/linodego/client.go @@ -0,0 +1,201 @@ +package linodego + +import ( + "context" + "fmt" + "log" + "net/http" + "os" + "strconv" + + "github.com/go-resty/resty" +) + +const ( + // APIHost Linode API hostname + APIHost = "api.linode.com" + // APIVersion Linode API version + APIVersion = "v4" + // APIProto connect to API with http(s) + APIProto = "https" + // Version of linodego + Version = "0.5.1" + // APIEnvVar environment var to check for API token + APIEnvVar = "LINODE_TOKEN" + // APISecondsPerPoll how frequently to poll for new Events + APISecondsPerPoll = 10 +) + +// DefaultUserAgent is the default User-Agent sent in HTTP request headers +const DefaultUserAgent = "linodego " + Version + " https://github.com/linode/linodego" + +var envDebug = false + +// Client is a wrapper around the Resty client +type Client struct { + resty *resty.Client + userAgent string + resources map[string]*Resource + debug bool + + Images *Resource + InstanceDisks *Resource + InstanceConfigs *Resource + InstanceSnapshots *Resource + InstanceIPs *Resource + InstanceVolumes *Resource + Instances *Resource + IPAddresses *Resource + IPv6Pools *Resource + IPv6Ranges *Resource + Regions *Resource + StackScripts *Resource + Volumes *Resource + Kernels *Resource + Types *Resource + Domains *Resource + DomainRecords *Resource + Longview *Resource + LongviewClients *Resource + LongviewSubscriptions *Resource + NodeBalancers *Resource + NodeBalancerConfigs *Resource + NodeBalancerNodes *Resource + SSHKeys *Resource + Tickets *Resource + Account *Resource + Invoices *Resource + InvoiceItems *Resource + Events *Resource + Notifications *Resource + Profile *Resource + Managed *Resource +} + +func init() { + // Wether or not we will enable Resty debugging output + if apiDebug, ok := os.LookupEnv("LINODE_DEBUG"); ok { + if parsed, err := strconv.ParseBool(apiDebug); err == nil { + envDebug = parsed + log.Println("[INFO] LINODE_DEBUG being set to", envDebug) + } else { + log.Println("[WARN] LINODE_DEBUG should be an integer, 0 or 1") + } + } + +} + +// SetUserAgent sets a custom user-agent for HTTP requests +func (c *Client) SetUserAgent(ua string) *Client { + c.userAgent = ua + c.resty.SetHeader("User-Agent", c.userAgent) + + return c +} + +// R wraps resty's R method +func (c *Client) R(ctx context.Context) *resty.Request { + return c.resty.R(). + ExpectContentType("application/json"). + SetHeader("Content-Type", "application/json"). + SetContext(ctx). + SetError(APIError{}) +} + +// SetDebug sets the debug on resty's client +func (c *Client) SetDebug(debug bool) *Client { + c.debug = debug + c.resty.SetDebug(debug) + return c +} + +// SetBaseURL sets the base URL of the Linode v4 API (https://api.linode.com/v4) +func (c *Client) SetBaseURL(url string) *Client { + c.resty.SetHostURL(url) + return c +} + +// Resource looks up a resource by name +func (c Client) Resource(resourceName string) *Resource { + selectedResource, ok := c.resources[resourceName] + if !ok { + log.Fatalf("Could not find resource named '%s', exiting.", resourceName) + } + return selectedResource +} + +// NewClient factory to create new Client struct +func NewClient(hc *http.Client) (client Client) { + restyClient := resty.NewWithClient(hc) + client.resty = restyClient + client.SetUserAgent(DefaultUserAgent) + client.SetBaseURL(fmt.Sprintf("%s://%s/%s", APIProto, APIHost, APIVersion)) + + resources := map[string]*Resource{ + stackscriptsName: NewResource(&client, stackscriptsName, stackscriptsEndpoint, false, Stackscript{}, StackscriptsPagedResponse{}), + imagesName: NewResource(&client, imagesName, imagesEndpoint, false, Image{}, ImagesPagedResponse{}), + instancesName: NewResource(&client, instancesName, instancesEndpoint, false, Instance{}, InstancesPagedResponse{}), + instanceDisksName: NewResource(&client, instanceDisksName, instanceDisksEndpoint, true, InstanceDisk{}, InstanceDisksPagedResponse{}), + instanceConfigsName: NewResource(&client, instanceConfigsName, instanceConfigsEndpoint, true, InstanceConfig{}, InstanceConfigsPagedResponse{}), + instanceSnapshotsName: NewResource(&client, instanceSnapshotsName, instanceSnapshotsEndpoint, true, InstanceSnapshot{}, nil), + instanceIPsName: NewResource(&client, instanceIPsName, instanceIPsEndpoint, true, InstanceIP{}, nil), // really? + instanceVolumesName: NewResource(&client, instanceVolumesName, instanceVolumesEndpoint, true, nil, InstanceVolumesPagedResponse{}), // really? + ipaddressesName: NewResource(&client, ipaddressesName, ipaddressesEndpoint, false, nil, IPAddressesPagedResponse{}), // really? + ipv6poolsName: NewResource(&client, ipv6poolsName, ipv6poolsEndpoint, false, nil, IPv6PoolsPagedResponse{}), // really? + ipv6rangesName: NewResource(&client, ipv6rangesName, ipv6rangesEndpoint, false, IPv6Range{}, IPv6RangesPagedResponse{}), + regionsName: NewResource(&client, regionsName, regionsEndpoint, false, Region{}, RegionsPagedResponse{}), + volumesName: NewResource(&client, volumesName, volumesEndpoint, false, Volume{}, VolumesPagedResponse{}), + kernelsName: NewResource(&client, kernelsName, kernelsEndpoint, false, LinodeKernel{}, LinodeKernelsPagedResponse{}), + typesName: NewResource(&client, typesName, typesEndpoint, false, LinodeType{}, LinodeTypesPagedResponse{}), + domainsName: NewResource(&client, domainsName, domainsEndpoint, false, Domain{}, DomainsPagedResponse{}), + domainRecordsName: NewResource(&client, domainRecordsName, domainRecordsEndpoint, true, DomainRecord{}, DomainRecordsPagedResponse{}), + longviewName: NewResource(&client, longviewName, longviewEndpoint, false, nil, nil), // really? + longviewclientsName: NewResource(&client, longviewclientsName, longviewclientsEndpoint, false, LongviewClient{}, LongviewClientsPagedResponse{}), + longviewsubscriptionsName: NewResource(&client, longviewsubscriptionsName, longviewsubscriptionsEndpoint, false, LongviewSubscription{}, LongviewSubscriptionsPagedResponse{}), + nodebalancersName: NewResource(&client, nodebalancersName, nodebalancersEndpoint, false, NodeBalancer{}, NodeBalancerConfigsPagedResponse{}), + nodebalancerconfigsName: NewResource(&client, nodebalancerconfigsName, nodebalancerconfigsEndpoint, true, NodeBalancerConfig{}, NodeBalancerConfigsPagedResponse{}), + nodebalancernodesName: NewResource(&client, nodebalancernodesName, nodebalancernodesEndpoint, true, NodeBalancerNode{}, NodeBalancerNodesPagedResponse{}), + sshkeysName: NewResource(&client, sshkeysName, sshkeysEndpoint, false, SSHKey{}, SSHKeysPagedResponse{}), + ticketsName: NewResource(&client, ticketsName, ticketsEndpoint, false, Ticket{}, TicketsPagedResponse{}), + accountName: NewResource(&client, accountName, accountEndpoint, false, Account{}, nil), // really? + eventsName: NewResource(&client, eventsName, eventsEndpoint, false, Event{}, EventsPagedResponse{}), + invoicesName: NewResource(&client, invoicesName, invoicesEndpoint, false, Invoice{}, InvoicesPagedResponse{}), + invoiceItemsName: NewResource(&client, invoiceItemsName, invoiceItemsEndpoint, true, InvoiceItem{}, InvoiceItemsPagedResponse{}), + profileName: NewResource(&client, profileName, profileEndpoint, false, nil, nil), // really? + managedName: NewResource(&client, managedName, managedEndpoint, false, nil, nil), // really? + } + + client.resources = resources + + client.SetDebug(envDebug) + client.Images = resources[imagesName] + client.StackScripts = resources[stackscriptsName] + client.Instances = resources[instancesName] + client.Regions = resources[regionsName] + client.InstanceDisks = resources[instanceDisksName] + client.InstanceConfigs = resources[instanceConfigsName] + client.InstanceSnapshots = resources[instanceSnapshotsName] + client.InstanceIPs = resources[instanceIPsName] + client.InstanceVolumes = resources[instanceVolumesName] + client.IPAddresses = resources[ipaddressesName] + client.IPv6Pools = resources[ipv6poolsName] + client.IPv6Ranges = resources[ipv6rangesName] + client.Volumes = resources[volumesName] + client.Kernels = resources[kernelsName] + client.Types = resources[typesName] + client.Domains = resources[domainsName] + client.DomainRecords = resources[domainRecordsName] + client.Longview = resources[longviewName] + client.LongviewSubscriptions = resources[longviewsubscriptionsName] + client.NodeBalancers = resources[nodebalancersName] + client.NodeBalancerConfigs = resources[nodebalancerconfigsName] + client.NodeBalancerNodes = resources[nodebalancernodesName] + client.SSHKeys = resources[sshkeysName] + client.Tickets = resources[ticketsName] + client.Account = resources[accountName] + client.Events = resources[eventsName] + client.Invoices = resources[invoicesName] + client.Profile = resources[profileName] + client.Managed = resources[managedName] + return +} diff --git a/vendor/github.com/linode/linodego/domain_records.go b/vendor/github.com/linode/linodego/domain_records.go new file mode 100644 index 000000000..2ebbff5b7 --- /dev/null +++ b/vendor/github.com/linode/linodego/domain_records.go @@ -0,0 +1,211 @@ +package linodego + +import ( + "context" + "encoding/json" + "fmt" +) + +// DomainRecord represents a DomainRecord object +type DomainRecord struct { + ID int `json:"id"` + Type DomainRecordType `json:"type"` + Name string `json:"name"` + Target string `json:"target"` + Priority int `json:"priority"` + Weight int `json:"weight"` + Port int `json:"port"` + Service *string `json:"service"` + Protocol *string `json:"protocol"` + TTLSec int `json:"ttl_sec"` + Tag *string `json:"tag"` +} + +// DomainRecordCreateOptions fields are those accepted by CreateDomainRecord +type DomainRecordCreateOptions struct { + Type DomainRecordType `json:"type"` + Name string `json:"name"` + Target string `json:"target"` + Priority *int `json:"priority,omitempty"` + Weight *int `json:"weight,omitempty"` + Port *int `json:"port,omitempty"` + Service *string `json:"service,omitempty"` + Protocol *string `json:"protocol,omitempty"` + TTLSec int `json:"ttl_sec,omitempty"` // 0 is not accepted by Linode, so can be omitted + Tag *string `json:"tag,omitempty"` +} + +// DomainRecordUpdateOptions fields are those accepted by UpdateDomainRecord +type DomainRecordUpdateOptions struct { + Type DomainRecordType `json:"type,omitempty"` + Name string `json:"name,omitempty"` + Target string `json:"target,omitempty"` + Priority *int `json:"priority,omitempty"` // 0 is valid, so omit only nil values + Weight *int `json:"weight,omitempty"` // 0 is valid, so omit only nil values + Port *int `json:"port,omitempty"` // 0 is valid to spec, so omit only nil values + Service *string `json:"service,omitempty"` + Protocol *string `json:"protocol,omitempty"` + TTLSec int `json:"ttl_sec,omitempty"` // 0 is not accepted by Linode, so can be omitted + Tag *string `json:"tag,omitempty"` +} + +// DomainRecordType constants start with RecordType and include Linode API Domain Record Types +type DomainRecordType string + +// DomainRecordType contants are the DNS record types a DomainRecord can assign +const ( + RecordTypeA DomainRecordType = "A" + RecordTypeAAAA DomainRecordType = "AAAA" + RecordTypeNS DomainRecordType = "NS" + RecordTypeMX DomainRecordType = "MX" + RecordTypeCNAME DomainRecordType = "CNAME" + RecordTypeTXT DomainRecordType = "TXT" + RecordTypeSRV DomainRecordType = "SRV" + RecordTypePTR DomainRecordType = "PTR" + RecordTypeCAA DomainRecordType = "CAA" +) + +// GetUpdateOptions converts a DomainRecord to DomainRecordUpdateOptions for use in UpdateDomainRecord +func (d DomainRecord) GetUpdateOptions() (du DomainRecordUpdateOptions) { + du.Type = d.Type + du.Name = d.Name + du.Target = d.Target + du.Priority = copyInt(&d.Priority) + du.Weight = copyInt(&d.Weight) + du.Port = copyInt(&d.Port) + du.Service = copyString(d.Service) + du.Protocol = copyString(d.Protocol) + du.TTLSec = d.TTLSec + du.Tag = copyString(d.Tag) + return +} + +func copyInt(iPtr *int) *int { + if iPtr == nil { + return nil + } + var t = *iPtr + return &t +} + +func copyString(sPtr *string) *string { + if sPtr == nil { + return nil + } + var t = *sPtr + return &t +} + +// DomainRecordsPagedResponse represents a paginated DomainRecord API response +type DomainRecordsPagedResponse struct { + *PageOptions + Data []DomainRecord `json:"data"` +} + +// endpoint gets the endpoint URL for InstanceConfig +func (DomainRecordsPagedResponse) endpointWithID(c *Client, id int) string { + endpoint, err := c.DomainRecords.endpointWithID(id) + if err != nil { + panic(err) + } + return endpoint +} + +// appendData appends DomainRecords when processing paginated DomainRecord responses +func (resp *DomainRecordsPagedResponse) appendData(r *DomainRecordsPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListDomainRecords lists DomainRecords +func (c *Client) ListDomainRecords(ctx context.Context, domainID int, opts *ListOptions) ([]DomainRecord, error) { + response := DomainRecordsPagedResponse{} + err := c.listHelperWithID(ctx, &response, domainID, opts) + if err != nil { + return nil, err + } + return response.Data, nil +} + +// fixDates converts JSON timestamps to Go time.Time values +func (d *DomainRecord) fixDates() *DomainRecord { + return d +} + +// GetDomainRecord gets the domainrecord with the provided ID +func (c *Client) GetDomainRecord(ctx context.Context, domainID int, id int) (*DomainRecord, error) { + e, err := c.DomainRecords.endpointWithID(domainID) + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, id) + r, err := coupleAPIErrors(c.R(ctx).SetResult(&DomainRecord{}).Get(e)) + if err != nil { + return nil, err + } + return r.Result().(*DomainRecord), nil +} + +// CreateDomainRecord creates a DomainRecord +func (c *Client) CreateDomainRecord(ctx context.Context, domainID int, domainrecord DomainRecordCreateOptions) (*DomainRecord, error) { + var body string + e, err := c.DomainRecords.endpointWithID(domainID) + if err != nil { + return nil, err + } + + req := c.R(ctx).SetResult(&DomainRecord{}) + + bodyData, err := json.Marshal(domainrecord) + if err != nil { + return nil, NewError(err) + } + body = string(bodyData) + + r, err := coupleAPIErrors(req. + SetBody(body). + Post(e)) + + if err != nil { + return nil, err + } + return r.Result().(*DomainRecord).fixDates(), nil +} + +// UpdateDomainRecord updates the DomainRecord with the specified id +func (c *Client) UpdateDomainRecord(ctx context.Context, domainID int, id int, domainrecord DomainRecordUpdateOptions) (*DomainRecord, error) { + var body string + e, err := c.DomainRecords.endpointWithID(domainID) + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, id) + + req := c.R(ctx).SetResult(&DomainRecord{}) + + if bodyData, err := json.Marshal(domainrecord); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + r, err := coupleAPIErrors(req. + SetBody(body). + Put(e)) + + if err != nil { + return nil, err + } + return r.Result().(*DomainRecord).fixDates(), nil +} + +// DeleteDomainRecord deletes the DomainRecord with the specified id +func (c *Client) DeleteDomainRecord(ctx context.Context, domainID int, id int) error { + e, err := c.DomainRecords.endpointWithID(domainID) + if err != nil { + return err + } + e = fmt.Sprintf("%s/%d", e, id) + + _, err = coupleAPIErrors(c.R(ctx).Delete(e)) + return err +} diff --git a/vendor/github.com/linode/linodego/domains.go b/vendor/github.com/linode/linodego/domains.go new file mode 100644 index 000000000..4ae7167fa --- /dev/null +++ b/vendor/github.com/linode/linodego/domains.go @@ -0,0 +1,285 @@ +package linodego + +import ( + "context" + "encoding/json" + "fmt" +) + +// Domain represents a Domain object +type Domain struct { + // This Domain's unique ID + ID int `json:"id"` + + // The domain this Domain represents. These must be unique in our system; you cannot have two Domains representing the same domain. + Domain string `json:"domain"` + + // If this Domain represents the authoritative source of information for the domain it describes, or if it is a read-only copy of a master (also called a slave). + Type DomainType `json:"type"` // Enum:"master" "slave" + + // Deprecated: The group this Domain belongs to. This is for display purposes only. + Group string `json:"group"` + + // Used to control whether this Domain is currently being rendered. + Status DomainStatus `json:"status"` // Enum:"disabled" "active" "edit_mode" "has_errors" + + // A description for this Domain. This is for display purposes only. + Description string `json:"description"` + + // Start of Authority email address. This is required for master Domains. + SOAEmail string `json:"soa_email"` + + // The interval, in seconds, at which a failed refresh should be retried. + // Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value. + RetrySec int `json:"retry_sec"` + + // The IP addresses representing the master DNS for this Domain. + MasterIPs []string `json:"master_ips"` + + // The list of IPs that may perform a zone transfer for this Domain. This is potentially dangerous, and should be set to an empty list unless you intend to use it. + AXfrIPs []string `json:"axfr_ips"` + + // The amount of time in seconds that may pass before this Domain is no longer authoritative. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value. + ExpireSec int `json:"expire_sec"` + + // The amount of time in seconds before this Domain should be refreshed. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value. + RefreshSec int `json:"refresh_sec"` + + // "Time to Live" - the amount of time in seconds that this Domain's records may be cached by resolvers or other domain servers. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value. + TTLSec int `json:"ttl_sec"` +} + +// DomainCreateOptions fields are those accepted by CreateDomain +type DomainCreateOptions struct { + // The domain this Domain represents. These must be unique in our system; you cannot have two Domains representing the same domain. + Domain string `json:"domain"` + + // If this Domain represents the authoritative source of information for the domain it describes, or if it is a read-only copy of a master (also called a slave). + // Enum:"master" "slave" + Type DomainType `json:"type"` + + // Deprecated: The group this Domain belongs to. This is for display purposes only. + Group string `json:"group,omitempty"` + + // Used to control whether this Domain is currently being rendered. + // Enum:"disabled" "active" "edit_mode" "has_errors" + Status DomainStatus `json:"status,omitempty"` + + // A description for this Domain. This is for display purposes only. + Description string `json:"description,omitempty"` + + // Start of Authority email address. This is required for master Domains. + SOAEmail string `json:"soa_email,omitempty"` + + // The interval, in seconds, at which a failed refresh should be retried. + // Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value. + RetrySec int `json:"retry_sec,omitempty"` + + // The IP addresses representing the master DNS for this Domain. + MasterIPs []string `json:"master_ips,omitempty"` + + // The list of IPs that may perform a zone transfer for this Domain. This is potentially dangerous, and should be set to an empty list unless you intend to use it. + AXfrIPs []string `json:"axfr_ips,omitempty"` + + // The amount of time in seconds that may pass before this Domain is no longer authoritative. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value. + ExpireSec int `json:"expire_sec,omitempty"` + + // The amount of time in seconds before this Domain should be refreshed. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value. + RefreshSec int `json:"refresh_sec,omitempty"` + + // "Time to Live" - the amount of time in seconds that this Domain's records may be cached by resolvers or other domain servers. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value. + TTLSec int `json:"ttl_sec,omitempty"` +} + +// DomainUpdateOptions converts a Domain to DomainUpdateOptions for use in UpdateDomain +type DomainUpdateOptions struct { + // The domain this Domain represents. These must be unique in our system; you cannot have two Domains representing the same domain. + Domain string `json:"domain,omitempty"` + + // If this Domain represents the authoritative source of information for the domain it describes, or if it is a read-only copy of a master (also called a slave). + // Enum:"master" "slave" + Type DomainType `json:"type,omitempty"` + + // Deprecated: The group this Domain belongs to. This is for display purposes only. + Group string `json:"group,omitempty"` + + // Used to control whether this Domain is currently being rendered. + // Enum:"disabled" "active" "edit_mode" "has_errors" + Status DomainStatus `json:"status,omitempty"` + + // A description for this Domain. This is for display purposes only. + Description string `json:"description,omitempty"` + + // Start of Authority email address. This is required for master Domains. + SOAEmail string `json:"soa_email,omitempty"` + + // The interval, in seconds, at which a failed refresh should be retried. + // Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value. + RetrySec int `json:"retry_sec,omitempty"` + + // The IP addresses representing the master DNS for this Domain. + MasterIPs []string `json:"master_ips,omitempty"` + + // The list of IPs that may perform a zone transfer for this Domain. This is potentially dangerous, and should be set to an empty list unless you intend to use it. + AXfrIPs []string `json:"axfr_ips,omitempty"` + + // The amount of time in seconds that may pass before this Domain is no longer authoritative. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value. + ExpireSec int `json:"expire_sec,omitempty"` + + // The amount of time in seconds before this Domain should be refreshed. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value. + RefreshSec int `json:"refresh_sec,omitempty"` + + // "Time to Live" - the amount of time in seconds that this Domain's records may be cached by resolvers or other domain servers. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value. + TTLSec int `json:"ttl_sec,omitempty"` +} + +// DomainType constants start with DomainType and include Linode API Domain Type values +type DomainType string + +// DomainType constants reflect the DNS zone type of a Domain +const ( + DomainTypeMaster DomainType = "master" + DomainTypeSlave DomainType = "slave" +) + +// DomainStatus constants start with DomainStatus and include Linode API Domain Status values +type DomainStatus string + +// DomainStatus constants reflect the current status of a Domain +const ( + DomainStatusDisabled DomainStatus = "disabled" + DomainStatusActive DomainStatus = "active" + DomainStatusEditMode DomainStatus = "edit_mode" + DomainStatusHasErrors DomainStatus = "has_errors" +) + +// GetUpdateOptions converts a Domain to DomainUpdateOptions for use in UpdateDomain +func (d Domain) GetUpdateOptions() (du DomainUpdateOptions) { + du.Domain = d.Domain + du.Type = d.Type + du.Group = d.Group + du.Status = d.Status + du.Description = d.Description + du.SOAEmail = d.SOAEmail + du.RetrySec = d.RetrySec + du.MasterIPs = d.MasterIPs + du.AXfrIPs = d.AXfrIPs + du.ExpireSec = d.ExpireSec + du.RefreshSec = d.RefreshSec + du.TTLSec = d.TTLSec + return +} + +// DomainsPagedResponse represents a paginated Domain API response +type DomainsPagedResponse struct { + *PageOptions + Data []Domain `json:"data"` +} + +// endpoint gets the endpoint URL for Domain +func (DomainsPagedResponse) endpoint(c *Client) string { + endpoint, err := c.Domains.Endpoint() + if err != nil { + panic(err) + } + return endpoint +} + +// appendData appends Domains when processing paginated Domain responses +func (resp *DomainsPagedResponse) appendData(r *DomainsPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListDomains lists Domains +func (c *Client) ListDomains(ctx context.Context, opts *ListOptions) ([]Domain, error) { + response := DomainsPagedResponse{} + err := c.listHelper(ctx, &response, opts) + if err != nil { + return nil, err + } + return response.Data, nil +} + +// fixDates converts JSON timestamps to Go time.Time values +func (d *Domain) fixDates() *Domain { + return d +} + +// GetDomain gets the domain with the provided ID +func (c *Client) GetDomain(ctx context.Context, id int) (*Domain, error) { + e, err := c.Domains.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, id) + r, err := coupleAPIErrors(c.R(ctx).SetResult(&Domain{}).Get(e)) + if err != nil { + return nil, err + } + return r.Result().(*Domain).fixDates(), nil +} + +// CreateDomain creates a Domain +func (c *Client) CreateDomain(ctx context.Context, domain DomainCreateOptions) (*Domain, error) { + var body string + e, err := c.Domains.Endpoint() + if err != nil { + return nil, err + } + + req := c.R(ctx).SetResult(&Domain{}) + + bodyData, err := json.Marshal(domain) + if err != nil { + return nil, NewError(err) + } + body = string(bodyData) + + r, err := coupleAPIErrors(req. + SetBody(body). + Post(e)) + + if err != nil { + return nil, err + } + return r.Result().(*Domain).fixDates(), nil +} + +// UpdateDomain updates the Domain with the specified id +func (c *Client) UpdateDomain(ctx context.Context, id int, domain DomainUpdateOptions) (*Domain, error) { + var body string + e, err := c.Domains.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, id) + + req := c.R(ctx).SetResult(&Domain{}) + + if bodyData, err := json.Marshal(domain); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + r, err := coupleAPIErrors(req. + SetBody(body). + Put(e)) + + if err != nil { + return nil, err + } + return r.Result().(*Domain).fixDates(), nil +} + +// DeleteDomain deletes the Domain with the specified id +func (c *Client) DeleteDomain(ctx context.Context, id int) error { + e, err := c.Domains.Endpoint() + if err != nil { + return err + } + e = fmt.Sprintf("%s/%d", e, id) + + _, err = coupleAPIErrors(c.R(ctx).Delete(e)) + return err +} diff --git a/vendor/github.com/linode/linodego/errors.go b/vendor/github.com/linode/linodego/errors.go new file mode 100644 index 000000000..7ca69c2ba --- /dev/null +++ b/vendor/github.com/linode/linodego/errors.go @@ -0,0 +1,109 @@ +package linodego + +import ( + "fmt" + "log" + "net/http" + "strings" + + "github.com/go-resty/resty" +) + +const ( + // ErrorFromString is the Code identifying Errors created by string types + ErrorFromString = 1 + // ErrorFromError is the Code identifying Errors created by error types + ErrorFromError = 2 + // ErrorFromStringer is the Code identifying Errors created by fmt.Stringer types + ErrorFromStringer = 3 +) + +// Error wraps the LinodeGo error with the relevant http.Response +type Error struct { + Response *http.Response + Code int + Message string +} + +// APIErrorReason is an individual invalid request message returned by the Linode API +type APIErrorReason struct { + Reason string `json:"reason"` + Field string `json:"field"` +} + +func (r APIErrorReason) Error() string { + if len(r.Field) == 0 { + return r.Reason + } + return fmt.Sprintf("[%s] %s", r.Field, r.Reason) +} + +// APIError is the error-set returned by the Linode API when presented with an invalid request +type APIError struct { + Errors []APIErrorReason `json:"errors"` +} + +func coupleAPIErrors(r *resty.Response, err error) (*resty.Response, error) { + if err != nil { + return nil, NewError(err) + } + + if r.Error() != nil { + apiError, ok := r.Error().(*APIError) + if !ok || (ok && len(apiError.Errors) == 0) { + return r, nil + } + return nil, NewError(r) + } + + return r, nil +} + +func (e APIError) Error() string { + var x []string + for _, msg := range e.Errors { + x = append(x, msg.Error()) + } + return strings.Join(x, "; ") +} + +func (g Error) Error() string { + return fmt.Sprintf("[%03d] %s", g.Code, g.Message) +} + +// NewError creates a linodego.Error with a Code identifying the source err type, +// - ErrorFromString (1) from a string +// - ErrorFromError (2) for an error +// - ErrorFromStringer (3) for a Stringer +// - HTTP Status Codes (100-600) for a resty.Response object +func NewError(err interface{}) *Error { + if err == nil { + return nil + } + + switch e := err.(type) { + case *Error: + return e + case *resty.Response: + apiError, ok := e.Error().(*APIError) + + if !ok { + log.Fatalln("Unexpected Resty Error Response") + } + + return &Error{ + Code: e.RawResponse.StatusCode, + Message: apiError.Error(), + Response: e.RawResponse, + } + case error: + return &Error{Code: ErrorFromError, Message: e.Error()} + case string: + return &Error{Code: ErrorFromString, Message: e} + case fmt.Stringer: + return &Error{Code: ErrorFromStringer, Message: e.String()} + default: + log.Fatalln("Unsupported type to linodego.NewError") + panic(err) + } +} diff --git a/vendor/github.com/linode/linodego/images.go b/vendor/github.com/linode/linodego/images.go new file mode 100644 index 000000000..91ab57bb9 --- /dev/null +++ b/vendor/github.com/linode/linodego/images.go @@ -0,0 +1,163 @@ +package linodego + +import ( + "context" + "encoding/json" + "fmt" + "time" +) + +// Image represents a deployable Image object for use with Linode Instances +type Image struct { + CreatedStr string `json:"created"` + UpdatedStr string `json:"updated"` + ID string `json:"id"` + CreatedBy string `json:"created_by"` + Label string `json:"label"` + Description string `json:"description"` + Type string `json:"type"` + Vendor string `json:"vendor"` + Size int `json:"size"` + IsPublic bool `json:"is_public"` + Deprecated bool `json:"deprecated"` + + Created *time.Time `json:"-"` + Updated *time.Time `json:"-"` +} + +// ImageCreateOptions fields are those accepted by CreateImage +type ImageCreateOptions struct { + DiskID int `json:"disk_id"` + Label string `json:"label"` + Description string `json:"description,omitempty"` +} + +// ImageUpdateOptions fields are those accepted by UpdateImage +type ImageUpdateOptions struct { + Label string `json:"label,omitempty"` + Description *string `json:"description,omitempty"` +} + +func (i *Image) fixDates() *Image { + i.Created, _ = parseDates(i.CreatedStr) + i.Updated, _ = parseDates(i.UpdatedStr) + return i +} + +// GetUpdateOptions converts an Image to ImageUpdateOptions for use in UpdateImage +func (i Image) GetUpdateOptions() (iu ImageUpdateOptions) { + iu.Label = i.Label + iu.Description = copyString(&i.Description) + return +} + +// ImagesPagedResponse represents a linode API response for listing of images +type ImagesPagedResponse struct { + *PageOptions + Data []Image `json:"data"` +} + +func (ImagesPagedResponse) endpoint(c *Client) string { + endpoint, err := c.Images.Endpoint() + if err != nil { + panic(err) + } + return endpoint +} + +func (resp *ImagesPagedResponse) appendData(r *ImagesPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListImages lists Images +func (c *Client) ListImages(ctx context.Context, opts *ListOptions) ([]Image, error) { + response := ImagesPagedResponse{} + err := c.listHelper(ctx, &response, opts) + for i := range response.Data { + response.Data[i].fixDates() + } + if err != nil { + return nil, err + } + return response.Data, nil + +} + +// GetImage gets the Image with the provided ID +func (c *Client) GetImage(ctx context.Context, id string) (*Image, error) { + e, err := c.Images.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%s", e, id) + r, err := coupleAPIErrors(c.Images.R(ctx).Get(e)) + if err != nil { + return nil, err + } + return r.Result().(*Image), nil +} + +// CreateImage creates a Image +func (c *Client) CreateImage(ctx context.Context, createOpts ImageCreateOptions) (*Image, error) { + var body string + e, err := c.Images.Endpoint() + if err != nil { + return nil, err + } + + req := c.R(ctx).SetResult(&Image{}) + + if bodyData, err := json.Marshal(createOpts); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + r, err := coupleAPIErrors(req. + SetBody(body). + Post(e)) + + if err != nil { + return nil, err + } + return r.Result().(*Image).fixDates(), nil +} + +// UpdateImage updates the Image with the specified id +func (c *Client) UpdateImage(ctx context.Context, id string, updateOpts ImageUpdateOptions) (*Image, error) { + var body string + e, err := c.Images.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%s", e, id) + + req := c.R(ctx).SetResult(&Image{}) + + if bodyData, err := json.Marshal(updateOpts); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + r, err := coupleAPIErrors(req. + SetBody(body). + Put(e)) + + if err != nil { + return nil, err + } + return r.Result().(*Image).fixDates(), nil +} + +// DeleteImage deletes the Image with the specified id +func (c *Client) DeleteImage(ctx context.Context, id string) error { + e, err := c.Images.Endpoint() + if err != nil { + return err + } + e = fmt.Sprintf("%s/%s", e, id) + + _, err = coupleAPIErrors(c.R(ctx).Delete(e)) + return err +} diff --git a/vendor/github.com/linode/linodego/instance_configs.go b/vendor/github.com/linode/linodego/instance_configs.go new file mode 100644 index 000000000..979a27495 --- /dev/null +++ b/vendor/github.com/linode/linodego/instance_configs.go @@ -0,0 +1,246 @@ +package linodego + +import ( + "context" + "encoding/json" + "fmt" + "time" +) + +// InstanceConfig represents all of the settings that control the boot and run configuration of a Linode Instance +type InstanceConfig struct { + CreatedStr string `json:"created"` + UpdatedStr string `json:"updated"` + + ID int `json:"id"` + Label string `json:"label"` + Comments string `json:"comments"` + Devices *InstanceConfigDeviceMap `json:"devices"` + Helpers *InstanceConfigHelpers `json:"helpers"` + MemoryLimit int `json:"memory_limit"` + Kernel string `json:"kernel"` + InitRD *int `json:"init_rd"` + RootDevice string `json:"root_device"` + RunLevel string `json:"run_level"` + VirtMode string `json:"virt_mode"` + Created *time.Time `json:"-"` + Updated *time.Time `json:"-"` +} + +// InstanceConfigDevice contains either the DiskID or VolumeID assigned to a Config Device +type InstanceConfigDevice struct { + DiskID int `json:"disk_id,omitempty"` + VolumeID int `json:"volume_id,omitempty"` +} + +// InstanceConfigDeviceMap contains SDA-SDH InstanceConfigDevice settings +type InstanceConfigDeviceMap struct { + SDA *InstanceConfigDevice `json:"sda,omitempty"` + SDB *InstanceConfigDevice `json:"sdb,omitempty"` + SDC *InstanceConfigDevice `json:"sdc,omitempty"` + SDD *InstanceConfigDevice `json:"sdd,omitempty"` + SDE *InstanceConfigDevice `json:"sde,omitempty"` + SDF *InstanceConfigDevice `json:"sdf,omitempty"` + SDG *InstanceConfigDevice `json:"sdg,omitempty"` + SDH *InstanceConfigDevice `json:"sdh,omitempty"` +} + +// InstanceConfigHelpers are Instance Config options that control Linux distribution specific tweaks +type InstanceConfigHelpers struct { + UpdateDBDisabled bool `json:"updatedb_disabled"` + Distro bool `json:"distro"` + ModulesDep bool `json:"modules_dep"` + Network bool `json:"network"` + DevTmpFsAutomount bool `json:"devtmpfs_automount"` +} + +// InstanceConfigsPagedResponse represents a paginated InstanceConfig API response +type InstanceConfigsPagedResponse struct { + *PageOptions + Data []InstanceConfig `json:"data"` +} + +// InstanceConfigCreateOptions are InstanceConfig settings that can be used at creation +type InstanceConfigCreateOptions struct { + Label string `json:"label,omitempty"` + Comments string `json:"comments,omitempty"` + Devices InstanceConfigDeviceMap `json:"devices"` + Helpers *InstanceConfigHelpers `json:"helpers,omitempty"` + MemoryLimit int `json:"memory_limit,omitempty"` + Kernel string `json:"kernel,omitempty"` + InitRD int `json:"init_rd,omitempty"` + RootDevice *string `json:"root_device,omitempty"` + RunLevel string `json:"run_level,omitempty"` + VirtMode string `json:"virt_mode,omitempty"` +} + +// InstanceConfigUpdateOptions are InstanceConfig settings that can be used in updates +type InstanceConfigUpdateOptions struct { + Label string `json:"label,omitempty"` + Comments string `json:"comments"` + Devices *InstanceConfigDeviceMap `json:"devices,omitempty"` + Helpers *InstanceConfigHelpers `json:"helpers,omitempty"` + // MemoryLimit 0 means unlimitted, this is not omitted + MemoryLimit int `json:"memory_limit"` + Kernel string `json:"kernel,omitempty"` + // InitRD is nullable, permit the sending of null + InitRD *int `json:"init_rd"` + RootDevice string `json:"root_device,omitempty"` + RunLevel string `json:"run_level,omitempty"` + VirtMode string `json:"virt_mode,omitempty"` +} + +// GetCreateOptions converts a InstanceConfig to InstanceConfigCreateOptions for use in CreateInstanceConfig +func (i InstanceConfig) GetCreateOptions() InstanceConfigCreateOptions { + initrd := 0 + if i.InitRD != nil { + initrd = *i.InitRD + } + return InstanceConfigCreateOptions{ + Label: i.Label, + Comments: i.Comments, + Devices: *i.Devices, + Helpers: i.Helpers, + MemoryLimit: i.MemoryLimit, + Kernel: i.Kernel, + InitRD: initrd, + RootDevice: copyString(&i.RootDevice), + RunLevel: i.RunLevel, + VirtMode: i.VirtMode, + } +} + +// GetUpdateOptions converts a InstanceConfig to InstanceConfigUpdateOptions for use in UpdateInstanceConfig +func (i InstanceConfig) GetUpdateOptions() InstanceConfigUpdateOptions { + return InstanceConfigUpdateOptions{ + Label: i.Label, + Comments: i.Comments, + Devices: i.Devices, + Helpers: i.Helpers, + MemoryLimit: i.MemoryLimit, + Kernel: i.Kernel, + InitRD: copyInt(i.InitRD), + RootDevice: i.RootDevice, + RunLevel: i.RunLevel, + VirtMode: i.VirtMode, + } +} + +// endpointWithID gets the endpoint URL for InstanceConfigs of a given Instance +func (InstanceConfigsPagedResponse) endpointWithID(c *Client, id int) string { + endpoint, err := c.InstanceConfigs.endpointWithID(id) + if err != nil { + panic(err) + } + return endpoint +} + +// appendData appends InstanceConfigs when processing paginated InstanceConfig responses +func (resp *InstanceConfigsPagedResponse) appendData(r *InstanceConfigsPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListInstanceConfigs lists InstanceConfigs +func (c *Client) ListInstanceConfigs(ctx context.Context, linodeID int, opts *ListOptions) ([]InstanceConfig, error) { + response := InstanceConfigsPagedResponse{} + err := c.listHelperWithID(ctx, &response, linodeID, opts) + for i := range response.Data { + response.Data[i].fixDates() + } + if err != nil { + return nil, err + } + return response.Data, nil +} + +// fixDates converts JSON timestamps to Go time.Time values +func (i *InstanceConfig) fixDates() *InstanceConfig { + i.Created, _ = parseDates(i.CreatedStr) + i.Updated, _ = parseDates(i.UpdatedStr) + return i +} + +// GetInstanceConfig gets the template with the provided ID +func (c *Client) GetInstanceConfig(ctx context.Context, linodeID int, configID int) (*InstanceConfig, error) { + e, err := c.InstanceConfigs.endpointWithID(linodeID) + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, configID) + r, err := coupleAPIErrors(c.R(ctx).SetResult(&InstanceConfig{}).Get(e)) + if err != nil { + return nil, err + } + return r.Result().(*InstanceConfig).fixDates(), nil +} + +// CreateInstanceConfig creates a new InstanceConfig for the given Instance +func (c *Client) CreateInstanceConfig(ctx context.Context, linodeID int, createOpts InstanceConfigCreateOptions) (*InstanceConfig, error) { + var body string + e, err := c.InstanceConfigs.endpointWithID(linodeID) + if err != nil { + return nil, err + } + + req := c.R(ctx).SetResult(&InstanceConfig{}) + + if bodyData, err := json.Marshal(createOpts); err == nil { + body = string(bodyData) + } else { + return nil, err + } + + r, err := coupleAPIErrors(req. + SetBody(body). + Post(e)) + + if err != nil { + return nil, err + } + + return r.Result().(*InstanceConfig).fixDates(), nil +} + +// UpdateInstanceConfig update an InstanceConfig for the given Instance +func (c *Client) UpdateInstanceConfig(ctx context.Context, linodeID int, configID int, updateOpts InstanceConfigUpdateOptions) (*InstanceConfig, error) { + var body string + e, err := c.InstanceConfigs.endpointWithID(linodeID) + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, configID) + req := c.R(ctx).SetResult(&InstanceConfig{}) + + if bodyData, err := json.Marshal(updateOpts); err == nil { + body = string(bodyData) + } else { + return nil, err + } + + r, err := coupleAPIErrors(req. + SetBody(body). + Put(e)) + + if err != nil { + return nil, err + } + + return r.Result().(*InstanceConfig).fixDates(), nil +} + +// RenameInstanceConfig renames an InstanceConfig +func (c *Client) RenameInstanceConfig(ctx context.Context, linodeID int, configID int, label string) (*InstanceConfig, error) { + return c.UpdateInstanceConfig(ctx, linodeID, configID, InstanceConfigUpdateOptions{Label: label}) +} + +// DeleteInstanceConfig deletes a Linode InstanceConfig +func (c *Client) DeleteInstanceConfig(ctx context.Context, linodeID int, configID int) error { + e, err := c.InstanceConfigs.endpointWithID(linodeID) + if err != nil { + return err + } + e = fmt.Sprintf("%s/%d", e, configID) + + _, err = coupleAPIErrors(c.R(ctx).Delete(e)) + return err +} diff --git a/vendor/github.com/linode/linodego/instance_disks.go b/vendor/github.com/linode/linodego/instance_disks.go new file mode 100644 index 000000000..8c31491a2 --- /dev/null +++ b/vendor/github.com/linode/linodego/instance_disks.go @@ -0,0 +1,217 @@ +package linodego + +import ( + "context" + "encoding/json" + "fmt" + "time" +) + +// InstanceDisk represents an Instance Disk object +type InstanceDisk struct { + CreatedStr string `json:"created"` + UpdatedStr string `json:"updated"` + + ID int `json:"id"` + Label string `json:"label"` + Status string `json:"status"` + Size int `json:"size"` + Filesystem DiskFilesystem `json:"filesystem"` + Created time.Time `json:"-"` + Updated time.Time `json:"-"` +} + +// DiskFilesystem constants start with Filesystem and include Linode API Filesystems +type DiskFilesystem string + +// DiskFilesystem constants represent the filesystems types an Instance Disk may use +const ( + FilesystemRaw DiskFilesystem = "raw" + FilesystemSwap DiskFilesystem = "swap" + FilesystemExt3 DiskFilesystem = "ext3" + FilesystemExt4 DiskFilesystem = "ext4" + FilesystemInitrd DiskFilesystem = "initrd" +) + +// InstanceDisksPagedResponse represents a paginated InstanceDisk API response +type InstanceDisksPagedResponse struct { + *PageOptions + Data []InstanceDisk `json:"data"` +} + +// InstanceDiskCreateOptions are InstanceDisk settings that can be used at creation +type InstanceDiskCreateOptions struct { + Label string `json:"label"` + Size int `json:"size"` + + // Image is optional, but requires RootPass if provided + Image string `json:"image,omitempty"` + RootPass string `json:"root_pass,omitempty"` + + Filesystem string `json:"filesystem,omitempty"` + AuthorizedKeys []string `json:"authorized_keys,omitempty"` + AuthorizedUsers []string `json:"authorized_users,omitempty"` + ReadOnly bool `json:"read_only,omitempty"` + StackscriptID int `json:"stackscript_id,omitempty"` + StackscriptData map[string]string `json:"stackscript_data,omitempty"` +} + +// InstanceDiskUpdateOptions are InstanceDisk settings that can be used in updates +type InstanceDiskUpdateOptions struct { + Label string `json:"label"` + ReadOnly bool `json:"read_only"` +} + +// endpointWithID gets the endpoint URL for InstanceDisks of a given Instance +func (InstanceDisksPagedResponse) endpointWithID(c *Client, id int) string { + endpoint, err := c.InstanceDisks.endpointWithID(id) + if err != nil { + panic(err) + } + return endpoint +} + +// appendData appends InstanceDisks when processing paginated InstanceDisk responses +func (resp *InstanceDisksPagedResponse) appendData(r *InstanceDisksPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListInstanceDisks lists InstanceDisks +func (c *Client) ListInstanceDisks(ctx context.Context, linodeID int, opts *ListOptions) ([]InstanceDisk, error) { + response := InstanceDisksPagedResponse{} + err := c.listHelperWithID(ctx, &response, linodeID, opts) + for i := range response.Data { + response.Data[i].fixDates() + } + if err != nil { + return nil, err + } + return response.Data, nil +} + +// fixDates converts JSON timestamps to Go time.Time values +func (v *InstanceDisk) fixDates() *InstanceDisk { + if created, err := parseDates(v.CreatedStr); err == nil { + v.Created = *created + } + if updated, err := parseDates(v.UpdatedStr); err == nil { + v.Updated = *updated + } + return v +} + +// GetInstanceDisk gets the template with the provided ID +func (c *Client) GetInstanceDisk(ctx context.Context, linodeID int, configID int) (*InstanceDisk, error) { + e, err := c.InstanceDisks.endpointWithID(linodeID) + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, configID) + r, err := coupleAPIErrors(c.R(ctx).SetResult(&InstanceDisk{}).Get(e)) + if err != nil { + return nil, err + } + return r.Result().(*InstanceDisk).fixDates(), nil +} + +// CreateInstanceDisk creates a new InstanceDisk for the given Instance +func (c *Client) CreateInstanceDisk(ctx context.Context, linodeID int, createOpts InstanceDiskCreateOptions) (*InstanceDisk, error) { + var body string + e, err := c.InstanceDisks.endpointWithID(linodeID) + if err != nil { + return nil, err + } + + req := c.R(ctx).SetResult(&InstanceDisk{}) + + if bodyData, err := json.Marshal(createOpts); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + r, err := coupleAPIErrors(req. + SetBody(body). + Post(e)) + + if err != nil { + return nil, err + } + + return r.Result().(*InstanceDisk).fixDates(), nil +} + +// UpdateInstanceDisk creates a new InstanceDisk for the given Instance +func (c *Client) UpdateInstanceDisk(ctx context.Context, linodeID int, diskID int, updateOpts InstanceDiskUpdateOptions) (*InstanceDisk, error) { + var body string + e, err := c.InstanceDisks.endpointWithID(linodeID) + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, diskID) + + req := c.R(ctx).SetResult(&InstanceDisk{}) + + if bodyData, err := json.Marshal(updateOpts); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + r, err := coupleAPIErrors(req. + SetBody(body). + Put(e)) + + if err != nil { + return nil, err + } + + return r.Result().(*InstanceDisk).fixDates(), nil +} + +// RenameInstanceDisk renames an InstanceDisk +func (c *Client) RenameInstanceDisk(ctx context.Context, linodeID int, diskID int, label string) (*InstanceDisk, error) { + return c.UpdateInstanceDisk(ctx, linodeID, diskID, InstanceDiskUpdateOptions{Label: label}) +} + +// ResizeInstanceDisk resizes the size of the Instance disk +func (c *Client) ResizeInstanceDisk(ctx context.Context, linodeID int, diskID int, size int) (*InstanceDisk, error) { + var body string + e, err := c.InstanceDisks.endpointWithID(linodeID) + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, diskID) + + req := c.R(ctx).SetResult(&InstanceDisk{}) + updateOpts := map[string]interface{}{ + "size": size, + } + + if bodyData, err := json.Marshal(updateOpts); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + r, err := coupleAPIErrors(req. + SetBody(body). + Post(e)) + + if err != nil { + return nil, err + } + return r.Result().(*InstanceDisk).fixDates(), nil +} + +// DeleteInstanceDisk deletes a Linode Instance Disk +func (c *Client) DeleteInstanceDisk(ctx context.Context, linodeID int, diskID int) error { + e, err := c.InstanceDisks.endpointWithID(linodeID) + if err != nil { + return err + } + e = fmt.Sprintf("%s/%d", e, diskID) + + _, err = coupleAPIErrors(c.R(ctx).Delete(e)) + return err +} diff --git a/vendor/github.com/linode/linodego/instance_ips.go b/vendor/github.com/linode/linodego/instance_ips.go new file mode 100644 index 000000000..a30d04f16 --- /dev/null +++ b/vendor/github.com/linode/linodego/instance_ips.go @@ -0,0 +1,106 @@ +package linodego + +import ( + "context" + "encoding/json" + "fmt" +) + +// InstanceIPAddressResponse contains the IPv4 and IPv6 details for an Instance +type InstanceIPAddressResponse struct { + IPv4 *InstanceIPv4Response `json:"ipv4"` + IPv6 *InstanceIPv6Response `json:"ipv6"` +} + +// InstanceIPv4Response contains the details of all IPv4 addresses associated with an Instance +type InstanceIPv4Response struct { + Public []*InstanceIP `json:"public"` + Private []*InstanceIP `json:"private"` + Shared []*InstanceIP `json:"shared"` +} + +// InstanceIP represents an Instance IP with additional DNS and networking details +type InstanceIP struct { + Address string `json:"address"` + Gateway string `json:"gateway"` + SubnetMask string `json:"subnet_mask"` + Prefix int `json:"prefix"` + Type string `json:"type"` + Public bool `json:"public"` + RDNS string `json:"rdns"` + LinodeID int `json:"linode_id"` + Region string `json:"region"` +} + +// InstanceIPv6Response contains the IPv6 addresses and ranges for an Instance +type InstanceIPv6Response struct { + LinkLocal *InstanceIP `json:"link_local"` + SLAAC *InstanceIP `json:"slaac"` + Global []*IPv6Range `json:"global"` +} + +// IPv6Range represents a range of IPv6 addresses routed to a single Linode in a given Region +type IPv6Range struct { + Range string `json:"range"` + Region string `json:"region"` +} + +// GetInstanceIPAddresses gets the IPAddresses for a Linode instance +func (c *Client) GetInstanceIPAddresses(ctx context.Context, linodeID int) (*InstanceIPAddressResponse, error) { + e, err := c.InstanceIPs.endpointWithID(linodeID) + if err != nil { + return nil, err + } + r, err := coupleAPIErrors(c.R(ctx).SetResult(&InstanceIPAddressResponse{}).Get(e)) + if err != nil { + return nil, err + } + return r.Result().(*InstanceIPAddressResponse), nil +} + +// GetInstanceIPAddress gets the IPAddress for a Linode instance matching a supplied IP address +func (c *Client) GetInstanceIPAddress(ctx context.Context, linodeID int, ipaddress string) (*InstanceIP, error) { + e, err := c.InstanceIPs.endpointWithID(linodeID) + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%s", e, ipaddress) + r, err := coupleAPIErrors(c.R(ctx).SetResult(&InstanceIP{}).Get(e)) + if err != nil { + return nil, err + } + return r.Result().(*InstanceIP), nil +} + +// AddInstanceIPAddress adds a public or private IP to a Linode instance +func (c *Client) AddInstanceIPAddress(ctx context.Context, linodeID int, public bool) (*InstanceIP, error) { + var body string + e, err := c.InstanceIPs.endpointWithID(linodeID) + if err != nil { + return nil, err + } + + req := c.R(ctx).SetResult(&InstanceIP{}) + + instanceipRequest := struct { + Type string `json:"type"` + Public bool `json:"public"` + }{"ipv4", public} + + if bodyData, err := json.Marshal(instanceipRequest); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + r, err := coupleAPIErrors(req. + SetHeader("Content-Type", "application/json"). + SetBody(body). + Post(e)) + + if err != nil { + return nil, err + } + + return r.Result().(*InstanceIP), nil +} diff --git a/vendor/github.com/linode/linodego/instance_snapshots.go b/vendor/github.com/linode/linodego/instance_snapshots.go new file mode 100644 index 000000000..7933507dc --- /dev/null +++ b/vendor/github.com/linode/linodego/instance_snapshots.go @@ -0,0 +1,188 @@ +package linodego + +import ( + "context" + "encoding/json" + "fmt" + "time" +) + +// InstanceBackupsResponse response struct for backup snapshot +type InstanceBackupsResponse struct { + Automatic []*InstanceSnapshot `json:"automatic"` + Snapshot *InstanceBackupSnapshotResponse `json:"snapshot"` +} + +// InstanceBackupSnapshotResponse fields are those representing Instance Backup Snapshots +type InstanceBackupSnapshotResponse struct { + Current *InstanceSnapshot `json:"current"` + InProgress *InstanceSnapshot `json:"in_progress"` +} + +// RestoreInstanceOptions fields are those accepted by InstanceRestore +type RestoreInstanceOptions struct { + LinodeID int `json:"linode_id"` + Overwrite bool `json:"overwrite"` +} + +// InstanceSnapshot represents a linode backup snapshot +type InstanceSnapshot struct { + CreatedStr string `json:"created"` + UpdatedStr string `json:"updated"` + FinishedStr string `json:"finished"` + + ID int `json:"id"` + Label string `json:"label"` + Status InstanceSnapshotStatus `json:"status"` + Type string `json:"type"` + Created *time.Time `json:"-"` + Updated *time.Time `json:"-"` + Finished *time.Time `json:"-"` + Configs []string `json:"configs"` + Disks []*InstanceSnapshotDisk `json:"disks"` +} + +// InstanceSnapshotDisk fields represent the source disk of a Snapshot +type InstanceSnapshotDisk struct { + Label string `json:"label"` + Size int `json:"size"` + Filesystem string `json:"filesystem"` +} + +// InstanceSnapshotStatus constants start with Snapshot and include Linode API Instance Backup Snapshot status values +type InstanceSnapshotStatus string + +// InstanceSnapshotStatus constants reflect the current status of an Instance Snapshot +var ( + SnapshotPaused InstanceSnapshotStatus = "paused" + SnapshotPending InstanceSnapshotStatus = "pending" + SnapshotRunning InstanceSnapshotStatus = "running" + SnapshotNeedsPostProcessing InstanceSnapshotStatus = "needsPostProcessing" + SnapshotSuccessful InstanceSnapshotStatus = "successful" + SnapshotFailed InstanceSnapshotStatus = "failed" + SnapshotUserAborted InstanceSnapshotStatus = "userAborted" +) + +func (l *InstanceSnapshot) fixDates() *InstanceSnapshot { + l.Created, _ = parseDates(l.CreatedStr) + l.Updated, _ = parseDates(l.UpdatedStr) + l.Finished, _ = parseDates(l.FinishedStr) + return l +} + +// GetInstanceSnapshot gets the snapshot with the provided ID +func (c *Client) GetInstanceSnapshot(ctx context.Context, linodeID int, snapshotID int) (*InstanceSnapshot, error) { + e, err := c.InstanceSnapshots.endpointWithID(linodeID) + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, snapshotID) + r, err := coupleAPIErrors(c.R(ctx).SetResult(&InstanceSnapshot{}).Get(e)) + if err != nil { + return nil, err + } + return r.Result().(*InstanceSnapshot).fixDates(), nil +} + +// CreateInstanceSnapshot Creates or Replaces the snapshot Backup of a Linode. If a previous snapshot exists for this Linode, it will be deleted. +func (c *Client) CreateInstanceSnapshot(ctx context.Context, linodeID int, label string) (*InstanceSnapshot, error) { + o, err := json.Marshal(map[string]string{"label": label}) + if err != nil { + return nil, err + } + body := string(o) + e, err := c.InstanceSnapshots.endpointWithID(linodeID) + if err != nil { + return nil, err + } + + r, err := coupleAPIErrors(c.R(ctx). + SetBody(body). + SetResult(&InstanceSnapshot{}). + Post(e)) + + if err != nil { + return nil, err + } + + return r.Result().(*InstanceSnapshot).fixDates(), nil +} + +// GetInstanceBackups gets the Instance's available Backups. +// This is not called ListInstanceBackups because a single object is returned, matching the API response. +func (c *Client) GetInstanceBackups(ctx context.Context, linodeID int) (*InstanceBackupsResponse, error) { + e, err := c.InstanceSnapshots.endpointWithID(linodeID) + if err != nil { + return nil, err + } + r, err := coupleAPIErrors(c.R(ctx). + SetResult(&InstanceBackupsResponse{}). + Get(e)) + if err != nil { + return nil, err + } + return r.Result().(*InstanceBackupsResponse).fixDates(), nil +} + +// EnableInstanceBackups Enables backups for the specified Linode. +func (c *Client) EnableInstanceBackups(ctx context.Context, linodeID int) error { + e, err := c.InstanceSnapshots.endpointWithID(linodeID) + if err != nil { + return err + } + e = fmt.Sprintf("%s/enable", e) + + _, err = coupleAPIErrors(c.R(ctx).Post(e)) + return err +} + +// CancelInstanceBackups Cancels backups for the specified Linode. +func (c *Client) CancelInstanceBackups(ctx context.Context, linodeID int) error { + e, err := c.InstanceSnapshots.endpointWithID(linodeID) + if err != nil { + return err + } + e = fmt.Sprintf("%s/cancel", e) + + _, err = coupleAPIErrors(c.R(ctx).Post(e)) + return err +} + +// RestoreInstanceBackup Restores a Linode's Backup to the specified Linode. +func (c *Client) RestoreInstanceBackup(ctx context.Context, linodeID int, backupID int, opts RestoreInstanceOptions) error { + o, err := json.Marshal(opts) + if err != nil { + return NewError(err) + } + body := string(o) + e, err := c.InstanceSnapshots.endpointWithID(linodeID) + if err != nil { + return err + } + e = fmt.Sprintf("%s/%d/restore", e, backupID) + + _, err = coupleAPIErrors(c.R(ctx).SetBody(body).Post(e)) + + return err + +} + +func (l *InstanceBackupSnapshotResponse) fixDates() *InstanceBackupSnapshotResponse { + if l.Current != nil { + l.Current.fixDates() + } + if l.InProgress != nil { + l.InProgress.fixDates() + } + return l +} + +func (l *InstanceBackupsResponse) fixDates() *InstanceBackupsResponse { + for i := range l.Automatic { + l.Automatic[i].fixDates() + } + if l.Snapshot != nil { + l.Snapshot.fixDates() + } + return l +} diff --git a/vendor/github.com/linode/linodego/instance_volumes.go b/vendor/github.com/linode/linodego/instance_volumes.go new file mode 100644 index 000000000..b6c9a9ae0 --- /dev/null +++ b/vendor/github.com/linode/linodego/instance_volumes.go @@ -0,0 +1,38 @@ +package linodego + +import ( + "context" +) + +// InstanceVolumesPagedResponse represents a paginated InstanceVolume API response +type InstanceVolumesPagedResponse struct { + *PageOptions + Data []Volume `json:"data"` +} + +// endpoint gets the endpoint URL for InstanceVolume +func (InstanceVolumesPagedResponse) endpointWithID(c *Client, id int) string { + endpoint, err := c.InstanceVolumes.endpointWithID(id) + if err != nil { + panic(err) + } + return endpoint +} + +// appendData appends InstanceVolumes when processing paginated InstanceVolume responses +func (resp *InstanceVolumesPagedResponse) appendData(r *InstanceVolumesPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListInstanceVolumes lists InstanceVolumes +func (c *Client) ListInstanceVolumes(ctx context.Context, linodeID int, opts *ListOptions) ([]Volume, error) { + response := InstanceVolumesPagedResponse{} + err := c.listHelperWithID(ctx, &response, linodeID, opts) + for i := range response.Data { + response.Data[i].fixDates() + } + if err != nil { + return nil, err + } + return response.Data, nil +} diff --git a/vendor/github.com/linode/linodego/instances.go b/vendor/github.com/linode/linodego/instances.go new file mode 100644 index 000000000..efb2bf814 --- /dev/null +++ b/vendor/github.com/linode/linodego/instances.go @@ -0,0 +1,438 @@ +package linodego + +import ( + "context" + "encoding/json" + "fmt" + "net" + "time" +) + +/* + * https://developers.linode.com/v4/reference/endpoints/linode/instances + */ + +// InstanceStatus constants start with Instance and include Linode API Instance Status values +type InstanceStatus string + +// InstanceStatus constants reflect the current status of an Instance +const ( + InstanceBooting InstanceStatus = "booting" + InstanceRunning InstanceStatus = "running" + InstanceOffline InstanceStatus = "offline" + InstanceShuttingDown InstanceStatus = "shutting_down" + InstanceRebooting InstanceStatus = "rebooting" + InstanceProvisioning InstanceStatus = "provisioning" + InstanceDeleting InstanceStatus = "deleting" + InstanceMigrating InstanceStatus = "migrating" + InstanceRebuilding InstanceStatus = "rebuilding" + InstanceCloning InstanceStatus = "cloning" + InstanceRestoring InstanceStatus = "restoring" + InstanceResizing InstanceStatus = "resizing" +) + +// Instance represents a linode object +type Instance struct { + CreatedStr string `json:"created"` + UpdatedStr string `json:"updated"` + + ID int `json:"id"` + Created *time.Time `json:"-"` + Updated *time.Time `json:"-"` + Region string `json:"region"` + Alerts *InstanceAlert `json:"alerts"` + Backups *InstanceBackup `json:"backups"` + Image string `json:"image"` + Group string `json:"group"` + IPv4 []*net.IP `json:"ipv4"` + IPv6 string `json:"ipv6"` + Label string `json:"label"` + Type string `json:"type"` + Status InstanceStatus `json:"status"` + Hypervisor string `json:"hypervisor"` + Specs *InstanceSpec `json:"specs"` +} + +// InstanceSpec represents a linode spec +type InstanceSpec struct { + Disk int `json:"disk"` + Memory int `json:"memory"` + VCPUs int `json:"vcpus"` + Transfer int `json:"transfer"` +} + +// InstanceAlert represents a metric alert +type InstanceAlert struct { + CPU int `json:"cpu"` + IO int `json:"io"` + NetworkIn int `json:"network_in"` + NetworkOut int `json:"network_out"` + TransferQuota int `json:"transfer_quota"` +} + +// InstanceBackup represents backup settings for an instance +type InstanceBackup struct { + Enabled bool `json:"enabled"` + Schedule struct { + Day string `json:"day,omitempty"` + Window string `json:"window,omitempty"` + } +} + +// InstanceCreateOptions require only Region and Type +type InstanceCreateOptions struct { + Region string `json:"region"` + Type string `json:"type"` + Label string `json:"label,omitempty"` + Group string `json:"group,omitempty"` + RootPass string `json:"root_pass,omitempty"` + AuthorizedKeys []string `json:"authorized_keys,omitempty"` + AuthorizedUsers []string `json:"authorized_users,omitempty"` + StackScriptID int `json:"stackscript_id,omitempty"` + StackScriptData map[string]string `json:"stackscript_data,omitempty"` + BackupID int `json:"backup_id,omitempty"` + Image string `json:"image,omitempty"` + BackupsEnabled bool `json:"backups_enabled,omitempty"` + PrivateIP bool `json:"private_ip,omitempty"` + + // Creation fields that need to be set explicitly false, "", or 0 use pointers + SwapSize *int `json:"swap_size,omitempty"` + Booted *bool `json:"booted,omitempty"` +} + +// InstanceUpdateOptions is an options struct used when Updating an Instance +type InstanceUpdateOptions struct { + Label string `json:"label,omitempty"` + Group string `json:"group,omitempty"` + Backups *InstanceBackup `json:"backups,omitempty"` + Alerts *InstanceAlert `json:"alerts,omitempty"` + WatchdogEnabled *bool `json:"watchdog_enabled,omitempty"` +} + +// InstanceCloneOptions is an options struct sent when Cloning an Instance +type InstanceCloneOptions struct { + Region string `json:"region,omitempty"` + Type string `json:"type,omitempty"` + + // LinodeID is an optional existing instance to use as the target of the clone + LinodeID int `json:"linode_id,omitempty"` + Label string `json:"label,omitempty"` + Group string `json:"group,omitempty"` + BackupsEnabled bool `json:"backups_enabled"` + Disks []int `json:"disks,omitempty"` + Configs []int `json:"configs,omitempty"` +} + +func (l *Instance) fixDates() *Instance { + l.Created, _ = parseDates(l.CreatedStr) + l.Updated, _ = parseDates(l.UpdatedStr) + return l +} + +// InstancesPagedResponse represents a linode API response for listing +type InstancesPagedResponse struct { + *PageOptions + Data []Instance `json:"data"` +} + +// endpoint gets the endpoint URL for Instance +func (InstancesPagedResponse) endpoint(c *Client) string { + endpoint, err := c.Instances.Endpoint() + if err != nil { + panic(err) + } + return endpoint +} + +// appendData appends Instances when processing paginated Instance responses +func (resp *InstancesPagedResponse) appendData(r *InstancesPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListInstances lists linode instances +func (c *Client) ListInstances(ctx context.Context, opts *ListOptions) ([]Instance, error) { + response := InstancesPagedResponse{} + err := c.listHelper(ctx, &response, opts) + for i := range response.Data { + response.Data[i].fixDates() + } + if err != nil { + return nil, err + } + return response.Data, nil +} + +// GetInstance gets the instance with the provided ID +func (c *Client) GetInstance(ctx context.Context, linodeID int) (*Instance, error) { + e, err := c.Instances.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, linodeID) + r, err := coupleAPIErrors(c.R(ctx). + SetResult(Instance{}). + Get(e)) + if err != nil { + return nil, err + } + return r.Result().(*Instance).fixDates(), nil +} + +// CreateInstance creates a Linode instance +func (c *Client) CreateInstance(ctx context.Context, instance InstanceCreateOptions) (*Instance, error) { + var body string + e, err := c.Instances.Endpoint() + if err != nil { + return nil, err + } + + req := c.R(ctx).SetResult(&Instance{}) + + if bodyData, err := json.Marshal(instance); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + r, err := coupleAPIErrors(req. + SetBody(body). + Post(e)) + + if err != nil { + return nil, err + } + return r.Result().(*Instance).fixDates(), nil +} + +// UpdateInstance creates a Linode instance +func (c *Client) UpdateInstance(ctx context.Context, id int, instance InstanceUpdateOptions) (*Instance, error) { + var body string + e, err := c.Instances.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, id) + + req := c.R(ctx).SetResult(&Instance{}) + + if bodyData, err := json.Marshal(instance); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + r, err := coupleAPIErrors(req. + SetBody(body). + Put(e)) + + if err != nil { + return nil, err + } + return r.Result().(*Instance).fixDates(), nil +} + +// RenameInstance renames an Instance +func (c *Client) RenameInstance(ctx context.Context, linodeID int, label string) (*Instance, error) { + return c.UpdateInstance(ctx, linodeID, InstanceUpdateOptions{Label: label}) +} + +// DeleteInstance deletes a Linode instance +func (c *Client) DeleteInstance(ctx context.Context, id int) error { + e, err := c.Instances.Endpoint() + if err != nil { + return err + } + e = fmt.Sprintf("%s/%d", e, id) + + _, err = coupleAPIErrors(c.R(ctx).Delete(e)) + return err +} + +// BootInstance will boot a Linode instance +// A configID of 0 will cause Linode to choose the last/best config +func (c *Client) BootInstance(ctx context.Context, id int, configID int) error { + bodyStr := "" + + if configID != 0 { + bodyMap := map[string]int{"config_id": configID} + bodyJSON, err := json.Marshal(bodyMap) + if err != nil { + return NewError(err) + } + bodyStr = string(bodyJSON) + } + + e, err := c.Instances.Endpoint() + if err != nil { + return err + } + + e = fmt.Sprintf("%s/%d/boot", e, id) + _, err = coupleAPIErrors(c.R(ctx). + SetBody(bodyStr). + Post(e)) + + return err +} + +// CloneInstance clone an existing Instances Disks and Configuration profiles to another Linode Instance +func (c *Client) CloneInstance(ctx context.Context, id int, options InstanceCloneOptions) (*Instance, error) { + var body string + e, err := c.Instances.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d/clone", e, id) + + req := c.R(ctx).SetResult(&Instance{}) + + if bodyData, err := json.Marshal(options); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + r, err := coupleAPIErrors(req. + SetBody(body). + Post(e)) + + if err != nil { + return nil, err + } + + return r.Result().(*Instance).fixDates(), nil +} + +// RebootInstance reboots a Linode instance +// A configID of 0 will cause Linode to choose the last/best config +func (c *Client) RebootInstance(ctx context.Context, id int, configID int) error { + bodyStr := "{}" + + if configID != 0 { + bodyMap := map[string]int{"config_id": configID} + bodyJSON, err := json.Marshal(bodyMap) + if err != nil { + return NewError(err) + } + bodyStr = string(bodyJSON) + } + + e, err := c.Instances.Endpoint() + if err != nil { + return err + } + + e = fmt.Sprintf("%s/%d/reboot", e, id) + + _, err = coupleAPIErrors(c.R(ctx). + SetBody(bodyStr). + Post(e)) + + return err +} + +// RebuildInstanceOptions is a struct representing the options to send to the rebuild linode endpoint +type RebuildInstanceOptions struct { + Image string `json:"image"` + RootPass string `json:"root_pass"` + AuthorizedKeys []string `json:"authorized_keys"` + AuthorizedUsers []string `json:"authorized_users"` + StackscriptID int `json:"stackscript_id"` + StackscriptData map[string]string `json:"stackscript_data"` + Booted bool `json:"booted"` +} + +// RebuildInstance Deletes all Disks and Configs on this Linode, +// then deploys a new Image to this Linode with the given attributes. +func (c *Client) RebuildInstance(ctx context.Context, id int, opts RebuildInstanceOptions) (*Instance, error) { + o, err := json.Marshal(opts) + if err != nil { + return nil, NewError(err) + } + b := string(o) + e, err := c.Instances.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d/rebuild", e, id) + r, err := coupleAPIErrors(c.R(ctx). + SetBody(b). + SetResult(&Instance{}). + Post(e)) + if err != nil { + return nil, err + } + return r.Result().(*Instance).fixDates(), nil +} + +// RescueInstanceOptions fields are those accepted by RescueInstance +type RescueInstanceOptions struct { + Devices InstanceConfigDeviceMap `json:"devices"` +} + +// RescueInstance reboots an instance into a safe environment for performing many system recovery and disk management tasks. +// Rescue Mode is based on the Finnix recovery distribution, a self-contained and bootable Linux distribution. +// You can also use Rescue Mode for tasks other than disaster recovery, such as formatting disks to use different filesystems, +// copying data between disks, and downloading files from a disk via SSH and SFTP. +func (c *Client) RescueInstance(ctx context.Context, id int, opts RescueInstanceOptions) error { + o, err := json.Marshal(opts) + if err != nil { + return NewError(err) + } + b := string(o) + e, err := c.Instances.Endpoint() + if err != nil { + return err + } + e = fmt.Sprintf("%s/%d/rescue", e, id) + + _, err = coupleAPIErrors(c.R(ctx). + SetBody(b). + Post(e)) + + return err +} + +// ResizeInstance resizes an instance to new Linode type +func (c *Client) ResizeInstance(ctx context.Context, id int, linodeType string) error { + body := fmt.Sprintf("{\"type\":\"%s\"}", linodeType) + + e, err := c.Instances.Endpoint() + if err != nil { + return err + } + e = fmt.Sprintf("%s/%d/resize", e, id) + + _, err = coupleAPIErrors(c.R(ctx). + SetBody(body). + Post(e)) + + return err +} + +// ShutdownInstance - Shutdown an instance +func (c *Client) ShutdownInstance(ctx context.Context, id int) error { + return c.simpleInstanceAction(ctx, "shutdown", id) +} + +// MutateInstance Upgrades a Linode to its next generation. +func (c *Client) MutateInstance(ctx context.Context, id int) error { + return c.simpleInstanceAction(ctx, "mutate", id) +} + +// MigrateInstance - Migrate an instance +func (c *Client) MigrateInstance(ctx context.Context, id int) error { + return c.simpleInstanceAction(ctx, "migrate", id) +} + +// simpleInstanceAction is a helper for Instance actions that take no parameters +// and return empty responses `{}` unless they return a standard error +func (c *Client) simpleInstanceAction(ctx context.Context, action string, id int) error { + e, err := c.Instances.Endpoint() + if err != nil { + return err + } + e = fmt.Sprintf("%s/%d/%s", e, id, action) + _, err = coupleAPIErrors(c.R(ctx).Post(e)) + return err +} diff --git a/vendor/github.com/linode/linodego/kernels.go b/vendor/github.com/linode/linodego/kernels.go new file mode 100644 index 000000000..233a38db5 --- /dev/null +++ b/vendor/github.com/linode/linodego/kernels.go @@ -0,0 +1,61 @@ +package linodego + +import ( + "context" + "fmt" +) + +// LinodeKernel represents a Linode Instance kernel object +type LinodeKernel struct { + ID string `json:"id"` + Label string `json:"label"` + Version string `json:"version"` + Architecture string `json:"architecture"` + KVM bool `json:"kvm"` + XEN bool `json:"xen"` + PVOPS bool `json:"pvops"` +} + +// LinodeKernelsPagedResponse represents a Linode kernels API response for listing +type LinodeKernelsPagedResponse struct { + *PageOptions + Data []LinodeKernel `json:"data"` +} + +// ListKernels lists linode kernels +func (c *Client) ListKernels(ctx context.Context, opts *ListOptions) ([]LinodeKernel, error) { + response := LinodeKernelsPagedResponse{} + err := c.listHelper(ctx, &response, opts) + if err != nil { + return nil, err + } + return response.Data, nil +} + +func (LinodeKernelsPagedResponse) endpoint(c *Client) string { + endpoint, err := c.Kernels.Endpoint() + if err != nil { + panic(err) + } + return endpoint +} + +func (resp *LinodeKernelsPagedResponse) appendData(r *LinodeKernelsPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// GetKernel gets the kernel with the provided ID +func (c *Client) GetKernel(ctx context.Context, kernelID string) (*LinodeKernel, error) { + e, err := c.Kernels.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%s", e, kernelID) + r, err := c.R(ctx). + SetResult(&LinodeKernel{}). + Get(e) + if err != nil { + return nil, err + } + return r.Result().(*LinodeKernel), nil +} diff --git a/vendor/github.com/linode/linodego/longview.go b/vendor/github.com/linode/linodego/longview.go new file mode 100644 index 000000000..524af6776 --- /dev/null +++ b/vendor/github.com/linode/linodego/longview.go @@ -0,0 +1,67 @@ +package linodego + +import ( + "context" + "fmt" +) + +// LongviewClient represents a LongviewClient object +type LongviewClient struct { + ID int `json:"id"` + // UpdatedStr string `json:"updated"` + // Updated *time.Time `json:"-"` +} + +// LongviewClientsPagedResponse represents a paginated LongviewClient API response +type LongviewClientsPagedResponse struct { + *PageOptions + Data []LongviewClient `json:"data"` +} + +// endpoint gets the endpoint URL for LongviewClient +func (LongviewClientsPagedResponse) endpoint(c *Client) string { + endpoint, err := c.LongviewClients.Endpoint() + if err != nil { + panic(err) + } + return endpoint +} + +// appendData appends LongviewClients when processing paginated LongviewClient responses +func (resp *LongviewClientsPagedResponse) appendData(r *LongviewClientsPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListLongviewClients lists LongviewClients +func (c *Client) ListLongviewClients(ctx context.Context, opts *ListOptions) ([]LongviewClient, error) { + response := LongviewClientsPagedResponse{} + err := c.listHelper(ctx, &response, opts) + for i := range response.Data { + response.Data[i].fixDates() + } + if err != nil { + return nil, err + } + return response.Data, nil +} + +// fixDates converts JSON timestamps to Go time.Time values +func (v *LongviewClient) fixDates() *LongviewClient { + // v.Created, _ = parseDates(v.CreatedStr) + // v.Updated, _ = parseDates(v.UpdatedStr) + return v +} + +// GetLongviewClient gets the template with the provided ID +func (c *Client) GetLongviewClient(ctx context.Context, id string) (*LongviewClient, error) { + e, err := c.LongviewClients.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%s", e, id) + r, err := c.R(ctx).SetResult(&LongviewClient{}).Get(e) + if err != nil { + return nil, err + } + return r.Result().(*LongviewClient).fixDates(), nil +} diff --git a/vendor/github.com/linode/linodego/longview_subscriptions.go b/vendor/github.com/linode/linodego/longview_subscriptions.go new file mode 100644 index 000000000..253316599 --- /dev/null +++ b/vendor/github.com/linode/linodego/longview_subscriptions.go @@ -0,0 +1,70 @@ +package linodego + +import ( + "context" + "fmt" +) + +// LongviewSubscription represents a LongviewSubscription object +type LongviewSubscription struct { + ID string `json:"id"` + Label string `json:"label"` + ClientsIncluded int `json:"clients_included"` + Price *LinodePrice `json:"price"` + // UpdatedStr string `json:"updated"` + // Updated *time.Time `json:"-"` +} + +// LongviewSubscriptionsPagedResponse represents a paginated LongviewSubscription API response +type LongviewSubscriptionsPagedResponse struct { + *PageOptions + Data []LongviewSubscription `json:"data"` +} + +// endpoint gets the endpoint URL for LongviewSubscription +func (LongviewSubscriptionsPagedResponse) endpoint(c *Client) string { + endpoint, err := c.LongviewSubscriptions.Endpoint() + if err != nil { + panic(err) + } + return endpoint +} + +// appendData appends LongviewSubscriptions when processing paginated LongviewSubscription responses +func (resp *LongviewSubscriptionsPagedResponse) appendData(r *LongviewSubscriptionsPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListLongviewSubscriptions lists LongviewSubscriptions +func (c *Client) ListLongviewSubscriptions(ctx context.Context, opts *ListOptions) ([]LongviewSubscription, error) { + response := LongviewSubscriptionsPagedResponse{} + err := c.listHelper(ctx, &response, opts) + for i := range response.Data { + response.Data[i].fixDates() + } + if err != nil { + return nil, err + } + return response.Data, nil +} + +// fixDates converts JSON timestamps to Go time.Time values +func (v *LongviewSubscription) fixDates() *LongviewSubscription { + // v.Created, _ = parseDates(v.CreatedStr) + // v.Updated, _ = parseDates(v.UpdatedStr) + return v +} + +// GetLongviewSubscription gets the template with the provided ID +func (c *Client) GetLongviewSubscription(ctx context.Context, id string) (*LongviewSubscription, error) { + e, err := c.LongviewSubscriptions.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%s", e, id) + r, err := c.R(ctx).SetResult(&LongviewSubscription{}).Get(e) + if err != nil { + return nil, err + } + return r.Result().(*LongviewSubscription).fixDates(), nil +} diff --git a/vendor/github.com/linode/linodego/managed.go b/vendor/github.com/linode/linodego/managed.go new file mode 100644 index 000000000..152361292 --- /dev/null +++ b/vendor/github.com/linode/linodego/managed.go @@ -0,0 +1 @@ +package linodego diff --git a/vendor/github.com/linode/linodego/network_ips.go b/vendor/github.com/linode/linodego/network_ips.go new file mode 100644 index 000000000..29b6f6b8e --- /dev/null +++ b/vendor/github.com/linode/linodego/network_ips.go @@ -0,0 +1,50 @@ +package linodego + +import ( + "context" + "fmt" +) + +// IPAddressesPagedResponse represents a paginated IPAddress API response +type IPAddressesPagedResponse struct { + *PageOptions + Data []InstanceIP `json:"data"` +} + +// endpoint gets the endpoint URL for IPAddress +func (IPAddressesPagedResponse) endpoint(c *Client) string { + endpoint, err := c.IPAddresses.Endpoint() + if err != nil { + panic(err) + } + return endpoint +} + +// appendData appends IPAddresses when processing paginated InstanceIPAddress responses +func (resp *IPAddressesPagedResponse) appendData(r *IPAddressesPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListIPAddresses lists IPAddresses +func (c *Client) ListIPAddresses(ctx context.Context, opts *ListOptions) ([]InstanceIP, error) { + response := IPAddressesPagedResponse{} + err := c.listHelper(ctx, &response, opts) + if err != nil { + return nil, err + } + return response.Data, nil +} + +// GetIPAddress gets the template with the provided ID +func (c *Client) GetIPAddress(ctx context.Context, id string) (*InstanceIP, error) { + e, err := c.IPAddresses.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%s", e, id) + r, err := coupleAPIErrors(c.R(ctx).SetResult(&InstanceIP{}).Get(e)) + if err != nil { + return nil, err + } + return r.Result().(*InstanceIP), nil +} diff --git a/vendor/github.com/linode/linodego/network_pools.go b/vendor/github.com/linode/linodego/network_pools.go new file mode 100644 index 000000000..03085923c --- /dev/null +++ b/vendor/github.com/linode/linodego/network_pools.go @@ -0,0 +1,50 @@ +package linodego + +import ( + "context" + "fmt" +) + +// IPv6PoolsPagedResponse represents a paginated IPv6Pool API response +type IPv6PoolsPagedResponse struct { + *PageOptions + Data []IPv6Range `json:"data"` +} + +// endpoint gets the endpoint URL for IPv6Pool +func (IPv6PoolsPagedResponse) endpoint(c *Client) string { + endpoint, err := c.IPv6Pools.Endpoint() + if err != nil { + panic(err) + } + return endpoint +} + +// appendData appends IPv6Pools when processing paginated IPv6Pool responses +func (resp *IPv6PoolsPagedResponse) appendData(r *IPv6PoolsPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListIPv6Pools lists IPv6Pools +func (c *Client) ListIPv6Pools(ctx context.Context, opts *ListOptions) ([]IPv6Range, error) { + response := IPv6PoolsPagedResponse{} + err := c.listHelper(ctx, &response, opts) + if err != nil { + return nil, err + } + return response.Data, nil +} + +// GetIPv6Pool gets the template with the provided ID +func (c *Client) GetIPv6Pool(ctx context.Context, id string) (*IPv6Range, error) { + e, err := c.IPv6Pools.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%s", e, id) + r, err := coupleAPIErrors(c.R(ctx).SetResult(&IPv6Range{}).Get(e)) + if err != nil { + return nil, err + } + return r.Result().(*IPv6Range), nil +} diff --git a/vendor/github.com/linode/linodego/network_ranges.go b/vendor/github.com/linode/linodego/network_ranges.go new file mode 100644 index 000000000..0c0ba15a3 --- /dev/null +++ b/vendor/github.com/linode/linodego/network_ranges.go @@ -0,0 +1,50 @@ +package linodego + +import ( + "context" + "fmt" +) + +// IPv6RangesPagedResponse represents a paginated IPv6Range API response +type IPv6RangesPagedResponse struct { + *PageOptions + Data []IPv6Range `json:"data"` +} + +// endpoint gets the endpoint URL for IPv6Range +func (IPv6RangesPagedResponse) endpoint(c *Client) string { + endpoint, err := c.IPv6Ranges.Endpoint() + if err != nil { + panic(err) + } + return endpoint +} + +// appendData appends IPv6Ranges when processing paginated IPv6Range responses +func (resp *IPv6RangesPagedResponse) appendData(r *IPv6RangesPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListIPv6Ranges lists IPv6Ranges +func (c *Client) ListIPv6Ranges(ctx context.Context, opts *ListOptions) ([]IPv6Range, error) { + response := IPv6RangesPagedResponse{} + err := c.listHelper(ctx, &response, opts) + if err != nil { + return nil, err + } + return response.Data, nil +} + +// GetIPv6Range gets the template with the provided ID +func (c *Client) GetIPv6Range(ctx context.Context, id string) (*IPv6Range, error) { + e, err := c.IPv6Ranges.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%s", e, id) + r, err := coupleAPIErrors(c.R(ctx).SetResult(&IPv6Range{}).Get(e)) + if err != nil { + return nil, err + } + return r.Result().(*IPv6Range), nil +} diff --git a/vendor/github.com/linode/linodego/nodebalancer.go b/vendor/github.com/linode/linodego/nodebalancer.go new file mode 100644 index 000000000..af4dacc47 --- /dev/null +++ b/vendor/github.com/linode/linodego/nodebalancer.go @@ -0,0 +1,192 @@ +package linodego + +import ( + "context" + "encoding/json" + "fmt" + "time" +) + +// NodeBalancer represents a NodeBalancer object +type NodeBalancer struct { + CreatedStr string `json:"created"` + UpdatedStr string `json:"updated"` + // This NodeBalancer's unique ID. + ID int `json:"id"` + // This NodeBalancer's label. These must be unique on your Account. + Label *string `json:"label"` + // The Region where this NodeBalancer is located. NodeBalancers only support backends in the same Region. + Region string `json:"region"` + // This NodeBalancer's hostname, ending with .nodebalancer.linode.com + Hostname *string `json:"hostname"` + // This NodeBalancer's public IPv4 address. + IPv4 *string `json:"ipv4"` + // This NodeBalancer's public IPv6 address. + IPv6 *string `json:"ipv6"` + // Throttle connections per second (0-20). Set to 0 (zero) to disable throttling. + ClientConnThrottle int `json:"client_conn_throttle"` + // Information about the amount of transfer this NodeBalancer has had so far this month. + Transfer NodeBalancerTransfer `json:"transfer"` + + Created *time.Time `json:"-"` + Updated *time.Time `json:"-"` +} + +// NodeBalancerTransfer contains information about the amount of transfer a NodeBalancer has had in the current month +type NodeBalancerTransfer struct { + // The total transfer, in MB, used by this NodeBalancer this month. + Total *float64 `json:"total"` + // The total inbound transfer, in MB, used for this NodeBalancer this month. + Out *float64 `json:"out"` + // The total outbound transfer, in MB, used for this NodeBalancer this month. + In *float64 `json:"in"` +} + +// NodeBalancerCreateOptions are the options permitted for CreateNodeBalancer +type NodeBalancerCreateOptions struct { + Label *string `json:"label,omitempty"` + Region string `json:"region,omitempty"` + ClientConnThrottle *int `json:"client_conn_throttle,omitempty"` + Configs []*NodeBalancerConfigCreateOptions `json:"configs,omitempty"` +} + +// NodeBalancerUpdateOptions are the options permitted for UpdateNodeBalancer +type NodeBalancerUpdateOptions struct { + Label *string `json:"label,omitempty"` + ClientConnThrottle *int `json:"client_conn_throttle,omitempty"` +} + +// GetCreateOptions converts a NodeBalancer to NodeBalancerCreateOptions for use in CreateNodeBalancer +func (i NodeBalancer) GetCreateOptions() NodeBalancerCreateOptions { + return NodeBalancerCreateOptions{ + Label: i.Label, + Region: i.Region, + ClientConnThrottle: &i.ClientConnThrottle, + } +} + +// GetUpdateOptions converts a NodeBalancer to NodeBalancerUpdateOptions for use in UpdateNodeBalancer +func (i NodeBalancer) GetUpdateOptions() NodeBalancerUpdateOptions { + return NodeBalancerUpdateOptions{ + Label: i.Label, + ClientConnThrottle: &i.ClientConnThrottle, + } +} + +// NodeBalancersPagedResponse represents a paginated NodeBalancer API response +type NodeBalancersPagedResponse struct { + *PageOptions + Data []NodeBalancer `json:"data"` +} + +func (NodeBalancersPagedResponse) endpoint(c *Client) string { + endpoint, err := c.NodeBalancers.Endpoint() + if err != nil { + panic(err) + } + return endpoint +} + +func (resp *NodeBalancersPagedResponse) appendData(r *NodeBalancersPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListNodeBalancers lists NodeBalancers +func (c *Client) ListNodeBalancers(ctx context.Context, opts *ListOptions) ([]NodeBalancer, error) { + response := NodeBalancersPagedResponse{} + err := c.listHelper(ctx, &response, opts) + if err != nil { + return nil, err + } + return response.Data, nil +} + +// fixDates converts JSON timestamps to Go time.Time values +func (i *NodeBalancer) fixDates() *NodeBalancer { + i.Created, _ = parseDates(i.CreatedStr) + i.Updated, _ = parseDates(i.UpdatedStr) + return i +} + +// GetNodeBalancer gets the NodeBalancer with the provided ID +func (c *Client) GetNodeBalancer(ctx context.Context, id int) (*NodeBalancer, error) { + e, err := c.NodeBalancers.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, id) + r, err := coupleAPIErrors(c.R(ctx). + SetResult(&NodeBalancer{}). + Get(e)) + if err != nil { + return nil, err + } + return r.Result().(*NodeBalancer).fixDates(), nil +} + +// CreateNodeBalancer creates a NodeBalancer +func (c *Client) CreateNodeBalancer(ctx context.Context, nodebalancer NodeBalancerCreateOptions) (*NodeBalancer, error) { + var body string + e, err := c.NodeBalancers.Endpoint() + if err != nil { + return nil, err + } + + req := c.R(ctx).SetResult(&NodeBalancer{}) + + if bodyData, err := json.Marshal(nodebalancer); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + r, err := coupleAPIErrors(req. + SetHeader("Content-Type", "application/json"). + SetBody(body). + Post(e)) + + if err != nil { + return nil, err + } + return r.Result().(*NodeBalancer).fixDates(), nil +} + +// UpdateNodeBalancer updates the NodeBalancer with the specified id +func (c *Client) UpdateNodeBalancer(ctx context.Context, id int, updateOpts NodeBalancerUpdateOptions) (*NodeBalancer, error) { + var body string + e, err := c.NodeBalancers.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, id) + + req := c.R(ctx).SetResult(&NodeBalancer{}) + + if bodyData, err := json.Marshal(updateOpts); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + r, err := coupleAPIErrors(req. + SetBody(body). + Put(e)) + + if err != nil { + return nil, err + } + return r.Result().(*NodeBalancer).fixDates(), nil +} + +// DeleteNodeBalancer deletes the NodeBalancer with the specified id +func (c *Client) DeleteNodeBalancer(ctx context.Context, id int) error { + e, err := c.NodeBalancers.Endpoint() + if err != nil { + return err + } + e = fmt.Sprintf("%s/%d", e, id) + + _, err = coupleAPIErrors(c.R(ctx).Delete(e)) + + return err +} diff --git a/vendor/github.com/linode/linodego/nodebalancer_config_nodes.go b/vendor/github.com/linode/linodego/nodebalancer_config_nodes.go new file mode 100644 index 000000000..e1ca55cb5 --- /dev/null +++ b/vendor/github.com/linode/linodego/nodebalancer_config_nodes.go @@ -0,0 +1,186 @@ +package linodego + +import ( + "context" + "encoding/json" + "fmt" +) + +// NodeBalancerNode objects represent a backend that can accept traffic for a NodeBalancer Config +type NodeBalancerNode struct { + ID int `json:"id"` + Address string `json:"address"` + Label string `json:"label"` + Status string `json:"status"` + Weight int `json:"weight"` + Mode NodeMode `json:"mode"` + ConfigID int `json:"config_id"` + NodeBalancerID int `json:"nodebalancer_id"` +} + +// NodeMode is the mode a NodeBalancer should use when sending traffic to a NodeBalancer Node +type NodeMode string + +var ( + // ModeAccept is the NodeMode indicating a NodeBalancer Node is accepting traffic + ModeAccept NodeMode = "accept" + + // ModeReject is the NodeMode indicating a NodeBalancer Node is not receiving traffic + ModeReject NodeMode = "reject" + + // ModeDrain is the NodeMode indicating a NodeBalancer Node is not receiving new traffic, but may continue receiving traffic from pinned connections + ModeDrain NodeMode = "drain" +) + +// NodeBalancerNodeCreateOptions fields are those accepted by CreateNodeBalancerNode +type NodeBalancerNodeCreateOptions struct { + Address string `json:"address"` + Label string `json:"label"` + Weight int `json:"weight,omitempty"` + Mode NodeMode `json:"mode,omitempty"` +} + +// NodeBalancerNodeUpdateOptions fields are those accepted by UpdateNodeBalancerNode +type NodeBalancerNodeUpdateOptions struct { + Address string `json:"address,omitempty"` + Label string `json:"label,omitempty"` + Weight int `json:"weight,omitempty"` + Mode NodeMode `json:"mode,omitempty"` +} + +// GetCreateOptions converts a NodeBalancerNode to NodeBalancerNodeCreateOptions for use in CreateNodeBalancerNode +func (i NodeBalancerNode) GetCreateOptions() NodeBalancerNodeCreateOptions { + return NodeBalancerNodeCreateOptions{ + Address: i.Address, + Label: i.Label, + Weight: i.Weight, + Mode: i.Mode, + } +} + +// GetUpdateOptions converts a NodeBalancerNode to NodeBalancerNodeUpdateOptions for use in UpdateNodeBalancerNode +func (i NodeBalancerNode) GetUpdateOptions() NodeBalancerNodeUpdateOptions { + return NodeBalancerNodeUpdateOptions{ + Address: i.Address, + Label: i.Label, + Weight: i.Weight, + Mode: i.Mode, + } +} + +// NodeBalancerNodesPagedResponse represents a paginated NodeBalancerNode API response +type NodeBalancerNodesPagedResponse struct { + *PageOptions + Data []NodeBalancerNode `json:"data"` +} + +// endpoint gets the endpoint URL for NodeBalancerNode +func (NodeBalancerNodesPagedResponse) endpointWithTwoIDs(c *Client, nodebalancerID int, configID int) string { + endpoint, err := c.NodeBalancerNodes.endpointWithID(nodebalancerID, configID) + if err != nil { + panic(err) + } + return endpoint +} + +// appendData appends NodeBalancerNodes when processing paginated NodeBalancerNode responses +func (resp *NodeBalancerNodesPagedResponse) appendData(r *NodeBalancerNodesPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListNodeBalancerNodes lists NodeBalancerNodes +func (c *Client) ListNodeBalancerNodes(ctx context.Context, nodebalancerID int, configID int, opts *ListOptions) ([]NodeBalancerNode, error) { + response := NodeBalancerNodesPagedResponse{} + err := c.listHelperWithTwoIDs(ctx, &response, nodebalancerID, configID, opts) + for i := range response.Data { + response.Data[i].fixDates() + } + if err != nil { + return nil, err + } + return response.Data, nil +} + +// fixDates converts JSON timestamps to Go time.Time values +func (i *NodeBalancerNode) fixDates() *NodeBalancerNode { + return i +} + +// GetNodeBalancerNode gets the template with the provided ID +func (c *Client) GetNodeBalancerNode(ctx context.Context, nodebalancerID int, configID int, nodeID int) (*NodeBalancerNode, error) { + e, err := c.NodeBalancerNodes.endpointWithID(nodebalancerID, configID) + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, nodeID) + r, err := coupleAPIErrors(c.R(ctx).SetResult(&NodeBalancerNode{}).Get(e)) + if err != nil { + return nil, err + } + return r.Result().(*NodeBalancerNode).fixDates(), nil +} + +// CreateNodeBalancerNode creates a NodeBalancerNode +func (c *Client) CreateNodeBalancerNode(ctx context.Context, nodebalancerID int, configID int, createOpts NodeBalancerNodeCreateOptions) (*NodeBalancerNode, error) { + var body string + e, err := c.NodeBalancerNodes.endpointWithID(nodebalancerID, configID) + if err != nil { + return nil, err + } + + req := c.R(ctx).SetResult(&NodeBalancerNode{}) + + if bodyData, err := json.Marshal(createOpts); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + r, err := coupleAPIErrors(req. + SetBody(body). + Post(e)) + + if err != nil { + return nil, err + } + return r.Result().(*NodeBalancerNode).fixDates(), nil +} + +// UpdateNodeBalancerNode updates the NodeBalancerNode with the specified id +func (c *Client) UpdateNodeBalancerNode(ctx context.Context, nodebalancerID int, configID int, nodeID int, updateOpts NodeBalancerNodeUpdateOptions) (*NodeBalancerNode, error) { + var body string + e, err := c.NodeBalancerNodes.endpointWithID(nodebalancerID, configID) + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, nodeID) + + req := c.R(ctx).SetResult(&NodeBalancerNode{}) + + if bodyData, err := json.Marshal(updateOpts); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + r, err := coupleAPIErrors(req. + SetBody(body). + Put(e)) + + if err != nil { + return nil, err + } + return r.Result().(*NodeBalancerNode).fixDates(), nil +} + +// DeleteNodeBalancerNode deletes the NodeBalancerNode with the specified id +func (c *Client) DeleteNodeBalancerNode(ctx context.Context, nodebalancerID int, configID int, nodeID int) error { + e, err := c.NodeBalancerNodes.endpointWithID(nodebalancerID, configID) + if err != nil { + return err + } + e = fmt.Sprintf("%s/%d", e, nodeID) + + _, err = coupleAPIErrors(c.R(ctx).Delete(e)) + return err +} diff --git a/vendor/github.com/linode/linodego/nodebalancer_configs.go b/vendor/github.com/linode/linodego/nodebalancer_configs.go new file mode 100644 index 000000000..574ec97d2 --- /dev/null +++ b/vendor/github.com/linode/linodego/nodebalancer_configs.go @@ -0,0 +1,334 @@ +package linodego + +import ( + "context" + "encoding/json" + "fmt" +) + +// NodeBalancerConfig objects allow a NodeBalancer to accept traffic on a new port +type NodeBalancerConfig struct { + ID int `json:"id"` + Port int `json:"port"` + Protocol ConfigProtocol `json:"protocol"` + Algorithm ConfigAlgorithm `json:"algorithm"` + Stickiness ConfigStickiness `json:"stickiness"` + Check ConfigCheck `json:"check"` + CheckInterval int `json:"check_interval"` + CheckAttempts int `json:"check_attempts"` + CheckPath string `json:"check_path"` + CheckBody string `json:"check_body"` + CheckPassive bool `json:"check_passive"` + CheckTimeout int `json:"check_timeout"` + CipherSuite ConfigCipher `json:"cipher_suite"` + NodeBalancerID int `json:"nodebalancer_id"` + SSLCommonName string `json:"ssl_commonname"` + SSLFingerprint string `json:"ssl_fingerprint"` + SSLCert string `json:"ssl_cert"` + SSLKey string `json:"ssl_key"` + NodesStatus *NodeBalancerNodeStatus `json:"nodes_status"` +} + +// ConfigAlgorithm constants start with Algorithm and include Linode API NodeBalancer Config Algorithms +type ConfigAlgorithm string + +// ConfigAlgorithm constants reflect the NodeBalancer Config Algorithm +const ( + AlgorithmRoundRobin ConfigAlgorithm = "roundrobin" + AlgorithmLeastConn ConfigAlgorithm = "leastconn" + AlgorithmSource ConfigAlgorithm = "source" +) + +// ConfigStickiness constants start with Stickiness and include Linode API NodeBalancer Config Stickiness +type ConfigStickiness string + +// ConfigStickiness constants reflect the node stickiness method for a NodeBalancer Config +const ( + StickinessNone ConfigStickiness = "none" + StickinessTable ConfigStickiness = "table" + StickinessHTTPCookie ConfigStickiness = "http_cookie" +) + +// ConfigCheck constants start with Check and include Linode API NodeBalancer Config Check methods +type ConfigCheck string + +// ConfigCheck constants reflect the node health status checking method for a NodeBalancer Config +const ( + CheckNone ConfigCheck = "none" + CheckConnection ConfigCheck = "connection" + CheckHTTP ConfigCheck = "http" + CheckHTTPBody ConfigCheck = "http_body" +) + +// ConfigProtocol constants start with Protocol and include Linode API Nodebalancer Config protocols +type ConfigProtocol string + +// ConfigProtocol constants reflect the protocol used by a NodeBalancer Config +const ( + ProtocolHTTP ConfigProtocol = "http" + ProtocolHTTPS ConfigProtocol = "https" + ProtocolTCP ConfigProtocol = "tcp" +) + +// ConfigCipher constants start with Cipher and include Linode API NodeBalancer Config Cipher values +type ConfigCipher string + +// ConfigCipher constants reflect the preferred cipher set for a NodeBalancer Config +const ( + CipherRecommended ConfigCipher = "recommended" + CipherLegacy ConfigCipher = "legacy" +) + +// NodeBalancerNodeStatus represents the total number of nodes whose status is Up or Down +type NodeBalancerNodeStatus struct { + Up int `json:"up"` + Down int `json:"down"` +} + +// NodeBalancerConfigCreateOptions are permitted by CreateNodeBalancerConfig +type NodeBalancerConfigCreateOptions struct { + Port int `json:"port"` + Protocol ConfigProtocol `json:"protocol,omitempty"` + Algorithm ConfigAlgorithm `json:"algorithm,omitempty"` + Stickiness ConfigStickiness `json:"stickiness,omitempty"` + Check ConfigCheck `json:"check,omitempty"` + CheckInterval int `json:"check_interval,omitempty"` + CheckAttempts int `json:"check_attempts,omitempty"` + CheckPath string `json:"check_path,omitempty"` + CheckBody string `json:"check_body,omitempty"` + CheckPassive *bool `json:"check_passive,omitempty"` + CheckTimeout int `json:"check_timeout,omitempty"` + CipherSuite ConfigCipher `json:"cipher_suite,omitempty"` + SSLCert string `json:"ssl_cert,omitempty"` + SSLKey string `json:"ssl_key,omitempty"` + Nodes []NodeBalancerNodeCreateOptions `json:"nodes,omitempty"` +} + +// NodeBalancerConfigRebuildOptions used by RebuildNodeBalancerConfig +type NodeBalancerConfigRebuildOptions struct { + Port int `json:"port"` + Protocol ConfigProtocol `json:"protocol,omitempty"` + Algorithm ConfigAlgorithm `json:"algorithm,omitempty"` + Stickiness ConfigStickiness `json:"stickiness,omitempty"` + Check ConfigCheck `json:"check,omitempty"` + CheckInterval int `json:"check_interval,omitempty"` + CheckAttempts int `json:"check_attempts,omitempty"` + CheckPath string `json:"check_path,omitempty"` + CheckBody string `json:"check_body,omitempty"` + CheckPassive *bool `json:"check_passive,omitempty"` + CheckTimeout int `json:"check_timeout,omitempty"` + CipherSuite ConfigCipher `json:"cipher_suite,omitempty"` + SSLCert string `json:"ssl_cert,omitempty"` + SSLKey string `json:"ssl_key,omitempty"` + Nodes []NodeBalancerNodeCreateOptions `json:"nodes"` +} + +// NodeBalancerConfigUpdateOptions are permitted by UpdateNodeBalancerConfig +type NodeBalancerConfigUpdateOptions NodeBalancerConfigCreateOptions + +// GetCreateOptions converts a NodeBalancerConfig to NodeBalancerConfigCreateOptions for use in CreateNodeBalancerConfig +func (i NodeBalancerConfig) GetCreateOptions() NodeBalancerConfigCreateOptions { + return NodeBalancerConfigCreateOptions{ + Port: i.Port, + Protocol: i.Protocol, + Algorithm: i.Algorithm, + Stickiness: i.Stickiness, + Check: i.Check, + CheckInterval: i.CheckInterval, + CheckAttempts: i.CheckAttempts, + CheckTimeout: i.CheckTimeout, + CheckPath: i.CheckPath, + CheckBody: i.CheckBody, + CheckPassive: &i.CheckPassive, + CipherSuite: i.CipherSuite, + SSLCert: i.SSLCert, + SSLKey: i.SSLKey, + } +} + +// GetUpdateOptions converts a NodeBalancerConfig to NodeBalancerConfigUpdateOptions for use in UpdateNodeBalancerConfig +func (i NodeBalancerConfig) GetUpdateOptions() NodeBalancerConfigUpdateOptions { + return NodeBalancerConfigUpdateOptions{ + Port: i.Port, + Protocol: i.Protocol, + Algorithm: i.Algorithm, + Stickiness: i.Stickiness, + Check: i.Check, + CheckInterval: i.CheckInterval, + CheckAttempts: i.CheckAttempts, + CheckPath: i.CheckPath, + CheckBody: i.CheckBody, + CheckPassive: &i.CheckPassive, + CheckTimeout: i.CheckTimeout, + CipherSuite: i.CipherSuite, + SSLCert: i.SSLCert, + SSLKey: i.SSLKey, + } +} + +// GetRebuildOptions converts a NodeBalancerConfig to NodeBalancerConfigRebuildOptions for use in RebuildNodeBalancerConfig +func (i NodeBalancerConfig) GetRebuildOptions() NodeBalancerConfigRebuildOptions { + return NodeBalancerConfigRebuildOptions{ + Port: i.Port, + Protocol: i.Protocol, + Algorithm: i.Algorithm, + Stickiness: i.Stickiness, + Check: i.Check, + CheckInterval: i.CheckInterval, + CheckAttempts: i.CheckAttempts, + CheckTimeout: i.CheckTimeout, + CheckPath: i.CheckPath, + CheckBody: i.CheckBody, + CheckPassive: &i.CheckPassive, + CipherSuite: i.CipherSuite, + SSLCert: i.SSLCert, + SSLKey: i.SSLKey, + Nodes: make([]NodeBalancerNodeCreateOptions, 0), + } +} + +// NodeBalancerConfigsPagedResponse represents a paginated NodeBalancerConfig API response +type NodeBalancerConfigsPagedResponse struct { + *PageOptions + Data []NodeBalancerConfig `json:"data"` +} + +// endpointWithID gets the endpoint URL for NodeBalancerConfig +func (NodeBalancerConfigsPagedResponse) endpointWithID(c *Client, id int) string { + endpoint, err := c.NodeBalancerConfigs.endpointWithID(id) + if err != nil { + panic(err) + } + return endpoint +} + +// appendData appends NodeBalancerConfigs when processing paginated NodeBalancerConfig responses +func (resp *NodeBalancerConfigsPagedResponse) appendData(r *NodeBalancerConfigsPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListNodeBalancerConfigs lists NodeBalancerConfigs +func (c *Client) ListNodeBalancerConfigs(ctx context.Context, nodebalancerID int, opts *ListOptions) ([]NodeBalancerConfig, error) { + response := NodeBalancerConfigsPagedResponse{} + err := c.listHelperWithID(ctx, &response, nodebalancerID, opts) + for i := range response.Data { + response.Data[i].fixDates() + } + if err != nil { + return nil, err + } + return response.Data, nil +} + +// fixDates converts JSON timestamps to Go time.Time values +func (i *NodeBalancerConfig) fixDates() *NodeBalancerConfig { + return i +} + +// GetNodeBalancerConfig gets the template with the provided ID +func (c *Client) GetNodeBalancerConfig(ctx context.Context, nodebalancerID int, configID int) (*NodeBalancerConfig, error) { + e, err := c.NodeBalancerConfigs.endpointWithID(nodebalancerID) + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, configID) + r, err := coupleAPIErrors(c.R(ctx).SetResult(&NodeBalancerConfig{}).Get(e)) + if err != nil { + return nil, err + } + return r.Result().(*NodeBalancerConfig).fixDates(), nil +} + +// CreateNodeBalancerConfig creates a NodeBalancerConfig +func (c *Client) CreateNodeBalancerConfig(ctx context.Context, nodebalancerID int, nodebalancerConfig NodeBalancerConfigCreateOptions) (*NodeBalancerConfig, error) { + var body string + e, err := c.NodeBalancerConfigs.endpointWithID(nodebalancerID) + + if err != nil { + return nil, err + } + + req := c.R(ctx).SetResult(&NodeBalancerConfig{}) + + if bodyData, err := json.Marshal(nodebalancerConfig); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + r, err := coupleAPIErrors(req. + SetHeader("Content-Type", "application/json"). + SetBody(body). + Post(e)) + + if err != nil { + return nil, err + } + return r.Result().(*NodeBalancerConfig).fixDates(), nil +} + +// UpdateNodeBalancerConfig updates the NodeBalancerConfig with the specified id +func (c *Client) UpdateNodeBalancerConfig(ctx context.Context, nodebalancerID int, configID int, updateOpts NodeBalancerConfigUpdateOptions) (*NodeBalancerConfig, error) { + var body string + e, err := c.NodeBalancerConfigs.endpointWithID(nodebalancerID) + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, configID) + + req := c.R(ctx).SetResult(&NodeBalancerConfig{}) + + if bodyData, err := json.Marshal(updateOpts); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + r, err := coupleAPIErrors(req. + SetBody(body). + Put(e)) + + if err != nil { + return nil, err + } + return r.Result().(*NodeBalancerConfig).fixDates(), nil +} + +// DeleteNodeBalancerConfig deletes the NodeBalancerConfig with the specified id +func (c *Client) DeleteNodeBalancerConfig(ctx context.Context, nodebalancerID int, configID int) error { + e, err := c.NodeBalancerConfigs.endpointWithID(nodebalancerID) + if err != nil { + return err + } + e = fmt.Sprintf("%s/%d", e, configID) + + _, err = coupleAPIErrors(c.R(ctx).Delete(e)) + return err +} + +// RebuildNodeBalancerConfig updates the NodeBalancer with the specified id +func (c *Client) RebuildNodeBalancerConfig(ctx context.Context, nodeBalancerID int, configID int, rebuildOpts NodeBalancerConfigRebuildOptions) (*NodeBalancerConfig, error) { + var body string + e, err := c.NodeBalancerConfigs.endpointWithID(nodeBalancerID) + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d/rebuild", e, configID) + + req := c.R(ctx).SetResult(&NodeBalancerConfig{}) + + if bodyData, err := json.Marshal(rebuildOpts); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + r, err := coupleAPIErrors(req. + SetBody(body). + Post(e)) + + if err != nil { + return nil, err + } + return r.Result().(*NodeBalancerConfig).fixDates(), nil +} diff --git a/vendor/github.com/linode/linodego/pagination.go b/vendor/github.com/linode/linodego/pagination.go new file mode 100644 index 000000000..a9b73d196 --- /dev/null +++ b/vendor/github.com/linode/linodego/pagination.go @@ -0,0 +1,481 @@ +package linodego + +/** + * Pagination and Filtering types and helpers + */ + +import ( + "context" + "fmt" + "log" + "strconv" + + "github.com/go-resty/resty" +) + +// PageOptions are the pagination parameters for List endpoints +type PageOptions struct { + Page int `url:"page,omitempty" json:"page"` + Pages int `url:"pages,omitempty" json:"pages"` + Results int `url:"results,omitempty" json:"results"` +} + +// ListOptions are the pagination and filtering (TODO) parameters for endpoints +type ListOptions struct { + *PageOptions + Filter string +} + +// NewListOptions simplified construction of ListOptions using only +// the two writable properties, Page and Filter +func NewListOptions(Page int, Filter string) *ListOptions { + return &ListOptions{PageOptions: &PageOptions{Page: Page}, Filter: Filter} + +} + +// listHelper abstracts fetching and pagination for GET endpoints that +// do not require any Ids (top level endpoints). +// When opts (or opts.Page) is nil, all pages will be fetched and +// returned in a single (endpoint-specific)PagedResponse +// opts.results and opts.pages will be updated from the API response +func (c *Client) listHelper(ctx context.Context, i interface{}, opts *ListOptions) error { + req := c.R(ctx) + if opts != nil && opts.PageOptions != nil && opts.Page > 0 { + req.SetQueryParam("page", strconv.Itoa(opts.Page)) + } + + var ( + err error + pages int + results int + r *resty.Response + ) + + if opts != nil && len(opts.Filter) > 0 { + req.SetHeader("X-Filter", opts.Filter) + } + + switch v := i.(type) { + case *LinodeKernelsPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(LinodeKernelsPagedResponse{}).Get(v.endpoint(c))); err == nil { + pages = r.Result().(*LinodeKernelsPagedResponse).Pages + results = r.Result().(*LinodeKernelsPagedResponse).Results + v.appendData(r.Result().(*LinodeKernelsPagedResponse)) + } + case *LinodeTypesPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(LinodeTypesPagedResponse{}).Get(v.endpoint(c))); err == nil { + pages = r.Result().(*LinodeTypesPagedResponse).Pages + results = r.Result().(*LinodeTypesPagedResponse).Results + v.appendData(r.Result().(*LinodeTypesPagedResponse)) + } + case *ImagesPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(ImagesPagedResponse{}).Get(v.endpoint(c))); err == nil { + pages = r.Result().(*ImagesPagedResponse).Pages + results = r.Result().(*ImagesPagedResponse).Results + v.appendData(r.Result().(*ImagesPagedResponse)) + } + case *StackscriptsPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(StackscriptsPagedResponse{}).Get(v.endpoint(c))); err == nil { + pages = r.Result().(*StackscriptsPagedResponse).Pages + results = r.Result().(*StackscriptsPagedResponse).Results + v.appendData(r.Result().(*StackscriptsPagedResponse)) + } + case *InstancesPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(InstancesPagedResponse{}).Get(v.endpoint(c))); err == nil { + pages = r.Result().(*InstancesPagedResponse).Pages + results = r.Result().(*InstancesPagedResponse).Results + v.appendData(r.Result().(*InstancesPagedResponse)) + } + case *RegionsPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(RegionsPagedResponse{}).Get(v.endpoint(c))); err == nil { + pages = r.Result().(*RegionsPagedResponse).Pages + results = r.Result().(*RegionsPagedResponse).Results + v.appendData(r.Result().(*RegionsPagedResponse)) + } + case *VolumesPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(VolumesPagedResponse{}).Get(v.endpoint(c))); err == nil { + pages = r.Result().(*VolumesPagedResponse).Pages + results = r.Result().(*VolumesPagedResponse).Results + v.appendData(r.Result().(*VolumesPagedResponse)) + } + case *DomainsPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(DomainsPagedResponse{}).Get(v.endpoint(c))); err == nil { + response, ok := r.Result().(*DomainsPagedResponse) + if !ok { + return fmt.Errorf("Response is not a *DomainsPagedResponse") + } + pages = response.Pages + results = response.Results + v.appendData(response) + } + case *EventsPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(EventsPagedResponse{}).Get(v.endpoint(c))); err == nil { + pages = r.Result().(*EventsPagedResponse).Pages + results = r.Result().(*EventsPagedResponse).Results + v.appendData(r.Result().(*EventsPagedResponse)) + } + case *LongviewSubscriptionsPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(LongviewSubscriptionsPagedResponse{}).Get(v.endpoint(c))); err == nil { + pages = r.Result().(*LongviewSubscriptionsPagedResponse).Pages + results = r.Result().(*LongviewSubscriptionsPagedResponse).Results + v.appendData(r.Result().(*LongviewSubscriptionsPagedResponse)) + } + case *LongviewClientsPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(LongviewClientsPagedResponse{}).Get(v.endpoint(c))); err == nil { + pages = r.Result().(*LongviewClientsPagedResponse).Pages + results = r.Result().(*LongviewClientsPagedResponse).Results + v.appendData(r.Result().(*LongviewClientsPagedResponse)) + } + case *IPAddressesPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(IPAddressesPagedResponse{}).Get(v.endpoint(c))); err == nil { + pages = r.Result().(*IPAddressesPagedResponse).Pages + results = r.Result().(*IPAddressesPagedResponse).Results + v.appendData(r.Result().(*IPAddressesPagedResponse)) + } + case *IPv6PoolsPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(IPv6PoolsPagedResponse{}).Get(v.endpoint(c))); err == nil { + pages = r.Result().(*IPv6PoolsPagedResponse).Pages + results = r.Result().(*IPv6PoolsPagedResponse).Results + v.appendData(r.Result().(*IPv6PoolsPagedResponse)) + } + case *IPv6RangesPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(IPv6RangesPagedResponse{}).Get(v.endpoint(c))); err == nil { + pages = r.Result().(*IPv6RangesPagedResponse).Pages + results = r.Result().(*IPv6RangesPagedResponse).Results + v.appendData(r.Result().(*IPv6RangesPagedResponse)) + // @TODO consolidate this type with IPv6PoolsPagedResponse? + } + case *SSHKeysPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(SSHKeysPagedResponse{}).Get(v.endpoint(c))); err == nil { + response, ok := r.Result().(*SSHKeysPagedResponse) + if !ok { + return fmt.Errorf("Response is not a *SSHKeysPagedResponse") + } + pages = response.Pages + results = response.Results + v.appendData(response) + } + case *TicketsPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(TicketsPagedResponse{}).Get(v.endpoint(c))); err == nil { + pages = r.Result().(*TicketsPagedResponse).Pages + results = r.Result().(*TicketsPagedResponse).Results + v.appendData(r.Result().(*TicketsPagedResponse)) + } + case *InvoicesPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(InvoicesPagedResponse{}).Get(v.endpoint(c))); err == nil { + pages = r.Result().(*InvoicesPagedResponse).Pages + results = r.Result().(*InvoicesPagedResponse).Results + v.appendData(r.Result().(*InvoicesPagedResponse)) + } + case *NotificationsPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(NotificationsPagedResponse{}).Get(v.endpoint(c))); err == nil { + pages = r.Result().(*NotificationsPagedResponse).Pages + results = r.Result().(*NotificationsPagedResponse).Results + v.appendData(r.Result().(*NotificationsPagedResponse)) + } + case *NodeBalancersPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(NodeBalancersPagedResponse{}).Get(v.endpoint(c))); err == nil { + pages = r.Result().(*NodeBalancersPagedResponse).Pages + results = r.Result().(*NodeBalancersPagedResponse).Results + v.appendData(r.Result().(*NodeBalancersPagedResponse)) + } + /** + case AccountOauthClientsPagedResponse: + if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil { + return NewError(r) + } else if err == nil { + pages = r.Result().(*AccountOauthClientsPagedResponse).Pages + results = r.Result().(*AccountOauthClientsPagedResponse).Results + v.appendData(r.Result().(*AccountOauthClientsPagedResponse)) + } + case AccountPaymentsPagedResponse: + if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil { + return NewError(r) + } else if err == nil { + pages = r.Result().(*AccountPaymentsPagedResponse).Pages + results = r.Result().(*AccountPaymentsPagedResponse).Results + v.appendData(r.Result().(*AccountPaymentsPagedResponse)) + } + case AccountUsersPagedResponse: + if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil { + return NewError(r) + } else if err == nil { + pages = r.Result().(*AccountUsersPagedResponse).Pages + results = r.Result().(*AccountUsersPagedResponse).Results + v.appendData(r.Result().(*AccountUsersPagedResponse)) + } + case ProfileAppsPagedResponse: + if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil { + return NewError(r) + } else if err == nil { + pages = r.Result().(*ProfileAppsPagedResponse).Pages + results = r.Result().(*ProfileAppsPagedResponse).Results + v.appendData(r.Result().(*ProfileAppsPagedResponse)) + } + case ProfileTokensPagedResponse: + if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil { + return NewError(r) + } else if err == nil { + pages = r.Result().(*ProfileTokensPagedResponse).Pages + results = r.Result().(*ProfileTokensPagedResponse).Results + v.appendData(r.Result().(*ProfileTokensPagedResponse)) + } + case ProfileWhitelistPagedResponse: + if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil { + return NewError(r) + } else if err == nil { + pages = r.Result().(*ProfileWhitelistPagedResponse).Pages + results = r.Result().(*ProfileWhitelistPagedResponse).Results + v.appendData(r.Result().(*ProfileWhitelistPagedResponse)) + } + case ManagedContactsPagedResponse: + if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil { + return NewError(r) + } else if err == nil { + pages = r.Result().(*ManagedContactsPagedResponse).Pages + results = r.Result().(*ManagedContactsPagedResponse).Results + v.appendData(r.Result().(*ManagedContactsPagedResponse)) + } + case ManagedCredentialsPagedResponse: + if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil { + return NewError(r) + } else if err == nil { + pages = r.Result().(*ManagedCredentialsPagedResponse).Pages + results = r.Result().(*ManagedCredentialsPagedResponse).Results + v.appendData(r.Result().(*ManagedCredentialsPagedResponse)) + } + case ManagedIssuesPagedResponse: + if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil { + return NewError(r) + } else if err == nil { + pages = r.Result().(*ManagedIssuesPagedResponse).Pages + results = r.Result().(*ManagedIssuesPagedResponse).Results + v.appendData(r.Result().(*ManagedIssuesPagedResponse)) + } + case ManagedLinodeSettingsPagedResponse: + if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil { + return NewError(r) + } else if err == nil { + pages = r.Result().(*ManagedLinodeSettingsPagedResponse).Pages + results = r.Result().(*ManagedLinodeSettingsPagedResponse).Results + v.appendData(r.Result().(*ManagedLinodeSettingsPagedResponse)) + } + case ManagedServicesPagedResponse: + if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil { + return NewError(r) + } else if err == nil { + pages = r.Result().(*ManagedServicesPagedResponse).Pages + results = r.Result().(*ManagedServicesPagedResponse).Results + v.appendData(r.Result().(*ManagedServicesPagedResponse)) + } + **/ + default: + log.Fatalf("listHelper interface{} %+v used", i) + } + + if err != nil { + return err + } + + if opts == nil { + for page := 2; page <= pages; page = page + 1 { + if err := c.listHelper(ctx, i, &ListOptions{PageOptions: &PageOptions{Page: page}}); err != nil { + return err + } + } + } else { + if opts.PageOptions == nil { + opts.PageOptions = &PageOptions{} + } + + if opts.Page == 0 { + for page := 2; page <= pages; page = page + 1 { + opts.Page = page + if err := c.listHelper(ctx, i, opts); err != nil { + return err + } + } + } + opts.Results = results + opts.Pages = pages + } + + return nil +} + +// listHelperWithID abstracts fetching and pagination for GET endpoints that +// require an Id (second level endpoints). +// When opts (or opts.Page) is nil, all pages will be fetched and +// returned in a single (endpoint-specific)PagedResponse +// opts.results and opts.pages will be updated from the API response +func (c *Client) listHelperWithID(ctx context.Context, i interface{}, id int, opts *ListOptions) error { + req := c.R(ctx) + if opts != nil && opts.Page > 0 { + req.SetQueryParam("page", strconv.Itoa(opts.Page)) + } + + var ( + err error + pages int + results int + r *resty.Response + ) + + if opts != nil && len(opts.Filter) > 0 { + req.SetHeader("X-Filter", opts.Filter) + } + + switch v := i.(type) { + case *InvoiceItemsPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(InvoiceItemsPagedResponse{}).Get(v.endpointWithID(c, id))); err == nil { + pages = r.Result().(*InvoiceItemsPagedResponse).Pages + results = r.Result().(*InvoiceItemsPagedResponse).Results + v.appendData(r.Result().(*InvoiceItemsPagedResponse)) + } + case *DomainRecordsPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(DomainRecordsPagedResponse{}).Get(v.endpointWithID(c, id))); err == nil { + response, ok := r.Result().(*DomainRecordsPagedResponse) + if !ok { + return fmt.Errorf("Response is not a *DomainRecordsPagedResponse") + } + pages = response.Pages + results = response.Results + v.appendData(response) + } + case *InstanceConfigsPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(InstanceConfigsPagedResponse{}).Get(v.endpointWithID(c, id))); err == nil { + pages = r.Result().(*InstanceConfigsPagedResponse).Pages + results = r.Result().(*InstanceConfigsPagedResponse).Results + v.appendData(r.Result().(*InstanceConfigsPagedResponse)) + } + case *InstanceDisksPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(InstanceDisksPagedResponse{}).Get(v.endpointWithID(c, id))); err == nil { + pages = r.Result().(*InstanceDisksPagedResponse).Pages + results = r.Result().(*InstanceDisksPagedResponse).Results + v.appendData(r.Result().(*InstanceDisksPagedResponse)) + } + case *NodeBalancerConfigsPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(NodeBalancerConfigsPagedResponse{}).Get(v.endpointWithID(c, id))); err == nil { + pages = r.Result().(*NodeBalancerConfigsPagedResponse).Pages + results = r.Result().(*NodeBalancerConfigsPagedResponse).Results + v.appendData(r.Result().(*NodeBalancerConfigsPagedResponse)) + } + case *InstanceVolumesPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(InstanceVolumesPagedResponse{}).Get(v.endpointWithID(c, id))); err == nil { + pages = r.Result().(*InstanceVolumesPagedResponse).Pages + results = r.Result().(*InstanceVolumesPagedResponse).Results + v.appendData(r.Result().(*InstanceVolumesPagedResponse)) + } + /** + case TicketAttachmentsPagedResponse: + if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil { + return NewError(r) + } else if err == nil { + pages = r.Result().(*TicketAttachmentsPagedResponse).Pages + results = r.Result().(*TicketAttachmentsPagedResponse).Results + v.appendData(r.Result().(*TicketAttachmentsPagedResponse)) + } + case TicketRepliesPagedResponse: + if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil { + return NewError(r) + } else if err == nil { + pages = r.Result().(*TicketRepliesPagedResponse).Pages + results = r.Result().(*TicketRepliesPagedResponse).Results + v.appendData(r.Result().(*TicketRepliesPagedResponse)) + } + **/ + default: + log.Fatalf("Unknown listHelperWithID interface{} %T used", i) + } + + if err != nil { + return err + } + + if opts == nil { + for page := 2; page <= pages; page = page + 1 { + if err := c.listHelperWithID(ctx, i, id, &ListOptions{PageOptions: &PageOptions{Page: page}}); err != nil { + return err + } + } + } else { + if opts.PageOptions == nil { + opts.PageOptions = &PageOptions{} + } + if opts.Page == 0 { + for page := 2; page <= pages; page = page + 1 { + opts.Page = page + if err := c.listHelperWithID(ctx, i, id, opts); err != nil { + return err + } + } + } + opts.Results = results + opts.Pages = pages + } + + return nil +} + +// listHelperWithTwoIDs abstracts fetching and pagination for GET endpoints that +// require twos IDs (third level endpoints). +// When opts (or opts.Page) is nil, all pages will be fetched and +// returned in a single (endpoint-specific)PagedResponse +// opts.results and opts.pages will be updated from the API response +func (c *Client) listHelperWithTwoIDs(ctx context.Context, i interface{}, firstID, secondID int, opts *ListOptions) error { + req := c.R(ctx) + if opts != nil && opts.Page > 0 { + req.SetQueryParam("page", strconv.Itoa(opts.Page)) + } + + var ( + err error + pages int + results int + r *resty.Response + ) + + if opts != nil && len(opts.Filter) > 0 { + req.SetHeader("X-Filter", opts.Filter) + } + + switch v := i.(type) { + case *NodeBalancerNodesPagedResponse: + if r, err = coupleAPIErrors(req.SetResult(NodeBalancerNodesPagedResponse{}).Get(v.endpointWithTwoIDs(c, firstID, secondID))); err == nil { + pages = r.Result().(*NodeBalancerNodesPagedResponse).Pages + results = r.Result().(*NodeBalancerNodesPagedResponse).Results + v.appendData(r.Result().(*NodeBalancerNodesPagedResponse)) + } + + default: + log.Fatalf("Unknown listHelperWithTwoIDs interface{} %T used", i) + } + + if err != nil { + return err + } + + if opts == nil { + for page := 2; page <= pages; page = page + 1 { + if err := c.listHelper(ctx, i, &ListOptions{PageOptions: &PageOptions{Page: page}}); err != nil { + return err + } + } + } else { + if opts.PageOptions == nil { + opts.PageOptions = &PageOptions{} + } + if opts.Page == 0 { + for page := 2; page <= pages; page = page + 1 { + opts.Page = page + if err := c.listHelperWithTwoIDs(ctx, i, firstID, secondID, opts); err != nil { + return err + } + } + } + opts.Results = results + opts.Pages = pages + } + + return nil +} diff --git a/vendor/github.com/linode/linodego/profile.go b/vendor/github.com/linode/linodego/profile.go new file mode 100644 index 000000000..152361292 --- /dev/null +++ b/vendor/github.com/linode/linodego/profile.go @@ -0,0 +1 @@ +package linodego diff --git a/vendor/github.com/linode/linodego/profile_sshkeys.go b/vendor/github.com/linode/linodego/profile_sshkeys.go new file mode 100644 index 000000000..2ec4d4f71 --- /dev/null +++ b/vendor/github.com/linode/linodego/profile_sshkeys.go @@ -0,0 +1,160 @@ +package linodego + +import ( + "context" + "encoding/json" + "fmt" + "time" +) + +// SSHKey represents a SSHKey object +type SSHKey struct { + ID int `json:"id"` + Label string `json:"label"` + SSHKey string `json:"ssh_key"` + CreatedStr string `json:"created"` + Created *time.Time `json:"-"` +} + +// SSHKeyCreateOptions fields are those accepted by CreateSSHKey +type SSHKeyCreateOptions struct { + Label string `json:"label"` + SSHKey string `json:"ssh_key"` +} + +// SSHKeyUpdateOptions fields are those accepted by UpdateSSHKey +type SSHKeyUpdateOptions struct { + Label string `json:"label"` +} + +// GetCreateOptions converts a SSHKey to SSHKeyCreateOptions for use in CreateSSHKey +func (i SSHKey) GetCreateOptions() (o SSHKeyCreateOptions) { + o.Label = i.Label + o.SSHKey = i.SSHKey + return +} + +// GetUpdateOptions converts a SSHKey to SSHKeyCreateOptions for use in UpdateSSHKey +func (i SSHKey) GetUpdateOptions() (o SSHKeyUpdateOptions) { + o.Label = i.Label + return +} + +// SSHKeysPagedResponse represents a paginated SSHKey API response +type SSHKeysPagedResponse struct { + *PageOptions + Data []SSHKey `json:"data"` +} + +// endpoint gets the endpoint URL for SSHKey +func (SSHKeysPagedResponse) endpoint(c *Client) string { + endpoint, err := c.SSHKeys.Endpoint() + if err != nil { + panic(err) + } + return endpoint +} + +// appendData appends SSHKeys when processing paginated SSHKey responses +func (resp *SSHKeysPagedResponse) appendData(r *SSHKeysPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListSSHKeys lists SSHKeys +func (c *Client) ListSSHKeys(ctx context.Context, opts *ListOptions) ([]SSHKey, error) { + response := SSHKeysPagedResponse{} + err := c.listHelper(ctx, &response, opts) + for i := range response.Data { + response.Data[i].fixDates() + } + if err != nil { + return nil, err + } + return response.Data, nil +} + +// fixDates converts JSON timestamps to Go time.Time values +func (i *SSHKey) fixDates() *SSHKey { + i.Created, _ = parseDates(i.CreatedStr) + return i +} + +// GetSSHKey gets the sshkey with the provided ID +func (c *Client) GetSSHKey(ctx context.Context, id int) (*SSHKey, error) { + e, err := c.SSHKeys.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, id) + r, err := coupleAPIErrors(c.R(ctx).SetResult(&SSHKey{}).Get(e)) + if err != nil { + return nil, err + } + return r.Result().(*SSHKey).fixDates(), nil +} + +// CreateSSHKey creates a SSHKey +func (c *Client) CreateSSHKey(ctx context.Context, createOpts SSHKeyCreateOptions) (*SSHKey, error) { + var body string + e, err := c.SSHKeys.Endpoint() + if err != nil { + return nil, err + } + + req := c.R(ctx).SetResult(&SSHKey{}) + + if bodyData, err := json.Marshal(createOpts); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + r, err := coupleAPIErrors(req. + SetBody(body). + Post(e)) + + if err != nil { + return nil, err + } + return r.Result().(*SSHKey).fixDates(), nil +} + +// UpdateSSHKey updates the SSHKey with the specified id +func (c *Client) UpdateSSHKey(ctx context.Context, id int, updateOpts SSHKeyUpdateOptions) (*SSHKey, error) { + var body string + e, err := c.SSHKeys.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, id) + + req := c.R(ctx).SetResult(&SSHKey{}) + + if bodyData, err := json.Marshal(updateOpts); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + r, err := coupleAPIErrors(req. + SetBody(body). + Put(e)) + + if err != nil { + return nil, err + } + return r.Result().(*SSHKey).fixDates(), nil +} + +// DeleteSSHKey deletes the SSHKey with the specified id +func (c *Client) DeleteSSHKey(ctx context.Context, id int) error { + e, err := c.SSHKeys.Endpoint() + if err != nil { + return err + } + e = fmt.Sprintf("%s/%d", e, id) + + _, err = coupleAPIErrors(c.R(ctx).Delete(e)) + return err + +} diff --git a/vendor/github.com/linode/linodego/regions.go b/vendor/github.com/linode/linodego/regions.go new file mode 100644 index 000000000..b13b996ea --- /dev/null +++ b/vendor/github.com/linode/linodego/regions.go @@ -0,0 +1,64 @@ +package linodego + +import ( + "context" + "fmt" +) + +// Region represents a linode region object +type Region struct { + ID string `json:"id"` + Country string `json:"country"` +} + +// RegionsPagedResponse represents a linode API response for listing +type RegionsPagedResponse struct { + *PageOptions + Data []Region `json:"data"` +} + +// endpoint gets the endpoint URL for Region +func (RegionsPagedResponse) endpoint(c *Client) string { + endpoint, err := c.Regions.Endpoint() + if err != nil { + panic(err) + } + return endpoint +} + +// appendData appends Regions when processing paginated Region responses +func (resp *RegionsPagedResponse) appendData(r *RegionsPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListRegions lists Regions +func (c *Client) ListRegions(ctx context.Context, opts *ListOptions) ([]Region, error) { + response := RegionsPagedResponse{} + err := c.listHelper(ctx, &response, opts) + for i := range response.Data { + response.Data[i].fixDates() + } + if err != nil { + return nil, err + } + return response.Data, nil +} + +// fixDates converts JSON timestamps to Go time.Time values +func (v *Region) fixDates() *Region { + return v +} + +// GetRegion gets the template with the provided ID +func (c *Client) GetRegion(ctx context.Context, id string) (*Region, error) { + e, err := c.Regions.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%s", e, id) + r, err := coupleAPIErrors(c.R(ctx).SetResult(&Region{}).Get(e)) + if err != nil { + return nil, err + } + return r.Result().(*Region).fixDates(), nil +} diff --git a/vendor/github.com/linode/linodego/resources.go b/vendor/github.com/linode/linodego/resources.go new file mode 100644 index 000000000..1a2027841 --- /dev/null +++ b/vendor/github.com/linode/linodego/resources.go @@ -0,0 +1,155 @@ +package linodego + +import ( + "bytes" + "context" + "fmt" + "text/template" + + "github.com/go-resty/resty" +) + +const ( + stackscriptsName = "stackscripts" + imagesName = "images" + instancesName = "instances" + instanceDisksName = "disks" + instanceConfigsName = "configs" + instanceIPsName = "ips" + instanceSnapshotsName = "snapshots" + instanceVolumesName = "instancevolumes" + ipaddressesName = "ipaddresses" + ipv6poolsName = "ipv6pools" + ipv6rangesName = "ipv6ranges" + regionsName = "regions" + volumesName = "volumes" + kernelsName = "kernels" + typesName = "types" + domainsName = "domains" + domainRecordsName = "records" + longviewName = "longview" + longviewclientsName = "longviewclients" + longviewsubscriptionsName = "longviewsubscriptions" + nodebalancersName = "nodebalancers" + nodebalancerconfigsName = "nodebalancerconfigs" + nodebalancernodesName = "nodebalancernodes" + sshkeysName = "sshkeys" + ticketsName = "tickets" + accountName = "account" + eventsName = "events" + invoicesName = "invoices" + invoiceItemsName = "invoiceitems" + profileName = "profile" + managedName = "managed" + // notificationsName = "notifications" + + stackscriptsEndpoint = "linode/stackscripts" + imagesEndpoint = "images" + instancesEndpoint = "linode/instances" + instanceConfigsEndpoint = "linode/instances/{{ .ID }}/configs" + instanceDisksEndpoint = "linode/instances/{{ .ID }}/disks" + instanceSnapshotsEndpoint = "linode/instances/{{ .ID }}/backups" + instanceIPsEndpoint = "linode/instances/{{ .ID }}/ips" + instanceVolumesEndpoint = "linode/instances/{{ .ID }}/volumes" + ipaddressesEndpoint = "network/ips" + ipv6poolsEndpoint = "network/ipv6/pools" + ipv6rangesEndpoint = "network/ipv6/ranges" + regionsEndpoint = "regions" + volumesEndpoint = "volumes" + kernelsEndpoint = "linode/kernels" + typesEndpoint = "linode/types" + domainsEndpoint = "domains" + domainRecordsEndpoint = "domains/{{ .ID }}/records" + longviewEndpoint = "longview" + longviewclientsEndpoint = "longview/clients" + longviewsubscriptionsEndpoint = "longview/subscriptions" + nodebalancersEndpoint = "nodebalancers" + // @TODO we can't use these nodebalancer endpoints unless we include these templated fields + // The API seems inconsistent about including parent IDs in objects, (compare instance configs to nb configs) + // Parent IDs would be immutable for updates and are ignored in create requests .. + // Should we include these fields in CreateOpts and UpdateOpts? + nodebalancerconfigsEndpoint = "nodebalancers/{{ .ID }}/configs" + nodebalancernodesEndpoint = "nodebalancers/{{ .ID }}/configs/{{ .SecondID }}/nodes" + sshkeysEndpoint = "profile/sshkeys" + ticketsEndpoint = "support/tickets" + accountEndpoint = "account" + eventsEndpoint = "account/events" + invoicesEndpoint = "account/invoices" + invoiceItemsEndpoint = "account/invoices/{{ .ID }}/items" + profileEndpoint = "profile" + managedEndpoint = "managed" + // notificationsEndpoint = "account/notifications" +) + +// Resource represents a linode API resource +type Resource struct { + name string + endpoint string + isTemplate bool + endpointTemplate *template.Template + R func(ctx context.Context) *resty.Request + PR func(ctx context.Context) *resty.Request +} + +// NewResource is the factory to create a new Resource struct. If it has a template string the useTemplate bool must be set. +func NewResource(client *Client, name string, endpoint string, useTemplate bool, singleType interface{}, pagedType interface{}) *Resource { + var tmpl *template.Template + + if useTemplate { + tmpl = template.Must(template.New(name).Parse(endpoint)) + } + + r := func(ctx context.Context) *resty.Request { + return client.R(ctx).SetResult(singleType) + } + + pr := func(ctx context.Context) *resty.Request { + return client.R(ctx).SetResult(pagedType) + } + + return &Resource{name, endpoint, useTemplate, tmpl, r, pr} +} + +func (r Resource) render(data ...interface{}) (string, error) { + if data == nil { + return "", NewError("Cannot template endpoint with data") + } + out := "" + buf := bytes.NewBufferString(out) + + var substitutions interface{} + if len(data) == 1 { + substitutions = struct{ ID interface{} }{data[0]} + } else if len(data) == 2 { + substitutions = struct { + ID interface{} + SecondID interface{} + }{data[0], data[1]} + } else { + return "", NewError("Too many arguments to render template (expected 1 or 2)") + } + if err := r.endpointTemplate.Execute(buf, substitutions); err != nil { + return "", NewError(err) + } + return buf.String(), nil +} + +// endpointWithID will return the rendered endpoint string for the resource with provided id +func (r Resource) endpointWithID(id ...int) (string, error) { + if !r.isTemplate { + return r.endpoint, nil + } + data := make([]interface{}, len(id)) + for i, v := range id { + data[i] = v + } + return r.render(data...) +} + +// Endpoint will return the non-templated endpoint string for resource +func (r Resource) Endpoint() (string, error) { + if r.isTemplate { + return "", NewError(fmt.Sprintf("Tried to get endpoint for %s without providing data for template", r.name)) + } + return r.endpoint, nil +} diff --git a/vendor/github.com/linode/linodego/stackscripts.go b/vendor/github.com/linode/linodego/stackscripts.go new file mode 100644 index 000000000..daf339b2f --- /dev/null +++ b/vendor/github.com/linode/linodego/stackscripts.go @@ -0,0 +1,208 @@ +package linodego + +import ( + "context" + "encoding/json" + "fmt" + "time" +) + +// Stackscript represents a Linode StackScript +type Stackscript struct { + CreatedStr string `json:"created"` + UpdatedStr string `json:"updated"` + + ID int `json:"id"` + Username string `json:"username"` + Label string `json:"label"` + Description string `json:"description"` + Images []string `json:"images"` + DeploymentsTotal int `json:"deployments_total"` + DeploymentsActive int `json:"deployments_active"` + IsPublic bool `json:"is_public"` + Created *time.Time `json:"-"` + Updated *time.Time `json:"-"` + RevNote string `json:"rev_note"` + Script string `json:"script"` + UserDefinedFields *[]StackscriptUDF `json:"user_defined_fields"` + UserGravatarID string `json:"user_gravatar_id"` +} + +// StackscriptUDF define a single variable that is accepted by a Stackscript +type StackscriptUDF struct { + // A human-readable label for the field that will serve as the input prompt for entering the value during deployment. + Label string `json:"label"` + + // The name of the field. + Name string `json:"name"` + + // An example value for the field. + Example string `json:"example"` + + // A list of acceptable single values for the field. + OneOf string `json:"oneOf,omitempty"` + + // A list of acceptable values for the field in any quantity, combination or order. + ManyOf string `json:"manyOf,omitempty"` + + // The default value. If not specified, this value will be used. + Default string `json:"default,omitempty"` +} + +// StackscriptCreateOptions fields are those accepted by CreateStackscript +type StackscriptCreateOptions struct { + Label string `json:"label"` + Description string `json:"description"` + Images []string `json:"images"` + IsPublic bool `json:"is_public"` + RevNote string `json:"rev_note"` + Script string `json:"script"` +} + +// StackscriptUpdateOptions fields are those accepted by UpdateStackscript +type StackscriptUpdateOptions StackscriptCreateOptions + +// GetCreateOptions converts a Stackscript to StackscriptCreateOptions for use in CreateStackscript +func (i Stackscript) GetCreateOptions() StackscriptCreateOptions { + return StackscriptCreateOptions{ + Label: i.Label, + Description: i.Description, + Images: i.Images, + IsPublic: i.IsPublic, + RevNote: i.RevNote, + Script: i.Script, + } +} + +// GetUpdateOptions converts a Stackscript to StackscriptUpdateOptions for use in UpdateStackscript +func (i Stackscript) GetUpdateOptions() StackscriptUpdateOptions { + return StackscriptUpdateOptions{ + Label: i.Label, + Description: i.Description, + Images: i.Images, + IsPublic: i.IsPublic, + RevNote: i.RevNote, + Script: i.Script, + } +} + +// StackscriptsPagedResponse represents a paginated Stackscript API response +type StackscriptsPagedResponse struct { + *PageOptions + Data []Stackscript `json:"data"` +} + +// endpoint gets the endpoint URL for Stackscript +func (StackscriptsPagedResponse) endpoint(c *Client) string { + endpoint, err := c.StackScripts.Endpoint() + if err != nil { + panic(err) + } + return endpoint +} + +// appendData appends Stackscripts when processing paginated Stackscript responses +func (resp *StackscriptsPagedResponse) appendData(r *StackscriptsPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListStackscripts lists Stackscripts +func (c *Client) ListStackscripts(ctx context.Context, opts *ListOptions) ([]Stackscript, error) { + response := StackscriptsPagedResponse{} + err := c.listHelper(ctx, &response, opts) + for i := range response.Data { + response.Data[i].fixDates() + } + if err != nil { + return nil, err + } + return response.Data, nil +} + +// fixDates converts JSON timestamps to Go time.Time values +func (i *Stackscript) fixDates() *Stackscript { + i.Created, _ = parseDates(i.CreatedStr) + i.Updated, _ = parseDates(i.UpdatedStr) + return i +} + +// GetStackscript gets the Stackscript with the provided ID +func (c *Client) GetStackscript(ctx context.Context, id int) (*Stackscript, error) { + e, err := c.StackScripts.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, id) + r, err := coupleAPIErrors(c.R(ctx). + SetResult(&Stackscript{}). + Get(e)) + if err != nil { + return nil, err + } + return r.Result().(*Stackscript).fixDates(), nil +} + +// CreateStackscript creates a StackScript +func (c *Client) CreateStackscript(ctx context.Context, createOpts StackscriptCreateOptions) (*Stackscript, error) { + var body string + e, err := c.StackScripts.Endpoint() + if err != nil { + return nil, err + } + + req := c.R(ctx).SetResult(&Stackscript{}) + + if bodyData, err := json.Marshal(createOpts); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + r, err := coupleAPIErrors(req. + SetBody(body). + Post(e)) + + if err != nil { + return nil, err + } + return r.Result().(*Stackscript).fixDates(), nil +} + +// UpdateStackscript updates the StackScript with the specified id +func (c *Client) UpdateStackscript(ctx context.Context, id int, updateOpts StackscriptUpdateOptions) (*Stackscript, error) { + var body string + e, err := c.StackScripts.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, id) + + req := c.R(ctx).SetResult(&Stackscript{}) + + if bodyData, err := json.Marshal(updateOpts); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + r, err := coupleAPIErrors(req. + SetBody(body). + Put(e)) + + if err != nil { + return nil, err + } + return r.Result().(*Stackscript).fixDates(), nil +} + +// DeleteStackscript deletes the StackScript with the specified id +func (c *Client) DeleteStackscript(ctx context.Context, id int) error { + e, err := c.StackScripts.Endpoint() + if err != nil { + return err + } + e = fmt.Sprintf("%s/%d", e, id) + + _, err = coupleAPIErrors(c.R(ctx).Delete(e)) + return err +} diff --git a/vendor/github.com/linode/linodego/support.go b/vendor/github.com/linode/linodego/support.go new file mode 100644 index 000000000..6c72a4adf --- /dev/null +++ b/vendor/github.com/linode/linodego/support.go @@ -0,0 +1,88 @@ +package linodego + +import ( + "context" + "fmt" + "time" +) + +// Ticket represents a support ticket object +type Ticket struct { + ID int `json:"id"` + Attachments []string `json:"attachments"` + Closed *time.Time `json:"-"` + Description string `json:"description"` + Entity *TicketEntity `json:"entity"` + GravatarID string `json:"gravatar_id"` + Opened *time.Time `json:"-"` + OpenedBy string `json:"opened_by"` + Status TicketStatus `json:"status"` + Summary string `json:"summary"` + Updated *time.Time `json:"-"` + UpdatedBy string `json:"updated_by"` +} + +// TicketEntity refers a ticket to a specific entity +type TicketEntity struct { + ID int `json:"id"` + Label string `json:"label"` + Type string `json:"type"` + URL string `json:"url"` +} + +// TicketStatus constants start with Ticket and include Linode API Ticket Status values +type TicketStatus string + +// TicketStatus constants reflect the current status of a Ticket +const ( + TicketNew TicketStatus = "new" + TicketClosed TicketStatus = "closed" + TicketOpen TicketStatus = "open" +) + +// TicketsPagedResponse represents a paginated ticket API response +type TicketsPagedResponse struct { + *PageOptions + Data []Ticket `json:"data"` +} + +func (TicketsPagedResponse) endpoint(c *Client) string { + endpoint, err := c.Tickets.Endpoint() + if err != nil { + panic(err) + } + return endpoint +} + +func (resp *TicketsPagedResponse) appendData(r *TicketsPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListTickets returns a collection of Support Tickets on the Account. Support Tickets +// can be both tickets opened with Linode for support, as well as tickets generated by +// Linode regarding the Account. This collection includes all Support Tickets generated +// on the Account, with open tickets returned first. +func (c *Client) ListTickets(ctx context.Context, opts *ListOptions) ([]Ticket, error) { + response := TicketsPagedResponse{} + err := c.listHelper(ctx, &response, opts) + if err != nil { + return nil, err + } + return response.Data, nil +} + +// GetTicket gets a Support Ticket on the Account with the specified ID +func (c *Client) GetTicket(ctx context.Context, id int) (*Ticket, error) { + e, err := c.Tickets.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, id) + r, err := coupleAPIErrors(c.R(ctx). + SetResult(&Ticket{}). + Get(e)) + if err != nil { + return nil, err + } + return r.Result().(*Ticket), nil +} diff --git a/vendor/github.com/linode/linodego/template.go b/vendor/github.com/linode/linodego/template.go new file mode 100644 index 000000000..6048dded3 --- /dev/null +++ b/vendor/github.com/linode/linodego/template.go @@ -0,0 +1,167 @@ +// +build ignore + +package linodego + +/* + - replace "Template" with "NameOfResource" + - replace "template" with "nameOfResource" + - copy template_test.go and do the same + - When updating Template structs, + - use pointers where ever null'able would have a different meaning if the wrapper + supplied "" or 0 instead + - Add "NameOfResource" to client.go, resources.go, pagination.go +*/ + +import ( + "context" + "encoding/json" + "fmt" +) + +// Template represents a Template object +type Template struct { + ID int `json:"id"` + // UpdatedStr string `json:"updated"` + // Updated *time.Time `json:"-"` +} + +// TemplateCreateOptions fields are those accepted by CreateTemplate +type TemplateCreateOptions struct { +} + +// TemplateUpdateOptions fields are those accepted by UpdateTemplate +type TemplateUpdateOptions struct { +} + +// GetCreateOptions converts a Template to TemplateCreateOptions for use in CreateTemplate +func (i Template) GetCreateOptions() (o TemplateCreateOptions) { + // o.Label = i.Label + // o.Description = copyString(o.Description) + return +} + +// GetUpdateOptions converts a Template to TemplateUpdateOptions for use in UpdateTemplate +func (i Template) GetUpdateOptions() (o TemplateUpdateOptions) { + // o.Label = i.Label + // o.Description = copyString(o.Description) + return +} + +// TemplatesPagedResponse represents a paginated Template API response +type TemplatesPagedResponse struct { + *PageOptions + Data []Template `json:"data"` +} + +// endpoint gets the endpoint URL for Template +func (TemplatesPagedResponse) endpoint(c *Client) string { + endpoint, err := c.Templates.Endpoint() + if err != nil { + panic(err) + } + return endpoint +} + +// appendData appends Templates when processing paginated Template responses +func (resp *TemplatesPagedResponse) appendData(r *TemplatesPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListTemplates lists Templates +func (c *Client) ListTemplates(ctx context.Context, opts *ListOptions) ([]Template, error) { + response := TemplatesPagedResponse{} + err := c.listHelper(ctx, &response, opts) + for i := range response.Data { + response.Data[i].fixDates() + } + if err != nil { + return nil, err + } + return response.Data, nil +} + +// fixDates converts JSON timestamps to Go time.Time values +func (i *Template) fixDates() *Template { + // i.Created, _ = parseDates(i.CreatedStr) + // i.Updated, _ = parseDates(i.UpdatedStr) + return i +} + +// GetTemplate gets the template with the provided ID +func (c *Client) GetTemplate(ctx context.Context, id int) (*Template, error) { + e, err := c.Templates.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, id) + r, err := coupleAPIErrors(c.R(ctx).SetResult(&Template{}).Get(e)) + if err != nil { + return nil, err + } + return r.Result().(*Template).fixDates(), nil +} + +// CreateTemplate creates a Template +func (c *Client) CreateTemplate(ctx context.Context, createOpts TemplateCreateOptions) (*Template, error) { + var body string + e, err := c.Templates.Endpoint() + if err != nil { + return nil, err + } + + req := c.R(ctx).SetResult(&Template{}) + + if bodyData, err := json.Marshal(createOpts); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + r, err := coupleAPIErrors(req. + SetBody(body). + Post(e)) + + if err != nil { + return nil, err + } + return r.Result().(*Template).fixDates(), nil +} + +// UpdateTemplate updates the Template with the specified id +func (c *Client) UpdateTemplate(ctx context.Context, id int, updateOpts TemplateUpdateOptions) (*Template, error) { + var body string + e, err := c.Templates.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, id) + + req := c.R(ctx).SetResult(&Template{}) + + if bodyData, err := json.Marshal(updateOpts); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + r, err := coupleAPIErrors(req. + SetBody(body). + Put(e)) + + if err != nil { + return nil, err + } + return r.Result().(*Template).fixDates(), nil +} + +// DeleteTemplate deletes the Template with the specified id +func (c *Client) DeleteTemplate(ctx context.Context, id int) error { + e, err := c.Templates.Endpoint() + if err != nil { + return err + } + e = fmt.Sprintf("%s/%d", e, id) + + _, err = coupleAPIErrors(c.R(ctx).Delete(e)) + return err +} diff --git a/vendor/github.com/linode/linodego/types.go b/vendor/github.com/linode/linodego/types.go new file mode 100644 index 000000000..edeada84a --- /dev/null +++ b/vendor/github.com/linode/linodego/types.go @@ -0,0 +1,89 @@ +package linodego + +import ( + "context" + "fmt" +) + +// LinodeType represents a linode type object +type LinodeType struct { + ID string `json:"id"` + Disk int `json:"disk"` + Class LinodeTypeClass `json:"class"` // enum: nanode, standard, highmem + Price *LinodePrice `json:"price"` + Label string `json:"label"` + Addons *LinodeAddons `json:"addons"` + NetworkOut int `json:"network_out"` + Memory int `json:"memory"` + Transfer int `json:"transfer"` + VCPUs int `json:"vcpus"` +} + +// LinodePrice represents a linode type price object +type LinodePrice struct { + Hourly float32 `json:"hourly"` + Monthly float32 `json:"monthly"` +} + +// LinodeBackupsAddon represents a linode backups addon object +type LinodeBackupsAddon struct { + Price *LinodePrice `json:"price"` +} + +// LinodeAddons represent the linode addons object +type LinodeAddons struct { + Backups *LinodeBackupsAddon `json:"backups"` +} + +// LinodeTypeClass constants start with Class and include Linode API Instance Type Classes +type LinodeTypeClass string + +// LinodeTypeClass contants are the Instance Type Classes that an Instance Type can be assigned +const ( + ClassNanode LinodeTypeClass = "nanode" + ClassStandard LinodeTypeClass = "standard" + ClassHighmem LinodeTypeClass = "highmem" +) + +// LinodeTypesPagedResponse represents a linode types API response for listing +type LinodeTypesPagedResponse struct { + *PageOptions + Data []LinodeType `json:"data"` +} + +func (LinodeTypesPagedResponse) endpoint(c *Client) string { + endpoint, err := c.Types.Endpoint() + if err != nil { + panic(err) + } + return endpoint +} + +func (resp *LinodeTypesPagedResponse) appendData(r *LinodeTypesPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListTypes lists linode types +func (c *Client) ListTypes(ctx context.Context, opts *ListOptions) ([]LinodeType, error) { + response := LinodeTypesPagedResponse{} + err := c.listHelper(ctx, &response, opts) + if err != nil { + return nil, err + } + return response.Data, nil +} + +// GetType gets the type with the provided ID +func (c *Client) GetType(ctx context.Context, typeID string) (*LinodeType, error) { + e, err := c.Types.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%s", e, typeID) + + r, err := coupleAPIErrors(c.Types.R(ctx).Get(e)) + if err != nil { + return nil, err + } + return r.Result().(*LinodeType), nil +} diff --git a/vendor/github.com/linode/linodego/util.go b/vendor/github.com/linode/linodego/util.go new file mode 100644 index 000000000..a37a304e4 --- /dev/null +++ b/vendor/github.com/linode/linodego/util.go @@ -0,0 +1,15 @@ +package linodego + +import "time" + +const ( + dateLayout = "2006-01-02T15:04:05" +) + +func parseDates(dateStr string) (*time.Time, error) { + d, err := time.Parse(dateLayout, dateStr) + if err != nil { + return nil, err + } + return &d, nil +} diff --git a/vendor/github.com/linode/linodego/volumes.go b/vendor/github.com/linode/linodego/volumes.go new file mode 100644 index 000000000..eef492d83 --- /dev/null +++ b/vendor/github.com/linode/linodego/volumes.go @@ -0,0 +1,260 @@ +package linodego + +import ( + "context" + "encoding/json" + "fmt" + "time" +) + +// VolumeStatus indicates the status of the Volume +type VolumeStatus string + +const ( + // VolumeCreating indicates the Volume is being created and is not yet available for use + VolumeCreating VolumeStatus = "creating" + + // VolumeActive indicates the Volume is online and available for use + VolumeActive VolumeStatus = "active" + + // VolumeResizing indicates the Volume is in the process of upgrading its current capacity + VolumeResizing VolumeStatus = "resizing" + + // VolumeContactSupport indicates there is a problem with the Volume. A support ticket must be opened to resolve the issue + VolumeContactSupport VolumeStatus = "contact_support" +) + +// Volume represents a linode volume object +type Volume struct { + CreatedStr string `json:"created"` + UpdatedStr string `json:"updated"` + + ID int `json:"id"` + Label string `json:"label"` + Status VolumeStatus `json:"status"` + Region string `json:"region"` + Size int `json:"size"` + LinodeID *int `json:"linode_id"` + FilesystemPath string `json:"filesystem_path"` + Created time.Time `json:"-"` + Updated time.Time `json:"-"` +} + +// VolumeCreateOptions fields are those accepted by CreateVolume +type VolumeCreateOptions struct { + Label string `json:"label,omitempty"` + Region string `json:"region,omitempty"` + LinodeID int `json:"linode_id,omitempty"` + ConfigID int `json:"config_id,omitempty"` + // The Volume's size, in GiB. Minimum size is 10GiB, maximum size is 10240GiB. A "0" value will result in the default size. + Size int `json:"size,omitempty"` +} + +// VolumeAttachOptions fields are those accepted by AttachVolume +type VolumeAttachOptions struct { + LinodeID int `json:"linode_id"` + ConfigID int `json:"config_id,omitempty"` +} + +// VolumesPagedResponse represents a linode API response for listing of volumes +type VolumesPagedResponse struct { + *PageOptions + Data []Volume `json:"data"` +} + +// endpoint gets the endpoint URL for Volume +func (VolumesPagedResponse) endpoint(c *Client) string { + endpoint, err := c.Volumes.Endpoint() + if err != nil { + panic(err) + } + return endpoint +} + +// appendData appends Volumes when processing paginated Volume responses +func (resp *VolumesPagedResponse) appendData(r *VolumesPagedResponse) { + resp.Data = append(resp.Data, r.Data...) +} + +// ListVolumes lists Volumes +func (c *Client) ListVolumes(ctx context.Context, opts *ListOptions) ([]Volume, error) { + response := VolumesPagedResponse{} + err := c.listHelper(ctx, &response, opts) + for i := range response.Data { + response.Data[i].fixDates() + } + if err != nil { + return nil, err + } + return response.Data, nil +} + +// fixDates converts JSON timestamps to Go time.Time values +func (v *Volume) fixDates() *Volume { + if parsed, err := parseDates(v.CreatedStr); err != nil { + v.Created = *parsed + } + if parsed, err := parseDates(v.UpdatedStr); err != nil { + v.Updated = *parsed + } + return v +} + +// GetVolume gets the template with the provided ID +func (c *Client) GetVolume(ctx context.Context, id int) (*Volume, error) { + e, err := c.Volumes.Endpoint() + if err != nil { + return nil, err + } + e = fmt.Sprintf("%s/%d", e, id) + r, err := coupleAPIErrors(c.R(ctx).SetResult(&Volume{}).Get(e)) + if err != nil { + return nil, err + } + return r.Result().(*Volume).fixDates(), nil +} + +// AttachVolume attaches a volume to a Linode instance +func (c *Client) AttachVolume(ctx context.Context, id int, options *VolumeAttachOptions) (*Volume, error) { + body := "" + if bodyData, err := json.Marshal(options); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + e, err := c.Volumes.Endpoint() + if err != nil { + return nil, NewError(err) + } + + e = fmt.Sprintf("%s/%d/attach", e, id) + resp, err := coupleAPIErrors(c.R(ctx). + SetResult(&Volume{}). + SetBody(body). + Post(e)) + + if err != nil { + return nil, err + } + + return resp.Result().(*Volume).fixDates(), nil +} + +// CreateVolume creates a Linode Volume +func (c *Client) CreateVolume(ctx context.Context, createOpts VolumeCreateOptions) (*Volume, error) { + body := "" + if bodyData, err := json.Marshal(createOpts); err == nil { + body = string(bodyData) + } else { + return nil, NewError(err) + } + + e, err := c.Volumes.Endpoint() + if err != nil { + return nil, NewError(err) + } + + resp, err := coupleAPIErrors(c.R(ctx). + SetResult(&Volume{}). + SetBody(body). + Post(e)) + + if err != nil { + return nil, err + } + + return resp.Result().(*Volume).fixDates(), nil +} + +// RenameVolume renames the label of a Linode volume +// There is no UpdateVolume because the label is the only alterable field. +func (c *Client) RenameVolume(ctx context.Context, id int, label string) (*Volume, error) { + body, _ := json.Marshal(map[string]string{"label": label}) + + e, err := c.Volumes.Endpoint() + if err != nil { + return nil, NewError(err) + } + e = fmt.Sprintf("%s/%d", e, id) + + resp, err := coupleAPIErrors(c.R(ctx). + SetResult(&Volume{}). + SetBody(body). + Put(e)) + + if err != nil { + return nil, err + } + + return resp.Result().(*Volume).fixDates(), nil +} + +// CloneVolume clones a Linode volume +func (c *Client) CloneVolume(ctx context.Context, id int, label string) (*Volume, error) { + body := fmt.Sprintf("{\"label\":\"%s\"}", label) + + e, err := c.Volumes.Endpoint() + if err != nil { + return nil, NewError(err) + } + e = fmt.Sprintf("%s/%d/clone", e, id) + + resp, err := coupleAPIErrors(c.R(ctx). + SetResult(&Volume{}). + SetBody(body). + Post(e)) + + if err != nil { + return nil, err + } + + return resp.Result().(*Volume).fixDates(), nil +} + +// DetachVolume detaches a Linode volume +func (c *Client) DetachVolume(ctx context.Context, id int) error { + body := "" + + e, err := c.Volumes.Endpoint() + if err != nil { + return NewError(err) + } + + e = fmt.Sprintf("%s/%d/detach", e, id) + + _, err = coupleAPIErrors(c.R(ctx). + SetBody(body). + Post(e)) + + return err +} + +// ResizeVolume resizes an instance to new Linode type +func (c *Client) ResizeVolume(ctx context.Context, id int, size int) error { + body := fmt.Sprintf("{\"size\": %d}", size) + + e, err := c.Volumes.Endpoint() + if err != nil { + return NewError(err) + } + e = fmt.Sprintf("%s/%d/resize", e, id) + + _, err = coupleAPIErrors(c.R(ctx). + SetBody(body). + Post(e)) + + return err +} + +// DeleteVolume deletes the Volume with the specified id +func (c *Client) DeleteVolume(ctx context.Context, id int) error { + e, err := c.Volumes.Endpoint() + if err != nil { + return err + } + e = fmt.Sprintf("%s/%d", e, id) + + _, err = coupleAPIErrors(c.R(ctx).Delete(e)) + return err +} diff --git a/vendor/github.com/linode/linodego/waitfor.go b/vendor/github.com/linode/linodego/waitfor.go new file mode 100644 index 000000000..aaf387c46 --- /dev/null +++ b/vendor/github.com/linode/linodego/waitfor.go @@ -0,0 +1,240 @@ +package linodego + +import ( + "context" + "encoding/json" + "fmt" + "log" + "strconv" + "strings" + "time" +) + +// WaitForInstanceStatus waits for the Linode instance to reach the desired state +// before returning. It will timeout with an error after timeoutSeconds. +func (client Client) WaitForInstanceStatus(ctx context.Context, instanceID int, status InstanceStatus, timeoutSeconds int) (*Instance, error) { + ctx, cancel := context.WithTimeout(ctx, time.Duration(timeoutSeconds)*time.Second) + defer cancel() + + ticker := time.NewTicker(time.Second) + defer ticker.Stop() + for { + select { + case <-ticker.C: + instance, err := client.GetInstance(ctx, instanceID) + if err != nil { + return instance, err + } + complete := (instance.Status == status) + + if complete { + return instance, nil + } + case <-ctx.Done(): + return nil, fmt.Errorf("Error waiting for Instance %d status %s: %s", instanceID, status, ctx.Err()) + } + } +} + +// WaitForVolumeStatus waits for the Volume to reach the desired state +// before returning. It will timeout with an error after timeoutSeconds. +func (client Client) WaitForVolumeStatus(ctx context.Context, volumeID int, status VolumeStatus, timeoutSeconds int) (*Volume, error) { + ctx, cancel := context.WithTimeout(ctx, time.Duration(timeoutSeconds)*time.Second) + defer cancel() + + ticker := time.NewTicker(time.Second) + defer ticker.Stop() + for { + select { + case <-ticker.C: + volume, err := client.GetVolume(ctx, volumeID) + if err != nil { + return volume, err + } + complete := (volume.Status == status) + + if complete { + return volume, nil + } + case <-ctx.Done(): + return nil, fmt.Errorf("Error waiting for Volume %d status %s: %s", volumeID, status, ctx.Err()) + } + } +} + +// WaitForSnapshotStatus waits for the Snapshot to reach the desired state +// before returning. It will timeout with an error after timeoutSeconds. +func (client Client) WaitForSnapshotStatus(ctx context.Context, instanceID int, snapshotID int, status InstanceSnapshotStatus, timeoutSeconds int) (*InstanceSnapshot, error) { + ctx, cancel := context.WithTimeout(ctx, time.Duration(timeoutSeconds)*time.Second) + defer cancel() + + ticker := time.NewTicker(time.Second) + defer ticker.Stop() + for { + select { + case <-ticker.C: + snapshot, err := client.GetInstanceSnapshot(ctx, instanceID, snapshotID) + if err != nil { + return snapshot, err + } + complete := (snapshot.Status == status) + + if complete { + return snapshot, nil + } + case <-ctx.Done(): + return nil, fmt.Errorf("Error waiting for Instance %d Snapshot %d status %s: %s", instanceID, snapshotID, status, ctx.Err()) + } + } +} + +// WaitForVolumeLinodeID waits for the Volume to match the desired LinodeID +// before returning. An active Instance will not immediately attach or detach a volume, so the +// the LinodeID must be polled to determine volume readiness from the API. +// WaitForVolumeLinodeID will timeout with an error after timeoutSeconds. +func (client Client) WaitForVolumeLinodeID(ctx context.Context, volumeID int, linodeID *int, timeoutSeconds int) (*Volume, error) { + ctx, cancel := context.WithTimeout(ctx, time.Duration(timeoutSeconds)*time.Second) + defer cancel() + + ticker := time.NewTicker(time.Second) + defer ticker.Stop() + for { + select { + case <-ticker.C: + volume, err := client.GetVolume(ctx, volumeID) + if err != nil { + return volume, err + } + + if linodeID == nil && volume.LinodeID == nil { + return volume, nil + } else if linodeID == nil || volume.LinodeID == nil { + // continue waiting + } else if *volume.LinodeID == *linodeID { + return volume, nil + } + + case <-ctx.Done(): + return nil, fmt.Errorf("Error waiting for Volume %d to have Instance %v: %s", volumeID, linodeID, ctx.Err()) + } + } +} + +// WaitForEventFinished waits for an entity action to reach the 'finished' state +// before returning. It will timeout with an error after timeoutSeconds. +// If the event indicates a failure both the failed event and the error will be returned. +func (client Client) WaitForEventFinished(ctx context.Context, id interface{}, entityType EntityType, action EventAction, minStart time.Time, timeoutSeconds int) (*Event, error) { + titledEntityType := strings.Title(string(entityType)) + filter, _ := json.Marshal(map[string]interface{}{ + // Entity is not filtered by the API + // Perhaps one day they will permit Entity ID/Type filtering. + // We'll have to verify these values manually, for now. + //"entity": map[string]interface{}{ + // "id": fmt.Sprintf("%v", id), + // "type": entityType, + //}, + + // Nor is action + //"action": action, + + // Created is not correctly filtered by the API + // We'll have to verify these values manually, for now. + //"created": map[string]interface{}{ + // "+gte": minStart.Format(time.RFC3339), + //}, + + // With potentially 1000+ events coming back, we should filter on something + "seen": false, + + // Float the latest events to page 1 + "+order_by": "created", + "+order": "desc", + }) + + // Optimistically restrict results to page 1. We should remove this when more + // precise filtering options exist. + listOptions := NewListOptions(1, string(filter)) + + ctx, cancel := context.WithTimeout(ctx, time.Duration(timeoutSeconds)*time.Second) + defer cancel() + + if deadline, ok := ctx.Deadline(); ok { + duration := time.Until(deadline) + log.Printf("[INFO] Waiting %d seconds for %s events since %v for %s %v", int(duration.Seconds()), action, minStart, titledEntityType, id) + } + + ticker := time.NewTicker(time.Second) + defer ticker.Stop() + for { + select { + case <-ticker.C: + + events, err := client.ListEvents(ctx, listOptions) + if err != nil { + return nil, err + } + + // If there are events for this instance + action, inspect them + for _, event := range events { + if event.Action != action { + // log.Println("action mismatch", event.Action, action) + continue + } + if event.Entity.Type != entityType { + // log.Println("type mismatch", event.Entity.Type, entityType) + continue + } + + var entID string + + switch event.Entity.ID.(type) { + case float64, float32: + entID = fmt.Sprintf("%.f", event.Entity.ID) + case int: + entID = strconv.Itoa(event.Entity.ID.(int)) + default: + entID = fmt.Sprintf("%v", event.Entity.ID) + } + + var findID string + switch id.(type) { + case float64, float32: + findID = fmt.Sprintf("%.f", id) + case int: + findID = strconv.Itoa(id.(int)) + default: + findID = fmt.Sprintf("%v", id) + } + + if entID != findID { + // log.Println("id mismatch", entID, findID) + continue + } + + // @TODO(displague) This event.Created check shouldn't be needed, but it appears + // that the ListEvents method is not populating it correctly + if event.Created == nil { + log.Printf("[WARN] event.Created is nil when API returned: %#+v", event.CreatedStr) + } else if *event.Created != minStart && !event.Created.After(minStart) { + // Not the event we were looking for + // log.Println(event.Created, "is not >=", minStart) + continue + + } + + if event.Status == EventFailed { + return &event, fmt.Errorf("%s %v action %s failed", titledEntityType, id, action) + } else if event.Status == EventScheduled { + log.Printf("[INFO] %s %v action %s is scheduled", titledEntityType, id, action) + } else if event.Status == EventFinished { + log.Printf("[INFO] %s %v action %s is finished", titledEntityType, id, action) + return &event, nil + } + // TODO(displague) can we bump the ticker to TimeRemaining/2 (>=1) when non-nil? + log.Printf("[INFO] %s %v action %s is %s", titledEntityType, id, action, event.Status) + } + case <-ctx.Done(): + return nil, fmt.Errorf("Error waiting for Event Status '%s' of %s %v action '%s': %s", EventFinished, titledEntityType, id, action, ctx.Err()) + } + } +} diff --git a/vendor/github.com/vulcand/oxy/buffer/buffer.go b/vendor/github.com/vulcand/oxy/buffer/buffer.go index 08eb8b3e7..404d79965 100644 --- a/vendor/github.com/vulcand/oxy/buffer/buffer.go +++ b/vendor/github.com/vulcand/oxy/buffer/buffer.go @@ -330,7 +330,7 @@ func (b *Buffer) copyRequest(req *http.Request, body io.ReadCloser, bodySize int o.TransferEncoding = []string{} // http.Transport will close the request body on any error, we are controlling the close process ourselves, so we override the closer here if body == nil { - o.Body = nil + o.Body = ioutil.NopCloser(req.Body) } else { o.Body = ioutil.NopCloser(body.(io.Reader)) } diff --git a/vendor/github.com/vulcand/oxy/forward/fwd.go b/vendor/github.com/vulcand/oxy/forward/fwd.go index e81ee89c2..5eca0a4a5 100644 --- a/vendor/github.com/vulcand/oxy/forward/fwd.go +++ b/vendor/github.com/vulcand/oxy/forward/fwd.go @@ -491,6 +491,9 @@ func (f *httpForwarder) copyWebSocketRequest(req *http.Request) (outReq *http.Re outReq.RequestURI = "" // Outgoing request should not have RequestURI outReq.URL.Host = req.URL.Host + if !f.passHost { + outReq.Host = req.URL.Host + } outReq.Header = make(http.Header) // gorilla websocket use this header to set the request.Host tested in checkSameOrigin diff --git a/vendor/github.com/xenolf/lego/acme/challenges.go b/vendor/github.com/xenolf/lego/acme/challenges.go index 1140b1073..d10f82b87 100644 --- a/vendor/github.com/xenolf/lego/acme/challenges.go +++ b/vendor/github.com/xenolf/lego/acme/challenges.go @@ -7,9 +7,11 @@ const ( // HTTP01 is the "http-01" ACME challenge https://github.com/ietf-wg-acme/acme/blob/master/draft-ietf-acme-acme.md#http // Note: HTTP01ChallengePath returns the URL path to fulfill this challenge HTTP01 = Challenge("http-01") + // DNS01 is the "dns-01" ACME challenge https://github.com/ietf-wg-acme/acme/blob/master/draft-ietf-acme-acme.md#dns // Note: DNS01Record returns a DNS record which will fulfill this challenge DNS01 = Challenge("dns-01") + // TLSALPN01 is the "tls-alpn-01" ACME challenge https://tools.ietf.org/html/draft-ietf-acme-tls-alpn-01 TLSALPN01 = Challenge("tls-alpn-01") ) diff --git a/vendor/github.com/xenolf/lego/acme/client.go b/vendor/github.com/xenolf/lego/acme/client.go index 12fd5b37a..4908d592c 100644 --- a/vendor/github.com/xenolf/lego/acme/client.go +++ b/vendor/github.com/xenolf/lego/acme/client.go @@ -26,6 +26,9 @@ const ( // “new-reg”, “new-authz” and “new-cert” endpoints. From the documentation the // limitation is 20 requests per second, but using 20 as value doesn't work but 18 do overallRequestLimit = 18 + + statusValid = "valid" + statusInvalid = "invalid" ) // User interface is to be implemented by users of this library. @@ -43,7 +46,7 @@ type solver interface { // Interface for challenges like dns, where we can set a record in advance for ALL challenges. // This saves quite a bit of time vs creating the records and solving them serially. -type presolver interface { +type preSolver interface { PreSolve(challenge challenge, domain string) error } @@ -409,7 +412,7 @@ DNSNames: // the whole certificate will fail. func (c *Client) ObtainCertificate(domains []string, bundle bool, privKey crypto.PrivateKey, mustStaple bool) (*CertificateResource, error) { if len(domains) == 0 { - return nil, errors.New("No domains to obtain a certificate for") + return nil, errors.New("no domains to obtain a certificate for") } if bundle { @@ -502,9 +505,9 @@ func (c *Client) RenewCertificate(cert CertificateResource, bundle, mustStaple b // Start by checking to see if the certificate was based off a CSR, and // use that if it's defined. if len(cert.CSR) > 0 { - csr, err := pemDecodeTox509CSR(cert.CSR) - if err != nil { - return nil, err + csr, errP := pemDecodeTox509CSR(cert.CSR) + if errP != nil { + return nil, errP } newCert, failures := c.ObtainCertificateForCSR(*csr, bundle) return newCert, failures @@ -537,7 +540,6 @@ func (c *Client) RenewCertificate(cert CertificateResource, bundle, mustStaple b } func (c *Client) createOrderForIdentifiers(domains []string) (orderResource, error) { - var identifiers []identifier for _, domain := range domains { identifiers = append(identifiers, identifier{Type: "dns", Value: domain}) @@ -577,16 +579,16 @@ func (c *Client) solveChallengeForAuthz(authorizations []authorization) error { // loop through the resources, basically through the domains. First pass just selects a solver for each authz. for _, authz := range authorizations { - if authz.Status == "valid" { + if authz.Status == statusValid { // Boulder might recycle recent validated authz (see issue #267) log.Infof("[%s] acme: Authorization already valid; skipping challenge", authz.Identifier.Value) continue } - if i, solver := c.chooseSolver(authz, authz.Identifier.Value); solver != nil { + if i, solvr := c.chooseSolver(authz, authz.Identifier.Value); solvr != nil { authSolvers = append(authSolvers, &selectedAuthSolver{ authz: authz, challengeIndex: i, - solver: solver, + solver: solvr, }) } else { failures[authz.Identifier.Value] = fmt.Errorf("[%s] acme: Could not determine solvers", authz.Identifier.Value) @@ -597,7 +599,7 @@ func (c *Client) solveChallengeForAuthz(authorizations []authorization) error { for _, item := range authSolvers { authz := item.authz i := item.challengeIndex - if presolver, ok := item.solver.(presolver); ok { + if presolver, ok := item.solver.(preSolver); ok { if err := presolver.PreSolve(authz.Challenges[i], authz.Identifier.Value); err != nil { failures[authz.Identifier.Value] = err } @@ -607,12 +609,12 @@ func (c *Client) solveChallengeForAuthz(authorizations []authorization) error { defer func() { // clean all created TXT records for _, item := range authSolvers { - if cleanup, ok := item.solver.(cleanup); ok { + if clean, ok := item.solver.(cleanup); ok { if failures[item.authz.Identifier.Value] != nil { // already failed in previous loop continue } - err := cleanup.CleanUp(item.authz.Challenges[item.challengeIndex], item.authz.Identifier.Value) + err := clean.CleanUp(item.authz.Challenges[item.challengeIndex], item.authz.Identifier.Value) if err != nil { log.Warnf("Error cleaning up %s: %v ", item.authz.Identifier.Value, err) } @@ -755,7 +757,7 @@ func (c *Client) requestCertificateForCsr(order orderResource, bundle bool, csr return nil, err } - if retOrder.Status == "invalid" { + if retOrder.Status == statusInvalid { return nil, err } @@ -765,7 +767,7 @@ func (c *Client) requestCertificateForCsr(order orderResource, bundle bool, csr PrivateKey: privateKeyPem, } - if retOrder.Status == "valid" { + if retOrder.Status == statusValid { // if the certificate is available right away, short cut! ok, err := c.checkCertResponse(retOrder, &certRes, bundle) if err != nil { @@ -809,9 +811,8 @@ func (c *Client) requestCertificateForCsr(order orderResource, bundle bool, csr // should already have the Domain (common name) field populated. If bundle is // true, the certificate will be bundled with the issuer's cert. func (c *Client) checkCertResponse(order orderMessage, certRes *CertificateResource, bundle bool) (bool, error) { - switch order.Status { - case "valid": + case statusValid: resp, err := httpGet(order.Certificate) if err != nil { return false, err @@ -860,7 +861,7 @@ func (c *Client) checkCertResponse(order orderMessage, certRes *CertificateResou case "processing": return false, nil - case "invalid": + case statusInvalid: return false, errors.New("order has invalid state: invalid") default: return false, nil @@ -922,12 +923,12 @@ func validate(j *jws, domain, uri string, c challenge) error { // Repeatedly check the server for an updated status on our request. for { switch chlng.Status { - case "valid": + case statusValid: log.Infof("[%s] The server validated our request", domain) return nil case "pending": case "processing": - case "invalid": + case statusInvalid: return handleChallengeError(chlng) default: return errors.New("the server returned an unexpected state") diff --git a/vendor/github.com/xenolf/lego/acme/crypto.go b/vendor/github.com/xenolf/lego/acme/crypto.go index f5ebbf08e..5ddeeaec2 100644 --- a/vendor/github.com/xenolf/lego/acme/crypto.go +++ b/vendor/github.com/xenolf/lego/acme/crypto.go @@ -81,20 +81,20 @@ func GetOCSPForCert(bundle []byte) ([]byte, *ocsp.Response, error) { return nil, nil, errors.New("no issuing certificate URL") } - resp, err := httpGet(issuedCert.IssuingCertificateURL[0]) - if err != nil { - return nil, nil, err + resp, errC := httpGet(issuedCert.IssuingCertificateURL[0]) + if errC != nil { + return nil, nil, errC } defer resp.Body.Close() - issuerBytes, err := ioutil.ReadAll(limitReader(resp.Body, 1024*1024)) - if err != nil { - return nil, nil, err + issuerBytes, errC := ioutil.ReadAll(limitReader(resp.Body, 1024*1024)) + if errC != nil { + return nil, nil, errC } - issuerCert, err := x509.ParseCertificate(issuerBytes) - if err != nil { - return nil, nil, err + issuerCert, errC := x509.ParseCertificate(issuerBytes) + if errC != nil { + return nil, nil, errC } // Insert it into the slice on position 0 @@ -258,15 +258,6 @@ func pemDecode(data []byte) (*pem.Block, error) { return pemBlock, nil } -func pemDecodeTox509(pem []byte) (*x509.Certificate, error) { - pemBlock, err := pemDecode(pem) - if pemBlock == nil { - return nil, err - } - - return x509.ParseCertificate(pemBlock.Bytes) -} - func pemDecodeTox509CSR(pem []byte) (*x509.CertificateRequest, error) { pemBlock, err := pemDecode(pem) if pemBlock == nil { diff --git a/vendor/github.com/xenolf/lego/acme/dns_challenge.go b/vendor/github.com/xenolf/lego/acme/dns_challenge.go index 630940647..f803d0a8c 100644 --- a/vendor/github.com/xenolf/lego/acme/dns_challenge.go +++ b/vendor/github.com/xenolf/lego/acme/dns_challenge.go @@ -196,7 +196,7 @@ func checkAuthoritativeNss(fqdn, value string, nameservers []string) (bool, erro } if !found { - return false, fmt.Errorf("NS %s did not return the expected TXT record", ns) + return false, fmt.Errorf("NS %s did not return the expected TXT record [fqdn: %s]", ns, fqdn) } } diff --git a/vendor/github.com/xenolf/lego/acme/http.go b/vendor/github.com/xenolf/lego/acme/http.go index 8d7ee51ef..2ddbc0f1b 100644 --- a/vendor/github.com/xenolf/lego/acme/http.go +++ b/vendor/github.com/xenolf/lego/acme/http.go @@ -148,29 +148,25 @@ func getJSON(uri string, respBody interface{}) (http.Header, error) { func postJSON(j *jws, uri string, reqBody, respBody interface{}) (http.Header, error) { jsonBytes, err := json.Marshal(reqBody) if err != nil { - return nil, errors.New("Failed to marshal network message") + return nil, errors.New("failed to marshal network message") } resp, err := j.post(uri, jsonBytes) if err != nil { - return nil, fmt.Errorf("Failed to post JWS message. -> %v", err) + return nil, fmt.Errorf("failed to post JWS message. -> %v", err) } defer resp.Body.Close() if resp.StatusCode >= http.StatusBadRequest { - - err := handleHTTPError(resp) - + err = handleHTTPError(resp) switch err.(type) { - case NonceError: - // Retry once if the nonce was invalidated - retryResp, err := j.post(uri, jsonBytes) - if err != nil { - return nil, fmt.Errorf("Failed to post JWS message. -> %v", err) + retryResp, errP := j.post(uri, jsonBytes) + if errP != nil { + return nil, fmt.Errorf("failed to post JWS message. -> %v", errP) } defer retryResp.Body.Close() diff --git a/vendor/github.com/xenolf/lego/acme/http_challenge_server.go b/vendor/github.com/xenolf/lego/acme/http_challenge_server.go index 319e26185..9c595d7f5 100644 --- a/vendor/github.com/xenolf/lego/acme/http_challenge_server.go +++ b/vendor/github.com/xenolf/lego/acme/http_challenge_server.go @@ -35,7 +35,7 @@ func (s *HTTPProviderServer) Present(domain, token, keyAuth string) error { var err error s.listener, err = net.Listen("tcp", net.JoinHostPort(s.iface, s.port)) if err != nil { - return fmt.Errorf("Could not start HTTP server for challenge -> %v", err) + return fmt.Errorf("could not start HTTP server for challenge -> %v", err) } s.done = make(chan bool) @@ -62,20 +62,31 @@ func (s *HTTPProviderServer) serve(domain, token, keyAuth string) { mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { if strings.HasPrefix(r.Host, domain) && r.Method == http.MethodGet { w.Header().Add("Content-Type", "text/plain") - w.Write([]byte(keyAuth)) + _, err := w.Write([]byte(keyAuth)) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } log.Infof("[%s] Served key authentication", domain) } else { log.Warnf("Received request for domain %s with method %s but the domain did not match any challenge. Please ensure your are passing the HOST header properly.", r.Host, r.Method) - w.Write([]byte("TEST")) + _, err := w.Write([]byte("TEST")) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } } }) - httpServer := &http.Server{ - Handler: mux, - } + httpServer := &http.Server{Handler: mux} + // Once httpServer is shut down we don't want any lingering // connections, so disable KeepAlives. httpServer.SetKeepAlivesEnabled(false) - httpServer.Serve(s.listener) + + err := httpServer.Serve(s.listener) + if err != nil { + log.Println(err) + } s.done <- true } diff --git a/vendor/github.com/xenolf/lego/acme/tls_alpn_challenge_server.go b/vendor/github.com/xenolf/lego/acme/tls_alpn_challenge_server.go index 8d33668e1..ee06a16d4 100644 --- a/vendor/github.com/xenolf/lego/acme/tls_alpn_challenge_server.go +++ b/vendor/github.com/xenolf/lego/acme/tls_alpn_challenge_server.go @@ -3,6 +3,7 @@ package acme import ( "crypto/tls" "fmt" + "log" "net" "net/http" ) @@ -65,7 +66,10 @@ func (t *TLSALPNProviderServer) Present(domain, token, keyAuth string) error { // Shut the server down when we're finished. go func() { - http.Serve(t.listener, nil) + err := http.Serve(t.listener, nil) + if err != nil { + log.Println(err) + } }() return nil diff --git a/vendor/github.com/xenolf/lego/acme/utils.go b/vendor/github.com/xenolf/lego/acme/utils.go index 2fa0db304..f3160806d 100644 --- a/vendor/github.com/xenolf/lego/acme/utils.go +++ b/vendor/github.com/xenolf/lego/acme/utils.go @@ -3,16 +3,20 @@ package acme import ( "fmt" "time" + + "github.com/xenolf/lego/log" ) // WaitFor polls the given function 'f', once every 'interval', up to 'timeout'. func WaitFor(timeout, interval time.Duration, f func() (bool, error)) error { + log.Infof("Wait [timeout: %s, interval: %s]", timeout, interval) + var lastErr string - timeup := time.After(timeout) + timeUp := time.After(timeout) for { select { - case <-timeup: - return fmt.Errorf("Time limit exceeded. Last error: %s", lastErr) + case <-timeUp: + return fmt.Errorf("time limit exceeded: last error: %s", lastErr) default: } diff --git a/vendor/github.com/xenolf/lego/platform/config/env/env.go b/vendor/github.com/xenolf/lego/platform/config/env/env.go index 4da1b4b60..aaae6eae7 100644 --- a/vendor/github.com/xenolf/lego/platform/config/env/env.go +++ b/vendor/github.com/xenolf/lego/platform/config/env/env.go @@ -1,11 +1,15 @@ package env import ( + "errors" "fmt" + "io/ioutil" "os" "strconv" "strings" "time" + + "github.com/xenolf/lego/log" ) // Get environment variables @@ -14,7 +18,7 @@ func Get(names ...string) (map[string]string, error) { var missingEnvVars []string for _, envVar := range names { - value := os.Getenv(envVar) + value := GetOrFile(envVar) if value == "" { missingEnvVars = append(missingEnvVars, envVar) } @@ -28,10 +32,72 @@ func Get(names ...string) (map[string]string, error) { return values, nil } +// GetWithFallback Get environment variable values +// The first name in each group is use as key in the result map +// +// // LEGO_ONE="ONE" +// // LEGO_TWO="TWO" +// env.GetWithFallback([]string{"LEGO_ONE", "LEGO_TWO"}) +// // => "LEGO_ONE" = "ONE" +// +// ---- +// +// // LEGO_ONE="" +// // LEGO_TWO="TWO" +// env.GetWithFallback([]string{"LEGO_ONE", "LEGO_TWO"}) +// // => "LEGO_ONE" = "TWO" +// +// ---- +// +// // LEGO_ONE="" +// // LEGO_TWO="" +// env.GetWithFallback([]string{"LEGO_ONE", "LEGO_TWO"}) +// // => error +// +func GetWithFallback(groups ...[]string) (map[string]string, error) { + values := map[string]string{} + + var missingEnvVars []string + for _, names := range groups { + if len(names) == 0 { + return nil, errors.New("undefined environment variable names") + } + + value, envVar := getOneWithFallback(names[0], names[1:]...) + if len(value) == 0 { + missingEnvVars = append(missingEnvVars, envVar) + continue + } + values[envVar] = value + } + + if len(missingEnvVars) > 0 { + return nil, fmt.Errorf("some credentials information are missing: %s", strings.Join(missingEnvVars, ",")) + } + + return values, nil +} + +func getOneWithFallback(main string, names ...string) (string, string) { + value := GetOrFile(main) + if len(value) > 0 { + return value, main + } + + for _, name := range names { + value := GetOrFile(name) + if len(value) > 0 { + return value, main + } + } + + return "", main +} + // GetOrDefaultInt returns the given environment variable value as an integer. // Returns the default if the envvar cannot be coopered to an int, or is not found. func GetOrDefaultInt(envVar string, defaultValue int) int { - v, err := strconv.Atoi(os.Getenv(envVar)) + v, err := strconv.Atoi(GetOrFile(envVar)) if err != nil { return defaultValue } @@ -53,7 +119,7 @@ func GetOrDefaultSecond(envVar string, defaultValue time.Duration) time.Duration // GetOrDefaultString returns the given environment variable value as a string. // Returns the default if the envvar cannot be find. func GetOrDefaultString(envVar string, defaultValue string) string { - v := os.Getenv(envVar) + v := GetOrFile(envVar) if len(v) == 0 { return defaultValue } @@ -64,10 +130,34 @@ func GetOrDefaultString(envVar string, defaultValue string) string { // GetOrDefaultBool returns the given environment variable value as a boolean. // Returns the default if the envvar cannot be coopered to a boolean, or is not found. func GetOrDefaultBool(envVar string, defaultValue bool) bool { - v, err := strconv.ParseBool(os.Getenv(envVar)) + v, err := strconv.ParseBool(GetOrFile(envVar)) if err != nil { return defaultValue } return v } + +// GetOrFile Attempts to resolve 'key' as an environment variable. +// Failing that, it will check to see if '_FILE' exists. +// If so, it will attempt to read from the referenced file to populate a value. +func GetOrFile(envVar string) string { + envVarValue := os.Getenv(envVar) + if envVarValue != "" { + return envVarValue + } + + fileVar := envVar + "_FILE" + fileVarValue := os.Getenv(fileVar) + if fileVarValue == "" { + return envVarValue + } + + fileContents, err := ioutil.ReadFile(fileVarValue) + if err != nil { + log.Printf("Failed to read the file %s (defined by env var %s): %s", fileVarValue, fileVar, err) + return "" + } + + return string(fileContents) +} diff --git a/vendor/github.com/xenolf/lego/providers/dns/acmedns/acmedns.go b/vendor/github.com/xenolf/lego/providers/dns/acmedns/acmedns.go index cce0d8d87..9ad0ef369 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/acmedns/acmedns.go +++ b/vendor/github.com/xenolf/lego/providers/dns/acmedns/acmedns.go @@ -1,6 +1,5 @@ -// Package acmedns implements a DNS provider for solving DNS-01 challenges using -// Joohoi's acme-dns project. For more information see the ACME-DNS homepage: -// https://github.com/joohoi/acme-dns +// Package acmedns implements a DNS provider for solving DNS-01 challenges using Joohoi's acme-dns project. +// For more information see the ACME-DNS homepage: https://github.com/joohoi/acme-dns package acmedns import ( @@ -101,7 +100,7 @@ func (e ErrCNAMERequired) Error() string { e.Domain, e.Domain, e.FQDN, e.Target) } -// Present creates a TXT record to fulfil the DNS-01 challenge. If there is an +// Present creates a TXT record to fulfill the DNS-01 challenge. If there is an // existing account for the domain in the provider's storage then it will be // used to set the challenge response TXT record with the ACME-DNS server and // issuance will continue. If there is not an account for the given domain diff --git a/vendor/github.com/xenolf/lego/providers/dns/alidns/alidns.go b/vendor/github.com/xenolf/lego/providers/dns/alidns/alidns.go index 2d3c38b64..41aeb905b 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/alidns/alidns.go +++ b/vendor/github.com/xenolf/lego/providers/dns/alidns/alidns.go @@ -5,7 +5,6 @@ package alidns import ( "errors" "fmt" - "os" "strings" "time" @@ -57,7 +56,7 @@ func NewDNSProvider() (*DNSProvider, error) { config := NewDefaultConfig() config.APIKey = values["ALICLOUD_ACCESS_KEY"] config.SecretKey = values["ALICLOUD_SECRET_KEY"] - config.RegionID = os.Getenv("ALICLOUD_REGION_ID") + config.RegionID = env.GetOrFile("ALICLOUD_REGION_ID") return NewDNSProviderConfig(config) } @@ -105,7 +104,7 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { return d.config.PropagationTimeout, d.config.PollingInterval } -// Present creates a TXT record to fulfil the dns-01 challenge. +// Present creates a TXT record to fulfill the dns-01 challenge. func (d *DNSProvider) Present(domain, token, keyAuth string) error { fqdn, value, _ := acme.DNS01Record(domain, keyAuth) diff --git a/vendor/github.com/xenolf/lego/providers/dns/auroradns/auroradns.go b/vendor/github.com/xenolf/lego/providers/dns/auroradns/auroradns.go index 5ddbc7af1..d3cb76da9 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/auroradns/auroradns.go +++ b/vendor/github.com/xenolf/lego/providers/dns/auroradns/auroradns.go @@ -3,7 +3,6 @@ package auroradns import ( "errors" "fmt" - "os" "sync" "time" @@ -53,7 +52,7 @@ func NewDNSProvider() (*DNSProvider, error) { } config := NewDefaultConfig() - config.BaseURL = os.Getenv("AURORA_ENDPOINT") + config.BaseURL = env.GetOrFile("AURORA_ENDPOINT") config.UserID = values["AURORA_USER_ID"] config.Key = values["AURORA_KEY"] diff --git a/vendor/github.com/xenolf/lego/providers/dns/azure/azure.go b/vendor/github.com/xenolf/lego/providers/dns/azure/azure.go index 8012aa659..d04c3d668 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/azure/azure.go +++ b/vendor/github.com/xenolf/lego/providers/dns/azure/azure.go @@ -99,7 +99,7 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { return d.config.PropagationTimeout, d.config.PollingInterval } -// Present creates a TXT record to fulfil the dns-01 challenge +// Present creates a TXT record to fulfill the dns-01 challenge func (d *DNSProvider) Present(domain, token, keyAuth string) error { ctx := context.Background() fqdn, value, _ := acme.DNS01Record(domain, keyAuth) diff --git a/vendor/github.com/xenolf/lego/providers/dns/bluecat/bluecat.go b/vendor/github.com/xenolf/lego/providers/dns/bluecat/bluecat.go index cc4369ba4..fabfdae3b 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/bluecat/bluecat.go +++ b/vendor/github.com/xenolf/lego/providers/dns/bluecat/bluecat.go @@ -61,7 +61,7 @@ type DNSProvider struct { // The REST endpoint will be appended. In addition, the Configuration name // and external DNS View Name must be passed in BLUECAT_CONFIG_NAME and BLUECAT_DNS_VIEW func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("BLUECAT_SERVER_URL", "BLUECAT_USER_NAME", "BLUECAT_CONFIG_NAME", "BLUECAT_CONFIG_NAME", "BLUECAT_DNS_VIEW") + values, err := env.Get("BLUECAT_SERVER_URL", "BLUECAT_USER_NAME", "BLUECAT_PASSWORD", "BLUECAT_CONFIG_NAME", "BLUECAT_DNS_VIEW") if err != nil { return nil, fmt.Errorf("bluecat: %v", err) } @@ -353,7 +353,7 @@ func (d *DNSProvider) lookupViewID(viewName string) (uint, error) { queryArgs := map[string]string{ "parentId": strconv.FormatUint(uint64(confID), 10), - "name": d.config.DNSView, + "name": viewName, "type": viewType, } diff --git a/vendor/github.com/xenolf/lego/providers/dns/cloudflare/client.go b/vendor/github.com/xenolf/lego/providers/dns/cloudflare/client.go deleted file mode 100644 index 24c4ae0f8..000000000 --- a/vendor/github.com/xenolf/lego/providers/dns/cloudflare/client.go +++ /dev/null @@ -1,212 +0,0 @@ -package cloudflare - -import ( - "bytes" - "encoding/json" - "errors" - "fmt" - "io" - "io/ioutil" - "net/http" - - "github.com/xenolf/lego/acme" -) - -// defaultBaseURL represents the API endpoint to call. -const defaultBaseURL = "https://api.cloudflare.com/client/v4" - -// APIError contains error details for failed requests -type APIError struct { - Code int `json:"code,omitempty"` - Message string `json:"message,omitempty"` - ErrorChain []APIError `json:"error_chain,omitempty"` -} - -// APIResponse represents a response from Cloudflare API -type APIResponse struct { - Success bool `json:"success"` - Errors []*APIError `json:"errors"` - Result json.RawMessage `json:"result"` -} - -// TxtRecord represents a Cloudflare DNS record -type TxtRecord struct { - Name string `json:"name"` - Type string `json:"type"` - Content string `json:"content"` - ID string `json:"id,omitempty"` - TTL int `json:"ttl,omitempty"` - ZoneID string `json:"zone_id,omitempty"` -} - -// HostedZone represents a Cloudflare DNS zone -type HostedZone struct { - ID string `json:"id"` - Name string `json:"name"` -} - -// Client Cloudflare API client -type Client struct { - authEmail string - authKey string - BaseURL string - HTTPClient *http.Client -} - -// NewClient create a Cloudflare API client -func NewClient(authEmail string, authKey string) (*Client, error) { - if authEmail == "" { - return nil, errors.New("cloudflare: some credentials information are missing: email") - } - - if authKey == "" { - return nil, errors.New("cloudflare: some credentials information are missing: key") - } - - return &Client{ - authEmail: authEmail, - authKey: authKey, - BaseURL: defaultBaseURL, - HTTPClient: http.DefaultClient, - }, nil -} - -// GetHostedZoneID get hosted zone -func (c *Client) GetHostedZoneID(fqdn string) (string, error) { - authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers) - if err != nil { - return "", err - } - - result, err := c.doRequest(http.MethodGet, "/zones?name="+acme.UnFqdn(authZone), nil) - if err != nil { - return "", err - } - - var hostedZone []HostedZone - err = json.Unmarshal(result, &hostedZone) - if err != nil { - return "", fmt.Errorf("cloudflare: HostedZone unmarshaling error: %v", err) - } - - count := len(hostedZone) - if count == 0 { - return "", fmt.Errorf("cloudflare: zone %s not found for domain %s", authZone, fqdn) - } else if count > 1 { - return "", fmt.Errorf("cloudflare: zone %s cannot be find for domain %s: too many hostedZone: %v", authZone, fqdn, hostedZone) - } - - return hostedZone[0].ID, nil -} - -// FindTxtRecord Find a TXT record -func (c *Client) FindTxtRecord(zoneID, fqdn string) (*TxtRecord, error) { - result, err := c.doRequest( - http.MethodGet, - fmt.Sprintf("/zones/%s/dns_records?per_page=1000&type=TXT&name=%s", zoneID, acme.UnFqdn(fqdn)), - nil, - ) - if err != nil { - return nil, err - } - - var records []TxtRecord - err = json.Unmarshal(result, &records) - if err != nil { - return nil, fmt.Errorf("cloudflare: record unmarshaling error: %v", err) - } - - for _, rec := range records { - fmt.Println(rec.Name, acme.UnFqdn(fqdn)) - if rec.Name == acme.UnFqdn(fqdn) { - return &rec, nil - } - } - - return nil, fmt.Errorf("cloudflare: no existing record found for %s", fqdn) -} - -// AddTxtRecord add a TXT record -func (c *Client) AddTxtRecord(fqdn string, record TxtRecord) error { - zoneID, err := c.GetHostedZoneID(fqdn) - if err != nil { - return err - } - - body, err := json.Marshal(record) - if err != nil { - return fmt.Errorf("cloudflare: record marshaling error: %v", err) - } - - _, err = c.doRequest(http.MethodPost, fmt.Sprintf("/zones/%s/dns_records", zoneID), bytes.NewReader(body)) - return err -} - -// RemoveTxtRecord Remove a TXT record -func (c *Client) RemoveTxtRecord(fqdn string) error { - zoneID, err := c.GetHostedZoneID(fqdn) - if err != nil { - return err - } - - record, err := c.FindTxtRecord(zoneID, fqdn) - if err != nil { - return err - } - - _, err = c.doRequest(http.MethodDelete, fmt.Sprintf("/zones/%s/dns_records/%s", record.ZoneID, record.ID), nil) - return err -} - -func (c *Client) doRequest(method, uri string, body io.Reader) (json.RawMessage, error) { - req, err := http.NewRequest(method, fmt.Sprintf("%s%s", c.BaseURL, uri), body) - if err != nil { - return nil, err - } - - req.Header.Set("X-Auth-Email", c.authEmail) - req.Header.Set("X-Auth-Key", c.authKey) - - resp, err := c.HTTPClient.Do(req) - if err != nil { - return nil, fmt.Errorf("cloudflare: error querying API: %v", err) - } - - defer resp.Body.Close() - - content, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, fmt.Errorf("cloudflare: %s", toUnreadableBodyMessage(req, content)) - } - - var r APIResponse - err = json.Unmarshal(content, &r) - if err != nil { - return nil, fmt.Errorf("cloudflare: APIResponse unmarshaling error: %v: %s", err, toUnreadableBodyMessage(req, content)) - } - - if !r.Success { - if len(r.Errors) > 0 { - return nil, fmt.Errorf("cloudflare: error \n%s", toError(r)) - } - - return nil, fmt.Errorf("cloudflare: %s", toUnreadableBodyMessage(req, content)) - } - - return r.Result, nil -} - -func toUnreadableBodyMessage(req *http.Request, rawBody []byte) string { - return fmt.Sprintf("the request %s sent a response with a body which is an invalid format: %q", req.URL, string(rawBody)) -} - -func toError(r APIResponse) error { - errStr := "" - for _, apiErr := range r.Errors { - errStr += fmt.Sprintf("\t Error: %d: %s", apiErr.Code, apiErr.Message) - for _, chainErr := range apiErr.ErrorChain { - errStr += fmt.Sprintf("<- %d: %s", chainErr.Code, chainErr.Message) - } - } - return fmt.Errorf("cloudflare: error \n%s", errStr) -} diff --git a/vendor/github.com/xenolf/lego/providers/dns/cloudflare/cloudflare.go b/vendor/github.com/xenolf/lego/providers/dns/cloudflare/cloudflare.go index 25d6fef3c..33c2065e0 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/cloudflare/cloudflare.go +++ b/vendor/github.com/xenolf/lego/providers/dns/cloudflare/cloudflare.go @@ -8,12 +8,18 @@ import ( "net/http" "time" + "github.com/cloudflare/cloudflare-go" "github.com/xenolf/lego/acme" + "github.com/xenolf/lego/log" "github.com/xenolf/lego/platform/config/env" ) // CloudFlareAPIURL represents the API endpoint to call. -const CloudFlareAPIURL = defaultBaseURL // Deprecated +const CloudFlareAPIURL = "https://api.cloudflare.com/client/v4" // Deprecated + +const ( + minTTL = 120 +) // Config is used to configure the creation of the DNSProvider type Config struct { @@ -28,7 +34,7 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("CLOUDFLARE_TTL", 120), + TTL: env.GetOrDefaultInt("CLOUDFLARE_TTL", minTTL), PropagationTimeout: env.GetOrDefaultSecond("CLOUDFLARE_PROPAGATION_TIMEOUT", 2*time.Minute), PollingInterval: env.GetOrDefaultSecond("CLOUDFLARE_POLLING_INTERVAL", 2*time.Second), HTTPClient: &http.Client{ @@ -39,7 +45,7 @@ func NewDefaultConfig() *Config { // DNSProvider is an implementation of the acme.ChallengeProvider interface type DNSProvider struct { - client *Client + client *cloudflare.API config *Config } @@ -47,7 +53,9 @@ type DNSProvider struct { // Credentials must be passed in the environment variables: // CLOUDFLARE_EMAIL and CLOUDFLARE_API_KEY. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("CLOUDFLARE_EMAIL", "CLOUDFLARE_API_KEY") + values, err := env.GetWithFallback( + []string{"CLOUDFLARE_EMAIL", "CF_API_EMAIL"}, + []string{"CLOUDFLARE_API_KEY", "CF_API_KEY"}) if err != nil { return nil, fmt.Errorf("cloudflare: %v", err) } @@ -76,45 +84,92 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { return nil, errors.New("cloudflare: the configuration of the DNS provider is nil") } - client, err := NewClient(config.AuthEmail, config.AuthKey) + if config.TTL < minTTL { + return nil, fmt.Errorf("cloudflare: invalid TTL, TTL (%d) must be greater than %d", config.TTL, minTTL) + } + + client, err := cloudflare.New(config.AuthKey, config.AuthEmail, cloudflare.HTTPClient(config.HTTPClient)) if err != nil { return nil, err } - client.HTTPClient = config.HTTPClient - // TODO: must be remove. keep only for compatibility reason. client.BaseURL = CloudFlareAPIURL - return &DNSProvider{ - client: client, - config: config, - }, nil + return &DNSProvider{client: client, config: config}, nil } -// Timeout returns the timeout and interval to use when checking for DNS -// propagation. Adjusting here to cope with spikes in propagation times. +// Timeout returns the timeout and interval to use when checking for DNS propagation. +// Adjusting here to cope with spikes in propagation times. func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { return d.config.PropagationTimeout, d.config.PollingInterval } -// Present creates a TXT record to fulfil the dns-01 challenge +// Present creates a TXT record to fulfill the dns-01 challenge func (d *DNSProvider) Present(domain, token, keyAuth string) error { fqdn, value, _ := acme.DNS01Record(domain, keyAuth) - rec := TxtRecord{ + authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers) + if err != nil { + return fmt.Errorf("cloudflare: %v", err) + } + + zoneID, err := d.client.ZoneIDByName(acme.UnFqdn(authZone)) + if err != nil { + return fmt.Errorf("cloudflare: failed to find zone %s: %v", authZone, err) + } + + dnsRecord := cloudflare.DNSRecord{ Type: "TXT", Name: acme.UnFqdn(fqdn), Content: value, TTL: d.config.TTL, } - return d.client.AddTxtRecord(fqdn, rec) + response, _ := d.client.CreateDNSRecord(zoneID, dnsRecord) + if err != nil { + return fmt.Errorf("cloudflare: failed to create TXT record: %v", err) + } + + if !response.Success { + return fmt.Errorf("cloudflare: failed to create TXT record: %+v %+v", response.Errors, response.Messages) + } + + log.Infof("cloudflare: new record for %s, ID %s", domain, response.Result.ID) + + return nil } // CleanUp removes the TXT record matching the specified parameters func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { fqdn, _, _ := acme.DNS01Record(domain, keyAuth) - return d.client.RemoveTxtRecord(fqdn) + authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers) + if err != nil { + return fmt.Errorf("cloudflare: %v", err) + } + + zoneID, err := d.client.ZoneIDByName(acme.UnFqdn(authZone)) + if err != nil { + return fmt.Errorf("cloudflare: failed to find zone %s: %v", authZone, err) + } + + dnsRecord := cloudflare.DNSRecord{ + Type: "TXT", + Name: acme.UnFqdn(fqdn), + } + + records, err := d.client.DNSRecords(zoneID, dnsRecord) + if err != nil { + return fmt.Errorf("cloudflare: failed to find TXT records: %v", err) + } + + for _, record := range records { + err = d.client.DeleteDNSRecord(zoneID, record.ID) + if err != nil { + log.Printf("cloudflare: failed to delete TXT record: %v", err) + } + } + + return nil } diff --git a/vendor/github.com/xenolf/lego/providers/dns/cloudxns/cloudxns.go b/vendor/github.com/xenolf/lego/providers/dns/cloudxns/cloudxns.go index 4b73564a8..6f4bb6bd7 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/cloudxns/cloudxns.go +++ b/vendor/github.com/xenolf/lego/providers/dns/cloudxns/cloudxns.go @@ -28,8 +28,8 @@ func NewDefaultConfig() *Config { client.Timeout = time.Second * time.Duration(env.GetOrDefaultInt("CLOUDXNS_HTTP_TIMEOUT", 30)) return &Config{ - PropagationTimeout: env.GetOrDefaultSecond("AKAMAI_PROPAGATION_TIMEOUT", acme.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("AKAMAI_POLLING_INTERVAL", acme.DefaultPollingInterval), + PropagationTimeout: env.GetOrDefaultSecond("CLOUDXNS_PROPAGATION_TIMEOUT", acme.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond("CLOUDXNS_POLLING_INTERVAL", acme.DefaultPollingInterval), TTL: env.GetOrDefaultInt("CLOUDXNS_TTL", 120), HTTPClient: &client, } @@ -76,10 +76,10 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { client.HTTPClient = config.HTTPClient - return &DNSProvider{client: client}, nil + return &DNSProvider{client: client, config: config}, nil } -// Present creates a TXT record to fulfil the dns-01 challenge. +// Present creates a TXT record to fulfill the dns-01 challenge. func (d *DNSProvider) Present(domain, token, keyAuth string) error { fqdn, value, _ := acme.DNS01Record(domain, keyAuth) diff --git a/vendor/github.com/xenolf/lego/providers/dns/dns_providers.go b/vendor/github.com/xenolf/lego/providers/dns/dns_providers.go index 38a1fd4c2..ff7a7d833 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/dns_providers.go +++ b/vendor/github.com/xenolf/lego/providers/dns/dns_providers.go @@ -15,6 +15,7 @@ import ( "github.com/xenolf/lego/providers/dns/dnsimple" "github.com/xenolf/lego/providers/dns/dnsmadeeasy" "github.com/xenolf/lego/providers/dns/dnspod" + "github.com/xenolf/lego/providers/dns/dreamhost" "github.com/xenolf/lego/providers/dns/duckdns" "github.com/xenolf/lego/providers/dns/dyn" "github.com/xenolf/lego/providers/dns/exec" @@ -29,6 +30,7 @@ import ( "github.com/xenolf/lego/providers/dns/iij" "github.com/xenolf/lego/providers/dns/lightsail" "github.com/xenolf/lego/providers/dns/linode" + "github.com/xenolf/lego/providers/dns/linodev4" "github.com/xenolf/lego/providers/dns/namecheap" "github.com/xenolf/lego/providers/dns/namedotcom" "github.com/xenolf/lego/providers/dns/netcup" @@ -41,6 +43,7 @@ import ( "github.com/xenolf/lego/providers/dns/rfc2136" "github.com/xenolf/lego/providers/dns/route53" "github.com/xenolf/lego/providers/dns/sakuracloud" + "github.com/xenolf/lego/providers/dns/stackpath" "github.com/xenolf/lego/providers/dns/vegadns" "github.com/xenolf/lego/providers/dns/vultr" ) @@ -70,12 +73,16 @@ func NewDNSChallengeProviderByName(name string) (acme.ChallengeProvider, error) return dnsmadeeasy.NewDNSProvider() case "dnspod": return dnspod.NewDNSProvider() + case "dreamhost": + return dreamhost.NewDNSProvider() case "duckdns": return duckdns.NewDNSProvider() case "dyn": return dyn.NewDNSProvider() case "fastdns": return fastdns.NewDNSProvider() + case "exec": + return exec.NewDNSProvider() case "exoscale": return exoscale.NewDNSProvider() case "gandi": @@ -96,6 +103,8 @@ func NewDNSChallengeProviderByName(name string) (acme.ChallengeProvider, error) return lightsail.NewDNSProvider() case "linode": return linode.NewDNSProvider() + case "linodev4": + return linodev4.NewDNSProvider() case "manual": return acme.NewDNSProviderManual() case "namecheap": @@ -106,6 +115,14 @@ func NewDNSChallengeProviderByName(name string) (acme.ChallengeProvider, error) return netcup.NewDNSProvider() case "nifcloud": return nifcloud.NewDNSProvider() + case "ns1": + return ns1.NewDNSProvider() + case "otc": + return otc.NewDNSProvider() + case "ovh": + return ovh.NewDNSProvider() + case "pdns": + return pdns.NewDNSProvider() case "rackspace": return rackspace.NewDNSProvider() case "route53": @@ -114,20 +131,12 @@ func NewDNSChallengeProviderByName(name string) (acme.ChallengeProvider, error) return rfc2136.NewDNSProvider() case "sakuracloud": return sakuracloud.NewDNSProvider() - case "vultr": - return vultr.NewDNSProvider() - case "ovh": - return ovh.NewDNSProvider() - case "pdns": - return pdns.NewDNSProvider() - case "ns1": - return ns1.NewDNSProvider() - case "otc": - return otc.NewDNSProvider() - case "exec": - return exec.NewDNSProvider() + case "stackpath": + return stackpath.NewDNSProvider() case "vegadns": return vegadns.NewDNSProvider() + case "vultr": + return vultr.NewDNSProvider() default: return nil, fmt.Errorf("unrecognised DNS provider: %s", name) } diff --git a/vendor/github.com/xenolf/lego/providers/dns/dnsimple/dnsimple.go b/vendor/github.com/xenolf/lego/providers/dns/dnsimple/dnsimple.go index c9c9ce3df..8f52cac6b 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/dnsimple/dnsimple.go +++ b/vendor/github.com/xenolf/lego/providers/dns/dnsimple/dnsimple.go @@ -5,7 +5,6 @@ package dnsimple import ( "errors" "fmt" - "os" "strconv" "strings" "time" @@ -45,8 +44,8 @@ type DNSProvider struct { // See: https://developer.dnsimple.com/v2/#authentication func NewDNSProvider() (*DNSProvider, error) { config := NewDefaultConfig() - config.AccessToken = os.Getenv("DNSIMPLE_OAUTH_TOKEN") - config.BaseURL = os.Getenv("DNSIMPLE_BASE_URL") + config.AccessToken = env.GetOrFile("DNSIMPLE_OAUTH_TOKEN") + config.BaseURL = env.GetOrFile("DNSIMPLE_BASE_URL") return NewDNSProviderConfig(config) } @@ -79,27 +78,27 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { client.BaseURL = config.BaseURL } - return &DNSProvider{client: client}, nil + return &DNSProvider{client: client, config: config}, nil } -// Present creates a TXT record to fulfil the dns-01 challenge. +// Present creates a TXT record to fulfill the dns-01 challenge. func (d *DNSProvider) Present(domain, token, keyAuth string) error { fqdn, value, _ := acme.DNS01Record(domain, keyAuth) zoneName, err := d.getHostedZone(domain) if err != nil { - return err + return fmt.Errorf("dnsimple: %v", err) } accountID, err := d.getAccountID() if err != nil { - return err + return fmt.Errorf("dnsimple: %v", err) } - recordAttributes := d.newTxtRecord(zoneName, fqdn, value, d.config.TTL) + recordAttributes := newTxtRecord(zoneName, fqdn, value, d.config.TTL) _, err = d.client.Zones.CreateRecord(accountID, zoneName, recordAttributes) if err != nil { - return fmt.Errorf("API call failed: %v", err) + return fmt.Errorf("dnsimple: API call failed: %v", err) } return nil @@ -111,22 +110,23 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { records, err := d.findTxtRecords(domain, fqdn) if err != nil { - return err + return fmt.Errorf("dnsimple: %v", err) } accountID, err := d.getAccountID() if err != nil { - return err + return fmt.Errorf("dnsimple: %v", err) } + var lastErr error for _, rec := range records { _, err := d.client.Zones.DeleteRecord(accountID, rec.ZoneID, rec.ID) if err != nil { - return err + lastErr = fmt.Errorf("dnsimple: %v", err) } } - return nil + return lastErr } // Timeout returns the timeout and interval to use when checking for DNS propagation. @@ -178,18 +178,18 @@ func (d *DNSProvider) findTxtRecords(domain, fqdn string) ([]dnsimple.ZoneRecord return nil, err } - recordName := d.extractRecordName(fqdn, zoneName) + recordName := extractRecordName(fqdn, zoneName) result, err := d.client.Zones.ListRecords(accountID, zoneName, &dnsimple.ZoneRecordListOptions{Name: recordName, Type: "TXT", ListOptions: dnsimple.ListOptions{}}) if err != nil { - return []dnsimple.ZoneRecord{}, fmt.Errorf("API call has failed: %v", err) + return nil, fmt.Errorf("API call has failed: %v", err) } return result.Data, nil } -func (d *DNSProvider) newTxtRecord(zoneName, fqdn, value string, ttl int) dnsimple.ZoneRecord { - name := d.extractRecordName(fqdn, zoneName) +func newTxtRecord(zoneName, fqdn, value string, ttl int) dnsimple.ZoneRecord { + name := extractRecordName(fqdn, zoneName) return dnsimple.ZoneRecord{ Type: "TXT", @@ -199,7 +199,7 @@ func (d *DNSProvider) newTxtRecord(zoneName, fqdn, value string, ttl int) dnsimp } } -func (d *DNSProvider) extractRecordName(fqdn, domain string) string { +func extractRecordName(fqdn, domain string) string { name := acme.UnFqdn(fqdn) if idx := strings.Index(name, "."+domain); idx != -1 { return name[:idx] diff --git a/vendor/github.com/xenolf/lego/providers/dns/dnsmadeeasy/client.go b/vendor/github.com/xenolf/lego/providers/dns/dnsmadeeasy/client.go index 54e87dccb..ba0727b9e 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/dnsmadeeasy/client.go +++ b/vendor/github.com/xenolf/lego/providers/dns/dnsmadeeasy/client.go @@ -38,11 +38,11 @@ type Client struct { // NewClient creates a DNSMadeEasy client func NewClient(apiKey string, apiSecret string) (*Client, error) { if apiKey == "" { - return nil, fmt.Errorf("DNSMadeEasy: credentials missing: API key") + return nil, fmt.Errorf("credentials missing: API key") } if apiSecret == "" { - return nil, fmt.Errorf("DNSMadeEasy: credentials missing: API secret") + return nil, fmt.Errorf("credentials missing: API secret") } return &Client{ diff --git a/vendor/github.com/xenolf/lego/providers/dns/dnsmadeeasy/dnsmadeeasy.go b/vendor/github.com/xenolf/lego/providers/dns/dnsmadeeasy/dnsmadeeasy.go index 262a30203..519a44519 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/dnsmadeeasy/dnsmadeeasy.go +++ b/vendor/github.com/xenolf/lego/providers/dns/dnsmadeeasy/dnsmadeeasy.go @@ -5,8 +5,6 @@ import ( "errors" "fmt" "net/http" - "os" - "strconv" "strings" "time" @@ -19,6 +17,7 @@ type Config struct { BaseURL string APIKey string APISecret string + Sandbox bool HTTPClient *http.Client PropagationTimeout time.Duration PollingInterval time.Duration @@ -56,15 +55,8 @@ func NewDNSProvider() (*DNSProvider, error) { return nil, fmt.Errorf("dnsmadeeasy: %v", err) } - var baseURL string - if sandbox, _ := strconv.ParseBool(os.Getenv("DNSMADEEASY_SANDBOX")); sandbox { - baseURL = "https://api.sandbox.dnsmadeeasy.com/V2.0" - } else { - baseURL = "https://api.dnsmadeeasy.com/V2.0" - } - config := NewDefaultConfig() - config.BaseURL = baseURL + config.Sandbox = env.GetOrDefaultBool("DNSMADEEASY_SANDBOX", false) config.APIKey = values["DNSMADEEASY_API_KEY"] config.APISecret = values["DNSMADEEASY_API_SECRET"] @@ -89,8 +81,15 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { return nil, errors.New("dnsmadeeasy: the configuration of the DNS provider is nil") } - if config.BaseURL == "" { - return nil, fmt.Errorf("dnsmadeeasy: base URL missing") + var baseURL string + if config.Sandbox { + baseURL = "https://api.sandbox.dnsmadeeasy.com/V2.0" + } else { + if len(config.BaseURL) > 0 { + baseURL = config.BaseURL + } else { + baseURL = "https://api.dnsmadeeasy.com/V2.0" + } } client, err := NewClient(config.APIKey, config.APISecret) @@ -99,7 +98,7 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { } client.HTTPClient = config.HTTPClient - client.BaseURL = config.BaseURL + client.BaseURL = baseURL return &DNSProvider{ client: client, diff --git a/vendor/github.com/xenolf/lego/providers/dns/dnspod/dnspod.go b/vendor/github.com/xenolf/lego/providers/dns/dnspod/dnspod.go index 35b748689..1aa8d9e97 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/dnspod/dnspod.go +++ b/vendor/github.com/xenolf/lego/providers/dns/dnspod/dnspod.go @@ -28,8 +28,8 @@ type Config struct { func NewDefaultConfig() *Config { return &Config{ TTL: env.GetOrDefaultInt("DNSPOD_TTL", 600), - PropagationTimeout: env.GetOrDefaultSecond("ALICLOUD_PROPAGATION_TIMEOUT", acme.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("ALICLOUD_POLLING_INTERVAL", acme.DefaultPollingInterval), + PropagationTimeout: env.GetOrDefaultSecond("DNSPOD_PROPAGATION_TIMEOUT", acme.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond("DNSPOD_POLLING_INTERVAL", acme.DefaultPollingInterval), HTTPClient: &http.Client{ Timeout: env.GetOrDefaultSecond("DNSPOD_HTTP_TIMEOUT", 0), }, @@ -81,10 +81,10 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { client := dnspod.NewClient(params) client.HttpClient = config.HTTPClient - return &DNSProvider{client: client}, nil + return &DNSProvider{client: client, config: config}, nil } -// Present creates a TXT record to fulfil the dns-01 challenge. +// Present creates a TXT record to fulfill the dns-01 challenge. func (d *DNSProvider) Present(domain, token, keyAuth string) error { fqdn, value, _ := acme.DNS01Record(domain, keyAuth) zoneID, zoneName, err := d.getHostedZone(domain) diff --git a/vendor/github.com/xenolf/lego/providers/dns/dreamhost/client.go b/vendor/github.com/xenolf/lego/providers/dns/dreamhost/client.go new file mode 100644 index 000000000..640f9604e --- /dev/null +++ b/vendor/github.com/xenolf/lego/providers/dns/dreamhost/client.go @@ -0,0 +1,73 @@ +package dreamhost + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/url" + + "github.com/xenolf/lego/log" +) + +const ( + defaultBaseURL = "https://api.dreamhost.com" + + cmdAddRecord = "dns-add_record" + cmdRemoveRecord = "dns-remove_record" +) + +type apiResponse struct { + Data string `json:"data"` + Result string `json:"result"` +} + +func (d *DNSProvider) buildQuery(action, domain, txt string) (*url.URL, error) { + u, err := url.Parse(d.config.BaseURL) + if err != nil { + return nil, err + } + + query := u.Query() + query.Set("key", d.config.APIKey) + query.Set("cmd", action) + query.Set("format", "json") + query.Set("record", domain) + query.Set("type", "TXT") + query.Set("value", txt) + query.Set("comment", url.QueryEscape("Managed By lego")) + u.RawQuery = query.Encode() + + return u, nil +} + +// updateTxtRecord will either add or remove a TXT record. +// action is either cmdAddRecord or cmdRemoveRecord +func (d *DNSProvider) updateTxtRecord(u fmt.Stringer) error { + resp, err := d.config.HTTPClient.Get(u.String()) + if err != nil { + return err + } + defer resp.Body.Close() + + if resp.StatusCode != 200 { + return fmt.Errorf("request failed with HTTP status code %d", resp.StatusCode) + } + + raw, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("failed to read body: %v", err) + } + + var response apiResponse + err = json.Unmarshal(raw, &response) + if err != nil { + return fmt.Errorf("unable to decode API server response: %v: %s", err, string(raw)) + } + + if response.Result == "error" { + return fmt.Errorf("add TXT record failed: %s", response.Data) + } + + log.Infof("dreamhost: %s", response.Data) + return nil +} diff --git a/vendor/github.com/xenolf/lego/providers/dns/dreamhost/dreamhost.go b/vendor/github.com/xenolf/lego/providers/dns/dreamhost/dreamhost.go new file mode 100644 index 000000000..cc0e36338 --- /dev/null +++ b/vendor/github.com/xenolf/lego/providers/dns/dreamhost/dreamhost.go @@ -0,0 +1,111 @@ +// Package dreamhost Adds lego support for http://dreamhost.com DNS updates +// See https://help.dreamhost.com/hc/en-us/articles/217560167-API_overview +// and https://help.dreamhost.com/hc/en-us/articles/217555707-DNS-API-commands for the API spec. +package dreamhost + +import ( + "errors" + "fmt" + "net/http" + "time" + + "github.com/xenolf/lego/acme" + "github.com/xenolf/lego/platform/config/env" +) + +// Config is used to configure the creation of the DNSProvider +type Config struct { + BaseURL string + APIKey string + PropagationTimeout time.Duration + PollingInterval time.Duration + HTTPClient *http.Client +} + +// NewDefaultConfig returns a default configuration for the DNSProvider +func NewDefaultConfig() *Config { + return &Config{ + BaseURL: defaultBaseURL, + PropagationTimeout: env.GetOrDefaultSecond("DREAMHOST_PROPAGATION_TIMEOUT", 60*time.Minute), + PollingInterval: env.GetOrDefaultSecond("DREAMHOST_POLLING_INTERVAL", 1*time.Minute), + HTTPClient: &http.Client{ + Timeout: env.GetOrDefaultSecond("DREAMHOST_HTTP_TIMEOUT", 30*time.Second), + }, + } +} + +// DNSProvider adds and removes the record for the DNS challenge +type DNSProvider struct { + config *Config +} + +// NewDNSProvider returns a new DNS provider using +// environment variable DREAMHOST_TOKEN for adding and removing the DNS record. +func NewDNSProvider() (*DNSProvider, error) { + values, err := env.Get("DREAMHOST_API_KEY") + if err != nil { + return nil, fmt.Errorf("dreamhost: %v", err) + } + + config := NewDefaultConfig() + config.APIKey = values["DREAMHOST_API_KEY"] + + return NewDNSProviderConfig(config) +} + +// NewDNSProviderConfig return a DNSProvider instance configured for DreamHost. +func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { + if config == nil { + return nil, errors.New("dreamhost: the configuration of the DNS provider is nil") + } + + if config.APIKey == "" { + return nil, errors.New("dreamhost: credentials missing") + } + + if config.BaseURL == "" { + config.BaseURL = defaultBaseURL + } + + return &DNSProvider{config: config}, nil +} + +// Present creates a TXT record to fulfill the dns-01 challenge. +func (d *DNSProvider) Present(domain, token, keyAuth string) error { + fqdn, value, _ := acme.DNS01Record(domain, keyAuth) + record := acme.UnFqdn(fqdn) + + u, err := d.buildQuery(cmdAddRecord, record, value) + if err != nil { + return fmt.Errorf("dreamhost: %v", err) + } + + err = d.updateTxtRecord(u) + if err != nil { + return fmt.Errorf("dreamhost: %v", err) + } + return nil +} + +// CleanUp clears DreamHost TXT record +func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { + fqdn, value, _ := acme.DNS01Record(domain, keyAuth) + record := acme.UnFqdn(fqdn) + + u, err := d.buildQuery(cmdRemoveRecord, record, value) + if err != nil { + return fmt.Errorf("dreamhost: %v", err) + } + + err = d.updateTxtRecord(u) + if err != nil { + return fmt.Errorf("dreamhost: %v", err) + } + return nil +} + +// Timeout returns the timeout and interval to use when checking for DNS propagation. +// Adjusting here to cope with spikes in propagation times. +func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { + return d.config.PropagationTimeout, d.config.PollingInterval +} diff --git a/vendor/github.com/xenolf/lego/providers/dns/duckdns/duckdns.go b/vendor/github.com/xenolf/lego/providers/dns/duckdns/duckdns.go index 7e2a55a41..6bbf76352 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/duckdns/duckdns.go +++ b/vendor/github.com/xenolf/lego/providers/dns/duckdns/duckdns.go @@ -75,7 +75,7 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { return &DNSProvider{config: config}, nil } -// Present creates a TXT record to fulfil the dns-01 challenge. +// Present creates a TXT record to fulfill the dns-01 challenge. func (d *DNSProvider) Present(domain, token, keyAuth string) error { _, txtRecord, _ := acme.DNS01Record(domain, keyAuth) return updateTxtRecord(domain, d.config.Token, txtRecord, false) diff --git a/vendor/github.com/xenolf/lego/providers/dns/exec/exec.go b/vendor/github.com/xenolf/lego/providers/dns/exec/exec.go index ea3a43983..b8ff0c7da 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/exec/exec.go +++ b/vendor/github.com/xenolf/lego/providers/dns/exec/exec.go @@ -59,7 +59,7 @@ func NewDNSProviderProgram(program string) (*DNSProvider, error) { return NewDNSProviderConfig(&Config{Program: program}) } -// Present creates a TXT record to fulfil the dns-01 challenge. +// Present creates a TXT record to fulfill the dns-01 challenge. func (d *DNSProvider) Present(domain, token, keyAuth string) error { var args []string if d.config.Mode == "RAW" { diff --git a/vendor/github.com/xenolf/lego/providers/dns/exoscale/exoscale.go b/vendor/github.com/xenolf/lego/providers/dns/exoscale/exoscale.go index 72015d36e..4ffcc6e71 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/exoscale/exoscale.go +++ b/vendor/github.com/xenolf/lego/providers/dns/exoscale/exoscale.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "net/http" - "os" "time" "github.com/exoscale/egoscale" @@ -56,7 +55,7 @@ func NewDNSProvider() (*DNSProvider, error) { config := NewDefaultConfig() config.APIKey = values["EXOSCALE_API_KEY"] config.APISecret = values["EXOSCALE_API_SECRET"] - config.Endpoint = os.Getenv("EXOSCALE_ENDPOINT") + config.Endpoint = env.GetOrFile("EXOSCALE_ENDPOINT") return NewDNSProviderConfig(config) } @@ -93,7 +92,7 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { return &DNSProvider{client: client, config: config}, nil } -// Present creates a TXT record to fulfil the dns-01 challenge. +// Present creates a TXT record to fulfill the dns-01 challenge. func (d *DNSProvider) Present(domain, token, keyAuth string) error { fqdn, value, _ := acme.DNS01Record(domain, keyAuth) zone, recordName, err := d.FindZoneAndRecordName(fqdn, domain) diff --git a/vendor/github.com/xenolf/lego/providers/dns/fastdns/fastdns.go b/vendor/github.com/xenolf/lego/providers/dns/fastdns/fastdns.go index fb8a9133a..9ef6cb0ef 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/fastdns/fastdns.go +++ b/vendor/github.com/xenolf/lego/providers/dns/fastdns/fastdns.go @@ -77,7 +77,7 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { } if config.ClientToken == "" || config.ClientSecret == "" || config.AccessToken == "" || config.Host == "" { - return nil, fmt.Errorf("FastDNS credentials are missing") + return nil, fmt.Errorf("fastdns: credentials are missing") } return &DNSProvider{config: config}, nil @@ -99,10 +99,10 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error { } record := configdns.NewTxtRecord() - record.SetField("name", recordName) - record.SetField("ttl", d.config.TTL) - record.SetField("target", value) - record.SetField("active", true) + _ = record.SetField("name", recordName) + _ = record.SetField("ttl", d.config.TTL) + _ = record.SetField("target", value) + _ = record.SetField("active", true) existingRecord := d.findExistingRecord(zone, recordName) @@ -110,8 +110,10 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error { if reflect.DeepEqual(existingRecord.ToMap(), record.ToMap()) { return nil } - zone.RemoveRecord(existingRecord) - return d.createRecord(zone, record) + err = zone.RemoveRecord(existingRecord) + if err != nil { + return fmt.Errorf("fastdns: %v", err) + } } return d.createRecord(zone, record) diff --git a/vendor/github.com/xenolf/lego/providers/dns/gandi/client.go b/vendor/github.com/xenolf/lego/providers/dns/gandi/client.go index 3af340ecb..0ed0bee44 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/gandi/client.go +++ b/vendor/github.com/xenolf/lego/providers/dns/gandi/client.go @@ -82,8 +82,6 @@ type responseBool struct { Value bool `xml:"params>param>value>boolean"` } -// POSTing/Marshalling/Unmarshalling - type rpcError struct { faultCode int faultString string diff --git a/vendor/github.com/xenolf/lego/providers/dns/gandi/gandi.go b/vendor/github.com/xenolf/lego/providers/dns/gandi/gandi.go index 1ac1a6fab..f282d28b3 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/gandi/gandi.go +++ b/vendor/github.com/xenolf/lego/providers/dns/gandi/gandi.go @@ -230,9 +230,9 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { } // rpcCall makes an XML-RPC call to Gandi's RPC endpoint by -// marshalling the data given in the call argument to XML and sending -// that via HTTP Post to Gandi. The response is then unmarshalled into -// the resp argument. +// marshaling the data given in the call argument to XML and sending +// that via HTTP Post to Gandi. +// The response is then unmarshalled into the resp argument. func (d *DNSProvider) rpcCall(call *methodCall, resp response) error { // marshal b, err := xml.MarshalIndent(call, "", " ") diff --git a/vendor/github.com/xenolf/lego/providers/dns/gandiv5/client.go b/vendor/github.com/xenolf/lego/providers/dns/gandiv5/client.go index 0116fdef3..e342007f1 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/gandiv5/client.go +++ b/vendor/github.com/xenolf/lego/providers/dns/gandiv5/client.go @@ -1,18 +1,123 @@ package gandiv5 -// types for JSON method calls and parameters +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" +) -type addFieldRequest struct { +const apiKeyHeader = "X-Api-Key" + +// types for JSON responses with only a message +type apiResponse struct { + Message string `json:"message"` + UUID string `json:"uuid,omitempty"` +} + +// Record TXT record representation +type Record struct { RRSetTTL int `json:"rrset_ttl"` RRSetValues []string `json:"rrset_values"` + RRSetName string `json:"rrset_name,omitempty"` + RRSetType string `json:"rrset_type,omitempty"` } -type deleteFieldRequest struct { - Delete bool `json:"delete"` +func (d *DNSProvider) newRequest(method, resource string, body interface{}) (*http.Request, error) { + u := fmt.Sprintf("%s/%s", d.config.BaseURL, resource) + + if body == nil { + req, err := http.NewRequest(method, u, nil) + if err != nil { + return nil, err + } + + return req, nil + } + + reqBody, err := json.Marshal(body) + if err != nil { + return nil, err + } + + req, err := http.NewRequest(method, u, bytes.NewBuffer(reqBody)) + if err != nil { + return nil, err + } + + req.Header.Set("Content-Type", "application/json") + + return req, nil } -// types for JSON responses +func (d *DNSProvider) do(req *http.Request, v interface{}) error { + if len(d.config.APIKey) > 0 { + req.Header.Set(apiKeyHeader, d.config.APIKey) + } -type responseStruct struct { - Message string `json:"message"` + resp, err := d.config.HTTPClient.Do(req) + if err != nil { + return err + } + + err = checkResponse(resp) + if err != nil { + return err + } + + if v == nil { + return nil + } + + raw, err := readBody(resp) + if err != nil { + return fmt.Errorf("failed to read body: %v", err) + } + + if len(raw) > 0 { + err = json.Unmarshal(raw, v) + if err != nil { + return fmt.Errorf("unmarshaling error: %v: %s", err, string(raw)) + } + } + + return nil +} + +func checkResponse(resp *http.Response) error { + if resp.StatusCode == 404 && resp.Request.Method == http.MethodGet { + return nil + } + + if resp.StatusCode >= 400 { + data, err := readBody(resp) + if err != nil { + return fmt.Errorf("%d [%s] request failed: %v", resp.StatusCode, http.StatusText(resp.StatusCode), err) + } + + message := &apiResponse{} + err = json.Unmarshal(data, message) + if err != nil { + return fmt.Errorf("%d [%s] request failed: %v: %s", resp.StatusCode, http.StatusText(resp.StatusCode), err, data) + } + return fmt.Errorf("%d [%s] request failed: %s", resp.StatusCode, http.StatusText(resp.StatusCode), message.Message) + } + + return nil +} + +func readBody(resp *http.Response) ([]byte, error) { + if resp.Body == nil { + return nil, fmt.Errorf("response body is nil") + } + + defer resp.Body.Close() + + rawBody, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + return rawBody, nil } diff --git a/vendor/github.com/xenolf/lego/providers/dns/gandiv5/gandiv5.go b/vendor/github.com/xenolf/lego/providers/dns/gandiv5/gandiv5.go index 054f71d56..0962d159c 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/gandiv5/gandiv5.go +++ b/vendor/github.com/xenolf/lego/providers/dns/gandiv5/gandiv5.go @@ -3,8 +3,6 @@ package gandiv5 import ( - "bytes" - "encoding/json" "errors" "fmt" "net/http" @@ -104,6 +102,10 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { config.BaseURL = defaultBaseURL } + if config.TTL < minTTL { + return nil, fmt.Errorf("gandiv5: invalid TTL, TTL (%d) must be greater than %d", config.TTL, minTTL) + } + return &DNSProvider{ config: config, inProgressFQDNs: make(map[string]inProgressInfo), @@ -114,10 +116,6 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { func (d *DNSProvider) Present(domain, token, keyAuth string) error { fqdn, value, _ := acme.DNS01Record(domain, keyAuth) - if d.config.TTL < minTTL { - d.config.TTL = minTTL // 300 is gandi minimum value for ttl - } - // find authZone authZone, err := findZoneByFqdn(fqdn, acme.RecursiveNameservers) if err != nil { @@ -184,61 +182,75 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { // functions to perform API actions func (d *DNSProvider) addTXTRecord(domain string, name string, value string, ttl int) error { - target := fmt.Sprintf("domains/%s/records/%s/TXT", domain, name) - response, err := d.sendRequest(http.MethodPut, target, addFieldRequest{ - RRSetTTL: ttl, - RRSetValues: []string{value}, - }) - if response != nil { - log.Infof("gandiv5: %s", response.Message) + // Get exiting values for the TXT records + // Needed to create challenges for both wildcard and base name domains + txtRecord, err := d.getTXTRecord(domain, name) + if err != nil { + return err } - return err + + values := []string{value} + if len(txtRecord.RRSetValues) > 0 { + values = append(values, txtRecord.RRSetValues...) + } + + target := fmt.Sprintf("domains/%s/records/%s/TXT", domain, name) + + newRecord := &Record{RRSetTTL: ttl, RRSetValues: values} + req, err := d.newRequest(http.MethodPut, target, newRecord) + if err != nil { + return err + } + + message := &apiResponse{} + err = d.do(req, message) + if err != nil { + return fmt.Errorf("unable to create TXT record for domain %s and name %s: %v", domain, name, err) + } + + if message != nil && len(message.Message) > 0 { + log.Infof("API response: %s", message.Message) + } + + return nil +} + +func (d *DNSProvider) getTXTRecord(domain, name string) (*Record, error) { + target := fmt.Sprintf("domains/%s/records/%s/TXT", domain, name) + + // Get exiting values for the TXT records + // Needed to create challenges for both wildcard and base name domains + req, err := d.newRequest(http.MethodGet, target, nil) + if err != nil { + return nil, err + } + + txtRecord := &Record{} + err = d.do(req, txtRecord) + if err != nil { + return nil, fmt.Errorf("unable to get TXT records for domain %s and name %s: %v", domain, name, err) + } + + return txtRecord, nil } func (d *DNSProvider) deleteTXTRecord(domain string, name string) error { target := fmt.Sprintf("domains/%s/records/%s/TXT", domain, name) - response, err := d.sendRequest(http.MethodDelete, target, deleteFieldRequest{ - Delete: true, - }) - if response != nil && response.Message == "" { - log.Infof("gandiv5: Zone record deleted") + + req, err := d.newRequest(http.MethodDelete, target, nil) + if err != nil { + return err } - return err -} - -func (d *DNSProvider) sendRequest(method string, resource string, payload interface{}) (*responseStruct, error) { - url := fmt.Sprintf("%s/%s", d.config.BaseURL, resource) - - body, err := json.Marshal(payload) - if err != nil { - return nil, err - } - - req, err := http.NewRequest(method, url, bytes.NewReader(body)) - if err != nil { - return nil, err - } - - req.Header.Set("Content-Type", "application/json") - if len(d.config.APIKey) > 0 { - req.Header.Set("X-Api-Key", d.config.APIKey) - } - - resp, err := d.config.HTTPClient.Do(req) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - if resp.StatusCode >= 400 { - return nil, fmt.Errorf("request failed with HTTP status code %d", resp.StatusCode) - } - - var response responseStruct - err = json.NewDecoder(resp.Body).Decode(&response) - if err != nil && method != http.MethodDelete { - return nil, err - } - - return &response, nil + + message := &apiResponse{} + err = d.do(req, message) + if err != nil { + return fmt.Errorf("unable to delete TXT record for domain %s and name %s: %v", domain, name, err) + } + + if message != nil && len(message.Message) > 0 { + log.Infof("API response: %s", message.Message) + } + + return nil } diff --git a/vendor/github.com/xenolf/lego/providers/dns/gcloud/googlecloud.go b/vendor/github.com/xenolf/lego/providers/dns/gcloud/googlecloud.go index d8df7071d..f799461b3 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/gcloud/googlecloud.go +++ b/vendor/github.com/xenolf/lego/providers/dns/gcloud/googlecloud.go @@ -122,7 +122,7 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { return &DNSProvider{config: config, client: svc}, nil } -// Present creates a TXT record to fulfil the dns-01 challenge. +// Present creates a TXT record to fulfill the dns-01 challenge. func (d *DNSProvider) Present(domain, token, keyAuth string) error { fqdn, value, _ := acme.DNS01Record(domain, keyAuth) diff --git a/vendor/github.com/xenolf/lego/providers/dns/glesys/glesys.go b/vendor/github.com/xenolf/lego/providers/dns/glesys/glesys.go index 78a0cff1a..4aab8744d 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/glesys/glesys.go +++ b/vendor/github.com/xenolf/lego/providers/dns/glesys/glesys.go @@ -93,7 +93,12 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { return nil, fmt.Errorf("glesys: incomplete credentials provided") } + if config.TTL < minTTL { + return nil, fmt.Errorf("glesys: invalid TTL, TTL (%d) must be greater than %d", config.TTL, minTTL) + } + return &DNSProvider{ + config: config, activeRecords: make(map[string]int), }, nil } @@ -102,9 +107,6 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { func (d *DNSProvider) Present(domain, token, keyAuth string) error { fqdn, value, _ := acme.DNS01Record(domain, keyAuth) - if d.config.TTL < minTTL { - d.config.TTL = minTTL // 60 is GleSYS minimum value for ttl - } // find authZone authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers) if err != nil { @@ -160,8 +162,6 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { return d.config.PropagationTimeout, d.config.PollingInterval } -// POSTing/Marshalling/Unmarshalling - func (d *DNSProvider) sendRequest(method string, resource string, payload interface{}) (*responseStruct, error) { url := fmt.Sprintf("%s/%s", defaultBaseURL, resource) diff --git a/vendor/github.com/xenolf/lego/providers/dns/godaddy/godaddy.go b/vendor/github.com/xenolf/lego/providers/dns/godaddy/godaddy.go index 7c5d48a6c..8aebde650 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/godaddy/godaddy.go +++ b/vendor/github.com/xenolf/lego/providers/dns/godaddy/godaddy.go @@ -86,6 +86,10 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { return nil, fmt.Errorf("godaddy: credentials missing") } + if config.TTL < minTTL { + return nil, fmt.Errorf("godaddy: invalid TTL, TTL (%d) must be greater than %d", config.TTL, minTTL) + } + return &DNSProvider{config: config}, nil } @@ -103,7 +107,7 @@ func (d *DNSProvider) extractRecordName(fqdn, domain string) string { return name } -// Present creates a TXT record to fulfil the dns-01 challenge +// Present creates a TXT record to fulfill the dns-01 challenge func (d *DNSProvider) Present(domain, token, keyAuth string) error { fqdn, value, _ := acme.DNS01Record(domain, keyAuth) domainZone, err := d.getZone(fqdn) @@ -111,10 +115,6 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error { return err } - if d.config.TTL < minTTL { - d.config.TTL = minTTL - } - recordName := d.extractRecordName(fqdn, domainZone) rec := []DNSRecord{ { diff --git a/vendor/github.com/xenolf/lego/providers/dns/hostingde/hostingde.go b/vendor/github.com/xenolf/lego/providers/dns/hostingde/hostingde.go index 7f8280aae..7a64b1b89 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/hostingde/hostingde.go +++ b/vendor/github.com/xenolf/lego/providers/dns/hostingde/hostingde.go @@ -89,7 +89,7 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { return d.config.PropagationTimeout, d.config.PollingInterval } -// Present creates a TXT record to fulfil the dns-01 challenge +// Present creates a TXT record to fulfill the dns-01 challenge func (d *DNSProvider) Present(domain, token, keyAuth string) error { fqdn, value, _ := acme.DNS01Record(domain, keyAuth) diff --git a/vendor/github.com/xenolf/lego/providers/dns/lightsail/lightsail.go b/vendor/github.com/xenolf/lego/providers/dns/lightsail/lightsail.go index 4c116dbb1..3b3fe6783 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/lightsail/lightsail.go +++ b/vendor/github.com/xenolf/lego/providers/dns/lightsail/lightsail.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "math/rand" - "os" "time" "github.com/aws/aws-sdk-go/aws" @@ -54,7 +53,7 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - DNSZone: os.Getenv("DNS_ZONE"), + DNSZone: env.GetOrFile("DNS_ZONE"), PropagationTimeout: env.GetOrDefaultSecond("LIGHTSAIL_PROPAGATION_TIMEOUT", acme.DefaultPropagationTimeout), PollingInterval: env.GetOrDefaultSecond("LIGHTSAIL_POLLING_INTERVAL", acme.DefaultPollingInterval), Region: env.GetOrDefaultString("LIGHTSAIL_REGION", "us-east-1"), @@ -108,7 +107,7 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { func (d *DNSProvider) Present(domain, token, keyAuth string) error { fqdn, value, _ := acme.DNS01Record(domain, keyAuth) - err := d.newTxtRecord(domain, fqdn, `"`+value+`"`) + err := d.newTxtRecord(fqdn, `"`+value+`"`) if err != nil { return fmt.Errorf("lightsail: %v", err) } @@ -141,7 +140,7 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { return d.config.PropagationTimeout, d.config.PollingInterval } -func (d *DNSProvider) newTxtRecord(domain string, fqdn string, value string) error { +func (d *DNSProvider) newTxtRecord(fqdn string, value string) error { params := &lightsail.CreateDomainEntryInput{ DomainName: aws.String(d.config.DNSZone), DomainEntry: &lightsail.DomainEntry{ diff --git a/vendor/github.com/xenolf/lego/providers/dns/linode/linode.go b/vendor/github.com/xenolf/lego/providers/dns/linode/linode.go index 48739c127..09b92e994 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/linode/linode.go +++ b/vendor/github.com/xenolf/lego/providers/dns/linode/linode.go @@ -14,7 +14,7 @@ import ( ) const ( - dnsMinTTLSecs = 300 + minTTL = 300 dnsUpdateFreqMins = 15 dnsUpdateFudgeSecs = 120 ) @@ -30,7 +30,7 @@ type Config struct { func NewDefaultConfig() *Config { return &Config{ PollingInterval: env.GetOrDefaultSecond("LINODE_POLLING_INTERVAL", 15*time.Second), - TTL: env.GetOrDefaultInt("LINODE_TTL", 60), + TTL: env.GetOrDefaultInt("LINODE_TTL", minTTL), } } @@ -79,6 +79,10 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { return nil, errors.New("linode: credentials missing") } + if config.TTL < minTTL { + return nil, fmt.Errorf("linode: invalid TTL, TTL (%d) must be greater than %d", config.TTL, minTTL) + } + return &DNSProvider{ config: config, client: dns.New(config.APIKey), @@ -96,7 +100,7 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { minsRemaining := dnsUpdateFreqMins - (time.Now().Minute() % dnsUpdateFreqMins) timeout = (time.Duration(minsRemaining) * time.Minute) + - (dnsMinTTLSecs * time.Second) + + (minTTL * time.Second) + (dnsUpdateFudgeSecs * time.Second) interval = d.config.PollingInterval return @@ -110,7 +114,7 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error { return err } - if _, err = d.client.CreateDomainResourceTXT(zone.domainID, acme.UnFqdn(fqdn), value, 60); err != nil { + if _, err = d.client.CreateDomainResourceTXT(zone.domainID, acme.UnFqdn(fqdn), value, d.config.TTL); err != nil { return err } @@ -138,6 +142,7 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { if err != nil { return err } + if resp.ResourceID != resource.ResourceID { return errors.New("error deleting resource: resource IDs do not match") } diff --git a/vendor/github.com/xenolf/lego/providers/dns/linodev4/linodev4.go b/vendor/github.com/xenolf/lego/providers/dns/linodev4/linodev4.go new file mode 100644 index 000000000..97903e9cf --- /dev/null +++ b/vendor/github.com/xenolf/lego/providers/dns/linodev4/linodev4.go @@ -0,0 +1,191 @@ +// Package linodev4 implements a DNS provider for solving the DNS-01 challenge +// using Linode DNS and Linode's APIv4 +package linodev4 + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "net/http" + "strings" + "time" + + "github.com/linode/linodego" + "github.com/xenolf/lego/acme" + "github.com/xenolf/lego/platform/config/env" + "golang.org/x/oauth2" +) + +const ( + minTTL = 300 + dnsUpdateFreqMins = 15 + dnsUpdateFudgeSecs = 120 +) + +// Config is used to configure the creation of the DNSProvider +type Config struct { + Token string + PollingInterval time.Duration + TTL int + HTTPTimeout time.Duration +} + +// NewDefaultConfig returns a default configuration for the DNSProvider +func NewDefaultConfig() *Config { + return &Config{ + PollingInterval: env.GetOrDefaultSecond("LINODE_POLLING_INTERVAL", 15*time.Second), + TTL: env.GetOrDefaultInt("LINODE_TTL", minTTL), + HTTPTimeout: env.GetOrDefaultSecond("LINODE_HTTP_TIMEOUT", 0), + } +} + +type hostedZoneInfo struct { + domainID int + resourceName string +} + +// DNSProvider implements the acme.ChallengeProvider interface. +type DNSProvider struct { + config *Config + client *linodego.Client +} + +// NewDNSProvider returns a DNSProvider instance configured for Linode. +// Credentials must be passed in the environment variable: LINODE_TOKEN. +func NewDNSProvider() (*DNSProvider, error) { + values, err := env.Get("LINODE_TOKEN") + if err != nil { + return nil, fmt.Errorf("linodev4: %v", err) + } + + config := NewDefaultConfig() + config.Token = values["LINODE_TOKEN"] + + return NewDNSProviderConfig(config) +} + +// NewDNSProviderConfig return a DNSProvider instance configured for Linode. +func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { + if config == nil { + return nil, errors.New("linodev4: the configuration of the DNS provider is nil") + } + + if len(config.Token) == 0 { + return nil, errors.New("linodev4: Linode Access Token missing") + } + + if config.TTL < minTTL { + return nil, fmt.Errorf("linodev4: invalid TTL, TTL (%d) must be greater than %d", config.TTL, minTTL) + } + + tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: config.Token}) + oauth2Client := &http.Client{ + Timeout: config.HTTPTimeout, + Transport: &oauth2.Transport{ + Source: tokenSource, + }, + } + + client := linodego.NewClient(oauth2Client) + client.SetUserAgent(fmt.Sprintf("lego-dns linodego/%s", linodego.Version)) + + return &DNSProvider{ + config: config, + client: &client, + }, nil +} + +// Timeout returns the timeout and interval to use when checking for DNS +// propagation. Adjusting here to cope with spikes in propagation times. +func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { + // Since Linode only updates their zone files every X minutes, we need + // to figure out how many minutes we have to wait until we hit the next + // interval of X. We then wait another couple of minutes, just to be + // safe. Hopefully at some point during all of this, the record will + // have propagated throughout Linode's network. + minsRemaining := dnsUpdateFreqMins - (time.Now().Minute() % dnsUpdateFreqMins) + + timeout = (time.Duration(minsRemaining) * time.Minute) + + (minTTL * time.Second) + + (dnsUpdateFudgeSecs * time.Second) + interval = d.config.PollingInterval + return +} + +// Present creates a TXT record using the specified parameters. +func (d *DNSProvider) Present(domain, token, keyAuth string) error { + fqdn, value, _ := acme.DNS01Record(domain, keyAuth) + zone, err := d.getHostedZoneInfo(fqdn) + if err != nil { + return err + } + + createOpts := linodego.DomainRecordCreateOptions{ + Name: acme.UnFqdn(fqdn), + Target: value, + TTLSec: d.config.TTL, + Type: linodego.RecordTypeTXT, + } + + _, err = d.client.CreateDomainRecord(context.Background(), zone.domainID, createOpts) + return err +} + +// CleanUp removes the TXT record matching the specified parameters. +func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { + fqdn, value, _ := acme.DNS01Record(domain, keyAuth) + + zone, err := d.getHostedZoneInfo(fqdn) + if err != nil { + return err + } + + // Get all TXT records for the specified domain. + listOpts := linodego.NewListOptions(0, "{\"type\":\"TXT\"}") + resources, err := d.client.ListDomainRecords(context.Background(), zone.domainID, listOpts) + if err != nil { + return err + } + + // Remove the specified resource, if it exists. + for _, resource := range resources { + if (resource.Name == strings.TrimSuffix(fqdn, ".") || resource.Name == zone.resourceName) && + resource.Target == value { + if err := d.client.DeleteDomainRecord(context.Background(), zone.domainID, resource.ID); err != nil { + return err + } + } + } + + return nil +} + +func (d *DNSProvider) getHostedZoneInfo(fqdn string) (*hostedZoneInfo, error) { + // Lookup the zone that handles the specified FQDN. + authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers) + if err != nil { + return nil, err + } + + // Query the authority zone. + data, err := json.Marshal(map[string]string{"domain": acme.UnFqdn(authZone)}) + if err != nil { + return nil, err + } + + listOpts := linodego.NewListOptions(0, string(data)) + domains, err := d.client.ListDomains(context.Background(), listOpts) + if err != nil { + return nil, err + } + + if len(domains) == 0 { + return nil, fmt.Errorf("domain not found") + } + + return &hostedZoneInfo{ + domainID: domains[0].ID, + resourceName: strings.TrimSuffix(fqdn, "."+authZone), + }, nil +} diff --git a/vendor/github.com/xenolf/lego/providers/dns/namecheap/client.go b/vendor/github.com/xenolf/lego/providers/dns/namecheap/client.go index b816e1b4d..d52f65509 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/namecheap/client.go +++ b/vendor/github.com/xenolf/lego/providers/dns/namecheap/client.go @@ -1,11 +1,18 @@ package namecheap -import "encoding/xml" +import ( + "encoding/xml" + "fmt" + "io/ioutil" + "net/http" + "net/url" + "strings" +) -// host describes a DNS record returned by the Namecheap DNS gethosts API. +// Record describes a DNS record returned by the Namecheap DNS gethosts API. // Namecheap uses the term "host" to refer to all DNS records that include // a host field (A, AAAA, CNAME, NS, TXT, URL). -type host struct { +type Record struct { Type string `xml:",attr"` Name string `xml:",attr"` Address string `xml:",attr"` @@ -32,7 +39,7 @@ type getHostsResponse struct { XMLName xml.Name `xml:"ApiResponse"` Status string `xml:"Status,attr"` Errors []apierror `xml:"Errors>Error"` - Hosts []host `xml:"CommandResponse>DomainDNSGetHostsResult>host"` + Hosts []Record `xml:"CommandResponse>DomainDNSGetHostsResult>host"` } type getTldsResponse struct { @@ -42,3 +49,177 @@ type getTldsResponse struct { Name string `xml:",attr"` } `xml:"CommandResponse>Tlds>Tld"` } + +// getTLDs requests the list of available TLDs. +// https://www.namecheap.com/support/api/methods/domains/get-tld-list.aspx +func (d *DNSProvider) getTLDs() (map[string]string, error) { + request, err := d.newRequestGet("namecheap.domains.getTldList") + if err != nil { + return nil, err + } + + var gtr getTldsResponse + err = d.do(request, >r) + if err != nil { + return nil, err + } + + if len(gtr.Errors) > 0 { + return nil, fmt.Errorf("%s [%d]", gtr.Errors[0].Description, gtr.Errors[0].Number) + } + + tlds := make(map[string]string) + for _, t := range gtr.Result { + tlds[t.Name] = t.Name + } + return tlds, nil +} + +// getHosts reads the full list of DNS host records. +// https://www.namecheap.com/support/api/methods/domains-dns/get-hosts.aspx +func (d *DNSProvider) getHosts(sld, tld string) ([]Record, error) { + request, err := d.newRequestGet("namecheap.domains.dns.getHosts", + addParam("SLD", sld), + addParam("TLD", tld), + ) + if err != nil { + return nil, err + } + + var ghr getHostsResponse + err = d.do(request, &ghr) + if err != nil { + return nil, err + } + + if len(ghr.Errors) > 0 { + return nil, fmt.Errorf("%s [%d]", ghr.Errors[0].Description, ghr.Errors[0].Number) + } + + return ghr.Hosts, nil +} + +// setHosts writes the full list of DNS host records . +// https://www.namecheap.com/support/api/methods/domains-dns/set-hosts.aspx +func (d *DNSProvider) setHosts(sld, tld string, hosts []Record) error { + req, err := d.newRequestPost("namecheap.domains.dns.setHosts", + addParam("SLD", sld), + addParam("TLD", tld), + func(values url.Values) { + for i, h := range hosts { + ind := fmt.Sprintf("%d", i+1) + values.Add("HostName"+ind, h.Name) + values.Add("RecordType"+ind, h.Type) + values.Add("Address"+ind, h.Address) + values.Add("MXPref"+ind, h.MXPref) + values.Add("TTL"+ind, h.TTL) + } + }, + ) + if err != nil { + return err + } + + var shr setHostsResponse + err = d.do(req, &shr) + if err != nil { + return err + } + + if len(shr.Errors) > 0 { + return fmt.Errorf("%s [%d]", shr.Errors[0].Description, shr.Errors[0].Number) + } + if shr.Result.IsSuccess != "true" { + return fmt.Errorf("setHosts failed") + } + + return nil +} + +func (d *DNSProvider) do(req *http.Request, out interface{}) error { + resp, err := d.config.HTTPClient.Do(req) + if err != nil { + return err + } + + if resp.StatusCode >= 400 { + var body []byte + body, err = readBody(resp) + if err != nil { + return fmt.Errorf("HTTP error %d [%s]: %v", resp.StatusCode, http.StatusText(resp.StatusCode), err) + } + return fmt.Errorf("HTTP error %d [%s]: %s", resp.StatusCode, http.StatusText(resp.StatusCode), string(body)) + } + + body, err := readBody(resp) + if err != nil { + return err + } + + if err := xml.Unmarshal(body, out); err != nil { + return err + } + + return nil +} + +func (d *DNSProvider) newRequestGet(cmd string, params ...func(url.Values)) (*http.Request, error) { + query := d.makeQuery(cmd, params...) + + reqURL, err := url.Parse(d.config.BaseURL) + if err != nil { + return nil, err + } + + reqURL.RawQuery = query.Encode() + + return http.NewRequest(http.MethodGet, reqURL.String(), nil) +} + +func (d *DNSProvider) newRequestPost(cmd string, params ...func(url.Values)) (*http.Request, error) { + query := d.makeQuery(cmd, params...) + + req, err := http.NewRequest(http.MethodPost, d.config.BaseURL, strings.NewReader(query.Encode())) + if err != nil { + return nil, err + } + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + + return req, nil +} + +func (d *DNSProvider) makeQuery(cmd string, params ...func(url.Values)) url.Values { + queryParams := make(url.Values) + queryParams.Set("ApiUser", d.config.APIUser) + queryParams.Set("ApiKey", d.config.APIKey) + queryParams.Set("UserName", d.config.APIUser) + queryParams.Set("Command", cmd) + queryParams.Set("ClientIp", d.config.ClientIP) + + for _, param := range params { + param(queryParams) + } + + return queryParams +} + +func addParam(key, value string) func(url.Values) { + return func(values url.Values) { + values.Set(key, value) + } +} + +func readBody(resp *http.Response) ([]byte, error) { + if resp.Body == nil { + return nil, fmt.Errorf("response body is nil") + } + + defer resp.Body.Close() + + rawBody, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + return rawBody, nil +} diff --git a/vendor/github.com/xenolf/lego/providers/dns/namecheap/namecheap.go b/vendor/github.com/xenolf/lego/providers/dns/namecheap/namecheap.go index c50cc628f..731699ac4 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/namecheap/namecheap.go +++ b/vendor/github.com/xenolf/lego/providers/dns/namecheap/namecheap.go @@ -3,12 +3,10 @@ package namecheap import ( - "encoding/xml" "errors" "fmt" "io/ioutil" "net/http" - "net/url" "strconv" "strings" "time" @@ -147,22 +145,28 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error { return fmt.Errorf("namecheap: %v", err) } - hosts, err := d.getHosts(ch) + records, err := d.getHosts(ch.sld, ch.tld) if err != nil { return fmt.Errorf("namecheap: %v", err) } - d.addChallengeRecord(ch, &hosts) + record := Record{ + Name: ch.key, + Type: "TXT", + Address: ch.keyValue, + MXPref: "10", + TTL: strconv.Itoa(d.config.TTL), + } + + records = append(records, record) if d.config.Debug { - for _, h := range hosts { - log.Printf( - "%-5.5s %-30.30s %-6s %-70.70s\n", - h.Type, h.Name, h.TTL, h.Address) + for _, h := range records { + log.Printf("%-5.5s %-30.30s %-6s %-70.70s", h.Type, h.Name, h.TTL, h.Address) } } - err = d.setHosts(ch, hosts) + err = d.setHosts(ch.sld, ch.tld, records) if err != nil { return fmt.Errorf("namecheap: %v", err) } @@ -181,16 +185,25 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { return fmt.Errorf("namecheap: %v", err) } - hosts, err := d.getHosts(ch) + records, err := d.getHosts(ch.sld, ch.tld) if err != nil { return fmt.Errorf("namecheap: %v", err) } - if removed := d.removeChallengeRecord(ch, &hosts); !removed { + // Find the challenge TXT record and remove it if found. + var found bool + for i, h := range records { + if h.Name == ch.key && h.Type == "TXT" { + records = append(records[:i], records[i+1:]...) + found = true + } + } + + if !found { return nil } - err = d.setHosts(ch, hosts) + err = d.setHosts(ch.sld, ch.tld, records) if err != nil { return fmt.Errorf("namecheap: %v", err) } @@ -243,189 +256,15 @@ func newChallenge(domain, keyAuth string, tlds map[string]string) (*challenge, e host = strings.Join(parts[:longest-1], ".") } - key, keyValue, _ := acme.DNS01Record(domain, keyAuth) + fqdn, value, _ := acme.DNS01Record(domain, keyAuth) return &challenge{ domain: domain, key: "_acme-challenge." + host, - keyFqdn: key, - keyValue: keyValue, + keyFqdn: fqdn, + keyValue: value, tld: tld, sld: sld, host: host, }, nil } - -// setGlobalParams adds the namecheap global parameters to the provided url -// Values record. -func (d *DNSProvider) setGlobalParams(v *url.Values, cmd string) { - v.Set("ApiUser", d.config.APIUser) - v.Set("ApiKey", d.config.APIKey) - v.Set("UserName", d.config.APIUser) - v.Set("Command", cmd) - v.Set("ClientIp", d.config.ClientIP) -} - -// getTLDs requests the list of available TLDs from namecheap. -func (d *DNSProvider) getTLDs() (tlds map[string]string, err error) { - values := make(url.Values) - d.setGlobalParams(&values, "namecheap.domains.getTldList") - - reqURL, err := url.Parse(d.config.BaseURL) - if err != nil { - return nil, err - } - reqURL.RawQuery = values.Encode() - - resp, err := d.config.HTTPClient.Get(reqURL.String()) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - if resp.StatusCode >= 400 { - return nil, fmt.Errorf("getHosts HTTP error %d", resp.StatusCode) - } - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, err - } - - var gtr getTldsResponse - if err := xml.Unmarshal(body, >r); err != nil { - return nil, err - } - if len(gtr.Errors) > 0 { - return nil, fmt.Errorf("%s [%d]", gtr.Errors[0].Description, gtr.Errors[0].Number) - } - - tlds = make(map[string]string) - for _, t := range gtr.Result { - tlds[t.Name] = t.Name - } - return tlds, nil -} - -// getHosts reads the full list of DNS host records using the Namecheap API. -func (d *DNSProvider) getHosts(ch *challenge) (hosts []host, err error) { - values := make(url.Values) - d.setGlobalParams(&values, "namecheap.domains.dns.getHosts") - - values.Set("SLD", ch.sld) - values.Set("TLD", ch.tld) - - reqURL, err := url.Parse(d.config.BaseURL) - if err != nil { - return nil, err - } - reqURL.RawQuery = values.Encode() - - resp, err := d.config.HTTPClient.Get(reqURL.String()) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - if resp.StatusCode >= 400 { - return nil, fmt.Errorf("getHosts HTTP error %d", resp.StatusCode) - } - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, err - } - - var ghr getHostsResponse - if err = xml.Unmarshal(body, &ghr); err != nil { - return nil, err - } - if len(ghr.Errors) > 0 { - return nil, fmt.Errorf("%s [%d]", ghr.Errors[0].Description, ghr.Errors[0].Number) - } - - return ghr.Hosts, nil -} - -// setHosts writes the full list of DNS host records using the Namecheap API. -func (d *DNSProvider) setHosts(ch *challenge, hosts []host) error { - values := make(url.Values) - d.setGlobalParams(&values, "namecheap.domains.dns.setHosts") - - values.Set("SLD", ch.sld) - values.Set("TLD", ch.tld) - - for i, h := range hosts { - ind := fmt.Sprintf("%d", i+1) - values.Add("HostName"+ind, h.Name) - values.Add("RecordType"+ind, h.Type) - values.Add("Address"+ind, h.Address) - values.Add("MXPref"+ind, h.MXPref) - values.Add("TTL"+ind, h.TTL) - } - - resp, err := d.config.HTTPClient.PostForm(d.config.BaseURL, values) - if err != nil { - return err - } - defer resp.Body.Close() - - if resp.StatusCode >= 400 { - return fmt.Errorf("setHosts HTTP error %d", resp.StatusCode) - } - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return err - } - - var shr setHostsResponse - if err := xml.Unmarshal(body, &shr); err != nil { - return err - } - if len(shr.Errors) > 0 { - return fmt.Errorf("%s [%d]", shr.Errors[0].Description, shr.Errors[0].Number) - } - if shr.Result.IsSuccess != "true" { - return fmt.Errorf("setHosts failed") - } - - return nil -} - -// addChallengeRecord adds a DNS challenge TXT record to a list of namecheap -// host records. -func (d *DNSProvider) addChallengeRecord(ch *challenge, hosts *[]host) { - host := host{ - Name: ch.key, - Type: "TXT", - Address: ch.keyValue, - MXPref: "10", - TTL: strconv.Itoa(d.config.TTL), - } - - // If there's already a TXT record with the same name, replace it. - for i, h := range *hosts { - if h.Name == ch.key && h.Type == "TXT" { - (*hosts)[i] = host - return - } - } - - // No record was replaced, so add a new one. - *hosts = append(*hosts, host) -} - -// removeChallengeRecord removes a DNS challenge TXT record from a list of -// namecheap host records. Return true if a record was removed. -func (d *DNSProvider) removeChallengeRecord(ch *challenge, hosts *[]host) bool { - // Find the challenge TXT record and remove it if found. - for i, h := range *hosts { - if h.Name == ch.key && h.Type == "TXT" { - *hosts = append((*hosts)[:i], (*hosts)[i+1:]...) - return true - } - } - - return false -} diff --git a/vendor/github.com/xenolf/lego/providers/dns/namedotcom/namedotcom.go b/vendor/github.com/xenolf/lego/providers/dns/namedotcom/namedotcom.go index 075f01835..2cd2b26d1 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/namedotcom/namedotcom.go +++ b/vendor/github.com/xenolf/lego/providers/dns/namedotcom/namedotcom.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "net/http" - "os" "strings" "time" @@ -15,6 +14,9 @@ import ( "github.com/xenolf/lego/platform/config/env" ) +// according to https://www.name.com/api-docs/DNS#CreateRecord +const minTTL = 300 + // Config is used to configure the creation of the DNSProvider type Config struct { Username string @@ -29,7 +31,7 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("NAMECOM_TTL", 120), + TTL: env.GetOrDefaultInt("NAMECOM_TTL", minTTL), PropagationTimeout: env.GetOrDefaultSecond("NAMECOM_PROPAGATION_TIMEOUT", acme.DefaultPropagationTimeout), PollingInterval: env.GetOrDefaultSecond("NAMECOM_POLLING_INTERVAL", acme.DefaultPollingInterval), HTTPClient: &http.Client{ @@ -56,7 +58,7 @@ func NewDNSProvider() (*DNSProvider, error) { config := NewDefaultConfig() config.Username = values["NAMECOM_USERNAME"] config.APIToken = values["NAMECOM_API_TOKEN"] - config.Server = os.Getenv("NAMECOM_SERVER") + config.Server = env.GetOrFile("NAMECOM_SERVER") return NewDNSProviderConfig(config) } @@ -87,6 +89,10 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { return nil, fmt.Errorf("namedotcom: API token is required") } + if config.TTL < minTTL { + return nil, fmt.Errorf("namedotcom: invalid TTL, TTL (%d) must be greater than %d", config.TTL, minTTL) + } + client := namecom.New(config.Username, config.APIToken) client.Client = config.HTTPClient @@ -97,7 +103,7 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { return &DNSProvider{client: client, config: config}, nil } -// Present creates a TXT record to fulfil the dns-01 challenge. +// Present creates a TXT record to fulfill the dns-01 challenge. func (d *DNSProvider) Present(domain, token, keyAuth string) error { fqdn, value, _ := acme.DNS01Record(domain, keyAuth) diff --git a/vendor/github.com/xenolf/lego/providers/dns/nifcloud/client.go b/vendor/github.com/xenolf/lego/providers/dns/nifcloud/client.go index dc10cbc20..24a8f52b6 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/nifcloud/client.go +++ b/vendor/github.com/xenolf/lego/providers/dns/nifcloud/client.go @@ -89,13 +89,17 @@ type ChangeInfo struct { } // NewClient Creates a new client of NIFCLOUD DNS -func NewClient(accessKey string, secretKey string) *Client { +func NewClient(accessKey string, secretKey string) (*Client, error) { + if len(accessKey) == 0 || len(secretKey) == 0 { + return nil, errors.New("credentials missing") + } + return &Client{ accessKey: accessKey, secretKey: secretKey, BaseURL: defaultBaseURL, HTTPClient: &http.Client{}, - } + }, nil } // Client client of NIFCLOUD DNS diff --git a/vendor/github.com/xenolf/lego/providers/dns/nifcloud/nifcloud.go b/vendor/github.com/xenolf/lego/providers/dns/nifcloud/nifcloud.go index 7e828aa96..c8cf04845 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/nifcloud/nifcloud.go +++ b/vendor/github.com/xenolf/lego/providers/dns/nifcloud/nifcloud.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "net/http" - "os" "time" "github.com/xenolf/lego/acme" @@ -52,7 +51,7 @@ func NewDNSProvider() (*DNSProvider, error) { } config := NewDefaultConfig() - config.BaseURL = os.Getenv("NIFCLOUD_DNS_ENDPOINT") + config.BaseURL = env.GetOrFile("NIFCLOUD_DNS_ENDPOINT") config.AccessKey = values["NIFCLOUD_ACCESS_KEY_ID"] config.SecretKey = values["NIFCLOUD_SECRET_ACCESS_KEY"] @@ -78,7 +77,10 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { return nil, errors.New("nifcloud: the configuration of the DNS provider is nil") } - client := NewClient(config.AccessKey, config.SecretKey) + client, err := NewClient(config.AccessKey, config.SecretKey) + if err != nil { + return nil, fmt.Errorf("nifcloud: %v", err) + } if config.HTTPClient != nil { client.HTTPClient = config.HTTPClient diff --git a/vendor/github.com/xenolf/lego/providers/dns/ns1/ns1.go b/vendor/github.com/xenolf/lego/providers/dns/ns1/ns1.go index 05397f27e..16be1c900 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/ns1/ns1.go +++ b/vendor/github.com/xenolf/lego/providers/dns/ns1/ns1.go @@ -10,6 +10,7 @@ import ( "time" "github.com/xenolf/lego/acme" + "github.com/xenolf/lego/log" "github.com/xenolf/lego/platform/config/env" "gopkg.in/ns1/ns1-go.v2/rest" "gopkg.in/ns1/ns1-go.v2/rest/model/dns" @@ -81,7 +82,7 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { return &DNSProvider{client: client, config: config}, nil } -// Present creates a TXT record to fulfil the dns-01 challenge. +// Present creates a TXT record to fulfill the dns-01 challenge. func (d *DNSProvider) Present(domain, token, keyAuth string) error { fqdn, value, _ := acme.DNS01Record(domain, keyAuth) @@ -90,10 +91,36 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error { return fmt.Errorf("ns1: %v", err) } - record := d.newTxtRecord(zone, fqdn, value, d.config.TTL) - _, err = d.client.Records.Create(record) - if err != nil && err != rest.ErrRecordExists { - return fmt.Errorf("ns1: failed to create record [zone: %q, fqdn: %q]: %v", zone.Zone, fqdn, err) + record, _, err := d.client.Records.Get(zone.Zone, acme.UnFqdn(fqdn), "TXT") + + // Create a new record + if err == rest.ErrRecordMissing || record == nil { + log.Infof("Create a new record for [zone: %s, fqdn: %s, domain: %s]", zone.Zone, fqdn) + + record = dns.NewRecord(zone.Zone, acme.UnFqdn(fqdn), "TXT") + record.TTL = d.config.TTL + record.Answers = []*dns.Answer{{Rdata: []string{value}}} + + _, err = d.client.Records.Create(record) + if err != nil { + return fmt.Errorf("ns1: failed to create record [zone: %q, fqdn: %q]: %v", zone.Zone, fqdn, err) + } + + return nil + } + + if err != nil { + return fmt.Errorf("ns1: failed to get the existing record: %v", err) + } + + // Update the existing records + record.Answers = append(record.Answers, &dns.Answer{Rdata: []string{value}}) + + log.Infof("Update an existing record for [zone: %s, fqdn: %s, domain: %s]", zone.Zone, fqdn, domain) + + _, err = d.client.Records.Update(record) + if err != nil { + return fmt.Errorf("ns1: failed to update record [zone: %q, fqdn: %q]: %v", zone.Zone, fqdn, err) } return nil @@ -110,7 +137,10 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { name := acme.UnFqdn(fqdn) _, err = d.client.Records.Delete(zone.Zone, name, "TXT") - return fmt.Errorf("ns1: failed to delete record [zone: %q, domain: %q]: %v", zone.Zone, name, err) + if err != nil { + return fmt.Errorf("ns1: failed to delete record [zone: %q, domain: %q]: %v", zone.Zone, name, err) + } + return nil } // Timeout returns the timeout and interval to use when checking for DNS propagation. @@ -141,17 +171,3 @@ func getAuthZone(fqdn string) (string, error) { return strings.TrimSuffix(authZone, "."), nil } - -func (d *DNSProvider) newTxtRecord(zone *dns.Zone, fqdn, value string, ttl int) *dns.Record { - name := acme.UnFqdn(fqdn) - - return &dns.Record{ - Type: "TXT", - Zone: zone.Zone, - Domain: name, - TTL: ttl, - Answers: []*dns.Answer{ - {Rdata: []string{value}}, - }, - } -} diff --git a/vendor/github.com/xenolf/lego/providers/dns/otc/mock.go b/vendor/github.com/xenolf/lego/providers/dns/otc/mock.go deleted file mode 100644 index 127dd5cc7..000000000 --- a/vendor/github.com/xenolf/lego/providers/dns/otc/mock.go +++ /dev/null @@ -1,162 +0,0 @@ -package otc - -import ( - "fmt" - "io/ioutil" - "net/http" - "net/http/httptest" - "testing" - - "github.com/stretchr/testify/assert" -) - -var fakeOTCUserName = "test" -var fakeOTCPassword = "test" -var fakeOTCDomainName = "test" -var fakeOTCProjectName = "test" -var fakeOTCToken = "62244bc21da68d03ebac94e6636ff01f" - -// DNSMock mock -type DNSMock struct { - t *testing.T - Server *httptest.Server - Mux *http.ServeMux -} - -// NewDNSMock create a new DNSMock -func NewDNSMock(t *testing.T) *DNSMock { - return &DNSMock{ - t: t, - } -} - -// Setup creates the mock server -func (m *DNSMock) Setup() { - m.Mux = http.NewServeMux() - m.Server = httptest.NewServer(m.Mux) -} - -// ShutdownServer creates the mock server -func (m *DNSMock) ShutdownServer() { - m.Server.Close() -} - -// HandleAuthSuccessfully Handle auth successfully -func (m *DNSMock) HandleAuthSuccessfully() { - m.Mux.HandleFunc("/v3/auth/token", func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("X-Subject-Token", fakeOTCToken) - - fmt.Fprintf(w, `{ - "token": { - "catalog": [ - { - "type": "dns", - "id": "56cd81db1f8445d98652479afe07c5ba", - "name": "", - "endpoints": [ - { - "url": "%s", - "region": "eu-de", - "region_id": "eu-de", - "interface": "public", - "id": "0047a06690484d86afe04877074efddf" - } - ] - } - ] - }}`, m.Server.URL) - }) -} - -// HandleListZonesSuccessfully Handle list zones successfully -func (m *DNSMock) HandleListZonesSuccessfully() { - m.Mux.HandleFunc("/v2/zones", func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, `{ - "zones":[{ - "id":"123123" - }]} - `) - - assert.Equal(m.t, r.Method, http.MethodGet) - assert.Equal(m.t, r.URL.Path, "/v2/zones") - assert.Equal(m.t, r.URL.RawQuery, "name=example.com.") - assert.Equal(m.t, r.Header.Get("Content-Type"), "application/json") - }) -} - -// HandleListZonesEmpty Handle list zones empty -func (m *DNSMock) HandleListZonesEmpty() { - m.Mux.HandleFunc("/v2/zones", func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, `{ - "zones":[ - ]} - `) - - assert.Equal(m.t, r.Method, http.MethodGet) - assert.Equal(m.t, r.URL.Path, "/v2/zones") - assert.Equal(m.t, r.URL.RawQuery, "name=example.com.") - assert.Equal(m.t, r.Header.Get("Content-Type"), "application/json") - }) -} - -// HandleDeleteRecordsetsSuccessfully Handle delete recordsets successfully -func (m *DNSMock) HandleDeleteRecordsetsSuccessfully() { - m.Mux.HandleFunc("/v2/zones/123123/recordsets/321321", func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, `{ - "zones":[{ - "id":"123123" - }]} - `) - - assert.Equal(m.t, r.Method, http.MethodDelete) - assert.Equal(m.t, r.URL.Path, "/v2/zones/123123/recordsets/321321") - assert.Equal(m.t, r.Header.Get("Content-Type"), "application/json") - }) -} - -// HandleListRecordsetsEmpty Handle list recordsets empty -func (m *DNSMock) HandleListRecordsetsEmpty() { - m.Mux.HandleFunc("/v2/zones/123123/recordsets", func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, `{ - "recordsets":[ - ]} - `) - - assert.Equal(m.t, r.URL.Path, "/v2/zones/123123/recordsets") - assert.Equal(m.t, r.URL.RawQuery, "type=TXT&name=_acme-challenge.example.com.") - }) -} - -// HandleListRecordsetsSuccessfully Handle list recordsets successfully -func (m *DNSMock) HandleListRecordsetsSuccessfully() { - m.Mux.HandleFunc("/v2/zones/123123/recordsets", func(w http.ResponseWriter, r *http.Request) { - if r.Method == http.MethodGet { - fmt.Fprintf(w, `{ - "recordsets":[{ - "id":"321321" - }]} - `) - - assert.Equal(m.t, r.URL.Path, "/v2/zones/123123/recordsets") - assert.Equal(m.t, r.URL.RawQuery, "type=TXT&name=_acme-challenge.example.com.") - - } else if r.Method == http.MethodPost { - body, err := ioutil.ReadAll(r.Body) - - assert.Nil(m.t, err) - exceptedString := "{\"name\":\"_acme-challenge.example.com.\",\"description\":\"Added TXT record for ACME dns-01 challenge using lego client\",\"type\":\"TXT\",\"ttl\":300,\"records\":[\"\\\"w6uP8Tcg6K2QR905Rms8iXTlksL6OD1KOWBxTK7wxPI\\\"\"]}" - assert.Equal(m.t, string(body), exceptedString) - - fmt.Fprintf(w, `{ - "recordsets":[{ - "id":"321321" - }]} - `) - - } else { - m.t.Errorf("Expected method to be 'GET' or 'POST' but got '%s'", r.Method) - } - - assert.Equal(m.t, r.Header.Get("Content-Type"), "application/json") - }) -} diff --git a/vendor/github.com/xenolf/lego/providers/dns/otc/otc.go b/vendor/github.com/xenolf/lego/providers/dns/otc/otc.go index b26934d15..fd0c40dff 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/otc/otc.go +++ b/vendor/github.com/xenolf/lego/providers/dns/otc/otc.go @@ -113,6 +113,10 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { return nil, fmt.Errorf("otc: credentials missing") } + if config.TTL < minTTL { + return nil, fmt.Errorf("otc: invalid TTL, TTL (%d) must be greater than %d", config.TTL, minTTL) + } + if config.IdentityEndpoint == "" { config.IdentityEndpoint = defaultIdentityEndpoint } @@ -124,10 +128,6 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { func (d *DNSProvider) Present(domain, token, keyAuth string) error { fqdn, value, _ := acme.DNS01Record(domain, keyAuth) - if d.config.TTL < minTTL { - d.config.TTL = minTTL - } - authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers) if err != nil { return fmt.Errorf("otc: %v", err) diff --git a/vendor/github.com/xenolf/lego/providers/dns/ovh/ovh.go b/vendor/github.com/xenolf/lego/providers/dns/ovh/ovh.go index c633ea7d1..e5e092443 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/ovh/ovh.go +++ b/vendor/github.com/xenolf/lego/providers/dns/ovh/ovh.go @@ -114,7 +114,7 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { }, nil } -// Present creates a TXT record to fulfil the dns-01 challenge. +// Present creates a TXT record to fulfill the dns-01 challenge. func (d *DNSProvider) Present(domain, token, keyAuth string) error { fqdn, value, _ := acme.DNS01Record(domain, keyAuth) diff --git a/vendor/github.com/xenolf/lego/providers/dns/pdns/pdns.go b/vendor/github.com/xenolf/lego/providers/dns/pdns/pdns.go index 44c4e4331..8dc9c175e 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/pdns/pdns.go +++ b/vendor/github.com/xenolf/lego/providers/dns/pdns/pdns.go @@ -110,7 +110,7 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { return d.config.PropagationTimeout, d.config.PollingInterval } -// Present creates a TXT record to fulfil the dns-01 challenge +// Present creates a TXT record to fulfill the dns-01 challenge func (d *DNSProvider) Present(domain, token, keyAuth string) error { fqdn, value, _ := acme.DNS01Record(domain, keyAuth) zone, err := d.getHostedZone(fqdn) diff --git a/vendor/github.com/xenolf/lego/providers/dns/rackspace/client.go b/vendor/github.com/xenolf/lego/providers/dns/rackspace/client.go index ce2032762..54b05c9db 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/rackspace/client.go +++ b/vendor/github.com/xenolf/lego/providers/dns/rackspace/client.go @@ -16,20 +16,44 @@ type AuthData struct { Auth `json:"auth"` } -// Identity Identity +// Identity Identity type Identity struct { - Access struct { - ServiceCatalog []struct { - Endpoints []struct { - PublicURL string `json:"publicURL"` - TenantID string `json:"tenantId"` - } `json:"endpoints"` - Name string `json:"name"` - } `json:"serviceCatalog"` - Token struct { - ID string `json:"id"` - } `json:"token"` - } `json:"access"` + Access Access `json:"access"` +} + +// Access Access +type Access struct { + ServiceCatalog []ServiceCatalog `json:"serviceCatalog"` + Token Token `json:"token"` +} + +// Token Token +type Token struct { + ID string `json:"id"` +} + +// ServiceCatalog ServiceCatalog +type ServiceCatalog struct { + Endpoints []Endpoint `json:"endpoints"` + Name string `json:"name"` +} + +// Endpoint Endpoint +type Endpoint struct { + PublicURL string `json:"publicURL"` + TenantID string `json:"tenantId"` +} + +// ZoneSearchResponse represents the response when querying Rackspace DNS zones +type ZoneSearchResponse struct { + TotalEntries int `json:"totalEntries"` + HostedZones []HostedZone `json:"domains"` +} + +// HostedZone HostedZone +type HostedZone struct { + ID int `json:"id"` + Name string `json:"name"` } // Records is the list of records sent/received from the DNS API diff --git a/vendor/github.com/xenolf/lego/providers/dns/rackspace/rackspace.go b/vendor/github.com/xenolf/lego/providers/dns/rackspace/rackspace.go index 8e56f20a9..7b5e73d43 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/rackspace/rackspace.go +++ b/vendor/github.com/xenolf/lego/providers/dns/rackspace/rackspace.go @@ -89,39 +89,7 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { return nil, fmt.Errorf("rackspace: credentials missing") } - authData := AuthData{ - Auth: Auth{ - APIKeyCredentials: APIKeyCredentials{ - Username: config.APIUser, - APIKey: config.APIKey, - }, - }, - } - - body, err := json.Marshal(authData) - if err != nil { - return nil, err - } - - req, err := http.NewRequest(http.MethodPost, config.BaseURL, bytes.NewReader(body)) - if err != nil { - return nil, err - } - req.Header.Set("Content-Type", "application/json") - - // client := &http.Client{Timeout: 30 * time.Second} - resp, err := config.HTTPClient.Do(req) - if err != nil { - return nil, fmt.Errorf("rackspace: error querying Identity API: %v", err) - } - defer resp.Body.Close() - - if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("rackspace: authentication failed: response code: %d", resp.StatusCode) - } - - var identity Identity - err = json.NewDecoder(resp.Body).Decode(&identity) + identity, err := login(config) if err != nil { return nil, fmt.Errorf("rackspace: %v", err) } @@ -134,6 +102,7 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { break } } + if dnsEndpoint == "" { return nil, fmt.Errorf("rackspace: failed to populate DNS endpoint, check Rackspace API for changes") } @@ -146,9 +115,10 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { } -// Present creates a TXT record to fulfil the dns-01 challenge +// Present creates a TXT record to fulfill the dns-01 challenge func (d *DNSProvider) Present(domain, token, keyAuth string) error { fqdn, value, _ := acme.DNS01Record(domain, keyAuth) + zoneID, err := d.getHostedZoneID(fqdn) if err != nil { return fmt.Errorf("rackspace: %v", err) @@ -178,6 +148,7 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error { // CleanUp removes the TXT record matching the specified parameters func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { fqdn, _, _ := acme.DNS01Record(domain, keyAuth) + zoneID, err := d.getHostedZoneID(fqdn) if err != nil { return fmt.Errorf("rackspace: %v", err) @@ -204,15 +175,6 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { // getHostedZoneID performs a lookup to get the DNS zone which needs // modifying for a given FQDN func (d *DNSProvider) getHostedZoneID(fqdn string) (int, error) { - // HostedZones represents the response when querying Rackspace DNS zones - type ZoneSearchResponse struct { - TotalEntries int `json:"totalEntries"` - HostedZones []struct { - ID int `json:"id"` - Name string `json:"name"` - } `json:"domains"` - } - authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers) if err != nil { return 0, err @@ -250,8 +212,7 @@ func (d *DNSProvider) findTxtRecord(fqdn string, zoneID int) (*Record, error) { return nil, err } - recordsLength := len(records.Record) - switch recordsLength { + switch len(records.Record) { case 1: case 0: return nil, fmt.Errorf("no TXT record found for %s", fqdn) @@ -265,6 +226,7 @@ func (d *DNSProvider) findTxtRecord(fqdn string, zoneID int) (*Record, error) { // makeRequest is a wrapper function used for making DNS API requests func (d *DNSProvider) makeRequest(method, uri string, body io.Reader) (json.RawMessage, error) { url := d.cloudDNSEndpoint + uri + req, err := http.NewRequest(method, url, body) if err != nil { return nil, err @@ -292,3 +254,44 @@ func (d *DNSProvider) makeRequest(method, uri string, body io.Reader) (json.RawM return r, nil } + +func login(config *Config) (*Identity, error) { + authData := AuthData{ + Auth: Auth{ + APIKeyCredentials: APIKeyCredentials{ + Username: config.APIUser, + APIKey: config.APIKey, + }, + }, + } + + body, err := json.Marshal(authData) + if err != nil { + return nil, err + } + + req, err := http.NewRequest(http.MethodPost, config.BaseURL, bytes.NewReader(body)) + if err != nil { + return nil, err + } + + req.Header.Set("Content-Type", "application/json") + + resp, err := config.HTTPClient.Do(req) + if err != nil { + return nil, fmt.Errorf("error querying Identity API: %v", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("authentication failed: response code: %d", resp.StatusCode) + } + + var identity Identity + err = json.NewDecoder(resp.Body).Decode(&identity) + if err != nil { + return nil, err + } + + return &identity, nil +} diff --git a/vendor/github.com/xenolf/lego/providers/dns/rfc2136/rfc2136.go b/vendor/github.com/xenolf/lego/providers/dns/rfc2136/rfc2136.go index 792ee8b0c..797b90a87 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/rfc2136/rfc2136.go +++ b/vendor/github.com/xenolf/lego/providers/dns/rfc2136/rfc2136.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "net" - "os" "strings" "time" @@ -62,8 +61,8 @@ func NewDNSProvider() (*DNSProvider, error) { config := NewDefaultConfig() config.Nameserver = values["RFC2136_NAMESERVER"] - config.TSIGKey = os.Getenv("RFC2136_TSIG_KEY") - config.TSIGSecret = os.Getenv("RFC2136_TSIG_SECRET") + config.TSIGKey = env.GetOrFile("RFC2136_TSIG_KEY") + config.TSIGSecret = env.GetOrFile("RFC2136_TSIG_SECRET") return NewDNSProviderConfig(config) } diff --git a/vendor/github.com/xenolf/lego/providers/dns/route53/route53.go b/vendor/github.com/xenolf/lego/providers/dns/route53/route53.go index a1d4a8a3e..1cace78e9 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/route53/route53.go +++ b/vendor/github.com/xenolf/lego/providers/dns/route53/route53.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "math/rand" - "os" "strings" "time" @@ -35,7 +34,7 @@ func NewDefaultConfig() *Config { TTL: env.GetOrDefaultInt("AWS_TTL", 10), PropagationTimeout: env.GetOrDefaultSecond("AWS_PROPAGATION_TIMEOUT", 2*time.Minute), PollingInterval: env.GetOrDefaultSecond("AWS_POLLING_INTERVAL", 4*time.Second), - HostedZoneID: os.Getenv("AWS_HOSTED_ZONE_ID"), + HostedZoneID: env.GetOrFile("AWS_HOSTED_ZONE_ID"), } } @@ -45,17 +44,16 @@ type DNSProvider struct { config *Config } -// customRetryer implements the client.Retryer interface by composing the -// DefaultRetryer. It controls the logic for retrying recoverable request -// errors (e.g. when rate limits are exceeded). +// customRetryer implements the client.Retryer interface by composing the DefaultRetryer. +// It controls the logic for retrying recoverable request errors (e.g. when rate limits are exceeded). type customRetryer struct { client.DefaultRetryer } // RetryRules overwrites the DefaultRetryer's method. -// It uses a basic exponential backoff algorithm that returns an initial -// delay of ~400ms with an upper limit of ~30 seconds which should prevent -// causing a high number of consecutive throttling errors. +// It uses a basic exponential backoff algorithm: +// that returns an initial delay of ~400ms with an upper limit of ~30 seconds, +// which should prevent causing a high number of consecutive throttling errors. // For reference: Route 53 enforces an account-wide(!) 5req/s query limit. func (d customRetryer) RetryRules(r *request.Request) time.Duration { retryCount := r.RetryCount @@ -67,57 +65,81 @@ func (d customRetryer) RetryRules(r *request.Request) time.Duration { return time.Duration(delay) * time.Millisecond } -// NewDNSProvider returns a DNSProvider instance configured for the AWS -// Route 53 service. +// NewDNSProvider returns a DNSProvider instance configured for the AWS Route 53 service. // -// AWS Credentials are automatically detected in the following locations -// and prioritized in the following order: +// AWS Credentials are automatically detected in the following locations and prioritized in the following order: // 1. Environment variables: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, // AWS_REGION, [AWS_SESSION_TOKEN] // 2. Shared credentials file (defaults to ~/.aws/credentials) // 3. Amazon EC2 IAM role // -// If AWS_HOSTED_ZONE_ID is not set, Lego tries to determine the correct -// public hosted zone via the FQDN. +// If AWS_HOSTED_ZONE_ID is not set, Lego tries to determine the correct public hosted zone via the FQDN. // // See also: https://github.com/aws/aws-sdk-go/wiki/configuring-sdk func NewDNSProvider() (*DNSProvider, error) { return NewDNSProviderConfig(NewDefaultConfig()) } -// NewDNSProviderConfig takes a given config ans returns a custom configured -// DNSProvider instance +// NewDNSProviderConfig takes a given config ans returns a custom configured DNSProvider instance func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { if config == nil { return nil, errors.New("route53: the configuration of the Route53 DNS provider is nil") } - r := customRetryer{} - r.NumMaxRetries = config.MaxRetries - sessionCfg := request.WithRetryer(aws.NewConfig(), r) + retry := customRetryer{} + retry.NumMaxRetries = config.MaxRetries + sessionCfg := request.WithRetryer(aws.NewConfig(), retry) + sess, err := session.NewSessionWithOptions(session.Options{Config: *sessionCfg}) if err != nil { return nil, err } - cl := route53.New(sess) - return &DNSProvider{ - client: cl, - config: config, - }, nil + cl := route53.New(sess) + return &DNSProvider{client: cl, config: config}, nil } // Timeout returns the timeout and interval to use when checking for DNS // propagation. -func (r *DNSProvider) Timeout() (timeout, interval time.Duration) { - return r.config.PropagationTimeout, r.config.PollingInterval +func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { + return d.config.PropagationTimeout, d.config.PollingInterval } // Present creates a TXT record using the specified parameters -func (r *DNSProvider) Present(domain, token, keyAuth string) error { +func (d *DNSProvider) Present(domain, token, keyAuth string) error { fqdn, value, _ := acme.DNS01Record(domain, keyAuth) - err := r.changeRecord("UPSERT", fqdn, `"`+value+`"`, r.config.TTL) + hostedZoneID, err := d.getHostedZoneID(fqdn) + if err != nil { + return fmt.Errorf("route53: failed to determine hosted zone ID: %v", err) + } + + records, err := d.getExistingRecordSets(hostedZoneID, fqdn) + if err != nil { + return fmt.Errorf("route53: %v", err) + } + + realValue := `"` + value + `"` + + var found bool + for _, record := range records { + if aws.StringValue(record.Value) == realValue { + found = true + } + } + + if !found { + records = append(records, &route53.ResourceRecord{Value: aws.String(realValue)}) + } + + recordSet := &route53.ResourceRecordSet{ + Name: aws.String(fqdn), + Type: aws.String("TXT"), + TTL: aws.Int64(int64(d.config.TTL)), + ResourceRecords: records, + } + + err = d.changeRecord(route53.ChangeActionUpsert, hostedZoneID, recordSet) if err != nil { return fmt.Errorf("route53: %v", err) } @@ -125,61 +147,101 @@ func (r *DNSProvider) Present(domain, token, keyAuth string) error { } // CleanUp removes the TXT record matching the specified parameters -func (r *DNSProvider) CleanUp(domain, token, keyAuth string) error { - fqdn, value, _ := acme.DNS01Record(domain, keyAuth) +func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { + fqdn, _, _ := acme.DNS01Record(domain, keyAuth) - err := r.changeRecord("DELETE", fqdn, `"`+value+`"`, r.config.TTL) + hostedZoneID, err := d.getHostedZoneID(fqdn) + if err != nil { + return fmt.Errorf("failed to determine Route 53 hosted zone ID: %v", err) + } + + records, err := d.getExistingRecordSets(hostedZoneID, fqdn) + if err != nil { + return fmt.Errorf("route53: %v", err) + } + + if len(records) == 0 { + return nil + } + + recordSet := &route53.ResourceRecordSet{ + Name: aws.String(fqdn), + Type: aws.String("TXT"), + TTL: aws.Int64(int64(d.config.TTL)), + ResourceRecords: records, + } + + err = d.changeRecord(route53.ChangeActionDelete, hostedZoneID, recordSet) if err != nil { return fmt.Errorf("route53: %v", err) } return nil } -func (r *DNSProvider) changeRecord(action, fqdn, value string, ttl int) error { - hostedZoneID, err := r.getHostedZoneID(fqdn) - if err != nil { - return fmt.Errorf("failed to determine Route 53 hosted zone ID: %v", err) - } - - recordSet := newTXTRecordSet(fqdn, value, ttl) - reqParams := &route53.ChangeResourceRecordSetsInput{ +func (d *DNSProvider) changeRecord(action, hostedZoneID string, recordSet *route53.ResourceRecordSet) error { + recordSetInput := &route53.ChangeResourceRecordSetsInput{ HostedZoneId: aws.String(hostedZoneID), ChangeBatch: &route53.ChangeBatch{ Comment: aws.String("Managed by Lego"), - Changes: []*route53.Change{ - { - Action: aws.String(action), - ResourceRecordSet: recordSet, - }, - }, + Changes: []*route53.Change{{ + Action: aws.String(action), + ResourceRecordSet: recordSet, + }}, }, } - resp, err := r.client.ChangeResourceRecordSets(reqParams) + resp, err := d.client.ChangeResourceRecordSets(recordSetInput) if err != nil { return fmt.Errorf("failed to change record set: %v", err) } - statusID := resp.ChangeInfo.Id + changeID := resp.ChangeInfo.Id - return acme.WaitFor(r.config.PropagationTimeout, r.config.PollingInterval, func() (bool, error) { - reqParams := &route53.GetChangeInput{ - Id: statusID, - } - resp, err := r.client.GetChange(reqParams) + return acme.WaitFor(d.config.PropagationTimeout, d.config.PollingInterval, func() (bool, error) { + reqParams := &route53.GetChangeInput{Id: changeID} + + resp, err := d.client.GetChange(reqParams) if err != nil { return false, fmt.Errorf("failed to query change status: %v", err) } + if aws.StringValue(resp.ChangeInfo.Status) == route53.ChangeStatusInsync { return true, nil } - return false, nil + return false, fmt.Errorf("unable to retrieve change: ID=%s", aws.StringValue(changeID)) }) } -func (r *DNSProvider) getHostedZoneID(fqdn string) (string, error) { - if r.config.HostedZoneID != "" { - return r.config.HostedZoneID, nil +func (d *DNSProvider) getExistingRecordSets(hostedZoneID string, fqdn string) ([]*route53.ResourceRecord, error) { + listInput := &route53.ListResourceRecordSetsInput{ + HostedZoneId: aws.String(hostedZoneID), + StartRecordName: aws.String(fqdn), + StartRecordType: aws.String("TXT"), + } + + recordSetsOutput, err := d.client.ListResourceRecordSets(listInput) + if err != nil { + return nil, err + } + + if recordSetsOutput == nil { + return nil, nil + } + + var records []*route53.ResourceRecord + + for _, recordSet := range recordSetsOutput.ResourceRecordSets { + if aws.StringValue(recordSet.Name) == fqdn { + records = append(records, recordSet.ResourceRecords...) + } + } + + return records, nil +} + +func (d *DNSProvider) getHostedZoneID(fqdn string) (string, error) { + if d.config.HostedZoneID != "" { + return d.config.HostedZoneID, nil } authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers) @@ -191,7 +253,7 @@ func (r *DNSProvider) getHostedZoneID(fqdn string) (string, error) { reqParams := &route53.ListHostedZonesByNameInput{ DNSName: aws.String(acme.UnFqdn(authZone)), } - resp, err := r.client.ListHostedZonesByName(reqParams) + resp, err := d.client.ListHostedZonesByName(reqParams) if err != nil { return "", err } @@ -215,14 +277,3 @@ func (r *DNSProvider) getHostedZoneID(fqdn string) (string, error) { return hostedZoneID, nil } - -func newTXTRecordSet(fqdn, value string, ttl int) *route53.ResourceRecordSet { - return &route53.ResourceRecordSet{ - Name: aws.String(fqdn), - Type: aws.String("TXT"), - TTL: aws.Int64(int64(ttl)), - ResourceRecords: []*route53.ResourceRecord{ - {Value: aws.String(value)}, - }, - } -} diff --git a/vendor/github.com/xenolf/lego/providers/dns/sakuracloud/sakuracloud.go b/vendor/github.com/xenolf/lego/providers/dns/sakuracloud/sakuracloud.go index b746fd66a..b0227f6ce 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/sakuracloud/sakuracloud.go +++ b/vendor/github.com/xenolf/lego/providers/dns/sakuracloud/sakuracloud.go @@ -82,10 +82,10 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { client := api.NewClient(config.Token, config.Secret, "tk1a") client.UserAgent = acme.UserAgent - return &DNSProvider{client: client}, nil + return &DNSProvider{client: client, config: config}, nil } -// Present creates a TXT record to fulfil the dns-01 challenge. +// Present creates a TXT record to fulfill the dns-01 challenge. func (d *DNSProvider) Present(domain, token, keyAuth string) error { fqdn, value, _ := acme.DNS01Record(domain, keyAuth) diff --git a/vendor/github.com/xenolf/lego/providers/dns/stackpath/client.go b/vendor/github.com/xenolf/lego/providers/dns/stackpath/client.go new file mode 100644 index 000000000..495d8c555 --- /dev/null +++ b/vendor/github.com/xenolf/lego/providers/dns/stackpath/client.go @@ -0,0 +1,217 @@ +package stackpath + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "path" + + "github.com/xenolf/lego/acme" + "golang.org/x/net/publicsuffix" +) + +// Zones is the response struct from the Stackpath api GetZones +type Zones struct { + Zones []Zone `json:"zones"` +} + +// Zone a DNS zone representation +type Zone struct { + ID string + Domain string +} + +// Records is the response struct from the Stackpath api GetZoneRecords +type Records struct { + Records []Record `json:"records"` +} + +// Record a DNS record representation +type Record struct { + ID string `json:"id,omitempty"` + Name string `json:"name"` + Type string `json:"type"` + TTL int `json:"ttl"` + Data string `json:"data"` +} + +// ErrorResponse the API error response representation +type ErrorResponse struct { + Code int `json:"code"` + Message string `json:"error"` +} + +func (e *ErrorResponse) Error() string { + return fmt.Sprintf("%d %s", e.Code, e.Message) +} + +// https://developer.stackpath.com/en/api/dns/#operation/GetZones +func (d *DNSProvider) getZones(domain string) (*Zone, error) { + domain = acme.UnFqdn(domain) + tld, err := publicsuffix.EffectiveTLDPlusOne(domain) + if err != nil { + return nil, err + } + + req, err := d.newRequest(http.MethodGet, "/zones", nil) + if err != nil { + return nil, err + } + + query := req.URL.Query() + query.Add("page_request.filter", fmt.Sprintf("domain='%s'", tld)) + req.URL.RawQuery = query.Encode() + + var zones Zones + err = d.do(req, &zones) + if err != nil { + return nil, err + } + + if len(zones.Zones) == 0 { + return nil, fmt.Errorf("did not find zone with domain %s", domain) + } + + return &zones.Zones[0], nil +} + +// https://developer.stackpath.com/en/api/dns/#operation/GetZoneRecords +func (d *DNSProvider) getZoneRecords(name string, zone *Zone) ([]Record, error) { + u := fmt.Sprintf("/zones/%s/records", zone.ID) + req, err := d.newRequest(http.MethodGet, u, nil) + if err != nil { + return nil, err + } + + query := req.URL.Query() + query.Add("page_request.filter", fmt.Sprintf("name='%s' and type='TXT'", name)) + req.URL.RawQuery = query.Encode() + + var records Records + err = d.do(req, &records) + if err != nil { + return nil, err + } + + if len(records.Records) == 0 { + return nil, fmt.Errorf("did not find record with name %s", name) + } + + return records.Records, nil +} + +// https://developer.stackpath.com/en/api/dns/#operation/CreateZoneRecord +func (d *DNSProvider) createZoneRecord(zone *Zone, record Record) error { + u := fmt.Sprintf("/zones/%s/records", zone.ID) + req, err := d.newRequest(http.MethodPost, u, record) + if err != nil { + return err + } + + return d.do(req, nil) +} + +// https://developer.stackpath.com/en/api/dns/#operation/DeleteZoneRecord +func (d *DNSProvider) deleteZoneRecord(zone *Zone, record Record) error { + u := fmt.Sprintf("/zones/%s/records/%s", zone.ID, record.ID) + req, err := d.newRequest(http.MethodDelete, u, nil) + if err != nil { + return err + } + + return d.do(req, nil) +} + +func (d *DNSProvider) newRequest(method, urlStr string, body interface{}) (*http.Request, error) { + u, err := d.BaseURL.Parse(path.Join(d.config.StackID, urlStr)) + if err != nil { + return nil, err + } + + if body == nil { + var req *http.Request + req, err = http.NewRequest(method, u.String(), nil) + if err != nil { + return nil, err + } + + return req, nil + } + + reqBody, err := json.Marshal(body) + if err != nil { + return nil, err + } + + req, err := http.NewRequest(method, u.String(), bytes.NewBuffer(reqBody)) + if err != nil { + return nil, err + } + + req.Header.Set("Content-Type", "application/json") + + return req, nil +} + +func (d *DNSProvider) do(req *http.Request, v interface{}) error { + resp, err := d.client.Do(req) + if err != nil { + return err + } + + err = checkResponse(resp) + if err != nil { + return err + } + + if v == nil { + return nil + } + + raw, err := readBody(resp) + if err != nil { + return fmt.Errorf("failed to read body: %v", err) + } + + err = json.Unmarshal(raw, v) + if err != nil { + return fmt.Errorf("unmarshaling error: %v: %s", err, string(raw)) + } + + return nil +} + +func checkResponse(resp *http.Response) error { + if resp.StatusCode > 299 { + data, err := readBody(resp) + if err != nil { + return &ErrorResponse{Code: resp.StatusCode, Message: err.Error()} + } + + errResp := &ErrorResponse{} + err = json.Unmarshal(data, errResp) + if err != nil { + return &ErrorResponse{Code: resp.StatusCode, Message: fmt.Sprintf("unmarshaling error: %v: %s", err, string(data))} + } + return errResp + } + + return nil +} + +func readBody(resp *http.Response) ([]byte, error) { + if resp.Body == nil { + return nil, fmt.Errorf("response body is nil") + } + + defer resp.Body.Close() + + rawBody, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + return rawBody, nil +} diff --git a/vendor/github.com/xenolf/lego/providers/dns/stackpath/stackpath.go b/vendor/github.com/xenolf/lego/providers/dns/stackpath/stackpath.go new file mode 100644 index 000000000..4c247def7 --- /dev/null +++ b/vendor/github.com/xenolf/lego/providers/dns/stackpath/stackpath.go @@ -0,0 +1,150 @@ +// Package stackpath implements a DNS provider for solving the DNS-01 challenge using Stackpath DNS. +// https://developer.stackpath.com/en/api/dns/ +package stackpath + +import ( + "context" + "errors" + "fmt" + "log" + "net/http" + "net/url" + "strings" + "time" + + "github.com/xenolf/lego/acme" + "github.com/xenolf/lego/platform/config/env" + "golang.org/x/oauth2/clientcredentials" +) + +const ( + defaultBaseURL = "https://gateway.stackpath.com/dns/v1/stacks/" + defaultAuthURL = "https://gateway.stackpath.com/identity/v1/oauth2/token" +) + +// Config is used to configure the creation of the DNSProvider +type Config struct { + ClientID string + ClientSecret string + StackID string + TTL int + PropagationTimeout time.Duration + PollingInterval time.Duration +} + +// NewDefaultConfig returns a default configuration for the DNSProvider +func NewDefaultConfig() *Config { + return &Config{ + TTL: env.GetOrDefaultInt("STACKPATH_TTL", 120), + PropagationTimeout: env.GetOrDefaultSecond("STACKPATH_PROPAGATION_TIMEOUT", acme.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond("STACKPATH_POLLING_INTERVAL", acme.DefaultPollingInterval), + } +} + +// DNSProvider is an implementation of the acme.ChallengeProvider interface. +type DNSProvider struct { + BaseURL *url.URL + client *http.Client + config *Config +} + +// NewDNSProvider returns a DNSProvider instance configured for Stackpath. +// Credentials must be passed in the environment variables: +// STACKPATH_CLIENT_ID, STACKPATH_CLIENT_SECRET, and STACKPATH_STACK_ID. +func NewDNSProvider() (*DNSProvider, error) { + values, err := env.Get("STACKPATH_CLIENT_ID", "STACKPATH_CLIENT_SECRET", "STACKPATH_STACK_ID") + if err != nil { + return nil, fmt.Errorf("stackpath: %v", err) + } + + config := NewDefaultConfig() + config.ClientID = values["STACKPATH_CLIENT_ID"] + config.ClientSecret = values["STACKPATH_CLIENT_SECRET"] + config.StackID = values["STACKPATH_STACK_ID"] + + return NewDNSProviderConfig(config) +} + +// NewDNSProviderConfig return a DNSProvider instance configured for Stackpath. +func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { + if config == nil { + return nil, errors.New("stackpath: the configuration of the DNS provider is nil") + } + + if len(config.ClientID) == 0 || len(config.ClientSecret) == 0 { + return nil, errors.New("stackpath: credentials missing") + } + + if len(config.StackID) == 0 { + return nil, errors.New("stackpath: stack id missing") + } + + baseURL, _ := url.Parse(defaultBaseURL) + + return &DNSProvider{ + BaseURL: baseURL, + client: getOathClient(config), + config: config, + }, nil +} + +func getOathClient(config *Config) *http.Client { + oathConfig := &clientcredentials.Config{ + TokenURL: defaultAuthURL, + ClientID: config.ClientID, + ClientSecret: config.ClientSecret, + } + + return oathConfig.Client(context.Background()) +} + +// Present creates a TXT record to fulfill the dns-01 challenge +func (d *DNSProvider) Present(domain, token, keyAuth string) error { + zone, err := d.getZones(domain) + if err != nil { + return fmt.Errorf("stackpath: %v", err) + } + + fqdn, value, _ := acme.DNS01Record(domain, keyAuth) + parts := strings.Split(fqdn, ".") + + record := Record{ + Name: parts[0], + Type: "TXT", + TTL: d.config.TTL, + Data: value, + } + + return d.createZoneRecord(zone, record) +} + +// CleanUp removes the TXT record matching the specified parameters +func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { + zone, err := d.getZones(domain) + if err != nil { + return fmt.Errorf("stackpath: %v", err) + } + + fqdn, _, _ := acme.DNS01Record(domain, keyAuth) + parts := strings.Split(fqdn, ".") + + records, err := d.getZoneRecords(parts[0], zone) + if err != nil { + return err + } + + for _, record := range records { + err = d.deleteZoneRecord(zone, record) + if err != nil { + log.Printf("stackpath: failed to delete TXT record: %v", err) + } + } + + return nil +} + +// Timeout returns the timeout and interval to use when checking for DNS propagation. +// Adjusting here to cope with spikes in propagation times. +func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { + return d.config.PropagationTimeout, d.config.PollingInterval +} diff --git a/vendor/github.com/xenolf/lego/providers/dns/vegadns/vegadns.go b/vendor/github.com/xenolf/lego/providers/dns/vegadns/vegadns.go index 034d5ed4d..6b4cf7d14 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/vegadns/vegadns.go +++ b/vendor/github.com/xenolf/lego/providers/dns/vegadns/vegadns.go @@ -5,7 +5,6 @@ package vegadns import ( "errors" "fmt" - "os" "strings" "time" @@ -50,8 +49,8 @@ func NewDNSProvider() (*DNSProvider, error) { config := NewDefaultConfig() config.BaseURL = values["VEGADNS_URL"] - config.APIKey = os.Getenv("SECRET_VEGADNS_KEY") - config.APISecret = os.Getenv("SECRET_VEGADNS_SECRET") + config.APIKey = env.GetOrFile("SECRET_VEGADNS_KEY") + config.APISecret = env.GetOrFile("SECRET_VEGADNS_SECRET") return NewDNSProviderConfig(config) } @@ -87,7 +86,7 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { return d.config.PropagationTimeout, d.config.PollingInterval } -// Present creates a TXT record to fulfil the dns-01 challenge +// Present creates a TXT record to fulfill the dns-01 challenge func (d *DNSProvider) Present(domain, token, keyAuth string) error { fqdn, value, _ := acme.DNS01Record(domain, keyAuth) diff --git a/vendor/github.com/xenolf/lego/providers/dns/vultr/vultr.go b/vendor/github.com/xenolf/lego/providers/dns/vultr/vultr.go index e6a2a336e..80feb9f6d 100644 --- a/vendor/github.com/xenolf/lego/providers/dns/vultr/vultr.go +++ b/vendor/github.com/xenolf/lego/providers/dns/vultr/vultr.go @@ -90,7 +90,7 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { return &DNSProvider{client: client, config: config}, nil } -// Present creates a TXT record to fulfil the DNS-01 challenge. +// Present creates a TXT record to fulfill the DNS-01 challenge. func (d *DNSProvider) Present(domain, token, keyAuth string) error { fqdn, value, _ := acme.DNS01Record(domain, keyAuth) diff --git a/vendor/golang.org/x/net/publicsuffix/gen.go b/vendor/golang.org/x/net/publicsuffix/gen.go new file mode 100644 index 000000000..f85a3c32b --- /dev/null +++ b/vendor/golang.org/x/net/publicsuffix/gen.go @@ -0,0 +1,713 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build ignore + +package main + +// This program generates table.go and table_test.go based on the authoritative +// public suffix list at https://publicsuffix.org/list/effective_tld_names.dat +// +// The version is derived from +// https://api.github.com/repos/publicsuffix/list/commits?path=public_suffix_list.dat +// and a human-readable form is at +// https://github.com/publicsuffix/list/commits/master/public_suffix_list.dat +// +// To fetch a particular git revision, such as 5c70ccd250, pass +// -url "https://raw.githubusercontent.com/publicsuffix/list/5c70ccd250/public_suffix_list.dat" +// and -version "an explicit version string". + +import ( + "bufio" + "bytes" + "flag" + "fmt" + "go/format" + "io" + "io/ioutil" + "net/http" + "os" + "regexp" + "sort" + "strings" + + "golang.org/x/net/idna" +) + +const ( + // These sum of these four values must be no greater than 32. + nodesBitsChildren = 10 + nodesBitsICANN = 1 + nodesBitsTextOffset = 15 + nodesBitsTextLength = 6 + + // These sum of these four values must be no greater than 32. + childrenBitsWildcard = 1 + childrenBitsNodeType = 2 + childrenBitsHi = 14 + childrenBitsLo = 14 +) + +var ( + maxChildren int + maxTextOffset int + maxTextLength int + maxHi uint32 + maxLo uint32 +) + +func max(a, b int) int { + if a < b { + return b + } + return a +} + +func u32max(a, b uint32) uint32 { + if a < b { + return b + } + return a +} + +const ( + nodeTypeNormal = 0 + nodeTypeException = 1 + nodeTypeParentOnly = 2 + numNodeType = 3 +) + +func nodeTypeStr(n int) string { + switch n { + case nodeTypeNormal: + return "+" + case nodeTypeException: + return "!" + case nodeTypeParentOnly: + return "o" + } + panic("unreachable") +} + +const ( + defaultURL = "https://publicsuffix.org/list/effective_tld_names.dat" + gitCommitURL = "https://api.github.com/repos/publicsuffix/list/commits?path=public_suffix_list.dat" +) + +var ( + labelEncoding = map[string]uint32{} + labelsList = []string{} + labelsMap = map[string]bool{} + rules = []string{} + + // validSuffixRE is used to check that the entries in the public suffix + // list are in canonical form (after Punycode encoding). Specifically, + // capital letters are not allowed. + validSuffixRE = regexp.MustCompile(`^[a-z0-9_\!\*\-\.]+$`) + + shaRE = regexp.MustCompile(`"sha":"([^"]+)"`) + dateRE = regexp.MustCompile(`"committer":{[^{]+"date":"([^"]+)"`) + + comments = flag.Bool("comments", false, "generate table.go comments, for debugging") + subset = flag.Bool("subset", false, "generate only a subset of the full table, for debugging") + url = flag.String("url", defaultURL, "URL of the publicsuffix.org list. If empty, stdin is read instead") + v = flag.Bool("v", false, "verbose output (to stderr)") + version = flag.String("version", "", "the effective_tld_names.dat version") +) + +func main() { + if err := main1(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} + +func main1() error { + flag.Parse() + if nodesBitsTextLength+nodesBitsTextOffset+nodesBitsICANN+nodesBitsChildren > 32 { + return fmt.Errorf("not enough bits to encode the nodes table") + } + if childrenBitsLo+childrenBitsHi+childrenBitsNodeType+childrenBitsWildcard > 32 { + return fmt.Errorf("not enough bits to encode the children table") + } + if *version == "" { + if *url != defaultURL { + return fmt.Errorf("-version was not specified, and the -url is not the default one") + } + sha, date, err := gitCommit() + if err != nil { + return err + } + *version = fmt.Sprintf("publicsuffix.org's public_suffix_list.dat, git revision %s (%s)", sha, date) + } + var r io.Reader = os.Stdin + if *url != "" { + res, err := http.Get(*url) + if err != nil { + return err + } + if res.StatusCode != http.StatusOK { + return fmt.Errorf("bad GET status for %s: %d", *url, res.Status) + } + r = res.Body + defer res.Body.Close() + } + + var root node + icann := false + br := bufio.NewReader(r) + for { + s, err := br.ReadString('\n') + if err != nil { + if err == io.EOF { + break + } + return err + } + s = strings.TrimSpace(s) + if strings.Contains(s, "BEGIN ICANN DOMAINS") { + icann = true + continue + } + if strings.Contains(s, "END ICANN DOMAINS") { + icann = false + continue + } + if s == "" || strings.HasPrefix(s, "//") { + continue + } + s, err = idna.ToASCII(s) + if err != nil { + return err + } + if !validSuffixRE.MatchString(s) { + return fmt.Errorf("bad publicsuffix.org list data: %q", s) + } + + if *subset { + switch { + case s == "ac.jp" || strings.HasSuffix(s, ".ac.jp"): + case s == "ak.us" || strings.HasSuffix(s, ".ak.us"): + case s == "ao" || strings.HasSuffix(s, ".ao"): + case s == "ar" || strings.HasSuffix(s, ".ar"): + case s == "arpa" || strings.HasSuffix(s, ".arpa"): + case s == "cy" || strings.HasSuffix(s, ".cy"): + case s == "dyndns.org" || strings.HasSuffix(s, ".dyndns.org"): + case s == "jp": + case s == "kobe.jp" || strings.HasSuffix(s, ".kobe.jp"): + case s == "kyoto.jp" || strings.HasSuffix(s, ".kyoto.jp"): + case s == "om" || strings.HasSuffix(s, ".om"): + case s == "uk" || strings.HasSuffix(s, ".uk"): + case s == "uk.com" || strings.HasSuffix(s, ".uk.com"): + case s == "tw" || strings.HasSuffix(s, ".tw"): + case s == "zw" || strings.HasSuffix(s, ".zw"): + case s == "xn--p1ai" || strings.HasSuffix(s, ".xn--p1ai"): + // xn--p1ai is Russian-Cyrillic "рф". + default: + continue + } + } + + rules = append(rules, s) + + nt, wildcard := nodeTypeNormal, false + switch { + case strings.HasPrefix(s, "*."): + s, nt = s[2:], nodeTypeParentOnly + wildcard = true + case strings.HasPrefix(s, "!"): + s, nt = s[1:], nodeTypeException + } + labels := strings.Split(s, ".") + for n, i := &root, len(labels)-1; i >= 0; i-- { + label := labels[i] + n = n.child(label) + if i == 0 { + if nt != nodeTypeParentOnly && n.nodeType == nodeTypeParentOnly { + n.nodeType = nt + } + n.icann = n.icann && icann + n.wildcard = n.wildcard || wildcard + } + labelsMap[label] = true + } + } + labelsList = make([]string, 0, len(labelsMap)) + for label := range labelsMap { + labelsList = append(labelsList, label) + } + sort.Strings(labelsList) + + if err := generate(printReal, &root, "table.go"); err != nil { + return err + } + if err := generate(printTest, &root, "table_test.go"); err != nil { + return err + } + return nil +} + +func generate(p func(io.Writer, *node) error, root *node, filename string) error { + buf := new(bytes.Buffer) + if err := p(buf, root); err != nil { + return err + } + b, err := format.Source(buf.Bytes()) + if err != nil { + return err + } + return ioutil.WriteFile(filename, b, 0644) +} + +func gitCommit() (sha, date string, retErr error) { + res, err := http.Get(gitCommitURL) + if err != nil { + return "", "", err + } + if res.StatusCode != http.StatusOK { + return "", "", fmt.Errorf("bad GET status for %s: %d", gitCommitURL, res.Status) + } + defer res.Body.Close() + b, err := ioutil.ReadAll(res.Body) + if err != nil { + return "", "", err + } + if m := shaRE.FindSubmatch(b); m != nil { + sha = string(m[1]) + } + if m := dateRE.FindSubmatch(b); m != nil { + date = string(m[1]) + } + if sha == "" || date == "" { + retErr = fmt.Errorf("could not find commit SHA and date in %s", gitCommitURL) + } + return sha, date, retErr +} + +func printTest(w io.Writer, n *node) error { + fmt.Fprintf(w, "// generated by go run gen.go; DO NOT EDIT\n\n") + fmt.Fprintf(w, "package publicsuffix\n\nvar rules = [...]string{\n") + for _, rule := range rules { + fmt.Fprintf(w, "%q,\n", rule) + } + fmt.Fprintf(w, "}\n\nvar nodeLabels = [...]string{\n") + if err := n.walk(w, printNodeLabel); err != nil { + return err + } + fmt.Fprintf(w, "}\n") + return nil +} + +func printReal(w io.Writer, n *node) error { + const header = `// generated by go run gen.go; DO NOT EDIT + +package publicsuffix + +const version = %q + +const ( + nodesBitsChildren = %d + nodesBitsICANN = %d + nodesBitsTextOffset = %d + nodesBitsTextLength = %d + + childrenBitsWildcard = %d + childrenBitsNodeType = %d + childrenBitsHi = %d + childrenBitsLo = %d +) + +const ( + nodeTypeNormal = %d + nodeTypeException = %d + nodeTypeParentOnly = %d +) + +// numTLD is the number of top level domains. +const numTLD = %d + +` + fmt.Fprintf(w, header, *version, + nodesBitsChildren, nodesBitsICANN, nodesBitsTextOffset, nodesBitsTextLength, + childrenBitsWildcard, childrenBitsNodeType, childrenBitsHi, childrenBitsLo, + nodeTypeNormal, nodeTypeException, nodeTypeParentOnly, len(n.children)) + + text := combineText(labelsList) + if text == "" { + return fmt.Errorf("internal error: makeText returned no text") + } + for _, label := range labelsList { + offset, length := strings.Index(text, label), len(label) + if offset < 0 { + return fmt.Errorf("internal error: could not find %q in text %q", label, text) + } + maxTextOffset, maxTextLength = max(maxTextOffset, offset), max(maxTextLength, length) + if offset >= 1<= 1< 64 { + n, plus = 64, " +" + } + fmt.Fprintf(w, "%q%s\n", text[:n], plus) + text = text[n:] + } + + if err := n.walk(w, assignIndexes); err != nil { + return err + } + + fmt.Fprintf(w, ` + +// nodes is the list of nodes. Each node is represented as a uint32, which +// encodes the node's children, wildcard bit and node type (as an index into +// the children array), ICANN bit and text. +// +// If the table was generated with the -comments flag, there is a //-comment +// after each node's data. In it is the nodes-array indexes of the children, +// formatted as (n0x1234-n0x1256), with * denoting the wildcard bit. The +// nodeType is printed as + for normal, ! for exception, and o for parent-only +// nodes that have children but don't match a domain label in their own right. +// An I denotes an ICANN domain. +// +// The layout within the uint32, from MSB to LSB, is: +// [%2d bits] unused +// [%2d bits] children index +// [%2d bits] ICANN bit +// [%2d bits] text index +// [%2d bits] text length +var nodes = [...]uint32{ +`, + 32-nodesBitsChildren-nodesBitsICANN-nodesBitsTextOffset-nodesBitsTextLength, + nodesBitsChildren, nodesBitsICANN, nodesBitsTextOffset, nodesBitsTextLength) + if err := n.walk(w, printNode); err != nil { + return err + } + fmt.Fprintf(w, `} + +// children is the list of nodes' children, the parent's wildcard bit and the +// parent's node type. If a node has no children then their children index +// will be in the range [0, 6), depending on the wildcard bit and node type. +// +// The layout within the uint32, from MSB to LSB, is: +// [%2d bits] unused +// [%2d bits] wildcard bit +// [%2d bits] node type +// [%2d bits] high nodes index (exclusive) of children +// [%2d bits] low nodes index (inclusive) of children +var children=[...]uint32{ +`, + 32-childrenBitsWildcard-childrenBitsNodeType-childrenBitsHi-childrenBitsLo, + childrenBitsWildcard, childrenBitsNodeType, childrenBitsHi, childrenBitsLo) + for i, c := range childrenEncoding { + s := "---------------" + lo := c & (1<> childrenBitsLo) & (1<>(childrenBitsLo+childrenBitsHi)) & (1<>(childrenBitsLo+childrenBitsHi+childrenBitsNodeType) != 0 + if *comments { + fmt.Fprintf(w, "0x%08x, // c0x%04x (%s)%s %s\n", + c, i, s, wildcardStr(wildcard), nodeTypeStr(nodeType)) + } else { + fmt.Fprintf(w, "0x%x,\n", c) + } + } + fmt.Fprintf(w, "}\n\n") + fmt.Fprintf(w, "// max children %d (capacity %d)\n", maxChildren, 1<= 1<= 1<= 1< 0 && ss[0] == "" { + ss = ss[1:] + } + return ss +} + +// crush combines a list of strings, taking advantage of overlaps. It returns a +// single string that contains each input string as a substring. +func crush(ss []string) string { + maxLabelLen := 0 + for _, s := range ss { + if maxLabelLen < len(s) { + maxLabelLen = len(s) + } + } + + for prefixLen := maxLabelLen; prefixLen > 0; prefixLen-- { + prefixes := makePrefixMap(ss, prefixLen) + for i, s := range ss { + if len(s) <= prefixLen { + continue + } + mergeLabel(ss, i, prefixLen, prefixes) + } + } + + return strings.Join(ss, "") +} + +// mergeLabel merges the label at ss[i] with the first available matching label +// in prefixMap, where the last "prefixLen" characters in ss[i] match the first +// "prefixLen" characters in the matching label. +// It will merge ss[i] repeatedly until no more matches are available. +// All matching labels merged into ss[i] are replaced by "". +func mergeLabel(ss []string, i, prefixLen int, prefixes prefixMap) { + s := ss[i] + suffix := s[len(s)-prefixLen:] + for _, j := range prefixes[suffix] { + // Empty strings mean "already used." Also avoid merging with self. + if ss[j] == "" || i == j { + continue + } + if *v { + fmt.Fprintf(os.Stderr, "%d-length overlap at (%4d,%4d): %q and %q share %q\n", + prefixLen, i, j, ss[i], ss[j], suffix) + } + ss[i] += ss[j][prefixLen:] + ss[j] = "" + // ss[i] has a new suffix, so merge again if possible. + // Note: we only have to merge again at the same prefix length. Shorter + // prefix lengths will be handled in the next iteration of crush's for loop. + // Can there be matches for longer prefix lengths, introduced by the merge? + // I believe that any such matches would by necessity have been eliminated + // during substring removal or merged at a higher prefix length. For + // instance, in crush("abc", "cde", "bcdef"), combining "abc" and "cde" + // would yield "abcde", which could be merged with "bcdef." However, in + // practice "cde" would already have been elimintated by removeSubstrings. + mergeLabel(ss, i, prefixLen, prefixes) + return + } +} + +// prefixMap maps from a prefix to a list of strings containing that prefix. The +// list of strings is represented as indexes into a slice of strings stored +// elsewhere. +type prefixMap map[string][]int + +// makePrefixMap constructs a prefixMap from a slice of strings. +func makePrefixMap(ss []string, prefixLen int) prefixMap { + prefixes := make(prefixMap) + for i, s := range ss { + // We use < rather than <= because if a label matches on a prefix equal to + // its full length, that's actually a substring match handled by + // removeSubstrings. + if prefixLen < len(s) { + prefix := s[:prefixLen] + prefixes[prefix] = append(prefixes[prefix], i) + } + } + + return prefixes +} diff --git a/vendor/golang.org/x/net/publicsuffix/list.go b/vendor/golang.org/x/net/publicsuffix/list.go new file mode 100644 index 000000000..8bbf3bcd7 --- /dev/null +++ b/vendor/golang.org/x/net/publicsuffix/list.go @@ -0,0 +1,135 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:generate go run gen.go + +// Package publicsuffix provides a public suffix list based on data from +// http://publicsuffix.org/. A public suffix is one under which Internet users +// can directly register names. +package publicsuffix // import "golang.org/x/net/publicsuffix" + +// TODO: specify case sensitivity and leading/trailing dot behavior for +// func PublicSuffix and func EffectiveTLDPlusOne. + +import ( + "fmt" + "net/http/cookiejar" + "strings" +) + +// List implements the cookiejar.PublicSuffixList interface by calling the +// PublicSuffix function. +var List cookiejar.PublicSuffixList = list{} + +type list struct{} + +func (list) PublicSuffix(domain string) string { + ps, _ := PublicSuffix(domain) + return ps +} + +func (list) String() string { + return version +} + +// PublicSuffix returns the public suffix of the domain using a copy of the +// publicsuffix.org database compiled into the library. +// +// icann is whether the public suffix is managed by the Internet Corporation +// for Assigned Names and Numbers. If not, the public suffix is privately +// managed. For example, foo.org and foo.co.uk are ICANN domains, +// foo.dyndns.org and foo.blogspot.co.uk are private domains. +// +// Use cases for distinguishing ICANN domains like foo.com from private +// domains like foo.appspot.com can be found at +// https://wiki.mozilla.org/Public_Suffix_List/Use_Cases +func PublicSuffix(domain string) (publicSuffix string, icann bool) { + lo, hi := uint32(0), uint32(numTLD) + s, suffix, wildcard := domain, len(domain), false +loop: + for { + dot := strings.LastIndex(s, ".") + if wildcard { + suffix = 1 + dot + } + if lo == hi { + break + } + f := find(s[1+dot:], lo, hi) + if f == notFound { + break + } + + u := nodes[f] >> (nodesBitsTextOffset + nodesBitsTextLength) + icann = u&(1<>= nodesBitsICANN + u = children[u&(1<>= childrenBitsLo + hi = u & (1<>= childrenBitsHi + switch u & (1<>= childrenBitsNodeType + wildcard = u&(1<>= nodesBitsTextLength + offset := x & (1< 0 { + v.Set("scope", strings.Join(c.conf.Scopes, " ")) + } + for k, p := range c.conf.EndpointParams { + if _, ok := v[k]; ok { + return nil, fmt.Errorf("oauth2: cannot overwrite parameter %q", k) + } + v[k] = p + } + tk, err := internal.RetrieveToken(c.ctx, c.conf.ClientID, c.conf.ClientSecret, c.conf.TokenURL, v) + if err != nil { + if rErr, ok := err.(*internal.RetrieveError); ok { + return nil, (*oauth2.RetrieveError)(rErr) + } + return nil, err + } + t := &oauth2.Token{ + AccessToken: tk.AccessToken, + TokenType: tk.TokenType, + RefreshToken: tk.RefreshToken, + Expiry: tk.Expiry, + } + return t.WithExtra(tk.Raw), nil +}