adding vendored sources for local builds
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
parent
e2d6977b0a
commit
5699bdfa8f
434 changed files with 234158 additions and 0 deletions
17
go.mod
Normal file
17
go.mod
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
module git.batts.cloud/vbatts/token-server
|
||||||
|
|
||||||
|
go 1.22.5
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/distribution/distribution v2.8.3+incompatible
|
||||||
|
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7
|
||||||
|
github.com/gorilla/mux v1.8.1
|
||||||
|
github.com/sirupsen/logrus v1.9.3
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/docker/distribution v2.8.3+incompatible // indirect
|
||||||
|
golang.org/x/crypto v0.32.0 // indirect
|
||||||
|
golang.org/x/sys v0.29.0 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
|
||||||
|
)
|
202
vendor/github.com/distribution/distribution/LICENSE
generated
vendored
Normal file
202
vendor/github.com/distribution/distribution/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,202 @@
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
APPENDIX: How to apply the Apache License to your work.
|
||||||
|
|
||||||
|
To apply the Apache License to your work, attach the following
|
||||||
|
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||||
|
replaced with your own identifying information. (Don't include
|
||||||
|
the brackets!) The text should be enclosed in the appropriate
|
||||||
|
comment syntax for the file format. We also recommend that a
|
||||||
|
file or class name and description of purpose be included on the
|
||||||
|
same "printed page" as the copyright notice for easier
|
||||||
|
identification within third-party archives.
|
||||||
|
|
||||||
|
Copyright {yyyy} {name of copyright owner}
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
|
73
vendor/github.com/distribution/distribution/context/context.go
generated
vendored
Normal file
73
vendor/github.com/distribution/distribution/context/context.go
generated
vendored
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
package context
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/docker/distribution/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// instanceContext is a context that provides only an instance id. It is
|
||||||
|
// provided as the main background context.
|
||||||
|
type instanceContext struct {
|
||||||
|
context.Context
|
||||||
|
id string // id of context, logged as "instance.id"
|
||||||
|
once sync.Once // once protect generation of the id
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ic *instanceContext) Value(key interface{}) interface{} {
|
||||||
|
if key == "instance.id" {
|
||||||
|
ic.once.Do(func() {
|
||||||
|
// We want to lazy initialize the UUID such that we don't
|
||||||
|
// call a random generator from the package initialization
|
||||||
|
// code. For various reasons random could not be available
|
||||||
|
// https://github.com/docker/distribution/issues/782
|
||||||
|
ic.id = uuid.Generate().String()
|
||||||
|
})
|
||||||
|
return ic.id
|
||||||
|
}
|
||||||
|
|
||||||
|
return ic.Context.Value(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
var background = &instanceContext{
|
||||||
|
Context: context.Background(),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Background returns a non-nil, empty Context. The background context
|
||||||
|
// provides a single key, "instance.id" that is globally unique to the
|
||||||
|
// process.
|
||||||
|
func Background() context.Context {
|
||||||
|
return background
|
||||||
|
}
|
||||||
|
|
||||||
|
// stringMapContext is a simple context implementation that checks a map for a
|
||||||
|
// key, falling back to a parent if not present.
|
||||||
|
type stringMapContext struct {
|
||||||
|
context.Context
|
||||||
|
m map[string]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithValues returns a context that proxies lookups through a map. Only
|
||||||
|
// supports string keys.
|
||||||
|
func WithValues(ctx context.Context, m map[string]interface{}) context.Context {
|
||||||
|
mo := make(map[string]interface{}, len(m)) // make our own copy.
|
||||||
|
for k, v := range m {
|
||||||
|
mo[k] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
return stringMapContext{
|
||||||
|
Context: ctx,
|
||||||
|
m: mo,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (smc stringMapContext) Value(key interface{}) interface{} {
|
||||||
|
if ks, ok := key.(string); ok {
|
||||||
|
if v, ok := smc.m[ks]; ok {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return smc.Context.Value(key)
|
||||||
|
}
|
88
vendor/github.com/distribution/distribution/context/doc.go
generated
vendored
Normal file
88
vendor/github.com/distribution/distribution/context/doc.go
generated
vendored
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
// Package context provides several utilities for working with
|
||||||
|
// Go's context in http requests. Primarily, the focus is on logging relevant
|
||||||
|
// request information but this package is not limited to that purpose.
|
||||||
|
//
|
||||||
|
// The easiest way to get started is to get the background context:
|
||||||
|
//
|
||||||
|
// ctx := context.Background()
|
||||||
|
//
|
||||||
|
// The returned context should be passed around your application and be the
|
||||||
|
// root of all other context instances. If the application has a version, this
|
||||||
|
// line should be called before anything else:
|
||||||
|
//
|
||||||
|
// ctx := context.WithVersion(context.Background(), version)
|
||||||
|
//
|
||||||
|
// The above will store the version in the context and will be available to
|
||||||
|
// the logger.
|
||||||
|
//
|
||||||
|
// # Logging
|
||||||
|
//
|
||||||
|
// The most useful aspect of this package is GetLogger. This function takes
|
||||||
|
// any context.Context interface and returns the current logger from the
|
||||||
|
// context. Canonical usage looks like this:
|
||||||
|
//
|
||||||
|
// GetLogger(ctx).Infof("something interesting happened")
|
||||||
|
//
|
||||||
|
// GetLogger also takes optional key arguments. The keys will be looked up in
|
||||||
|
// the context and reported with the logger. The following example would
|
||||||
|
// return a logger that prints the version with each log message:
|
||||||
|
//
|
||||||
|
// ctx := context.Context(context.Background(), "version", version)
|
||||||
|
// GetLogger(ctx, "version").Infof("this log message has a version field")
|
||||||
|
//
|
||||||
|
// The above would print out a log message like this:
|
||||||
|
//
|
||||||
|
// INFO[0000] this log message has a version field version=v2.0.0-alpha.2.m
|
||||||
|
//
|
||||||
|
// When used with WithLogger, we gain the ability to decorate the context with
|
||||||
|
// loggers that have information from disparate parts of the call stack.
|
||||||
|
// Following from the version example, we can build a new context with the
|
||||||
|
// configured logger such that we always print the version field:
|
||||||
|
//
|
||||||
|
// ctx = WithLogger(ctx, GetLogger(ctx, "version"))
|
||||||
|
//
|
||||||
|
// Since the logger has been pushed to the context, we can now get the version
|
||||||
|
// field for free with our log messages. Future calls to GetLogger on the new
|
||||||
|
// context will have the version field:
|
||||||
|
//
|
||||||
|
// GetLogger(ctx).Infof("this log message has a version field")
|
||||||
|
//
|
||||||
|
// This becomes more powerful when we start stacking loggers. Let's say we
|
||||||
|
// have the version logger from above but also want a request id. Using the
|
||||||
|
// context above, in our request scoped function, we place another logger in
|
||||||
|
// the context:
|
||||||
|
//
|
||||||
|
// ctx = context.WithValue(ctx, "http.request.id", "unique id") // called when building request context
|
||||||
|
// ctx = WithLogger(ctx, GetLogger(ctx, "http.request.id"))
|
||||||
|
//
|
||||||
|
// When GetLogger is called on the new context, "http.request.id" will be
|
||||||
|
// included as a logger field, along with the original "version" field:
|
||||||
|
//
|
||||||
|
// INFO[0000] this log message has a version field http.request.id=unique id version=v2.0.0-alpha.2.m
|
||||||
|
//
|
||||||
|
// Note that this only affects the new context, the previous context, with the
|
||||||
|
// version field, can be used independently. Put another way, the new logger,
|
||||||
|
// added to the request context, is unique to that context and can have
|
||||||
|
// request scoped variables.
|
||||||
|
//
|
||||||
|
// # HTTP Requests
|
||||||
|
//
|
||||||
|
// This package also contains several methods for working with http requests.
|
||||||
|
// The concepts are very similar to those described above. We simply place the
|
||||||
|
// request in the context using WithRequest. This makes the request variables
|
||||||
|
// available. GetRequestLogger can then be called to get request specific
|
||||||
|
// variables in a log line:
|
||||||
|
//
|
||||||
|
// ctx = WithRequest(ctx, req)
|
||||||
|
// GetRequestLogger(ctx).Infof("request variables")
|
||||||
|
//
|
||||||
|
// Like above, if we want to include the request data in all log messages in
|
||||||
|
// the context, we push the logger to a new context and use that one:
|
||||||
|
//
|
||||||
|
// ctx = WithLogger(ctx, GetRequestLogger(ctx))
|
||||||
|
//
|
||||||
|
// The concept is fairly powerful and ensures that calls throughout the stack
|
||||||
|
// can be traced in log messages. Using the fields like "http.request.id", one
|
||||||
|
// can analyze call flow for a particular request with a simple grep of the
|
||||||
|
// logs.
|
||||||
|
package context
|
333
vendor/github.com/distribution/distribution/context/http.go
generated
vendored
Normal file
333
vendor/github.com/distribution/distribution/context/http.go
generated
vendored
Normal file
|
@ -0,0 +1,333 @@
|
||||||
|
package context
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/docker/distribution/uuid"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Common errors used with this package.
|
||||||
|
var (
|
||||||
|
ErrNoRequestContext = errors.New("no http request in context")
|
||||||
|
ErrNoResponseWriterContext = errors.New("no http response in context")
|
||||||
|
)
|
||||||
|
|
||||||
|
func parseIP(ipStr string) net.IP {
|
||||||
|
ip := net.ParseIP(ipStr)
|
||||||
|
if ip == nil {
|
||||||
|
log.Warnf("invalid remote IP address: %q", ipStr)
|
||||||
|
}
|
||||||
|
return ip
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoteAddr extracts the remote address of the request, taking into
|
||||||
|
// account proxy headers.
|
||||||
|
func RemoteAddr(r *http.Request) string {
|
||||||
|
if prior := r.Header.Get("X-Forwarded-For"); prior != "" {
|
||||||
|
proxies := strings.Split(prior, ",")
|
||||||
|
if len(proxies) > 0 {
|
||||||
|
remoteAddr := strings.Trim(proxies[0], " ")
|
||||||
|
if parseIP(remoteAddr) != nil {
|
||||||
|
return remoteAddr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// X-Real-Ip is less supported, but worth checking in the
|
||||||
|
// absence of X-Forwarded-For
|
||||||
|
if realIP := r.Header.Get("X-Real-Ip"); realIP != "" {
|
||||||
|
if parseIP(realIP) != nil {
|
||||||
|
return realIP
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.RemoteAddr
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoteIP extracts the remote IP of the request, taking into
|
||||||
|
// account proxy headers.
|
||||||
|
func RemoteIP(r *http.Request) string {
|
||||||
|
addr := RemoteAddr(r)
|
||||||
|
|
||||||
|
// Try parsing it as "IP:port"
|
||||||
|
if ip, _, err := net.SplitHostPort(addr); err == nil {
|
||||||
|
return ip
|
||||||
|
}
|
||||||
|
|
||||||
|
return addr
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithRequest places the request on the context. The context of the request
|
||||||
|
// is assigned a unique id, available at "http.request.id". The request itself
|
||||||
|
// is available at "http.request". Other common attributes are available under
|
||||||
|
// the prefix "http.request.". If a request is already present on the context,
|
||||||
|
// this method will panic.
|
||||||
|
func WithRequest(ctx context.Context, r *http.Request) context.Context {
|
||||||
|
if ctx.Value("http.request") != nil {
|
||||||
|
// NOTE(stevvooe): This needs to be considered a programming error. It
|
||||||
|
// is unlikely that we'd want to have more than one request in
|
||||||
|
// context.
|
||||||
|
panic("only one request per context")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &httpRequestContext{
|
||||||
|
Context: ctx,
|
||||||
|
startedAt: time.Now(),
|
||||||
|
id: uuid.Generate().String(),
|
||||||
|
r: r,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRequest returns the http request in the given context. Returns
|
||||||
|
// ErrNoRequestContext if the context does not have an http request associated
|
||||||
|
// with it.
|
||||||
|
func GetRequest(ctx context.Context) (*http.Request, error) {
|
||||||
|
if r, ok := ctx.Value("http.request").(*http.Request); r != nil && ok {
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
return nil, ErrNoRequestContext
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRequestID attempts to resolve the current request id, if possible. An
|
||||||
|
// error is return if it is not available on the context.
|
||||||
|
func GetRequestID(ctx context.Context) string {
|
||||||
|
return GetStringValue(ctx, "http.request.id")
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithResponseWriter returns a new context and response writer that makes
|
||||||
|
// interesting response statistics available within the context.
|
||||||
|
func WithResponseWriter(ctx context.Context, w http.ResponseWriter) (context.Context, http.ResponseWriter) {
|
||||||
|
irw := instrumentedResponseWriter{
|
||||||
|
ResponseWriter: w,
|
||||||
|
Context: ctx,
|
||||||
|
}
|
||||||
|
return &irw, &irw
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetResponseWriter returns the http.ResponseWriter from the provided
|
||||||
|
// context. If not present, ErrNoResponseWriterContext is returned. The
|
||||||
|
// returned instance provides instrumentation in the context.
|
||||||
|
func GetResponseWriter(ctx context.Context) (http.ResponseWriter, error) {
|
||||||
|
v := ctx.Value("http.response")
|
||||||
|
|
||||||
|
rw, ok := v.(http.ResponseWriter)
|
||||||
|
if !ok || rw == nil {
|
||||||
|
return nil, ErrNoResponseWriterContext
|
||||||
|
}
|
||||||
|
|
||||||
|
return rw, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getVarsFromRequest let's us change request vars implementation for testing
|
||||||
|
// and maybe future changes.
|
||||||
|
var getVarsFromRequest = mux.Vars
|
||||||
|
|
||||||
|
// WithVars extracts gorilla/mux vars and makes them available on the returned
|
||||||
|
// context. Variables are available at keys with the prefix "vars.". For
|
||||||
|
// example, if looking for the variable "name", it can be accessed as
|
||||||
|
// "vars.name". Implementations that are accessing values need not know that
|
||||||
|
// the underlying context is implemented with gorilla/mux vars.
|
||||||
|
func WithVars(ctx context.Context, r *http.Request) context.Context {
|
||||||
|
return &muxVarsContext{
|
||||||
|
Context: ctx,
|
||||||
|
vars: getVarsFromRequest(r),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRequestLogger returns a logger that contains fields from the request in
|
||||||
|
// the current context. If the request is not available in the context, no
|
||||||
|
// fields will display. Request loggers can safely be pushed onto the context.
|
||||||
|
func GetRequestLogger(ctx context.Context) Logger {
|
||||||
|
return GetLogger(ctx,
|
||||||
|
"http.request.id",
|
||||||
|
"http.request.method",
|
||||||
|
"http.request.host",
|
||||||
|
"http.request.uri",
|
||||||
|
"http.request.referer",
|
||||||
|
"http.request.useragent",
|
||||||
|
"http.request.remoteaddr",
|
||||||
|
"http.request.contenttype")
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetResponseLogger reads the current response stats and builds a logger.
|
||||||
|
// Because the values are read at call time, pushing a logger returned from
|
||||||
|
// this function on the context will lead to missing or invalid data. Only
|
||||||
|
// call this at the end of a request, after the response has been written.
|
||||||
|
func GetResponseLogger(ctx context.Context) Logger {
|
||||||
|
l := getLogrusLogger(ctx,
|
||||||
|
"http.response.written",
|
||||||
|
"http.response.status",
|
||||||
|
"http.response.contenttype")
|
||||||
|
|
||||||
|
duration := Since(ctx, "http.request.startedat")
|
||||||
|
|
||||||
|
if duration > 0 {
|
||||||
|
l = l.WithField("http.response.duration", duration.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
// httpRequestContext makes information about a request available to context.
|
||||||
|
type httpRequestContext struct {
|
||||||
|
context.Context
|
||||||
|
|
||||||
|
startedAt time.Time
|
||||||
|
id string
|
||||||
|
r *http.Request
|
||||||
|
}
|
||||||
|
|
||||||
|
// Value returns a keyed element of the request for use in the context. To get
|
||||||
|
// the request itself, query "request". For other components, access them as
|
||||||
|
// "request.<component>". For example, r.RequestURI
|
||||||
|
func (ctx *httpRequestContext) Value(key interface{}) interface{} {
|
||||||
|
if keyStr, ok := key.(string); ok {
|
||||||
|
if keyStr == "http.request" {
|
||||||
|
return ctx.r
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.HasPrefix(keyStr, "http.request.") {
|
||||||
|
goto fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := strings.Split(keyStr, ".")
|
||||||
|
|
||||||
|
if len(parts) != 3 {
|
||||||
|
goto fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
switch parts[2] {
|
||||||
|
case "uri":
|
||||||
|
return ctx.r.RequestURI
|
||||||
|
case "remoteaddr":
|
||||||
|
return RemoteAddr(ctx.r)
|
||||||
|
case "method":
|
||||||
|
return ctx.r.Method
|
||||||
|
case "host":
|
||||||
|
return ctx.r.Host
|
||||||
|
case "referer":
|
||||||
|
referer := ctx.r.Referer()
|
||||||
|
if referer != "" {
|
||||||
|
return referer
|
||||||
|
}
|
||||||
|
case "useragent":
|
||||||
|
return ctx.r.UserAgent()
|
||||||
|
case "id":
|
||||||
|
return ctx.id
|
||||||
|
case "startedat":
|
||||||
|
return ctx.startedAt
|
||||||
|
case "contenttype":
|
||||||
|
ct := ctx.r.Header.Get("Content-Type")
|
||||||
|
if ct != "" {
|
||||||
|
return ct
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fallback:
|
||||||
|
return ctx.Context.Value(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
type muxVarsContext struct {
|
||||||
|
context.Context
|
||||||
|
vars map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ctx *muxVarsContext) Value(key interface{}) interface{} {
|
||||||
|
if keyStr, ok := key.(string); ok {
|
||||||
|
if keyStr == "vars" {
|
||||||
|
return ctx.vars
|
||||||
|
}
|
||||||
|
|
||||||
|
if v, ok := ctx.vars[strings.TrimPrefix(keyStr, "vars.")]; ok {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx.Context.Value(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// instrumentedResponseWriter provides response writer information in a
|
||||||
|
// context. This variant is only used in the case where CloseNotifier is not
|
||||||
|
// implemented by the parent ResponseWriter.
|
||||||
|
type instrumentedResponseWriter struct {
|
||||||
|
http.ResponseWriter
|
||||||
|
context.Context
|
||||||
|
|
||||||
|
mu sync.Mutex
|
||||||
|
status int
|
||||||
|
written int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (irw *instrumentedResponseWriter) Write(p []byte) (n int, err error) {
|
||||||
|
n, err = irw.ResponseWriter.Write(p)
|
||||||
|
|
||||||
|
irw.mu.Lock()
|
||||||
|
irw.written += int64(n)
|
||||||
|
|
||||||
|
// Guess the likely status if not set.
|
||||||
|
if irw.status == 0 {
|
||||||
|
irw.status = http.StatusOK
|
||||||
|
}
|
||||||
|
|
||||||
|
irw.mu.Unlock()
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (irw *instrumentedResponseWriter) WriteHeader(status int) {
|
||||||
|
irw.ResponseWriter.WriteHeader(status)
|
||||||
|
|
||||||
|
irw.mu.Lock()
|
||||||
|
irw.status = status
|
||||||
|
irw.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (irw *instrumentedResponseWriter) Flush() {
|
||||||
|
if flusher, ok := irw.ResponseWriter.(http.Flusher); ok {
|
||||||
|
flusher.Flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (irw *instrumentedResponseWriter) Value(key interface{}) interface{} {
|
||||||
|
if keyStr, ok := key.(string); ok {
|
||||||
|
if keyStr == "http.response" {
|
||||||
|
return irw
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.HasPrefix(keyStr, "http.response.") {
|
||||||
|
goto fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := strings.Split(keyStr, ".")
|
||||||
|
|
||||||
|
if len(parts) != 3 {
|
||||||
|
goto fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
irw.mu.Lock()
|
||||||
|
defer irw.mu.Unlock()
|
||||||
|
|
||||||
|
switch parts[2] {
|
||||||
|
case "written":
|
||||||
|
return irw.written
|
||||||
|
case "status":
|
||||||
|
return irw.status
|
||||||
|
case "contenttype":
|
||||||
|
contentType := irw.Header().Get("Content-Type")
|
||||||
|
if contentType != "" {
|
||||||
|
return contentType
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fallback:
|
||||||
|
return irw.Context.Value(key)
|
||||||
|
}
|
121
vendor/github.com/distribution/distribution/context/logger.go
generated
vendored
Normal file
121
vendor/github.com/distribution/distribution/context/logger.go
generated
vendored
Normal file
|
@ -0,0 +1,121 @@
|
||||||
|
package context
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Logger provides a leveled-logging interface.
|
||||||
|
type Logger interface {
|
||||||
|
// standard logger methods
|
||||||
|
Print(args ...interface{})
|
||||||
|
Printf(format string, args ...interface{})
|
||||||
|
Println(args ...interface{})
|
||||||
|
|
||||||
|
Fatal(args ...interface{})
|
||||||
|
Fatalf(format string, args ...interface{})
|
||||||
|
Fatalln(args ...interface{})
|
||||||
|
|
||||||
|
Panic(args ...interface{})
|
||||||
|
Panicf(format string, args ...interface{})
|
||||||
|
Panicln(args ...interface{})
|
||||||
|
|
||||||
|
// Leveled methods, from logrus
|
||||||
|
Debug(args ...interface{})
|
||||||
|
Debugf(format string, args ...interface{})
|
||||||
|
Debugln(args ...interface{})
|
||||||
|
|
||||||
|
Error(args ...interface{})
|
||||||
|
Errorf(format string, args ...interface{})
|
||||||
|
Errorln(args ...interface{})
|
||||||
|
|
||||||
|
Info(args ...interface{})
|
||||||
|
Infof(format string, args ...interface{})
|
||||||
|
Infoln(args ...interface{})
|
||||||
|
|
||||||
|
Warn(args ...interface{})
|
||||||
|
Warnf(format string, args ...interface{})
|
||||||
|
Warnln(args ...interface{})
|
||||||
|
|
||||||
|
WithError(err error) *logrus.Entry
|
||||||
|
}
|
||||||
|
|
||||||
|
type loggerKey struct{}
|
||||||
|
|
||||||
|
// WithLogger creates a new context with provided logger.
|
||||||
|
func WithLogger(ctx context.Context, logger Logger) context.Context {
|
||||||
|
return context.WithValue(ctx, loggerKey{}, logger)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLoggerWithField returns a logger instance with the specified field key
|
||||||
|
// and value without affecting the context. Extra specified keys will be
|
||||||
|
// resolved from the context.
|
||||||
|
func GetLoggerWithField(ctx context.Context, key, value interface{}, keys ...interface{}) Logger {
|
||||||
|
return getLogrusLogger(ctx, keys...).WithField(fmt.Sprint(key), value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLoggerWithFields returns a logger instance with the specified fields
|
||||||
|
// without affecting the context. Extra specified keys will be resolved from
|
||||||
|
// the context.
|
||||||
|
func GetLoggerWithFields(ctx context.Context, fields map[interface{}]interface{}, keys ...interface{}) Logger {
|
||||||
|
// must convert from interface{} -> interface{} to string -> interface{} for logrus.
|
||||||
|
lfields := make(logrus.Fields, len(fields))
|
||||||
|
for key, value := range fields {
|
||||||
|
lfields[fmt.Sprint(key)] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
return getLogrusLogger(ctx, keys...).WithFields(lfields)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLogger returns the logger from the current context, if present. If one
|
||||||
|
// or more keys are provided, they will be resolved on the context and
|
||||||
|
// included in the logger. While context.Value takes an interface, any key
|
||||||
|
// argument passed to GetLogger will be passed to fmt.Sprint when expanded as
|
||||||
|
// a logging key field. If context keys are integer constants, for example,
|
||||||
|
// its recommended that a String method is implemented.
|
||||||
|
func GetLogger(ctx context.Context, keys ...interface{}) Logger {
|
||||||
|
return getLogrusLogger(ctx, keys...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLogrusLogger returns the logrus logger for the context. If one more keys
|
||||||
|
// are provided, they will be resolved on the context and included in the
|
||||||
|
// logger. Only use this function if specific logrus functionality is
|
||||||
|
// required.
|
||||||
|
func getLogrusLogger(ctx context.Context, keys ...interface{}) *logrus.Entry {
|
||||||
|
var logger *logrus.Entry
|
||||||
|
|
||||||
|
// Get a logger, if it is present.
|
||||||
|
loggerInterface := ctx.Value(loggerKey{})
|
||||||
|
if loggerInterface != nil {
|
||||||
|
if lgr, ok := loggerInterface.(*logrus.Entry); ok {
|
||||||
|
logger = lgr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if logger == nil {
|
||||||
|
fields := logrus.Fields{}
|
||||||
|
|
||||||
|
// Fill in the instance id, if we have it.
|
||||||
|
instanceID := ctx.Value("instance.id")
|
||||||
|
if instanceID != nil {
|
||||||
|
fields["instance.id"] = instanceID
|
||||||
|
}
|
||||||
|
|
||||||
|
fields["go.version"] = runtime.Version()
|
||||||
|
// If no logger is found, just return the standard logger.
|
||||||
|
logger = logrus.StandardLogger().WithFields(fields)
|
||||||
|
}
|
||||||
|
|
||||||
|
fields := logrus.Fields{}
|
||||||
|
for _, key := range keys {
|
||||||
|
v := ctx.Value(key)
|
||||||
|
if v != nil {
|
||||||
|
fields[fmt.Sprint(key)] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return logger.WithFields(fields)
|
||||||
|
}
|
105
vendor/github.com/distribution/distribution/context/trace.go
generated
vendored
Normal file
105
vendor/github.com/distribution/distribution/context/trace.go
generated
vendored
Normal file
|
@ -0,0 +1,105 @@
|
||||||
|
package context
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"runtime"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/docker/distribution/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// WithTrace allocates a traced timing span in a new context. This allows a
|
||||||
|
// caller to track the time between calling WithTrace and the returned done
|
||||||
|
// function. When the done function is called, a log message is emitted with a
|
||||||
|
// "trace.duration" field, corresponding to the elapsed time and a
|
||||||
|
// "trace.func" field, corresponding to the function that called WithTrace.
|
||||||
|
//
|
||||||
|
// The logging keys "trace.id" and "trace.parent.id" are provided to implement
|
||||||
|
// dapper-like tracing. This function should be complemented with a WithSpan
|
||||||
|
// method that could be used for tracing distributed RPC calls.
|
||||||
|
//
|
||||||
|
// The main benefit of this function is to post-process log messages or
|
||||||
|
// intercept them in a hook to provide timing data. Trace ids and parent ids
|
||||||
|
// can also be linked to provide call tracing, if so required.
|
||||||
|
//
|
||||||
|
// Here is an example of the usage:
|
||||||
|
//
|
||||||
|
// func timedOperation(ctx Context) {
|
||||||
|
// ctx, done := WithTrace(ctx)
|
||||||
|
// defer done("this will be the log message")
|
||||||
|
// // ... function body ...
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// If the function ran for roughly 1s, such a usage would emit a log message
|
||||||
|
// as follows:
|
||||||
|
//
|
||||||
|
// INFO[0001] this will be the log message trace.duration=1.004575763s trace.func=github.com/docker/distribution/context.traceOperation trace.id=<id> ...
|
||||||
|
//
|
||||||
|
// Notice that the function name is automatically resolved, along with the
|
||||||
|
// package and a trace id is emitted that can be linked with parent ids.
|
||||||
|
func WithTrace(ctx context.Context) (context.Context, func(format string, a ...interface{})) {
|
||||||
|
if ctx == nil {
|
||||||
|
ctx = Background()
|
||||||
|
}
|
||||||
|
|
||||||
|
pc, file, line, _ := runtime.Caller(1)
|
||||||
|
f := runtime.FuncForPC(pc)
|
||||||
|
ctx = &traced{
|
||||||
|
Context: ctx,
|
||||||
|
id: uuid.Generate().String(),
|
||||||
|
start: time.Now(),
|
||||||
|
parent: GetStringValue(ctx, "trace.id"),
|
||||||
|
fnname: f.Name(),
|
||||||
|
file: file,
|
||||||
|
line: line,
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx, func(format string, a ...interface{}) {
|
||||||
|
GetLogger(ctx,
|
||||||
|
"trace.duration",
|
||||||
|
"trace.id",
|
||||||
|
"trace.parent.id",
|
||||||
|
"trace.func",
|
||||||
|
"trace.file",
|
||||||
|
"trace.line").
|
||||||
|
Debugf(format, a...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// traced represents a context that is traced for function call timing. It
|
||||||
|
// also provides fast lookup for the various attributes that are available on
|
||||||
|
// the trace.
|
||||||
|
type traced struct {
|
||||||
|
context.Context
|
||||||
|
id string
|
||||||
|
parent string
|
||||||
|
start time.Time
|
||||||
|
fnname string
|
||||||
|
file string
|
||||||
|
line int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ts *traced) Value(key interface{}) interface{} {
|
||||||
|
switch key {
|
||||||
|
case "trace.start":
|
||||||
|
return ts.start
|
||||||
|
case "trace.duration":
|
||||||
|
return time.Since(ts.start)
|
||||||
|
case "trace.id":
|
||||||
|
return ts.id
|
||||||
|
case "trace.parent.id":
|
||||||
|
if ts.parent == "" {
|
||||||
|
return nil // must return nil to signal no parent.
|
||||||
|
}
|
||||||
|
|
||||||
|
return ts.parent
|
||||||
|
case "trace.func":
|
||||||
|
return ts.fnname
|
||||||
|
case "trace.file":
|
||||||
|
return ts.file
|
||||||
|
case "trace.line":
|
||||||
|
return ts.line
|
||||||
|
}
|
||||||
|
|
||||||
|
return ts.Context.Value(key)
|
||||||
|
}
|
25
vendor/github.com/distribution/distribution/context/util.go
generated
vendored
Normal file
25
vendor/github.com/distribution/distribution/context/util.go
generated
vendored
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
package context
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Since looks up key, which should be a time.Time, and returns the duration
|
||||||
|
// since that time. If the key is not found, the value returned will be zero.
|
||||||
|
// This is helpful when inferring metrics related to context execution times.
|
||||||
|
func Since(ctx context.Context, key interface{}) time.Duration {
|
||||||
|
if startedAt, ok := ctx.Value(key).(time.Time); ok {
|
||||||
|
return time.Since(startedAt)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetStringValue returns a string value from the context. The empty string
|
||||||
|
// will be returned if not found.
|
||||||
|
func GetStringValue(ctx context.Context, key interface{}) (value string) {
|
||||||
|
if valuev, ok := ctx.Value(key).(string); ok {
|
||||||
|
value = valuev
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
22
vendor/github.com/distribution/distribution/context/version.go
generated
vendored
Normal file
22
vendor/github.com/distribution/distribution/context/version.go
generated
vendored
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
package context
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
|
type versionKey struct{}
|
||||||
|
|
||||||
|
func (versionKey) String() string { return "version" }
|
||||||
|
|
||||||
|
// WithVersion stores the application version in the context. The new context
|
||||||
|
// gets a logger to ensure log messages are marked with the application
|
||||||
|
// version.
|
||||||
|
func WithVersion(ctx context.Context, version string) context.Context {
|
||||||
|
ctx = context.WithValue(ctx, versionKey{}, version)
|
||||||
|
// push a new logger onto the stack
|
||||||
|
return WithLogger(ctx, GetLogger(ctx, versionKey{}))
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetVersion returns the application version from the context. An empty
|
||||||
|
// string may returned if the version was not set on the context.
|
||||||
|
func GetVersion(ctx context.Context) string {
|
||||||
|
return GetStringValue(ctx, versionKey{})
|
||||||
|
}
|
267
vendor/github.com/distribution/distribution/registry/api/errcode/errors.go
generated
vendored
Normal file
267
vendor/github.com/distribution/distribution/registry/api/errcode/errors.go
generated
vendored
Normal file
|
@ -0,0 +1,267 @@
|
||||||
|
package errcode
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ErrorCoder is the base interface for ErrorCode and Error allowing
|
||||||
|
// users of each to just call ErrorCode to get the real ID of each
|
||||||
|
type ErrorCoder interface {
|
||||||
|
ErrorCode() ErrorCode
|
||||||
|
}
|
||||||
|
|
||||||
|
// ErrorCode represents the error type. The errors are serialized via strings
|
||||||
|
// and the integer format may change and should *never* be exported.
|
||||||
|
type ErrorCode int
|
||||||
|
|
||||||
|
var _ error = ErrorCode(0)
|
||||||
|
|
||||||
|
// ErrorCode just returns itself
|
||||||
|
func (ec ErrorCode) ErrorCode() ErrorCode {
|
||||||
|
return ec
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error returns the ID/Value
|
||||||
|
func (ec ErrorCode) Error() string {
|
||||||
|
// NOTE(stevvooe): Cannot use message here since it may have unpopulated args.
|
||||||
|
return strings.ToLower(strings.Replace(ec.String(), "_", " ", -1))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Descriptor returns the descriptor for the error code.
|
||||||
|
func (ec ErrorCode) Descriptor() ErrorDescriptor {
|
||||||
|
d, ok := errorCodeToDescriptors[ec]
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return ErrorCodeUnknown.Descriptor()
|
||||||
|
}
|
||||||
|
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns the canonical identifier for this error code.
|
||||||
|
func (ec ErrorCode) String() string {
|
||||||
|
return ec.Descriptor().Value
|
||||||
|
}
|
||||||
|
|
||||||
|
// Message returned the human-readable error message for this error code.
|
||||||
|
func (ec ErrorCode) Message() string {
|
||||||
|
return ec.Descriptor().Message
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalText encodes the receiver into UTF-8-encoded text and returns the
|
||||||
|
// result.
|
||||||
|
func (ec ErrorCode) MarshalText() (text []byte, err error) {
|
||||||
|
return []byte(ec.String()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalText decodes the form generated by MarshalText.
|
||||||
|
func (ec *ErrorCode) UnmarshalText(text []byte) error {
|
||||||
|
desc, ok := idToDescriptors[string(text)]
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
desc = ErrorCodeUnknown.Descriptor()
|
||||||
|
}
|
||||||
|
|
||||||
|
*ec = desc.Code
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithMessage creates a new Error struct based on the passed-in info and
|
||||||
|
// overrides the Message property.
|
||||||
|
func (ec ErrorCode) WithMessage(message string) Error {
|
||||||
|
return Error{
|
||||||
|
Code: ec,
|
||||||
|
Message: message,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithDetail creates a new Error struct based on the passed-in info and
|
||||||
|
// set the Detail property appropriately
|
||||||
|
func (ec ErrorCode) WithDetail(detail interface{}) Error {
|
||||||
|
return Error{
|
||||||
|
Code: ec,
|
||||||
|
Message: ec.Message(),
|
||||||
|
}.WithDetail(detail)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithArgs creates a new Error struct and sets the Args slice
|
||||||
|
func (ec ErrorCode) WithArgs(args ...interface{}) Error {
|
||||||
|
return Error{
|
||||||
|
Code: ec,
|
||||||
|
Message: ec.Message(),
|
||||||
|
}.WithArgs(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error provides a wrapper around ErrorCode with extra Details provided.
|
||||||
|
type Error struct {
|
||||||
|
Code ErrorCode `json:"code"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Detail interface{} `json:"detail,omitempty"`
|
||||||
|
|
||||||
|
// TODO(duglin): See if we need an "args" property so we can do the
|
||||||
|
// variable substitution right before showing the message to the user
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ error = Error{}
|
||||||
|
|
||||||
|
// ErrorCode returns the ID/Value of this Error
|
||||||
|
func (e Error) ErrorCode() ErrorCode {
|
||||||
|
return e.Code
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error returns a human readable representation of the error.
|
||||||
|
func (e Error) Error() string {
|
||||||
|
return fmt.Sprintf("%s: %s", e.Code.Error(), e.Message)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithDetail will return a new Error, based on the current one, but with
|
||||||
|
// some Detail info added
|
||||||
|
func (e Error) WithDetail(detail interface{}) Error {
|
||||||
|
return Error{
|
||||||
|
Code: e.Code,
|
||||||
|
Message: e.Message,
|
||||||
|
Detail: detail,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithArgs uses the passed-in list of interface{} as the substitution
|
||||||
|
// variables in the Error's Message string, but returns a new Error
|
||||||
|
func (e Error) WithArgs(args ...interface{}) Error {
|
||||||
|
return Error{
|
||||||
|
Code: e.Code,
|
||||||
|
Message: fmt.Sprintf(e.Code.Message(), args...),
|
||||||
|
Detail: e.Detail,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ErrorDescriptor provides relevant information about a given error code.
|
||||||
|
type ErrorDescriptor struct {
|
||||||
|
// Code is the error code that this descriptor describes.
|
||||||
|
Code ErrorCode
|
||||||
|
|
||||||
|
// Value provides a unique, string key, often captilized with
|
||||||
|
// underscores, to identify the error code. This value is used as the
|
||||||
|
// keyed value when serializing api errors.
|
||||||
|
Value string
|
||||||
|
|
||||||
|
// Message is a short, human readable decription of the error condition
|
||||||
|
// included in API responses.
|
||||||
|
Message string
|
||||||
|
|
||||||
|
// Description provides a complete account of the errors purpose, suitable
|
||||||
|
// for use in documentation.
|
||||||
|
Description string
|
||||||
|
|
||||||
|
// HTTPStatusCode provides the http status code that is associated with
|
||||||
|
// this error condition.
|
||||||
|
HTTPStatusCode int
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseErrorCode returns the value by the string error code.
|
||||||
|
// `ErrorCodeUnknown` will be returned if the error is not known.
|
||||||
|
func ParseErrorCode(value string) ErrorCode {
|
||||||
|
ed, ok := idToDescriptors[value]
|
||||||
|
if ok {
|
||||||
|
return ed.Code
|
||||||
|
}
|
||||||
|
|
||||||
|
return ErrorCodeUnknown
|
||||||
|
}
|
||||||
|
|
||||||
|
// Errors provides the envelope for multiple errors and a few sugar methods
|
||||||
|
// for use within the application.
|
||||||
|
type Errors []error
|
||||||
|
|
||||||
|
var _ error = Errors{}
|
||||||
|
|
||||||
|
func (errs Errors) Error() string {
|
||||||
|
switch len(errs) {
|
||||||
|
case 0:
|
||||||
|
return "<nil>"
|
||||||
|
case 1:
|
||||||
|
return errs[0].Error()
|
||||||
|
default:
|
||||||
|
msg := "errors:\n"
|
||||||
|
for _, err := range errs {
|
||||||
|
msg += err.Error() + "\n"
|
||||||
|
}
|
||||||
|
return msg
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Len returns the current number of errors.
|
||||||
|
func (errs Errors) Len() int {
|
||||||
|
return len(errs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalJSON converts slice of error, ErrorCode or Error into a
|
||||||
|
// slice of Error - then serializes
|
||||||
|
func (errs Errors) MarshalJSON() ([]byte, error) {
|
||||||
|
var tmpErrs struct {
|
||||||
|
Errors []Error `json:"errors,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, daErr := range errs {
|
||||||
|
var err Error
|
||||||
|
|
||||||
|
switch daErr := daErr.(type) {
|
||||||
|
case ErrorCode:
|
||||||
|
err = daErr.WithDetail(nil)
|
||||||
|
case Error:
|
||||||
|
err = daErr
|
||||||
|
default:
|
||||||
|
err = ErrorCodeUnknown.WithDetail(daErr)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the Error struct was setup and they forgot to set the
|
||||||
|
// Message field (meaning its "") then grab it from the ErrCode
|
||||||
|
msg := err.Message
|
||||||
|
if msg == "" {
|
||||||
|
msg = err.Code.Message()
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpErrs.Errors = append(tmpErrs.Errors, Error{
|
||||||
|
Code: err.Code,
|
||||||
|
Message: msg,
|
||||||
|
Detail: err.Detail,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.Marshal(tmpErrs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON deserializes []Error and then converts it into slice of
|
||||||
|
// Error or ErrorCode
|
||||||
|
func (errs *Errors) UnmarshalJSON(data []byte) error {
|
||||||
|
var tmpErrs struct {
|
||||||
|
Errors []Error
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(data, &tmpErrs); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var newErrs Errors
|
||||||
|
for _, daErr := range tmpErrs.Errors {
|
||||||
|
// If Message is empty or exactly matches the Code's message string
|
||||||
|
// then just use the Code, no need for a full Error struct
|
||||||
|
if daErr.Detail == nil && (daErr.Message == "" || daErr.Message == daErr.Code.Message()) {
|
||||||
|
// Error's w/o details get converted to ErrorCode
|
||||||
|
newErrs = append(newErrs, daErr.Code)
|
||||||
|
} else {
|
||||||
|
// Error's w/ details are untouched
|
||||||
|
newErrs = append(newErrs, Error{
|
||||||
|
Code: daErr.Code,
|
||||||
|
Message: daErr.Message,
|
||||||
|
Detail: daErr.Detail,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*errs = newErrs
|
||||||
|
return nil
|
||||||
|
}
|
40
vendor/github.com/distribution/distribution/registry/api/errcode/handler.go
generated
vendored
Normal file
40
vendor/github.com/distribution/distribution/registry/api/errcode/handler.go
generated
vendored
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
package errcode
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ServeJSON attempts to serve the errcode in a JSON envelope. It marshals err
|
||||||
|
// and sets the content-type header to 'application/json'. It will handle
|
||||||
|
// ErrorCoder and Errors, and if necessary will create an envelope.
|
||||||
|
func ServeJSON(w http.ResponseWriter, err error) error {
|
||||||
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
|
var sc int
|
||||||
|
|
||||||
|
switch errs := err.(type) {
|
||||||
|
case Errors:
|
||||||
|
if len(errs) < 1 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if err, ok := errs[0].(ErrorCoder); ok {
|
||||||
|
sc = err.ErrorCode().Descriptor().HTTPStatusCode
|
||||||
|
}
|
||||||
|
case ErrorCoder:
|
||||||
|
sc = errs.ErrorCode().Descriptor().HTTPStatusCode
|
||||||
|
err = Errors{err} // create an envelope.
|
||||||
|
default:
|
||||||
|
// We just have an unhandled error type, so just place in an envelope
|
||||||
|
// and move along.
|
||||||
|
err = Errors{err}
|
||||||
|
}
|
||||||
|
|
||||||
|
if sc == 0 {
|
||||||
|
sc = http.StatusInternalServerError
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(sc)
|
||||||
|
|
||||||
|
return json.NewEncoder(w).Encode(err)
|
||||||
|
}
|
138
vendor/github.com/distribution/distribution/registry/api/errcode/register.go
generated
vendored
Normal file
138
vendor/github.com/distribution/distribution/registry/api/errcode/register.go
generated
vendored
Normal file
|
@ -0,0 +1,138 @@
|
||||||
|
package errcode
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"sort"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
errorCodeToDescriptors = map[ErrorCode]ErrorDescriptor{}
|
||||||
|
idToDescriptors = map[string]ErrorDescriptor{}
|
||||||
|
groupToDescriptors = map[string][]ErrorDescriptor{}
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrorCodeUnknown is a generic error that can be used as a last
|
||||||
|
// resort if there is no situation-specific error message that can be used
|
||||||
|
ErrorCodeUnknown = Register("errcode", ErrorDescriptor{
|
||||||
|
Value: "UNKNOWN",
|
||||||
|
Message: "unknown error",
|
||||||
|
Description: `Generic error returned when the error does not have an
|
||||||
|
API classification.`,
|
||||||
|
HTTPStatusCode: http.StatusInternalServerError,
|
||||||
|
})
|
||||||
|
|
||||||
|
// ErrorCodeUnsupported is returned when an operation is not supported.
|
||||||
|
ErrorCodeUnsupported = Register("errcode", ErrorDescriptor{
|
||||||
|
Value: "UNSUPPORTED",
|
||||||
|
Message: "The operation is unsupported.",
|
||||||
|
Description: `The operation was unsupported due to a missing
|
||||||
|
implementation or invalid set of parameters.`,
|
||||||
|
HTTPStatusCode: http.StatusMethodNotAllowed,
|
||||||
|
})
|
||||||
|
|
||||||
|
// ErrorCodeUnauthorized is returned if a request requires
|
||||||
|
// authentication.
|
||||||
|
ErrorCodeUnauthorized = Register("errcode", ErrorDescriptor{
|
||||||
|
Value: "UNAUTHORIZED",
|
||||||
|
Message: "authentication required",
|
||||||
|
Description: `The access controller was unable to authenticate
|
||||||
|
the client. Often this will be accompanied by a
|
||||||
|
Www-Authenticate HTTP response header indicating how to
|
||||||
|
authenticate.`,
|
||||||
|
HTTPStatusCode: http.StatusUnauthorized,
|
||||||
|
})
|
||||||
|
|
||||||
|
// ErrorCodeDenied is returned if a client does not have sufficient
|
||||||
|
// permission to perform an action.
|
||||||
|
ErrorCodeDenied = Register("errcode", ErrorDescriptor{
|
||||||
|
Value: "DENIED",
|
||||||
|
Message: "requested access to the resource is denied",
|
||||||
|
Description: `The access controller denied access for the
|
||||||
|
operation on a resource.`,
|
||||||
|
HTTPStatusCode: http.StatusForbidden,
|
||||||
|
})
|
||||||
|
|
||||||
|
// ErrorCodeUnavailable provides a common error to report unavailability
|
||||||
|
// of a service or endpoint.
|
||||||
|
ErrorCodeUnavailable = Register("errcode", ErrorDescriptor{
|
||||||
|
Value: "UNAVAILABLE",
|
||||||
|
Message: "service unavailable",
|
||||||
|
Description: "Returned when a service is not available",
|
||||||
|
HTTPStatusCode: http.StatusServiceUnavailable,
|
||||||
|
})
|
||||||
|
|
||||||
|
// ErrorCodeTooManyRequests is returned if a client attempts too many
|
||||||
|
// times to contact a service endpoint.
|
||||||
|
ErrorCodeTooManyRequests = Register("errcode", ErrorDescriptor{
|
||||||
|
Value: "TOOMANYREQUESTS",
|
||||||
|
Message: "too many requests",
|
||||||
|
Description: `Returned when a client attempts to contact a
|
||||||
|
service too many times`,
|
||||||
|
HTTPStatusCode: http.StatusTooManyRequests,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
var nextCode = 1000
|
||||||
|
var registerLock sync.Mutex
|
||||||
|
|
||||||
|
// Register will make the passed-in error known to the environment and
|
||||||
|
// return a new ErrorCode
|
||||||
|
func Register(group string, descriptor ErrorDescriptor) ErrorCode {
|
||||||
|
registerLock.Lock()
|
||||||
|
defer registerLock.Unlock()
|
||||||
|
|
||||||
|
descriptor.Code = ErrorCode(nextCode)
|
||||||
|
|
||||||
|
if _, ok := idToDescriptors[descriptor.Value]; ok {
|
||||||
|
panic(fmt.Sprintf("ErrorValue %q is already registered", descriptor.Value))
|
||||||
|
}
|
||||||
|
if _, ok := errorCodeToDescriptors[descriptor.Code]; ok {
|
||||||
|
panic(fmt.Sprintf("ErrorCode %v is already registered", descriptor.Code))
|
||||||
|
}
|
||||||
|
|
||||||
|
groupToDescriptors[group] = append(groupToDescriptors[group], descriptor)
|
||||||
|
errorCodeToDescriptors[descriptor.Code] = descriptor
|
||||||
|
idToDescriptors[descriptor.Value] = descriptor
|
||||||
|
|
||||||
|
nextCode++
|
||||||
|
return descriptor.Code
|
||||||
|
}
|
||||||
|
|
||||||
|
type byValue []ErrorDescriptor
|
||||||
|
|
||||||
|
func (a byValue) Len() int { return len(a) }
|
||||||
|
func (a byValue) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||||
|
func (a byValue) Less(i, j int) bool { return a[i].Value < a[j].Value }
|
||||||
|
|
||||||
|
// GetGroupNames returns the list of Error group names that are registered
|
||||||
|
func GetGroupNames() []string {
|
||||||
|
keys := []string{}
|
||||||
|
|
||||||
|
for k := range groupToDescriptors {
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
|
sort.Strings(keys)
|
||||||
|
return keys
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetErrorCodeGroup returns the named group of error descriptors
|
||||||
|
func GetErrorCodeGroup(name string) []ErrorDescriptor {
|
||||||
|
desc := groupToDescriptors[name]
|
||||||
|
sort.Sort(byValue(desc))
|
||||||
|
return desc
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetErrorAllDescriptors returns a slice of all ErrorDescriptors that are
|
||||||
|
// registered, irrespective of what group they're in
|
||||||
|
func GetErrorAllDescriptors() []ErrorDescriptor {
|
||||||
|
result := []ErrorDescriptor{}
|
||||||
|
|
||||||
|
for _, group := range GetGroupNames() {
|
||||||
|
result = append(result, GetErrorCodeGroup(group)...)
|
||||||
|
}
|
||||||
|
sort.Sort(byValue(result))
|
||||||
|
return result
|
||||||
|
}
|
200
vendor/github.com/distribution/distribution/registry/auth/auth.go
generated
vendored
Normal file
200
vendor/github.com/distribution/distribution/registry/auth/auth.go
generated
vendored
Normal file
|
@ -0,0 +1,200 @@
|
||||||
|
// Package auth defines a standard interface for request access controllers.
|
||||||
|
//
|
||||||
|
// An access controller has a simple interface with a single `Authorized`
|
||||||
|
// method which checks that a given request is authorized to perform one or
|
||||||
|
// more actions on one or more resources. This method should return a non-nil
|
||||||
|
// error if the request is not authorized.
|
||||||
|
//
|
||||||
|
// An implementation registers its access controller by name with a constructor
|
||||||
|
// which accepts an options map for configuring the access controller.
|
||||||
|
//
|
||||||
|
// options := map[string]interface{}{"sillySecret": "whysosilly?"}
|
||||||
|
// accessController, _ := auth.GetAccessController("silly", options)
|
||||||
|
//
|
||||||
|
// This `accessController` can then be used in a request handler like so:
|
||||||
|
//
|
||||||
|
// func updateOrder(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// orderNumber := r.FormValue("orderNumber")
|
||||||
|
// resource := auth.Resource{Type: "customerOrder", Name: orderNumber}
|
||||||
|
// access := auth.Access{Resource: resource, Action: "update"}
|
||||||
|
//
|
||||||
|
// if ctx, err := accessController.Authorized(ctx, access); err != nil {
|
||||||
|
// if challenge, ok := err.(auth.Challenge) {
|
||||||
|
// // Let the challenge write the response.
|
||||||
|
// challenge.SetHeaders(r, w)
|
||||||
|
// w.WriteHeader(http.StatusUnauthorized)
|
||||||
|
// return
|
||||||
|
// } else {
|
||||||
|
// // Some other error.
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// UserKey is used to get the user object from
|
||||||
|
// a user context
|
||||||
|
UserKey = "auth.user"
|
||||||
|
|
||||||
|
// UserNameKey is used to get the user name from
|
||||||
|
// a user context
|
||||||
|
UserNameKey = "auth.user.name"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrInvalidCredential is returned when the auth token does not authenticate correctly.
|
||||||
|
ErrInvalidCredential = errors.New("invalid authorization credential")
|
||||||
|
|
||||||
|
// ErrAuthenticationFailure returned when authentication fails.
|
||||||
|
ErrAuthenticationFailure = errors.New("authentication failure")
|
||||||
|
)
|
||||||
|
|
||||||
|
// UserInfo carries information about
|
||||||
|
// an autenticated/authorized client.
|
||||||
|
type UserInfo struct {
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resource describes a resource by type and name.
|
||||||
|
type Resource struct {
|
||||||
|
Type string
|
||||||
|
Class string
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Access describes a specific action that is
|
||||||
|
// requested or allowed for a given resource.
|
||||||
|
type Access struct {
|
||||||
|
Resource
|
||||||
|
Action string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Challenge is a special error type which is used for HTTP 401 Unauthorized
|
||||||
|
// responses and is able to write the response with WWW-Authenticate challenge
|
||||||
|
// header values based on the error.
|
||||||
|
type Challenge interface {
|
||||||
|
error
|
||||||
|
|
||||||
|
// SetHeaders prepares the request to conduct a challenge response by
|
||||||
|
// adding the an HTTP challenge header on the response message. Callers
|
||||||
|
// are expected to set the appropriate HTTP status code (e.g. 401)
|
||||||
|
// themselves.
|
||||||
|
SetHeaders(r *http.Request, w http.ResponseWriter)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AccessController controls access to registry resources based on a request
|
||||||
|
// and required access levels for a request. Implementations can support both
|
||||||
|
// complete denial and http authorization challenges.
|
||||||
|
type AccessController interface {
|
||||||
|
// Authorized returns a non-nil error if the context is granted access and
|
||||||
|
// returns a new authorized context. If one or more Access structs are
|
||||||
|
// provided, the requested access will be compared with what is available
|
||||||
|
// to the context. The given context will contain a "http.request" key with
|
||||||
|
// a `*http.Request` value. If the error is non-nil, access should always
|
||||||
|
// be denied. The error may be of type Challenge, in which case the caller
|
||||||
|
// may have the Challenge handle the request or choose what action to take
|
||||||
|
// based on the Challenge header or response status. The returned context
|
||||||
|
// object should have a "auth.user" value set to a UserInfo struct.
|
||||||
|
Authorized(ctx context.Context, access ...Access) (context.Context, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CredentialAuthenticator is an object which is able to authenticate credentials
|
||||||
|
type CredentialAuthenticator interface {
|
||||||
|
AuthenticateUser(username, password string) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithUser returns a context with the authorized user info.
|
||||||
|
func WithUser(ctx context.Context, user UserInfo) context.Context {
|
||||||
|
return userInfoContext{
|
||||||
|
Context: ctx,
|
||||||
|
user: user,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type userInfoContext struct {
|
||||||
|
context.Context
|
||||||
|
user UserInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
func (uic userInfoContext) Value(key interface{}) interface{} {
|
||||||
|
switch key {
|
||||||
|
case UserKey:
|
||||||
|
return uic.user
|
||||||
|
case UserNameKey:
|
||||||
|
return uic.user.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
return uic.Context.Value(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithResources returns a context with the authorized resources.
|
||||||
|
func WithResources(ctx context.Context, resources []Resource) context.Context {
|
||||||
|
return resourceContext{
|
||||||
|
Context: ctx,
|
||||||
|
resources: resources,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type resourceContext struct {
|
||||||
|
context.Context
|
||||||
|
resources []Resource
|
||||||
|
}
|
||||||
|
|
||||||
|
type resourceKey struct{}
|
||||||
|
|
||||||
|
func (rc resourceContext) Value(key interface{}) interface{} {
|
||||||
|
if key == (resourceKey{}) {
|
||||||
|
return rc.resources
|
||||||
|
}
|
||||||
|
|
||||||
|
return rc.Context.Value(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AuthorizedResources returns the list of resources which have
|
||||||
|
// been authorized for this request.
|
||||||
|
func AuthorizedResources(ctx context.Context) []Resource {
|
||||||
|
if resources, ok := ctx.Value(resourceKey{}).([]Resource); ok {
|
||||||
|
return resources
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// InitFunc is the type of an AccessController factory function and is used
|
||||||
|
// to register the constructor for different AccesController backends.
|
||||||
|
type InitFunc func(options map[string]interface{}) (AccessController, error)
|
||||||
|
|
||||||
|
var accessControllers map[string]InitFunc
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
accessControllers = make(map[string]InitFunc)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register is used to register an InitFunc for
|
||||||
|
// an AccessController backend with the given name.
|
||||||
|
func Register(name string, initFunc InitFunc) error {
|
||||||
|
if _, exists := accessControllers[name]; exists {
|
||||||
|
return fmt.Errorf("name already registered: %s", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
accessControllers[name] = initFunc
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAccessController constructs an AccessController
|
||||||
|
// with the given options using the named backend.
|
||||||
|
func GetAccessController(name string, options map[string]interface{}) (AccessController, error) {
|
||||||
|
if initFunc, exists := accessControllers[name]; exists {
|
||||||
|
return initFunc(options)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("no access controller registered with name: %s", name)
|
||||||
|
}
|
160
vendor/github.com/distribution/distribution/registry/auth/htpasswd/access.go
generated
vendored
Normal file
160
vendor/github.com/distribution/distribution/registry/auth/htpasswd/access.go
generated
vendored
Normal file
|
@ -0,0 +1,160 @@
|
||||||
|
// Package htpasswd provides a simple authentication scheme that checks for the
|
||||||
|
// user credential hash in an htpasswd formatted file in a configuration-determined
|
||||||
|
// location.
|
||||||
|
//
|
||||||
|
// This authentication method MUST be used under TLS, as simple token-replay attack is possible.
|
||||||
|
package htpasswd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/base64"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
|
||||||
|
dcontext "github.com/docker/distribution/context"
|
||||||
|
"github.com/docker/distribution/registry/auth"
|
||||||
|
)
|
||||||
|
|
||||||
|
type accessController struct {
|
||||||
|
realm string
|
||||||
|
path string
|
||||||
|
modtime time.Time
|
||||||
|
mu sync.Mutex
|
||||||
|
htpasswd *htpasswd
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ auth.AccessController = &accessController{}
|
||||||
|
|
||||||
|
func newAccessController(options map[string]interface{}) (auth.AccessController, error) {
|
||||||
|
realm, present := options["realm"]
|
||||||
|
if _, ok := realm.(string); !present || !ok {
|
||||||
|
return nil, fmt.Errorf(`"realm" must be set for htpasswd access controller`)
|
||||||
|
}
|
||||||
|
|
||||||
|
pathOpt, present := options["path"]
|
||||||
|
path, ok := pathOpt.(string)
|
||||||
|
if !present || !ok {
|
||||||
|
return nil, fmt.Errorf(`"path" must be set for htpasswd access controller`)
|
||||||
|
}
|
||||||
|
if err := createHtpasswdFile(path); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &accessController{realm: realm.(string), path: path}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ac *accessController) Authorized(ctx context.Context, accessRecords ...auth.Access) (context.Context, error) {
|
||||||
|
req, err := dcontext.GetRequest(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
username, password, ok := req.BasicAuth()
|
||||||
|
if !ok {
|
||||||
|
return nil, &challenge{
|
||||||
|
realm: ac.realm,
|
||||||
|
err: auth.ErrInvalidCredential,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dynamically parsing the latest account list
|
||||||
|
fstat, err := os.Stat(ac.path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
lastModified := fstat.ModTime()
|
||||||
|
ac.mu.Lock()
|
||||||
|
if ac.htpasswd == nil || !ac.modtime.Equal(lastModified) {
|
||||||
|
ac.modtime = lastModified
|
||||||
|
|
||||||
|
f, err := os.Open(ac.path)
|
||||||
|
if err != nil {
|
||||||
|
ac.mu.Unlock()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
h, err := newHTPasswd(f)
|
||||||
|
if err != nil {
|
||||||
|
ac.mu.Unlock()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
ac.htpasswd = h
|
||||||
|
}
|
||||||
|
localHTPasswd := ac.htpasswd
|
||||||
|
ac.mu.Unlock()
|
||||||
|
|
||||||
|
if err := localHTPasswd.authenticateUser(username, password); err != nil {
|
||||||
|
dcontext.GetLogger(ctx).Errorf("error authenticating user %q: %v", username, err)
|
||||||
|
return nil, &challenge{
|
||||||
|
realm: ac.realm,
|
||||||
|
err: auth.ErrAuthenticationFailure,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return auth.WithUser(ctx, auth.UserInfo{Name: username}), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// challenge implements the auth.Challenge interface.
|
||||||
|
type challenge struct {
|
||||||
|
realm string
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ auth.Challenge = challenge{}
|
||||||
|
|
||||||
|
// SetHeaders sets the basic challenge header on the response.
|
||||||
|
func (ch challenge) SetHeaders(r *http.Request, w http.ResponseWriter) {
|
||||||
|
w.Header().Set("WWW-Authenticate", fmt.Sprintf("Basic realm=%q", ch.realm))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ch challenge) Error() string {
|
||||||
|
return fmt.Sprintf("basic authentication challenge for realm %q: %s", ch.realm, ch.err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// createHtpasswdFile creates and populates htpasswd file with a new user in case the file is missing
|
||||||
|
func createHtpasswdFile(path string) error {
|
||||||
|
if f, err := os.Open(path); err == nil {
|
||||||
|
f.Close()
|
||||||
|
return nil
|
||||||
|
} else if !os.IsNotExist(err) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0600)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to open htpasswd path %s", err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
var secretBytes [32]byte
|
||||||
|
if _, err := rand.Read(secretBytes[:]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
pass := base64.RawURLEncoding.EncodeToString(secretBytes[:])
|
||||||
|
encryptedPass, err := bcrypt.GenerateFromPassword([]byte(pass), bcrypt.DefaultCost)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := f.Write([]byte(fmt.Sprintf("docker:%s", string(encryptedPass[:])))); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
dcontext.GetLoggerWithFields(context.Background(), map[interface{}]interface{}{
|
||||||
|
"user": "docker",
|
||||||
|
"password": pass,
|
||||||
|
}).Warnf("htpasswd is missing, provisioning with default user")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
auth.Register("htpasswd", auth.InitFunc(newAccessController))
|
||||||
|
}
|
82
vendor/github.com/distribution/distribution/registry/auth/htpasswd/htpasswd.go
generated
vendored
Normal file
82
vendor/github.com/distribution/distribution/registry/auth/htpasswd/htpasswd.go
generated
vendored
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
package htpasswd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/docker/distribution/registry/auth"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// htpasswd holds a path to a system .htpasswd file and the machinery to parse
|
||||||
|
// it. Only bcrypt hash entries are supported.
|
||||||
|
type htpasswd struct {
|
||||||
|
entries map[string][]byte // maps username to password byte slice.
|
||||||
|
}
|
||||||
|
|
||||||
|
// newHTPasswd parses the reader and returns an htpasswd or an error.
|
||||||
|
func newHTPasswd(rd io.Reader) (*htpasswd, error) {
|
||||||
|
entries, err := parseHTPasswd(rd)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &htpasswd{entries: entries}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AuthenticateUser checks a given user:password credential against the
|
||||||
|
// receiving HTPasswd's file. If the check passes, nil is returned.
|
||||||
|
func (htpasswd *htpasswd) authenticateUser(username string, password string) error {
|
||||||
|
credentials, ok := htpasswd.entries[username]
|
||||||
|
if !ok {
|
||||||
|
// timing attack paranoia
|
||||||
|
bcrypt.CompareHashAndPassword([]byte{}, []byte(password))
|
||||||
|
|
||||||
|
return auth.ErrAuthenticationFailure
|
||||||
|
}
|
||||||
|
|
||||||
|
err := bcrypt.CompareHashAndPassword(credentials, []byte(password))
|
||||||
|
if err != nil {
|
||||||
|
return auth.ErrAuthenticationFailure
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseHTPasswd parses the contents of htpasswd. This will read all the
|
||||||
|
// entries in the file, whether or not they are needed. An error is returned
|
||||||
|
// if a syntax errors are encountered or if the reader fails.
|
||||||
|
func parseHTPasswd(rd io.Reader) (map[string][]byte, error) {
|
||||||
|
entries := map[string][]byte{}
|
||||||
|
scanner := bufio.NewScanner(rd)
|
||||||
|
var line int
|
||||||
|
for scanner.Scan() {
|
||||||
|
line++ // 1-based line numbering
|
||||||
|
t := strings.TrimSpace(scanner.Text())
|
||||||
|
|
||||||
|
if len(t) < 1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// lines that *begin* with a '#' are considered comments
|
||||||
|
if t[0] == '#' {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
i := strings.Index(t, ":")
|
||||||
|
if i < 0 || i >= len(t) {
|
||||||
|
return nil, fmt.Errorf("htpasswd: invalid entry at line %d: %q", line, scanner.Text())
|
||||||
|
}
|
||||||
|
|
||||||
|
entries[t[:i]] = []byte(t[i+1:])
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return entries, nil
|
||||||
|
}
|
293
vendor/github.com/distribution/distribution/registry/auth/token/accesscontroller.go
generated
vendored
Normal file
293
vendor/github.com/distribution/distribution/registry/auth/token/accesscontroller.go
generated
vendored
Normal file
|
@ -0,0 +1,293 @@
|
||||||
|
package token
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/pem"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
dcontext "github.com/docker/distribution/context"
|
||||||
|
"github.com/docker/distribution/registry/auth"
|
||||||
|
"github.com/docker/libtrust"
|
||||||
|
)
|
||||||
|
|
||||||
|
// accessSet maps a typed, named resource to
|
||||||
|
// a set of actions requested or authorized.
|
||||||
|
type accessSet map[auth.Resource]actionSet
|
||||||
|
|
||||||
|
// newAccessSet constructs an accessSet from
|
||||||
|
// a variable number of auth.Access items.
|
||||||
|
func newAccessSet(accessItems ...auth.Access) accessSet {
|
||||||
|
accessSet := make(accessSet, len(accessItems))
|
||||||
|
|
||||||
|
for _, access := range accessItems {
|
||||||
|
resource := auth.Resource{
|
||||||
|
Type: access.Type,
|
||||||
|
Name: access.Name,
|
||||||
|
}
|
||||||
|
|
||||||
|
set, exists := accessSet[resource]
|
||||||
|
if !exists {
|
||||||
|
set = newActionSet()
|
||||||
|
accessSet[resource] = set
|
||||||
|
}
|
||||||
|
|
||||||
|
set.add(access.Action)
|
||||||
|
}
|
||||||
|
|
||||||
|
return accessSet
|
||||||
|
}
|
||||||
|
|
||||||
|
// contains returns whether or not the given access is in this accessSet.
|
||||||
|
func (s accessSet) contains(access auth.Access) bool {
|
||||||
|
actionSet, ok := s[access.Resource]
|
||||||
|
if ok {
|
||||||
|
return actionSet.contains(access.Action)
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// scopeParam returns a collection of scopes which can
|
||||||
|
// be used for a WWW-Authenticate challenge parameter.
|
||||||
|
// See https://tools.ietf.org/html/rfc6750#section-3
|
||||||
|
func (s accessSet) scopeParam() string {
|
||||||
|
scopes := make([]string, 0, len(s))
|
||||||
|
|
||||||
|
for resource, actionSet := range s {
|
||||||
|
actions := strings.Join(actionSet.keys(), ",")
|
||||||
|
scopes = append(scopes, fmt.Sprintf("%s:%s:%s", resource.Type, resource.Name, actions))
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Join(scopes, " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Errors used and exported by this package.
|
||||||
|
var (
|
||||||
|
ErrInsufficientScope = errors.New("insufficient scope")
|
||||||
|
ErrTokenRequired = errors.New("authorization token required")
|
||||||
|
)
|
||||||
|
|
||||||
|
// authChallenge implements the auth.Challenge interface.
|
||||||
|
type authChallenge struct {
|
||||||
|
err error
|
||||||
|
realm string
|
||||||
|
autoRedirect bool
|
||||||
|
service string
|
||||||
|
accessSet accessSet
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ auth.Challenge = authChallenge{}
|
||||||
|
|
||||||
|
// Error returns the internal error string for this authChallenge.
|
||||||
|
func (ac authChallenge) Error() string {
|
||||||
|
return ac.err.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Status returns the HTTP Response Status Code for this authChallenge.
|
||||||
|
func (ac authChallenge) Status() int {
|
||||||
|
return http.StatusUnauthorized
|
||||||
|
}
|
||||||
|
|
||||||
|
// challengeParams constructs the value to be used in
|
||||||
|
// the WWW-Authenticate response challenge header.
|
||||||
|
// See https://tools.ietf.org/html/rfc6750#section-3
|
||||||
|
func (ac authChallenge) challengeParams(r *http.Request) string {
|
||||||
|
var realm string
|
||||||
|
if ac.autoRedirect {
|
||||||
|
realm = fmt.Sprintf("https://%s/auth/token", r.Host)
|
||||||
|
} else {
|
||||||
|
realm = ac.realm
|
||||||
|
}
|
||||||
|
str := fmt.Sprintf("Bearer realm=%q,service=%q", realm, ac.service)
|
||||||
|
|
||||||
|
if scope := ac.accessSet.scopeParam(); scope != "" {
|
||||||
|
str = fmt.Sprintf("%s,scope=%q", str, scope)
|
||||||
|
}
|
||||||
|
|
||||||
|
if ac.err == ErrInvalidToken || ac.err == ErrMalformedToken {
|
||||||
|
str = fmt.Sprintf("%s,error=%q", str, "invalid_token")
|
||||||
|
} else if ac.err == ErrInsufficientScope {
|
||||||
|
str = fmt.Sprintf("%s,error=%q", str, "insufficient_scope")
|
||||||
|
}
|
||||||
|
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetChallenge sets the WWW-Authenticate value for the response.
|
||||||
|
func (ac authChallenge) SetHeaders(r *http.Request, w http.ResponseWriter) {
|
||||||
|
w.Header().Add("WWW-Authenticate", ac.challengeParams(r))
|
||||||
|
}
|
||||||
|
|
||||||
|
// accessController implements the auth.AccessController interface.
|
||||||
|
type accessController struct {
|
||||||
|
realm string
|
||||||
|
autoRedirect bool
|
||||||
|
issuer string
|
||||||
|
service string
|
||||||
|
rootCerts *x509.CertPool
|
||||||
|
trustedKeys map[string]libtrust.PublicKey
|
||||||
|
}
|
||||||
|
|
||||||
|
// tokenAccessOptions is a convenience type for handling
|
||||||
|
// options to the contstructor of an accessController.
|
||||||
|
type tokenAccessOptions struct {
|
||||||
|
realm string
|
||||||
|
autoRedirect bool
|
||||||
|
issuer string
|
||||||
|
service string
|
||||||
|
rootCertBundle string
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkOptions gathers the necessary options
|
||||||
|
// for an accessController from the given map.
|
||||||
|
func checkOptions(options map[string]interface{}) (tokenAccessOptions, error) {
|
||||||
|
var opts tokenAccessOptions
|
||||||
|
|
||||||
|
keys := []string{"realm", "issuer", "service", "rootcertbundle"}
|
||||||
|
vals := make([]string, 0, len(keys))
|
||||||
|
for _, key := range keys {
|
||||||
|
val, ok := options[key].(string)
|
||||||
|
if !ok {
|
||||||
|
return opts, fmt.Errorf("token auth requires a valid option string: %q", key)
|
||||||
|
}
|
||||||
|
vals = append(vals, val)
|
||||||
|
}
|
||||||
|
|
||||||
|
opts.realm, opts.issuer, opts.service, opts.rootCertBundle = vals[0], vals[1], vals[2], vals[3]
|
||||||
|
|
||||||
|
autoRedirectVal, ok := options["autoredirect"]
|
||||||
|
if ok {
|
||||||
|
autoRedirect, ok := autoRedirectVal.(bool)
|
||||||
|
if !ok {
|
||||||
|
return opts, fmt.Errorf("token auth requires a valid option bool: autoredirect")
|
||||||
|
}
|
||||||
|
opts.autoRedirect = autoRedirect
|
||||||
|
}
|
||||||
|
|
||||||
|
return opts, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// newAccessController creates an accessController using the given options.
|
||||||
|
func newAccessController(options map[string]interface{}) (auth.AccessController, error) {
|
||||||
|
config, err := checkOptions(options)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
fp, err := os.Open(config.rootCertBundle)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to open token auth root certificate bundle file %q: %s", config.rootCertBundle, err)
|
||||||
|
}
|
||||||
|
defer fp.Close()
|
||||||
|
|
||||||
|
rawCertBundle, err := ioutil.ReadAll(fp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to read token auth root certificate bundle file %q: %s", config.rootCertBundle, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var rootCerts []*x509.Certificate
|
||||||
|
pemBlock, rawCertBundle := pem.Decode(rawCertBundle)
|
||||||
|
for pemBlock != nil {
|
||||||
|
if pemBlock.Type == "CERTIFICATE" {
|
||||||
|
cert, err := x509.ParseCertificate(pemBlock.Bytes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to parse token auth root certificate: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rootCerts = append(rootCerts, cert)
|
||||||
|
}
|
||||||
|
|
||||||
|
pemBlock, rawCertBundle = pem.Decode(rawCertBundle)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(rootCerts) == 0 {
|
||||||
|
return nil, errors.New("token auth requires at least one token signing root certificate")
|
||||||
|
}
|
||||||
|
|
||||||
|
rootPool := x509.NewCertPool()
|
||||||
|
trustedKeys := make(map[string]libtrust.PublicKey, len(rootCerts))
|
||||||
|
for _, rootCert := range rootCerts {
|
||||||
|
rootPool.AddCert(rootCert)
|
||||||
|
pubKey, err := libtrust.FromCryptoPublicKey(crypto.PublicKey(rootCert.PublicKey))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to get public key from token auth root certificate: %s", err)
|
||||||
|
}
|
||||||
|
trustedKeys[pubKey.KeyID()] = pubKey
|
||||||
|
}
|
||||||
|
|
||||||
|
return &accessController{
|
||||||
|
realm: config.realm,
|
||||||
|
autoRedirect: config.autoRedirect,
|
||||||
|
issuer: config.issuer,
|
||||||
|
service: config.service,
|
||||||
|
rootCerts: rootPool,
|
||||||
|
trustedKeys: trustedKeys,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Authorized handles checking whether the given request is authorized
|
||||||
|
// for actions on resources described by the given access items.
|
||||||
|
func (ac *accessController) Authorized(ctx context.Context, accessItems ...auth.Access) (context.Context, error) {
|
||||||
|
challenge := &authChallenge{
|
||||||
|
realm: ac.realm,
|
||||||
|
autoRedirect: ac.autoRedirect,
|
||||||
|
service: ac.service,
|
||||||
|
accessSet: newAccessSet(accessItems...),
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := dcontext.GetRequest(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := strings.Split(req.Header.Get("Authorization"), " ")
|
||||||
|
|
||||||
|
if len(parts) != 2 || strings.ToLower(parts[0]) != "bearer" {
|
||||||
|
challenge.err = ErrTokenRequired
|
||||||
|
return nil, challenge
|
||||||
|
}
|
||||||
|
|
||||||
|
rawToken := parts[1]
|
||||||
|
|
||||||
|
token, err := NewToken(rawToken)
|
||||||
|
if err != nil {
|
||||||
|
challenge.err = err
|
||||||
|
return nil, challenge
|
||||||
|
}
|
||||||
|
|
||||||
|
verifyOpts := VerifyOptions{
|
||||||
|
TrustedIssuers: []string{ac.issuer},
|
||||||
|
AcceptedAudiences: []string{ac.service},
|
||||||
|
Roots: ac.rootCerts,
|
||||||
|
TrustedKeys: ac.trustedKeys,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = token.Verify(verifyOpts); err != nil {
|
||||||
|
challenge.err = err
|
||||||
|
return nil, challenge
|
||||||
|
}
|
||||||
|
|
||||||
|
accessSet := token.accessSet()
|
||||||
|
for _, access := range accessItems {
|
||||||
|
if !accessSet.contains(access) {
|
||||||
|
challenge.err = ErrInsufficientScope
|
||||||
|
return nil, challenge
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx = auth.WithResources(ctx, token.resources())
|
||||||
|
|
||||||
|
return auth.WithUser(ctx, auth.UserInfo{Name: token.Claims.Subject}), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// init handles registering the token auth backend.
|
||||||
|
func init() {
|
||||||
|
auth.Register("token", auth.InitFunc(newAccessController))
|
||||||
|
}
|
35
vendor/github.com/distribution/distribution/registry/auth/token/stringset.go
generated
vendored
Normal file
35
vendor/github.com/distribution/distribution/registry/auth/token/stringset.go
generated
vendored
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
package token
|
||||||
|
|
||||||
|
// StringSet is a useful type for looking up strings.
|
||||||
|
type stringSet map[string]struct{}
|
||||||
|
|
||||||
|
// NewStringSet creates a new StringSet with the given strings.
|
||||||
|
func newStringSet(keys ...string) stringSet {
|
||||||
|
ss := make(stringSet, len(keys))
|
||||||
|
ss.add(keys...)
|
||||||
|
return ss
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add inserts the given keys into this StringSet.
|
||||||
|
func (ss stringSet) add(keys ...string) {
|
||||||
|
for _, key := range keys {
|
||||||
|
ss[key] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Contains returns whether the given key is in this StringSet.
|
||||||
|
func (ss stringSet) contains(key string) bool {
|
||||||
|
_, ok := ss[key]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keys returns a slice of all keys in this StringSet.
|
||||||
|
func (ss stringSet) keys() []string {
|
||||||
|
keys := make([]string, 0, len(ss))
|
||||||
|
|
||||||
|
for key := range ss {
|
||||||
|
keys = append(keys, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
return keys
|
||||||
|
}
|
380
vendor/github.com/distribution/distribution/registry/auth/token/token.go
generated
vendored
Normal file
380
vendor/github.com/distribution/distribution/registry/auth/token/token.go
generated
vendored
Normal file
|
@ -0,0 +1,380 @@
|
||||||
|
package token
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/docker/libtrust"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/docker/distribution/registry/auth"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// TokenSeparator is the value which separates the header, claims, and
|
||||||
|
// signature in the compact serialization of a JSON Web Token.
|
||||||
|
TokenSeparator = "."
|
||||||
|
// Leeway is the Duration that will be added to NBF and EXP claim
|
||||||
|
// checks to account for clock skew as per https://tools.ietf.org/html/rfc7519#section-4.1.5
|
||||||
|
Leeway = 60 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
|
// Errors used by token parsing and verification.
|
||||||
|
var (
|
||||||
|
ErrMalformedToken = errors.New("malformed token")
|
||||||
|
ErrInvalidToken = errors.New("invalid token")
|
||||||
|
)
|
||||||
|
|
||||||
|
// ResourceActions stores allowed actions on a named and typed resource.
|
||||||
|
type ResourceActions struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Class string `json:"class,omitempty"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Actions []string `json:"actions"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ClaimSet describes the main section of a JSON Web Token.
|
||||||
|
type ClaimSet struct {
|
||||||
|
// Public claims
|
||||||
|
Issuer string `json:"iss"`
|
||||||
|
Subject string `json:"sub"`
|
||||||
|
Audience string `json:"aud"`
|
||||||
|
Expiration int64 `json:"exp"`
|
||||||
|
NotBefore int64 `json:"nbf"`
|
||||||
|
IssuedAt int64 `json:"iat"`
|
||||||
|
JWTID string `json:"jti"`
|
||||||
|
|
||||||
|
// Private claims
|
||||||
|
Access []*ResourceActions `json:"access"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Header describes the header section of a JSON Web Token.
|
||||||
|
type Header struct {
|
||||||
|
Type string `json:"typ"`
|
||||||
|
SigningAlg string `json:"alg"`
|
||||||
|
KeyID string `json:"kid,omitempty"`
|
||||||
|
X5c []string `json:"x5c,omitempty"`
|
||||||
|
RawJWK *json.RawMessage `json:"jwk,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Token describes a JSON Web Token.
|
||||||
|
type Token struct {
|
||||||
|
Raw string
|
||||||
|
Header *Header
|
||||||
|
Claims *ClaimSet
|
||||||
|
Signature []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// VerifyOptions is used to specify
|
||||||
|
// options when verifying a JSON Web Token.
|
||||||
|
type VerifyOptions struct {
|
||||||
|
TrustedIssuers []string
|
||||||
|
AcceptedAudiences []string
|
||||||
|
Roots *x509.CertPool
|
||||||
|
TrustedKeys map[string]libtrust.PublicKey
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewToken parses the given raw token string
|
||||||
|
// and constructs an unverified JSON Web Token.
|
||||||
|
func NewToken(rawToken string) (*Token, error) {
|
||||||
|
parts := strings.Split(rawToken, TokenSeparator)
|
||||||
|
if len(parts) != 3 {
|
||||||
|
return nil, ErrMalformedToken
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
rawHeader, rawClaims = parts[0], parts[1]
|
||||||
|
headerJSON, claimsJSON []byte
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if err != nil {
|
||||||
|
log.Infof("error while unmarshalling raw token: %s", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
if headerJSON, err = joseBase64UrlDecode(rawHeader); err != nil {
|
||||||
|
err = fmt.Errorf("unable to decode header: %s", err)
|
||||||
|
return nil, ErrMalformedToken
|
||||||
|
}
|
||||||
|
|
||||||
|
if claimsJSON, err = joseBase64UrlDecode(rawClaims); err != nil {
|
||||||
|
err = fmt.Errorf("unable to decode claims: %s", err)
|
||||||
|
return nil, ErrMalformedToken
|
||||||
|
}
|
||||||
|
|
||||||
|
token := new(Token)
|
||||||
|
token.Header = new(Header)
|
||||||
|
token.Claims = new(ClaimSet)
|
||||||
|
|
||||||
|
token.Raw = strings.Join(parts[:2], TokenSeparator)
|
||||||
|
if token.Signature, err = joseBase64UrlDecode(parts[2]); err != nil {
|
||||||
|
err = fmt.Errorf("unable to decode signature: %s", err)
|
||||||
|
return nil, ErrMalformedToken
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = json.Unmarshal(headerJSON, token.Header); err != nil {
|
||||||
|
return nil, ErrMalformedToken
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = json.Unmarshal(claimsJSON, token.Claims); err != nil {
|
||||||
|
return nil, ErrMalformedToken
|
||||||
|
}
|
||||||
|
|
||||||
|
return token, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify attempts to verify this token using the given options.
|
||||||
|
// Returns a nil error if the token is valid.
|
||||||
|
func (t *Token) Verify(verifyOpts VerifyOptions) error {
|
||||||
|
// Verify that the Issuer claim is a trusted authority.
|
||||||
|
if !contains(verifyOpts.TrustedIssuers, t.Claims.Issuer) {
|
||||||
|
log.Infof("token from untrusted issuer: %q", t.Claims.Issuer)
|
||||||
|
return ErrInvalidToken
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify that the Audience claim is allowed.
|
||||||
|
if !contains(verifyOpts.AcceptedAudiences, t.Claims.Audience) {
|
||||||
|
log.Infof("token intended for another audience: %q", t.Claims.Audience)
|
||||||
|
return ErrInvalidToken
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify that the token is currently usable and not expired.
|
||||||
|
currentTime := time.Now()
|
||||||
|
|
||||||
|
ExpWithLeeway := time.Unix(t.Claims.Expiration, 0).Add(Leeway)
|
||||||
|
if currentTime.After(ExpWithLeeway) {
|
||||||
|
log.Infof("token not to be used after %s - currently %s", ExpWithLeeway, currentTime)
|
||||||
|
return ErrInvalidToken
|
||||||
|
}
|
||||||
|
|
||||||
|
NotBeforeWithLeeway := time.Unix(t.Claims.NotBefore, 0).Add(-Leeway)
|
||||||
|
if currentTime.Before(NotBeforeWithLeeway) {
|
||||||
|
log.Infof("token not to be used before %s - currently %s", NotBeforeWithLeeway, currentTime)
|
||||||
|
return ErrInvalidToken
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify the token signature.
|
||||||
|
if len(t.Signature) == 0 {
|
||||||
|
log.Info("token has no signature")
|
||||||
|
return ErrInvalidToken
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify that the signing key is trusted.
|
||||||
|
signingKey, err := t.VerifySigningKey(verifyOpts)
|
||||||
|
if err != nil {
|
||||||
|
log.Info(err)
|
||||||
|
return ErrInvalidToken
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finally, verify the signature of the token using the key which signed it.
|
||||||
|
if err := signingKey.Verify(strings.NewReader(t.Raw), t.Header.SigningAlg, t.Signature); err != nil {
|
||||||
|
log.Infof("unable to verify token signature: %s", err)
|
||||||
|
return ErrInvalidToken
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// VerifySigningKey attempts to get the key which was used to sign this token.
|
||||||
|
// The token header should contain either of these 3 fields:
|
||||||
|
//
|
||||||
|
// `x5c` - The x509 certificate chain for the signing key. Needs to be
|
||||||
|
// verified.
|
||||||
|
// `jwk` - The JSON Web Key representation of the signing key.
|
||||||
|
// May contain its own `x5c` field which needs to be verified.
|
||||||
|
// `kid` - The unique identifier for the key. This library interprets it
|
||||||
|
// as a libtrust fingerprint. The key itself can be looked up in
|
||||||
|
// the trustedKeys field of the given verify options.
|
||||||
|
//
|
||||||
|
// Each of these methods are tried in that order of preference until the
|
||||||
|
// signing key is found or an error is returned.
|
||||||
|
func (t *Token) VerifySigningKey(verifyOpts VerifyOptions) (signingKey libtrust.PublicKey, err error) {
|
||||||
|
// First attempt to get an x509 certificate chain from the header.
|
||||||
|
var (
|
||||||
|
x5c = t.Header.X5c
|
||||||
|
rawJWK = t.Header.RawJWK
|
||||||
|
keyID = t.Header.KeyID
|
||||||
|
)
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case len(x5c) > 0:
|
||||||
|
signingKey, err = parseAndVerifyCertChain(x5c, verifyOpts.Roots)
|
||||||
|
case rawJWK != nil:
|
||||||
|
signingKey, err = parseAndVerifyRawJWK(rawJWK, verifyOpts)
|
||||||
|
case len(keyID) > 0:
|
||||||
|
signingKey = verifyOpts.TrustedKeys[keyID]
|
||||||
|
if signingKey == nil {
|
||||||
|
err = fmt.Errorf("token signed by untrusted key with ID: %q", keyID)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
err = errors.New("unable to get token signing key")
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseAndVerifyCertChain(x5c []string, roots *x509.CertPool) (leafKey libtrust.PublicKey, err error) {
|
||||||
|
if len(x5c) == 0 {
|
||||||
|
return nil, errors.New("empty x509 certificate chain")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure the first element is encoded correctly.
|
||||||
|
leafCertDer, err := base64.StdEncoding.DecodeString(x5c[0])
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to decode leaf certificate: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// And that it is a valid x509 certificate.
|
||||||
|
leafCert, err := x509.ParseCertificate(leafCertDer)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to parse leaf certificate: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The rest of the certificate chain are intermediate certificates.
|
||||||
|
intermediates := x509.NewCertPool()
|
||||||
|
for i := 1; i < len(x5c); i++ {
|
||||||
|
intermediateCertDer, err := base64.StdEncoding.DecodeString(x5c[i])
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to decode intermediate certificate: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
intermediateCert, err := x509.ParseCertificate(intermediateCertDer)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to parse intermediate certificate: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
intermediates.AddCert(intermediateCert)
|
||||||
|
}
|
||||||
|
|
||||||
|
verifyOpts := x509.VerifyOptions{
|
||||||
|
Intermediates: intermediates,
|
||||||
|
Roots: roots,
|
||||||
|
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageAny},
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: this call returns certificate chains which we ignore for now, but
|
||||||
|
// we should check them for revocations if we have the ability later.
|
||||||
|
if _, err = leafCert.Verify(verifyOpts); err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to verify certificate chain: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the public key from the leaf certificate.
|
||||||
|
leafCryptoKey, ok := leafCert.PublicKey.(crypto.PublicKey)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("unable to get leaf cert public key value")
|
||||||
|
}
|
||||||
|
|
||||||
|
leafKey, err = libtrust.FromCryptoPublicKey(leafCryptoKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to make libtrust public key from leaf certificate: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseAndVerifyRawJWK(rawJWK *json.RawMessage, verifyOpts VerifyOptions) (pubKey libtrust.PublicKey, err error) {
|
||||||
|
pubKey, err = libtrust.UnmarshalPublicKeyJWK([]byte(*rawJWK))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to decode raw JWK value: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check to see if the key includes a certificate chain.
|
||||||
|
x5cVal, ok := pubKey.GetExtendedField("x5c").([]interface{})
|
||||||
|
if !ok {
|
||||||
|
// The JWK should be one of the trusted root keys.
|
||||||
|
if _, trusted := verifyOpts.TrustedKeys[pubKey.KeyID()]; !trusted {
|
||||||
|
return nil, errors.New("untrusted JWK with no certificate chain")
|
||||||
|
}
|
||||||
|
|
||||||
|
// The JWK is one of the trusted keys.
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure each item in the chain is of the correct type.
|
||||||
|
x5c := make([]string, len(x5cVal))
|
||||||
|
for i, val := range x5cVal {
|
||||||
|
certString, ok := val.(string)
|
||||||
|
if !ok || len(certString) == 0 {
|
||||||
|
return nil, errors.New("malformed certificate chain")
|
||||||
|
}
|
||||||
|
x5c[i] = certString
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure that the x509 certificate chain can
|
||||||
|
// be verified up to one of our trusted roots.
|
||||||
|
leafKey, err := parseAndVerifyCertChain(x5c, verifyOpts.Roots)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("could not verify JWK certificate chain: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify that the public key in the leaf cert *is* the signing key.
|
||||||
|
if pubKey.KeyID() != leafKey.KeyID() {
|
||||||
|
return nil, errors.New("leaf certificate public key ID does not match JWK key ID")
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// accessSet returns a set of actions available for the resource
|
||||||
|
// actions listed in the `access` section of this token.
|
||||||
|
func (t *Token) accessSet() accessSet {
|
||||||
|
if t.Claims == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
accessSet := make(accessSet, len(t.Claims.Access))
|
||||||
|
|
||||||
|
for _, resourceActions := range t.Claims.Access {
|
||||||
|
resource := auth.Resource{
|
||||||
|
Type: resourceActions.Type,
|
||||||
|
Name: resourceActions.Name,
|
||||||
|
}
|
||||||
|
|
||||||
|
set, exists := accessSet[resource]
|
||||||
|
if !exists {
|
||||||
|
set = newActionSet()
|
||||||
|
accessSet[resource] = set
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, action := range resourceActions.Actions {
|
||||||
|
set.add(action)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return accessSet
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Token) resources() []auth.Resource {
|
||||||
|
if t.Claims == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
resourceSet := map[auth.Resource]struct{}{}
|
||||||
|
for _, resourceActions := range t.Claims.Access {
|
||||||
|
resource := auth.Resource{
|
||||||
|
Type: resourceActions.Type,
|
||||||
|
Class: resourceActions.Class,
|
||||||
|
Name: resourceActions.Name,
|
||||||
|
}
|
||||||
|
resourceSet[resource] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
resources := make([]auth.Resource, 0, len(resourceSet))
|
||||||
|
for resource := range resourceSet {
|
||||||
|
resources = append(resources, resource)
|
||||||
|
}
|
||||||
|
|
||||||
|
return resources
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Token) compactRaw() string {
|
||||||
|
return fmt.Sprintf("%s.%s", t.Raw, joseBase64UrlEncode(t.Signature))
|
||||||
|
}
|
58
vendor/github.com/distribution/distribution/registry/auth/token/util.go
generated
vendored
Normal file
58
vendor/github.com/distribution/distribution/registry/auth/token/util.go
generated
vendored
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
package token
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// joseBase64UrlEncode encodes the given data using the standard base64 url
|
||||||
|
// encoding format but with all trailing '=' characters omitted in accordance
|
||||||
|
// with the jose specification.
|
||||||
|
// http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-31#section-2
|
||||||
|
func joseBase64UrlEncode(b []byte) string {
|
||||||
|
return strings.TrimRight(base64.URLEncoding.EncodeToString(b), "=")
|
||||||
|
}
|
||||||
|
|
||||||
|
// joseBase64UrlDecode decodes the given string using the standard base64 url
|
||||||
|
// decoder but first adds the appropriate number of trailing '=' characters in
|
||||||
|
// accordance with the jose specification.
|
||||||
|
// http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-31#section-2
|
||||||
|
func joseBase64UrlDecode(s string) ([]byte, error) {
|
||||||
|
switch len(s) % 4 {
|
||||||
|
case 0:
|
||||||
|
case 2:
|
||||||
|
s += "=="
|
||||||
|
case 3:
|
||||||
|
s += "="
|
||||||
|
default:
|
||||||
|
return nil, errors.New("illegal base64url string")
|
||||||
|
}
|
||||||
|
return base64.URLEncoding.DecodeString(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// actionSet is a special type of stringSet.
|
||||||
|
type actionSet struct {
|
||||||
|
stringSet
|
||||||
|
}
|
||||||
|
|
||||||
|
func newActionSet(actions ...string) actionSet {
|
||||||
|
return actionSet{newStringSet(actions...)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Contains calls StringSet.Contains() for
|
||||||
|
// either "*" or the given action string.
|
||||||
|
func (s actionSet) contains(action string) bool {
|
||||||
|
return s.stringSet.contains("*") || s.stringSet.contains(action)
|
||||||
|
}
|
||||||
|
|
||||||
|
// contains returns true if q is found in ss.
|
||||||
|
func contains(ss []string, q string) bool {
|
||||||
|
for _, s := range ss {
|
||||||
|
if s == q {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
202
vendor/github.com/docker/distribution/LICENSE
generated
vendored
Normal file
202
vendor/github.com/docker/distribution/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,202 @@
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
APPENDIX: How to apply the Apache License to your work.
|
||||||
|
|
||||||
|
To apply the Apache License to your work, attach the following
|
||||||
|
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||||
|
replaced with your own identifying information. (Don't include
|
||||||
|
the brackets!) The text should be enclosed in the appropriate
|
||||||
|
comment syntax for the file format. We also recommend that a
|
||||||
|
file or class name and description of purpose be included on the
|
||||||
|
same "printed page" as the copyright notice for easier
|
||||||
|
identification within third-party archives.
|
||||||
|
|
||||||
|
Copyright {yyyy} {name of copyright owner}
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
|
73
vendor/github.com/docker/distribution/context/context.go
generated
vendored
Normal file
73
vendor/github.com/docker/distribution/context/context.go
generated
vendored
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
package context
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/docker/distribution/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// instanceContext is a context that provides only an instance id. It is
|
||||||
|
// provided as the main background context.
|
||||||
|
type instanceContext struct {
|
||||||
|
context.Context
|
||||||
|
id string // id of context, logged as "instance.id"
|
||||||
|
once sync.Once // once protect generation of the id
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ic *instanceContext) Value(key interface{}) interface{} {
|
||||||
|
if key == "instance.id" {
|
||||||
|
ic.once.Do(func() {
|
||||||
|
// We want to lazy initialize the UUID such that we don't
|
||||||
|
// call a random generator from the package initialization
|
||||||
|
// code. For various reasons random could not be available
|
||||||
|
// https://github.com/docker/distribution/issues/782
|
||||||
|
ic.id = uuid.Generate().String()
|
||||||
|
})
|
||||||
|
return ic.id
|
||||||
|
}
|
||||||
|
|
||||||
|
return ic.Context.Value(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
var background = &instanceContext{
|
||||||
|
Context: context.Background(),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Background returns a non-nil, empty Context. The background context
|
||||||
|
// provides a single key, "instance.id" that is globally unique to the
|
||||||
|
// process.
|
||||||
|
func Background() context.Context {
|
||||||
|
return background
|
||||||
|
}
|
||||||
|
|
||||||
|
// stringMapContext is a simple context implementation that checks a map for a
|
||||||
|
// key, falling back to a parent if not present.
|
||||||
|
type stringMapContext struct {
|
||||||
|
context.Context
|
||||||
|
m map[string]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithValues returns a context that proxies lookups through a map. Only
|
||||||
|
// supports string keys.
|
||||||
|
func WithValues(ctx context.Context, m map[string]interface{}) context.Context {
|
||||||
|
mo := make(map[string]interface{}, len(m)) // make our own copy.
|
||||||
|
for k, v := range m {
|
||||||
|
mo[k] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
return stringMapContext{
|
||||||
|
Context: ctx,
|
||||||
|
m: mo,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (smc stringMapContext) Value(key interface{}) interface{} {
|
||||||
|
if ks, ok := key.(string); ok {
|
||||||
|
if v, ok := smc.m[ks]; ok {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return smc.Context.Value(key)
|
||||||
|
}
|
88
vendor/github.com/docker/distribution/context/doc.go
generated
vendored
Normal file
88
vendor/github.com/docker/distribution/context/doc.go
generated
vendored
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
// Package context provides several utilities for working with
|
||||||
|
// Go's context in http requests. Primarily, the focus is on logging relevant
|
||||||
|
// request information but this package is not limited to that purpose.
|
||||||
|
//
|
||||||
|
// The easiest way to get started is to get the background context:
|
||||||
|
//
|
||||||
|
// ctx := context.Background()
|
||||||
|
//
|
||||||
|
// The returned context should be passed around your application and be the
|
||||||
|
// root of all other context instances. If the application has a version, this
|
||||||
|
// line should be called before anything else:
|
||||||
|
//
|
||||||
|
// ctx := context.WithVersion(context.Background(), version)
|
||||||
|
//
|
||||||
|
// The above will store the version in the context and will be available to
|
||||||
|
// the logger.
|
||||||
|
//
|
||||||
|
// # Logging
|
||||||
|
//
|
||||||
|
// The most useful aspect of this package is GetLogger. This function takes
|
||||||
|
// any context.Context interface and returns the current logger from the
|
||||||
|
// context. Canonical usage looks like this:
|
||||||
|
//
|
||||||
|
// GetLogger(ctx).Infof("something interesting happened")
|
||||||
|
//
|
||||||
|
// GetLogger also takes optional key arguments. The keys will be looked up in
|
||||||
|
// the context and reported with the logger. The following example would
|
||||||
|
// return a logger that prints the version with each log message:
|
||||||
|
//
|
||||||
|
// ctx := context.Context(context.Background(), "version", version)
|
||||||
|
// GetLogger(ctx, "version").Infof("this log message has a version field")
|
||||||
|
//
|
||||||
|
// The above would print out a log message like this:
|
||||||
|
//
|
||||||
|
// INFO[0000] this log message has a version field version=v2.0.0-alpha.2.m
|
||||||
|
//
|
||||||
|
// When used with WithLogger, we gain the ability to decorate the context with
|
||||||
|
// loggers that have information from disparate parts of the call stack.
|
||||||
|
// Following from the version example, we can build a new context with the
|
||||||
|
// configured logger such that we always print the version field:
|
||||||
|
//
|
||||||
|
// ctx = WithLogger(ctx, GetLogger(ctx, "version"))
|
||||||
|
//
|
||||||
|
// Since the logger has been pushed to the context, we can now get the version
|
||||||
|
// field for free with our log messages. Future calls to GetLogger on the new
|
||||||
|
// context will have the version field:
|
||||||
|
//
|
||||||
|
// GetLogger(ctx).Infof("this log message has a version field")
|
||||||
|
//
|
||||||
|
// This becomes more powerful when we start stacking loggers. Let's say we
|
||||||
|
// have the version logger from above but also want a request id. Using the
|
||||||
|
// context above, in our request scoped function, we place another logger in
|
||||||
|
// the context:
|
||||||
|
//
|
||||||
|
// ctx = context.WithValue(ctx, "http.request.id", "unique id") // called when building request context
|
||||||
|
// ctx = WithLogger(ctx, GetLogger(ctx, "http.request.id"))
|
||||||
|
//
|
||||||
|
// When GetLogger is called on the new context, "http.request.id" will be
|
||||||
|
// included as a logger field, along with the original "version" field:
|
||||||
|
//
|
||||||
|
// INFO[0000] this log message has a version field http.request.id=unique id version=v2.0.0-alpha.2.m
|
||||||
|
//
|
||||||
|
// Note that this only affects the new context, the previous context, with the
|
||||||
|
// version field, can be used independently. Put another way, the new logger,
|
||||||
|
// added to the request context, is unique to that context and can have
|
||||||
|
// request scoped variables.
|
||||||
|
//
|
||||||
|
// # HTTP Requests
|
||||||
|
//
|
||||||
|
// This package also contains several methods for working with http requests.
|
||||||
|
// The concepts are very similar to those described above. We simply place the
|
||||||
|
// request in the context using WithRequest. This makes the request variables
|
||||||
|
// available. GetRequestLogger can then be called to get request specific
|
||||||
|
// variables in a log line:
|
||||||
|
//
|
||||||
|
// ctx = WithRequest(ctx, req)
|
||||||
|
// GetRequestLogger(ctx).Infof("request variables")
|
||||||
|
//
|
||||||
|
// Like above, if we want to include the request data in all log messages in
|
||||||
|
// the context, we push the logger to a new context and use that one:
|
||||||
|
//
|
||||||
|
// ctx = WithLogger(ctx, GetRequestLogger(ctx))
|
||||||
|
//
|
||||||
|
// The concept is fairly powerful and ensures that calls throughout the stack
|
||||||
|
// can be traced in log messages. Using the fields like "http.request.id", one
|
||||||
|
// can analyze call flow for a particular request with a simple grep of the
|
||||||
|
// logs.
|
||||||
|
package context
|
333
vendor/github.com/docker/distribution/context/http.go
generated
vendored
Normal file
333
vendor/github.com/docker/distribution/context/http.go
generated
vendored
Normal file
|
@ -0,0 +1,333 @@
|
||||||
|
package context
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/docker/distribution/uuid"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Common errors used with this package.
|
||||||
|
var (
|
||||||
|
ErrNoRequestContext = errors.New("no http request in context")
|
||||||
|
ErrNoResponseWriterContext = errors.New("no http response in context")
|
||||||
|
)
|
||||||
|
|
||||||
|
func parseIP(ipStr string) net.IP {
|
||||||
|
ip := net.ParseIP(ipStr)
|
||||||
|
if ip == nil {
|
||||||
|
log.Warnf("invalid remote IP address: %q", ipStr)
|
||||||
|
}
|
||||||
|
return ip
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoteAddr extracts the remote address of the request, taking into
|
||||||
|
// account proxy headers.
|
||||||
|
func RemoteAddr(r *http.Request) string {
|
||||||
|
if prior := r.Header.Get("X-Forwarded-For"); prior != "" {
|
||||||
|
proxies := strings.Split(prior, ",")
|
||||||
|
if len(proxies) > 0 {
|
||||||
|
remoteAddr := strings.Trim(proxies[0], " ")
|
||||||
|
if parseIP(remoteAddr) != nil {
|
||||||
|
return remoteAddr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// X-Real-Ip is less supported, but worth checking in the
|
||||||
|
// absence of X-Forwarded-For
|
||||||
|
if realIP := r.Header.Get("X-Real-Ip"); realIP != "" {
|
||||||
|
if parseIP(realIP) != nil {
|
||||||
|
return realIP
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.RemoteAddr
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoteIP extracts the remote IP of the request, taking into
|
||||||
|
// account proxy headers.
|
||||||
|
func RemoteIP(r *http.Request) string {
|
||||||
|
addr := RemoteAddr(r)
|
||||||
|
|
||||||
|
// Try parsing it as "IP:port"
|
||||||
|
if ip, _, err := net.SplitHostPort(addr); err == nil {
|
||||||
|
return ip
|
||||||
|
}
|
||||||
|
|
||||||
|
return addr
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithRequest places the request on the context. The context of the request
|
||||||
|
// is assigned a unique id, available at "http.request.id". The request itself
|
||||||
|
// is available at "http.request". Other common attributes are available under
|
||||||
|
// the prefix "http.request.". If a request is already present on the context,
|
||||||
|
// this method will panic.
|
||||||
|
func WithRequest(ctx context.Context, r *http.Request) context.Context {
|
||||||
|
if ctx.Value("http.request") != nil {
|
||||||
|
// NOTE(stevvooe): This needs to be considered a programming error. It
|
||||||
|
// is unlikely that we'd want to have more than one request in
|
||||||
|
// context.
|
||||||
|
panic("only one request per context")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &httpRequestContext{
|
||||||
|
Context: ctx,
|
||||||
|
startedAt: time.Now(),
|
||||||
|
id: uuid.Generate().String(),
|
||||||
|
r: r,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRequest returns the http request in the given context. Returns
|
||||||
|
// ErrNoRequestContext if the context does not have an http request associated
|
||||||
|
// with it.
|
||||||
|
func GetRequest(ctx context.Context) (*http.Request, error) {
|
||||||
|
if r, ok := ctx.Value("http.request").(*http.Request); r != nil && ok {
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
return nil, ErrNoRequestContext
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRequestID attempts to resolve the current request id, if possible. An
|
||||||
|
// error is return if it is not available on the context.
|
||||||
|
func GetRequestID(ctx context.Context) string {
|
||||||
|
return GetStringValue(ctx, "http.request.id")
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithResponseWriter returns a new context and response writer that makes
|
||||||
|
// interesting response statistics available within the context.
|
||||||
|
func WithResponseWriter(ctx context.Context, w http.ResponseWriter) (context.Context, http.ResponseWriter) {
|
||||||
|
irw := instrumentedResponseWriter{
|
||||||
|
ResponseWriter: w,
|
||||||
|
Context: ctx,
|
||||||
|
}
|
||||||
|
return &irw, &irw
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetResponseWriter returns the http.ResponseWriter from the provided
|
||||||
|
// context. If not present, ErrNoResponseWriterContext is returned. The
|
||||||
|
// returned instance provides instrumentation in the context.
|
||||||
|
func GetResponseWriter(ctx context.Context) (http.ResponseWriter, error) {
|
||||||
|
v := ctx.Value("http.response")
|
||||||
|
|
||||||
|
rw, ok := v.(http.ResponseWriter)
|
||||||
|
if !ok || rw == nil {
|
||||||
|
return nil, ErrNoResponseWriterContext
|
||||||
|
}
|
||||||
|
|
||||||
|
return rw, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getVarsFromRequest let's us change request vars implementation for testing
|
||||||
|
// and maybe future changes.
|
||||||
|
var getVarsFromRequest = mux.Vars
|
||||||
|
|
||||||
|
// WithVars extracts gorilla/mux vars and makes them available on the returned
|
||||||
|
// context. Variables are available at keys with the prefix "vars.". For
|
||||||
|
// example, if looking for the variable "name", it can be accessed as
|
||||||
|
// "vars.name". Implementations that are accessing values need not know that
|
||||||
|
// the underlying context is implemented with gorilla/mux vars.
|
||||||
|
func WithVars(ctx context.Context, r *http.Request) context.Context {
|
||||||
|
return &muxVarsContext{
|
||||||
|
Context: ctx,
|
||||||
|
vars: getVarsFromRequest(r),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRequestLogger returns a logger that contains fields from the request in
|
||||||
|
// the current context. If the request is not available in the context, no
|
||||||
|
// fields will display. Request loggers can safely be pushed onto the context.
|
||||||
|
func GetRequestLogger(ctx context.Context) Logger {
|
||||||
|
return GetLogger(ctx,
|
||||||
|
"http.request.id",
|
||||||
|
"http.request.method",
|
||||||
|
"http.request.host",
|
||||||
|
"http.request.uri",
|
||||||
|
"http.request.referer",
|
||||||
|
"http.request.useragent",
|
||||||
|
"http.request.remoteaddr",
|
||||||
|
"http.request.contenttype")
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetResponseLogger reads the current response stats and builds a logger.
|
||||||
|
// Because the values are read at call time, pushing a logger returned from
|
||||||
|
// this function on the context will lead to missing or invalid data. Only
|
||||||
|
// call this at the end of a request, after the response has been written.
|
||||||
|
func GetResponseLogger(ctx context.Context) Logger {
|
||||||
|
l := getLogrusLogger(ctx,
|
||||||
|
"http.response.written",
|
||||||
|
"http.response.status",
|
||||||
|
"http.response.contenttype")
|
||||||
|
|
||||||
|
duration := Since(ctx, "http.request.startedat")
|
||||||
|
|
||||||
|
if duration > 0 {
|
||||||
|
l = l.WithField("http.response.duration", duration.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
// httpRequestContext makes information about a request available to context.
|
||||||
|
type httpRequestContext struct {
|
||||||
|
context.Context
|
||||||
|
|
||||||
|
startedAt time.Time
|
||||||
|
id string
|
||||||
|
r *http.Request
|
||||||
|
}
|
||||||
|
|
||||||
|
// Value returns a keyed element of the request for use in the context. To get
|
||||||
|
// the request itself, query "request". For other components, access them as
|
||||||
|
// "request.<component>". For example, r.RequestURI
|
||||||
|
func (ctx *httpRequestContext) Value(key interface{}) interface{} {
|
||||||
|
if keyStr, ok := key.(string); ok {
|
||||||
|
if keyStr == "http.request" {
|
||||||
|
return ctx.r
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.HasPrefix(keyStr, "http.request.") {
|
||||||
|
goto fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := strings.Split(keyStr, ".")
|
||||||
|
|
||||||
|
if len(parts) != 3 {
|
||||||
|
goto fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
switch parts[2] {
|
||||||
|
case "uri":
|
||||||
|
return ctx.r.RequestURI
|
||||||
|
case "remoteaddr":
|
||||||
|
return RemoteAddr(ctx.r)
|
||||||
|
case "method":
|
||||||
|
return ctx.r.Method
|
||||||
|
case "host":
|
||||||
|
return ctx.r.Host
|
||||||
|
case "referer":
|
||||||
|
referer := ctx.r.Referer()
|
||||||
|
if referer != "" {
|
||||||
|
return referer
|
||||||
|
}
|
||||||
|
case "useragent":
|
||||||
|
return ctx.r.UserAgent()
|
||||||
|
case "id":
|
||||||
|
return ctx.id
|
||||||
|
case "startedat":
|
||||||
|
return ctx.startedAt
|
||||||
|
case "contenttype":
|
||||||
|
ct := ctx.r.Header.Get("Content-Type")
|
||||||
|
if ct != "" {
|
||||||
|
return ct
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fallback:
|
||||||
|
return ctx.Context.Value(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
type muxVarsContext struct {
|
||||||
|
context.Context
|
||||||
|
vars map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ctx *muxVarsContext) Value(key interface{}) interface{} {
|
||||||
|
if keyStr, ok := key.(string); ok {
|
||||||
|
if keyStr == "vars" {
|
||||||
|
return ctx.vars
|
||||||
|
}
|
||||||
|
|
||||||
|
if v, ok := ctx.vars[strings.TrimPrefix(keyStr, "vars.")]; ok {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx.Context.Value(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// instrumentedResponseWriter provides response writer information in a
|
||||||
|
// context. This variant is only used in the case where CloseNotifier is not
|
||||||
|
// implemented by the parent ResponseWriter.
|
||||||
|
type instrumentedResponseWriter struct {
|
||||||
|
http.ResponseWriter
|
||||||
|
context.Context
|
||||||
|
|
||||||
|
mu sync.Mutex
|
||||||
|
status int
|
||||||
|
written int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (irw *instrumentedResponseWriter) Write(p []byte) (n int, err error) {
|
||||||
|
n, err = irw.ResponseWriter.Write(p)
|
||||||
|
|
||||||
|
irw.mu.Lock()
|
||||||
|
irw.written += int64(n)
|
||||||
|
|
||||||
|
// Guess the likely status if not set.
|
||||||
|
if irw.status == 0 {
|
||||||
|
irw.status = http.StatusOK
|
||||||
|
}
|
||||||
|
|
||||||
|
irw.mu.Unlock()
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (irw *instrumentedResponseWriter) WriteHeader(status int) {
|
||||||
|
irw.ResponseWriter.WriteHeader(status)
|
||||||
|
|
||||||
|
irw.mu.Lock()
|
||||||
|
irw.status = status
|
||||||
|
irw.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (irw *instrumentedResponseWriter) Flush() {
|
||||||
|
if flusher, ok := irw.ResponseWriter.(http.Flusher); ok {
|
||||||
|
flusher.Flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (irw *instrumentedResponseWriter) Value(key interface{}) interface{} {
|
||||||
|
if keyStr, ok := key.(string); ok {
|
||||||
|
if keyStr == "http.response" {
|
||||||
|
return irw
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.HasPrefix(keyStr, "http.response.") {
|
||||||
|
goto fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := strings.Split(keyStr, ".")
|
||||||
|
|
||||||
|
if len(parts) != 3 {
|
||||||
|
goto fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
irw.mu.Lock()
|
||||||
|
defer irw.mu.Unlock()
|
||||||
|
|
||||||
|
switch parts[2] {
|
||||||
|
case "written":
|
||||||
|
return irw.written
|
||||||
|
case "status":
|
||||||
|
return irw.status
|
||||||
|
case "contenttype":
|
||||||
|
contentType := irw.Header().Get("Content-Type")
|
||||||
|
if contentType != "" {
|
||||||
|
return contentType
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fallback:
|
||||||
|
return irw.Context.Value(key)
|
||||||
|
}
|
121
vendor/github.com/docker/distribution/context/logger.go
generated
vendored
Normal file
121
vendor/github.com/docker/distribution/context/logger.go
generated
vendored
Normal file
|
@ -0,0 +1,121 @@
|
||||||
|
package context
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Logger provides a leveled-logging interface.
|
||||||
|
type Logger interface {
|
||||||
|
// standard logger methods
|
||||||
|
Print(args ...interface{})
|
||||||
|
Printf(format string, args ...interface{})
|
||||||
|
Println(args ...interface{})
|
||||||
|
|
||||||
|
Fatal(args ...interface{})
|
||||||
|
Fatalf(format string, args ...interface{})
|
||||||
|
Fatalln(args ...interface{})
|
||||||
|
|
||||||
|
Panic(args ...interface{})
|
||||||
|
Panicf(format string, args ...interface{})
|
||||||
|
Panicln(args ...interface{})
|
||||||
|
|
||||||
|
// Leveled methods, from logrus
|
||||||
|
Debug(args ...interface{})
|
||||||
|
Debugf(format string, args ...interface{})
|
||||||
|
Debugln(args ...interface{})
|
||||||
|
|
||||||
|
Error(args ...interface{})
|
||||||
|
Errorf(format string, args ...interface{})
|
||||||
|
Errorln(args ...interface{})
|
||||||
|
|
||||||
|
Info(args ...interface{})
|
||||||
|
Infof(format string, args ...interface{})
|
||||||
|
Infoln(args ...interface{})
|
||||||
|
|
||||||
|
Warn(args ...interface{})
|
||||||
|
Warnf(format string, args ...interface{})
|
||||||
|
Warnln(args ...interface{})
|
||||||
|
|
||||||
|
WithError(err error) *logrus.Entry
|
||||||
|
}
|
||||||
|
|
||||||
|
type loggerKey struct{}
|
||||||
|
|
||||||
|
// WithLogger creates a new context with provided logger.
|
||||||
|
func WithLogger(ctx context.Context, logger Logger) context.Context {
|
||||||
|
return context.WithValue(ctx, loggerKey{}, logger)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLoggerWithField returns a logger instance with the specified field key
|
||||||
|
// and value without affecting the context. Extra specified keys will be
|
||||||
|
// resolved from the context.
|
||||||
|
func GetLoggerWithField(ctx context.Context, key, value interface{}, keys ...interface{}) Logger {
|
||||||
|
return getLogrusLogger(ctx, keys...).WithField(fmt.Sprint(key), value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLoggerWithFields returns a logger instance with the specified fields
|
||||||
|
// without affecting the context. Extra specified keys will be resolved from
|
||||||
|
// the context.
|
||||||
|
func GetLoggerWithFields(ctx context.Context, fields map[interface{}]interface{}, keys ...interface{}) Logger {
|
||||||
|
// must convert from interface{} -> interface{} to string -> interface{} for logrus.
|
||||||
|
lfields := make(logrus.Fields, len(fields))
|
||||||
|
for key, value := range fields {
|
||||||
|
lfields[fmt.Sprint(key)] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
return getLogrusLogger(ctx, keys...).WithFields(lfields)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLogger returns the logger from the current context, if present. If one
|
||||||
|
// or more keys are provided, they will be resolved on the context and
|
||||||
|
// included in the logger. While context.Value takes an interface, any key
|
||||||
|
// argument passed to GetLogger will be passed to fmt.Sprint when expanded as
|
||||||
|
// a logging key field. If context keys are integer constants, for example,
|
||||||
|
// its recommended that a String method is implemented.
|
||||||
|
func GetLogger(ctx context.Context, keys ...interface{}) Logger {
|
||||||
|
return getLogrusLogger(ctx, keys...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLogrusLogger returns the logrus logger for the context. If one more keys
|
||||||
|
// are provided, they will be resolved on the context and included in the
|
||||||
|
// logger. Only use this function if specific logrus functionality is
|
||||||
|
// required.
|
||||||
|
func getLogrusLogger(ctx context.Context, keys ...interface{}) *logrus.Entry {
|
||||||
|
var logger *logrus.Entry
|
||||||
|
|
||||||
|
// Get a logger, if it is present.
|
||||||
|
loggerInterface := ctx.Value(loggerKey{})
|
||||||
|
if loggerInterface != nil {
|
||||||
|
if lgr, ok := loggerInterface.(*logrus.Entry); ok {
|
||||||
|
logger = lgr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if logger == nil {
|
||||||
|
fields := logrus.Fields{}
|
||||||
|
|
||||||
|
// Fill in the instance id, if we have it.
|
||||||
|
instanceID := ctx.Value("instance.id")
|
||||||
|
if instanceID != nil {
|
||||||
|
fields["instance.id"] = instanceID
|
||||||
|
}
|
||||||
|
|
||||||
|
fields["go.version"] = runtime.Version()
|
||||||
|
// If no logger is found, just return the standard logger.
|
||||||
|
logger = logrus.StandardLogger().WithFields(fields)
|
||||||
|
}
|
||||||
|
|
||||||
|
fields := logrus.Fields{}
|
||||||
|
for _, key := range keys {
|
||||||
|
v := ctx.Value(key)
|
||||||
|
if v != nil {
|
||||||
|
fields[fmt.Sprint(key)] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return logger.WithFields(fields)
|
||||||
|
}
|
105
vendor/github.com/docker/distribution/context/trace.go
generated
vendored
Normal file
105
vendor/github.com/docker/distribution/context/trace.go
generated
vendored
Normal file
|
@ -0,0 +1,105 @@
|
||||||
|
package context
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"runtime"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/docker/distribution/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// WithTrace allocates a traced timing span in a new context. This allows a
|
||||||
|
// caller to track the time between calling WithTrace and the returned done
|
||||||
|
// function. When the done function is called, a log message is emitted with a
|
||||||
|
// "trace.duration" field, corresponding to the elapsed time and a
|
||||||
|
// "trace.func" field, corresponding to the function that called WithTrace.
|
||||||
|
//
|
||||||
|
// The logging keys "trace.id" and "trace.parent.id" are provided to implement
|
||||||
|
// dapper-like tracing. This function should be complemented with a WithSpan
|
||||||
|
// method that could be used for tracing distributed RPC calls.
|
||||||
|
//
|
||||||
|
// The main benefit of this function is to post-process log messages or
|
||||||
|
// intercept them in a hook to provide timing data. Trace ids and parent ids
|
||||||
|
// can also be linked to provide call tracing, if so required.
|
||||||
|
//
|
||||||
|
// Here is an example of the usage:
|
||||||
|
//
|
||||||
|
// func timedOperation(ctx Context) {
|
||||||
|
// ctx, done := WithTrace(ctx)
|
||||||
|
// defer done("this will be the log message")
|
||||||
|
// // ... function body ...
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// If the function ran for roughly 1s, such a usage would emit a log message
|
||||||
|
// as follows:
|
||||||
|
//
|
||||||
|
// INFO[0001] this will be the log message trace.duration=1.004575763s trace.func=github.com/docker/distribution/context.traceOperation trace.id=<id> ...
|
||||||
|
//
|
||||||
|
// Notice that the function name is automatically resolved, along with the
|
||||||
|
// package and a trace id is emitted that can be linked with parent ids.
|
||||||
|
func WithTrace(ctx context.Context) (context.Context, func(format string, a ...interface{})) {
|
||||||
|
if ctx == nil {
|
||||||
|
ctx = Background()
|
||||||
|
}
|
||||||
|
|
||||||
|
pc, file, line, _ := runtime.Caller(1)
|
||||||
|
f := runtime.FuncForPC(pc)
|
||||||
|
ctx = &traced{
|
||||||
|
Context: ctx,
|
||||||
|
id: uuid.Generate().String(),
|
||||||
|
start: time.Now(),
|
||||||
|
parent: GetStringValue(ctx, "trace.id"),
|
||||||
|
fnname: f.Name(),
|
||||||
|
file: file,
|
||||||
|
line: line,
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx, func(format string, a ...interface{}) {
|
||||||
|
GetLogger(ctx,
|
||||||
|
"trace.duration",
|
||||||
|
"trace.id",
|
||||||
|
"trace.parent.id",
|
||||||
|
"trace.func",
|
||||||
|
"trace.file",
|
||||||
|
"trace.line").
|
||||||
|
Debugf(format, a...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// traced represents a context that is traced for function call timing. It
|
||||||
|
// also provides fast lookup for the various attributes that are available on
|
||||||
|
// the trace.
|
||||||
|
type traced struct {
|
||||||
|
context.Context
|
||||||
|
id string
|
||||||
|
parent string
|
||||||
|
start time.Time
|
||||||
|
fnname string
|
||||||
|
file string
|
||||||
|
line int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ts *traced) Value(key interface{}) interface{} {
|
||||||
|
switch key {
|
||||||
|
case "trace.start":
|
||||||
|
return ts.start
|
||||||
|
case "trace.duration":
|
||||||
|
return time.Since(ts.start)
|
||||||
|
case "trace.id":
|
||||||
|
return ts.id
|
||||||
|
case "trace.parent.id":
|
||||||
|
if ts.parent == "" {
|
||||||
|
return nil // must return nil to signal no parent.
|
||||||
|
}
|
||||||
|
|
||||||
|
return ts.parent
|
||||||
|
case "trace.func":
|
||||||
|
return ts.fnname
|
||||||
|
case "trace.file":
|
||||||
|
return ts.file
|
||||||
|
case "trace.line":
|
||||||
|
return ts.line
|
||||||
|
}
|
||||||
|
|
||||||
|
return ts.Context.Value(key)
|
||||||
|
}
|
25
vendor/github.com/docker/distribution/context/util.go
generated
vendored
Normal file
25
vendor/github.com/docker/distribution/context/util.go
generated
vendored
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
package context
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Since looks up key, which should be a time.Time, and returns the duration
|
||||||
|
// since that time. If the key is not found, the value returned will be zero.
|
||||||
|
// This is helpful when inferring metrics related to context execution times.
|
||||||
|
func Since(ctx context.Context, key interface{}) time.Duration {
|
||||||
|
if startedAt, ok := ctx.Value(key).(time.Time); ok {
|
||||||
|
return time.Since(startedAt)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetStringValue returns a string value from the context. The empty string
|
||||||
|
// will be returned if not found.
|
||||||
|
func GetStringValue(ctx context.Context, key interface{}) (value string) {
|
||||||
|
if valuev, ok := ctx.Value(key).(string); ok {
|
||||||
|
value = valuev
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
22
vendor/github.com/docker/distribution/context/version.go
generated
vendored
Normal file
22
vendor/github.com/docker/distribution/context/version.go
generated
vendored
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
package context
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
|
type versionKey struct{}
|
||||||
|
|
||||||
|
func (versionKey) String() string { return "version" }
|
||||||
|
|
||||||
|
// WithVersion stores the application version in the context. The new context
|
||||||
|
// gets a logger to ensure log messages are marked with the application
|
||||||
|
// version.
|
||||||
|
func WithVersion(ctx context.Context, version string) context.Context {
|
||||||
|
ctx = context.WithValue(ctx, versionKey{}, version)
|
||||||
|
// push a new logger onto the stack
|
||||||
|
return WithLogger(ctx, GetLogger(ctx, versionKey{}))
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetVersion returns the application version from the context. An empty
|
||||||
|
// string may returned if the version was not set on the context.
|
||||||
|
func GetVersion(ctx context.Context) string {
|
||||||
|
return GetStringValue(ctx, versionKey{})
|
||||||
|
}
|
200
vendor/github.com/docker/distribution/registry/auth/auth.go
generated
vendored
Normal file
200
vendor/github.com/docker/distribution/registry/auth/auth.go
generated
vendored
Normal file
|
@ -0,0 +1,200 @@
|
||||||
|
// Package auth defines a standard interface for request access controllers.
|
||||||
|
//
|
||||||
|
// An access controller has a simple interface with a single `Authorized`
|
||||||
|
// method which checks that a given request is authorized to perform one or
|
||||||
|
// more actions on one or more resources. This method should return a non-nil
|
||||||
|
// error if the request is not authorized.
|
||||||
|
//
|
||||||
|
// An implementation registers its access controller by name with a constructor
|
||||||
|
// which accepts an options map for configuring the access controller.
|
||||||
|
//
|
||||||
|
// options := map[string]interface{}{"sillySecret": "whysosilly?"}
|
||||||
|
// accessController, _ := auth.GetAccessController("silly", options)
|
||||||
|
//
|
||||||
|
// This `accessController` can then be used in a request handler like so:
|
||||||
|
//
|
||||||
|
// func updateOrder(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// orderNumber := r.FormValue("orderNumber")
|
||||||
|
// resource := auth.Resource{Type: "customerOrder", Name: orderNumber}
|
||||||
|
// access := auth.Access{Resource: resource, Action: "update"}
|
||||||
|
//
|
||||||
|
// if ctx, err := accessController.Authorized(ctx, access); err != nil {
|
||||||
|
// if challenge, ok := err.(auth.Challenge) {
|
||||||
|
// // Let the challenge write the response.
|
||||||
|
// challenge.SetHeaders(r, w)
|
||||||
|
// w.WriteHeader(http.StatusUnauthorized)
|
||||||
|
// return
|
||||||
|
// } else {
|
||||||
|
// // Some other error.
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// UserKey is used to get the user object from
|
||||||
|
// a user context
|
||||||
|
UserKey = "auth.user"
|
||||||
|
|
||||||
|
// UserNameKey is used to get the user name from
|
||||||
|
// a user context
|
||||||
|
UserNameKey = "auth.user.name"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrInvalidCredential is returned when the auth token does not authenticate correctly.
|
||||||
|
ErrInvalidCredential = errors.New("invalid authorization credential")
|
||||||
|
|
||||||
|
// ErrAuthenticationFailure returned when authentication fails.
|
||||||
|
ErrAuthenticationFailure = errors.New("authentication failure")
|
||||||
|
)
|
||||||
|
|
||||||
|
// UserInfo carries information about
|
||||||
|
// an autenticated/authorized client.
|
||||||
|
type UserInfo struct {
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resource describes a resource by type and name.
|
||||||
|
type Resource struct {
|
||||||
|
Type string
|
||||||
|
Class string
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Access describes a specific action that is
|
||||||
|
// requested or allowed for a given resource.
|
||||||
|
type Access struct {
|
||||||
|
Resource
|
||||||
|
Action string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Challenge is a special error type which is used for HTTP 401 Unauthorized
|
||||||
|
// responses and is able to write the response with WWW-Authenticate challenge
|
||||||
|
// header values based on the error.
|
||||||
|
type Challenge interface {
|
||||||
|
error
|
||||||
|
|
||||||
|
// SetHeaders prepares the request to conduct a challenge response by
|
||||||
|
// adding the an HTTP challenge header on the response message. Callers
|
||||||
|
// are expected to set the appropriate HTTP status code (e.g. 401)
|
||||||
|
// themselves.
|
||||||
|
SetHeaders(r *http.Request, w http.ResponseWriter)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AccessController controls access to registry resources based on a request
|
||||||
|
// and required access levels for a request. Implementations can support both
|
||||||
|
// complete denial and http authorization challenges.
|
||||||
|
type AccessController interface {
|
||||||
|
// Authorized returns a non-nil error if the context is granted access and
|
||||||
|
// returns a new authorized context. If one or more Access structs are
|
||||||
|
// provided, the requested access will be compared with what is available
|
||||||
|
// to the context. The given context will contain a "http.request" key with
|
||||||
|
// a `*http.Request` value. If the error is non-nil, access should always
|
||||||
|
// be denied. The error may be of type Challenge, in which case the caller
|
||||||
|
// may have the Challenge handle the request or choose what action to take
|
||||||
|
// based on the Challenge header or response status. The returned context
|
||||||
|
// object should have a "auth.user" value set to a UserInfo struct.
|
||||||
|
Authorized(ctx context.Context, access ...Access) (context.Context, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CredentialAuthenticator is an object which is able to authenticate credentials
|
||||||
|
type CredentialAuthenticator interface {
|
||||||
|
AuthenticateUser(username, password string) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithUser returns a context with the authorized user info.
|
||||||
|
func WithUser(ctx context.Context, user UserInfo) context.Context {
|
||||||
|
return userInfoContext{
|
||||||
|
Context: ctx,
|
||||||
|
user: user,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type userInfoContext struct {
|
||||||
|
context.Context
|
||||||
|
user UserInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
func (uic userInfoContext) Value(key interface{}) interface{} {
|
||||||
|
switch key {
|
||||||
|
case UserKey:
|
||||||
|
return uic.user
|
||||||
|
case UserNameKey:
|
||||||
|
return uic.user.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
return uic.Context.Value(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithResources returns a context with the authorized resources.
|
||||||
|
func WithResources(ctx context.Context, resources []Resource) context.Context {
|
||||||
|
return resourceContext{
|
||||||
|
Context: ctx,
|
||||||
|
resources: resources,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type resourceContext struct {
|
||||||
|
context.Context
|
||||||
|
resources []Resource
|
||||||
|
}
|
||||||
|
|
||||||
|
type resourceKey struct{}
|
||||||
|
|
||||||
|
func (rc resourceContext) Value(key interface{}) interface{} {
|
||||||
|
if key == (resourceKey{}) {
|
||||||
|
return rc.resources
|
||||||
|
}
|
||||||
|
|
||||||
|
return rc.Context.Value(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AuthorizedResources returns the list of resources which have
|
||||||
|
// been authorized for this request.
|
||||||
|
func AuthorizedResources(ctx context.Context) []Resource {
|
||||||
|
if resources, ok := ctx.Value(resourceKey{}).([]Resource); ok {
|
||||||
|
return resources
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// InitFunc is the type of an AccessController factory function and is used
|
||||||
|
// to register the constructor for different AccesController backends.
|
||||||
|
type InitFunc func(options map[string]interface{}) (AccessController, error)
|
||||||
|
|
||||||
|
var accessControllers map[string]InitFunc
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
accessControllers = make(map[string]InitFunc)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register is used to register an InitFunc for
|
||||||
|
// an AccessController backend with the given name.
|
||||||
|
func Register(name string, initFunc InitFunc) error {
|
||||||
|
if _, exists := accessControllers[name]; exists {
|
||||||
|
return fmt.Errorf("name already registered: %s", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
accessControllers[name] = initFunc
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAccessController constructs an AccessController
|
||||||
|
// with the given options using the named backend.
|
||||||
|
func GetAccessController(name string, options map[string]interface{}) (AccessController, error) {
|
||||||
|
if initFunc, exists := accessControllers[name]; exists {
|
||||||
|
return initFunc(options)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("no access controller registered with name: %s", name)
|
||||||
|
}
|
126
vendor/github.com/docker/distribution/uuid/uuid.go
generated
vendored
Normal file
126
vendor/github.com/docker/distribution/uuid/uuid.go
generated
vendored
Normal file
|
@ -0,0 +1,126 @@
|
||||||
|
// Package uuid provides simple UUID generation. Only version 4 style UUIDs
|
||||||
|
// can be generated.
|
||||||
|
//
|
||||||
|
// Please see http://tools.ietf.org/html/rfc4122 for details on UUIDs.
|
||||||
|
package uuid
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Bits is the number of bits in a UUID
|
||||||
|
Bits = 128
|
||||||
|
|
||||||
|
// Size is the number of bytes in a UUID
|
||||||
|
Size = Bits / 8
|
||||||
|
|
||||||
|
format = "%08x-%04x-%04x-%04x-%012x"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrUUIDInvalid indicates a parsed string is not a valid uuid.
|
||||||
|
ErrUUIDInvalid = fmt.Errorf("invalid uuid")
|
||||||
|
|
||||||
|
// Loggerf can be used to override the default logging destination. Such
|
||||||
|
// log messages in this library should be logged at warning or higher.
|
||||||
|
Loggerf = func(format string, args ...interface{}) {}
|
||||||
|
)
|
||||||
|
|
||||||
|
// UUID represents a UUID value. UUIDs can be compared and set to other values
|
||||||
|
// and accessed by byte.
|
||||||
|
type UUID [Size]byte
|
||||||
|
|
||||||
|
// Generate creates a new, version 4 uuid.
|
||||||
|
func Generate() (u UUID) {
|
||||||
|
const (
|
||||||
|
// ensures we backoff for less than 450ms total. Use the following to
|
||||||
|
// select new value, in units of 10ms:
|
||||||
|
// n*(n+1)/2 = d -> n^2 + n - 2d -> n = (sqrt(8d + 1) - 1)/2
|
||||||
|
maxretries = 9
|
||||||
|
backoff = time.Millisecond * 10
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
totalBackoff time.Duration
|
||||||
|
count int
|
||||||
|
retries int
|
||||||
|
)
|
||||||
|
|
||||||
|
for {
|
||||||
|
// This should never block but the read may fail. Because of this,
|
||||||
|
// we just try to read the random number generator until we get
|
||||||
|
// something. This is a very rare condition but may happen.
|
||||||
|
b := time.Duration(retries) * backoff
|
||||||
|
time.Sleep(b)
|
||||||
|
totalBackoff += b
|
||||||
|
|
||||||
|
n, err := io.ReadFull(rand.Reader, u[count:])
|
||||||
|
if err != nil {
|
||||||
|
if retryOnError(err) && retries < maxretries {
|
||||||
|
count += n
|
||||||
|
retries++
|
||||||
|
Loggerf("error generating version 4 uuid, retrying: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Any other errors represent a system problem. What did someone
|
||||||
|
// do to /dev/urandom?
|
||||||
|
panic(fmt.Errorf("error reading random number generator, retried for %v: %v", totalBackoff.String(), err))
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
u[6] = (u[6] & 0x0f) | 0x40 // set version byte
|
||||||
|
u[8] = (u[8] & 0x3f) | 0x80 // set high order byte 0b10{8,9,a,b}
|
||||||
|
|
||||||
|
return u
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse attempts to extract a uuid from the string or returns an error.
|
||||||
|
func Parse(s string) (u UUID, err error) {
|
||||||
|
if len(s) != 36 {
|
||||||
|
return UUID{}, ErrUUIDInvalid
|
||||||
|
}
|
||||||
|
|
||||||
|
// create stack addresses for each section of the uuid.
|
||||||
|
p := make([][]byte, 5)
|
||||||
|
|
||||||
|
if _, err := fmt.Sscanf(s, format, &p[0], &p[1], &p[2], &p[3], &p[4]); err != nil {
|
||||||
|
return u, err
|
||||||
|
}
|
||||||
|
|
||||||
|
copy(u[0:4], p[0])
|
||||||
|
copy(u[4:6], p[1])
|
||||||
|
copy(u[6:8], p[2])
|
||||||
|
copy(u[8:10], p[3])
|
||||||
|
copy(u[10:16], p[4])
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u UUID) String() string {
|
||||||
|
return fmt.Sprintf(format, u[:4], u[4:6], u[6:8], u[8:10], u[10:])
|
||||||
|
}
|
||||||
|
|
||||||
|
// retryOnError tries to detect whether or not retrying would be fruitful.
|
||||||
|
func retryOnError(err error) bool {
|
||||||
|
switch err := err.(type) {
|
||||||
|
case *os.PathError:
|
||||||
|
return retryOnError(err.Err) // unpack the target error
|
||||||
|
case syscall.Errno:
|
||||||
|
if err == syscall.EPERM {
|
||||||
|
// EPERM represents an entropy pool exhaustion, a condition under
|
||||||
|
// which we backoff and retry.
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
13
vendor/github.com/docker/libtrust/CONTRIBUTING.md
generated
vendored
Normal file
13
vendor/github.com/docker/libtrust/CONTRIBUTING.md
generated
vendored
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
# Contributing to libtrust
|
||||||
|
|
||||||
|
Want to hack on libtrust? Awesome! Here are instructions to get you
|
||||||
|
started.
|
||||||
|
|
||||||
|
libtrust is a part of the [Docker](https://www.docker.com) project, and follows
|
||||||
|
the same rules and principles. If you're already familiar with the way
|
||||||
|
Docker does things, you'll feel right at home.
|
||||||
|
|
||||||
|
Otherwise, go read
|
||||||
|
[Docker's contributions guidelines](https://github.com/docker/docker/blob/master/CONTRIBUTING.md).
|
||||||
|
|
||||||
|
Happy hacking!
|
191
vendor/github.com/docker/libtrust/LICENSE
generated
vendored
Normal file
191
vendor/github.com/docker/libtrust/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,191 @@
|
||||||
|
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
Copyright 2014 Docker, Inc.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
3
vendor/github.com/docker/libtrust/MAINTAINERS
generated
vendored
Normal file
3
vendor/github.com/docker/libtrust/MAINTAINERS
generated
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
Solomon Hykes <solomon@docker.com>
|
||||||
|
Josh Hawn <josh@docker.com> (github: jlhawn)
|
||||||
|
Derek McGowan <derek@docker.com> (github: dmcgowan)
|
22
vendor/github.com/docker/libtrust/README.md
generated
vendored
Normal file
22
vendor/github.com/docker/libtrust/README.md
generated
vendored
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
# libtrust
|
||||||
|
|
||||||
|
> **WARNING** this library is no longer actively developed, and will be integrated
|
||||||
|
> in the [docker/distribution][https://www.github.com/docker/distribution]
|
||||||
|
> repository in future.
|
||||||
|
|
||||||
|
Libtrust is library for managing authentication and authorization using public key cryptography.
|
||||||
|
|
||||||
|
Authentication is handled using the identity attached to the public key.
|
||||||
|
Libtrust provides multiple methods to prove possession of the private key associated with an identity.
|
||||||
|
- TLS x509 certificates
|
||||||
|
- Signature verification
|
||||||
|
- Key Challenge
|
||||||
|
|
||||||
|
Authorization and access control is managed through a distributed trust graph.
|
||||||
|
Trust servers are used as the authorities of the trust graph and allow caching portions of the graph for faster access.
|
||||||
|
|
||||||
|
## Copyright and license
|
||||||
|
|
||||||
|
Code and documentation copyright 2014 Docker, inc. Code released under the Apache 2.0 license.
|
||||||
|
Docs released under Creative commons.
|
||||||
|
|
175
vendor/github.com/docker/libtrust/certificates.go
generated
vendored
Normal file
175
vendor/github.com/docker/libtrust/certificates.go
generated
vendored
Normal file
|
@ -0,0 +1,175 @@
|
||||||
|
package libtrust
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/x509"
|
||||||
|
"crypto/x509/pkix"
|
||||||
|
"encoding/pem"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"math/big"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type certTemplateInfo struct {
|
||||||
|
commonName string
|
||||||
|
domains []string
|
||||||
|
ipAddresses []net.IP
|
||||||
|
isCA bool
|
||||||
|
clientAuth bool
|
||||||
|
serverAuth bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateCertTemplate(info *certTemplateInfo) *x509.Certificate {
|
||||||
|
// Generate a certificate template which is valid from the past week to
|
||||||
|
// 10 years from now. The usage of the certificate depends on the
|
||||||
|
// specified fields in the given certTempInfo object.
|
||||||
|
var (
|
||||||
|
keyUsage x509.KeyUsage
|
||||||
|
extKeyUsage []x509.ExtKeyUsage
|
||||||
|
)
|
||||||
|
|
||||||
|
if info.isCA {
|
||||||
|
keyUsage = x509.KeyUsageCertSign
|
||||||
|
}
|
||||||
|
|
||||||
|
if info.clientAuth {
|
||||||
|
extKeyUsage = append(extKeyUsage, x509.ExtKeyUsageClientAuth)
|
||||||
|
}
|
||||||
|
|
||||||
|
if info.serverAuth {
|
||||||
|
extKeyUsage = append(extKeyUsage, x509.ExtKeyUsageServerAuth)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &x509.Certificate{
|
||||||
|
SerialNumber: big.NewInt(0),
|
||||||
|
Subject: pkix.Name{
|
||||||
|
CommonName: info.commonName,
|
||||||
|
},
|
||||||
|
NotBefore: time.Now().Add(-time.Hour * 24 * 7),
|
||||||
|
NotAfter: time.Now().Add(time.Hour * 24 * 365 * 10),
|
||||||
|
DNSNames: info.domains,
|
||||||
|
IPAddresses: info.ipAddresses,
|
||||||
|
IsCA: info.isCA,
|
||||||
|
KeyUsage: keyUsage,
|
||||||
|
ExtKeyUsage: extKeyUsage,
|
||||||
|
BasicConstraintsValid: info.isCA,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateCert(pub PublicKey, priv PrivateKey, subInfo, issInfo *certTemplateInfo) (cert *x509.Certificate, err error) {
|
||||||
|
pubCertTemplate := generateCertTemplate(subInfo)
|
||||||
|
privCertTemplate := generateCertTemplate(issInfo)
|
||||||
|
|
||||||
|
certDER, err := x509.CreateCertificate(
|
||||||
|
rand.Reader, pubCertTemplate, privCertTemplate,
|
||||||
|
pub.CryptoPublicKey(), priv.CryptoPrivateKey(),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create certificate: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cert, err = x509.ParseCertificate(certDER)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to parse certificate: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenerateSelfSignedServerCert creates a self-signed certificate for the
|
||||||
|
// given key which is to be used for TLS servers with the given domains and
|
||||||
|
// IP addresses.
|
||||||
|
func GenerateSelfSignedServerCert(key PrivateKey, domains []string, ipAddresses []net.IP) (*x509.Certificate, error) {
|
||||||
|
info := &certTemplateInfo{
|
||||||
|
commonName: key.KeyID(),
|
||||||
|
domains: domains,
|
||||||
|
ipAddresses: ipAddresses,
|
||||||
|
serverAuth: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
return generateCert(key.PublicKey(), key, info, info)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenerateSelfSignedClientCert creates a self-signed certificate for the
|
||||||
|
// given key which is to be used for TLS clients.
|
||||||
|
func GenerateSelfSignedClientCert(key PrivateKey) (*x509.Certificate, error) {
|
||||||
|
info := &certTemplateInfo{
|
||||||
|
commonName: key.KeyID(),
|
||||||
|
clientAuth: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
return generateCert(key.PublicKey(), key, info, info)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenerateCACert creates a certificate which can be used as a trusted
|
||||||
|
// certificate authority.
|
||||||
|
func GenerateCACert(signer PrivateKey, trustedKey PublicKey) (*x509.Certificate, error) {
|
||||||
|
subjectInfo := &certTemplateInfo{
|
||||||
|
commonName: trustedKey.KeyID(),
|
||||||
|
isCA: true,
|
||||||
|
}
|
||||||
|
issuerInfo := &certTemplateInfo{
|
||||||
|
commonName: signer.KeyID(),
|
||||||
|
}
|
||||||
|
|
||||||
|
return generateCert(trustedKey, signer, subjectInfo, issuerInfo)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenerateCACertPool creates a certificate authority pool to be used for a
|
||||||
|
// TLS configuration. Any self-signed certificates issued by the specified
|
||||||
|
// trusted keys will be verified during a TLS handshake
|
||||||
|
func GenerateCACertPool(signer PrivateKey, trustedKeys []PublicKey) (*x509.CertPool, error) {
|
||||||
|
certPool := x509.NewCertPool()
|
||||||
|
|
||||||
|
for _, trustedKey := range trustedKeys {
|
||||||
|
cert, err := GenerateCACert(signer, trustedKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to generate CA certificate: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
certPool.AddCert(cert)
|
||||||
|
}
|
||||||
|
|
||||||
|
return certPool, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadCertificateBundle loads certificates from the given file. The file should be pem encoded
|
||||||
|
// containing one or more certificates. The expected pem type is "CERTIFICATE".
|
||||||
|
func LoadCertificateBundle(filename string) ([]*x509.Certificate, error) {
|
||||||
|
b, err := ioutil.ReadFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
certificates := []*x509.Certificate{}
|
||||||
|
var block *pem.Block
|
||||||
|
block, b = pem.Decode(b)
|
||||||
|
for ; block != nil; block, b = pem.Decode(b) {
|
||||||
|
if block.Type == "CERTIFICATE" {
|
||||||
|
cert, err := x509.ParseCertificate(block.Bytes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
certificates = append(certificates, cert)
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("invalid pem block type: %s", block.Type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return certificates, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadCertificatePool loads a CA pool from the given file. The file should be pem encoded
|
||||||
|
// containing one or more certificates. The expected pem type is "CERTIFICATE".
|
||||||
|
func LoadCertificatePool(filename string) (*x509.CertPool, error) {
|
||||||
|
certs, err := LoadCertificateBundle(filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
pool := x509.NewCertPool()
|
||||||
|
for _, cert := range certs {
|
||||||
|
pool.AddCert(cert)
|
||||||
|
}
|
||||||
|
return pool, nil
|
||||||
|
}
|
9
vendor/github.com/docker/libtrust/doc.go
generated
vendored
Normal file
9
vendor/github.com/docker/libtrust/doc.go
generated
vendored
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
/*
|
||||||
|
Package libtrust provides an interface for managing authentication and
|
||||||
|
authorization using public key cryptography. Authentication is handled
|
||||||
|
using the identity attached to the public key and verified through TLS
|
||||||
|
x509 certificates, a key challenge, or signature. Authorization and
|
||||||
|
access control is managed through a trust graph distributed between
|
||||||
|
both remote trust servers and locally cached and managed data.
|
||||||
|
*/
|
||||||
|
package libtrust
|
428
vendor/github.com/docker/libtrust/ec_key.go
generated
vendored
Normal file
428
vendor/github.com/docker/libtrust/ec_key.go
generated
vendored
Normal file
|
@ -0,0 +1,428 @@
|
||||||
|
package libtrust
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto"
|
||||||
|
"crypto/ecdsa"
|
||||||
|
"crypto/elliptic"
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/json"
|
||||||
|
"encoding/pem"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"math/big"
|
||||||
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* EC DSA PUBLIC KEY
|
||||||
|
*/
|
||||||
|
|
||||||
|
// ecPublicKey implements a libtrust.PublicKey using elliptic curve digital
|
||||||
|
// signature algorithms.
|
||||||
|
type ecPublicKey struct {
|
||||||
|
*ecdsa.PublicKey
|
||||||
|
curveName string
|
||||||
|
signatureAlgorithm *signatureAlgorithm
|
||||||
|
extended map[string]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func fromECPublicKey(cryptoPublicKey *ecdsa.PublicKey) (*ecPublicKey, error) {
|
||||||
|
curve := cryptoPublicKey.Curve
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case curve == elliptic.P256():
|
||||||
|
return &ecPublicKey{cryptoPublicKey, "P-256", es256, map[string]interface{}{}}, nil
|
||||||
|
case curve == elliptic.P384():
|
||||||
|
return &ecPublicKey{cryptoPublicKey, "P-384", es384, map[string]interface{}{}}, nil
|
||||||
|
case curve == elliptic.P521():
|
||||||
|
return &ecPublicKey{cryptoPublicKey, "P-521", es512, map[string]interface{}{}}, nil
|
||||||
|
default:
|
||||||
|
return nil, errors.New("unsupported elliptic curve")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// KeyType returns the key type for elliptic curve keys, i.e., "EC".
|
||||||
|
func (k *ecPublicKey) KeyType() string {
|
||||||
|
return "EC"
|
||||||
|
}
|
||||||
|
|
||||||
|
// CurveName returns the elliptic curve identifier.
|
||||||
|
// Possible values are "P-256", "P-384", and "P-521".
|
||||||
|
func (k *ecPublicKey) CurveName() string {
|
||||||
|
return k.curveName
|
||||||
|
}
|
||||||
|
|
||||||
|
// KeyID returns a distinct identifier which is unique to this Public Key.
|
||||||
|
func (k *ecPublicKey) KeyID() string {
|
||||||
|
return keyIDFromCryptoKey(k)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *ecPublicKey) String() string {
|
||||||
|
return fmt.Sprintf("EC Public Key <%s>", k.KeyID())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify verifyies the signature of the data in the io.Reader using this
|
||||||
|
// PublicKey. The alg parameter should identify the digital signature
|
||||||
|
// algorithm which was used to produce the signature and should be supported
|
||||||
|
// by this public key. Returns a nil error if the signature is valid.
|
||||||
|
func (k *ecPublicKey) Verify(data io.Reader, alg string, signature []byte) error {
|
||||||
|
// For EC keys there is only one supported signature algorithm depending
|
||||||
|
// on the curve parameters.
|
||||||
|
if k.signatureAlgorithm.HeaderParam() != alg {
|
||||||
|
return fmt.Errorf("unable to verify signature: EC Public Key with curve %q does not support signature algorithm %q", k.curveName, alg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// signature is the concatenation of (r, s), base64Url encoded.
|
||||||
|
sigLength := len(signature)
|
||||||
|
expectedOctetLength := 2 * ((k.Params().BitSize + 7) >> 3)
|
||||||
|
if sigLength != expectedOctetLength {
|
||||||
|
return fmt.Errorf("signature length is %d octets long, should be %d", sigLength, expectedOctetLength)
|
||||||
|
}
|
||||||
|
|
||||||
|
rBytes, sBytes := signature[:sigLength/2], signature[sigLength/2:]
|
||||||
|
r := new(big.Int).SetBytes(rBytes)
|
||||||
|
s := new(big.Int).SetBytes(sBytes)
|
||||||
|
|
||||||
|
hasher := k.signatureAlgorithm.HashID().New()
|
||||||
|
_, err := io.Copy(hasher, data)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error reading data to sign: %s", err)
|
||||||
|
}
|
||||||
|
hash := hasher.Sum(nil)
|
||||||
|
|
||||||
|
if !ecdsa.Verify(k.PublicKey, hash, r, s) {
|
||||||
|
return errors.New("invalid signature")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CryptoPublicKey returns the internal object which can be used as a
|
||||||
|
// crypto.PublicKey for use with other standard library operations. The type
|
||||||
|
// is either *rsa.PublicKey or *ecdsa.PublicKey
|
||||||
|
func (k *ecPublicKey) CryptoPublicKey() crypto.PublicKey {
|
||||||
|
return k.PublicKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *ecPublicKey) toMap() map[string]interface{} {
|
||||||
|
jwk := make(map[string]interface{})
|
||||||
|
for k, v := range k.extended {
|
||||||
|
jwk[k] = v
|
||||||
|
}
|
||||||
|
jwk["kty"] = k.KeyType()
|
||||||
|
jwk["kid"] = k.KeyID()
|
||||||
|
jwk["crv"] = k.CurveName()
|
||||||
|
|
||||||
|
xBytes := k.X.Bytes()
|
||||||
|
yBytes := k.Y.Bytes()
|
||||||
|
octetLength := (k.Params().BitSize + 7) >> 3
|
||||||
|
// MUST include leading zeros in the output so that x, y are each
|
||||||
|
// *octetLength* bytes long.
|
||||||
|
xBuf := make([]byte, octetLength-len(xBytes), octetLength)
|
||||||
|
yBuf := make([]byte, octetLength-len(yBytes), octetLength)
|
||||||
|
xBuf = append(xBuf, xBytes...)
|
||||||
|
yBuf = append(yBuf, yBytes...)
|
||||||
|
|
||||||
|
jwk["x"] = joseBase64UrlEncode(xBuf)
|
||||||
|
jwk["y"] = joseBase64UrlEncode(yBuf)
|
||||||
|
|
||||||
|
return jwk
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalJSON serializes this Public Key using the JWK JSON serialization format for
|
||||||
|
// elliptic curve keys.
|
||||||
|
func (k *ecPublicKey) MarshalJSON() (data []byte, err error) {
|
||||||
|
return json.Marshal(k.toMap())
|
||||||
|
}
|
||||||
|
|
||||||
|
// PEMBlock serializes this Public Key to DER-encoded PKIX format.
|
||||||
|
func (k *ecPublicKey) PEMBlock() (*pem.Block, error) {
|
||||||
|
derBytes, err := x509.MarshalPKIXPublicKey(k.PublicKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to serialize EC PublicKey to DER-encoded PKIX format: %s", err)
|
||||||
|
}
|
||||||
|
k.extended["kid"] = k.KeyID() // For display purposes.
|
||||||
|
return createPemBlock("PUBLIC KEY", derBytes, k.extended)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *ecPublicKey) AddExtendedField(field string, value interface{}) {
|
||||||
|
k.extended[field] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *ecPublicKey) GetExtendedField(field string) interface{} {
|
||||||
|
v, ok := k.extended[field]
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
func ecPublicKeyFromMap(jwk map[string]interface{}) (*ecPublicKey, error) {
|
||||||
|
// JWK key type (kty) has already been determined to be "EC".
|
||||||
|
// Need to extract 'crv', 'x', 'y', and 'kid' and check for
|
||||||
|
// consistency.
|
||||||
|
|
||||||
|
// Get the curve identifier value.
|
||||||
|
crv, err := stringFromMap(jwk, "crv")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("JWK EC Public Key curve identifier: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
curve elliptic.Curve
|
||||||
|
sigAlg *signatureAlgorithm
|
||||||
|
)
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case crv == "P-256":
|
||||||
|
curve = elliptic.P256()
|
||||||
|
sigAlg = es256
|
||||||
|
case crv == "P-384":
|
||||||
|
curve = elliptic.P384()
|
||||||
|
sigAlg = es384
|
||||||
|
case crv == "P-521":
|
||||||
|
curve = elliptic.P521()
|
||||||
|
sigAlg = es512
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("JWK EC Public Key curve identifier not supported: %q\n", crv)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the X and Y coordinates for the public key point.
|
||||||
|
xB64Url, err := stringFromMap(jwk, "x")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("JWK EC Public Key x-coordinate: %s", err)
|
||||||
|
}
|
||||||
|
x, err := parseECCoordinate(xB64Url, curve)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("JWK EC Public Key x-coordinate: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
yB64Url, err := stringFromMap(jwk, "y")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("JWK EC Public Key y-coordinate: %s", err)
|
||||||
|
}
|
||||||
|
y, err := parseECCoordinate(yB64Url, curve)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("JWK EC Public Key y-coordinate: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
key := &ecPublicKey{
|
||||||
|
PublicKey: &ecdsa.PublicKey{Curve: curve, X: x, Y: y},
|
||||||
|
curveName: crv, signatureAlgorithm: sigAlg,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Key ID is optional too, but if it exists, it should match the key.
|
||||||
|
_, ok := jwk["kid"]
|
||||||
|
if ok {
|
||||||
|
kid, err := stringFromMap(jwk, "kid")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("JWK EC Public Key ID: %s", err)
|
||||||
|
}
|
||||||
|
if kid != key.KeyID() {
|
||||||
|
return nil, fmt.Errorf("JWK EC Public Key ID does not match: %s", kid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
key.extended = jwk
|
||||||
|
|
||||||
|
return key, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* EC DSA PRIVATE KEY
|
||||||
|
*/
|
||||||
|
|
||||||
|
// ecPrivateKey implements a JWK Private Key using elliptic curve digital signature
|
||||||
|
// algorithms.
|
||||||
|
type ecPrivateKey struct {
|
||||||
|
ecPublicKey
|
||||||
|
*ecdsa.PrivateKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func fromECPrivateKey(cryptoPrivateKey *ecdsa.PrivateKey) (*ecPrivateKey, error) {
|
||||||
|
publicKey, err := fromECPublicKey(&cryptoPrivateKey.PublicKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &ecPrivateKey{*publicKey, cryptoPrivateKey}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// PublicKey returns the Public Key data associated with this Private Key.
|
||||||
|
func (k *ecPrivateKey) PublicKey() PublicKey {
|
||||||
|
return &k.ecPublicKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *ecPrivateKey) String() string {
|
||||||
|
return fmt.Sprintf("EC Private Key <%s>", k.KeyID())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sign signs the data read from the io.Reader using a signature algorithm supported
|
||||||
|
// by the elliptic curve private key. If the specified hashing algorithm is
|
||||||
|
// supported by this key, that hash function is used to generate the signature
|
||||||
|
// otherwise the the default hashing algorithm for this key is used. Returns
|
||||||
|
// the signature and the name of the JWK signature algorithm used, e.g.,
|
||||||
|
// "ES256", "ES384", "ES512".
|
||||||
|
func (k *ecPrivateKey) Sign(data io.Reader, hashID crypto.Hash) (signature []byte, alg string, err error) {
|
||||||
|
// Generate a signature of the data using the internal alg.
|
||||||
|
// The given hashId is only a suggestion, and since EC keys only support
|
||||||
|
// on signature/hash algorithm given the curve name, we disregard it for
|
||||||
|
// the elliptic curve JWK signature implementation.
|
||||||
|
hasher := k.signatureAlgorithm.HashID().New()
|
||||||
|
_, err = io.Copy(hasher, data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, "", fmt.Errorf("error reading data to sign: %s", err)
|
||||||
|
}
|
||||||
|
hash := hasher.Sum(nil)
|
||||||
|
|
||||||
|
r, s, err := ecdsa.Sign(rand.Reader, k.PrivateKey, hash)
|
||||||
|
if err != nil {
|
||||||
|
return nil, "", fmt.Errorf("error producing signature: %s", err)
|
||||||
|
}
|
||||||
|
rBytes, sBytes := r.Bytes(), s.Bytes()
|
||||||
|
octetLength := (k.ecPublicKey.Params().BitSize + 7) >> 3
|
||||||
|
// MUST include leading zeros in the output
|
||||||
|
rBuf := make([]byte, octetLength-len(rBytes), octetLength)
|
||||||
|
sBuf := make([]byte, octetLength-len(sBytes), octetLength)
|
||||||
|
|
||||||
|
rBuf = append(rBuf, rBytes...)
|
||||||
|
sBuf = append(sBuf, sBytes...)
|
||||||
|
|
||||||
|
signature = append(rBuf, sBuf...)
|
||||||
|
alg = k.signatureAlgorithm.HeaderParam()
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// CryptoPrivateKey returns the internal object which can be used as a
|
||||||
|
// crypto.PublicKey for use with other standard library operations. The type
|
||||||
|
// is either *rsa.PublicKey or *ecdsa.PublicKey
|
||||||
|
func (k *ecPrivateKey) CryptoPrivateKey() crypto.PrivateKey {
|
||||||
|
return k.PrivateKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *ecPrivateKey) toMap() map[string]interface{} {
|
||||||
|
jwk := k.ecPublicKey.toMap()
|
||||||
|
|
||||||
|
dBytes := k.D.Bytes()
|
||||||
|
// The length of this octet string MUST be ceiling(log-base-2(n)/8)
|
||||||
|
// octets (where n is the order of the curve). This is because the private
|
||||||
|
// key d must be in the interval [1, n-1] so the bitlength of d should be
|
||||||
|
// no larger than the bitlength of n-1. The easiest way to find the octet
|
||||||
|
// length is to take bitlength(n-1), add 7 to force a carry, and shift this
|
||||||
|
// bit sequence right by 3, which is essentially dividing by 8 and adding
|
||||||
|
// 1 if there is any remainder. Thus, the private key value d should be
|
||||||
|
// output to (bitlength(n-1)+7)>>3 octets.
|
||||||
|
n := k.ecPublicKey.Params().N
|
||||||
|
octetLength := (new(big.Int).Sub(n, big.NewInt(1)).BitLen() + 7) >> 3
|
||||||
|
// Create a buffer with the necessary zero-padding.
|
||||||
|
dBuf := make([]byte, octetLength-len(dBytes), octetLength)
|
||||||
|
dBuf = append(dBuf, dBytes...)
|
||||||
|
|
||||||
|
jwk["d"] = joseBase64UrlEncode(dBuf)
|
||||||
|
|
||||||
|
return jwk
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalJSON serializes this Private Key using the JWK JSON serialization format for
|
||||||
|
// elliptic curve keys.
|
||||||
|
func (k *ecPrivateKey) MarshalJSON() (data []byte, err error) {
|
||||||
|
return json.Marshal(k.toMap())
|
||||||
|
}
|
||||||
|
|
||||||
|
// PEMBlock serializes this Private Key to DER-encoded PKIX format.
|
||||||
|
func (k *ecPrivateKey) PEMBlock() (*pem.Block, error) {
|
||||||
|
derBytes, err := x509.MarshalECPrivateKey(k.PrivateKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to serialize EC PrivateKey to DER-encoded PKIX format: %s", err)
|
||||||
|
}
|
||||||
|
k.extended["keyID"] = k.KeyID() // For display purposes.
|
||||||
|
return createPemBlock("EC PRIVATE KEY", derBytes, k.extended)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ecPrivateKeyFromMap(jwk map[string]interface{}) (*ecPrivateKey, error) {
|
||||||
|
dB64Url, err := stringFromMap(jwk, "d")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("JWK EC Private Key: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// JWK key type (kty) has already been determined to be "EC".
|
||||||
|
// Need to extract the public key information, then extract the private
|
||||||
|
// key value 'd'.
|
||||||
|
publicKey, err := ecPublicKeyFromMap(jwk)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
d, err := parseECPrivateParam(dB64Url, publicKey.Curve)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("JWK EC Private Key d-param: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
key := &ecPrivateKey{
|
||||||
|
ecPublicKey: *publicKey,
|
||||||
|
PrivateKey: &ecdsa.PrivateKey{
|
||||||
|
PublicKey: *publicKey.PublicKey,
|
||||||
|
D: d,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return key, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Key Generation Functions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
func generateECPrivateKey(curve elliptic.Curve) (k *ecPrivateKey, err error) {
|
||||||
|
k = new(ecPrivateKey)
|
||||||
|
k.PrivateKey, err = ecdsa.GenerateKey(curve, rand.Reader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
k.ecPublicKey.PublicKey = &k.PrivateKey.PublicKey
|
||||||
|
k.extended = make(map[string]interface{})
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenerateECP256PrivateKey generates a key pair using elliptic curve P-256.
|
||||||
|
func GenerateECP256PrivateKey() (PrivateKey, error) {
|
||||||
|
k, err := generateECPrivateKey(elliptic.P256())
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error generating EC P-256 key: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
k.curveName = "P-256"
|
||||||
|
k.signatureAlgorithm = es256
|
||||||
|
|
||||||
|
return k, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenerateECP384PrivateKey generates a key pair using elliptic curve P-384.
|
||||||
|
func GenerateECP384PrivateKey() (PrivateKey, error) {
|
||||||
|
k, err := generateECPrivateKey(elliptic.P384())
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error generating EC P-384 key: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
k.curveName = "P-384"
|
||||||
|
k.signatureAlgorithm = es384
|
||||||
|
|
||||||
|
return k, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenerateECP521PrivateKey generates aß key pair using elliptic curve P-521.
|
||||||
|
func GenerateECP521PrivateKey() (PrivateKey, error) {
|
||||||
|
k, err := generateECPrivateKey(elliptic.P521())
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error generating EC P-521 key: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
k.curveName = "P-521"
|
||||||
|
k.signatureAlgorithm = es512
|
||||||
|
|
||||||
|
return k, nil
|
||||||
|
}
|
50
vendor/github.com/docker/libtrust/filter.go
generated
vendored
Normal file
50
vendor/github.com/docker/libtrust/filter.go
generated
vendored
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
package libtrust
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
// FilterByHosts filters the list of PublicKeys to only those which contain a
|
||||||
|
// 'hosts' pattern which matches the given host. If *includeEmpty* is true,
|
||||||
|
// then keys which do not specify any hosts are also returned.
|
||||||
|
func FilterByHosts(keys []PublicKey, host string, includeEmpty bool) ([]PublicKey, error) {
|
||||||
|
filtered := make([]PublicKey, 0, len(keys))
|
||||||
|
|
||||||
|
for _, pubKey := range keys {
|
||||||
|
var hosts []string
|
||||||
|
switch v := pubKey.GetExtendedField("hosts").(type) {
|
||||||
|
case []string:
|
||||||
|
hosts = v
|
||||||
|
case []interface{}:
|
||||||
|
for _, value := range v {
|
||||||
|
h, ok := value.(string)
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
hosts = append(hosts, h)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(hosts) == 0 {
|
||||||
|
if includeEmpty {
|
||||||
|
filtered = append(filtered, pubKey)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if any hosts match pattern
|
||||||
|
for _, hostPattern := range hosts {
|
||||||
|
match, err := filepath.Match(hostPattern, host)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if match {
|
||||||
|
filtered = append(filtered, pubKey)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return filtered, nil
|
||||||
|
}
|
56
vendor/github.com/docker/libtrust/hash.go
generated
vendored
Normal file
56
vendor/github.com/docker/libtrust/hash.go
generated
vendored
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
package libtrust
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto"
|
||||||
|
_ "crypto/sha256" // Registrer SHA224 and SHA256
|
||||||
|
_ "crypto/sha512" // Registrer SHA384 and SHA512
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type signatureAlgorithm struct {
|
||||||
|
algHeaderParam string
|
||||||
|
hashID crypto.Hash
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *signatureAlgorithm) HeaderParam() string {
|
||||||
|
return h.algHeaderParam
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *signatureAlgorithm) HashID() crypto.Hash {
|
||||||
|
return h.hashID
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
rs256 = &signatureAlgorithm{"RS256", crypto.SHA256}
|
||||||
|
rs384 = &signatureAlgorithm{"RS384", crypto.SHA384}
|
||||||
|
rs512 = &signatureAlgorithm{"RS512", crypto.SHA512}
|
||||||
|
es256 = &signatureAlgorithm{"ES256", crypto.SHA256}
|
||||||
|
es384 = &signatureAlgorithm{"ES384", crypto.SHA384}
|
||||||
|
es512 = &signatureAlgorithm{"ES512", crypto.SHA512}
|
||||||
|
)
|
||||||
|
|
||||||
|
func rsaSignatureAlgorithmByName(alg string) (*signatureAlgorithm, error) {
|
||||||
|
switch {
|
||||||
|
case alg == "RS256":
|
||||||
|
return rs256, nil
|
||||||
|
case alg == "RS384":
|
||||||
|
return rs384, nil
|
||||||
|
case alg == "RS512":
|
||||||
|
return rs512, nil
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("RSA Digital Signature Algorithm %q not supported", alg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func rsaPKCS1v15SignatureAlgorithmForHashID(hashID crypto.Hash) *signatureAlgorithm {
|
||||||
|
switch {
|
||||||
|
case hashID == crypto.SHA512:
|
||||||
|
return rs512
|
||||||
|
case hashID == crypto.SHA384:
|
||||||
|
return rs384
|
||||||
|
case hashID == crypto.SHA256:
|
||||||
|
fallthrough
|
||||||
|
default:
|
||||||
|
return rs256
|
||||||
|
}
|
||||||
|
}
|
657
vendor/github.com/docker/libtrust/jsonsign.go
generated
vendored
Normal file
657
vendor/github.com/docker/libtrust/jsonsign.go
generated
vendored
Normal file
|
@ -0,0 +1,657 @@
|
||||||
|
package libtrust
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
"time"
|
||||||
|
"unicode"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrInvalidSignContent is used when the content to be signed is invalid.
|
||||||
|
ErrInvalidSignContent = errors.New("invalid sign content")
|
||||||
|
|
||||||
|
// ErrInvalidJSONContent is used when invalid json is encountered.
|
||||||
|
ErrInvalidJSONContent = errors.New("invalid json content")
|
||||||
|
|
||||||
|
// ErrMissingSignatureKey is used when the specified signature key
|
||||||
|
// does not exist in the JSON content.
|
||||||
|
ErrMissingSignatureKey = errors.New("missing signature key")
|
||||||
|
)
|
||||||
|
|
||||||
|
type jsHeader struct {
|
||||||
|
JWK PublicKey `json:"jwk,omitempty"`
|
||||||
|
Algorithm string `json:"alg"`
|
||||||
|
Chain []string `json:"x5c,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type jsSignature struct {
|
||||||
|
Header jsHeader `json:"header"`
|
||||||
|
Signature string `json:"signature"`
|
||||||
|
Protected string `json:"protected,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type jsSignaturesSorted []jsSignature
|
||||||
|
|
||||||
|
func (jsbkid jsSignaturesSorted) Swap(i, j int) { jsbkid[i], jsbkid[j] = jsbkid[j], jsbkid[i] }
|
||||||
|
func (jsbkid jsSignaturesSorted) Len() int { return len(jsbkid) }
|
||||||
|
|
||||||
|
func (jsbkid jsSignaturesSorted) Less(i, j int) bool {
|
||||||
|
ki, kj := jsbkid[i].Header.JWK.KeyID(), jsbkid[j].Header.JWK.KeyID()
|
||||||
|
si, sj := jsbkid[i].Signature, jsbkid[j].Signature
|
||||||
|
|
||||||
|
if ki == kj {
|
||||||
|
return si < sj
|
||||||
|
}
|
||||||
|
|
||||||
|
return ki < kj
|
||||||
|
}
|
||||||
|
|
||||||
|
type signKey struct {
|
||||||
|
PrivateKey
|
||||||
|
Chain []*x509.Certificate
|
||||||
|
}
|
||||||
|
|
||||||
|
// JSONSignature represents a signature of a json object.
|
||||||
|
type JSONSignature struct {
|
||||||
|
payload string
|
||||||
|
signatures []jsSignature
|
||||||
|
indent string
|
||||||
|
formatLength int
|
||||||
|
formatTail []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func newJSONSignature() *JSONSignature {
|
||||||
|
return &JSONSignature{
|
||||||
|
signatures: make([]jsSignature, 0, 1),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Payload returns the encoded payload of the signature. This
|
||||||
|
// payload should not be signed directly
|
||||||
|
func (js *JSONSignature) Payload() ([]byte, error) {
|
||||||
|
return joseBase64UrlDecode(js.payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (js *JSONSignature) protectedHeader() (string, error) {
|
||||||
|
protected := map[string]interface{}{
|
||||||
|
"formatLength": js.formatLength,
|
||||||
|
"formatTail": joseBase64UrlEncode(js.formatTail),
|
||||||
|
"time": time.Now().UTC().Format(time.RFC3339),
|
||||||
|
}
|
||||||
|
protectedBytes, err := json.Marshal(protected)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return joseBase64UrlEncode(protectedBytes), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (js *JSONSignature) signBytes(protectedHeader string) ([]byte, error) {
|
||||||
|
buf := make([]byte, len(js.payload)+len(protectedHeader)+1)
|
||||||
|
copy(buf, protectedHeader)
|
||||||
|
buf[len(protectedHeader)] = '.'
|
||||||
|
copy(buf[len(protectedHeader)+1:], js.payload)
|
||||||
|
return buf, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sign adds a signature using the given private key.
|
||||||
|
func (js *JSONSignature) Sign(key PrivateKey) error {
|
||||||
|
protected, err := js.protectedHeader()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
signBytes, err := js.signBytes(protected)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
sigBytes, algorithm, err := key.Sign(bytes.NewReader(signBytes), crypto.SHA256)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
js.signatures = append(js.signatures, jsSignature{
|
||||||
|
Header: jsHeader{
|
||||||
|
JWK: key.PublicKey(),
|
||||||
|
Algorithm: algorithm,
|
||||||
|
},
|
||||||
|
Signature: joseBase64UrlEncode(sigBytes),
|
||||||
|
Protected: protected,
|
||||||
|
})
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SignWithChain adds a signature using the given private key
|
||||||
|
// and setting the x509 chain. The public key of the first element
|
||||||
|
// in the chain must be the public key corresponding with the sign key.
|
||||||
|
func (js *JSONSignature) SignWithChain(key PrivateKey, chain []*x509.Certificate) error {
|
||||||
|
// Ensure key.Chain[0] is public key for key
|
||||||
|
//key.Chain.PublicKey
|
||||||
|
//key.PublicKey().CryptoPublicKey()
|
||||||
|
|
||||||
|
// Verify chain
|
||||||
|
protected, err := js.protectedHeader()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
signBytes, err := js.signBytes(protected)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
sigBytes, algorithm, err := key.Sign(bytes.NewReader(signBytes), crypto.SHA256)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
header := jsHeader{
|
||||||
|
Chain: make([]string, len(chain)),
|
||||||
|
Algorithm: algorithm,
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, cert := range chain {
|
||||||
|
header.Chain[i] = base64.StdEncoding.EncodeToString(cert.Raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
js.signatures = append(js.signatures, jsSignature{
|
||||||
|
Header: header,
|
||||||
|
Signature: joseBase64UrlEncode(sigBytes),
|
||||||
|
Protected: protected,
|
||||||
|
})
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify verifies all the signatures and returns the list of
|
||||||
|
// public keys used to sign. Any x509 chains are not checked.
|
||||||
|
func (js *JSONSignature) Verify() ([]PublicKey, error) {
|
||||||
|
keys := make([]PublicKey, len(js.signatures))
|
||||||
|
for i, signature := range js.signatures {
|
||||||
|
signBytes, err := js.signBytes(signature.Protected)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var publicKey PublicKey
|
||||||
|
if len(signature.Header.Chain) > 0 {
|
||||||
|
certBytes, err := base64.StdEncoding.DecodeString(signature.Header.Chain[0])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
cert, err := x509.ParseCertificate(certBytes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
publicKey, err = FromCryptoPublicKey(cert.PublicKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
} else if signature.Header.JWK != nil {
|
||||||
|
publicKey = signature.Header.JWK
|
||||||
|
} else {
|
||||||
|
return nil, errors.New("missing public key")
|
||||||
|
}
|
||||||
|
|
||||||
|
sigBytes, err := joseBase64UrlDecode(signature.Signature)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = publicKey.Verify(bytes.NewReader(signBytes), signature.Header.Algorithm, sigBytes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
keys[i] = publicKey
|
||||||
|
}
|
||||||
|
return keys, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// VerifyChains verifies all the signatures and the chains associated
|
||||||
|
// with each signature and returns the list of verified chains.
|
||||||
|
// Signatures without an x509 chain are not checked.
|
||||||
|
func (js *JSONSignature) VerifyChains(ca *x509.CertPool) ([][]*x509.Certificate, error) {
|
||||||
|
chains := make([][]*x509.Certificate, 0, len(js.signatures))
|
||||||
|
for _, signature := range js.signatures {
|
||||||
|
signBytes, err := js.signBytes(signature.Protected)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var publicKey PublicKey
|
||||||
|
if len(signature.Header.Chain) > 0 {
|
||||||
|
certBytes, err := base64.StdEncoding.DecodeString(signature.Header.Chain[0])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
cert, err := x509.ParseCertificate(certBytes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
publicKey, err = FromCryptoPublicKey(cert.PublicKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
intermediates := x509.NewCertPool()
|
||||||
|
if len(signature.Header.Chain) > 1 {
|
||||||
|
intermediateChain := signature.Header.Chain[1:]
|
||||||
|
for i := range intermediateChain {
|
||||||
|
certBytes, err := base64.StdEncoding.DecodeString(intermediateChain[i])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
intermediate, err := x509.ParseCertificate(certBytes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
intermediates.AddCert(intermediate)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
verifyOptions := x509.VerifyOptions{
|
||||||
|
Intermediates: intermediates,
|
||||||
|
Roots: ca,
|
||||||
|
}
|
||||||
|
|
||||||
|
verifiedChains, err := cert.Verify(verifyOptions)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
chains = append(chains, verifiedChains...)
|
||||||
|
|
||||||
|
sigBytes, err := joseBase64UrlDecode(signature.Signature)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = publicKey.Verify(bytes.NewReader(signBytes), signature.Header.Algorithm, sigBytes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return chains, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// JWS returns JSON serialized JWS according to
|
||||||
|
// http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-31#section-7.2
|
||||||
|
func (js *JSONSignature) JWS() ([]byte, error) {
|
||||||
|
if len(js.signatures) == 0 {
|
||||||
|
return nil, errors.New("missing signature")
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Sort(jsSignaturesSorted(js.signatures))
|
||||||
|
|
||||||
|
jsonMap := map[string]interface{}{
|
||||||
|
"payload": js.payload,
|
||||||
|
"signatures": js.signatures,
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.MarshalIndent(jsonMap, "", " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
func notSpace(r rune) bool {
|
||||||
|
return !unicode.IsSpace(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func detectJSONIndent(jsonContent []byte) (indent string) {
|
||||||
|
if len(jsonContent) > 2 && jsonContent[0] == '{' && jsonContent[1] == '\n' {
|
||||||
|
quoteIndex := bytes.IndexRune(jsonContent[1:], '"')
|
||||||
|
if quoteIndex > 0 {
|
||||||
|
indent = string(jsonContent[2 : quoteIndex+1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
type jsParsedHeader struct {
|
||||||
|
JWK json.RawMessage `json:"jwk"`
|
||||||
|
Algorithm string `json:"alg"`
|
||||||
|
Chain []string `json:"x5c"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type jsParsedSignature struct {
|
||||||
|
Header jsParsedHeader `json:"header"`
|
||||||
|
Signature string `json:"signature"`
|
||||||
|
Protected string `json:"protected"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseJWS parses a JWS serialized JSON object into a Json Signature.
|
||||||
|
func ParseJWS(content []byte) (*JSONSignature, error) {
|
||||||
|
type jsParsed struct {
|
||||||
|
Payload string `json:"payload"`
|
||||||
|
Signatures []jsParsedSignature `json:"signatures"`
|
||||||
|
}
|
||||||
|
parsed := &jsParsed{}
|
||||||
|
err := json.Unmarshal(content, parsed)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(parsed.Signatures) == 0 {
|
||||||
|
return nil, errors.New("missing signatures")
|
||||||
|
}
|
||||||
|
payload, err := joseBase64UrlDecode(parsed.Payload)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
js, err := NewJSONSignature(payload)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
js.signatures = make([]jsSignature, len(parsed.Signatures))
|
||||||
|
for i, signature := range parsed.Signatures {
|
||||||
|
header := jsHeader{
|
||||||
|
Algorithm: signature.Header.Algorithm,
|
||||||
|
}
|
||||||
|
if signature.Header.Chain != nil {
|
||||||
|
header.Chain = signature.Header.Chain
|
||||||
|
}
|
||||||
|
if signature.Header.JWK != nil {
|
||||||
|
publicKey, err := UnmarshalPublicKeyJWK([]byte(signature.Header.JWK))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
header.JWK = publicKey
|
||||||
|
}
|
||||||
|
js.signatures[i] = jsSignature{
|
||||||
|
Header: header,
|
||||||
|
Signature: signature.Signature,
|
||||||
|
Protected: signature.Protected,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return js, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewJSONSignature returns a new unsigned JWS from a json byte array.
|
||||||
|
// JSONSignature will need to be signed before serializing or storing.
|
||||||
|
// Optionally, one or more signatures can be provided as byte buffers,
|
||||||
|
// containing serialized JWS signatures, to assemble a fully signed JWS
|
||||||
|
// package. It is the callers responsibility to ensure uniqueness of the
|
||||||
|
// provided signatures.
|
||||||
|
func NewJSONSignature(content []byte, signatures ...[]byte) (*JSONSignature, error) {
|
||||||
|
var dataMap map[string]interface{}
|
||||||
|
err := json.Unmarshal(content, &dataMap)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
js := newJSONSignature()
|
||||||
|
js.indent = detectJSONIndent(content)
|
||||||
|
|
||||||
|
js.payload = joseBase64UrlEncode(content)
|
||||||
|
|
||||||
|
// Find trailing } and whitespace, put in protected header
|
||||||
|
closeIndex := bytes.LastIndexFunc(content, notSpace)
|
||||||
|
if content[closeIndex] != '}' {
|
||||||
|
return nil, ErrInvalidJSONContent
|
||||||
|
}
|
||||||
|
lastRuneIndex := bytes.LastIndexFunc(content[:closeIndex], notSpace)
|
||||||
|
if content[lastRuneIndex] == ',' {
|
||||||
|
return nil, ErrInvalidJSONContent
|
||||||
|
}
|
||||||
|
js.formatLength = lastRuneIndex + 1
|
||||||
|
js.formatTail = content[js.formatLength:]
|
||||||
|
|
||||||
|
if len(signatures) > 0 {
|
||||||
|
for _, signature := range signatures {
|
||||||
|
var parsedJSig jsParsedSignature
|
||||||
|
|
||||||
|
if err := json.Unmarshal(signature, &parsedJSig); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO(stevvooe): A lot of the code below is repeated in
|
||||||
|
// ParseJWS. It will require more refactoring to fix that.
|
||||||
|
jsig := jsSignature{
|
||||||
|
Header: jsHeader{
|
||||||
|
Algorithm: parsedJSig.Header.Algorithm,
|
||||||
|
},
|
||||||
|
Signature: parsedJSig.Signature,
|
||||||
|
Protected: parsedJSig.Protected,
|
||||||
|
}
|
||||||
|
|
||||||
|
if parsedJSig.Header.Chain != nil {
|
||||||
|
jsig.Header.Chain = parsedJSig.Header.Chain
|
||||||
|
}
|
||||||
|
|
||||||
|
if parsedJSig.Header.JWK != nil {
|
||||||
|
publicKey, err := UnmarshalPublicKeyJWK([]byte(parsedJSig.Header.JWK))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
jsig.Header.JWK = publicKey
|
||||||
|
}
|
||||||
|
|
||||||
|
js.signatures = append(js.signatures, jsig)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return js, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewJSONSignatureFromMap returns a new unsigned JSONSignature from a map or
|
||||||
|
// struct. JWS will need to be signed before serializing or storing.
|
||||||
|
func NewJSONSignatureFromMap(content interface{}) (*JSONSignature, error) {
|
||||||
|
switch content.(type) {
|
||||||
|
case map[string]interface{}:
|
||||||
|
case struct{}:
|
||||||
|
default:
|
||||||
|
return nil, errors.New("invalid data type")
|
||||||
|
}
|
||||||
|
|
||||||
|
js := newJSONSignature()
|
||||||
|
js.indent = " "
|
||||||
|
|
||||||
|
payload, err := json.MarshalIndent(content, "", js.indent)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
js.payload = joseBase64UrlEncode(payload)
|
||||||
|
|
||||||
|
// Remove '\n}' from formatted section, put in protected header
|
||||||
|
js.formatLength = len(payload) - 2
|
||||||
|
js.formatTail = payload[js.formatLength:]
|
||||||
|
|
||||||
|
return js, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func readIntFromMap(key string, m map[string]interface{}) (int, bool) {
|
||||||
|
value, ok := m[key]
|
||||||
|
if !ok {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
switch v := value.(type) {
|
||||||
|
case int:
|
||||||
|
return v, true
|
||||||
|
case float64:
|
||||||
|
return int(v), true
|
||||||
|
default:
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func readStringFromMap(key string, m map[string]interface{}) (v string, ok bool) {
|
||||||
|
value, ok := m[key]
|
||||||
|
if !ok {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
v, ok = value.(string)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParsePrettySignature parses a formatted signature into a
|
||||||
|
// JSON signature. If the signatures are missing the format information
|
||||||
|
// an error is thrown. The formatted signature must be created by
|
||||||
|
// the same method as format signature.
|
||||||
|
func ParsePrettySignature(content []byte, signatureKey string) (*JSONSignature, error) {
|
||||||
|
var contentMap map[string]json.RawMessage
|
||||||
|
err := json.Unmarshal(content, &contentMap)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error unmarshalling content: %s", err)
|
||||||
|
}
|
||||||
|
sigMessage, ok := contentMap[signatureKey]
|
||||||
|
if !ok {
|
||||||
|
return nil, ErrMissingSignatureKey
|
||||||
|
}
|
||||||
|
|
||||||
|
var signatureBlocks []jsParsedSignature
|
||||||
|
err = json.Unmarshal([]byte(sigMessage), &signatureBlocks)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error unmarshalling signatures: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
js := newJSONSignature()
|
||||||
|
js.signatures = make([]jsSignature, len(signatureBlocks))
|
||||||
|
|
||||||
|
for i, signatureBlock := range signatureBlocks {
|
||||||
|
protectedBytes, err := joseBase64UrlDecode(signatureBlock.Protected)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("base64 decode error: %s", err)
|
||||||
|
}
|
||||||
|
var protectedHeader map[string]interface{}
|
||||||
|
err = json.Unmarshal(protectedBytes, &protectedHeader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error unmarshalling protected header: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
formatLength, ok := readIntFromMap("formatLength", protectedHeader)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("missing formatted length")
|
||||||
|
}
|
||||||
|
encodedTail, ok := readStringFromMap("formatTail", protectedHeader)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("missing formatted tail")
|
||||||
|
}
|
||||||
|
formatTail, err := joseBase64UrlDecode(encodedTail)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("base64 decode error on tail: %s", err)
|
||||||
|
}
|
||||||
|
if js.formatLength == 0 {
|
||||||
|
js.formatLength = formatLength
|
||||||
|
} else if js.formatLength != formatLength {
|
||||||
|
return nil, errors.New("conflicting format length")
|
||||||
|
}
|
||||||
|
if len(js.formatTail) == 0 {
|
||||||
|
js.formatTail = formatTail
|
||||||
|
} else if bytes.Compare(js.formatTail, formatTail) != 0 {
|
||||||
|
return nil, errors.New("conflicting format tail")
|
||||||
|
}
|
||||||
|
|
||||||
|
header := jsHeader{
|
||||||
|
Algorithm: signatureBlock.Header.Algorithm,
|
||||||
|
Chain: signatureBlock.Header.Chain,
|
||||||
|
}
|
||||||
|
if signatureBlock.Header.JWK != nil {
|
||||||
|
publicKey, err := UnmarshalPublicKeyJWK([]byte(signatureBlock.Header.JWK))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error unmarshalling public key: %s", err)
|
||||||
|
}
|
||||||
|
header.JWK = publicKey
|
||||||
|
}
|
||||||
|
js.signatures[i] = jsSignature{
|
||||||
|
Header: header,
|
||||||
|
Signature: signatureBlock.Signature,
|
||||||
|
Protected: signatureBlock.Protected,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if js.formatLength > len(content) {
|
||||||
|
return nil, errors.New("invalid format length")
|
||||||
|
}
|
||||||
|
formatted := make([]byte, js.formatLength+len(js.formatTail))
|
||||||
|
copy(formatted, content[:js.formatLength])
|
||||||
|
copy(formatted[js.formatLength:], js.formatTail)
|
||||||
|
js.indent = detectJSONIndent(formatted)
|
||||||
|
js.payload = joseBase64UrlEncode(formatted)
|
||||||
|
|
||||||
|
return js, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrettySignature formats a json signature into an easy to read
|
||||||
|
// single json serialized object.
|
||||||
|
func (js *JSONSignature) PrettySignature(signatureKey string) ([]byte, error) {
|
||||||
|
if len(js.signatures) == 0 {
|
||||||
|
return nil, errors.New("no signatures")
|
||||||
|
}
|
||||||
|
payload, err := joseBase64UrlDecode(js.payload)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
payload = payload[:js.formatLength]
|
||||||
|
|
||||||
|
sort.Sort(jsSignaturesSorted(js.signatures))
|
||||||
|
|
||||||
|
var marshalled []byte
|
||||||
|
var marshallErr error
|
||||||
|
if js.indent != "" {
|
||||||
|
marshalled, marshallErr = json.MarshalIndent(js.signatures, js.indent, js.indent)
|
||||||
|
} else {
|
||||||
|
marshalled, marshallErr = json.Marshal(js.signatures)
|
||||||
|
}
|
||||||
|
if marshallErr != nil {
|
||||||
|
return nil, marshallErr
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := bytes.NewBuffer(make([]byte, 0, len(payload)+len(marshalled)+34))
|
||||||
|
buf.Write(payload)
|
||||||
|
buf.WriteByte(',')
|
||||||
|
if js.indent != "" {
|
||||||
|
buf.WriteByte('\n')
|
||||||
|
buf.WriteString(js.indent)
|
||||||
|
buf.WriteByte('"')
|
||||||
|
buf.WriteString(signatureKey)
|
||||||
|
buf.WriteString("\": ")
|
||||||
|
buf.Write(marshalled)
|
||||||
|
buf.WriteByte('\n')
|
||||||
|
} else {
|
||||||
|
buf.WriteByte('"')
|
||||||
|
buf.WriteString(signatureKey)
|
||||||
|
buf.WriteString("\":")
|
||||||
|
buf.Write(marshalled)
|
||||||
|
}
|
||||||
|
buf.WriteByte('}')
|
||||||
|
|
||||||
|
return buf.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Signatures provides the signatures on this JWS as opaque blobs, sorted by
|
||||||
|
// keyID. These blobs can be stored and reassembled with payloads. Internally,
|
||||||
|
// they are simply marshaled json web signatures but implementations should
|
||||||
|
// not rely on this.
|
||||||
|
func (js *JSONSignature) Signatures() ([][]byte, error) {
|
||||||
|
sort.Sort(jsSignaturesSorted(js.signatures))
|
||||||
|
|
||||||
|
var sb [][]byte
|
||||||
|
for _, jsig := range js.signatures {
|
||||||
|
p, err := json.Marshal(jsig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
sb = append(sb, p)
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Merge combines the signatures from one or more other signatures into the
|
||||||
|
// method receiver. If the payloads differ for any argument, an error will be
|
||||||
|
// returned and the receiver will not be modified.
|
||||||
|
func (js *JSONSignature) Merge(others ...*JSONSignature) error {
|
||||||
|
merged := js.signatures
|
||||||
|
for _, other := range others {
|
||||||
|
if js.payload != other.payload {
|
||||||
|
return fmt.Errorf("payloads differ from merge target")
|
||||||
|
}
|
||||||
|
merged = append(merged, other.signatures...)
|
||||||
|
}
|
||||||
|
|
||||||
|
js.signatures = merged
|
||||||
|
return nil
|
||||||
|
}
|
253
vendor/github.com/docker/libtrust/key.go
generated
vendored
Normal file
253
vendor/github.com/docker/libtrust/key.go
generated
vendored
Normal file
|
@ -0,0 +1,253 @@
|
||||||
|
package libtrust
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto"
|
||||||
|
"crypto/ecdsa"
|
||||||
|
"crypto/rsa"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/json"
|
||||||
|
"encoding/pem"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
// PublicKey is a generic interface for a Public Key.
|
||||||
|
type PublicKey interface {
|
||||||
|
// KeyType returns the key type for this key. For elliptic curve keys,
|
||||||
|
// this value should be "EC". For RSA keys, this value should be "RSA".
|
||||||
|
KeyType() string
|
||||||
|
// KeyID returns a distinct identifier which is unique to this Public Key.
|
||||||
|
// The format generated by this library is a base32 encoding of a 240 bit
|
||||||
|
// hash of the public key data divided into 12 groups like so:
|
||||||
|
// ABCD:EFGH:IJKL:MNOP:QRST:UVWX:YZ23:4567:ABCD:EFGH:IJKL:MNOP
|
||||||
|
KeyID() string
|
||||||
|
// Verify verifyies the signature of the data in the io.Reader using this
|
||||||
|
// Public Key. The alg parameter should identify the digital signature
|
||||||
|
// algorithm which was used to produce the signature and should be
|
||||||
|
// supported by this public key. Returns a nil error if the signature
|
||||||
|
// is valid.
|
||||||
|
Verify(data io.Reader, alg string, signature []byte) error
|
||||||
|
// CryptoPublicKey returns the internal object which can be used as a
|
||||||
|
// crypto.PublicKey for use with other standard library operations. The type
|
||||||
|
// is either *rsa.PublicKey or *ecdsa.PublicKey
|
||||||
|
CryptoPublicKey() crypto.PublicKey
|
||||||
|
// These public keys can be serialized to the standard JSON encoding for
|
||||||
|
// JSON Web Keys. See section 6 of the IETF draft RFC for JOSE JSON Web
|
||||||
|
// Algorithms.
|
||||||
|
MarshalJSON() ([]byte, error)
|
||||||
|
// These keys can also be serialized to the standard PEM encoding.
|
||||||
|
PEMBlock() (*pem.Block, error)
|
||||||
|
// The string representation of a key is its key type and ID.
|
||||||
|
String() string
|
||||||
|
AddExtendedField(string, interface{})
|
||||||
|
GetExtendedField(string) interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrivateKey is a generic interface for a Private Key.
|
||||||
|
type PrivateKey interface {
|
||||||
|
// A PrivateKey contains all fields and methods of a PublicKey of the
|
||||||
|
// same type. The MarshalJSON method also outputs the private key as a
|
||||||
|
// JSON Web Key, and the PEMBlock method outputs the private key as a
|
||||||
|
// PEM block.
|
||||||
|
PublicKey
|
||||||
|
// PublicKey returns the PublicKey associated with this PrivateKey.
|
||||||
|
PublicKey() PublicKey
|
||||||
|
// Sign signs the data read from the io.Reader using a signature algorithm
|
||||||
|
// supported by the private key. If the specified hashing algorithm is
|
||||||
|
// supported by this key, that hash function is used to generate the
|
||||||
|
// signature otherwise the the default hashing algorithm for this key is
|
||||||
|
// used. Returns the signature and identifier of the algorithm used.
|
||||||
|
Sign(data io.Reader, hashID crypto.Hash) (signature []byte, alg string, err error)
|
||||||
|
// CryptoPrivateKey returns the internal object which can be used as a
|
||||||
|
// crypto.PublicKey for use with other standard library operations. The
|
||||||
|
// type is either *rsa.PublicKey or *ecdsa.PublicKey
|
||||||
|
CryptoPrivateKey() crypto.PrivateKey
|
||||||
|
}
|
||||||
|
|
||||||
|
// FromCryptoPublicKey returns a libtrust PublicKey representation of the given
|
||||||
|
// *ecdsa.PublicKey or *rsa.PublicKey. Returns a non-nil error when the given
|
||||||
|
// key is of an unsupported type.
|
||||||
|
func FromCryptoPublicKey(cryptoPublicKey crypto.PublicKey) (PublicKey, error) {
|
||||||
|
switch cryptoPublicKey := cryptoPublicKey.(type) {
|
||||||
|
case *ecdsa.PublicKey:
|
||||||
|
return fromECPublicKey(cryptoPublicKey)
|
||||||
|
case *rsa.PublicKey:
|
||||||
|
return fromRSAPublicKey(cryptoPublicKey), nil
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("public key type %T is not supported", cryptoPublicKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// FromCryptoPrivateKey returns a libtrust PrivateKey representation of the given
|
||||||
|
// *ecdsa.PrivateKey or *rsa.PrivateKey. Returns a non-nil error when the given
|
||||||
|
// key is of an unsupported type.
|
||||||
|
func FromCryptoPrivateKey(cryptoPrivateKey crypto.PrivateKey) (PrivateKey, error) {
|
||||||
|
switch cryptoPrivateKey := cryptoPrivateKey.(type) {
|
||||||
|
case *ecdsa.PrivateKey:
|
||||||
|
return fromECPrivateKey(cryptoPrivateKey)
|
||||||
|
case *rsa.PrivateKey:
|
||||||
|
return fromRSAPrivateKey(cryptoPrivateKey), nil
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("private key type %T is not supported", cryptoPrivateKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalPublicKeyPEM parses the PEM encoded data and returns a libtrust
|
||||||
|
// PublicKey or an error if there is a problem with the encoding.
|
||||||
|
func UnmarshalPublicKeyPEM(data []byte) (PublicKey, error) {
|
||||||
|
pemBlock, _ := pem.Decode(data)
|
||||||
|
if pemBlock == nil {
|
||||||
|
return nil, errors.New("unable to find PEM encoded data")
|
||||||
|
} else if pemBlock.Type != "PUBLIC KEY" {
|
||||||
|
return nil, fmt.Errorf("unable to get PublicKey from PEM type: %s", pemBlock.Type)
|
||||||
|
}
|
||||||
|
|
||||||
|
return pubKeyFromPEMBlock(pemBlock)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalPublicKeyPEMBundle parses the PEM encoded data as a bundle of
|
||||||
|
// PEM blocks appended one after the other and returns a slice of PublicKey
|
||||||
|
// objects that it finds.
|
||||||
|
func UnmarshalPublicKeyPEMBundle(data []byte) ([]PublicKey, error) {
|
||||||
|
pubKeys := []PublicKey{}
|
||||||
|
|
||||||
|
for {
|
||||||
|
var pemBlock *pem.Block
|
||||||
|
pemBlock, data = pem.Decode(data)
|
||||||
|
if pemBlock == nil {
|
||||||
|
break
|
||||||
|
} else if pemBlock.Type != "PUBLIC KEY" {
|
||||||
|
return nil, fmt.Errorf("unable to get PublicKey from PEM type: %s", pemBlock.Type)
|
||||||
|
}
|
||||||
|
|
||||||
|
pubKey, err := pubKeyFromPEMBlock(pemBlock)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
pubKeys = append(pubKeys, pubKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
return pubKeys, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalPrivateKeyPEM parses the PEM encoded data and returns a libtrust
|
||||||
|
// PrivateKey or an error if there is a problem with the encoding.
|
||||||
|
func UnmarshalPrivateKeyPEM(data []byte) (PrivateKey, error) {
|
||||||
|
pemBlock, _ := pem.Decode(data)
|
||||||
|
if pemBlock == nil {
|
||||||
|
return nil, errors.New("unable to find PEM encoded data")
|
||||||
|
}
|
||||||
|
|
||||||
|
var key PrivateKey
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case pemBlock.Type == "RSA PRIVATE KEY":
|
||||||
|
rsaPrivateKey, err := x509.ParsePKCS1PrivateKey(pemBlock.Bytes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to decode RSA Private Key PEM data: %s", err)
|
||||||
|
}
|
||||||
|
key = fromRSAPrivateKey(rsaPrivateKey)
|
||||||
|
case pemBlock.Type == "EC PRIVATE KEY":
|
||||||
|
ecPrivateKey, err := x509.ParseECPrivateKey(pemBlock.Bytes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to decode EC Private Key PEM data: %s", err)
|
||||||
|
}
|
||||||
|
key, err = fromECPrivateKey(ecPrivateKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("unable to get PrivateKey from PEM type: %s", pemBlock.Type)
|
||||||
|
}
|
||||||
|
|
||||||
|
addPEMHeadersToKey(pemBlock, key.PublicKey())
|
||||||
|
|
||||||
|
return key, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalPublicKeyJWK unmarshals the given JSON Web Key into a generic
|
||||||
|
// Public Key to be used with libtrust.
|
||||||
|
func UnmarshalPublicKeyJWK(data []byte) (PublicKey, error) {
|
||||||
|
jwk := make(map[string]interface{})
|
||||||
|
|
||||||
|
err := json.Unmarshal(data, &jwk)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"decoding JWK Public Key JSON data: %s\n", err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the Key Type value.
|
||||||
|
kty, err := stringFromMap(jwk, "kty")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("JWK Public Key type: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case kty == "EC":
|
||||||
|
// Call out to unmarshal EC public key.
|
||||||
|
return ecPublicKeyFromMap(jwk)
|
||||||
|
case kty == "RSA":
|
||||||
|
// Call out to unmarshal RSA public key.
|
||||||
|
return rsaPublicKeyFromMap(jwk)
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"JWK Public Key type not supported: %q\n", kty,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalPublicKeyJWKSet parses the JSON encoded data as a JSON Web Key Set
|
||||||
|
// and returns a slice of Public Key objects.
|
||||||
|
func UnmarshalPublicKeyJWKSet(data []byte) ([]PublicKey, error) {
|
||||||
|
rawKeys, err := loadJSONKeySetRaw(data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
pubKeys := make([]PublicKey, 0, len(rawKeys))
|
||||||
|
|
||||||
|
for _, rawKey := range rawKeys {
|
||||||
|
pubKey, err := UnmarshalPublicKeyJWK(rawKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
pubKeys = append(pubKeys, pubKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
return pubKeys, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalPrivateKeyJWK unmarshals the given JSON Web Key into a generic
|
||||||
|
// Private Key to be used with libtrust.
|
||||||
|
func UnmarshalPrivateKeyJWK(data []byte) (PrivateKey, error) {
|
||||||
|
jwk := make(map[string]interface{})
|
||||||
|
|
||||||
|
err := json.Unmarshal(data, &jwk)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"decoding JWK Private Key JSON data: %s\n", err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the Key Type value.
|
||||||
|
kty, err := stringFromMap(jwk, "kty")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("JWK Private Key type: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case kty == "EC":
|
||||||
|
// Call out to unmarshal EC private key.
|
||||||
|
return ecPrivateKeyFromMap(jwk)
|
||||||
|
case kty == "RSA":
|
||||||
|
// Call out to unmarshal RSA private key.
|
||||||
|
return rsaPrivateKeyFromMap(jwk)
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"JWK Private Key type not supported: %q\n", kty,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
255
vendor/github.com/docker/libtrust/key_files.go
generated
vendored
Normal file
255
vendor/github.com/docker/libtrust/key_files.go
generated
vendored
Normal file
|
@ -0,0 +1,255 @@
|
||||||
|
package libtrust
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"encoding/pem"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrKeyFileDoesNotExist indicates that the private key file does not exist.
|
||||||
|
ErrKeyFileDoesNotExist = errors.New("key file does not exist")
|
||||||
|
)
|
||||||
|
|
||||||
|
func readKeyFileBytes(filename string) ([]byte, error) {
|
||||||
|
data, err := ioutil.ReadFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
err = ErrKeyFileDoesNotExist
|
||||||
|
} else {
|
||||||
|
err = fmt.Errorf("unable to read key file %s: %s", filename, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Loading and Saving of Public and Private Keys in either PEM or JWK format.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// LoadKeyFile opens the given filename and attempts to read a Private Key
|
||||||
|
// encoded in either PEM or JWK format (if .json or .jwk file extension).
|
||||||
|
func LoadKeyFile(filename string) (PrivateKey, error) {
|
||||||
|
contents, err := readKeyFileBytes(filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var key PrivateKey
|
||||||
|
|
||||||
|
if strings.HasSuffix(filename, ".json") || strings.HasSuffix(filename, ".jwk") {
|
||||||
|
key, err = UnmarshalPrivateKeyJWK(contents)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to decode private key JWK: %s", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
key, err = UnmarshalPrivateKeyPEM(contents)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to decode private key PEM: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return key, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadPublicKeyFile opens the given filename and attempts to read a Public Key
|
||||||
|
// encoded in either PEM or JWK format (if .json or .jwk file extension).
|
||||||
|
func LoadPublicKeyFile(filename string) (PublicKey, error) {
|
||||||
|
contents, err := readKeyFileBytes(filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var key PublicKey
|
||||||
|
|
||||||
|
if strings.HasSuffix(filename, ".json") || strings.HasSuffix(filename, ".jwk") {
|
||||||
|
key, err = UnmarshalPublicKeyJWK(contents)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to decode public key JWK: %s", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
key, err = UnmarshalPublicKeyPEM(contents)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to decode public key PEM: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return key, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SaveKey saves the given key to a file using the provided filename.
|
||||||
|
// This process will overwrite any existing file at the provided location.
|
||||||
|
func SaveKey(filename string, key PrivateKey) error {
|
||||||
|
var encodedKey []byte
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if strings.HasSuffix(filename, ".json") || strings.HasSuffix(filename, ".jwk") {
|
||||||
|
// Encode in JSON Web Key format.
|
||||||
|
encodedKey, err = json.MarshalIndent(key, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to encode private key JWK: %s", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Encode in PEM format.
|
||||||
|
pemBlock, err := key.PEMBlock()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to encode private key PEM: %s", err)
|
||||||
|
}
|
||||||
|
encodedKey = pem.EncodeToMemory(pemBlock)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ioutil.WriteFile(filename, encodedKey, os.FileMode(0600))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to write private key file %s: %s", filename, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SavePublicKey saves the given public key to the file.
|
||||||
|
func SavePublicKey(filename string, key PublicKey) error {
|
||||||
|
var encodedKey []byte
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if strings.HasSuffix(filename, ".json") || strings.HasSuffix(filename, ".jwk") {
|
||||||
|
// Encode in JSON Web Key format.
|
||||||
|
encodedKey, err = json.MarshalIndent(key, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to encode public key JWK: %s", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Encode in PEM format.
|
||||||
|
pemBlock, err := key.PEMBlock()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to encode public key PEM: %s", err)
|
||||||
|
}
|
||||||
|
encodedKey = pem.EncodeToMemory(pemBlock)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ioutil.WriteFile(filename, encodedKey, os.FileMode(0644))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to write public key file %s: %s", filename, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Public Key Set files
|
||||||
|
|
||||||
|
type jwkSet struct {
|
||||||
|
Keys []json.RawMessage `json:"keys"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadKeySetFile loads a key set
|
||||||
|
func LoadKeySetFile(filename string) ([]PublicKey, error) {
|
||||||
|
if strings.HasSuffix(filename, ".json") || strings.HasSuffix(filename, ".jwk") {
|
||||||
|
return loadJSONKeySetFile(filename)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Must be a PEM format file
|
||||||
|
return loadPEMKeySetFile(filename)
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadJSONKeySetRaw(data []byte) ([]json.RawMessage, error) {
|
||||||
|
if len(data) == 0 {
|
||||||
|
// This is okay, just return an empty slice.
|
||||||
|
return []json.RawMessage{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
keySet := jwkSet{}
|
||||||
|
|
||||||
|
err := json.Unmarshal(data, &keySet)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to decode JSON Web Key Set: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return keySet.Keys, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadJSONKeySetFile(filename string) ([]PublicKey, error) {
|
||||||
|
contents, err := readKeyFileBytes(filename)
|
||||||
|
if err != nil && err != ErrKeyFileDoesNotExist {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return UnmarshalPublicKeyJWKSet(contents)
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadPEMKeySetFile(filename string) ([]PublicKey, error) {
|
||||||
|
data, err := readKeyFileBytes(filename)
|
||||||
|
if err != nil && err != ErrKeyFileDoesNotExist {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return UnmarshalPublicKeyPEMBundle(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddKeySetFile adds a key to a key set
|
||||||
|
func AddKeySetFile(filename string, key PublicKey) error {
|
||||||
|
if strings.HasSuffix(filename, ".json") || strings.HasSuffix(filename, ".jwk") {
|
||||||
|
return addKeySetJSONFile(filename, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Must be a PEM format file
|
||||||
|
return addKeySetPEMFile(filename, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func addKeySetJSONFile(filename string, key PublicKey) error {
|
||||||
|
encodedKey, err := json.Marshal(key)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to encode trusted client key: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
contents, err := readKeyFileBytes(filename)
|
||||||
|
if err != nil && err != ErrKeyFileDoesNotExist {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rawEntries, err := loadJSONKeySetRaw(contents)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rawEntries = append(rawEntries, json.RawMessage(encodedKey))
|
||||||
|
entriesWrapper := jwkSet{Keys: rawEntries}
|
||||||
|
|
||||||
|
encodedEntries, err := json.MarshalIndent(entriesWrapper, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to encode trusted client keys: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ioutil.WriteFile(filename, encodedEntries, os.FileMode(0644))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to write trusted client keys file %s: %s", filename, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func addKeySetPEMFile(filename string, key PublicKey) error {
|
||||||
|
// Encode to PEM, open file for appending, write PEM.
|
||||||
|
file, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_RDWR, os.FileMode(0644))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to open trusted client keys file %s: %s", filename, err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
pemBlock, err := key.PEMBlock()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to encoded trusted key: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = file.Write(pem.EncodeToMemory(pemBlock))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to write trusted keys file: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
175
vendor/github.com/docker/libtrust/key_manager.go
generated
vendored
Normal file
175
vendor/github.com/docker/libtrust/key_manager.go
generated
vendored
Normal file
|
@ -0,0 +1,175 @@
|
||||||
|
package libtrust
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"crypto/x509"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ClientKeyManager manages client keys on the filesystem
|
||||||
|
type ClientKeyManager struct {
|
||||||
|
key PrivateKey
|
||||||
|
clientFile string
|
||||||
|
clientDir string
|
||||||
|
|
||||||
|
clientLock sync.RWMutex
|
||||||
|
clients []PublicKey
|
||||||
|
|
||||||
|
configLock sync.Mutex
|
||||||
|
configs []*tls.Config
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewClientKeyManager loads a new manager from a set of key files
|
||||||
|
// and managed by the given private key.
|
||||||
|
func NewClientKeyManager(trustKey PrivateKey, clientFile, clientDir string) (*ClientKeyManager, error) {
|
||||||
|
m := &ClientKeyManager{
|
||||||
|
key: trustKey,
|
||||||
|
clientFile: clientFile,
|
||||||
|
clientDir: clientDir,
|
||||||
|
}
|
||||||
|
if err := m.loadKeys(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// TODO Start watching file and directory
|
||||||
|
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientKeyManager) loadKeys() (err error) {
|
||||||
|
// Load authorized keys file
|
||||||
|
var clients []PublicKey
|
||||||
|
if c.clientFile != "" {
|
||||||
|
clients, err = LoadKeySetFile(c.clientFile)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to load authorized keys: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add clients from authorized keys directory
|
||||||
|
files, err := ioutil.ReadDir(c.clientDir)
|
||||||
|
if err != nil && !os.IsNotExist(err) {
|
||||||
|
return fmt.Errorf("unable to open authorized keys directory: %s", err)
|
||||||
|
}
|
||||||
|
for _, f := range files {
|
||||||
|
if !f.IsDir() {
|
||||||
|
publicKey, err := LoadPublicKeyFile(path.Join(c.clientDir, f.Name()))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to load authorized key file: %s", err)
|
||||||
|
}
|
||||||
|
clients = append(clients, publicKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
c.clientLock.Lock()
|
||||||
|
c.clients = clients
|
||||||
|
c.clientLock.Unlock()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RegisterTLSConfig registers a tls configuration to manager
|
||||||
|
// such that any changes to the keys may be reflected in
|
||||||
|
// the tls client CA pool
|
||||||
|
func (c *ClientKeyManager) RegisterTLSConfig(tlsConfig *tls.Config) error {
|
||||||
|
c.clientLock.RLock()
|
||||||
|
certPool, err := GenerateCACertPool(c.key, c.clients)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("CA pool generation error: %s", err)
|
||||||
|
}
|
||||||
|
c.clientLock.RUnlock()
|
||||||
|
|
||||||
|
tlsConfig.ClientCAs = certPool
|
||||||
|
|
||||||
|
c.configLock.Lock()
|
||||||
|
c.configs = append(c.configs, tlsConfig)
|
||||||
|
c.configLock.Unlock()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewIdentityAuthTLSConfig creates a tls.Config for the server to use for
|
||||||
|
// libtrust identity authentication for the domain specified
|
||||||
|
func NewIdentityAuthTLSConfig(trustKey PrivateKey, clients *ClientKeyManager, addr string, domain string) (*tls.Config, error) {
|
||||||
|
tlsConfig := newTLSConfig()
|
||||||
|
|
||||||
|
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
|
||||||
|
if err := clients.RegisterTLSConfig(tlsConfig); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate cert
|
||||||
|
ips, domains, err := parseAddr(addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// add domain that it expects clients to use
|
||||||
|
domains = append(domains, domain)
|
||||||
|
x509Cert, err := GenerateSelfSignedServerCert(trustKey, domains, ips)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("certificate generation error: %s", err)
|
||||||
|
}
|
||||||
|
tlsConfig.Certificates = []tls.Certificate{{
|
||||||
|
Certificate: [][]byte{x509Cert.Raw},
|
||||||
|
PrivateKey: trustKey.CryptoPrivateKey(),
|
||||||
|
Leaf: x509Cert,
|
||||||
|
}}
|
||||||
|
|
||||||
|
return tlsConfig, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewCertAuthTLSConfig creates a tls.Config for the server to use for
|
||||||
|
// certificate authentication
|
||||||
|
func NewCertAuthTLSConfig(caPath, certPath, keyPath string) (*tls.Config, error) {
|
||||||
|
tlsConfig := newTLSConfig()
|
||||||
|
|
||||||
|
cert, err := tls.LoadX509KeyPair(certPath, keyPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Couldn't load X509 key pair (%s, %s): %s. Key encrypted?", certPath, keyPath, err)
|
||||||
|
}
|
||||||
|
tlsConfig.Certificates = []tls.Certificate{cert}
|
||||||
|
|
||||||
|
// Verify client certificates against a CA?
|
||||||
|
if caPath != "" {
|
||||||
|
certPool := x509.NewCertPool()
|
||||||
|
file, err := ioutil.ReadFile(caPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Couldn't read CA certificate: %s", err)
|
||||||
|
}
|
||||||
|
certPool.AppendCertsFromPEM(file)
|
||||||
|
|
||||||
|
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
|
||||||
|
tlsConfig.ClientCAs = certPool
|
||||||
|
}
|
||||||
|
|
||||||
|
return tlsConfig, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTLSConfig() *tls.Config {
|
||||||
|
return &tls.Config{
|
||||||
|
NextProtos: []string{"http/1.1"},
|
||||||
|
// Avoid fallback on insecure SSL protocols
|
||||||
|
MinVersion: tls.VersionTLS10,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseAddr parses an address into an array of IPs and domains
|
||||||
|
func parseAddr(addr string) ([]net.IP, []string, error) {
|
||||||
|
host, _, err := net.SplitHostPort(addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
var domains []string
|
||||||
|
var ips []net.IP
|
||||||
|
ip := net.ParseIP(host)
|
||||||
|
if ip != nil {
|
||||||
|
ips = []net.IP{ip}
|
||||||
|
} else {
|
||||||
|
domains = []string{host}
|
||||||
|
}
|
||||||
|
return ips, domains, nil
|
||||||
|
}
|
427
vendor/github.com/docker/libtrust/rsa_key.go
generated
vendored
Normal file
427
vendor/github.com/docker/libtrust/rsa_key.go
generated
vendored
Normal file
|
@ -0,0 +1,427 @@
|
||||||
|
package libtrust
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto"
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/rsa"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/json"
|
||||||
|
"encoding/pem"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"math/big"
|
||||||
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* RSA DSA PUBLIC KEY
|
||||||
|
*/
|
||||||
|
|
||||||
|
// rsaPublicKey implements a JWK Public Key using RSA digital signature algorithms.
|
||||||
|
type rsaPublicKey struct {
|
||||||
|
*rsa.PublicKey
|
||||||
|
extended map[string]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func fromRSAPublicKey(cryptoPublicKey *rsa.PublicKey) *rsaPublicKey {
|
||||||
|
return &rsaPublicKey{cryptoPublicKey, map[string]interface{}{}}
|
||||||
|
}
|
||||||
|
|
||||||
|
// KeyType returns the JWK key type for RSA keys, i.e., "RSA".
|
||||||
|
func (k *rsaPublicKey) KeyType() string {
|
||||||
|
return "RSA"
|
||||||
|
}
|
||||||
|
|
||||||
|
// KeyID returns a distinct identifier which is unique to this Public Key.
|
||||||
|
func (k *rsaPublicKey) KeyID() string {
|
||||||
|
return keyIDFromCryptoKey(k)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *rsaPublicKey) String() string {
|
||||||
|
return fmt.Sprintf("RSA Public Key <%s>", k.KeyID())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify verifyies the signature of the data in the io.Reader using this Public Key.
|
||||||
|
// The alg parameter should be the name of the JWA digital signature algorithm
|
||||||
|
// which was used to produce the signature and should be supported by this
|
||||||
|
// public key. Returns a nil error if the signature is valid.
|
||||||
|
func (k *rsaPublicKey) Verify(data io.Reader, alg string, signature []byte) error {
|
||||||
|
// Verify the signature of the given date, return non-nil error if valid.
|
||||||
|
sigAlg, err := rsaSignatureAlgorithmByName(alg)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to verify Signature: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
hasher := sigAlg.HashID().New()
|
||||||
|
_, err = io.Copy(hasher, data)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error reading data to sign: %s", err)
|
||||||
|
}
|
||||||
|
hash := hasher.Sum(nil)
|
||||||
|
|
||||||
|
err = rsa.VerifyPKCS1v15(k.PublicKey, sigAlg.HashID(), hash, signature)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid %s signature: %s", sigAlg.HeaderParam(), err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CryptoPublicKey returns the internal object which can be used as a
|
||||||
|
// crypto.PublicKey for use with other standard library operations. The type
|
||||||
|
// is either *rsa.PublicKey or *ecdsa.PublicKey
|
||||||
|
func (k *rsaPublicKey) CryptoPublicKey() crypto.PublicKey {
|
||||||
|
return k.PublicKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *rsaPublicKey) toMap() map[string]interface{} {
|
||||||
|
jwk := make(map[string]interface{})
|
||||||
|
for k, v := range k.extended {
|
||||||
|
jwk[k] = v
|
||||||
|
}
|
||||||
|
jwk["kty"] = k.KeyType()
|
||||||
|
jwk["kid"] = k.KeyID()
|
||||||
|
jwk["n"] = joseBase64UrlEncode(k.N.Bytes())
|
||||||
|
jwk["e"] = joseBase64UrlEncode(serializeRSAPublicExponentParam(k.E))
|
||||||
|
|
||||||
|
return jwk
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalJSON serializes this Public Key using the JWK JSON serialization format for
|
||||||
|
// RSA keys.
|
||||||
|
func (k *rsaPublicKey) MarshalJSON() (data []byte, err error) {
|
||||||
|
return json.Marshal(k.toMap())
|
||||||
|
}
|
||||||
|
|
||||||
|
// PEMBlock serializes this Public Key to DER-encoded PKIX format.
|
||||||
|
func (k *rsaPublicKey) PEMBlock() (*pem.Block, error) {
|
||||||
|
derBytes, err := x509.MarshalPKIXPublicKey(k.PublicKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to serialize RSA PublicKey to DER-encoded PKIX format: %s", err)
|
||||||
|
}
|
||||||
|
k.extended["kid"] = k.KeyID() // For display purposes.
|
||||||
|
return createPemBlock("PUBLIC KEY", derBytes, k.extended)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *rsaPublicKey) AddExtendedField(field string, value interface{}) {
|
||||||
|
k.extended[field] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *rsaPublicKey) GetExtendedField(field string) interface{} {
|
||||||
|
v, ok := k.extended[field]
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
func rsaPublicKeyFromMap(jwk map[string]interface{}) (*rsaPublicKey, error) {
|
||||||
|
// JWK key type (kty) has already been determined to be "RSA".
|
||||||
|
// Need to extract 'n', 'e', and 'kid' and check for
|
||||||
|
// consistency.
|
||||||
|
|
||||||
|
// Get the modulus parameter N.
|
||||||
|
nB64Url, err := stringFromMap(jwk, "n")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("JWK RSA Public Key modulus: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
n, err := parseRSAModulusParam(nB64Url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("JWK RSA Public Key modulus: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the public exponent E.
|
||||||
|
eB64Url, err := stringFromMap(jwk, "e")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("JWK RSA Public Key exponent: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
e, err := parseRSAPublicExponentParam(eB64Url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("JWK RSA Public Key exponent: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
key := &rsaPublicKey{
|
||||||
|
PublicKey: &rsa.PublicKey{N: n, E: e},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Key ID is optional, but if it exists, it should match the key.
|
||||||
|
_, ok := jwk["kid"]
|
||||||
|
if ok {
|
||||||
|
kid, err := stringFromMap(jwk, "kid")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("JWK RSA Public Key ID: %s", err)
|
||||||
|
}
|
||||||
|
if kid != key.KeyID() {
|
||||||
|
return nil, fmt.Errorf("JWK RSA Public Key ID does not match: %s", kid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := jwk["d"]; ok {
|
||||||
|
return nil, fmt.Errorf("JWK RSA Public Key cannot contain private exponent")
|
||||||
|
}
|
||||||
|
|
||||||
|
key.extended = jwk
|
||||||
|
|
||||||
|
return key, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* RSA DSA PRIVATE KEY
|
||||||
|
*/
|
||||||
|
|
||||||
|
// rsaPrivateKey implements a JWK Private Key using RSA digital signature algorithms.
|
||||||
|
type rsaPrivateKey struct {
|
||||||
|
rsaPublicKey
|
||||||
|
*rsa.PrivateKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func fromRSAPrivateKey(cryptoPrivateKey *rsa.PrivateKey) *rsaPrivateKey {
|
||||||
|
return &rsaPrivateKey{
|
||||||
|
*fromRSAPublicKey(&cryptoPrivateKey.PublicKey),
|
||||||
|
cryptoPrivateKey,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// PublicKey returns the Public Key data associated with this Private Key.
|
||||||
|
func (k *rsaPrivateKey) PublicKey() PublicKey {
|
||||||
|
return &k.rsaPublicKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *rsaPrivateKey) String() string {
|
||||||
|
return fmt.Sprintf("RSA Private Key <%s>", k.KeyID())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sign signs the data read from the io.Reader using a signature algorithm supported
|
||||||
|
// by the RSA private key. If the specified hashing algorithm is supported by
|
||||||
|
// this key, that hash function is used to generate the signature otherwise the
|
||||||
|
// the default hashing algorithm for this key is used. Returns the signature
|
||||||
|
// and the name of the JWK signature algorithm used, e.g., "RS256", "RS384",
|
||||||
|
// "RS512".
|
||||||
|
func (k *rsaPrivateKey) Sign(data io.Reader, hashID crypto.Hash) (signature []byte, alg string, err error) {
|
||||||
|
// Generate a signature of the data using the internal alg.
|
||||||
|
sigAlg := rsaPKCS1v15SignatureAlgorithmForHashID(hashID)
|
||||||
|
hasher := sigAlg.HashID().New()
|
||||||
|
|
||||||
|
_, err = io.Copy(hasher, data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, "", fmt.Errorf("error reading data to sign: %s", err)
|
||||||
|
}
|
||||||
|
hash := hasher.Sum(nil)
|
||||||
|
|
||||||
|
signature, err = rsa.SignPKCS1v15(rand.Reader, k.PrivateKey, sigAlg.HashID(), hash)
|
||||||
|
if err != nil {
|
||||||
|
return nil, "", fmt.Errorf("error producing signature: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
alg = sigAlg.HeaderParam()
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// CryptoPrivateKey returns the internal object which can be used as a
|
||||||
|
// crypto.PublicKey for use with other standard library operations. The type
|
||||||
|
// is either *rsa.PublicKey or *ecdsa.PublicKey
|
||||||
|
func (k *rsaPrivateKey) CryptoPrivateKey() crypto.PrivateKey {
|
||||||
|
return k.PrivateKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *rsaPrivateKey) toMap() map[string]interface{} {
|
||||||
|
k.Precompute() // Make sure the precomputed values are stored.
|
||||||
|
jwk := k.rsaPublicKey.toMap()
|
||||||
|
|
||||||
|
jwk["d"] = joseBase64UrlEncode(k.D.Bytes())
|
||||||
|
jwk["p"] = joseBase64UrlEncode(k.Primes[0].Bytes())
|
||||||
|
jwk["q"] = joseBase64UrlEncode(k.Primes[1].Bytes())
|
||||||
|
jwk["dp"] = joseBase64UrlEncode(k.Precomputed.Dp.Bytes())
|
||||||
|
jwk["dq"] = joseBase64UrlEncode(k.Precomputed.Dq.Bytes())
|
||||||
|
jwk["qi"] = joseBase64UrlEncode(k.Precomputed.Qinv.Bytes())
|
||||||
|
|
||||||
|
otherPrimes := k.Primes[2:]
|
||||||
|
|
||||||
|
if len(otherPrimes) > 0 {
|
||||||
|
otherPrimesInfo := make([]interface{}, len(otherPrimes))
|
||||||
|
for i, r := range otherPrimes {
|
||||||
|
otherPrimeInfo := make(map[string]string, 3)
|
||||||
|
otherPrimeInfo["r"] = joseBase64UrlEncode(r.Bytes())
|
||||||
|
crtVal := k.Precomputed.CRTValues[i]
|
||||||
|
otherPrimeInfo["d"] = joseBase64UrlEncode(crtVal.Exp.Bytes())
|
||||||
|
otherPrimeInfo["t"] = joseBase64UrlEncode(crtVal.Coeff.Bytes())
|
||||||
|
otherPrimesInfo[i] = otherPrimeInfo
|
||||||
|
}
|
||||||
|
jwk["oth"] = otherPrimesInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
return jwk
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalJSON serializes this Private Key using the JWK JSON serialization format for
|
||||||
|
// RSA keys.
|
||||||
|
func (k *rsaPrivateKey) MarshalJSON() (data []byte, err error) {
|
||||||
|
return json.Marshal(k.toMap())
|
||||||
|
}
|
||||||
|
|
||||||
|
// PEMBlock serializes this Private Key to DER-encoded PKIX format.
|
||||||
|
func (k *rsaPrivateKey) PEMBlock() (*pem.Block, error) {
|
||||||
|
derBytes := x509.MarshalPKCS1PrivateKey(k.PrivateKey)
|
||||||
|
k.extended["keyID"] = k.KeyID() // For display purposes.
|
||||||
|
return createPemBlock("RSA PRIVATE KEY", derBytes, k.extended)
|
||||||
|
}
|
||||||
|
|
||||||
|
func rsaPrivateKeyFromMap(jwk map[string]interface{}) (*rsaPrivateKey, error) {
|
||||||
|
// The JWA spec for RSA Private Keys (draft rfc section 5.3.2) states that
|
||||||
|
// only the private key exponent 'd' is REQUIRED, the others are just for
|
||||||
|
// signature/decryption optimizations and SHOULD be included when the JWK
|
||||||
|
// is produced. We MAY choose to accept a JWK which only includes 'd', but
|
||||||
|
// we're going to go ahead and not choose to accept it without the extra
|
||||||
|
// fields. Only the 'oth' field will be optional (for multi-prime keys).
|
||||||
|
privateExponent, err := parseRSAPrivateKeyParamFromMap(jwk, "d")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("JWK RSA Private Key exponent: %s", err)
|
||||||
|
}
|
||||||
|
firstPrimeFactor, err := parseRSAPrivateKeyParamFromMap(jwk, "p")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("JWK RSA Private Key prime factor: %s", err)
|
||||||
|
}
|
||||||
|
secondPrimeFactor, err := parseRSAPrivateKeyParamFromMap(jwk, "q")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("JWK RSA Private Key prime factor: %s", err)
|
||||||
|
}
|
||||||
|
firstFactorCRT, err := parseRSAPrivateKeyParamFromMap(jwk, "dp")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("JWK RSA Private Key CRT exponent: %s", err)
|
||||||
|
}
|
||||||
|
secondFactorCRT, err := parseRSAPrivateKeyParamFromMap(jwk, "dq")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("JWK RSA Private Key CRT exponent: %s", err)
|
||||||
|
}
|
||||||
|
crtCoeff, err := parseRSAPrivateKeyParamFromMap(jwk, "qi")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("JWK RSA Private Key CRT coefficient: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var oth interface{}
|
||||||
|
if _, ok := jwk["oth"]; ok {
|
||||||
|
oth = jwk["oth"]
|
||||||
|
delete(jwk, "oth")
|
||||||
|
}
|
||||||
|
|
||||||
|
// JWK key type (kty) has already been determined to be "RSA".
|
||||||
|
// Need to extract the public key information, then extract the private
|
||||||
|
// key values.
|
||||||
|
publicKey, err := rsaPublicKeyFromMap(jwk)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
privateKey := &rsa.PrivateKey{
|
||||||
|
PublicKey: *publicKey.PublicKey,
|
||||||
|
D: privateExponent,
|
||||||
|
Primes: []*big.Int{firstPrimeFactor, secondPrimeFactor},
|
||||||
|
Precomputed: rsa.PrecomputedValues{
|
||||||
|
Dp: firstFactorCRT,
|
||||||
|
Dq: secondFactorCRT,
|
||||||
|
Qinv: crtCoeff,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
if oth != nil {
|
||||||
|
// Should be an array of more JSON objects.
|
||||||
|
otherPrimesInfo, ok := oth.([]interface{})
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("JWK RSA Private Key: Invalid other primes info: must be an array")
|
||||||
|
}
|
||||||
|
numOtherPrimeFactors := len(otherPrimesInfo)
|
||||||
|
if numOtherPrimeFactors == 0 {
|
||||||
|
return nil, errors.New("JWK RSA Privake Key: Invalid other primes info: must be absent or non-empty")
|
||||||
|
}
|
||||||
|
otherPrimeFactors := make([]*big.Int, numOtherPrimeFactors)
|
||||||
|
productOfPrimes := new(big.Int).Mul(firstPrimeFactor, secondPrimeFactor)
|
||||||
|
crtValues := make([]rsa.CRTValue, numOtherPrimeFactors)
|
||||||
|
|
||||||
|
for i, val := range otherPrimesInfo {
|
||||||
|
otherPrimeinfo, ok := val.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("JWK RSA Private Key: Invalid other prime info: must be a JSON object")
|
||||||
|
}
|
||||||
|
|
||||||
|
otherPrimeFactor, err := parseRSAPrivateKeyParamFromMap(otherPrimeinfo, "r")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("JWK RSA Private Key prime factor: %s", err)
|
||||||
|
}
|
||||||
|
otherFactorCRT, err := parseRSAPrivateKeyParamFromMap(otherPrimeinfo, "d")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("JWK RSA Private Key CRT exponent: %s", err)
|
||||||
|
}
|
||||||
|
otherCrtCoeff, err := parseRSAPrivateKeyParamFromMap(otherPrimeinfo, "t")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("JWK RSA Private Key CRT coefficient: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
crtValue := crtValues[i]
|
||||||
|
crtValue.Exp = otherFactorCRT
|
||||||
|
crtValue.Coeff = otherCrtCoeff
|
||||||
|
crtValue.R = productOfPrimes
|
||||||
|
otherPrimeFactors[i] = otherPrimeFactor
|
||||||
|
productOfPrimes = new(big.Int).Mul(productOfPrimes, otherPrimeFactor)
|
||||||
|
}
|
||||||
|
|
||||||
|
privateKey.Primes = append(privateKey.Primes, otherPrimeFactors...)
|
||||||
|
privateKey.Precomputed.CRTValues = crtValues
|
||||||
|
}
|
||||||
|
|
||||||
|
key := &rsaPrivateKey{
|
||||||
|
rsaPublicKey: *publicKey,
|
||||||
|
PrivateKey: privateKey,
|
||||||
|
}
|
||||||
|
|
||||||
|
return key, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Key Generation Functions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
func generateRSAPrivateKey(bits int) (k *rsaPrivateKey, err error) {
|
||||||
|
k = new(rsaPrivateKey)
|
||||||
|
k.PrivateKey, err = rsa.GenerateKey(rand.Reader, bits)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
k.rsaPublicKey.PublicKey = &k.PrivateKey.PublicKey
|
||||||
|
k.extended = make(map[string]interface{})
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenerateRSA2048PrivateKey generates a key pair using 2048-bit RSA.
|
||||||
|
func GenerateRSA2048PrivateKey() (PrivateKey, error) {
|
||||||
|
k, err := generateRSAPrivateKey(2048)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error generating RSA 2048-bit key: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return k, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenerateRSA3072PrivateKey generates a key pair using 3072-bit RSA.
|
||||||
|
func GenerateRSA3072PrivateKey() (PrivateKey, error) {
|
||||||
|
k, err := generateRSAPrivateKey(3072)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error generating RSA 3072-bit key: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return k, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenerateRSA4096PrivateKey generates a key pair using 4096-bit RSA.
|
||||||
|
func GenerateRSA4096PrivateKey() (PrivateKey, error) {
|
||||||
|
k, err := generateRSAPrivateKey(4096)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error generating RSA 4096-bit key: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return k, nil
|
||||||
|
}
|
363
vendor/github.com/docker/libtrust/util.go
generated
vendored
Normal file
363
vendor/github.com/docker/libtrust/util.go
generated
vendored
Normal file
|
@ -0,0 +1,363 @@
|
||||||
|
package libtrust
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto"
|
||||||
|
"crypto/elliptic"
|
||||||
|
"crypto/tls"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/base32"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/binary"
|
||||||
|
"encoding/pem"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"math/big"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// LoadOrCreateTrustKey will load a PrivateKey from the specified path
|
||||||
|
func LoadOrCreateTrustKey(trustKeyPath string) (PrivateKey, error) {
|
||||||
|
if err := os.MkdirAll(filepath.Dir(trustKeyPath), 0700); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
trustKey, err := LoadKeyFile(trustKeyPath)
|
||||||
|
if err == ErrKeyFileDoesNotExist {
|
||||||
|
trustKey, err = GenerateECP256PrivateKey()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error generating key: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := SaveKey(trustKeyPath, trustKey); err != nil {
|
||||||
|
return nil, fmt.Errorf("error saving key file: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
dir, file := filepath.Split(trustKeyPath)
|
||||||
|
if err := SavePublicKey(filepath.Join(dir, "public-"+file), trustKey.PublicKey()); err != nil {
|
||||||
|
return nil, fmt.Errorf("error saving public key file: %s", err)
|
||||||
|
}
|
||||||
|
} else if err != nil {
|
||||||
|
return nil, fmt.Errorf("error loading key file: %s", err)
|
||||||
|
}
|
||||||
|
return trustKey, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewIdentityAuthTLSClientConfig returns a tls.Config configured to use identity
|
||||||
|
// based authentication from the specified dockerUrl, the rootConfigPath and
|
||||||
|
// the server name to which it is connecting.
|
||||||
|
// If trustUnknownHosts is true it will automatically add the host to the
|
||||||
|
// known-hosts.json in rootConfigPath.
|
||||||
|
func NewIdentityAuthTLSClientConfig(dockerUrl string, trustUnknownHosts bool, rootConfigPath string, serverName string) (*tls.Config, error) {
|
||||||
|
tlsConfig := newTLSConfig()
|
||||||
|
|
||||||
|
trustKeyPath := filepath.Join(rootConfigPath, "key.json")
|
||||||
|
knownHostsPath := filepath.Join(rootConfigPath, "known-hosts.json")
|
||||||
|
|
||||||
|
u, err := url.Parse(dockerUrl)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to parse machine url")
|
||||||
|
}
|
||||||
|
|
||||||
|
if u.Scheme == "unix" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
addr := u.Host
|
||||||
|
proto := "tcp"
|
||||||
|
|
||||||
|
trustKey, err := LoadOrCreateTrustKey(trustKeyPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to load trust key: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
knownHosts, err := LoadKeySetFile(knownHostsPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("could not load trusted hosts file: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
allowedHosts, err := FilterByHosts(knownHosts, addr, false)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error filtering hosts: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
certPool, err := GenerateCACertPool(trustKey, allowedHosts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Could not create CA pool: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tlsConfig.ServerName = serverName
|
||||||
|
tlsConfig.RootCAs = certPool
|
||||||
|
|
||||||
|
x509Cert, err := GenerateSelfSignedClientCert(trustKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("certificate generation error: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tlsConfig.Certificates = []tls.Certificate{{
|
||||||
|
Certificate: [][]byte{x509Cert.Raw},
|
||||||
|
PrivateKey: trustKey.CryptoPrivateKey(),
|
||||||
|
Leaf: x509Cert,
|
||||||
|
}}
|
||||||
|
|
||||||
|
tlsConfig.InsecureSkipVerify = true
|
||||||
|
|
||||||
|
testConn, err := tls.Dial(proto, addr, tlsConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("tls Handshake error: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
opts := x509.VerifyOptions{
|
||||||
|
Roots: tlsConfig.RootCAs,
|
||||||
|
CurrentTime: time.Now(),
|
||||||
|
DNSName: tlsConfig.ServerName,
|
||||||
|
Intermediates: x509.NewCertPool(),
|
||||||
|
}
|
||||||
|
|
||||||
|
certs := testConn.ConnectionState().PeerCertificates
|
||||||
|
for i, cert := range certs {
|
||||||
|
if i == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
opts.Intermediates.AddCert(cert)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := certs[0].Verify(opts); err != nil {
|
||||||
|
if _, ok := err.(x509.UnknownAuthorityError); ok {
|
||||||
|
if trustUnknownHosts {
|
||||||
|
pubKey, err := FromCryptoPublicKey(certs[0].PublicKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error extracting public key from cert: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
pubKey.AddExtendedField("hosts", []string{addr})
|
||||||
|
|
||||||
|
if err := AddKeySetFile(knownHostsPath, pubKey); err != nil {
|
||||||
|
return nil, fmt.Errorf("error adding machine to known hosts: %s", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("unable to connect. unknown host: %s", addr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
testConn.Close()
|
||||||
|
tlsConfig.InsecureSkipVerify = false
|
||||||
|
|
||||||
|
return tlsConfig, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// joseBase64UrlEncode encodes the given data using the standard base64 url
|
||||||
|
// encoding format but with all trailing '=' characters omitted in accordance
|
||||||
|
// with the jose specification.
|
||||||
|
// http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-31#section-2
|
||||||
|
func joseBase64UrlEncode(b []byte) string {
|
||||||
|
return strings.TrimRight(base64.URLEncoding.EncodeToString(b), "=")
|
||||||
|
}
|
||||||
|
|
||||||
|
// joseBase64UrlDecode decodes the given string using the standard base64 url
|
||||||
|
// decoder but first adds the appropriate number of trailing '=' characters in
|
||||||
|
// accordance with the jose specification.
|
||||||
|
// http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-31#section-2
|
||||||
|
func joseBase64UrlDecode(s string) ([]byte, error) {
|
||||||
|
s = strings.Replace(s, "\n", "", -1)
|
||||||
|
s = strings.Replace(s, " ", "", -1)
|
||||||
|
switch len(s) % 4 {
|
||||||
|
case 0:
|
||||||
|
case 2:
|
||||||
|
s += "=="
|
||||||
|
case 3:
|
||||||
|
s += "="
|
||||||
|
default:
|
||||||
|
return nil, errors.New("illegal base64url string")
|
||||||
|
}
|
||||||
|
return base64.URLEncoding.DecodeString(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func keyIDEncode(b []byte) string {
|
||||||
|
s := strings.TrimRight(base32.StdEncoding.EncodeToString(b), "=")
|
||||||
|
var buf bytes.Buffer
|
||||||
|
var i int
|
||||||
|
for i = 0; i < len(s)/4-1; i++ {
|
||||||
|
start := i * 4
|
||||||
|
end := start + 4
|
||||||
|
buf.WriteString(s[start:end] + ":")
|
||||||
|
}
|
||||||
|
buf.WriteString(s[i*4:])
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func keyIDFromCryptoKey(pubKey PublicKey) string {
|
||||||
|
// Generate and return a 'libtrust' fingerprint of the public key.
|
||||||
|
// For an RSA key this should be:
|
||||||
|
// SHA256(DER encoded ASN1)
|
||||||
|
// Then truncated to 240 bits and encoded into 12 base32 groups like so:
|
||||||
|
// ABCD:EFGH:IJKL:MNOP:QRST:UVWX:YZ23:4567:ABCD:EFGH:IJKL:MNOP
|
||||||
|
derBytes, err := x509.MarshalPKIXPublicKey(pubKey.CryptoPublicKey())
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
hasher := crypto.SHA256.New()
|
||||||
|
hasher.Write(derBytes)
|
||||||
|
return keyIDEncode(hasher.Sum(nil)[:30])
|
||||||
|
}
|
||||||
|
|
||||||
|
func stringFromMap(m map[string]interface{}, key string) (string, error) {
|
||||||
|
val, ok := m[key]
|
||||||
|
if !ok {
|
||||||
|
return "", fmt.Errorf("%q value not specified", key)
|
||||||
|
}
|
||||||
|
|
||||||
|
str, ok := val.(string)
|
||||||
|
if !ok {
|
||||||
|
return "", fmt.Errorf("%q value must be a string", key)
|
||||||
|
}
|
||||||
|
delete(m, key)
|
||||||
|
|
||||||
|
return str, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseECCoordinate(cB64Url string, curve elliptic.Curve) (*big.Int, error) {
|
||||||
|
curveByteLen := (curve.Params().BitSize + 7) >> 3
|
||||||
|
|
||||||
|
cBytes, err := joseBase64UrlDecode(cB64Url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("invalid base64 URL encoding: %s", err)
|
||||||
|
}
|
||||||
|
cByteLength := len(cBytes)
|
||||||
|
if cByteLength != curveByteLen {
|
||||||
|
return nil, fmt.Errorf("invalid number of octets: got %d, should be %d", cByteLength, curveByteLen)
|
||||||
|
}
|
||||||
|
return new(big.Int).SetBytes(cBytes), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseECPrivateParam(dB64Url string, curve elliptic.Curve) (*big.Int, error) {
|
||||||
|
dBytes, err := joseBase64UrlDecode(dB64Url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("invalid base64 URL encoding: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The length of this octet string MUST be ceiling(log-base-2(n)/8)
|
||||||
|
// octets (where n is the order of the curve). This is because the private
|
||||||
|
// key d must be in the interval [1, n-1] so the bitlength of d should be
|
||||||
|
// no larger than the bitlength of n-1. The easiest way to find the octet
|
||||||
|
// length is to take bitlength(n-1), add 7 to force a carry, and shift this
|
||||||
|
// bit sequence right by 3, which is essentially dividing by 8 and adding
|
||||||
|
// 1 if there is any remainder. Thus, the private key value d should be
|
||||||
|
// output to (bitlength(n-1)+7)>>3 octets.
|
||||||
|
n := curve.Params().N
|
||||||
|
octetLength := (new(big.Int).Sub(n, big.NewInt(1)).BitLen() + 7) >> 3
|
||||||
|
dByteLength := len(dBytes)
|
||||||
|
|
||||||
|
if dByteLength != octetLength {
|
||||||
|
return nil, fmt.Errorf("invalid number of octets: got %d, should be %d", dByteLength, octetLength)
|
||||||
|
}
|
||||||
|
|
||||||
|
return new(big.Int).SetBytes(dBytes), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseRSAModulusParam(nB64Url string) (*big.Int, error) {
|
||||||
|
nBytes, err := joseBase64UrlDecode(nB64Url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("invalid base64 URL encoding: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return new(big.Int).SetBytes(nBytes), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func serializeRSAPublicExponentParam(e int) []byte {
|
||||||
|
// We MUST use the minimum number of octets to represent E.
|
||||||
|
// E is supposed to be 65537 for performance and security reasons
|
||||||
|
// and is what golang's rsa package generates, but it might be
|
||||||
|
// different if imported from some other generator.
|
||||||
|
buf := make([]byte, 4)
|
||||||
|
binary.BigEndian.PutUint32(buf, uint32(e))
|
||||||
|
var i int
|
||||||
|
for i = 0; i < 8; i++ {
|
||||||
|
if buf[i] != 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return buf[i:]
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseRSAPublicExponentParam(eB64Url string) (int, error) {
|
||||||
|
eBytes, err := joseBase64UrlDecode(eB64Url)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("invalid base64 URL encoding: %s", err)
|
||||||
|
}
|
||||||
|
// Only the minimum number of bytes were used to represent E, but
|
||||||
|
// binary.BigEndian.Uint32 expects at least 4 bytes, so we need
|
||||||
|
// to add zero padding if necassary.
|
||||||
|
byteLen := len(eBytes)
|
||||||
|
buf := make([]byte, 4-byteLen, 4)
|
||||||
|
eBytes = append(buf, eBytes...)
|
||||||
|
|
||||||
|
return int(binary.BigEndian.Uint32(eBytes)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseRSAPrivateKeyParamFromMap(m map[string]interface{}, key string) (*big.Int, error) {
|
||||||
|
b64Url, err := stringFromMap(m, key)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
paramBytes, err := joseBase64UrlDecode(b64Url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("invaled base64 URL encoding: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return new(big.Int).SetBytes(paramBytes), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func createPemBlock(name string, derBytes []byte, headers map[string]interface{}) (*pem.Block, error) {
|
||||||
|
pemBlock := &pem.Block{Type: name, Bytes: derBytes, Headers: map[string]string{}}
|
||||||
|
for k, v := range headers {
|
||||||
|
switch val := v.(type) {
|
||||||
|
case string:
|
||||||
|
pemBlock.Headers[k] = val
|
||||||
|
case []string:
|
||||||
|
if k == "hosts" {
|
||||||
|
pemBlock.Headers[k] = strings.Join(val, ",")
|
||||||
|
} else {
|
||||||
|
// Return error, non-encodable type
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
// Return error, non-encodable type
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return pemBlock, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func pubKeyFromPEMBlock(pemBlock *pem.Block) (PublicKey, error) {
|
||||||
|
cryptoPublicKey, err := x509.ParsePKIXPublicKey(pemBlock.Bytes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to decode Public Key PEM data: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
pubKey, err := FromCryptoPublicKey(cryptoPublicKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
addPEMHeadersToKey(pemBlock, pubKey)
|
||||||
|
|
||||||
|
return pubKey, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func addPEMHeadersToKey(pemBlock *pem.Block, pubKey PublicKey) {
|
||||||
|
for key, value := range pemBlock.Headers {
|
||||||
|
var safeVal interface{}
|
||||||
|
if key == "hosts" {
|
||||||
|
safeVal = strings.Split(value, ",")
|
||||||
|
} else {
|
||||||
|
safeVal = value
|
||||||
|
}
|
||||||
|
pubKey.AddExtendedField(key, safeVal)
|
||||||
|
}
|
||||||
|
}
|
20
vendor/github.com/gorilla/mux/.editorconfig
generated
vendored
Normal file
20
vendor/github.com/gorilla/mux/.editorconfig
generated
vendored
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
; https://editorconfig.org/
|
||||||
|
|
||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
insert_final_newline = true
|
||||||
|
charset = utf-8
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 2
|
||||||
|
|
||||||
|
[{Makefile,go.mod,go.sum,*.go,.gitmodules}]
|
||||||
|
indent_style = tab
|
||||||
|
indent_size = 4
|
||||||
|
|
||||||
|
[*.md]
|
||||||
|
indent_size = 4
|
||||||
|
trim_trailing_whitespace = false
|
||||||
|
|
||||||
|
eclint_indent_style = unset
|
1
vendor/github.com/gorilla/mux/.gitignore
generated
vendored
Normal file
1
vendor/github.com/gorilla/mux/.gitignore
generated
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
coverage.coverprofile
|
27
vendor/github.com/gorilla/mux/LICENSE
generated
vendored
Normal file
27
vendor/github.com/gorilla/mux/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
Copyright (c) 2023 The Gorilla Authors. All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are
|
||||||
|
met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer.
|
||||||
|
* 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.
|
||||||
|
* Neither the name of Google Inc. 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
|
||||||
|
OWNER 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.
|
34
vendor/github.com/gorilla/mux/Makefile
generated
vendored
Normal file
34
vendor/github.com/gorilla/mux/Makefile
generated
vendored
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
GO_LINT=$(shell which golangci-lint 2> /dev/null || echo '')
|
||||||
|
GO_LINT_URI=github.com/golangci/golangci-lint/cmd/golangci-lint@latest
|
||||||
|
|
||||||
|
GO_SEC=$(shell which gosec 2> /dev/null || echo '')
|
||||||
|
GO_SEC_URI=github.com/securego/gosec/v2/cmd/gosec@latest
|
||||||
|
|
||||||
|
GO_VULNCHECK=$(shell which govulncheck 2> /dev/null || echo '')
|
||||||
|
GO_VULNCHECK_URI=golang.org/x/vuln/cmd/govulncheck@latest
|
||||||
|
|
||||||
|
.PHONY: golangci-lint
|
||||||
|
golangci-lint:
|
||||||
|
$(if $(GO_LINT), ,go install $(GO_LINT_URI))
|
||||||
|
@echo "##### Running golangci-lint"
|
||||||
|
golangci-lint run -v
|
||||||
|
|
||||||
|
.PHONY: gosec
|
||||||
|
gosec:
|
||||||
|
$(if $(GO_SEC), ,go install $(GO_SEC_URI))
|
||||||
|
@echo "##### Running gosec"
|
||||||
|
gosec ./...
|
||||||
|
|
||||||
|
.PHONY: govulncheck
|
||||||
|
govulncheck:
|
||||||
|
$(if $(GO_VULNCHECK), ,go install $(GO_VULNCHECK_URI))
|
||||||
|
@echo "##### Running govulncheck"
|
||||||
|
govulncheck ./...
|
||||||
|
|
||||||
|
.PHONY: verify
|
||||||
|
verify: golangci-lint gosec govulncheck
|
||||||
|
|
||||||
|
.PHONY: test
|
||||||
|
test:
|
||||||
|
@echo "##### Running tests"
|
||||||
|
go test -race -cover -coverprofile=coverage.coverprofile -covermode=atomic -v ./...
|
812
vendor/github.com/gorilla/mux/README.md
generated
vendored
Normal file
812
vendor/github.com/gorilla/mux/README.md
generated
vendored
Normal file
|
@ -0,0 +1,812 @@
|
||||||
|
# gorilla/mux
|
||||||
|
|
||||||
|

|
||||||
|
[](https://codecov.io/github/gorilla/mux)
|
||||||
|
[](https://godoc.org/github.com/gorilla/mux)
|
||||||
|
[](https://sourcegraph.com/github.com/gorilla/mux?badge)
|
||||||
|
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
Package `gorilla/mux` implements a request router and dispatcher for matching incoming requests to
|
||||||
|
their respective handler.
|
||||||
|
|
||||||
|
The name mux stands for "HTTP request multiplexer". Like the standard `http.ServeMux`, `mux.Router` matches incoming requests against a list of registered routes and calls a handler for the route that matches the URL or other conditions. The main features are:
|
||||||
|
|
||||||
|
* It implements the `http.Handler` interface so it is compatible with the standard `http.ServeMux`.
|
||||||
|
* Requests can be matched based on URL host, path, path prefix, schemes, header and query values, HTTP methods or using custom matchers.
|
||||||
|
* URL hosts, paths and query values can have variables with an optional regular expression.
|
||||||
|
* Registered URLs can be built, or "reversed", which helps maintaining references to resources.
|
||||||
|
* Routes can be used as subrouters: nested routes are only tested if the parent route matches. This is useful to define groups of routes that share common conditions like a host, a path prefix or other repeated attributes. As a bonus, this optimizes request matching.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
* [Install](#install)
|
||||||
|
* [Examples](#examples)
|
||||||
|
* [Matching Routes](#matching-routes)
|
||||||
|
* [Static Files](#static-files)
|
||||||
|
* [Serving Single Page Applications](#serving-single-page-applications) (e.g. React, Vue, Ember.js, etc.)
|
||||||
|
* [Registered URLs](#registered-urls)
|
||||||
|
* [Walking Routes](#walking-routes)
|
||||||
|
* [Graceful Shutdown](#graceful-shutdown)
|
||||||
|
* [Middleware](#middleware)
|
||||||
|
* [Handling CORS Requests](#handling-cors-requests)
|
||||||
|
* [Testing Handlers](#testing-handlers)
|
||||||
|
* [Full Example](#full-example)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
With a [correctly configured](https://golang.org/doc/install#testing) Go toolchain:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
go get -u github.com/gorilla/mux
|
||||||
|
```
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
Let's start registering a couple of URL paths and handlers:
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.HandleFunc("/", HomeHandler)
|
||||||
|
r.HandleFunc("/products", ProductsHandler)
|
||||||
|
r.HandleFunc("/articles", ArticlesHandler)
|
||||||
|
http.Handle("/", r)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Here we register three routes mapping URL paths to handlers. This is equivalent to how `http.HandleFunc()` works: if an incoming request URL matches one of the paths, the corresponding handler is called passing (`http.ResponseWriter`, `*http.Request`) as parameters.
|
||||||
|
|
||||||
|
Paths can have variables. They are defined using the format `{name}` or `{name:pattern}`. If a regular expression pattern is not defined, the matched variable will be anything until the next slash. For example:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.HandleFunc("/products/{key}", ProductHandler)
|
||||||
|
r.HandleFunc("/articles/{category}/", ArticlesCategoryHandler)
|
||||||
|
r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler)
|
||||||
|
```
|
||||||
|
|
||||||
|
The names are used to create a map of route variables which can be retrieved calling `mux.Vars()`:
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ArticlesCategoryHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
vars := mux.Vars(r)
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
fmt.Fprintf(w, "Category: %v\n", vars["category"])
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
And this is all you need to know about the basic usage. More advanced options are explained below.
|
||||||
|
|
||||||
|
### Matching Routes
|
||||||
|
|
||||||
|
Routes can also be restricted to a domain or subdomain. Just define a host pattern to be matched. They can also have variables:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r := mux.NewRouter()
|
||||||
|
// Only matches if domain is "www.example.com".
|
||||||
|
r.Host("www.example.com")
|
||||||
|
// Matches a dynamic subdomain.
|
||||||
|
r.Host("{subdomain:[a-z]+}.example.com")
|
||||||
|
```
|
||||||
|
|
||||||
|
There are several other matchers that can be added. To match path prefixes:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r.PathPrefix("/products/")
|
||||||
|
```
|
||||||
|
|
||||||
|
...or HTTP methods:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r.Methods("GET", "POST")
|
||||||
|
```
|
||||||
|
|
||||||
|
...or URL schemes:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r.Schemes("https")
|
||||||
|
```
|
||||||
|
|
||||||
|
...or header values:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r.Headers("X-Requested-With", "XMLHttpRequest")
|
||||||
|
```
|
||||||
|
|
||||||
|
...or query values:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r.Queries("key", "value")
|
||||||
|
```
|
||||||
|
|
||||||
|
...or to use a custom matcher function:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r.MatcherFunc(func(r *http.Request, rm *RouteMatch) bool {
|
||||||
|
return r.ProtoMajor == 0
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
...and finally, it is possible to combine several matchers in a single route:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r.HandleFunc("/products", ProductsHandler).
|
||||||
|
Host("www.example.com").
|
||||||
|
Methods("GET").
|
||||||
|
Schemes("http")
|
||||||
|
```
|
||||||
|
|
||||||
|
Routes are tested in the order they were added to the router. If two routes match, the first one wins:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.HandleFunc("/specific", specificHandler)
|
||||||
|
r.PathPrefix("/").Handler(catchAllHandler)
|
||||||
|
```
|
||||||
|
|
||||||
|
Setting the same matching conditions again and again can be boring, so we have a way to group several routes that share the same requirements. We call it "subrouting".
|
||||||
|
|
||||||
|
For example, let's say we have several URLs that should only match when the host is `www.example.com`. Create a route for that host and get a "subrouter" from it:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r := mux.NewRouter()
|
||||||
|
s := r.Host("www.example.com").Subrouter()
|
||||||
|
```
|
||||||
|
|
||||||
|
Then register routes in the subrouter:
|
||||||
|
|
||||||
|
```go
|
||||||
|
s.HandleFunc("/products/", ProductsHandler)
|
||||||
|
s.HandleFunc("/products/{key}", ProductHandler)
|
||||||
|
s.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler)
|
||||||
|
```
|
||||||
|
|
||||||
|
The three URL paths we registered above will only be tested if the domain is `www.example.com`, because the subrouter is tested first. This is not only convenient, but also optimizes request matching. You can create subrouters combining any attribute matchers accepted by a route.
|
||||||
|
|
||||||
|
Subrouters can be used to create domain or path "namespaces": you define subrouters in a central place and then parts of the app can register its paths relatively to a given subrouter.
|
||||||
|
|
||||||
|
There's one more thing about subroutes. When a subrouter has a path prefix, the inner routes use it as base for their paths:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r := mux.NewRouter()
|
||||||
|
s := r.PathPrefix("/products").Subrouter()
|
||||||
|
// "/products/"
|
||||||
|
s.HandleFunc("/", ProductsHandler)
|
||||||
|
// "/products/{key}/"
|
||||||
|
s.HandleFunc("/{key}/", ProductHandler)
|
||||||
|
// "/products/{key}/details"
|
||||||
|
s.HandleFunc("/{key}/details", ProductDetailsHandler)
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### Static Files
|
||||||
|
|
||||||
|
Note that the path provided to `PathPrefix()` represents a "wildcard": calling
|
||||||
|
`PathPrefix("/static/").Handler(...)` means that the handler will be passed any
|
||||||
|
request that matches "/static/\*". This makes it easy to serve static files with mux:
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
var dir string
|
||||||
|
|
||||||
|
flag.StringVar(&dir, "dir", ".", "the directory to serve files from. Defaults to the current dir")
|
||||||
|
flag.Parse()
|
||||||
|
r := mux.NewRouter()
|
||||||
|
|
||||||
|
// This will serve files under http://localhost:8000/static/<filename>
|
||||||
|
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(dir))))
|
||||||
|
|
||||||
|
srv := &http.Server{
|
||||||
|
Handler: r,
|
||||||
|
Addr: "127.0.0.1:8000",
|
||||||
|
// Good practice: enforce timeouts for servers you create!
|
||||||
|
WriteTimeout: 15 * time.Second,
|
||||||
|
ReadTimeout: 15 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Fatal(srv.ListenAndServe())
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Serving Single Page Applications
|
||||||
|
|
||||||
|
Most of the time it makes sense to serve your SPA on a separate web server from your API,
|
||||||
|
but sometimes it's desirable to serve them both from one place. It's possible to write a simple
|
||||||
|
handler for serving your SPA (for use with React Router's [BrowserRouter](https://reacttraining.com/react-router/web/api/BrowserRouter) for example), and leverage
|
||||||
|
mux's powerful routing for your API endpoints.
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
)
|
||||||
|
|
||||||
|
// spaHandler implements the http.Handler interface, so we can use it
|
||||||
|
// to respond to HTTP requests. The path to the static directory and
|
||||||
|
// path to the index file within that static directory are used to
|
||||||
|
// serve the SPA in the given static directory.
|
||||||
|
type spaHandler struct {
|
||||||
|
staticPath string
|
||||||
|
indexPath string
|
||||||
|
}
|
||||||
|
|
||||||
|
// ServeHTTP inspects the URL path to locate a file within the static dir
|
||||||
|
// on the SPA handler. If a file is found, it will be served. If not, the
|
||||||
|
// file located at the index path on the SPA handler will be served. This
|
||||||
|
// is suitable behavior for serving an SPA (single page application).
|
||||||
|
func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Join internally call path.Clean to prevent directory traversal
|
||||||
|
path := filepath.Join(h.staticPath, r.URL.Path)
|
||||||
|
|
||||||
|
// check whether a file exists or is a directory at the given path
|
||||||
|
fi, err := os.Stat(path)
|
||||||
|
if os.IsNotExist(err) || fi.IsDir() {
|
||||||
|
// file does not exist or path is a directory, serve index.html
|
||||||
|
http.ServeFile(w, r, filepath.Join(h.staticPath, h.indexPath))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
// if we got an error (that wasn't that the file doesn't exist) stating the
|
||||||
|
// file, return a 500 internal server error and stop
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// otherwise, use http.FileServer to serve the static file
|
||||||
|
http.FileServer(http.Dir(h.staticPath)).ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
router := mux.NewRouter()
|
||||||
|
|
||||||
|
router.HandleFunc("/api/health", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// an example API handler
|
||||||
|
json.NewEncoder(w).Encode(map[string]bool{"ok": true})
|
||||||
|
})
|
||||||
|
|
||||||
|
spa := spaHandler{staticPath: "build", indexPath: "index.html"}
|
||||||
|
router.PathPrefix("/").Handler(spa)
|
||||||
|
|
||||||
|
srv := &http.Server{
|
||||||
|
Handler: router,
|
||||||
|
Addr: "127.0.0.1:8000",
|
||||||
|
// Good practice: enforce timeouts for servers you create!
|
||||||
|
WriteTimeout: 15 * time.Second,
|
||||||
|
ReadTimeout: 15 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Fatal(srv.ListenAndServe())
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Registered URLs
|
||||||
|
|
||||||
|
Now let's see how to build registered URLs.
|
||||||
|
|
||||||
|
Routes can be named. All routes that define a name can have their URLs built, or "reversed". We define a name calling `Name()` on a route. For example:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler).
|
||||||
|
Name("article")
|
||||||
|
```
|
||||||
|
|
||||||
|
To build a URL, get the route and call the `URL()` method, passing a sequence of key/value pairs for the route variables. For the previous route, we would do:
|
||||||
|
|
||||||
|
```go
|
||||||
|
url, err := r.Get("article").URL("category", "technology", "id", "42")
|
||||||
|
```
|
||||||
|
|
||||||
|
...and the result will be a `url.URL` with the following path:
|
||||||
|
|
||||||
|
```
|
||||||
|
"/articles/technology/42"
|
||||||
|
```
|
||||||
|
|
||||||
|
This also works for host and query value variables:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.Host("{subdomain}.example.com").
|
||||||
|
Path("/articles/{category}/{id:[0-9]+}").
|
||||||
|
Queries("filter", "{filter}").
|
||||||
|
HandlerFunc(ArticleHandler).
|
||||||
|
Name("article")
|
||||||
|
|
||||||
|
// url.String() will be "http://news.example.com/articles/technology/42?filter=gorilla"
|
||||||
|
url, err := r.Get("article").URL("subdomain", "news",
|
||||||
|
"category", "technology",
|
||||||
|
"id", "42",
|
||||||
|
"filter", "gorilla")
|
||||||
|
```
|
||||||
|
|
||||||
|
All variables defined in the route are required, and their values must conform to the corresponding patterns. These requirements guarantee that a generated URL will always match a registered route -- the only exception is for explicitly defined "build-only" routes which never match.
|
||||||
|
|
||||||
|
Regex support also exists for matching Headers within a route. For example, we could do:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r.HeadersRegexp("Content-Type", "application/(text|json)")
|
||||||
|
```
|
||||||
|
|
||||||
|
...and the route will match both requests with a Content-Type of `application/json` as well as `application/text`
|
||||||
|
|
||||||
|
There's also a way to build only the URL host or path for a route: use the methods `URLHost()` or `URLPath()` instead. For the previous route, we would do:
|
||||||
|
|
||||||
|
```go
|
||||||
|
// "http://news.example.com/"
|
||||||
|
host, err := r.Get("article").URLHost("subdomain", "news")
|
||||||
|
|
||||||
|
// "/articles/technology/42"
|
||||||
|
path, err := r.Get("article").URLPath("category", "technology", "id", "42")
|
||||||
|
```
|
||||||
|
|
||||||
|
And if you use subrouters, host and path defined separately can be built as well:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r := mux.NewRouter()
|
||||||
|
s := r.Host("{subdomain}.example.com").Subrouter()
|
||||||
|
s.Path("/articles/{category}/{id:[0-9]+}").
|
||||||
|
HandlerFunc(ArticleHandler).
|
||||||
|
Name("article")
|
||||||
|
|
||||||
|
// "http://news.example.com/articles/technology/42"
|
||||||
|
url, err := r.Get("article").URL("subdomain", "news",
|
||||||
|
"category", "technology",
|
||||||
|
"id", "42")
|
||||||
|
```
|
||||||
|
|
||||||
|
To find all the required variables for a given route when calling `URL()`, the method `GetVarNames()` is available:
|
||||||
|
```go
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.Host("{domain}").
|
||||||
|
Path("/{group}/{item_id}").
|
||||||
|
Queries("some_data1", "{some_data1}").
|
||||||
|
Queries("some_data2", "{some_data2}").
|
||||||
|
Name("article")
|
||||||
|
|
||||||
|
// Will print [domain group item_id some_data1 some_data2] <nil>
|
||||||
|
fmt.Println(r.Get("article").GetVarNames())
|
||||||
|
|
||||||
|
```
|
||||||
|
### Walking Routes
|
||||||
|
|
||||||
|
The `Walk` function on `mux.Router` can be used to visit all of the routes that are registered on a router. For example,
|
||||||
|
the following prints all of the registered routes:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
)
|
||||||
|
|
||||||
|
func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.HandleFunc("/", handler)
|
||||||
|
r.HandleFunc("/products", handler).Methods("POST")
|
||||||
|
r.HandleFunc("/articles", handler).Methods("GET")
|
||||||
|
r.HandleFunc("/articles/{id}", handler).Methods("GET", "PUT")
|
||||||
|
r.HandleFunc("/authors", handler).Queries("surname", "{surname}")
|
||||||
|
err := r.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
|
||||||
|
pathTemplate, err := route.GetPathTemplate()
|
||||||
|
if err == nil {
|
||||||
|
fmt.Println("ROUTE:", pathTemplate)
|
||||||
|
}
|
||||||
|
pathRegexp, err := route.GetPathRegexp()
|
||||||
|
if err == nil {
|
||||||
|
fmt.Println("Path regexp:", pathRegexp)
|
||||||
|
}
|
||||||
|
queriesTemplates, err := route.GetQueriesTemplates()
|
||||||
|
if err == nil {
|
||||||
|
fmt.Println("Queries templates:", strings.Join(queriesTemplates, ","))
|
||||||
|
}
|
||||||
|
queriesRegexps, err := route.GetQueriesRegexp()
|
||||||
|
if err == nil {
|
||||||
|
fmt.Println("Queries regexps:", strings.Join(queriesRegexps, ","))
|
||||||
|
}
|
||||||
|
methods, err := route.GetMethods()
|
||||||
|
if err == nil {
|
||||||
|
fmt.Println("Methods:", strings.Join(methods, ","))
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
http.Handle("/", r)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Graceful Shutdown
|
||||||
|
|
||||||
|
Go 1.8 introduced the ability to [gracefully shutdown](https://golang.org/doc/go1.8#http_shutdown) a `*http.Server`. Here's how to do that alongside `mux`:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"flag"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var wait time.Duration
|
||||||
|
flag.DurationVar(&wait, "graceful-timeout", time.Second * 15, "the duration for which the server gracefully wait for existing connections to finish - e.g. 15s or 1m")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
r := mux.NewRouter()
|
||||||
|
// Add your routes as needed
|
||||||
|
|
||||||
|
srv := &http.Server{
|
||||||
|
Addr: "0.0.0.0:8080",
|
||||||
|
// Good practice to set timeouts to avoid Slowloris attacks.
|
||||||
|
WriteTimeout: time.Second * 15,
|
||||||
|
ReadTimeout: time.Second * 15,
|
||||||
|
IdleTimeout: time.Second * 60,
|
||||||
|
Handler: r, // Pass our instance of gorilla/mux in.
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run our server in a goroutine so that it doesn't block.
|
||||||
|
go func() {
|
||||||
|
if err := srv.ListenAndServe(); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
c := make(chan os.Signal, 1)
|
||||||
|
// We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C)
|
||||||
|
// SIGKILL, SIGQUIT or SIGTERM (Ctrl+/) will not be caught.
|
||||||
|
signal.Notify(c, os.Interrupt)
|
||||||
|
|
||||||
|
// Block until we receive our signal.
|
||||||
|
<-c
|
||||||
|
|
||||||
|
// Create a deadline to wait for.
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), wait)
|
||||||
|
defer cancel()
|
||||||
|
// Doesn't block if no connections, but will otherwise wait
|
||||||
|
// until the timeout deadline.
|
||||||
|
srv.Shutdown(ctx)
|
||||||
|
// Optionally, you could run srv.Shutdown in a goroutine and block on
|
||||||
|
// <-ctx.Done() if your application should wait for other services
|
||||||
|
// to finalize based on context cancellation.
|
||||||
|
log.Println("shutting down")
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Middleware
|
||||||
|
|
||||||
|
Mux supports the addition of middlewares to a [Router](https://godoc.org/github.com/gorilla/mux#Router), which are executed in the order they are added if a match is found, including its subrouters.
|
||||||
|
Middlewares are (typically) small pieces of code which take one request, do something with it, and pass it down to another middleware or the final handler. Some common use cases for middleware are request logging, header manipulation, or `ResponseWriter` hijacking.
|
||||||
|
|
||||||
|
Mux middlewares are defined using the de facto standard type:
|
||||||
|
|
||||||
|
```go
|
||||||
|
type MiddlewareFunc func(http.Handler) http.Handler
|
||||||
|
```
|
||||||
|
|
||||||
|
Typically, the returned handler is a closure which does something with the http.ResponseWriter and http.Request passed to it, and then calls the handler passed as parameter to the MiddlewareFunc. This takes advantage of closures being able access variables from the context where they are created, while retaining the signature enforced by the receivers.
|
||||||
|
|
||||||
|
A very basic middleware which logs the URI of the request being handled could be written as:
|
||||||
|
|
||||||
|
```go
|
||||||
|
func loggingMiddleware(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Do stuff here
|
||||||
|
log.Println(r.RequestURI)
|
||||||
|
// Call the next handler, which can be another middleware in the chain, or the final handler.
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Middlewares can be added to a router using `Router.Use()`:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.HandleFunc("/", handler)
|
||||||
|
r.Use(loggingMiddleware)
|
||||||
|
```
|
||||||
|
|
||||||
|
A more complex authentication middleware, which maps session token to users, could be written as:
|
||||||
|
|
||||||
|
```go
|
||||||
|
// Define our struct
|
||||||
|
type authenticationMiddleware struct {
|
||||||
|
tokenUsers map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize it somewhere
|
||||||
|
func (amw *authenticationMiddleware) Populate() {
|
||||||
|
amw.tokenUsers["00000000"] = "user0"
|
||||||
|
amw.tokenUsers["aaaaaaaa"] = "userA"
|
||||||
|
amw.tokenUsers["05f717e5"] = "randomUser"
|
||||||
|
amw.tokenUsers["deadbeef"] = "user0"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Middleware function, which will be called for each request
|
||||||
|
func (amw *authenticationMiddleware) Middleware(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
token := r.Header.Get("X-Session-Token")
|
||||||
|
|
||||||
|
if user, found := amw.tokenUsers[token]; found {
|
||||||
|
// We found the token in our map
|
||||||
|
log.Printf("Authenticated user %s\n", user)
|
||||||
|
// Pass down the request to the next middleware (or final handler)
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
} else {
|
||||||
|
// Write an error and stop the handler chain
|
||||||
|
http.Error(w, "Forbidden", http.StatusForbidden)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```go
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.HandleFunc("/", handler)
|
||||||
|
|
||||||
|
amw := authenticationMiddleware{tokenUsers: make(map[string]string)}
|
||||||
|
amw.Populate()
|
||||||
|
|
||||||
|
r.Use(amw.Middleware)
|
||||||
|
```
|
||||||
|
|
||||||
|
Note: The handler chain will be stopped if your middleware doesn't call `next.ServeHTTP()` with the corresponding parameters. This can be used to abort a request if the middleware writer wants to. Middlewares _should_ write to `ResponseWriter` if they _are_ going to terminate the request, and they _should not_ write to `ResponseWriter` if they _are not_ going to terminate it.
|
||||||
|
|
||||||
|
### Handling CORS Requests
|
||||||
|
|
||||||
|
[CORSMethodMiddleware](https://godoc.org/github.com/gorilla/mux#CORSMethodMiddleware) intends to make it easier to strictly set the `Access-Control-Allow-Methods` response header.
|
||||||
|
|
||||||
|
* You will still need to use your own CORS handler to set the other CORS headers such as `Access-Control-Allow-Origin`
|
||||||
|
* The middleware will set the `Access-Control-Allow-Methods` header to all the method matchers (e.g. `r.Methods(http.MethodGet, http.MethodPut, http.MethodOptions)` -> `Access-Control-Allow-Methods: GET,PUT,OPTIONS`) on a route
|
||||||
|
* If you do not specify any methods, then:
|
||||||
|
> _Important_: there must be an `OPTIONS` method matcher for the middleware to set the headers.
|
||||||
|
|
||||||
|
Here is an example of using `CORSMethodMiddleware` along with a custom `OPTIONS` handler to set all the required CORS headers:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := mux.NewRouter()
|
||||||
|
|
||||||
|
// IMPORTANT: you must specify an OPTIONS method matcher for the middleware to set CORS headers
|
||||||
|
r.HandleFunc("/foo", fooHandler).Methods(http.MethodGet, http.MethodPut, http.MethodPatch, http.MethodOptions)
|
||||||
|
r.Use(mux.CORSMethodMiddleware(r))
|
||||||
|
|
||||||
|
http.ListenAndServe(":8080", r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func fooHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
if r.Method == http.MethodOptions {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Write([]byte("foo"))
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
And an request to `/foo` using something like:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl localhost:8080/foo -v
|
||||||
|
```
|
||||||
|
|
||||||
|
Would look like:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
* Trying ::1...
|
||||||
|
* TCP_NODELAY set
|
||||||
|
* Connected to localhost (::1) port 8080 (#0)
|
||||||
|
> GET /foo HTTP/1.1
|
||||||
|
> Host: localhost:8080
|
||||||
|
> User-Agent: curl/7.59.0
|
||||||
|
> Accept: */*
|
||||||
|
>
|
||||||
|
< HTTP/1.1 200 OK
|
||||||
|
< Access-Control-Allow-Methods: GET,PUT,PATCH,OPTIONS
|
||||||
|
< Access-Control-Allow-Origin: *
|
||||||
|
< Date: Fri, 28 Jun 2019 20:13:30 GMT
|
||||||
|
< Content-Length: 3
|
||||||
|
< Content-Type: text/plain; charset=utf-8
|
||||||
|
<
|
||||||
|
* Connection #0 to host localhost left intact
|
||||||
|
foo
|
||||||
|
```
|
||||||
|
|
||||||
|
### Testing Handlers
|
||||||
|
|
||||||
|
Testing handlers in a Go web application is straightforward, and _mux_ doesn't complicate this any further. Given two files: `endpoints.go` and `endpoints_test.go`, here's how we'd test an application using _mux_.
|
||||||
|
|
||||||
|
First, our simple HTTP handler:
|
||||||
|
|
||||||
|
```go
|
||||||
|
// endpoints.go
|
||||||
|
package main
|
||||||
|
|
||||||
|
func HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// A very simple health check.
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
|
// In the future we could report back on the status of our DB, or our cache
|
||||||
|
// (e.g. Redis) by performing a simple PING, and include them in the response.
|
||||||
|
io.WriteString(w, `{"alive": true}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.HandleFunc("/health", HealthCheckHandler)
|
||||||
|
|
||||||
|
log.Fatal(http.ListenAndServe("localhost:8080", r))
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Our test code:
|
||||||
|
|
||||||
|
```go
|
||||||
|
// endpoints_test.go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHealthCheckHandler(t *testing.T) {
|
||||||
|
// Create a request to pass to our handler. We don't have any query parameters for now, so we'll
|
||||||
|
// pass 'nil' as the third parameter.
|
||||||
|
req, err := http.NewRequest("GET", "/health", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
handler := http.HandlerFunc(HealthCheckHandler)
|
||||||
|
|
||||||
|
// Our handlers satisfy http.Handler, so we can call their ServeHTTP method
|
||||||
|
// directly and pass in our Request and ResponseRecorder.
|
||||||
|
handler.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
// Check the status code is what we expect.
|
||||||
|
if status := rr.Code; status != http.StatusOK {
|
||||||
|
t.Errorf("handler returned wrong status code: got %v want %v",
|
||||||
|
status, http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check the response body is what we expect.
|
||||||
|
expected := `{"alive": true}`
|
||||||
|
if rr.Body.String() != expected {
|
||||||
|
t.Errorf("handler returned unexpected body: got %v want %v",
|
||||||
|
rr.Body.String(), expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
In the case that our routes have [variables](#examples), we can pass those in the request. We could write
|
||||||
|
[table-driven tests](https://dave.cheney.net/2013/06/09/writing-table-driven-tests-in-go) to test multiple
|
||||||
|
possible route variables as needed.
|
||||||
|
|
||||||
|
```go
|
||||||
|
// endpoints.go
|
||||||
|
func main() {
|
||||||
|
r := mux.NewRouter()
|
||||||
|
// A route with a route variable:
|
||||||
|
r.HandleFunc("/metrics/{type}", MetricsHandler)
|
||||||
|
|
||||||
|
log.Fatal(http.ListenAndServe("localhost:8080", r))
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Our test file, with a table-driven test of `routeVariables`:
|
||||||
|
|
||||||
|
```go
|
||||||
|
// endpoints_test.go
|
||||||
|
func TestMetricsHandler(t *testing.T) {
|
||||||
|
tt := []struct{
|
||||||
|
routeVariable string
|
||||||
|
shouldPass bool
|
||||||
|
}{
|
||||||
|
{"goroutines", true},
|
||||||
|
{"heap", true},
|
||||||
|
{"counters", true},
|
||||||
|
{"queries", true},
|
||||||
|
{"adhadaeqm3k", false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tt {
|
||||||
|
path := fmt.Sprintf("/metrics/%s", tc.routeVariable)
|
||||||
|
req, err := http.NewRequest("GET", path, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
|
// To add the vars to the context,
|
||||||
|
// we need to create a router through which we can pass the request.
|
||||||
|
router := mux.NewRouter()
|
||||||
|
router.HandleFunc("/metrics/{type}", MetricsHandler)
|
||||||
|
router.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
// In this case, our MetricsHandler returns a non-200 response
|
||||||
|
// for a route variable it doesn't know about.
|
||||||
|
if rr.Code == http.StatusOK && !tc.shouldPass {
|
||||||
|
t.Errorf("handler should have failed on routeVariable %s: got %v want %v",
|
||||||
|
tc.routeVariable, rr.Code, http.StatusOK)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Full Example
|
||||||
|
|
||||||
|
Here's a complete, runnable example of a small `mux` based server:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"log"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
)
|
||||||
|
|
||||||
|
func YourHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Write([]byte("Gorilla!\n"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := mux.NewRouter()
|
||||||
|
// Routes consist of a path and a handler function.
|
||||||
|
r.HandleFunc("/", YourHandler)
|
||||||
|
|
||||||
|
// Bind to a port and pass our router in
|
||||||
|
log.Fatal(http.ListenAndServe(":8000", r))
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
BSD licensed. See the LICENSE file for details.
|
305
vendor/github.com/gorilla/mux/doc.go
generated
vendored
Normal file
305
vendor/github.com/gorilla/mux/doc.go
generated
vendored
Normal file
|
@ -0,0 +1,305 @@
|
||||||
|
// Copyright 2012 The Gorilla Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
/*
|
||||||
|
Package mux implements a request router and dispatcher.
|
||||||
|
|
||||||
|
The name mux stands for "HTTP request multiplexer". Like the standard
|
||||||
|
http.ServeMux, mux.Router matches incoming requests against a list of
|
||||||
|
registered routes and calls a handler for the route that matches the URL
|
||||||
|
or other conditions. The main features are:
|
||||||
|
|
||||||
|
- Requests can be matched based on URL host, path, path prefix, schemes,
|
||||||
|
header and query values, HTTP methods or using custom matchers.
|
||||||
|
- URL hosts, paths and query values can have variables with an optional
|
||||||
|
regular expression.
|
||||||
|
- Registered URLs can be built, or "reversed", which helps maintaining
|
||||||
|
references to resources.
|
||||||
|
- Routes can be used as subrouters: nested routes are only tested if the
|
||||||
|
parent route matches. This is useful to define groups of routes that
|
||||||
|
share common conditions like a host, a path prefix or other repeated
|
||||||
|
attributes. As a bonus, this optimizes request matching.
|
||||||
|
- It implements the http.Handler interface so it is compatible with the
|
||||||
|
standard http.ServeMux.
|
||||||
|
|
||||||
|
Let's start registering a couple of URL paths and handlers:
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.HandleFunc("/", HomeHandler)
|
||||||
|
r.HandleFunc("/products", ProductsHandler)
|
||||||
|
r.HandleFunc("/articles", ArticlesHandler)
|
||||||
|
http.Handle("/", r)
|
||||||
|
}
|
||||||
|
|
||||||
|
Here we register three routes mapping URL paths to handlers. This is
|
||||||
|
equivalent to how http.HandleFunc() works: if an incoming request URL matches
|
||||||
|
one of the paths, the corresponding handler is called passing
|
||||||
|
(http.ResponseWriter, *http.Request) as parameters.
|
||||||
|
|
||||||
|
Paths can have variables. They are defined using the format {name} or
|
||||||
|
{name:pattern}. If a regular expression pattern is not defined, the matched
|
||||||
|
variable will be anything until the next slash. For example:
|
||||||
|
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.HandleFunc("/products/{key}", ProductHandler)
|
||||||
|
r.HandleFunc("/articles/{category}/", ArticlesCategoryHandler)
|
||||||
|
r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler)
|
||||||
|
|
||||||
|
Groups can be used inside patterns, as long as they are non-capturing (?:re). For example:
|
||||||
|
|
||||||
|
r.HandleFunc("/articles/{category}/{sort:(?:asc|desc|new)}", ArticlesCategoryHandler)
|
||||||
|
|
||||||
|
The names are used to create a map of route variables which can be retrieved
|
||||||
|
calling mux.Vars():
|
||||||
|
|
||||||
|
vars := mux.Vars(request)
|
||||||
|
category := vars["category"]
|
||||||
|
|
||||||
|
Note that if any capturing groups are present, mux will panic() during parsing. To prevent
|
||||||
|
this, convert any capturing groups to non-capturing, e.g. change "/{sort:(asc|desc)}" to
|
||||||
|
"/{sort:(?:asc|desc)}". This is a change from prior versions which behaved unpredictably
|
||||||
|
when capturing groups were present.
|
||||||
|
|
||||||
|
And this is all you need to know about the basic usage. More advanced options
|
||||||
|
are explained below.
|
||||||
|
|
||||||
|
Routes can also be restricted to a domain or subdomain. Just define a host
|
||||||
|
pattern to be matched. They can also have variables:
|
||||||
|
|
||||||
|
r := mux.NewRouter()
|
||||||
|
// Only matches if domain is "www.example.com".
|
||||||
|
r.Host("www.example.com")
|
||||||
|
// Matches a dynamic subdomain.
|
||||||
|
r.Host("{subdomain:[a-z]+}.domain.com")
|
||||||
|
|
||||||
|
There are several other matchers that can be added. To match path prefixes:
|
||||||
|
|
||||||
|
r.PathPrefix("/products/")
|
||||||
|
|
||||||
|
...or HTTP methods:
|
||||||
|
|
||||||
|
r.Methods("GET", "POST")
|
||||||
|
|
||||||
|
...or URL schemes:
|
||||||
|
|
||||||
|
r.Schemes("https")
|
||||||
|
|
||||||
|
...or header values:
|
||||||
|
|
||||||
|
r.Headers("X-Requested-With", "XMLHttpRequest")
|
||||||
|
|
||||||
|
...or query values:
|
||||||
|
|
||||||
|
r.Queries("key", "value")
|
||||||
|
|
||||||
|
...or to use a custom matcher function:
|
||||||
|
|
||||||
|
r.MatcherFunc(func(r *http.Request, rm *RouteMatch) bool {
|
||||||
|
return r.ProtoMajor == 0
|
||||||
|
})
|
||||||
|
|
||||||
|
...and finally, it is possible to combine several matchers in a single route:
|
||||||
|
|
||||||
|
r.HandleFunc("/products", ProductsHandler).
|
||||||
|
Host("www.example.com").
|
||||||
|
Methods("GET").
|
||||||
|
Schemes("http")
|
||||||
|
|
||||||
|
Setting the same matching conditions again and again can be boring, so we have
|
||||||
|
a way to group several routes that share the same requirements.
|
||||||
|
We call it "subrouting".
|
||||||
|
|
||||||
|
For example, let's say we have several URLs that should only match when the
|
||||||
|
host is "www.example.com". Create a route for that host and get a "subrouter"
|
||||||
|
from it:
|
||||||
|
|
||||||
|
r := mux.NewRouter()
|
||||||
|
s := r.Host("www.example.com").Subrouter()
|
||||||
|
|
||||||
|
Then register routes in the subrouter:
|
||||||
|
|
||||||
|
s.HandleFunc("/products/", ProductsHandler)
|
||||||
|
s.HandleFunc("/products/{key}", ProductHandler)
|
||||||
|
s.HandleFunc("/articles/{category}/{id:[0-9]+}"), ArticleHandler)
|
||||||
|
|
||||||
|
The three URL paths we registered above will only be tested if the domain is
|
||||||
|
"www.example.com", because the subrouter is tested first. This is not
|
||||||
|
only convenient, but also optimizes request matching. You can create
|
||||||
|
subrouters combining any attribute matchers accepted by a route.
|
||||||
|
|
||||||
|
Subrouters can be used to create domain or path "namespaces": you define
|
||||||
|
subrouters in a central place and then parts of the app can register its
|
||||||
|
paths relatively to a given subrouter.
|
||||||
|
|
||||||
|
There's one more thing about subroutes. When a subrouter has a path prefix,
|
||||||
|
the inner routes use it as base for their paths:
|
||||||
|
|
||||||
|
r := mux.NewRouter()
|
||||||
|
s := r.PathPrefix("/products").Subrouter()
|
||||||
|
// "/products/"
|
||||||
|
s.HandleFunc("/", ProductsHandler)
|
||||||
|
// "/products/{key}/"
|
||||||
|
s.HandleFunc("/{key}/", ProductHandler)
|
||||||
|
// "/products/{key}/details"
|
||||||
|
s.HandleFunc("/{key}/details", ProductDetailsHandler)
|
||||||
|
|
||||||
|
Note that the path provided to PathPrefix() represents a "wildcard": calling
|
||||||
|
PathPrefix("/static/").Handler(...) means that the handler will be passed any
|
||||||
|
request that matches "/static/*". This makes it easy to serve static files with mux:
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var dir string
|
||||||
|
|
||||||
|
flag.StringVar(&dir, "dir", ".", "the directory to serve files from. Defaults to the current dir")
|
||||||
|
flag.Parse()
|
||||||
|
r := mux.NewRouter()
|
||||||
|
|
||||||
|
// This will serve files under http://localhost:8000/static/<filename>
|
||||||
|
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(dir))))
|
||||||
|
|
||||||
|
srv := &http.Server{
|
||||||
|
Handler: r,
|
||||||
|
Addr: "127.0.0.1:8000",
|
||||||
|
// Good practice: enforce timeouts for servers you create!
|
||||||
|
WriteTimeout: 15 * time.Second,
|
||||||
|
ReadTimeout: 15 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Fatal(srv.ListenAndServe())
|
||||||
|
}
|
||||||
|
|
||||||
|
Now let's see how to build registered URLs.
|
||||||
|
|
||||||
|
Routes can be named. All routes that define a name can have their URLs built,
|
||||||
|
or "reversed". We define a name calling Name() on a route. For example:
|
||||||
|
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler).
|
||||||
|
Name("article")
|
||||||
|
|
||||||
|
To build a URL, get the route and call the URL() method, passing a sequence of
|
||||||
|
key/value pairs for the route variables. For the previous route, we would do:
|
||||||
|
|
||||||
|
url, err := r.Get("article").URL("category", "technology", "id", "42")
|
||||||
|
|
||||||
|
...and the result will be a url.URL with the following path:
|
||||||
|
|
||||||
|
"/articles/technology/42"
|
||||||
|
|
||||||
|
This also works for host and query value variables:
|
||||||
|
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.Host("{subdomain}.domain.com").
|
||||||
|
Path("/articles/{category}/{id:[0-9]+}").
|
||||||
|
Queries("filter", "{filter}").
|
||||||
|
HandlerFunc(ArticleHandler).
|
||||||
|
Name("article")
|
||||||
|
|
||||||
|
// url.String() will be "http://news.domain.com/articles/technology/42?filter=gorilla"
|
||||||
|
url, err := r.Get("article").URL("subdomain", "news",
|
||||||
|
"category", "technology",
|
||||||
|
"id", "42",
|
||||||
|
"filter", "gorilla")
|
||||||
|
|
||||||
|
All variables defined in the route are required, and their values must
|
||||||
|
conform to the corresponding patterns. These requirements guarantee that a
|
||||||
|
generated URL will always match a registered route -- the only exception is
|
||||||
|
for explicitly defined "build-only" routes which never match.
|
||||||
|
|
||||||
|
Regex support also exists for matching Headers within a route. For example, we could do:
|
||||||
|
|
||||||
|
r.HeadersRegexp("Content-Type", "application/(text|json)")
|
||||||
|
|
||||||
|
...and the route will match both requests with a Content-Type of `application/json` as well as
|
||||||
|
`application/text`
|
||||||
|
|
||||||
|
There's also a way to build only the URL host or path for a route:
|
||||||
|
use the methods URLHost() or URLPath() instead. For the previous route,
|
||||||
|
we would do:
|
||||||
|
|
||||||
|
// "http://news.domain.com/"
|
||||||
|
host, err := r.Get("article").URLHost("subdomain", "news")
|
||||||
|
|
||||||
|
// "/articles/technology/42"
|
||||||
|
path, err := r.Get("article").URLPath("category", "technology", "id", "42")
|
||||||
|
|
||||||
|
And if you use subrouters, host and path defined separately can be built
|
||||||
|
as well:
|
||||||
|
|
||||||
|
r := mux.NewRouter()
|
||||||
|
s := r.Host("{subdomain}.domain.com").Subrouter()
|
||||||
|
s.Path("/articles/{category}/{id:[0-9]+}").
|
||||||
|
HandlerFunc(ArticleHandler).
|
||||||
|
Name("article")
|
||||||
|
|
||||||
|
// "http://news.domain.com/articles/technology/42"
|
||||||
|
url, err := r.Get("article").URL("subdomain", "news",
|
||||||
|
"category", "technology",
|
||||||
|
"id", "42")
|
||||||
|
|
||||||
|
Mux supports the addition of middlewares to a Router, which are executed in the order they are added if a match is found, including its subrouters. Middlewares are (typically) small pieces of code which take one request, do something with it, and pass it down to another middleware or the final handler. Some common use cases for middleware are request logging, header manipulation, or ResponseWriter hijacking.
|
||||||
|
|
||||||
|
type MiddlewareFunc func(http.Handler) http.Handler
|
||||||
|
|
||||||
|
Typically, the returned handler is a closure which does something with the http.ResponseWriter and http.Request passed to it, and then calls the handler passed as parameter to the MiddlewareFunc (closures can access variables from the context where they are created).
|
||||||
|
|
||||||
|
A very basic middleware which logs the URI of the request being handled could be written as:
|
||||||
|
|
||||||
|
func simpleMw(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Do stuff here
|
||||||
|
log.Println(r.RequestURI)
|
||||||
|
// Call the next handler, which can be another middleware in the chain, or the final handler.
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
Middlewares can be added to a router using `Router.Use()`:
|
||||||
|
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.HandleFunc("/", handler)
|
||||||
|
r.Use(simpleMw)
|
||||||
|
|
||||||
|
A more complex authentication middleware, which maps session token to users, could be written as:
|
||||||
|
|
||||||
|
// Define our struct
|
||||||
|
type authenticationMiddleware struct {
|
||||||
|
tokenUsers map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize it somewhere
|
||||||
|
func (amw *authenticationMiddleware) Populate() {
|
||||||
|
amw.tokenUsers["00000000"] = "user0"
|
||||||
|
amw.tokenUsers["aaaaaaaa"] = "userA"
|
||||||
|
amw.tokenUsers["05f717e5"] = "randomUser"
|
||||||
|
amw.tokenUsers["deadbeef"] = "user0"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Middleware function, which will be called for each request
|
||||||
|
func (amw *authenticationMiddleware) Middleware(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
token := r.Header.Get("X-Session-Token")
|
||||||
|
|
||||||
|
if user, found := amw.tokenUsers[token]; found {
|
||||||
|
// We found the token in our map
|
||||||
|
log.Printf("Authenticated user %s\n", user)
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
} else {
|
||||||
|
http.Error(w, "Forbidden", http.StatusForbidden)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.HandleFunc("/", handler)
|
||||||
|
|
||||||
|
amw := authenticationMiddleware{tokenUsers: make(map[string]string)}
|
||||||
|
amw.Populate()
|
||||||
|
|
||||||
|
r.Use(amw.Middleware)
|
||||||
|
|
||||||
|
Note: The handler chain will be stopped if your middleware doesn't call `next.ServeHTTP()` with the corresponding parameters. This can be used to abort a request if the middleware writer wants to.
|
||||||
|
*/
|
||||||
|
package mux
|
74
vendor/github.com/gorilla/mux/middleware.go
generated
vendored
Normal file
74
vendor/github.com/gorilla/mux/middleware.go
generated
vendored
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
package mux
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MiddlewareFunc is a function which receives an http.Handler and returns another http.Handler.
|
||||||
|
// Typically, the returned handler is a closure which does something with the http.ResponseWriter and http.Request passed
|
||||||
|
// to it, and then calls the handler passed as parameter to the MiddlewareFunc.
|
||||||
|
type MiddlewareFunc func(http.Handler) http.Handler
|
||||||
|
|
||||||
|
// middleware interface is anything which implements a MiddlewareFunc named Middleware.
|
||||||
|
type middleware interface {
|
||||||
|
Middleware(handler http.Handler) http.Handler
|
||||||
|
}
|
||||||
|
|
||||||
|
// Middleware allows MiddlewareFunc to implement the middleware interface.
|
||||||
|
func (mw MiddlewareFunc) Middleware(handler http.Handler) http.Handler {
|
||||||
|
return mw(handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use appends a MiddlewareFunc to the chain. Middleware can be used to intercept or otherwise modify requests and/or responses, and are executed in the order that they are applied to the Router.
|
||||||
|
func (r *Router) Use(mwf ...MiddlewareFunc) {
|
||||||
|
for _, fn := range mwf {
|
||||||
|
r.middlewares = append(r.middlewares, fn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// useInterface appends a middleware to the chain. Middleware can be used to intercept or otherwise modify requests and/or responses, and are executed in the order that they are applied to the Router.
|
||||||
|
func (r *Router) useInterface(mw middleware) {
|
||||||
|
r.middlewares = append(r.middlewares, mw)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CORSMethodMiddleware automatically sets the Access-Control-Allow-Methods response header
|
||||||
|
// on requests for routes that have an OPTIONS method matcher to all the method matchers on
|
||||||
|
// the route. Routes that do not explicitly handle OPTIONS requests will not be processed
|
||||||
|
// by the middleware. See examples for usage.
|
||||||
|
func CORSMethodMiddleware(r *Router) MiddlewareFunc {
|
||||||
|
return func(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||||
|
allMethods, err := getAllMethodsForRoute(r, req)
|
||||||
|
if err == nil {
|
||||||
|
for _, v := range allMethods {
|
||||||
|
if v == http.MethodOptions {
|
||||||
|
w.Header().Set("Access-Control-Allow-Methods", strings.Join(allMethods, ","))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
next.ServeHTTP(w, req)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// getAllMethodsForRoute returns all the methods from method matchers matching a given
|
||||||
|
// request.
|
||||||
|
func getAllMethodsForRoute(r *Router, req *http.Request) ([]string, error) {
|
||||||
|
var allMethods []string
|
||||||
|
|
||||||
|
for _, route := range r.routes {
|
||||||
|
var match RouteMatch
|
||||||
|
if route.Match(req, &match) || match.MatchErr == ErrMethodMismatch {
|
||||||
|
methods, err := route.GetMethods()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
allMethods = append(allMethods, methods...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return allMethods, nil
|
||||||
|
}
|
608
vendor/github.com/gorilla/mux/mux.go
generated
vendored
Normal file
608
vendor/github.com/gorilla/mux/mux.go
generated
vendored
Normal file
|
@ -0,0 +1,608 @@
|
||||||
|
// Copyright 2012 The Gorilla Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package mux
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"path"
|
||||||
|
"regexp"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrMethodMismatch is returned when the method in the request does not match
|
||||||
|
// the method defined against the route.
|
||||||
|
ErrMethodMismatch = errors.New("method is not allowed")
|
||||||
|
// ErrNotFound is returned when no route match is found.
|
||||||
|
ErrNotFound = errors.New("no matching route was found")
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewRouter returns a new router instance.
|
||||||
|
func NewRouter() *Router {
|
||||||
|
return &Router{namedRoutes: make(map[string]*Route)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Router registers routes to be matched and dispatches a handler.
|
||||||
|
//
|
||||||
|
// It implements the http.Handler interface, so it can be registered to serve
|
||||||
|
// requests:
|
||||||
|
//
|
||||||
|
// var router = mux.NewRouter()
|
||||||
|
//
|
||||||
|
// func main() {
|
||||||
|
// http.Handle("/", router)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// Or, for Google App Engine, register it in a init() function:
|
||||||
|
//
|
||||||
|
// func init() {
|
||||||
|
// http.Handle("/", router)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// This will send all incoming requests to the router.
|
||||||
|
type Router struct {
|
||||||
|
// Configurable Handler to be used when no route matches.
|
||||||
|
// This can be used to render your own 404 Not Found errors.
|
||||||
|
NotFoundHandler http.Handler
|
||||||
|
|
||||||
|
// Configurable Handler to be used when the request method does not match the route.
|
||||||
|
// This can be used to render your own 405 Method Not Allowed errors.
|
||||||
|
MethodNotAllowedHandler http.Handler
|
||||||
|
|
||||||
|
// Routes to be matched, in order.
|
||||||
|
routes []*Route
|
||||||
|
|
||||||
|
// Routes by name for URL building.
|
||||||
|
namedRoutes map[string]*Route
|
||||||
|
|
||||||
|
// If true, do not clear the request context after handling the request.
|
||||||
|
//
|
||||||
|
// Deprecated: No effect, since the context is stored on the request itself.
|
||||||
|
KeepContext bool
|
||||||
|
|
||||||
|
// Slice of middlewares to be called after a match is found
|
||||||
|
middlewares []middleware
|
||||||
|
|
||||||
|
// configuration shared with `Route`
|
||||||
|
routeConf
|
||||||
|
}
|
||||||
|
|
||||||
|
// common route configuration shared between `Router` and `Route`
|
||||||
|
type routeConf struct {
|
||||||
|
// If true, "/path/foo%2Fbar/to" will match the path "/path/{var}/to"
|
||||||
|
useEncodedPath bool
|
||||||
|
|
||||||
|
// If true, when the path pattern is "/path/", accessing "/path" will
|
||||||
|
// redirect to the former and vice versa.
|
||||||
|
strictSlash bool
|
||||||
|
|
||||||
|
// If true, when the path pattern is "/path//to", accessing "/path//to"
|
||||||
|
// will not redirect
|
||||||
|
skipClean bool
|
||||||
|
|
||||||
|
// Manager for the variables from host and path.
|
||||||
|
regexp routeRegexpGroup
|
||||||
|
|
||||||
|
// List of matchers.
|
||||||
|
matchers []matcher
|
||||||
|
|
||||||
|
// The scheme used when building URLs.
|
||||||
|
buildScheme string
|
||||||
|
|
||||||
|
buildVarsFunc BuildVarsFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns an effective deep copy of `routeConf`
|
||||||
|
func copyRouteConf(r routeConf) routeConf {
|
||||||
|
c := r
|
||||||
|
|
||||||
|
if r.regexp.path != nil {
|
||||||
|
c.regexp.path = copyRouteRegexp(r.regexp.path)
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.regexp.host != nil {
|
||||||
|
c.regexp.host = copyRouteRegexp(r.regexp.host)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.regexp.queries = make([]*routeRegexp, 0, len(r.regexp.queries))
|
||||||
|
for _, q := range r.regexp.queries {
|
||||||
|
c.regexp.queries = append(c.regexp.queries, copyRouteRegexp(q))
|
||||||
|
}
|
||||||
|
|
||||||
|
c.matchers = make([]matcher, len(r.matchers))
|
||||||
|
copy(c.matchers, r.matchers)
|
||||||
|
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func copyRouteRegexp(r *routeRegexp) *routeRegexp {
|
||||||
|
c := *r
|
||||||
|
return &c
|
||||||
|
}
|
||||||
|
|
||||||
|
// Match attempts to match the given request against the router's registered routes.
|
||||||
|
//
|
||||||
|
// If the request matches a route of this router or one of its subrouters the Route,
|
||||||
|
// Handler, and Vars fields of the the match argument are filled and this function
|
||||||
|
// returns true.
|
||||||
|
//
|
||||||
|
// If the request does not match any of this router's or its subrouters' routes
|
||||||
|
// then this function returns false. If available, a reason for the match failure
|
||||||
|
// will be filled in the match argument's MatchErr field. If the match failure type
|
||||||
|
// (eg: not found) has a registered handler, the handler is assigned to the Handler
|
||||||
|
// field of the match argument.
|
||||||
|
func (r *Router) Match(req *http.Request, match *RouteMatch) bool {
|
||||||
|
for _, route := range r.routes {
|
||||||
|
if route.Match(req, match) {
|
||||||
|
// Build middleware chain if no error was found
|
||||||
|
if match.MatchErr == nil {
|
||||||
|
for i := len(r.middlewares) - 1; i >= 0; i-- {
|
||||||
|
match.Handler = r.middlewares[i].Middleware(match.Handler)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if match.MatchErr == ErrMethodMismatch {
|
||||||
|
if r.MethodNotAllowedHandler != nil {
|
||||||
|
match.Handler = r.MethodNotAllowedHandler
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Closest match for a router (includes sub-routers)
|
||||||
|
if r.NotFoundHandler != nil {
|
||||||
|
match.Handler = r.NotFoundHandler
|
||||||
|
match.MatchErr = ErrNotFound
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
match.MatchErr = ErrNotFound
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// ServeHTTP dispatches the handler registered in the matched route.
|
||||||
|
//
|
||||||
|
// When there is a match, the route variables can be retrieved calling
|
||||||
|
// mux.Vars(request).
|
||||||
|
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
|
if !r.skipClean {
|
||||||
|
path := req.URL.Path
|
||||||
|
if r.useEncodedPath {
|
||||||
|
path = req.URL.EscapedPath()
|
||||||
|
}
|
||||||
|
// Clean path to canonical form and redirect.
|
||||||
|
if p := cleanPath(path); p != path {
|
||||||
|
|
||||||
|
// Added 3 lines (Philip Schlump) - It was dropping the query string and #whatever from query.
|
||||||
|
// This matches with fix in go 1.2 r.c. 4 for same problem. Go Issue:
|
||||||
|
// http://code.google.com/p/go/issues/detail?id=5252
|
||||||
|
url := *req.URL
|
||||||
|
url.Path = p
|
||||||
|
p = url.String()
|
||||||
|
|
||||||
|
w.Header().Set("Location", p)
|
||||||
|
w.WriteHeader(http.StatusMovedPermanently)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var match RouteMatch
|
||||||
|
var handler http.Handler
|
||||||
|
if r.Match(req, &match) {
|
||||||
|
handler = match.Handler
|
||||||
|
req = requestWithVars(req, match.Vars)
|
||||||
|
req = requestWithRoute(req, match.Route)
|
||||||
|
}
|
||||||
|
|
||||||
|
if handler == nil && match.MatchErr == ErrMethodMismatch {
|
||||||
|
handler = methodNotAllowedHandler()
|
||||||
|
}
|
||||||
|
|
||||||
|
if handler == nil {
|
||||||
|
handler = http.NotFoundHandler()
|
||||||
|
}
|
||||||
|
|
||||||
|
handler.ServeHTTP(w, req)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get returns a route registered with the given name.
|
||||||
|
func (r *Router) Get(name string) *Route {
|
||||||
|
return r.namedRoutes[name]
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRoute returns a route registered with the given name. This method
|
||||||
|
// was renamed to Get() and remains here for backwards compatibility.
|
||||||
|
func (r *Router) GetRoute(name string) *Route {
|
||||||
|
return r.namedRoutes[name]
|
||||||
|
}
|
||||||
|
|
||||||
|
// StrictSlash defines the trailing slash behavior for new routes. The initial
|
||||||
|
// value is false.
|
||||||
|
//
|
||||||
|
// When true, if the route path is "/path/", accessing "/path" will perform a redirect
|
||||||
|
// to the former and vice versa. In other words, your application will always
|
||||||
|
// see the path as specified in the route.
|
||||||
|
//
|
||||||
|
// When false, if the route path is "/path", accessing "/path/" will not match
|
||||||
|
// this route and vice versa.
|
||||||
|
//
|
||||||
|
// The re-direct is a HTTP 301 (Moved Permanently). Note that when this is set for
|
||||||
|
// routes with a non-idempotent method (e.g. POST, PUT), the subsequent re-directed
|
||||||
|
// request will be made as a GET by most clients. Use middleware or client settings
|
||||||
|
// to modify this behaviour as needed.
|
||||||
|
//
|
||||||
|
// Special case: when a route sets a path prefix using the PathPrefix() method,
|
||||||
|
// strict slash is ignored for that route because the redirect behavior can't
|
||||||
|
// be determined from a prefix alone. However, any subrouters created from that
|
||||||
|
// route inherit the original StrictSlash setting.
|
||||||
|
func (r *Router) StrictSlash(value bool) *Router {
|
||||||
|
r.strictSlash = value
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SkipClean defines the path cleaning behaviour for new routes. The initial
|
||||||
|
// value is false. Users should be careful about which routes are not cleaned
|
||||||
|
//
|
||||||
|
// When true, if the route path is "/path//to", it will remain with the double
|
||||||
|
// slash. This is helpful if you have a route like: /fetch/http://xkcd.com/534/
|
||||||
|
//
|
||||||
|
// When false, the path will be cleaned, so /fetch/http://xkcd.com/534/ will
|
||||||
|
// become /fetch/http/xkcd.com/534
|
||||||
|
func (r *Router) SkipClean(value bool) *Router {
|
||||||
|
r.skipClean = value
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// UseEncodedPath tells the router to match the encoded original path
|
||||||
|
// to the routes.
|
||||||
|
// For eg. "/path/foo%2Fbar/to" will match the path "/path/{var}/to".
|
||||||
|
//
|
||||||
|
// If not called, the router will match the unencoded path to the routes.
|
||||||
|
// For eg. "/path/foo%2Fbar/to" will match the path "/path/foo/bar/to"
|
||||||
|
func (r *Router) UseEncodedPath() *Router {
|
||||||
|
r.useEncodedPath = true
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// Route factories
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// NewRoute registers an empty route.
|
||||||
|
func (r *Router) NewRoute() *Route {
|
||||||
|
// initialize a route with a copy of the parent router's configuration
|
||||||
|
route := &Route{routeConf: copyRouteConf(r.routeConf), namedRoutes: r.namedRoutes}
|
||||||
|
r.routes = append(r.routes, route)
|
||||||
|
return route
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name registers a new route with a name.
|
||||||
|
// See Route.Name().
|
||||||
|
func (r *Router) Name(name string) *Route {
|
||||||
|
return r.NewRoute().Name(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle registers a new route with a matcher for the URL path.
|
||||||
|
// See Route.Path() and Route.Handler().
|
||||||
|
func (r *Router) Handle(path string, handler http.Handler) *Route {
|
||||||
|
return r.NewRoute().Path(path).Handler(handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
// HandleFunc registers a new route with a matcher for the URL path.
|
||||||
|
// See Route.Path() and Route.HandlerFunc().
|
||||||
|
func (r *Router) HandleFunc(path string, f func(http.ResponseWriter,
|
||||||
|
*http.Request)) *Route {
|
||||||
|
return r.NewRoute().Path(path).HandlerFunc(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Headers registers a new route with a matcher for request header values.
|
||||||
|
// See Route.Headers().
|
||||||
|
func (r *Router) Headers(pairs ...string) *Route {
|
||||||
|
return r.NewRoute().Headers(pairs...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Host registers a new route with a matcher for the URL host.
|
||||||
|
// See Route.Host().
|
||||||
|
func (r *Router) Host(tpl string) *Route {
|
||||||
|
return r.NewRoute().Host(tpl)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MatcherFunc registers a new route with a custom matcher function.
|
||||||
|
// See Route.MatcherFunc().
|
||||||
|
func (r *Router) MatcherFunc(f MatcherFunc) *Route {
|
||||||
|
return r.NewRoute().MatcherFunc(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Methods registers a new route with a matcher for HTTP methods.
|
||||||
|
// See Route.Methods().
|
||||||
|
func (r *Router) Methods(methods ...string) *Route {
|
||||||
|
return r.NewRoute().Methods(methods...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Path registers a new route with a matcher for the URL path.
|
||||||
|
// See Route.Path().
|
||||||
|
func (r *Router) Path(tpl string) *Route {
|
||||||
|
return r.NewRoute().Path(tpl)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PathPrefix registers a new route with a matcher for the URL path prefix.
|
||||||
|
// See Route.PathPrefix().
|
||||||
|
func (r *Router) PathPrefix(tpl string) *Route {
|
||||||
|
return r.NewRoute().PathPrefix(tpl)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Queries registers a new route with a matcher for URL query values.
|
||||||
|
// See Route.Queries().
|
||||||
|
func (r *Router) Queries(pairs ...string) *Route {
|
||||||
|
return r.NewRoute().Queries(pairs...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Schemes registers a new route with a matcher for URL schemes.
|
||||||
|
// See Route.Schemes().
|
||||||
|
func (r *Router) Schemes(schemes ...string) *Route {
|
||||||
|
return r.NewRoute().Schemes(schemes...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BuildVarsFunc registers a new route with a custom function for modifying
|
||||||
|
// route variables before building a URL.
|
||||||
|
func (r *Router) BuildVarsFunc(f BuildVarsFunc) *Route {
|
||||||
|
return r.NewRoute().BuildVarsFunc(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Walk walks the router and all its sub-routers, calling walkFn for each route
|
||||||
|
// in the tree. The routes are walked in the order they were added. Sub-routers
|
||||||
|
// are explored depth-first.
|
||||||
|
func (r *Router) Walk(walkFn WalkFunc) error {
|
||||||
|
return r.walk(walkFn, []*Route{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// SkipRouter is used as a return value from WalkFuncs to indicate that the
|
||||||
|
// router that walk is about to descend down to should be skipped.
|
||||||
|
var SkipRouter = errors.New("skip this router")
|
||||||
|
|
||||||
|
// WalkFunc is the type of the function called for each route visited by Walk.
|
||||||
|
// At every invocation, it is given the current route, and the current router,
|
||||||
|
// and a list of ancestor routes that lead to the current route.
|
||||||
|
type WalkFunc func(route *Route, router *Router, ancestors []*Route) error
|
||||||
|
|
||||||
|
func (r *Router) walk(walkFn WalkFunc, ancestors []*Route) error {
|
||||||
|
for _, t := range r.routes {
|
||||||
|
err := walkFn(t, r, ancestors)
|
||||||
|
if err == SkipRouter {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, sr := range t.matchers {
|
||||||
|
if h, ok := sr.(*Router); ok {
|
||||||
|
ancestors = append(ancestors, t)
|
||||||
|
err := h.walk(walkFn, ancestors)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ancestors = ancestors[:len(ancestors)-1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if h, ok := t.handler.(*Router); ok {
|
||||||
|
ancestors = append(ancestors, t)
|
||||||
|
err := h.walk(walkFn, ancestors)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ancestors = ancestors[:len(ancestors)-1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// Context
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// RouteMatch stores information about a matched route.
|
||||||
|
type RouteMatch struct {
|
||||||
|
Route *Route
|
||||||
|
Handler http.Handler
|
||||||
|
Vars map[string]string
|
||||||
|
|
||||||
|
// MatchErr is set to appropriate matching error
|
||||||
|
// It is set to ErrMethodMismatch if there is a mismatch in
|
||||||
|
// the request method and route method
|
||||||
|
MatchErr error
|
||||||
|
}
|
||||||
|
|
||||||
|
type contextKey int
|
||||||
|
|
||||||
|
const (
|
||||||
|
varsKey contextKey = iota
|
||||||
|
routeKey
|
||||||
|
)
|
||||||
|
|
||||||
|
// Vars returns the route variables for the current request, if any.
|
||||||
|
func Vars(r *http.Request) map[string]string {
|
||||||
|
if rv := r.Context().Value(varsKey); rv != nil {
|
||||||
|
return rv.(map[string]string)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CurrentRoute returns the matched route for the current request, if any.
|
||||||
|
// This only works when called inside the handler of the matched route
|
||||||
|
// because the matched route is stored in the request context which is cleared
|
||||||
|
// after the handler returns.
|
||||||
|
func CurrentRoute(r *http.Request) *Route {
|
||||||
|
if rv := r.Context().Value(routeKey); rv != nil {
|
||||||
|
return rv.(*Route)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func requestWithVars(r *http.Request, vars map[string]string) *http.Request {
|
||||||
|
ctx := context.WithValue(r.Context(), varsKey, vars)
|
||||||
|
return r.WithContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func requestWithRoute(r *http.Request, route *Route) *http.Request {
|
||||||
|
ctx := context.WithValue(r.Context(), routeKey, route)
|
||||||
|
return r.WithContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// Helpers
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// cleanPath returns the canonical path for p, eliminating . and .. elements.
|
||||||
|
// Borrowed from the net/http package.
|
||||||
|
func cleanPath(p string) string {
|
||||||
|
if p == "" {
|
||||||
|
return "/"
|
||||||
|
}
|
||||||
|
if p[0] != '/' {
|
||||||
|
p = "/" + p
|
||||||
|
}
|
||||||
|
np := path.Clean(p)
|
||||||
|
// path.Clean removes trailing slash except for root;
|
||||||
|
// put the trailing slash back if necessary.
|
||||||
|
if p[len(p)-1] == '/' && np != "/" {
|
||||||
|
np += "/"
|
||||||
|
}
|
||||||
|
|
||||||
|
return np
|
||||||
|
}
|
||||||
|
|
||||||
|
// uniqueVars returns an error if two slices contain duplicated strings.
|
||||||
|
func uniqueVars(s1, s2 []string) error {
|
||||||
|
for _, v1 := range s1 {
|
||||||
|
for _, v2 := range s2 {
|
||||||
|
if v1 == v2 {
|
||||||
|
return fmt.Errorf("mux: duplicated route variable %q", v2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkPairs returns the count of strings passed in, and an error if
|
||||||
|
// the count is not an even number.
|
||||||
|
func checkPairs(pairs ...string) (int, error) {
|
||||||
|
length := len(pairs)
|
||||||
|
if length%2 != 0 {
|
||||||
|
return length, fmt.Errorf(
|
||||||
|
"mux: number of parameters must be multiple of 2, got %v", pairs)
|
||||||
|
}
|
||||||
|
return length, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// mapFromPairsToString converts variadic string parameters to a
|
||||||
|
// string to string map.
|
||||||
|
func mapFromPairsToString(pairs ...string) (map[string]string, error) {
|
||||||
|
length, err := checkPairs(pairs...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
m := make(map[string]string, length/2)
|
||||||
|
for i := 0; i < length; i += 2 {
|
||||||
|
m[pairs[i]] = pairs[i+1]
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// mapFromPairsToRegex converts variadic string parameters to a
|
||||||
|
// string to regex map.
|
||||||
|
func mapFromPairsToRegex(pairs ...string) (map[string]*regexp.Regexp, error) {
|
||||||
|
length, err := checkPairs(pairs...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
m := make(map[string]*regexp.Regexp, length/2)
|
||||||
|
for i := 0; i < length; i += 2 {
|
||||||
|
regex, err := regexp.Compile(pairs[i+1])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
m[pairs[i]] = regex
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// matchInArray returns true if the given string value is in the array.
|
||||||
|
func matchInArray(arr []string, value string) bool {
|
||||||
|
for _, v := range arr {
|
||||||
|
if v == value {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// matchMapWithString returns true if the given key/value pairs exist in a given map.
|
||||||
|
func matchMapWithString(toCheck map[string]string, toMatch map[string][]string, canonicalKey bool) bool {
|
||||||
|
for k, v := range toCheck {
|
||||||
|
// Check if key exists.
|
||||||
|
if canonicalKey {
|
||||||
|
k = http.CanonicalHeaderKey(k)
|
||||||
|
}
|
||||||
|
if values := toMatch[k]; values == nil {
|
||||||
|
return false
|
||||||
|
} else if v != "" {
|
||||||
|
// If value was defined as an empty string we only check that the
|
||||||
|
// key exists. Otherwise we also check for equality.
|
||||||
|
valueExists := false
|
||||||
|
for _, value := range values {
|
||||||
|
if v == value {
|
||||||
|
valueExists = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !valueExists {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// matchMapWithRegex returns true if the given key/value pairs exist in a given map compiled against
|
||||||
|
// the given regex
|
||||||
|
func matchMapWithRegex(toCheck map[string]*regexp.Regexp, toMatch map[string][]string, canonicalKey bool) bool {
|
||||||
|
for k, v := range toCheck {
|
||||||
|
// Check if key exists.
|
||||||
|
if canonicalKey {
|
||||||
|
k = http.CanonicalHeaderKey(k)
|
||||||
|
}
|
||||||
|
if values := toMatch[k]; values == nil {
|
||||||
|
return false
|
||||||
|
} else if v != nil {
|
||||||
|
// If value was defined as an empty string we only check that the
|
||||||
|
// key exists. Otherwise we also check for equality.
|
||||||
|
valueExists := false
|
||||||
|
for _, value := range values {
|
||||||
|
if v.MatchString(value) {
|
||||||
|
valueExists = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !valueExists {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// methodNotAllowed replies to the request with an HTTP status code 405.
|
||||||
|
func methodNotAllowed(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
|
}
|
||||||
|
|
||||||
|
// methodNotAllowedHandler returns a simple request handler
|
||||||
|
// that replies to each request with a status code 405.
|
||||||
|
func methodNotAllowedHandler() http.Handler { return http.HandlerFunc(methodNotAllowed) }
|
388
vendor/github.com/gorilla/mux/regexp.go
generated
vendored
Normal file
388
vendor/github.com/gorilla/mux/regexp.go
generated
vendored
Normal file
|
@ -0,0 +1,388 @@
|
||||||
|
// Copyright 2012 The Gorilla Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package mux
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type routeRegexpOptions struct {
|
||||||
|
strictSlash bool
|
||||||
|
useEncodedPath bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type regexpType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
regexpTypePath regexpType = iota
|
||||||
|
regexpTypeHost
|
||||||
|
regexpTypePrefix
|
||||||
|
regexpTypeQuery
|
||||||
|
)
|
||||||
|
|
||||||
|
// newRouteRegexp parses a route template and returns a routeRegexp,
|
||||||
|
// used to match a host, a path or a query string.
|
||||||
|
//
|
||||||
|
// It will extract named variables, assemble a regexp to be matched, create
|
||||||
|
// a "reverse" template to build URLs and compile regexps to validate variable
|
||||||
|
// values used in URL building.
|
||||||
|
//
|
||||||
|
// Previously we accepted only Python-like identifiers for variable
|
||||||
|
// names ([a-zA-Z_][a-zA-Z0-9_]*), but currently the only restriction is that
|
||||||
|
// name and pattern can't be empty, and names can't contain a colon.
|
||||||
|
func newRouteRegexp(tpl string, typ regexpType, options routeRegexpOptions) (*routeRegexp, error) {
|
||||||
|
// Check if it is well-formed.
|
||||||
|
idxs, errBraces := braceIndices(tpl)
|
||||||
|
if errBraces != nil {
|
||||||
|
return nil, errBraces
|
||||||
|
}
|
||||||
|
// Backup the original.
|
||||||
|
template := tpl
|
||||||
|
// Now let's parse it.
|
||||||
|
defaultPattern := "[^/]+"
|
||||||
|
if typ == regexpTypeQuery {
|
||||||
|
defaultPattern = ".*"
|
||||||
|
} else if typ == regexpTypeHost {
|
||||||
|
defaultPattern = "[^.]+"
|
||||||
|
}
|
||||||
|
// Only match strict slash if not matching
|
||||||
|
if typ != regexpTypePath {
|
||||||
|
options.strictSlash = false
|
||||||
|
}
|
||||||
|
// Set a flag for strictSlash.
|
||||||
|
endSlash := false
|
||||||
|
if options.strictSlash && strings.HasSuffix(tpl, "/") {
|
||||||
|
tpl = tpl[:len(tpl)-1]
|
||||||
|
endSlash = true
|
||||||
|
}
|
||||||
|
varsN := make([]string, len(idxs)/2)
|
||||||
|
varsR := make([]*regexp.Regexp, len(idxs)/2)
|
||||||
|
pattern := bytes.NewBufferString("")
|
||||||
|
pattern.WriteByte('^')
|
||||||
|
reverse := bytes.NewBufferString("")
|
||||||
|
var end int
|
||||||
|
var err error
|
||||||
|
for i := 0; i < len(idxs); i += 2 {
|
||||||
|
// Set all values we are interested in.
|
||||||
|
raw := tpl[end:idxs[i]]
|
||||||
|
end = idxs[i+1]
|
||||||
|
parts := strings.SplitN(tpl[idxs[i]+1:end-1], ":", 2)
|
||||||
|
name := parts[0]
|
||||||
|
patt := defaultPattern
|
||||||
|
if len(parts) == 2 {
|
||||||
|
patt = parts[1]
|
||||||
|
}
|
||||||
|
// Name or pattern can't be empty.
|
||||||
|
if name == "" || patt == "" {
|
||||||
|
return nil, fmt.Errorf("mux: missing name or pattern in %q",
|
||||||
|
tpl[idxs[i]:end])
|
||||||
|
}
|
||||||
|
// Build the regexp pattern.
|
||||||
|
fmt.Fprintf(pattern, "%s(?P<%s>%s)", regexp.QuoteMeta(raw), varGroupName(i/2), patt)
|
||||||
|
|
||||||
|
// Build the reverse template.
|
||||||
|
fmt.Fprintf(reverse, "%s%%s", raw)
|
||||||
|
|
||||||
|
// Append variable name and compiled pattern.
|
||||||
|
varsN[i/2] = name
|
||||||
|
varsR[i/2], err = regexp.Compile(fmt.Sprintf("^%s$", patt))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Add the remaining.
|
||||||
|
raw := tpl[end:]
|
||||||
|
pattern.WriteString(regexp.QuoteMeta(raw))
|
||||||
|
if options.strictSlash {
|
||||||
|
pattern.WriteString("[/]?")
|
||||||
|
}
|
||||||
|
if typ == regexpTypeQuery {
|
||||||
|
// Add the default pattern if the query value is empty
|
||||||
|
if queryVal := strings.SplitN(template, "=", 2)[1]; queryVal == "" {
|
||||||
|
pattern.WriteString(defaultPattern)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if typ != regexpTypePrefix {
|
||||||
|
pattern.WriteByte('$')
|
||||||
|
}
|
||||||
|
|
||||||
|
var wildcardHostPort bool
|
||||||
|
if typ == regexpTypeHost {
|
||||||
|
if !strings.Contains(pattern.String(), ":") {
|
||||||
|
wildcardHostPort = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
reverse.WriteString(raw)
|
||||||
|
if endSlash {
|
||||||
|
reverse.WriteByte('/')
|
||||||
|
}
|
||||||
|
// Compile full regexp.
|
||||||
|
reg, errCompile := regexp.Compile(pattern.String())
|
||||||
|
if errCompile != nil {
|
||||||
|
return nil, errCompile
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for capturing groups which used to work in older versions
|
||||||
|
if reg.NumSubexp() != len(idxs)/2 {
|
||||||
|
panic(fmt.Sprintf("route %s contains capture groups in its regexp. ", template) +
|
||||||
|
"Only non-capturing groups are accepted: e.g. (?:pattern) instead of (pattern)")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Done!
|
||||||
|
return &routeRegexp{
|
||||||
|
template: template,
|
||||||
|
regexpType: typ,
|
||||||
|
options: options,
|
||||||
|
regexp: reg,
|
||||||
|
reverse: reverse.String(),
|
||||||
|
varsN: varsN,
|
||||||
|
varsR: varsR,
|
||||||
|
wildcardHostPort: wildcardHostPort,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// routeRegexp stores a regexp to match a host or path and information to
|
||||||
|
// collect and validate route variables.
|
||||||
|
type routeRegexp struct {
|
||||||
|
// The unmodified template.
|
||||||
|
template string
|
||||||
|
// The type of match
|
||||||
|
regexpType regexpType
|
||||||
|
// Options for matching
|
||||||
|
options routeRegexpOptions
|
||||||
|
// Expanded regexp.
|
||||||
|
regexp *regexp.Regexp
|
||||||
|
// Reverse template.
|
||||||
|
reverse string
|
||||||
|
// Variable names.
|
||||||
|
varsN []string
|
||||||
|
// Variable regexps (validators).
|
||||||
|
varsR []*regexp.Regexp
|
||||||
|
// Wildcard host-port (no strict port match in hostname)
|
||||||
|
wildcardHostPort bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Match matches the regexp against the URL host or path.
|
||||||
|
func (r *routeRegexp) Match(req *http.Request, match *RouteMatch) bool {
|
||||||
|
if r.regexpType == regexpTypeHost {
|
||||||
|
host := getHost(req)
|
||||||
|
if r.wildcardHostPort {
|
||||||
|
// Don't be strict on the port match
|
||||||
|
if i := strings.Index(host, ":"); i != -1 {
|
||||||
|
host = host[:i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return r.regexp.MatchString(host)
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.regexpType == regexpTypeQuery {
|
||||||
|
return r.matchQueryString(req)
|
||||||
|
}
|
||||||
|
path := req.URL.Path
|
||||||
|
if r.options.useEncodedPath {
|
||||||
|
path = req.URL.EscapedPath()
|
||||||
|
}
|
||||||
|
return r.regexp.MatchString(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
// url builds a URL part using the given values.
|
||||||
|
func (r *routeRegexp) url(values map[string]string) (string, error) {
|
||||||
|
urlValues := make([]interface{}, len(r.varsN))
|
||||||
|
for k, v := range r.varsN {
|
||||||
|
value, ok := values[v]
|
||||||
|
if !ok {
|
||||||
|
return "", fmt.Errorf("mux: missing route variable %q", v)
|
||||||
|
}
|
||||||
|
if r.regexpType == regexpTypeQuery {
|
||||||
|
value = url.QueryEscape(value)
|
||||||
|
}
|
||||||
|
urlValues[k] = value
|
||||||
|
}
|
||||||
|
rv := fmt.Sprintf(r.reverse, urlValues...)
|
||||||
|
if !r.regexp.MatchString(rv) {
|
||||||
|
// The URL is checked against the full regexp, instead of checking
|
||||||
|
// individual variables. This is faster but to provide a good error
|
||||||
|
// message, we check individual regexps if the URL doesn't match.
|
||||||
|
for k, v := range r.varsN {
|
||||||
|
if !r.varsR[k].MatchString(values[v]) {
|
||||||
|
return "", fmt.Errorf(
|
||||||
|
"mux: variable %q doesn't match, expected %q", values[v],
|
||||||
|
r.varsR[k].String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return rv, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getURLQuery returns a single query parameter from a request URL.
|
||||||
|
// For a URL with foo=bar&baz=ding, we return only the relevant key
|
||||||
|
// value pair for the routeRegexp.
|
||||||
|
func (r *routeRegexp) getURLQuery(req *http.Request) string {
|
||||||
|
if r.regexpType != regexpTypeQuery {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
templateKey := strings.SplitN(r.template, "=", 2)[0]
|
||||||
|
val, ok := findFirstQueryKey(req.URL.RawQuery, templateKey)
|
||||||
|
if ok {
|
||||||
|
return templateKey + "=" + val
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// findFirstQueryKey returns the same result as (*url.URL).Query()[key][0].
|
||||||
|
// If key was not found, empty string and false is returned.
|
||||||
|
func findFirstQueryKey(rawQuery, key string) (value string, ok bool) {
|
||||||
|
query := []byte(rawQuery)
|
||||||
|
for len(query) > 0 {
|
||||||
|
foundKey := query
|
||||||
|
if i := bytes.IndexAny(foundKey, "&;"); i >= 0 {
|
||||||
|
foundKey, query = foundKey[:i], foundKey[i+1:]
|
||||||
|
} else {
|
||||||
|
query = query[:0]
|
||||||
|
}
|
||||||
|
if len(foundKey) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var value []byte
|
||||||
|
if i := bytes.IndexByte(foundKey, '='); i >= 0 {
|
||||||
|
foundKey, value = foundKey[:i], foundKey[i+1:]
|
||||||
|
}
|
||||||
|
if len(foundKey) < len(key) {
|
||||||
|
// Cannot possibly be key.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
keyString, err := url.QueryUnescape(string(foundKey))
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if keyString != key {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
valueString, err := url.QueryUnescape(string(value))
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return valueString, true
|
||||||
|
}
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *routeRegexp) matchQueryString(req *http.Request) bool {
|
||||||
|
return r.regexp.MatchString(r.getURLQuery(req))
|
||||||
|
}
|
||||||
|
|
||||||
|
// braceIndices returns the first level curly brace indices from a string.
|
||||||
|
// It returns an error in case of unbalanced braces.
|
||||||
|
func braceIndices(s string) ([]int, error) {
|
||||||
|
var level, idx int
|
||||||
|
var idxs []int
|
||||||
|
for i := 0; i < len(s); i++ {
|
||||||
|
switch s[i] {
|
||||||
|
case '{':
|
||||||
|
if level++; level == 1 {
|
||||||
|
idx = i
|
||||||
|
}
|
||||||
|
case '}':
|
||||||
|
if level--; level == 0 {
|
||||||
|
idxs = append(idxs, idx, i+1)
|
||||||
|
} else if level < 0 {
|
||||||
|
return nil, fmt.Errorf("mux: unbalanced braces in %q", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if level != 0 {
|
||||||
|
return nil, fmt.Errorf("mux: unbalanced braces in %q", s)
|
||||||
|
}
|
||||||
|
return idxs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// varGroupName builds a capturing group name for the indexed variable.
|
||||||
|
func varGroupName(idx int) string {
|
||||||
|
return "v" + strconv.Itoa(idx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// routeRegexpGroup
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// routeRegexpGroup groups the route matchers that carry variables.
|
||||||
|
type routeRegexpGroup struct {
|
||||||
|
host *routeRegexp
|
||||||
|
path *routeRegexp
|
||||||
|
queries []*routeRegexp
|
||||||
|
}
|
||||||
|
|
||||||
|
// setMatch extracts the variables from the URL once a route matches.
|
||||||
|
func (v routeRegexpGroup) setMatch(req *http.Request, m *RouteMatch, r *Route) {
|
||||||
|
// Store host variables.
|
||||||
|
if v.host != nil {
|
||||||
|
host := getHost(req)
|
||||||
|
if v.host.wildcardHostPort {
|
||||||
|
// Don't be strict on the port match
|
||||||
|
if i := strings.Index(host, ":"); i != -1 {
|
||||||
|
host = host[:i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
matches := v.host.regexp.FindStringSubmatchIndex(host)
|
||||||
|
if len(matches) > 0 {
|
||||||
|
extractVars(host, matches, v.host.varsN, m.Vars)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
path := req.URL.Path
|
||||||
|
if r.useEncodedPath {
|
||||||
|
path = req.URL.EscapedPath()
|
||||||
|
}
|
||||||
|
// Store path variables.
|
||||||
|
if v.path != nil {
|
||||||
|
matches := v.path.regexp.FindStringSubmatchIndex(path)
|
||||||
|
if len(matches) > 0 {
|
||||||
|
extractVars(path, matches, v.path.varsN, m.Vars)
|
||||||
|
// Check if we should redirect.
|
||||||
|
if v.path.options.strictSlash {
|
||||||
|
p1 := strings.HasSuffix(path, "/")
|
||||||
|
p2 := strings.HasSuffix(v.path.template, "/")
|
||||||
|
if p1 != p2 {
|
||||||
|
u, _ := url.Parse(req.URL.String())
|
||||||
|
if p1 {
|
||||||
|
u.Path = u.Path[:len(u.Path)-1]
|
||||||
|
} else {
|
||||||
|
u.Path += "/"
|
||||||
|
}
|
||||||
|
m.Handler = http.RedirectHandler(u.String(), http.StatusMovedPermanently)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Store query string variables.
|
||||||
|
for _, q := range v.queries {
|
||||||
|
queryURL := q.getURLQuery(req)
|
||||||
|
matches := q.regexp.FindStringSubmatchIndex(queryURL)
|
||||||
|
if len(matches) > 0 {
|
||||||
|
extractVars(queryURL, matches, q.varsN, m.Vars)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// getHost tries its best to return the request host.
|
||||||
|
// According to section 14.23 of RFC 2616 the Host header
|
||||||
|
// can include the port number if the default value of 80 is not used.
|
||||||
|
func getHost(r *http.Request) string {
|
||||||
|
if r.URL.IsAbs() {
|
||||||
|
return r.URL.Host
|
||||||
|
}
|
||||||
|
return r.Host
|
||||||
|
}
|
||||||
|
|
||||||
|
func extractVars(input string, matches []int, names []string, output map[string]string) {
|
||||||
|
for i, name := range names {
|
||||||
|
output[name] = input[matches[2*i+2]:matches[2*i+3]]
|
||||||
|
}
|
||||||
|
}
|
765
vendor/github.com/gorilla/mux/route.go
generated
vendored
Normal file
765
vendor/github.com/gorilla/mux/route.go
generated
vendored
Normal file
|
@ -0,0 +1,765 @@
|
||||||
|
// Copyright 2012 The Gorilla Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package mux
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Route stores information to match a request and build URLs.
|
||||||
|
type Route struct {
|
||||||
|
// Request handler for the route.
|
||||||
|
handler http.Handler
|
||||||
|
// If true, this route never matches: it is only used to build URLs.
|
||||||
|
buildOnly bool
|
||||||
|
// The name used to build URLs.
|
||||||
|
name string
|
||||||
|
// Error resulted from building a route.
|
||||||
|
err error
|
||||||
|
|
||||||
|
// "global" reference to all named routes
|
||||||
|
namedRoutes map[string]*Route
|
||||||
|
|
||||||
|
// config possibly passed in from `Router`
|
||||||
|
routeConf
|
||||||
|
}
|
||||||
|
|
||||||
|
// SkipClean reports whether path cleaning is enabled for this route via
|
||||||
|
// Router.SkipClean.
|
||||||
|
func (r *Route) SkipClean() bool {
|
||||||
|
return r.skipClean
|
||||||
|
}
|
||||||
|
|
||||||
|
// Match matches the route against the request.
|
||||||
|
func (r *Route) Match(req *http.Request, match *RouteMatch) bool {
|
||||||
|
if r.buildOnly || r.err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
var matchErr error
|
||||||
|
|
||||||
|
// Match everything.
|
||||||
|
for _, m := range r.matchers {
|
||||||
|
if matched := m.Match(req, match); !matched {
|
||||||
|
if _, ok := m.(methodMatcher); ok {
|
||||||
|
matchErr = ErrMethodMismatch
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ignore ErrNotFound errors. These errors arise from match call
|
||||||
|
// to Subrouters.
|
||||||
|
//
|
||||||
|
// This prevents subsequent matching subrouters from failing to
|
||||||
|
// run middleware. If not ignored, the middleware would see a
|
||||||
|
// non-nil MatchErr and be skipped, even when there was a
|
||||||
|
// matching route.
|
||||||
|
if match.MatchErr == ErrNotFound {
|
||||||
|
match.MatchErr = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
matchErr = nil // nolint:ineffassign
|
||||||
|
return false
|
||||||
|
} else {
|
||||||
|
// Multiple routes may share the same path but use different HTTP methods. For instance:
|
||||||
|
// Route 1: POST "/users/{id}".
|
||||||
|
// Route 2: GET "/users/{id}", parameters: "id": "[0-9]+".
|
||||||
|
//
|
||||||
|
// The router must handle these cases correctly. For a GET request to "/users/abc" with "id" as "-2",
|
||||||
|
// The router should return a "Not Found" error as no route fully matches this request.
|
||||||
|
if match.MatchErr == ErrMethodMismatch {
|
||||||
|
match.MatchErr = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if matchErr != nil {
|
||||||
|
match.MatchErr = matchErr
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if match.MatchErr == ErrMethodMismatch && r.handler != nil {
|
||||||
|
// We found a route which matches request method, clear MatchErr
|
||||||
|
match.MatchErr = nil
|
||||||
|
// Then override the mis-matched handler
|
||||||
|
match.Handler = r.handler
|
||||||
|
}
|
||||||
|
|
||||||
|
// Yay, we have a match. Let's collect some info about it.
|
||||||
|
if match.Route == nil {
|
||||||
|
match.Route = r
|
||||||
|
}
|
||||||
|
if match.Handler == nil {
|
||||||
|
match.Handler = r.handler
|
||||||
|
}
|
||||||
|
if match.Vars == nil {
|
||||||
|
match.Vars = make(map[string]string)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set variables.
|
||||||
|
r.regexp.setMatch(req, match, r)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// Route attributes
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// GetError returns an error resulted from building the route, if any.
|
||||||
|
func (r *Route) GetError() error {
|
||||||
|
return r.err
|
||||||
|
}
|
||||||
|
|
||||||
|
// BuildOnly sets the route to never match: it is only used to build URLs.
|
||||||
|
func (r *Route) BuildOnly() *Route {
|
||||||
|
r.buildOnly = true
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handler --------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Handler sets a handler for the route.
|
||||||
|
func (r *Route) Handler(handler http.Handler) *Route {
|
||||||
|
if r.err == nil {
|
||||||
|
r.handler = handler
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// HandlerFunc sets a handler function for the route.
|
||||||
|
func (r *Route) HandlerFunc(f func(http.ResponseWriter, *http.Request)) *Route {
|
||||||
|
return r.Handler(http.HandlerFunc(f))
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetHandler returns the handler for the route, if any.
|
||||||
|
func (r *Route) GetHandler() http.Handler {
|
||||||
|
return r.handler
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Name sets the name for the route, used to build URLs.
|
||||||
|
// It is an error to call Name more than once on a route.
|
||||||
|
func (r *Route) Name(name string) *Route {
|
||||||
|
if r.name != "" {
|
||||||
|
r.err = fmt.Errorf("mux: route already has name %q, can't set %q",
|
||||||
|
r.name, name)
|
||||||
|
}
|
||||||
|
if r.err == nil {
|
||||||
|
r.name = name
|
||||||
|
r.namedRoutes[name] = r
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetName returns the name for the route, if any.
|
||||||
|
func (r *Route) GetName() string {
|
||||||
|
return r.name
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// Matchers
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// matcher types try to match a request.
|
||||||
|
type matcher interface {
|
||||||
|
Match(*http.Request, *RouteMatch) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// addMatcher adds a matcher to the route.
|
||||||
|
func (r *Route) addMatcher(m matcher) *Route {
|
||||||
|
if r.err == nil {
|
||||||
|
r.matchers = append(r.matchers, m)
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// addRegexpMatcher adds a host or path matcher and builder to a route.
|
||||||
|
func (r *Route) addRegexpMatcher(tpl string, typ regexpType) error {
|
||||||
|
if r.err != nil {
|
||||||
|
return r.err
|
||||||
|
}
|
||||||
|
if typ == regexpTypePath || typ == regexpTypePrefix {
|
||||||
|
if len(tpl) > 0 && tpl[0] != '/' {
|
||||||
|
return fmt.Errorf("mux: path must start with a slash, got %q", tpl)
|
||||||
|
}
|
||||||
|
if r.regexp.path != nil {
|
||||||
|
tpl = strings.TrimRight(r.regexp.path.template, "/") + tpl
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rr, err := newRouteRegexp(tpl, typ, routeRegexpOptions{
|
||||||
|
strictSlash: r.strictSlash,
|
||||||
|
useEncodedPath: r.useEncodedPath,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, q := range r.regexp.queries {
|
||||||
|
if err = uniqueVars(rr.varsN, q.varsN); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if typ == regexpTypeHost {
|
||||||
|
if r.regexp.path != nil {
|
||||||
|
if err = uniqueVars(rr.varsN, r.regexp.path.varsN); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
r.regexp.host = rr
|
||||||
|
} else {
|
||||||
|
if r.regexp.host != nil {
|
||||||
|
if err = uniqueVars(rr.varsN, r.regexp.host.varsN); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if typ == regexpTypeQuery {
|
||||||
|
r.regexp.queries = append(r.regexp.queries, rr)
|
||||||
|
} else {
|
||||||
|
r.regexp.path = rr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
r.addMatcher(rr)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Headers --------------------------------------------------------------------
|
||||||
|
|
||||||
|
// headerMatcher matches the request against header values.
|
||||||
|
type headerMatcher map[string]string
|
||||||
|
|
||||||
|
func (m headerMatcher) Match(r *http.Request, match *RouteMatch) bool {
|
||||||
|
return matchMapWithString(m, r.Header, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Headers adds a matcher for request header values.
|
||||||
|
// It accepts a sequence of key/value pairs to be matched. For example:
|
||||||
|
//
|
||||||
|
// r := mux.NewRouter().NewRoute()
|
||||||
|
// r.Headers("Content-Type", "application/json",
|
||||||
|
// "X-Requested-With", "XMLHttpRequest")
|
||||||
|
//
|
||||||
|
// The above route will only match if both request header values match.
|
||||||
|
// If the value is an empty string, it will match any value if the key is set.
|
||||||
|
func (r *Route) Headers(pairs ...string) *Route {
|
||||||
|
if r.err == nil {
|
||||||
|
var headers map[string]string
|
||||||
|
headers, r.err = mapFromPairsToString(pairs...)
|
||||||
|
return r.addMatcher(headerMatcher(headers))
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// headerRegexMatcher matches the request against the route given a regex for the header
|
||||||
|
type headerRegexMatcher map[string]*regexp.Regexp
|
||||||
|
|
||||||
|
func (m headerRegexMatcher) Match(r *http.Request, match *RouteMatch) bool {
|
||||||
|
return matchMapWithRegex(m, r.Header, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// HeadersRegexp accepts a sequence of key/value pairs, where the value has regex
|
||||||
|
// support. For example:
|
||||||
|
//
|
||||||
|
// r := mux.NewRouter().NewRoute()
|
||||||
|
// r.HeadersRegexp("Content-Type", "application/(text|json)",
|
||||||
|
// "X-Requested-With", "XMLHttpRequest")
|
||||||
|
//
|
||||||
|
// The above route will only match if both the request header matches both regular expressions.
|
||||||
|
// If the value is an empty string, it will match any value if the key is set.
|
||||||
|
// Use the start and end of string anchors (^ and $) to match an exact value.
|
||||||
|
func (r *Route) HeadersRegexp(pairs ...string) *Route {
|
||||||
|
if r.err == nil {
|
||||||
|
var headers map[string]*regexp.Regexp
|
||||||
|
headers, r.err = mapFromPairsToRegex(pairs...)
|
||||||
|
return r.addMatcher(headerRegexMatcher(headers))
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// Host -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Host adds a matcher for the URL host.
|
||||||
|
// It accepts a template with zero or more URL variables enclosed by {}.
|
||||||
|
// Variables can define an optional regexp pattern to be matched:
|
||||||
|
//
|
||||||
|
// - {name} matches anything until the next dot.
|
||||||
|
//
|
||||||
|
// - {name:pattern} matches the given regexp pattern.
|
||||||
|
//
|
||||||
|
// For example:
|
||||||
|
//
|
||||||
|
// r := mux.NewRouter().NewRoute()
|
||||||
|
// r.Host("www.example.com")
|
||||||
|
// r.Host("{subdomain}.domain.com")
|
||||||
|
// r.Host("{subdomain:[a-z]+}.domain.com")
|
||||||
|
//
|
||||||
|
// Variable names must be unique in a given route. They can be retrieved
|
||||||
|
// calling mux.Vars(request).
|
||||||
|
func (r *Route) Host(tpl string) *Route {
|
||||||
|
r.err = r.addRegexpMatcher(tpl, regexpTypeHost)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// MatcherFunc ----------------------------------------------------------------
|
||||||
|
|
||||||
|
// MatcherFunc is the function signature used by custom matchers.
|
||||||
|
type MatcherFunc func(*http.Request, *RouteMatch) bool
|
||||||
|
|
||||||
|
// Match returns the match for a given request.
|
||||||
|
func (m MatcherFunc) Match(r *http.Request, match *RouteMatch) bool {
|
||||||
|
return m(r, match)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MatcherFunc adds a custom function to be used as request matcher.
|
||||||
|
func (r *Route) MatcherFunc(f MatcherFunc) *Route {
|
||||||
|
return r.addMatcher(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Methods --------------------------------------------------------------------
|
||||||
|
|
||||||
|
// methodMatcher matches the request against HTTP methods.
|
||||||
|
type methodMatcher []string
|
||||||
|
|
||||||
|
func (m methodMatcher) Match(r *http.Request, match *RouteMatch) bool {
|
||||||
|
return matchInArray(m, r.Method)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Methods adds a matcher for HTTP methods.
|
||||||
|
// It accepts a sequence of one or more methods to be matched, e.g.:
|
||||||
|
// "GET", "POST", "PUT".
|
||||||
|
func (r *Route) Methods(methods ...string) *Route {
|
||||||
|
for k, v := range methods {
|
||||||
|
methods[k] = strings.ToUpper(v)
|
||||||
|
}
|
||||||
|
return r.addMatcher(methodMatcher(methods))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Path -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Path adds a matcher for the URL path.
|
||||||
|
// It accepts a template with zero or more URL variables enclosed by {}. The
|
||||||
|
// template must start with a "/".
|
||||||
|
// Variables can define an optional regexp pattern to be matched:
|
||||||
|
//
|
||||||
|
// - {name} matches anything until the next slash.
|
||||||
|
//
|
||||||
|
// - {name:pattern} matches the given regexp pattern.
|
||||||
|
//
|
||||||
|
// For example:
|
||||||
|
//
|
||||||
|
// r := mux.NewRouter().NewRoute()
|
||||||
|
// r.Path("/products/").Handler(ProductsHandler)
|
||||||
|
// r.Path("/products/{key}").Handler(ProductsHandler)
|
||||||
|
// r.Path("/articles/{category}/{id:[0-9]+}").
|
||||||
|
// Handler(ArticleHandler)
|
||||||
|
//
|
||||||
|
// Variable names must be unique in a given route. They can be retrieved
|
||||||
|
// calling mux.Vars(request).
|
||||||
|
func (r *Route) Path(tpl string) *Route {
|
||||||
|
r.err = r.addRegexpMatcher(tpl, regexpTypePath)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// PathPrefix -----------------------------------------------------------------
|
||||||
|
|
||||||
|
// PathPrefix adds a matcher for the URL path prefix. This matches if the given
|
||||||
|
// template is a prefix of the full URL path. See Route.Path() for details on
|
||||||
|
// the tpl argument.
|
||||||
|
//
|
||||||
|
// Note that it does not treat slashes specially ("/foobar/" will be matched by
|
||||||
|
// the prefix "/foo") so you may want to use a trailing slash here.
|
||||||
|
//
|
||||||
|
// Also note that the setting of Router.StrictSlash() has no effect on routes
|
||||||
|
// with a PathPrefix matcher.
|
||||||
|
func (r *Route) PathPrefix(tpl string) *Route {
|
||||||
|
r.err = r.addRegexpMatcher(tpl, regexpTypePrefix)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// Query ----------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Queries adds a matcher for URL query values.
|
||||||
|
// It accepts a sequence of key/value pairs. Values may define variables.
|
||||||
|
// For example:
|
||||||
|
//
|
||||||
|
// r := mux.NewRouter().NewRoute()
|
||||||
|
// r.Queries("foo", "bar", "id", "{id:[0-9]+}")
|
||||||
|
//
|
||||||
|
// The above route will only match if the URL contains the defined queries
|
||||||
|
// values, e.g.: ?foo=bar&id=42.
|
||||||
|
//
|
||||||
|
// If the value is an empty string, it will match any value if the key is set.
|
||||||
|
//
|
||||||
|
// Variables can define an optional regexp pattern to be matched:
|
||||||
|
//
|
||||||
|
// - {name} matches anything until the next slash.
|
||||||
|
//
|
||||||
|
// - {name:pattern} matches the given regexp pattern.
|
||||||
|
func (r *Route) Queries(pairs ...string) *Route {
|
||||||
|
length := len(pairs)
|
||||||
|
if length%2 != 0 {
|
||||||
|
r.err = fmt.Errorf(
|
||||||
|
"mux: number of parameters must be multiple of 2, got %v", pairs)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for i := 0; i < length; i += 2 {
|
||||||
|
if r.err = r.addRegexpMatcher(pairs[i]+"="+pairs[i+1], regexpTypeQuery); r.err != nil {
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// Schemes --------------------------------------------------------------------
|
||||||
|
|
||||||
|
// schemeMatcher matches the request against URL schemes.
|
||||||
|
type schemeMatcher []string
|
||||||
|
|
||||||
|
func (m schemeMatcher) Match(r *http.Request, match *RouteMatch) bool {
|
||||||
|
scheme := r.URL.Scheme
|
||||||
|
// https://golang.org/pkg/net/http/#Request
|
||||||
|
// "For [most] server requests, fields other than Path and RawQuery will be
|
||||||
|
// empty."
|
||||||
|
// Since we're an http muxer, the scheme is either going to be http or https
|
||||||
|
// though, so we can just set it based on the tls termination state.
|
||||||
|
if scheme == "" {
|
||||||
|
if r.TLS == nil {
|
||||||
|
scheme = "http"
|
||||||
|
} else {
|
||||||
|
scheme = "https"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return matchInArray(m, scheme)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Schemes adds a matcher for URL schemes.
|
||||||
|
// It accepts a sequence of schemes to be matched, e.g.: "http", "https".
|
||||||
|
// If the request's URL has a scheme set, it will be matched against.
|
||||||
|
// Generally, the URL scheme will only be set if a previous handler set it,
|
||||||
|
// such as the ProxyHeaders handler from gorilla/handlers.
|
||||||
|
// If unset, the scheme will be determined based on the request's TLS
|
||||||
|
// termination state.
|
||||||
|
// The first argument to Schemes will be used when constructing a route URL.
|
||||||
|
func (r *Route) Schemes(schemes ...string) *Route {
|
||||||
|
for k, v := range schemes {
|
||||||
|
schemes[k] = strings.ToLower(v)
|
||||||
|
}
|
||||||
|
if len(schemes) > 0 {
|
||||||
|
r.buildScheme = schemes[0]
|
||||||
|
}
|
||||||
|
return r.addMatcher(schemeMatcher(schemes))
|
||||||
|
}
|
||||||
|
|
||||||
|
// BuildVarsFunc --------------------------------------------------------------
|
||||||
|
|
||||||
|
// BuildVarsFunc is the function signature used by custom build variable
|
||||||
|
// functions (which can modify route variables before a route's URL is built).
|
||||||
|
type BuildVarsFunc func(map[string]string) map[string]string
|
||||||
|
|
||||||
|
// BuildVarsFunc adds a custom function to be used to modify build variables
|
||||||
|
// before a route's URL is built.
|
||||||
|
func (r *Route) BuildVarsFunc(f BuildVarsFunc) *Route {
|
||||||
|
if r.buildVarsFunc != nil {
|
||||||
|
// compose the old and new functions
|
||||||
|
old := r.buildVarsFunc
|
||||||
|
r.buildVarsFunc = func(m map[string]string) map[string]string {
|
||||||
|
return f(old(m))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
r.buildVarsFunc = f
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// Subrouter ------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Subrouter creates a subrouter for the route.
|
||||||
|
//
|
||||||
|
// It will test the inner routes only if the parent route matched. For example:
|
||||||
|
//
|
||||||
|
// r := mux.NewRouter().NewRoute()
|
||||||
|
// s := r.Host("www.example.com").Subrouter()
|
||||||
|
// s.HandleFunc("/products/", ProductsHandler)
|
||||||
|
// s.HandleFunc("/products/{key}", ProductHandler)
|
||||||
|
// s.HandleFunc("/articles/{category}/{id:[0-9]+}"), ArticleHandler)
|
||||||
|
//
|
||||||
|
// Here, the routes registered in the subrouter won't be tested if the host
|
||||||
|
// doesn't match.
|
||||||
|
func (r *Route) Subrouter() *Router {
|
||||||
|
// initialize a subrouter with a copy of the parent route's configuration
|
||||||
|
router := &Router{routeConf: copyRouteConf(r.routeConf), namedRoutes: r.namedRoutes}
|
||||||
|
r.addMatcher(router)
|
||||||
|
return router
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// URL building
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// URL builds a URL for the route.
|
||||||
|
//
|
||||||
|
// It accepts a sequence of key/value pairs for the route variables. For
|
||||||
|
// example, given this route:
|
||||||
|
//
|
||||||
|
// r := mux.NewRouter()
|
||||||
|
// r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler).
|
||||||
|
// Name("article")
|
||||||
|
//
|
||||||
|
// ...a URL for it can be built using:
|
||||||
|
//
|
||||||
|
// url, err := r.Get("article").URL("category", "technology", "id", "42")
|
||||||
|
//
|
||||||
|
// ...which will return an url.URL with the following path:
|
||||||
|
//
|
||||||
|
// "/articles/technology/42"
|
||||||
|
//
|
||||||
|
// This also works for host variables:
|
||||||
|
//
|
||||||
|
// r := mux.NewRouter()
|
||||||
|
// r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler).
|
||||||
|
// Host("{subdomain}.domain.com").
|
||||||
|
// Name("article")
|
||||||
|
//
|
||||||
|
// // url.String() will be "http://news.domain.com/articles/technology/42"
|
||||||
|
// url, err := r.Get("article").URL("subdomain", "news",
|
||||||
|
// "category", "technology",
|
||||||
|
// "id", "42")
|
||||||
|
//
|
||||||
|
// The scheme of the resulting url will be the first argument that was passed to Schemes:
|
||||||
|
//
|
||||||
|
// // url.String() will be "https://example.com"
|
||||||
|
// r := mux.NewRouter().NewRoute()
|
||||||
|
// url, err := r.Host("example.com")
|
||||||
|
// .Schemes("https", "http").URL()
|
||||||
|
//
|
||||||
|
// All variables defined in the route are required, and their values must
|
||||||
|
// conform to the corresponding patterns.
|
||||||
|
func (r *Route) URL(pairs ...string) (*url.URL, error) {
|
||||||
|
if r.err != nil {
|
||||||
|
return nil, r.err
|
||||||
|
}
|
||||||
|
values, err := r.prepareVars(pairs...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var scheme, host, path string
|
||||||
|
queries := make([]string, 0, len(r.regexp.queries))
|
||||||
|
if r.regexp.host != nil {
|
||||||
|
if host, err = r.regexp.host.url(values); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
scheme = "http"
|
||||||
|
if r.buildScheme != "" {
|
||||||
|
scheme = r.buildScheme
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if r.regexp.path != nil {
|
||||||
|
if path, err = r.regexp.path.url(values); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, q := range r.regexp.queries {
|
||||||
|
var query string
|
||||||
|
if query, err = q.url(values); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
queries = append(queries, query)
|
||||||
|
}
|
||||||
|
return &url.URL{
|
||||||
|
Scheme: scheme,
|
||||||
|
Host: host,
|
||||||
|
Path: path,
|
||||||
|
RawQuery: strings.Join(queries, "&"),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// URLHost builds the host part of the URL for a route. See Route.URL().
|
||||||
|
//
|
||||||
|
// The route must have a host defined.
|
||||||
|
func (r *Route) URLHost(pairs ...string) (*url.URL, error) {
|
||||||
|
if r.err != nil {
|
||||||
|
return nil, r.err
|
||||||
|
}
|
||||||
|
if r.regexp.host == nil {
|
||||||
|
return nil, errors.New("mux: route doesn't have a host")
|
||||||
|
}
|
||||||
|
values, err := r.prepareVars(pairs...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
host, err := r.regexp.host.url(values)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
u := &url.URL{
|
||||||
|
Scheme: "http",
|
||||||
|
Host: host,
|
||||||
|
}
|
||||||
|
if r.buildScheme != "" {
|
||||||
|
u.Scheme = r.buildScheme
|
||||||
|
}
|
||||||
|
return u, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// URLPath builds the path part of the URL for a route. See Route.URL().
|
||||||
|
//
|
||||||
|
// The route must have a path defined.
|
||||||
|
func (r *Route) URLPath(pairs ...string) (*url.URL, error) {
|
||||||
|
if r.err != nil {
|
||||||
|
return nil, r.err
|
||||||
|
}
|
||||||
|
if r.regexp.path == nil {
|
||||||
|
return nil, errors.New("mux: route doesn't have a path")
|
||||||
|
}
|
||||||
|
values, err := r.prepareVars(pairs...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
path, err := r.regexp.path.url(values)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &url.URL{
|
||||||
|
Path: path,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetPathTemplate returns the template used to build the
|
||||||
|
// route match.
|
||||||
|
// This is useful for building simple REST API documentation and for instrumentation
|
||||||
|
// against third-party services.
|
||||||
|
// An error will be returned if the route does not define a path.
|
||||||
|
func (r *Route) GetPathTemplate() (string, error) {
|
||||||
|
if r.err != nil {
|
||||||
|
return "", r.err
|
||||||
|
}
|
||||||
|
if r.regexp.path == nil {
|
||||||
|
return "", errors.New("mux: route doesn't have a path")
|
||||||
|
}
|
||||||
|
return r.regexp.path.template, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetPathRegexp returns the expanded regular expression used to match route path.
|
||||||
|
// This is useful for building simple REST API documentation and for instrumentation
|
||||||
|
// against third-party services.
|
||||||
|
// An error will be returned if the route does not define a path.
|
||||||
|
func (r *Route) GetPathRegexp() (string, error) {
|
||||||
|
if r.err != nil {
|
||||||
|
return "", r.err
|
||||||
|
}
|
||||||
|
if r.regexp.path == nil {
|
||||||
|
return "", errors.New("mux: route does not have a path")
|
||||||
|
}
|
||||||
|
return r.regexp.path.regexp.String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetQueriesRegexp returns the expanded regular expressions used to match the
|
||||||
|
// route queries.
|
||||||
|
// This is useful for building simple REST API documentation and for instrumentation
|
||||||
|
// against third-party services.
|
||||||
|
// An error will be returned if the route does not have queries.
|
||||||
|
func (r *Route) GetQueriesRegexp() ([]string, error) {
|
||||||
|
if r.err != nil {
|
||||||
|
return nil, r.err
|
||||||
|
}
|
||||||
|
if r.regexp.queries == nil {
|
||||||
|
return nil, errors.New("mux: route doesn't have queries")
|
||||||
|
}
|
||||||
|
queries := make([]string, 0, len(r.regexp.queries))
|
||||||
|
for _, query := range r.regexp.queries {
|
||||||
|
queries = append(queries, query.regexp.String())
|
||||||
|
}
|
||||||
|
return queries, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetQueriesTemplates returns the templates used to build the
|
||||||
|
// query matching.
|
||||||
|
// This is useful for building simple REST API documentation and for instrumentation
|
||||||
|
// against third-party services.
|
||||||
|
// An error will be returned if the route does not define queries.
|
||||||
|
func (r *Route) GetQueriesTemplates() ([]string, error) {
|
||||||
|
if r.err != nil {
|
||||||
|
return nil, r.err
|
||||||
|
}
|
||||||
|
if r.regexp.queries == nil {
|
||||||
|
return nil, errors.New("mux: route doesn't have queries")
|
||||||
|
}
|
||||||
|
queries := make([]string, 0, len(r.regexp.queries))
|
||||||
|
for _, query := range r.regexp.queries {
|
||||||
|
queries = append(queries, query.template)
|
||||||
|
}
|
||||||
|
return queries, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMethods returns the methods the route matches against
|
||||||
|
// This is useful for building simple REST API documentation and for instrumentation
|
||||||
|
// against third-party services.
|
||||||
|
// An error will be returned if route does not have methods.
|
||||||
|
func (r *Route) GetMethods() ([]string, error) {
|
||||||
|
if r.err != nil {
|
||||||
|
return nil, r.err
|
||||||
|
}
|
||||||
|
for _, m := range r.matchers {
|
||||||
|
if methods, ok := m.(methodMatcher); ok {
|
||||||
|
return []string(methods), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, errors.New("mux: route doesn't have methods")
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetHostTemplate returns the template used to build the
|
||||||
|
// route match.
|
||||||
|
// This is useful for building simple REST API documentation and for instrumentation
|
||||||
|
// against third-party services.
|
||||||
|
// An error will be returned if the route does not define a host.
|
||||||
|
func (r *Route) GetHostTemplate() (string, error) {
|
||||||
|
if r.err != nil {
|
||||||
|
return "", r.err
|
||||||
|
}
|
||||||
|
if r.regexp.host == nil {
|
||||||
|
return "", errors.New("mux: route doesn't have a host")
|
||||||
|
}
|
||||||
|
return r.regexp.host.template, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetVarNames returns the names of all variables added by regexp matchers
|
||||||
|
// These can be used to know which route variables should be passed into r.URL()
|
||||||
|
func (r *Route) GetVarNames() ([]string, error) {
|
||||||
|
if r.err != nil {
|
||||||
|
return nil, r.err
|
||||||
|
}
|
||||||
|
var varNames []string
|
||||||
|
if r.regexp.host != nil {
|
||||||
|
varNames = append(varNames, r.regexp.host.varsN...)
|
||||||
|
}
|
||||||
|
if r.regexp.path != nil {
|
||||||
|
varNames = append(varNames, r.regexp.path.varsN...)
|
||||||
|
}
|
||||||
|
for _, regx := range r.regexp.queries {
|
||||||
|
varNames = append(varNames, regx.varsN...)
|
||||||
|
}
|
||||||
|
return varNames, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// prepareVars converts the route variable pairs into a map. If the route has a
|
||||||
|
// BuildVarsFunc, it is invoked.
|
||||||
|
func (r *Route) prepareVars(pairs ...string) (map[string]string, error) {
|
||||||
|
m, err := mapFromPairsToString(pairs...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r.buildVars(m), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Route) buildVars(m map[string]string) map[string]string {
|
||||||
|
if r.buildVarsFunc != nil {
|
||||||
|
m = r.buildVarsFunc(m)
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
19
vendor/github.com/gorilla/mux/test_helpers.go
generated
vendored
Normal file
19
vendor/github.com/gorilla/mux/test_helpers.go
generated
vendored
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
// Copyright 2012 The Gorilla Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package mux
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
// SetURLVars sets the URL variables for the given request, to be accessed via
|
||||||
|
// mux.Vars for testing route behaviour. Arguments are not modified, a shallow
|
||||||
|
// copy is returned.
|
||||||
|
//
|
||||||
|
// This API should only be used for testing purposes; it provides a way to
|
||||||
|
// inject variables into the request context. Alternatively, URL variables
|
||||||
|
// can be set by making a route that captures the required variables,
|
||||||
|
// starting a server and sending the request to that server.
|
||||||
|
func SetURLVars(r *http.Request, val map[string]string) *http.Request {
|
||||||
|
return requestWithVars(r, val)
|
||||||
|
}
|
4
vendor/github.com/sirupsen/logrus/.gitignore
generated
vendored
Normal file
4
vendor/github.com/sirupsen/logrus/.gitignore
generated
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
logrus
|
||||||
|
vendor
|
||||||
|
|
||||||
|
.idea/
|
40
vendor/github.com/sirupsen/logrus/.golangci.yml
generated
vendored
Normal file
40
vendor/github.com/sirupsen/logrus/.golangci.yml
generated
vendored
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
run:
|
||||||
|
# do not run on test files yet
|
||||||
|
tests: false
|
||||||
|
|
||||||
|
# all available settings of specific linters
|
||||||
|
linters-settings:
|
||||||
|
errcheck:
|
||||||
|
# report about not checking of errors in type assetions: `a := b.(MyStruct)`;
|
||||||
|
# default is false: such cases aren't reported by default.
|
||||||
|
check-type-assertions: false
|
||||||
|
|
||||||
|
# report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`;
|
||||||
|
# default is false: such cases aren't reported by default.
|
||||||
|
check-blank: false
|
||||||
|
|
||||||
|
lll:
|
||||||
|
line-length: 100
|
||||||
|
tab-width: 4
|
||||||
|
|
||||||
|
prealloc:
|
||||||
|
simple: false
|
||||||
|
range-loops: false
|
||||||
|
for-loops: false
|
||||||
|
|
||||||
|
whitespace:
|
||||||
|
multi-if: false # Enforces newlines (or comments) after every multi-line if statement
|
||||||
|
multi-func: false # Enforces newlines (or comments) after every multi-line function signature
|
||||||
|
|
||||||
|
linters:
|
||||||
|
enable:
|
||||||
|
- megacheck
|
||||||
|
- govet
|
||||||
|
disable:
|
||||||
|
- maligned
|
||||||
|
- prealloc
|
||||||
|
disable-all: false
|
||||||
|
presets:
|
||||||
|
- bugs
|
||||||
|
- unused
|
||||||
|
fast: false
|
15
vendor/github.com/sirupsen/logrus/.travis.yml
generated
vendored
Normal file
15
vendor/github.com/sirupsen/logrus/.travis.yml
generated
vendored
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
language: go
|
||||||
|
go_import_path: github.com/sirupsen/logrus
|
||||||
|
git:
|
||||||
|
depth: 1
|
||||||
|
env:
|
||||||
|
- GO111MODULE=on
|
||||||
|
go: 1.15.x
|
||||||
|
os: linux
|
||||||
|
install:
|
||||||
|
- ./travis/install.sh
|
||||||
|
script:
|
||||||
|
- cd ci
|
||||||
|
- go run mage.go -v -w ../ crossBuild
|
||||||
|
- go run mage.go -v -w ../ lint
|
||||||
|
- go run mage.go -v -w ../ test
|
259
vendor/github.com/sirupsen/logrus/CHANGELOG.md
generated
vendored
Normal file
259
vendor/github.com/sirupsen/logrus/CHANGELOG.md
generated
vendored
Normal file
|
@ -0,0 +1,259 @@
|
||||||
|
# 1.8.1
|
||||||
|
Code quality:
|
||||||
|
* move magefile in its own subdir/submodule to remove magefile dependency on logrus consumer
|
||||||
|
* improve timestamp format documentation
|
||||||
|
|
||||||
|
Fixes:
|
||||||
|
* fix race condition on logger hooks
|
||||||
|
|
||||||
|
|
||||||
|
# 1.8.0
|
||||||
|
|
||||||
|
Correct versioning number replacing v1.7.1.
|
||||||
|
|
||||||
|
# 1.7.1
|
||||||
|
|
||||||
|
Beware this release has introduced a new public API and its semver is therefore incorrect.
|
||||||
|
|
||||||
|
Code quality:
|
||||||
|
* use go 1.15 in travis
|
||||||
|
* use magefile as task runner
|
||||||
|
|
||||||
|
Fixes:
|
||||||
|
* small fixes about new go 1.13 error formatting system
|
||||||
|
* Fix for long time race condiction with mutating data hooks
|
||||||
|
|
||||||
|
Features:
|
||||||
|
* build support for zos
|
||||||
|
|
||||||
|
# 1.7.0
|
||||||
|
Fixes:
|
||||||
|
* the dependency toward a windows terminal library has been removed
|
||||||
|
|
||||||
|
Features:
|
||||||
|
* a new buffer pool management API has been added
|
||||||
|
* a set of `<LogLevel>Fn()` functions have been added
|
||||||
|
|
||||||
|
# 1.6.0
|
||||||
|
Fixes:
|
||||||
|
* end of line cleanup
|
||||||
|
* revert the entry concurrency bug fix whic leads to deadlock under some circumstances
|
||||||
|
* update dependency on go-windows-terminal-sequences to fix a crash with go 1.14
|
||||||
|
|
||||||
|
Features:
|
||||||
|
* add an option to the `TextFormatter` to completely disable fields quoting
|
||||||
|
|
||||||
|
# 1.5.0
|
||||||
|
Code quality:
|
||||||
|
* add golangci linter run on travis
|
||||||
|
|
||||||
|
Fixes:
|
||||||
|
* add mutex for hooks concurrent access on `Entry` data
|
||||||
|
* caller function field for go1.14
|
||||||
|
* fix build issue for gopherjs target
|
||||||
|
|
||||||
|
Feature:
|
||||||
|
* add an hooks/writer sub-package whose goal is to split output on different stream depending on the trace level
|
||||||
|
* add a `DisableHTMLEscape` option in the `JSONFormatter`
|
||||||
|
* add `ForceQuote` and `PadLevelText` options in the `TextFormatter`
|
||||||
|
|
||||||
|
# 1.4.2
|
||||||
|
* Fixes build break for plan9, nacl, solaris
|
||||||
|
# 1.4.1
|
||||||
|
This new release introduces:
|
||||||
|
* Enhance TextFormatter to not print caller information when they are empty (#944)
|
||||||
|
* Remove dependency on golang.org/x/crypto (#932, #943)
|
||||||
|
|
||||||
|
Fixes:
|
||||||
|
* Fix Entry.WithContext method to return a copy of the initial entry (#941)
|
||||||
|
|
||||||
|
# 1.4.0
|
||||||
|
This new release introduces:
|
||||||
|
* Add `DeferExitHandler`, similar to `RegisterExitHandler` but prepending the handler to the list of handlers (semantically like `defer`) (#848).
|
||||||
|
* Add `CallerPrettyfier` to `JSONFormatter` and `TextFormatter` (#909, #911)
|
||||||
|
* Add `Entry.WithContext()` and `Entry.Context`, to set a context on entries to be used e.g. in hooks (#919).
|
||||||
|
|
||||||
|
Fixes:
|
||||||
|
* Fix wrong method calls `Logger.Print` and `Logger.Warningln` (#893).
|
||||||
|
* Update `Entry.Logf` to not do string formatting unless the log level is enabled (#903)
|
||||||
|
* Fix infinite recursion on unknown `Level.String()` (#907)
|
||||||
|
* Fix race condition in `getCaller` (#916).
|
||||||
|
|
||||||
|
|
||||||
|
# 1.3.0
|
||||||
|
This new release introduces:
|
||||||
|
* Log, Logf, Logln functions for Logger and Entry that take a Level
|
||||||
|
|
||||||
|
Fixes:
|
||||||
|
* Building prometheus node_exporter on AIX (#840)
|
||||||
|
* Race condition in TextFormatter (#468)
|
||||||
|
* Travis CI import path (#868)
|
||||||
|
* Remove coloured output on Windows (#862)
|
||||||
|
* Pointer to func as field in JSONFormatter (#870)
|
||||||
|
* Properly marshal Levels (#873)
|
||||||
|
|
||||||
|
# 1.2.0
|
||||||
|
This new release introduces:
|
||||||
|
* A new method `SetReportCaller` in the `Logger` to enable the file, line and calling function from which the trace has been issued
|
||||||
|
* A new trace level named `Trace` whose level is below `Debug`
|
||||||
|
* A configurable exit function to be called upon a Fatal trace
|
||||||
|
* The `Level` object now implements `encoding.TextUnmarshaler` interface
|
||||||
|
|
||||||
|
# 1.1.1
|
||||||
|
This is a bug fix release.
|
||||||
|
* fix the build break on Solaris
|
||||||
|
* don't drop a whole trace in JSONFormatter when a field param is a function pointer which can not be serialized
|
||||||
|
|
||||||
|
# 1.1.0
|
||||||
|
This new release introduces:
|
||||||
|
* several fixes:
|
||||||
|
* a fix for a race condition on entry formatting
|
||||||
|
* proper cleanup of previously used entries before putting them back in the pool
|
||||||
|
* the extra new line at the end of message in text formatter has been removed
|
||||||
|
* a new global public API to check if a level is activated: IsLevelEnabled
|
||||||
|
* the following methods have been added to the Logger object
|
||||||
|
* IsLevelEnabled
|
||||||
|
* SetFormatter
|
||||||
|
* SetOutput
|
||||||
|
* ReplaceHooks
|
||||||
|
* introduction of go module
|
||||||
|
* an indent configuration for the json formatter
|
||||||
|
* output colour support for windows
|
||||||
|
* the field sort function is now configurable for text formatter
|
||||||
|
* the CLICOLOR and CLICOLOR\_FORCE environment variable support in text formater
|
||||||
|
|
||||||
|
# 1.0.6
|
||||||
|
|
||||||
|
This new release introduces:
|
||||||
|
* a new api WithTime which allows to easily force the time of the log entry
|
||||||
|
which is mostly useful for logger wrapper
|
||||||
|
* a fix reverting the immutability of the entry given as parameter to the hooks
|
||||||
|
a new configuration field of the json formatter in order to put all the fields
|
||||||
|
in a nested dictionnary
|
||||||
|
* a new SetOutput method in the Logger
|
||||||
|
* a new configuration of the textformatter to configure the name of the default keys
|
||||||
|
* a new configuration of the text formatter to disable the level truncation
|
||||||
|
|
||||||
|
# 1.0.5
|
||||||
|
|
||||||
|
* Fix hooks race (#707)
|
||||||
|
* Fix panic deadlock (#695)
|
||||||
|
|
||||||
|
# 1.0.4
|
||||||
|
|
||||||
|
* Fix race when adding hooks (#612)
|
||||||
|
* Fix terminal check in AppEngine (#635)
|
||||||
|
|
||||||
|
# 1.0.3
|
||||||
|
|
||||||
|
* Replace example files with testable examples
|
||||||
|
|
||||||
|
# 1.0.2
|
||||||
|
|
||||||
|
* bug: quote non-string values in text formatter (#583)
|
||||||
|
* Make (*Logger) SetLevel a public method
|
||||||
|
|
||||||
|
# 1.0.1
|
||||||
|
|
||||||
|
* bug: fix escaping in text formatter (#575)
|
||||||
|
|
||||||
|
# 1.0.0
|
||||||
|
|
||||||
|
* Officially changed name to lower-case
|
||||||
|
* bug: colors on Windows 10 (#541)
|
||||||
|
* bug: fix race in accessing level (#512)
|
||||||
|
|
||||||
|
# 0.11.5
|
||||||
|
|
||||||
|
* feature: add writer and writerlevel to entry (#372)
|
||||||
|
|
||||||
|
# 0.11.4
|
||||||
|
|
||||||
|
* bug: fix undefined variable on solaris (#493)
|
||||||
|
|
||||||
|
# 0.11.3
|
||||||
|
|
||||||
|
* formatter: configure quoting of empty values (#484)
|
||||||
|
* formatter: configure quoting character (default is `"`) (#484)
|
||||||
|
* bug: fix not importing io correctly in non-linux environments (#481)
|
||||||
|
|
||||||
|
# 0.11.2
|
||||||
|
|
||||||
|
* bug: fix windows terminal detection (#476)
|
||||||
|
|
||||||
|
# 0.11.1
|
||||||
|
|
||||||
|
* bug: fix tty detection with custom out (#471)
|
||||||
|
|
||||||
|
# 0.11.0
|
||||||
|
|
||||||
|
* performance: Use bufferpool to allocate (#370)
|
||||||
|
* terminal: terminal detection for app-engine (#343)
|
||||||
|
* feature: exit handler (#375)
|
||||||
|
|
||||||
|
# 0.10.0
|
||||||
|
|
||||||
|
* feature: Add a test hook (#180)
|
||||||
|
* feature: `ParseLevel` is now case-insensitive (#326)
|
||||||
|
* feature: `FieldLogger` interface that generalizes `Logger` and `Entry` (#308)
|
||||||
|
* performance: avoid re-allocations on `WithFields` (#335)
|
||||||
|
|
||||||
|
# 0.9.0
|
||||||
|
|
||||||
|
* logrus/text_formatter: don't emit empty msg
|
||||||
|
* logrus/hooks/airbrake: move out of main repository
|
||||||
|
* logrus/hooks/sentry: move out of main repository
|
||||||
|
* logrus/hooks/papertrail: move out of main repository
|
||||||
|
* logrus/hooks/bugsnag: move out of main repository
|
||||||
|
* logrus/core: run tests with `-race`
|
||||||
|
* logrus/core: detect TTY based on `stderr`
|
||||||
|
* logrus/core: support `WithError` on logger
|
||||||
|
* logrus/core: Solaris support
|
||||||
|
|
||||||
|
# 0.8.7
|
||||||
|
|
||||||
|
* logrus/core: fix possible race (#216)
|
||||||
|
* logrus/doc: small typo fixes and doc improvements
|
||||||
|
|
||||||
|
|
||||||
|
# 0.8.6
|
||||||
|
|
||||||
|
* hooks/raven: allow passing an initialized client
|
||||||
|
|
||||||
|
# 0.8.5
|
||||||
|
|
||||||
|
* logrus/core: revert #208
|
||||||
|
|
||||||
|
# 0.8.4
|
||||||
|
|
||||||
|
* formatter/text: fix data race (#218)
|
||||||
|
|
||||||
|
# 0.8.3
|
||||||
|
|
||||||
|
* logrus/core: fix entry log level (#208)
|
||||||
|
* logrus/core: improve performance of text formatter by 40%
|
||||||
|
* logrus/core: expose `LevelHooks` type
|
||||||
|
* logrus/core: add support for DragonflyBSD and NetBSD
|
||||||
|
* formatter/text: print structs more verbosely
|
||||||
|
|
||||||
|
# 0.8.2
|
||||||
|
|
||||||
|
* logrus: fix more Fatal family functions
|
||||||
|
|
||||||
|
# 0.8.1
|
||||||
|
|
||||||
|
* logrus: fix not exiting on `Fatalf` and `Fatalln`
|
||||||
|
|
||||||
|
# 0.8.0
|
||||||
|
|
||||||
|
* logrus: defaults to stderr instead of stdout
|
||||||
|
* hooks/sentry: add special field for `*http.Request`
|
||||||
|
* formatter/text: ignore Windows for colors
|
||||||
|
|
||||||
|
# 0.7.3
|
||||||
|
|
||||||
|
* formatter/\*: allow configuration of timestamp layout
|
||||||
|
|
||||||
|
# 0.7.2
|
||||||
|
|
||||||
|
* formatter/text: Add configuration option for time format (#158)
|
21
vendor/github.com/sirupsen/logrus/LICENSE
generated
vendored
Normal file
21
vendor/github.com/sirupsen/logrus/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2014 Simon Eskildsen
|
||||||
|
|
||||||
|
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.
|
515
vendor/github.com/sirupsen/logrus/README.md
generated
vendored
Normal file
515
vendor/github.com/sirupsen/logrus/README.md
generated
vendored
Normal file
|
@ -0,0 +1,515 @@
|
||||||
|
# Logrus <img src="http://i.imgur.com/hTeVwmJ.png" width="40" height="40" alt=":walrus:" class="emoji" title=":walrus:"/> [](https://github.com/sirupsen/logrus/actions?query=workflow%3ACI) [](https://travis-ci.org/sirupsen/logrus) [](https://pkg.go.dev/github.com/sirupsen/logrus)
|
||||||
|
|
||||||
|
Logrus is a structured logger for Go (golang), completely API compatible with
|
||||||
|
the standard library logger.
|
||||||
|
|
||||||
|
**Logrus is in maintenance-mode.** We will not be introducing new features. It's
|
||||||
|
simply too hard to do in a way that won't break many people's projects, which is
|
||||||
|
the last thing you want from your Logging library (again...).
|
||||||
|
|
||||||
|
This does not mean Logrus is dead. Logrus will continue to be maintained for
|
||||||
|
security, (backwards compatible) bug fixes, and performance (where we are
|
||||||
|
limited by the interface).
|
||||||
|
|
||||||
|
I believe Logrus' biggest contribution is to have played a part in today's
|
||||||
|
widespread use of structured logging in Golang. There doesn't seem to be a
|
||||||
|
reason to do a major, breaking iteration into Logrus V2, since the fantastic Go
|
||||||
|
community has built those independently. Many fantastic alternatives have sprung
|
||||||
|
up. Logrus would look like those, had it been re-designed with what we know
|
||||||
|
about structured logging in Go today. Check out, for example,
|
||||||
|
[Zerolog][zerolog], [Zap][zap], and [Apex][apex].
|
||||||
|
|
||||||
|
[zerolog]: https://github.com/rs/zerolog
|
||||||
|
[zap]: https://github.com/uber-go/zap
|
||||||
|
[apex]: https://github.com/apex/log
|
||||||
|
|
||||||
|
**Seeing weird case-sensitive problems?** It's in the past been possible to
|
||||||
|
import Logrus as both upper- and lower-case. Due to the Go package environment,
|
||||||
|
this caused issues in the community and we needed a standard. Some environments
|
||||||
|
experienced problems with the upper-case variant, so the lower-case was decided.
|
||||||
|
Everything using `logrus` will need to use the lower-case:
|
||||||
|
`github.com/sirupsen/logrus`. Any package that isn't, should be changed.
|
||||||
|
|
||||||
|
To fix Glide, see [these
|
||||||
|
comments](https://github.com/sirupsen/logrus/issues/553#issuecomment-306591437).
|
||||||
|
For an in-depth explanation of the casing issue, see [this
|
||||||
|
comment](https://github.com/sirupsen/logrus/issues/570#issuecomment-313933276).
|
||||||
|
|
||||||
|
Nicely color-coded in development (when a TTY is attached, otherwise just
|
||||||
|
plain text):
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
With `log.SetFormatter(&log.JSONFormatter{})`, for easy parsing by logstash
|
||||||
|
or Splunk:
|
||||||
|
|
||||||
|
```text
|
||||||
|
{"animal":"walrus","level":"info","msg":"A group of walrus emerges from the
|
||||||
|
ocean","size":10,"time":"2014-03-10 19:57:38.562264131 -0400 EDT"}
|
||||||
|
|
||||||
|
{"level":"warning","msg":"The group's number increased tremendously!",
|
||||||
|
"number":122,"omg":true,"time":"2014-03-10 19:57:38.562471297 -0400 EDT"}
|
||||||
|
|
||||||
|
{"animal":"walrus","level":"info","msg":"A giant walrus appears!",
|
||||||
|
"size":10,"time":"2014-03-10 19:57:38.562500591 -0400 EDT"}
|
||||||
|
|
||||||
|
{"animal":"walrus","level":"info","msg":"Tremendously sized cow enters the ocean.",
|
||||||
|
"size":9,"time":"2014-03-10 19:57:38.562527896 -0400 EDT"}
|
||||||
|
|
||||||
|
{"level":"fatal","msg":"The ice breaks!","number":100,"omg":true,
|
||||||
|
"time":"2014-03-10 19:57:38.562543128 -0400 EDT"}
|
||||||
|
```
|
||||||
|
|
||||||
|
With the default `log.SetFormatter(&log.TextFormatter{})` when a TTY is not
|
||||||
|
attached, the output is compatible with the
|
||||||
|
[logfmt](http://godoc.org/github.com/kr/logfmt) format:
|
||||||
|
|
||||||
|
```text
|
||||||
|
time="2015-03-26T01:27:38-04:00" level=debug msg="Started observing beach" animal=walrus number=8
|
||||||
|
time="2015-03-26T01:27:38-04:00" level=info msg="A group of walrus emerges from the ocean" animal=walrus size=10
|
||||||
|
time="2015-03-26T01:27:38-04:00" level=warning msg="The group's number increased tremendously!" number=122 omg=true
|
||||||
|
time="2015-03-26T01:27:38-04:00" level=debug msg="Temperature changes" temperature=-4
|
||||||
|
time="2015-03-26T01:27:38-04:00" level=panic msg="It's over 9000!" animal=orca size=9009
|
||||||
|
time="2015-03-26T01:27:38-04:00" level=fatal msg="The ice breaks!" err=&{0x2082280c0 map[animal:orca size:9009] 2015-03-26 01:27:38.441574009 -0400 EDT panic It's over 9000!} number=100 omg=true
|
||||||
|
```
|
||||||
|
To ensure this behaviour even if a TTY is attached, set your formatter as follows:
|
||||||
|
|
||||||
|
```go
|
||||||
|
log.SetFormatter(&log.TextFormatter{
|
||||||
|
DisableColors: true,
|
||||||
|
FullTimestamp: true,
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Logging Method Name
|
||||||
|
|
||||||
|
If you wish to add the calling method as a field, instruct the logger via:
|
||||||
|
```go
|
||||||
|
log.SetReportCaller(true)
|
||||||
|
```
|
||||||
|
This adds the caller as 'method' like so:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"animal":"penguin","level":"fatal","method":"github.com/sirupsen/arcticcreatures.migrate","msg":"a penguin swims by",
|
||||||
|
"time":"2014-03-10 19:57:38.562543129 -0400 EDT"}
|
||||||
|
```
|
||||||
|
|
||||||
|
```text
|
||||||
|
time="2015-03-26T01:27:38-04:00" level=fatal method=github.com/sirupsen/arcticcreatures.migrate msg="a penguin swims by" animal=penguin
|
||||||
|
```
|
||||||
|
Note that this does add measurable overhead - the cost will depend on the version of Go, but is
|
||||||
|
between 20 and 40% in recent tests with 1.6 and 1.7. You can validate this in your
|
||||||
|
environment via benchmarks:
|
||||||
|
```
|
||||||
|
go test -bench=.*CallerTracing
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
#### Case-sensitivity
|
||||||
|
|
||||||
|
The organization's name was changed to lower-case--and this will not be changed
|
||||||
|
back. If you are getting import conflicts due to case sensitivity, please use
|
||||||
|
the lower-case import: `github.com/sirupsen/logrus`.
|
||||||
|
|
||||||
|
#### Example
|
||||||
|
|
||||||
|
The simplest way to use Logrus is simply the package-level exported logger:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"animal": "walrus",
|
||||||
|
}).Info("A walrus appears")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that it's completely api-compatible with the stdlib logger, so you can
|
||||||
|
replace your `log` imports everywhere with `log "github.com/sirupsen/logrus"`
|
||||||
|
and you'll now have the flexibility of Logrus. You can customize it all you
|
||||||
|
want:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// Log as JSON instead of the default ASCII formatter.
|
||||||
|
log.SetFormatter(&log.JSONFormatter{})
|
||||||
|
|
||||||
|
// Output to stdout instead of the default stderr
|
||||||
|
// Can be any io.Writer, see below for File example
|
||||||
|
log.SetOutput(os.Stdout)
|
||||||
|
|
||||||
|
// Only log the warning severity or above.
|
||||||
|
log.SetLevel(log.WarnLevel)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"animal": "walrus",
|
||||||
|
"size": 10,
|
||||||
|
}).Info("A group of walrus emerges from the ocean")
|
||||||
|
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"omg": true,
|
||||||
|
"number": 122,
|
||||||
|
}).Warn("The group's number increased tremendously!")
|
||||||
|
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"omg": true,
|
||||||
|
"number": 100,
|
||||||
|
}).Fatal("The ice breaks!")
|
||||||
|
|
||||||
|
// A common pattern is to re-use fields between logging statements by re-using
|
||||||
|
// the logrus.Entry returned from WithFields()
|
||||||
|
contextLogger := log.WithFields(log.Fields{
|
||||||
|
"common": "this is a common field",
|
||||||
|
"other": "I also should be logged always",
|
||||||
|
})
|
||||||
|
|
||||||
|
contextLogger.Info("I'll be logged with common and other field")
|
||||||
|
contextLogger.Info("Me too")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
For more advanced usage such as logging to multiple locations from the same
|
||||||
|
application, you can also create an instance of the `logrus` Logger:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Create a new instance of the logger. You can have any number of instances.
|
||||||
|
var log = logrus.New()
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// The API for setting attributes is a little different than the package level
|
||||||
|
// exported logger. See Godoc.
|
||||||
|
log.Out = os.Stdout
|
||||||
|
|
||||||
|
// You could set this to any `io.Writer` such as a file
|
||||||
|
// file, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||||
|
// if err == nil {
|
||||||
|
// log.Out = file
|
||||||
|
// } else {
|
||||||
|
// log.Info("Failed to log to file, using default stderr")
|
||||||
|
// }
|
||||||
|
|
||||||
|
log.WithFields(logrus.Fields{
|
||||||
|
"animal": "walrus",
|
||||||
|
"size": 10,
|
||||||
|
}).Info("A group of walrus emerges from the ocean")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Fields
|
||||||
|
|
||||||
|
Logrus encourages careful, structured logging through logging fields instead of
|
||||||
|
long, unparseable error messages. For example, instead of: `log.Fatalf("Failed
|
||||||
|
to send event %s to topic %s with key %d")`, you should log the much more
|
||||||
|
discoverable:
|
||||||
|
|
||||||
|
```go
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"event": event,
|
||||||
|
"topic": topic,
|
||||||
|
"key": key,
|
||||||
|
}).Fatal("Failed to send event")
|
||||||
|
```
|
||||||
|
|
||||||
|
We've found this API forces you to think about logging in a way that produces
|
||||||
|
much more useful logging messages. We've been in countless situations where just
|
||||||
|
a single added field to a log statement that was already there would've saved us
|
||||||
|
hours. The `WithFields` call is optional.
|
||||||
|
|
||||||
|
In general, with Logrus using any of the `printf`-family functions should be
|
||||||
|
seen as a hint you should add a field, however, you can still use the
|
||||||
|
`printf`-family functions with Logrus.
|
||||||
|
|
||||||
|
#### Default Fields
|
||||||
|
|
||||||
|
Often it's helpful to have fields _always_ attached to log statements in an
|
||||||
|
application or parts of one. For example, you may want to always log the
|
||||||
|
`request_id` and `user_ip` in the context of a request. Instead of writing
|
||||||
|
`log.WithFields(log.Fields{"request_id": request_id, "user_ip": user_ip})` on
|
||||||
|
every line, you can create a `logrus.Entry` to pass around instead:
|
||||||
|
|
||||||
|
```go
|
||||||
|
requestLogger := log.WithFields(log.Fields{"request_id": request_id, "user_ip": user_ip})
|
||||||
|
requestLogger.Info("something happened on that request") # will log request_id and user_ip
|
||||||
|
requestLogger.Warn("something not great happened")
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Hooks
|
||||||
|
|
||||||
|
You can add hooks for logging levels. For example to send errors to an exception
|
||||||
|
tracking service on `Error`, `Fatal` and `Panic`, info to StatsD or log to
|
||||||
|
multiple places simultaneously, e.g. syslog.
|
||||||
|
|
||||||
|
Logrus comes with [built-in hooks](hooks/). Add those, or your custom hook, in
|
||||||
|
`init`:
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"gopkg.in/gemnasium/logrus-airbrake-hook.v2" // the package is named "airbrake"
|
||||||
|
logrus_syslog "github.com/sirupsen/logrus/hooks/syslog"
|
||||||
|
"log/syslog"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
|
||||||
|
// Use the Airbrake hook to report errors that have Error severity or above to
|
||||||
|
// an exception tracker. You can create custom hooks, see the Hooks section.
|
||||||
|
log.AddHook(airbrake.NewHook(123, "xyz", "production"))
|
||||||
|
|
||||||
|
hook, err := logrus_syslog.NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "")
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Unable to connect to local syslog daemon")
|
||||||
|
} else {
|
||||||
|
log.AddHook(hook)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
Note: Syslog hook also support connecting to local syslog (Ex. "/dev/log" or "/var/run/syslog" or "/var/run/log"). For the detail, please check the [syslog hook README](hooks/syslog/README.md).
|
||||||
|
|
||||||
|
A list of currently known service hooks can be found in this wiki [page](https://github.com/sirupsen/logrus/wiki/Hooks)
|
||||||
|
|
||||||
|
|
||||||
|
#### Level logging
|
||||||
|
|
||||||
|
Logrus has seven logging levels: Trace, Debug, Info, Warning, Error, Fatal and Panic.
|
||||||
|
|
||||||
|
```go
|
||||||
|
log.Trace("Something very low level.")
|
||||||
|
log.Debug("Useful debugging information.")
|
||||||
|
log.Info("Something noteworthy happened!")
|
||||||
|
log.Warn("You should probably take a look at this.")
|
||||||
|
log.Error("Something failed but I'm not quitting.")
|
||||||
|
// Calls os.Exit(1) after logging
|
||||||
|
log.Fatal("Bye.")
|
||||||
|
// Calls panic() after logging
|
||||||
|
log.Panic("I'm bailing.")
|
||||||
|
```
|
||||||
|
|
||||||
|
You can set the logging level on a `Logger`, then it will only log entries with
|
||||||
|
that severity or anything above it:
|
||||||
|
|
||||||
|
```go
|
||||||
|
// Will log anything that is info or above (warn, error, fatal, panic). Default.
|
||||||
|
log.SetLevel(log.InfoLevel)
|
||||||
|
```
|
||||||
|
|
||||||
|
It may be useful to set `log.Level = logrus.DebugLevel` in a debug or verbose
|
||||||
|
environment if your application has that.
|
||||||
|
|
||||||
|
Note: If you want different log levels for global (`log.SetLevel(...)`) and syslog logging, please check the [syslog hook README](hooks/syslog/README.md#different-log-levels-for-local-and-remote-logging).
|
||||||
|
|
||||||
|
#### Entries
|
||||||
|
|
||||||
|
Besides the fields added with `WithField` or `WithFields` some fields are
|
||||||
|
automatically added to all logging events:
|
||||||
|
|
||||||
|
1. `time`. The timestamp when the entry was created.
|
||||||
|
2. `msg`. The logging message passed to `{Info,Warn,Error,Fatal,Panic}` after
|
||||||
|
the `AddFields` call. E.g. `Failed to send event.`
|
||||||
|
3. `level`. The logging level. E.g. `info`.
|
||||||
|
|
||||||
|
#### Environments
|
||||||
|
|
||||||
|
Logrus has no notion of environment.
|
||||||
|
|
||||||
|
If you wish for hooks and formatters to only be used in specific environments,
|
||||||
|
you should handle that yourself. For example, if your application has a global
|
||||||
|
variable `Environment`, which is a string representation of the environment you
|
||||||
|
could do:
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// do something here to set environment depending on an environment variable
|
||||||
|
// or command-line flag
|
||||||
|
if Environment == "production" {
|
||||||
|
log.SetFormatter(&log.JSONFormatter{})
|
||||||
|
} else {
|
||||||
|
// The TextFormatter is default, you don't actually have to do this.
|
||||||
|
log.SetFormatter(&log.TextFormatter{})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
This configuration is how `logrus` was intended to be used, but JSON in
|
||||||
|
production is mostly only useful if you do log aggregation with tools like
|
||||||
|
Splunk or Logstash.
|
||||||
|
|
||||||
|
#### Formatters
|
||||||
|
|
||||||
|
The built-in logging formatters are:
|
||||||
|
|
||||||
|
* `logrus.TextFormatter`. Logs the event in colors if stdout is a tty, otherwise
|
||||||
|
without colors.
|
||||||
|
* *Note:* to force colored output when there is no TTY, set the `ForceColors`
|
||||||
|
field to `true`. To force no colored output even if there is a TTY set the
|
||||||
|
`DisableColors` field to `true`. For Windows, see
|
||||||
|
[github.com/mattn/go-colorable](https://github.com/mattn/go-colorable).
|
||||||
|
* When colors are enabled, levels are truncated to 4 characters by default. To disable
|
||||||
|
truncation set the `DisableLevelTruncation` field to `true`.
|
||||||
|
* When outputting to a TTY, it's often helpful to visually scan down a column where all the levels are the same width. Setting the `PadLevelText` field to `true` enables this behavior, by adding padding to the level text.
|
||||||
|
* All options are listed in the [generated docs](https://godoc.org/github.com/sirupsen/logrus#TextFormatter).
|
||||||
|
* `logrus.JSONFormatter`. Logs fields as JSON.
|
||||||
|
* All options are listed in the [generated docs](https://godoc.org/github.com/sirupsen/logrus#JSONFormatter).
|
||||||
|
|
||||||
|
Third party logging formatters:
|
||||||
|
|
||||||
|
* [`FluentdFormatter`](https://github.com/joonix/log). Formats entries that can be parsed by Kubernetes and Google Container Engine.
|
||||||
|
* [`GELF`](https://github.com/fabienm/go-logrus-formatters). Formats entries so they comply to Graylog's [GELF 1.1 specification](http://docs.graylog.org/en/2.4/pages/gelf.html).
|
||||||
|
* [`logstash`](https://github.com/bshuster-repo/logrus-logstash-hook). Logs fields as [Logstash](http://logstash.net) Events.
|
||||||
|
* [`prefixed`](https://github.com/x-cray/logrus-prefixed-formatter). Displays log entry source along with alternative layout.
|
||||||
|
* [`zalgo`](https://github.com/aybabtme/logzalgo). Invoking the Power of Zalgo.
|
||||||
|
* [`nested-logrus-formatter`](https://github.com/antonfisher/nested-logrus-formatter). Converts logrus fields to a nested structure.
|
||||||
|
* [`powerful-logrus-formatter`](https://github.com/zput/zxcTool). get fileName, log's line number and the latest function's name when print log; Sava log to files.
|
||||||
|
* [`caption-json-formatter`](https://github.com/nolleh/caption_json_formatter). logrus's message json formatter with human-readable caption added.
|
||||||
|
|
||||||
|
You can define your formatter by implementing the `Formatter` interface,
|
||||||
|
requiring a `Format` method. `Format` takes an `*Entry`. `entry.Data` is a
|
||||||
|
`Fields` type (`map[string]interface{}`) with all your fields as well as the
|
||||||
|
default ones (see Entries section above):
|
||||||
|
|
||||||
|
```go
|
||||||
|
type MyJSONFormatter struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
log.SetFormatter(new(MyJSONFormatter))
|
||||||
|
|
||||||
|
func (f *MyJSONFormatter) Format(entry *Entry) ([]byte, error) {
|
||||||
|
// Note this doesn't include Time, Level and Message which are available on
|
||||||
|
// the Entry. Consult `godoc` on information about those fields or read the
|
||||||
|
// source of the official loggers.
|
||||||
|
serialized, err := json.Marshal(entry.Data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Failed to marshal fields to JSON, %w", err)
|
||||||
|
}
|
||||||
|
return append(serialized, '\n'), nil
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Logger as an `io.Writer`
|
||||||
|
|
||||||
|
Logrus can be transformed into an `io.Writer`. That writer is the end of an `io.Pipe` and it is your responsibility to close it.
|
||||||
|
|
||||||
|
```go
|
||||||
|
w := logger.Writer()
|
||||||
|
defer w.Close()
|
||||||
|
|
||||||
|
srv := http.Server{
|
||||||
|
// create a stdlib log.Logger that writes to
|
||||||
|
// logrus.Logger.
|
||||||
|
ErrorLog: log.New(w, "", 0),
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Each line written to that writer will be printed the usual way, using formatters
|
||||||
|
and hooks. The level for those entries is `info`.
|
||||||
|
|
||||||
|
This means that we can override the standard library logger easily:
|
||||||
|
|
||||||
|
```go
|
||||||
|
logger := logrus.New()
|
||||||
|
logger.Formatter = &logrus.JSONFormatter{}
|
||||||
|
|
||||||
|
// Use logrus for standard log output
|
||||||
|
// Note that `log` here references stdlib's log
|
||||||
|
// Not logrus imported under the name `log`.
|
||||||
|
log.SetOutput(logger.Writer())
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Rotation
|
||||||
|
|
||||||
|
Log rotation is not provided with Logrus. Log rotation should be done by an
|
||||||
|
external program (like `logrotate(8)`) that can compress and delete old log
|
||||||
|
entries. It should not be a feature of the application-level logger.
|
||||||
|
|
||||||
|
#### Tools
|
||||||
|
|
||||||
|
| Tool | Description |
|
||||||
|
| ---- | ----------- |
|
||||||
|
|[Logrus Mate](https://github.com/gogap/logrus_mate)|Logrus mate is a tool for Logrus to manage loggers, you can initial logger's level, hook and formatter by config file, the logger will be generated with different configs in different environments.|
|
||||||
|
|[Logrus Viper Helper](https://github.com/heirko/go-contrib/tree/master/logrusHelper)|An Helper around Logrus to wrap with spf13/Viper to load configuration with fangs! And to simplify Logrus configuration use some behavior of [Logrus Mate](https://github.com/gogap/logrus_mate). [sample](https://github.com/heirko/iris-contrib/blob/master/middleware/logrus-logger/example) |
|
||||||
|
|
||||||
|
#### Testing
|
||||||
|
|
||||||
|
Logrus has a built in facility for asserting the presence of log messages. This is implemented through the `test` hook and provides:
|
||||||
|
|
||||||
|
* decorators for existing logger (`test.NewLocal` and `test.NewGlobal`) which basically just adds the `test` hook
|
||||||
|
* a test logger (`test.NewNullLogger`) that just records log messages (and does not output any):
|
||||||
|
|
||||||
|
```go
|
||||||
|
import(
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"github.com/sirupsen/logrus/hooks/test"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSomething(t*testing.T){
|
||||||
|
logger, hook := test.NewNullLogger()
|
||||||
|
logger.Error("Helloerror")
|
||||||
|
|
||||||
|
assert.Equal(t, 1, len(hook.Entries))
|
||||||
|
assert.Equal(t, logrus.ErrorLevel, hook.LastEntry().Level)
|
||||||
|
assert.Equal(t, "Helloerror", hook.LastEntry().Message)
|
||||||
|
|
||||||
|
hook.Reset()
|
||||||
|
assert.Nil(t, hook.LastEntry())
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Fatal handlers
|
||||||
|
|
||||||
|
Logrus can register one or more functions that will be called when any `fatal`
|
||||||
|
level message is logged. The registered handlers will be executed before
|
||||||
|
logrus performs an `os.Exit(1)`. This behavior may be helpful if callers need
|
||||||
|
to gracefully shutdown. Unlike a `panic("Something went wrong...")` call which can be intercepted with a deferred `recover` a call to `os.Exit(1)` can not be intercepted.
|
||||||
|
|
||||||
|
```
|
||||||
|
...
|
||||||
|
handler := func() {
|
||||||
|
// gracefully shutdown something...
|
||||||
|
}
|
||||||
|
logrus.RegisterExitHandler(handler)
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Thread safety
|
||||||
|
|
||||||
|
By default, Logger is protected by a mutex for concurrent writes. The mutex is held when calling hooks and writing logs.
|
||||||
|
If you are sure such locking is not needed, you can call logger.SetNoLock() to disable the locking.
|
||||||
|
|
||||||
|
Situation when locking is not needed includes:
|
||||||
|
|
||||||
|
* You have no hooks registered, or hooks calling is already thread-safe.
|
||||||
|
|
||||||
|
* Writing to logger.Out is already thread-safe, for example:
|
||||||
|
|
||||||
|
1) logger.Out is protected by locks.
|
||||||
|
|
||||||
|
2) logger.Out is an os.File handler opened with `O_APPEND` flag, and every write is smaller than 4k. (This allows multi-thread/multi-process writing)
|
||||||
|
|
||||||
|
(Refer to http://www.notthewizard.com/2014/06/17/are-files-appends-really-atomic/)
|
76
vendor/github.com/sirupsen/logrus/alt_exit.go
generated
vendored
Normal file
76
vendor/github.com/sirupsen/logrus/alt_exit.go
generated
vendored
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
package logrus
|
||||||
|
|
||||||
|
// The following code was sourced and modified from the
|
||||||
|
// https://github.com/tebeka/atexit package governed by the following license:
|
||||||
|
//
|
||||||
|
// Copyright (c) 2012 Miki Tebeka <miki.tebeka@gmail.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.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
var handlers = []func(){}
|
||||||
|
|
||||||
|
func runHandler(handler func()) {
|
||||||
|
defer func() {
|
||||||
|
if err := recover(); err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, "Error: Logrus exit handler error:", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
handler()
|
||||||
|
}
|
||||||
|
|
||||||
|
func runHandlers() {
|
||||||
|
for _, handler := range handlers {
|
||||||
|
runHandler(handler)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exit runs all the Logrus atexit handlers and then terminates the program using os.Exit(code)
|
||||||
|
func Exit(code int) {
|
||||||
|
runHandlers()
|
||||||
|
os.Exit(code)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RegisterExitHandler appends a Logrus Exit handler to the list of handlers,
|
||||||
|
// call logrus.Exit to invoke all handlers. The handlers will also be invoked when
|
||||||
|
// any Fatal log entry is made.
|
||||||
|
//
|
||||||
|
// This method is useful when a caller wishes to use logrus to log a fatal
|
||||||
|
// message but also needs to gracefully shutdown. An example usecase could be
|
||||||
|
// closing database connections, or sending a alert that the application is
|
||||||
|
// closing.
|
||||||
|
func RegisterExitHandler(handler func()) {
|
||||||
|
handlers = append(handlers, handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeferExitHandler prepends a Logrus Exit handler to the list of handlers,
|
||||||
|
// call logrus.Exit to invoke all handlers. The handlers will also be invoked when
|
||||||
|
// any Fatal log entry is made.
|
||||||
|
//
|
||||||
|
// This method is useful when a caller wishes to use logrus to log a fatal
|
||||||
|
// message but also needs to gracefully shutdown. An example usecase could be
|
||||||
|
// closing database connections, or sending a alert that the application is
|
||||||
|
// closing.
|
||||||
|
func DeferExitHandler(handler func()) {
|
||||||
|
handlers = append([]func(){handler}, handlers...)
|
||||||
|
}
|
14
vendor/github.com/sirupsen/logrus/appveyor.yml
generated
vendored
Normal file
14
vendor/github.com/sirupsen/logrus/appveyor.yml
generated
vendored
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
version: "{build}"
|
||||||
|
platform: x64
|
||||||
|
clone_folder: c:\gopath\src\github.com\sirupsen\logrus
|
||||||
|
environment:
|
||||||
|
GOPATH: c:\gopath
|
||||||
|
branches:
|
||||||
|
only:
|
||||||
|
- master
|
||||||
|
install:
|
||||||
|
- set PATH=%GOPATH%\bin;c:\go\bin;%PATH%
|
||||||
|
- go version
|
||||||
|
build_script:
|
||||||
|
- go get -t
|
||||||
|
- go test
|
43
vendor/github.com/sirupsen/logrus/buffer_pool.go
generated
vendored
Normal file
43
vendor/github.com/sirupsen/logrus/buffer_pool.go
generated
vendored
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
package logrus
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
bufferPool BufferPool
|
||||||
|
)
|
||||||
|
|
||||||
|
type BufferPool interface {
|
||||||
|
Put(*bytes.Buffer)
|
||||||
|
Get() *bytes.Buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
type defaultPool struct {
|
||||||
|
pool *sync.Pool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *defaultPool) Put(buf *bytes.Buffer) {
|
||||||
|
p.pool.Put(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *defaultPool) Get() *bytes.Buffer {
|
||||||
|
return p.pool.Get().(*bytes.Buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetBufferPool allows to replace the default logrus buffer pool
|
||||||
|
// to better meets the specific needs of an application.
|
||||||
|
func SetBufferPool(bp BufferPool) {
|
||||||
|
bufferPool = bp
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
SetBufferPool(&defaultPool{
|
||||||
|
pool: &sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
return new(bytes.Buffer)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
26
vendor/github.com/sirupsen/logrus/doc.go
generated
vendored
Normal file
26
vendor/github.com/sirupsen/logrus/doc.go
generated
vendored
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
/*
|
||||||
|
Package logrus is a structured logger for Go, completely API compatible with the standard library logger.
|
||||||
|
|
||||||
|
|
||||||
|
The simplest way to use Logrus is simply the package-level exported logger:
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"animal": "walrus",
|
||||||
|
"number": 1,
|
||||||
|
"size": 10,
|
||||||
|
}).Info("A walrus appears")
|
||||||
|
}
|
||||||
|
|
||||||
|
Output:
|
||||||
|
time="2015-09-07T08:48:33Z" level=info msg="A walrus appears" animal=walrus number=1 size=10
|
||||||
|
|
||||||
|
For a full guide visit https://github.com/sirupsen/logrus
|
||||||
|
*/
|
||||||
|
package logrus
|
442
vendor/github.com/sirupsen/logrus/entry.go
generated
vendored
Normal file
442
vendor/github.com/sirupsen/logrus/entry.go
generated
vendored
Normal file
|
@ -0,0 +1,442 @@
|
||||||
|
package logrus
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"reflect"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
|
||||||
|
// qualified package name, cached at first use
|
||||||
|
logrusPackage string
|
||||||
|
|
||||||
|
// Positions in the call stack when tracing to report the calling method
|
||||||
|
minimumCallerDepth int
|
||||||
|
|
||||||
|
// Used for caller information initialisation
|
||||||
|
callerInitOnce sync.Once
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
maximumCallerDepth int = 25
|
||||||
|
knownLogrusFrames int = 4
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// start at the bottom of the stack before the package-name cache is primed
|
||||||
|
minimumCallerDepth = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Defines the key when adding errors using WithError.
|
||||||
|
var ErrorKey = "error"
|
||||||
|
|
||||||
|
// An entry is the final or intermediate Logrus logging entry. It contains all
|
||||||
|
// the fields passed with WithField{,s}. It's finally logged when Trace, Debug,
|
||||||
|
// Info, Warn, Error, Fatal or Panic is called on it. These objects can be
|
||||||
|
// reused and passed around as much as you wish to avoid field duplication.
|
||||||
|
type Entry struct {
|
||||||
|
Logger *Logger
|
||||||
|
|
||||||
|
// Contains all the fields set by the user.
|
||||||
|
Data Fields
|
||||||
|
|
||||||
|
// Time at which the log entry was created
|
||||||
|
Time time.Time
|
||||||
|
|
||||||
|
// Level the log entry was logged at: Trace, Debug, Info, Warn, Error, Fatal or Panic
|
||||||
|
// This field will be set on entry firing and the value will be equal to the one in Logger struct field.
|
||||||
|
Level Level
|
||||||
|
|
||||||
|
// Calling method, with package name
|
||||||
|
Caller *runtime.Frame
|
||||||
|
|
||||||
|
// Message passed to Trace, Debug, Info, Warn, Error, Fatal or Panic
|
||||||
|
Message string
|
||||||
|
|
||||||
|
// When formatter is called in entry.log(), a Buffer may be set to entry
|
||||||
|
Buffer *bytes.Buffer
|
||||||
|
|
||||||
|
// Contains the context set by the user. Useful for hook processing etc.
|
||||||
|
Context context.Context
|
||||||
|
|
||||||
|
// err may contain a field formatting error
|
||||||
|
err string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEntry(logger *Logger) *Entry {
|
||||||
|
return &Entry{
|
||||||
|
Logger: logger,
|
||||||
|
// Default is three fields, plus one optional. Give a little extra room.
|
||||||
|
Data: make(Fields, 6),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Dup() *Entry {
|
||||||
|
data := make(Fields, len(entry.Data))
|
||||||
|
for k, v := range entry.Data {
|
||||||
|
data[k] = v
|
||||||
|
}
|
||||||
|
return &Entry{Logger: entry.Logger, Data: data, Time: entry.Time, Context: entry.Context, err: entry.err}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the bytes representation of this entry from the formatter.
|
||||||
|
func (entry *Entry) Bytes() ([]byte, error) {
|
||||||
|
return entry.Logger.Formatter.Format(entry)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the string representation from the reader and ultimately the
|
||||||
|
// formatter.
|
||||||
|
func (entry *Entry) String() (string, error) {
|
||||||
|
serialized, err := entry.Bytes()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
str := string(serialized)
|
||||||
|
return str, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add an error as single field (using the key defined in ErrorKey) to the Entry.
|
||||||
|
func (entry *Entry) WithError(err error) *Entry {
|
||||||
|
return entry.WithField(ErrorKey, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add a context to the Entry.
|
||||||
|
func (entry *Entry) WithContext(ctx context.Context) *Entry {
|
||||||
|
dataCopy := make(Fields, len(entry.Data))
|
||||||
|
for k, v := range entry.Data {
|
||||||
|
dataCopy[k] = v
|
||||||
|
}
|
||||||
|
return &Entry{Logger: entry.Logger, Data: dataCopy, Time: entry.Time, err: entry.err, Context: ctx}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add a single field to the Entry.
|
||||||
|
func (entry *Entry) WithField(key string, value interface{}) *Entry {
|
||||||
|
return entry.WithFields(Fields{key: value})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add a map of fields to the Entry.
|
||||||
|
func (entry *Entry) WithFields(fields Fields) *Entry {
|
||||||
|
data := make(Fields, len(entry.Data)+len(fields))
|
||||||
|
for k, v := range entry.Data {
|
||||||
|
data[k] = v
|
||||||
|
}
|
||||||
|
fieldErr := entry.err
|
||||||
|
for k, v := range fields {
|
||||||
|
isErrField := false
|
||||||
|
if t := reflect.TypeOf(v); t != nil {
|
||||||
|
switch {
|
||||||
|
case t.Kind() == reflect.Func, t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Func:
|
||||||
|
isErrField = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if isErrField {
|
||||||
|
tmp := fmt.Sprintf("can not add field %q", k)
|
||||||
|
if fieldErr != "" {
|
||||||
|
fieldErr = entry.err + ", " + tmp
|
||||||
|
} else {
|
||||||
|
fieldErr = tmp
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
data[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &Entry{Logger: entry.Logger, Data: data, Time: entry.Time, err: fieldErr, Context: entry.Context}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Overrides the time of the Entry.
|
||||||
|
func (entry *Entry) WithTime(t time.Time) *Entry {
|
||||||
|
dataCopy := make(Fields, len(entry.Data))
|
||||||
|
for k, v := range entry.Data {
|
||||||
|
dataCopy[k] = v
|
||||||
|
}
|
||||||
|
return &Entry{Logger: entry.Logger, Data: dataCopy, Time: t, err: entry.err, Context: entry.Context}
|
||||||
|
}
|
||||||
|
|
||||||
|
// getPackageName reduces a fully qualified function name to the package name
|
||||||
|
// There really ought to be to be a better way...
|
||||||
|
func getPackageName(f string) string {
|
||||||
|
for {
|
||||||
|
lastPeriod := strings.LastIndex(f, ".")
|
||||||
|
lastSlash := strings.LastIndex(f, "/")
|
||||||
|
if lastPeriod > lastSlash {
|
||||||
|
f = f[:lastPeriod]
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
// getCaller retrieves the name of the first non-logrus calling function
|
||||||
|
func getCaller() *runtime.Frame {
|
||||||
|
// cache this package's fully-qualified name
|
||||||
|
callerInitOnce.Do(func() {
|
||||||
|
pcs := make([]uintptr, maximumCallerDepth)
|
||||||
|
_ = runtime.Callers(0, pcs)
|
||||||
|
|
||||||
|
// dynamic get the package name and the minimum caller depth
|
||||||
|
for i := 0; i < maximumCallerDepth; i++ {
|
||||||
|
funcName := runtime.FuncForPC(pcs[i]).Name()
|
||||||
|
if strings.Contains(funcName, "getCaller") {
|
||||||
|
logrusPackage = getPackageName(funcName)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
minimumCallerDepth = knownLogrusFrames
|
||||||
|
})
|
||||||
|
|
||||||
|
// Restrict the lookback frames to avoid runaway lookups
|
||||||
|
pcs := make([]uintptr, maximumCallerDepth)
|
||||||
|
depth := runtime.Callers(minimumCallerDepth, pcs)
|
||||||
|
frames := runtime.CallersFrames(pcs[:depth])
|
||||||
|
|
||||||
|
for f, again := frames.Next(); again; f, again = frames.Next() {
|
||||||
|
pkg := getPackageName(f.Function)
|
||||||
|
|
||||||
|
// If the caller isn't part of this package, we're done
|
||||||
|
if pkg != logrusPackage {
|
||||||
|
return &f //nolint:scopelint
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if we got here, we failed to find the caller's context
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry Entry) HasCaller() (has bool) {
|
||||||
|
return entry.Logger != nil &&
|
||||||
|
entry.Logger.ReportCaller &&
|
||||||
|
entry.Caller != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) log(level Level, msg string) {
|
||||||
|
var buffer *bytes.Buffer
|
||||||
|
|
||||||
|
newEntry := entry.Dup()
|
||||||
|
|
||||||
|
if newEntry.Time.IsZero() {
|
||||||
|
newEntry.Time = time.Now()
|
||||||
|
}
|
||||||
|
|
||||||
|
newEntry.Level = level
|
||||||
|
newEntry.Message = msg
|
||||||
|
|
||||||
|
newEntry.Logger.mu.Lock()
|
||||||
|
reportCaller := newEntry.Logger.ReportCaller
|
||||||
|
bufPool := newEntry.getBufferPool()
|
||||||
|
newEntry.Logger.mu.Unlock()
|
||||||
|
|
||||||
|
if reportCaller {
|
||||||
|
newEntry.Caller = getCaller()
|
||||||
|
}
|
||||||
|
|
||||||
|
newEntry.fireHooks()
|
||||||
|
buffer = bufPool.Get()
|
||||||
|
defer func() {
|
||||||
|
newEntry.Buffer = nil
|
||||||
|
buffer.Reset()
|
||||||
|
bufPool.Put(buffer)
|
||||||
|
}()
|
||||||
|
buffer.Reset()
|
||||||
|
newEntry.Buffer = buffer
|
||||||
|
|
||||||
|
newEntry.write()
|
||||||
|
|
||||||
|
newEntry.Buffer = nil
|
||||||
|
|
||||||
|
// To avoid Entry#log() returning a value that only would make sense for
|
||||||
|
// panic() to use in Entry#Panic(), we avoid the allocation by checking
|
||||||
|
// directly here.
|
||||||
|
if level <= PanicLevel {
|
||||||
|
panic(newEntry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) getBufferPool() (pool BufferPool) {
|
||||||
|
if entry.Logger.BufferPool != nil {
|
||||||
|
return entry.Logger.BufferPool
|
||||||
|
}
|
||||||
|
return bufferPool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) fireHooks() {
|
||||||
|
var tmpHooks LevelHooks
|
||||||
|
entry.Logger.mu.Lock()
|
||||||
|
tmpHooks = make(LevelHooks, len(entry.Logger.Hooks))
|
||||||
|
for k, v := range entry.Logger.Hooks {
|
||||||
|
tmpHooks[k] = v
|
||||||
|
}
|
||||||
|
entry.Logger.mu.Unlock()
|
||||||
|
|
||||||
|
err := tmpHooks.Fire(entry.Level, entry)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Failed to fire hook: %v\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) write() {
|
||||||
|
entry.Logger.mu.Lock()
|
||||||
|
defer entry.Logger.mu.Unlock()
|
||||||
|
serialized, err := entry.Logger.Formatter.Format(entry)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Failed to obtain reader, %v\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, err := entry.Logger.Out.Write(serialized); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Failed to write to log, %v\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log will log a message at the level given as parameter.
|
||||||
|
// Warning: using Log at Panic or Fatal level will not respectively Panic nor Exit.
|
||||||
|
// For this behaviour Entry.Panic or Entry.Fatal should be used instead.
|
||||||
|
func (entry *Entry) Log(level Level, args ...interface{}) {
|
||||||
|
if entry.Logger.IsLevelEnabled(level) {
|
||||||
|
entry.log(level, fmt.Sprint(args...))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Trace(args ...interface{}) {
|
||||||
|
entry.Log(TraceLevel, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Debug(args ...interface{}) {
|
||||||
|
entry.Log(DebugLevel, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Print(args ...interface{}) {
|
||||||
|
entry.Info(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Info(args ...interface{}) {
|
||||||
|
entry.Log(InfoLevel, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Warn(args ...interface{}) {
|
||||||
|
entry.Log(WarnLevel, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Warning(args ...interface{}) {
|
||||||
|
entry.Warn(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Error(args ...interface{}) {
|
||||||
|
entry.Log(ErrorLevel, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Fatal(args ...interface{}) {
|
||||||
|
entry.Log(FatalLevel, args...)
|
||||||
|
entry.Logger.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Panic(args ...interface{}) {
|
||||||
|
entry.Log(PanicLevel, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Entry Printf family functions
|
||||||
|
|
||||||
|
func (entry *Entry) Logf(level Level, format string, args ...interface{}) {
|
||||||
|
if entry.Logger.IsLevelEnabled(level) {
|
||||||
|
entry.Log(level, fmt.Sprintf(format, args...))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Tracef(format string, args ...interface{}) {
|
||||||
|
entry.Logf(TraceLevel, format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Debugf(format string, args ...interface{}) {
|
||||||
|
entry.Logf(DebugLevel, format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Infof(format string, args ...interface{}) {
|
||||||
|
entry.Logf(InfoLevel, format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Printf(format string, args ...interface{}) {
|
||||||
|
entry.Infof(format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Warnf(format string, args ...interface{}) {
|
||||||
|
entry.Logf(WarnLevel, format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Warningf(format string, args ...interface{}) {
|
||||||
|
entry.Warnf(format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Errorf(format string, args ...interface{}) {
|
||||||
|
entry.Logf(ErrorLevel, format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Fatalf(format string, args ...interface{}) {
|
||||||
|
entry.Logf(FatalLevel, format, args...)
|
||||||
|
entry.Logger.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Panicf(format string, args ...interface{}) {
|
||||||
|
entry.Logf(PanicLevel, format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Entry Println family functions
|
||||||
|
|
||||||
|
func (entry *Entry) Logln(level Level, args ...interface{}) {
|
||||||
|
if entry.Logger.IsLevelEnabled(level) {
|
||||||
|
entry.Log(level, entry.sprintlnn(args...))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Traceln(args ...interface{}) {
|
||||||
|
entry.Logln(TraceLevel, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Debugln(args ...interface{}) {
|
||||||
|
entry.Logln(DebugLevel, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Infoln(args ...interface{}) {
|
||||||
|
entry.Logln(InfoLevel, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Println(args ...interface{}) {
|
||||||
|
entry.Infoln(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Warnln(args ...interface{}) {
|
||||||
|
entry.Logln(WarnLevel, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Warningln(args ...interface{}) {
|
||||||
|
entry.Warnln(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Errorln(args ...interface{}) {
|
||||||
|
entry.Logln(ErrorLevel, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Fatalln(args ...interface{}) {
|
||||||
|
entry.Logln(FatalLevel, args...)
|
||||||
|
entry.Logger.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (entry *Entry) Panicln(args ...interface{}) {
|
||||||
|
entry.Logln(PanicLevel, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sprintlnn => Sprint no newline. This is to get the behavior of how
|
||||||
|
// fmt.Sprintln where spaces are always added between operands, regardless of
|
||||||
|
// their type. Instead of vendoring the Sprintln implementation to spare a
|
||||||
|
// string allocation, we do the simplest thing.
|
||||||
|
func (entry *Entry) sprintlnn(args ...interface{}) string {
|
||||||
|
msg := fmt.Sprintln(args...)
|
||||||
|
return msg[:len(msg)-1]
|
||||||
|
}
|
270
vendor/github.com/sirupsen/logrus/exported.go
generated
vendored
Normal file
270
vendor/github.com/sirupsen/logrus/exported.go
generated
vendored
Normal file
|
@ -0,0 +1,270 @@
|
||||||
|
package logrus
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"io"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// std is the name of the standard logger in stdlib `log`
|
||||||
|
std = New()
|
||||||
|
)
|
||||||
|
|
||||||
|
func StandardLogger() *Logger {
|
||||||
|
return std
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetOutput sets the standard logger output.
|
||||||
|
func SetOutput(out io.Writer) {
|
||||||
|
std.SetOutput(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetFormatter sets the standard logger formatter.
|
||||||
|
func SetFormatter(formatter Formatter) {
|
||||||
|
std.SetFormatter(formatter)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetReportCaller sets whether the standard logger will include the calling
|
||||||
|
// method as a field.
|
||||||
|
func SetReportCaller(include bool) {
|
||||||
|
std.SetReportCaller(include)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetLevel sets the standard logger level.
|
||||||
|
func SetLevel(level Level) {
|
||||||
|
std.SetLevel(level)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLevel returns the standard logger level.
|
||||||
|
func GetLevel() Level {
|
||||||
|
return std.GetLevel()
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsLevelEnabled checks if the log level of the standard logger is greater than the level param
|
||||||
|
func IsLevelEnabled(level Level) bool {
|
||||||
|
return std.IsLevelEnabled(level)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddHook adds a hook to the standard logger hooks.
|
||||||
|
func AddHook(hook Hook) {
|
||||||
|
std.AddHook(hook)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithError creates an entry from the standard logger and adds an error to it, using the value defined in ErrorKey as key.
|
||||||
|
func WithError(err error) *Entry {
|
||||||
|
return std.WithField(ErrorKey, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithContext creates an entry from the standard logger and adds a context to it.
|
||||||
|
func WithContext(ctx context.Context) *Entry {
|
||||||
|
return std.WithContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithField creates an entry from the standard logger and adds a field to
|
||||||
|
// it. If you want multiple fields, use `WithFields`.
|
||||||
|
//
|
||||||
|
// Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal
|
||||||
|
// or Panic on the Entry it returns.
|
||||||
|
func WithField(key string, value interface{}) *Entry {
|
||||||
|
return std.WithField(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithFields creates an entry from the standard logger and adds multiple
|
||||||
|
// fields to it. This is simply a helper for `WithField`, invoking it
|
||||||
|
// once for each field.
|
||||||
|
//
|
||||||
|
// Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal
|
||||||
|
// or Panic on the Entry it returns.
|
||||||
|
func WithFields(fields Fields) *Entry {
|
||||||
|
return std.WithFields(fields)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithTime creates an entry from the standard logger and overrides the time of
|
||||||
|
// logs generated with it.
|
||||||
|
//
|
||||||
|
// Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal
|
||||||
|
// or Panic on the Entry it returns.
|
||||||
|
func WithTime(t time.Time) *Entry {
|
||||||
|
return std.WithTime(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trace logs a message at level Trace on the standard logger.
|
||||||
|
func Trace(args ...interface{}) {
|
||||||
|
std.Trace(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Debug logs a message at level Debug on the standard logger.
|
||||||
|
func Debug(args ...interface{}) {
|
||||||
|
std.Debug(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print logs a message at level Info on the standard logger.
|
||||||
|
func Print(args ...interface{}) {
|
||||||
|
std.Print(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info logs a message at level Info on the standard logger.
|
||||||
|
func Info(args ...interface{}) {
|
||||||
|
std.Info(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warn logs a message at level Warn on the standard logger.
|
||||||
|
func Warn(args ...interface{}) {
|
||||||
|
std.Warn(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warning logs a message at level Warn on the standard logger.
|
||||||
|
func Warning(args ...interface{}) {
|
||||||
|
std.Warning(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error logs a message at level Error on the standard logger.
|
||||||
|
func Error(args ...interface{}) {
|
||||||
|
std.Error(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Panic logs a message at level Panic on the standard logger.
|
||||||
|
func Panic(args ...interface{}) {
|
||||||
|
std.Panic(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fatal logs a message at level Fatal on the standard logger then the process will exit with status set to 1.
|
||||||
|
func Fatal(args ...interface{}) {
|
||||||
|
std.Fatal(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TraceFn logs a message from a func at level Trace on the standard logger.
|
||||||
|
func TraceFn(fn LogFunction) {
|
||||||
|
std.TraceFn(fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DebugFn logs a message from a func at level Debug on the standard logger.
|
||||||
|
func DebugFn(fn LogFunction) {
|
||||||
|
std.DebugFn(fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrintFn logs a message from a func at level Info on the standard logger.
|
||||||
|
func PrintFn(fn LogFunction) {
|
||||||
|
std.PrintFn(fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
// InfoFn logs a message from a func at level Info on the standard logger.
|
||||||
|
func InfoFn(fn LogFunction) {
|
||||||
|
std.InfoFn(fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WarnFn logs a message from a func at level Warn on the standard logger.
|
||||||
|
func WarnFn(fn LogFunction) {
|
||||||
|
std.WarnFn(fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WarningFn logs a message from a func at level Warn on the standard logger.
|
||||||
|
func WarningFn(fn LogFunction) {
|
||||||
|
std.WarningFn(fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ErrorFn logs a message from a func at level Error on the standard logger.
|
||||||
|
func ErrorFn(fn LogFunction) {
|
||||||
|
std.ErrorFn(fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PanicFn logs a message from a func at level Panic on the standard logger.
|
||||||
|
func PanicFn(fn LogFunction) {
|
||||||
|
std.PanicFn(fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FatalFn logs a message from a func at level Fatal on the standard logger then the process will exit with status set to 1.
|
||||||
|
func FatalFn(fn LogFunction) {
|
||||||
|
std.FatalFn(fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tracef logs a message at level Trace on the standard logger.
|
||||||
|
func Tracef(format string, args ...interface{}) {
|
||||||
|
std.Tracef(format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Debugf logs a message at level Debug on the standard logger.
|
||||||
|
func Debugf(format string, args ...interface{}) {
|
||||||
|
std.Debugf(format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Printf logs a message at level Info on the standard logger.
|
||||||
|
func Printf(format string, args ...interface{}) {
|
||||||
|
std.Printf(format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Infof logs a message at level Info on the standard logger.
|
||||||
|
func Infof(format string, args ...interface{}) {
|
||||||
|
std.Infof(format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warnf logs a message at level Warn on the standard logger.
|
||||||
|
func Warnf(format string, args ...interface{}) {
|
||||||
|
std.Warnf(format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warningf logs a message at level Warn on the standard logger.
|
||||||
|
func Warningf(format string, args ...interface{}) {
|
||||||
|
std.Warningf(format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Errorf logs a message at level Error on the standard logger.
|
||||||
|
func Errorf(format string, args ...interface{}) {
|
||||||
|
std.Errorf(format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Panicf logs a message at level Panic on the standard logger.
|
||||||
|
func Panicf(format string, args ...interface{}) {
|
||||||
|
std.Panicf(format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fatalf logs a message at level Fatal on the standard logger then the process will exit with status set to 1.
|
||||||
|
func Fatalf(format string, args ...interface{}) {
|
||||||
|
std.Fatalf(format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Traceln logs a message at level Trace on the standard logger.
|
||||||
|
func Traceln(args ...interface{}) {
|
||||||
|
std.Traceln(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Debugln logs a message at level Debug on the standard logger.
|
||||||
|
func Debugln(args ...interface{}) {
|
||||||
|
std.Debugln(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Println logs a message at level Info on the standard logger.
|
||||||
|
func Println(args ...interface{}) {
|
||||||
|
std.Println(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Infoln logs a message at level Info on the standard logger.
|
||||||
|
func Infoln(args ...interface{}) {
|
||||||
|
std.Infoln(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warnln logs a message at level Warn on the standard logger.
|
||||||
|
func Warnln(args ...interface{}) {
|
||||||
|
std.Warnln(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warningln logs a message at level Warn on the standard logger.
|
||||||
|
func Warningln(args ...interface{}) {
|
||||||
|
std.Warningln(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Errorln logs a message at level Error on the standard logger.
|
||||||
|
func Errorln(args ...interface{}) {
|
||||||
|
std.Errorln(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Panicln logs a message at level Panic on the standard logger.
|
||||||
|
func Panicln(args ...interface{}) {
|
||||||
|
std.Panicln(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fatalln logs a message at level Fatal on the standard logger then the process will exit with status set to 1.
|
||||||
|
func Fatalln(args ...interface{}) {
|
||||||
|
std.Fatalln(args...)
|
||||||
|
}
|
78
vendor/github.com/sirupsen/logrus/formatter.go
generated
vendored
Normal file
78
vendor/github.com/sirupsen/logrus/formatter.go
generated
vendored
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
package logrus
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
// Default key names for the default fields
|
||||||
|
const (
|
||||||
|
defaultTimestampFormat = time.RFC3339
|
||||||
|
FieldKeyMsg = "msg"
|
||||||
|
FieldKeyLevel = "level"
|
||||||
|
FieldKeyTime = "time"
|
||||||
|
FieldKeyLogrusError = "logrus_error"
|
||||||
|
FieldKeyFunc = "func"
|
||||||
|
FieldKeyFile = "file"
|
||||||
|
)
|
||||||
|
|
||||||
|
// The Formatter interface is used to implement a custom Formatter. It takes an
|
||||||
|
// `Entry`. It exposes all the fields, including the default ones:
|
||||||
|
//
|
||||||
|
// * `entry.Data["msg"]`. The message passed from Info, Warn, Error ..
|
||||||
|
// * `entry.Data["time"]`. The timestamp.
|
||||||
|
// * `entry.Data["level"]. The level the entry was logged at.
|
||||||
|
//
|
||||||
|
// Any additional fields added with `WithField` or `WithFields` are also in
|
||||||
|
// `entry.Data`. Format is expected to return an array of bytes which are then
|
||||||
|
// logged to `logger.Out`.
|
||||||
|
type Formatter interface {
|
||||||
|
Format(*Entry) ([]byte, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is to not silently overwrite `time`, `msg`, `func` and `level` fields when
|
||||||
|
// dumping it. If this code wasn't there doing:
|
||||||
|
//
|
||||||
|
// logrus.WithField("level", 1).Info("hello")
|
||||||
|
//
|
||||||
|
// Would just silently drop the user provided level. Instead with this code
|
||||||
|
// it'll logged as:
|
||||||
|
//
|
||||||
|
// {"level": "info", "fields.level": 1, "msg": "hello", "time": "..."}
|
||||||
|
//
|
||||||
|
// It's not exported because it's still using Data in an opinionated way. It's to
|
||||||
|
// avoid code duplication between the two default formatters.
|
||||||
|
func prefixFieldClashes(data Fields, fieldMap FieldMap, reportCaller bool) {
|
||||||
|
timeKey := fieldMap.resolve(FieldKeyTime)
|
||||||
|
if t, ok := data[timeKey]; ok {
|
||||||
|
data["fields."+timeKey] = t
|
||||||
|
delete(data, timeKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
msgKey := fieldMap.resolve(FieldKeyMsg)
|
||||||
|
if m, ok := data[msgKey]; ok {
|
||||||
|
data["fields."+msgKey] = m
|
||||||
|
delete(data, msgKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
levelKey := fieldMap.resolve(FieldKeyLevel)
|
||||||
|
if l, ok := data[levelKey]; ok {
|
||||||
|
data["fields."+levelKey] = l
|
||||||
|
delete(data, levelKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
logrusErrKey := fieldMap.resolve(FieldKeyLogrusError)
|
||||||
|
if l, ok := data[logrusErrKey]; ok {
|
||||||
|
data["fields."+logrusErrKey] = l
|
||||||
|
delete(data, logrusErrKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
// If reportCaller is not set, 'func' will not conflict.
|
||||||
|
if reportCaller {
|
||||||
|
funcKey := fieldMap.resolve(FieldKeyFunc)
|
||||||
|
if l, ok := data[funcKey]; ok {
|
||||||
|
data["fields."+funcKey] = l
|
||||||
|
}
|
||||||
|
fileKey := fieldMap.resolve(FieldKeyFile)
|
||||||
|
if l, ok := data[fileKey]; ok {
|
||||||
|
data["fields."+fileKey] = l
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
34
vendor/github.com/sirupsen/logrus/hooks.go
generated
vendored
Normal file
34
vendor/github.com/sirupsen/logrus/hooks.go
generated
vendored
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
package logrus
|
||||||
|
|
||||||
|
// A hook to be fired when logging on the logging levels returned from
|
||||||
|
// `Levels()` on your implementation of the interface. Note that this is not
|
||||||
|
// fired in a goroutine or a channel with workers, you should handle such
|
||||||
|
// functionality yourself if your call is non-blocking and you don't wish for
|
||||||
|
// the logging calls for levels returned from `Levels()` to block.
|
||||||
|
type Hook interface {
|
||||||
|
Levels() []Level
|
||||||
|
Fire(*Entry) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Internal type for storing the hooks on a logger instance.
|
||||||
|
type LevelHooks map[Level][]Hook
|
||||||
|
|
||||||
|
// Add a hook to an instance of logger. This is called with
|
||||||
|
// `log.Hooks.Add(new(MyHook))` where `MyHook` implements the `Hook` interface.
|
||||||
|
func (hooks LevelHooks) Add(hook Hook) {
|
||||||
|
for _, level := range hook.Levels() {
|
||||||
|
hooks[level] = append(hooks[level], hook)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fire all the hooks for the passed level. Used by `entry.log` to fire
|
||||||
|
// appropriate hooks for a log entry.
|
||||||
|
func (hooks LevelHooks) Fire(level Level, entry *Entry) error {
|
||||||
|
for _, hook := range hooks[level] {
|
||||||
|
if err := hook.Fire(entry); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
128
vendor/github.com/sirupsen/logrus/json_formatter.go
generated
vendored
Normal file
128
vendor/github.com/sirupsen/logrus/json_formatter.go
generated
vendored
Normal file
|
@ -0,0 +1,128 @@
|
||||||
|
package logrus
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"runtime"
|
||||||
|
)
|
||||||
|
|
||||||
|
type fieldKey string
|
||||||
|
|
||||||
|
// FieldMap allows customization of the key names for default fields.
|
||||||
|
type FieldMap map[fieldKey]string
|
||||||
|
|
||||||
|
func (f FieldMap) resolve(key fieldKey) string {
|
||||||
|
if k, ok := f[key]; ok {
|
||||||
|
return k
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// JSONFormatter formats logs into parsable json
|
||||||
|
type JSONFormatter struct {
|
||||||
|
// TimestampFormat sets the format used for marshaling timestamps.
|
||||||
|
// The format to use is the same than for time.Format or time.Parse from the standard
|
||||||
|
// library.
|
||||||
|
// The standard Library already provides a set of predefined format.
|
||||||
|
TimestampFormat string
|
||||||
|
|
||||||
|
// DisableTimestamp allows disabling automatic timestamps in output
|
||||||
|
DisableTimestamp bool
|
||||||
|
|
||||||
|
// DisableHTMLEscape allows disabling html escaping in output
|
||||||
|
DisableHTMLEscape bool
|
||||||
|
|
||||||
|
// DataKey allows users to put all the log entry parameters into a nested dictionary at a given key.
|
||||||
|
DataKey string
|
||||||
|
|
||||||
|
// FieldMap allows users to customize the names of keys for default fields.
|
||||||
|
// As an example:
|
||||||
|
// formatter := &JSONFormatter{
|
||||||
|
// FieldMap: FieldMap{
|
||||||
|
// FieldKeyTime: "@timestamp",
|
||||||
|
// FieldKeyLevel: "@level",
|
||||||
|
// FieldKeyMsg: "@message",
|
||||||
|
// FieldKeyFunc: "@caller",
|
||||||
|
// },
|
||||||
|
// }
|
||||||
|
FieldMap FieldMap
|
||||||
|
|
||||||
|
// CallerPrettyfier can be set by the user to modify the content
|
||||||
|
// of the function and file keys in the json data when ReportCaller is
|
||||||
|
// activated. If any of the returned value is the empty string the
|
||||||
|
// corresponding key will be removed from json fields.
|
||||||
|
CallerPrettyfier func(*runtime.Frame) (function string, file string)
|
||||||
|
|
||||||
|
// PrettyPrint will indent all json logs
|
||||||
|
PrettyPrint bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format renders a single log entry
|
||||||
|
func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
|
||||||
|
data := make(Fields, len(entry.Data)+4)
|
||||||
|
for k, v := range entry.Data {
|
||||||
|
switch v := v.(type) {
|
||||||
|
case error:
|
||||||
|
// Otherwise errors are ignored by `encoding/json`
|
||||||
|
// https://github.com/sirupsen/logrus/issues/137
|
||||||
|
data[k] = v.Error()
|
||||||
|
default:
|
||||||
|
data[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.DataKey != "" {
|
||||||
|
newData := make(Fields, 4)
|
||||||
|
newData[f.DataKey] = data
|
||||||
|
data = newData
|
||||||
|
}
|
||||||
|
|
||||||
|
prefixFieldClashes(data, f.FieldMap, entry.HasCaller())
|
||||||
|
|
||||||
|
timestampFormat := f.TimestampFormat
|
||||||
|
if timestampFormat == "" {
|
||||||
|
timestampFormat = defaultTimestampFormat
|
||||||
|
}
|
||||||
|
|
||||||
|
if entry.err != "" {
|
||||||
|
data[f.FieldMap.resolve(FieldKeyLogrusError)] = entry.err
|
||||||
|
}
|
||||||
|
if !f.DisableTimestamp {
|
||||||
|
data[f.FieldMap.resolve(FieldKeyTime)] = entry.Time.Format(timestampFormat)
|
||||||
|
}
|
||||||
|
data[f.FieldMap.resolve(FieldKeyMsg)] = entry.Message
|
||||||
|
data[f.FieldMap.resolve(FieldKeyLevel)] = entry.Level.String()
|
||||||
|
if entry.HasCaller() {
|
||||||
|
funcVal := entry.Caller.Function
|
||||||
|
fileVal := fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line)
|
||||||
|
if f.CallerPrettyfier != nil {
|
||||||
|
funcVal, fileVal = f.CallerPrettyfier(entry.Caller)
|
||||||
|
}
|
||||||
|
if funcVal != "" {
|
||||||
|
data[f.FieldMap.resolve(FieldKeyFunc)] = funcVal
|
||||||
|
}
|
||||||
|
if fileVal != "" {
|
||||||
|
data[f.FieldMap.resolve(FieldKeyFile)] = fileVal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var b *bytes.Buffer
|
||||||
|
if entry.Buffer != nil {
|
||||||
|
b = entry.Buffer
|
||||||
|
} else {
|
||||||
|
b = &bytes.Buffer{}
|
||||||
|
}
|
||||||
|
|
||||||
|
encoder := json.NewEncoder(b)
|
||||||
|
encoder.SetEscapeHTML(!f.DisableHTMLEscape)
|
||||||
|
if f.PrettyPrint {
|
||||||
|
encoder.SetIndent("", " ")
|
||||||
|
}
|
||||||
|
if err := encoder.Encode(data); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to marshal fields to JSON, %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.Bytes(), nil
|
||||||
|
}
|
417
vendor/github.com/sirupsen/logrus/logger.go
generated
vendored
Normal file
417
vendor/github.com/sirupsen/logrus/logger.go
generated
vendored
Normal file
|
@ -0,0 +1,417 @@
|
||||||
|
package logrus
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// LogFunction For big messages, it can be more efficient to pass a function
|
||||||
|
// and only call it if the log level is actually enables rather than
|
||||||
|
// generating the log message and then checking if the level is enabled
|
||||||
|
type LogFunction func() []interface{}
|
||||||
|
|
||||||
|
type Logger struct {
|
||||||
|
// The logs are `io.Copy`'d to this in a mutex. It's common to set this to a
|
||||||
|
// file, or leave it default which is `os.Stderr`. You can also set this to
|
||||||
|
// something more adventurous, such as logging to Kafka.
|
||||||
|
Out io.Writer
|
||||||
|
// Hooks for the logger instance. These allow firing events based on logging
|
||||||
|
// levels and log entries. For example, to send errors to an error tracking
|
||||||
|
// service, log to StatsD or dump the core on fatal errors.
|
||||||
|
Hooks LevelHooks
|
||||||
|
// All log entries pass through the formatter before logged to Out. The
|
||||||
|
// included formatters are `TextFormatter` and `JSONFormatter` for which
|
||||||
|
// TextFormatter is the default. In development (when a TTY is attached) it
|
||||||
|
// logs with colors, but to a file it wouldn't. You can easily implement your
|
||||||
|
// own that implements the `Formatter` interface, see the `README` or included
|
||||||
|
// formatters for examples.
|
||||||
|
Formatter Formatter
|
||||||
|
|
||||||
|
// Flag for whether to log caller info (off by default)
|
||||||
|
ReportCaller bool
|
||||||
|
|
||||||
|
// The logging level the logger should log at. This is typically (and defaults
|
||||||
|
// to) `logrus.Info`, which allows Info(), Warn(), Error() and Fatal() to be
|
||||||
|
// logged.
|
||||||
|
Level Level
|
||||||
|
// Used to sync writing to the log. Locking is enabled by Default
|
||||||
|
mu MutexWrap
|
||||||
|
// Reusable empty entry
|
||||||
|
entryPool sync.Pool
|
||||||
|
// Function to exit the application, defaults to `os.Exit()`
|
||||||
|
ExitFunc exitFunc
|
||||||
|
// The buffer pool used to format the log. If it is nil, the default global
|
||||||
|
// buffer pool will be used.
|
||||||
|
BufferPool BufferPool
|
||||||
|
}
|
||||||
|
|
||||||
|
type exitFunc func(int)
|
||||||
|
|
||||||
|
type MutexWrap struct {
|
||||||
|
lock sync.Mutex
|
||||||
|
disabled bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mw *MutexWrap) Lock() {
|
||||||
|
if !mw.disabled {
|
||||||
|
mw.lock.Lock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mw *MutexWrap) Unlock() {
|
||||||
|
if !mw.disabled {
|
||||||
|
mw.lock.Unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mw *MutexWrap) Disable() {
|
||||||
|
mw.disabled = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Creates a new logger. Configuration should be set by changing `Formatter`,
|
||||||
|
// `Out` and `Hooks` directly on the default logger instance. You can also just
|
||||||
|
// instantiate your own:
|
||||||
|
//
|
||||||
|
// var log = &logrus.Logger{
|
||||||
|
// Out: os.Stderr,
|
||||||
|
// Formatter: new(logrus.TextFormatter),
|
||||||
|
// Hooks: make(logrus.LevelHooks),
|
||||||
|
// Level: logrus.DebugLevel,
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// It's recommended to make this a global instance called `log`.
|
||||||
|
func New() *Logger {
|
||||||
|
return &Logger{
|
||||||
|
Out: os.Stderr,
|
||||||
|
Formatter: new(TextFormatter),
|
||||||
|
Hooks: make(LevelHooks),
|
||||||
|
Level: InfoLevel,
|
||||||
|
ExitFunc: os.Exit,
|
||||||
|
ReportCaller: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) newEntry() *Entry {
|
||||||
|
entry, ok := logger.entryPool.Get().(*Entry)
|
||||||
|
if ok {
|
||||||
|
return entry
|
||||||
|
}
|
||||||
|
return NewEntry(logger)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) releaseEntry(entry *Entry) {
|
||||||
|
entry.Data = map[string]interface{}{}
|
||||||
|
logger.entryPool.Put(entry)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithField allocates a new entry and adds a field to it.
|
||||||
|
// Debug, Print, Info, Warn, Error, Fatal or Panic must be then applied to
|
||||||
|
// this new returned entry.
|
||||||
|
// If you want multiple fields, use `WithFields`.
|
||||||
|
func (logger *Logger) WithField(key string, value interface{}) *Entry {
|
||||||
|
entry := logger.newEntry()
|
||||||
|
defer logger.releaseEntry(entry)
|
||||||
|
return entry.WithField(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adds a struct of fields to the log entry. All it does is call `WithField` for
|
||||||
|
// each `Field`.
|
||||||
|
func (logger *Logger) WithFields(fields Fields) *Entry {
|
||||||
|
entry := logger.newEntry()
|
||||||
|
defer logger.releaseEntry(entry)
|
||||||
|
return entry.WithFields(fields)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add an error as single field to the log entry. All it does is call
|
||||||
|
// `WithError` for the given `error`.
|
||||||
|
func (logger *Logger) WithError(err error) *Entry {
|
||||||
|
entry := logger.newEntry()
|
||||||
|
defer logger.releaseEntry(entry)
|
||||||
|
return entry.WithError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add a context to the log entry.
|
||||||
|
func (logger *Logger) WithContext(ctx context.Context) *Entry {
|
||||||
|
entry := logger.newEntry()
|
||||||
|
defer logger.releaseEntry(entry)
|
||||||
|
return entry.WithContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Overrides the time of the log entry.
|
||||||
|
func (logger *Logger) WithTime(t time.Time) *Entry {
|
||||||
|
entry := logger.newEntry()
|
||||||
|
defer logger.releaseEntry(entry)
|
||||||
|
return entry.WithTime(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Logf(level Level, format string, args ...interface{}) {
|
||||||
|
if logger.IsLevelEnabled(level) {
|
||||||
|
entry := logger.newEntry()
|
||||||
|
entry.Logf(level, format, args...)
|
||||||
|
logger.releaseEntry(entry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Tracef(format string, args ...interface{}) {
|
||||||
|
logger.Logf(TraceLevel, format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Debugf(format string, args ...interface{}) {
|
||||||
|
logger.Logf(DebugLevel, format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Infof(format string, args ...interface{}) {
|
||||||
|
logger.Logf(InfoLevel, format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Printf(format string, args ...interface{}) {
|
||||||
|
entry := logger.newEntry()
|
||||||
|
entry.Printf(format, args...)
|
||||||
|
logger.releaseEntry(entry)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Warnf(format string, args ...interface{}) {
|
||||||
|
logger.Logf(WarnLevel, format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Warningf(format string, args ...interface{}) {
|
||||||
|
logger.Warnf(format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Errorf(format string, args ...interface{}) {
|
||||||
|
logger.Logf(ErrorLevel, format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Fatalf(format string, args ...interface{}) {
|
||||||
|
logger.Logf(FatalLevel, format, args...)
|
||||||
|
logger.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Panicf(format string, args ...interface{}) {
|
||||||
|
logger.Logf(PanicLevel, format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log will log a message at the level given as parameter.
|
||||||
|
// Warning: using Log at Panic or Fatal level will not respectively Panic nor Exit.
|
||||||
|
// For this behaviour Logger.Panic or Logger.Fatal should be used instead.
|
||||||
|
func (logger *Logger) Log(level Level, args ...interface{}) {
|
||||||
|
if logger.IsLevelEnabled(level) {
|
||||||
|
entry := logger.newEntry()
|
||||||
|
entry.Log(level, args...)
|
||||||
|
logger.releaseEntry(entry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) LogFn(level Level, fn LogFunction) {
|
||||||
|
if logger.IsLevelEnabled(level) {
|
||||||
|
entry := logger.newEntry()
|
||||||
|
entry.Log(level, fn()...)
|
||||||
|
logger.releaseEntry(entry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Trace(args ...interface{}) {
|
||||||
|
logger.Log(TraceLevel, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Debug(args ...interface{}) {
|
||||||
|
logger.Log(DebugLevel, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Info(args ...interface{}) {
|
||||||
|
logger.Log(InfoLevel, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Print(args ...interface{}) {
|
||||||
|
entry := logger.newEntry()
|
||||||
|
entry.Print(args...)
|
||||||
|
logger.releaseEntry(entry)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Warn(args ...interface{}) {
|
||||||
|
logger.Log(WarnLevel, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Warning(args ...interface{}) {
|
||||||
|
logger.Warn(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Error(args ...interface{}) {
|
||||||
|
logger.Log(ErrorLevel, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Fatal(args ...interface{}) {
|
||||||
|
logger.Log(FatalLevel, args...)
|
||||||
|
logger.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Panic(args ...interface{}) {
|
||||||
|
logger.Log(PanicLevel, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) TraceFn(fn LogFunction) {
|
||||||
|
logger.LogFn(TraceLevel, fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) DebugFn(fn LogFunction) {
|
||||||
|
logger.LogFn(DebugLevel, fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) InfoFn(fn LogFunction) {
|
||||||
|
logger.LogFn(InfoLevel, fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) PrintFn(fn LogFunction) {
|
||||||
|
entry := logger.newEntry()
|
||||||
|
entry.Print(fn()...)
|
||||||
|
logger.releaseEntry(entry)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) WarnFn(fn LogFunction) {
|
||||||
|
logger.LogFn(WarnLevel, fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) WarningFn(fn LogFunction) {
|
||||||
|
logger.WarnFn(fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) ErrorFn(fn LogFunction) {
|
||||||
|
logger.LogFn(ErrorLevel, fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) FatalFn(fn LogFunction) {
|
||||||
|
logger.LogFn(FatalLevel, fn)
|
||||||
|
logger.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) PanicFn(fn LogFunction) {
|
||||||
|
logger.LogFn(PanicLevel, fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Logln(level Level, args ...interface{}) {
|
||||||
|
if logger.IsLevelEnabled(level) {
|
||||||
|
entry := logger.newEntry()
|
||||||
|
entry.Logln(level, args...)
|
||||||
|
logger.releaseEntry(entry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Traceln(args ...interface{}) {
|
||||||
|
logger.Logln(TraceLevel, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Debugln(args ...interface{}) {
|
||||||
|
logger.Logln(DebugLevel, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Infoln(args ...interface{}) {
|
||||||
|
logger.Logln(InfoLevel, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Println(args ...interface{}) {
|
||||||
|
entry := logger.newEntry()
|
||||||
|
entry.Println(args...)
|
||||||
|
logger.releaseEntry(entry)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Warnln(args ...interface{}) {
|
||||||
|
logger.Logln(WarnLevel, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Warningln(args ...interface{}) {
|
||||||
|
logger.Warnln(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Errorln(args ...interface{}) {
|
||||||
|
logger.Logln(ErrorLevel, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Fatalln(args ...interface{}) {
|
||||||
|
logger.Logln(FatalLevel, args...)
|
||||||
|
logger.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Panicln(args ...interface{}) {
|
||||||
|
logger.Logln(PanicLevel, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Exit(code int) {
|
||||||
|
runHandlers()
|
||||||
|
if logger.ExitFunc == nil {
|
||||||
|
logger.ExitFunc = os.Exit
|
||||||
|
}
|
||||||
|
logger.ExitFunc(code)
|
||||||
|
}
|
||||||
|
|
||||||
|
//When file is opened with appending mode, it's safe to
|
||||||
|
//write concurrently to a file (within 4k message on Linux).
|
||||||
|
//In these cases user can choose to disable the lock.
|
||||||
|
func (logger *Logger) SetNoLock() {
|
||||||
|
logger.mu.Disable()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) level() Level {
|
||||||
|
return Level(atomic.LoadUint32((*uint32)(&logger.Level)))
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetLevel sets the logger level.
|
||||||
|
func (logger *Logger) SetLevel(level Level) {
|
||||||
|
atomic.StoreUint32((*uint32)(&logger.Level), uint32(level))
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLevel returns the logger level.
|
||||||
|
func (logger *Logger) GetLevel() Level {
|
||||||
|
return logger.level()
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddHook adds a hook to the logger hooks.
|
||||||
|
func (logger *Logger) AddHook(hook Hook) {
|
||||||
|
logger.mu.Lock()
|
||||||
|
defer logger.mu.Unlock()
|
||||||
|
logger.Hooks.Add(hook)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsLevelEnabled checks if the log level of the logger is greater than the level param
|
||||||
|
func (logger *Logger) IsLevelEnabled(level Level) bool {
|
||||||
|
return logger.level() >= level
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetFormatter sets the logger formatter.
|
||||||
|
func (logger *Logger) SetFormatter(formatter Formatter) {
|
||||||
|
logger.mu.Lock()
|
||||||
|
defer logger.mu.Unlock()
|
||||||
|
logger.Formatter = formatter
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetOutput sets the logger output.
|
||||||
|
func (logger *Logger) SetOutput(output io.Writer) {
|
||||||
|
logger.mu.Lock()
|
||||||
|
defer logger.mu.Unlock()
|
||||||
|
logger.Out = output
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) SetReportCaller(reportCaller bool) {
|
||||||
|
logger.mu.Lock()
|
||||||
|
defer logger.mu.Unlock()
|
||||||
|
logger.ReportCaller = reportCaller
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReplaceHooks replaces the logger hooks and returns the old ones
|
||||||
|
func (logger *Logger) ReplaceHooks(hooks LevelHooks) LevelHooks {
|
||||||
|
logger.mu.Lock()
|
||||||
|
oldHooks := logger.Hooks
|
||||||
|
logger.Hooks = hooks
|
||||||
|
logger.mu.Unlock()
|
||||||
|
return oldHooks
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetBufferPool sets the logger buffer pool.
|
||||||
|
func (logger *Logger) SetBufferPool(pool BufferPool) {
|
||||||
|
logger.mu.Lock()
|
||||||
|
defer logger.mu.Unlock()
|
||||||
|
logger.BufferPool = pool
|
||||||
|
}
|
186
vendor/github.com/sirupsen/logrus/logrus.go
generated
vendored
Normal file
186
vendor/github.com/sirupsen/logrus/logrus.go
generated
vendored
Normal file
|
@ -0,0 +1,186 @@
|
||||||
|
package logrus
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Fields type, used to pass to `WithFields`.
|
||||||
|
type Fields map[string]interface{}
|
||||||
|
|
||||||
|
// Level type
|
||||||
|
type Level uint32
|
||||||
|
|
||||||
|
// Convert the Level to a string. E.g. PanicLevel becomes "panic".
|
||||||
|
func (level Level) String() string {
|
||||||
|
if b, err := level.MarshalText(); err == nil {
|
||||||
|
return string(b)
|
||||||
|
} else {
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseLevel takes a string level and returns the Logrus log level constant.
|
||||||
|
func ParseLevel(lvl string) (Level, error) {
|
||||||
|
switch strings.ToLower(lvl) {
|
||||||
|
case "panic":
|
||||||
|
return PanicLevel, nil
|
||||||
|
case "fatal":
|
||||||
|
return FatalLevel, nil
|
||||||
|
case "error":
|
||||||
|
return ErrorLevel, nil
|
||||||
|
case "warn", "warning":
|
||||||
|
return WarnLevel, nil
|
||||||
|
case "info":
|
||||||
|
return InfoLevel, nil
|
||||||
|
case "debug":
|
||||||
|
return DebugLevel, nil
|
||||||
|
case "trace":
|
||||||
|
return TraceLevel, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var l Level
|
||||||
|
return l, fmt.Errorf("not a valid logrus Level: %q", lvl)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||||
|
func (level *Level) UnmarshalText(text []byte) error {
|
||||||
|
l, err := ParseLevel(string(text))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
*level = l
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (level Level) MarshalText() ([]byte, error) {
|
||||||
|
switch level {
|
||||||
|
case TraceLevel:
|
||||||
|
return []byte("trace"), nil
|
||||||
|
case DebugLevel:
|
||||||
|
return []byte("debug"), nil
|
||||||
|
case InfoLevel:
|
||||||
|
return []byte("info"), nil
|
||||||
|
case WarnLevel:
|
||||||
|
return []byte("warning"), nil
|
||||||
|
case ErrorLevel:
|
||||||
|
return []byte("error"), nil
|
||||||
|
case FatalLevel:
|
||||||
|
return []byte("fatal"), nil
|
||||||
|
case PanicLevel:
|
||||||
|
return []byte("panic"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("not a valid logrus level %d", level)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A constant exposing all logging levels
|
||||||
|
var AllLevels = []Level{
|
||||||
|
PanicLevel,
|
||||||
|
FatalLevel,
|
||||||
|
ErrorLevel,
|
||||||
|
WarnLevel,
|
||||||
|
InfoLevel,
|
||||||
|
DebugLevel,
|
||||||
|
TraceLevel,
|
||||||
|
}
|
||||||
|
|
||||||
|
// These are the different logging levels. You can set the logging level to log
|
||||||
|
// on your instance of logger, obtained with `logrus.New()`.
|
||||||
|
const (
|
||||||
|
// PanicLevel level, highest level of severity. Logs and then calls panic with the
|
||||||
|
// message passed to Debug, Info, ...
|
||||||
|
PanicLevel Level = iota
|
||||||
|
// FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
|
||||||
|
// logging level is set to Panic.
|
||||||
|
FatalLevel
|
||||||
|
// ErrorLevel level. Logs. Used for errors that should definitely be noted.
|
||||||
|
// Commonly used for hooks to send errors to an error tracking service.
|
||||||
|
ErrorLevel
|
||||||
|
// WarnLevel level. Non-critical entries that deserve eyes.
|
||||||
|
WarnLevel
|
||||||
|
// InfoLevel level. General operational entries about what's going on inside the
|
||||||
|
// application.
|
||||||
|
InfoLevel
|
||||||
|
// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
|
||||||
|
DebugLevel
|
||||||
|
// TraceLevel level. Designates finer-grained informational events than the Debug.
|
||||||
|
TraceLevel
|
||||||
|
)
|
||||||
|
|
||||||
|
// Won't compile if StdLogger can't be realized by a log.Logger
|
||||||
|
var (
|
||||||
|
_ StdLogger = &log.Logger{}
|
||||||
|
_ StdLogger = &Entry{}
|
||||||
|
_ StdLogger = &Logger{}
|
||||||
|
)
|
||||||
|
|
||||||
|
// StdLogger is what your logrus-enabled library should take, that way
|
||||||
|
// it'll accept a stdlib logger and a logrus logger. There's no standard
|
||||||
|
// interface, this is the closest we get, unfortunately.
|
||||||
|
type StdLogger interface {
|
||||||
|
Print(...interface{})
|
||||||
|
Printf(string, ...interface{})
|
||||||
|
Println(...interface{})
|
||||||
|
|
||||||
|
Fatal(...interface{})
|
||||||
|
Fatalf(string, ...interface{})
|
||||||
|
Fatalln(...interface{})
|
||||||
|
|
||||||
|
Panic(...interface{})
|
||||||
|
Panicf(string, ...interface{})
|
||||||
|
Panicln(...interface{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// The FieldLogger interface generalizes the Entry and Logger types
|
||||||
|
type FieldLogger interface {
|
||||||
|
WithField(key string, value interface{}) *Entry
|
||||||
|
WithFields(fields Fields) *Entry
|
||||||
|
WithError(err error) *Entry
|
||||||
|
|
||||||
|
Debugf(format string, args ...interface{})
|
||||||
|
Infof(format string, args ...interface{})
|
||||||
|
Printf(format string, args ...interface{})
|
||||||
|
Warnf(format string, args ...interface{})
|
||||||
|
Warningf(format string, args ...interface{})
|
||||||
|
Errorf(format string, args ...interface{})
|
||||||
|
Fatalf(format string, args ...interface{})
|
||||||
|
Panicf(format string, args ...interface{})
|
||||||
|
|
||||||
|
Debug(args ...interface{})
|
||||||
|
Info(args ...interface{})
|
||||||
|
Print(args ...interface{})
|
||||||
|
Warn(args ...interface{})
|
||||||
|
Warning(args ...interface{})
|
||||||
|
Error(args ...interface{})
|
||||||
|
Fatal(args ...interface{})
|
||||||
|
Panic(args ...interface{})
|
||||||
|
|
||||||
|
Debugln(args ...interface{})
|
||||||
|
Infoln(args ...interface{})
|
||||||
|
Println(args ...interface{})
|
||||||
|
Warnln(args ...interface{})
|
||||||
|
Warningln(args ...interface{})
|
||||||
|
Errorln(args ...interface{})
|
||||||
|
Fatalln(args ...interface{})
|
||||||
|
Panicln(args ...interface{})
|
||||||
|
|
||||||
|
// IsDebugEnabled() bool
|
||||||
|
// IsInfoEnabled() bool
|
||||||
|
// IsWarnEnabled() bool
|
||||||
|
// IsErrorEnabled() bool
|
||||||
|
// IsFatalEnabled() bool
|
||||||
|
// IsPanicEnabled() bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ext1FieldLogger (the first extension to FieldLogger) is superfluous, it is
|
||||||
|
// here for consistancy. Do not use. Use Logger or Entry instead.
|
||||||
|
type Ext1FieldLogger interface {
|
||||||
|
FieldLogger
|
||||||
|
Tracef(format string, args ...interface{})
|
||||||
|
Trace(args ...interface{})
|
||||||
|
Traceln(args ...interface{})
|
||||||
|
}
|
11
vendor/github.com/sirupsen/logrus/terminal_check_appengine.go
generated
vendored
Normal file
11
vendor/github.com/sirupsen/logrus/terminal_check_appengine.go
generated
vendored
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
// +build appengine
|
||||||
|
|
||||||
|
package logrus
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
func checkIfTerminal(w io.Writer) bool {
|
||||||
|
return true
|
||||||
|
}
|
13
vendor/github.com/sirupsen/logrus/terminal_check_bsd.go
generated
vendored
Normal file
13
vendor/github.com/sirupsen/logrus/terminal_check_bsd.go
generated
vendored
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
// +build darwin dragonfly freebsd netbsd openbsd
|
||||||
|
// +build !js
|
||||||
|
|
||||||
|
package logrus
|
||||||
|
|
||||||
|
import "golang.org/x/sys/unix"
|
||||||
|
|
||||||
|
const ioctlReadTermios = unix.TIOCGETA
|
||||||
|
|
||||||
|
func isTerminal(fd int) bool {
|
||||||
|
_, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
|
||||||
|
return err == nil
|
||||||
|
}
|
7
vendor/github.com/sirupsen/logrus/terminal_check_js.go
generated
vendored
Normal file
7
vendor/github.com/sirupsen/logrus/terminal_check_js.go
generated
vendored
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
// +build js
|
||||||
|
|
||||||
|
package logrus
|
||||||
|
|
||||||
|
func isTerminal(fd int) bool {
|
||||||
|
return false
|
||||||
|
}
|
11
vendor/github.com/sirupsen/logrus/terminal_check_no_terminal.go
generated
vendored
Normal file
11
vendor/github.com/sirupsen/logrus/terminal_check_no_terminal.go
generated
vendored
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
// +build js nacl plan9
|
||||||
|
|
||||||
|
package logrus
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
func checkIfTerminal(w io.Writer) bool {
|
||||||
|
return false
|
||||||
|
}
|
17
vendor/github.com/sirupsen/logrus/terminal_check_notappengine.go
generated
vendored
Normal file
17
vendor/github.com/sirupsen/logrus/terminal_check_notappengine.go
generated
vendored
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
// +build !appengine,!js,!windows,!nacl,!plan9
|
||||||
|
|
||||||
|
package logrus
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func checkIfTerminal(w io.Writer) bool {
|
||||||
|
switch v := w.(type) {
|
||||||
|
case *os.File:
|
||||||
|
return isTerminal(int(v.Fd()))
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
11
vendor/github.com/sirupsen/logrus/terminal_check_solaris.go
generated
vendored
Normal file
11
vendor/github.com/sirupsen/logrus/terminal_check_solaris.go
generated
vendored
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
package logrus
|
||||||
|
|
||||||
|
import (
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
// IsTerminal returns true if the given file descriptor is a terminal.
|
||||||
|
func isTerminal(fd int) bool {
|
||||||
|
_, err := unix.IoctlGetTermio(fd, unix.TCGETA)
|
||||||
|
return err == nil
|
||||||
|
}
|
13
vendor/github.com/sirupsen/logrus/terminal_check_unix.go
generated
vendored
Normal file
13
vendor/github.com/sirupsen/logrus/terminal_check_unix.go
generated
vendored
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
// +build linux aix zos
|
||||||
|
// +build !js
|
||||||
|
|
||||||
|
package logrus
|
||||||
|
|
||||||
|
import "golang.org/x/sys/unix"
|
||||||
|
|
||||||
|
const ioctlReadTermios = unix.TCGETS
|
||||||
|
|
||||||
|
func isTerminal(fd int) bool {
|
||||||
|
_, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
|
||||||
|
return err == nil
|
||||||
|
}
|
27
vendor/github.com/sirupsen/logrus/terminal_check_windows.go
generated
vendored
Normal file
27
vendor/github.com/sirupsen/logrus/terminal_check_windows.go
generated
vendored
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
// +build !appengine,!js,windows
|
||||||
|
|
||||||
|
package logrus
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"golang.org/x/sys/windows"
|
||||||
|
)
|
||||||
|
|
||||||
|
func checkIfTerminal(w io.Writer) bool {
|
||||||
|
switch v := w.(type) {
|
||||||
|
case *os.File:
|
||||||
|
handle := windows.Handle(v.Fd())
|
||||||
|
var mode uint32
|
||||||
|
if err := windows.GetConsoleMode(handle, &mode); err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
mode |= windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING
|
||||||
|
if err := windows.SetConsoleMode(handle, mode); err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
339
vendor/github.com/sirupsen/logrus/text_formatter.go
generated
vendored
Normal file
339
vendor/github.com/sirupsen/logrus/text_formatter.go
generated
vendored
Normal file
|
@ -0,0 +1,339 @@
|
||||||
|
package logrus
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"runtime"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
"unicode/utf8"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
red = 31
|
||||||
|
yellow = 33
|
||||||
|
blue = 36
|
||||||
|
gray = 37
|
||||||
|
)
|
||||||
|
|
||||||
|
var baseTimestamp time.Time
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
baseTimestamp = time.Now()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TextFormatter formats logs into text
|
||||||
|
type TextFormatter struct {
|
||||||
|
// Set to true to bypass checking for a TTY before outputting colors.
|
||||||
|
ForceColors bool
|
||||||
|
|
||||||
|
// Force disabling colors.
|
||||||
|
DisableColors bool
|
||||||
|
|
||||||
|
// Force quoting of all values
|
||||||
|
ForceQuote bool
|
||||||
|
|
||||||
|
// DisableQuote disables quoting for all values.
|
||||||
|
// DisableQuote will have a lower priority than ForceQuote.
|
||||||
|
// If both of them are set to true, quote will be forced on all values.
|
||||||
|
DisableQuote bool
|
||||||
|
|
||||||
|
// Override coloring based on CLICOLOR and CLICOLOR_FORCE. - https://bixense.com/clicolors/
|
||||||
|
EnvironmentOverrideColors bool
|
||||||
|
|
||||||
|
// Disable timestamp logging. useful when output is redirected to logging
|
||||||
|
// system that already adds timestamps.
|
||||||
|
DisableTimestamp bool
|
||||||
|
|
||||||
|
// Enable logging the full timestamp when a TTY is attached instead of just
|
||||||
|
// the time passed since beginning of execution.
|
||||||
|
FullTimestamp bool
|
||||||
|
|
||||||
|
// TimestampFormat to use for display when a full timestamp is printed.
|
||||||
|
// The format to use is the same than for time.Format or time.Parse from the standard
|
||||||
|
// library.
|
||||||
|
// The standard Library already provides a set of predefined format.
|
||||||
|
TimestampFormat string
|
||||||
|
|
||||||
|
// The fields are sorted by default for a consistent output. For applications
|
||||||
|
// that log extremely frequently and don't use the JSON formatter this may not
|
||||||
|
// be desired.
|
||||||
|
DisableSorting bool
|
||||||
|
|
||||||
|
// The keys sorting function, when uninitialized it uses sort.Strings.
|
||||||
|
SortingFunc func([]string)
|
||||||
|
|
||||||
|
// Disables the truncation of the level text to 4 characters.
|
||||||
|
DisableLevelTruncation bool
|
||||||
|
|
||||||
|
// PadLevelText Adds padding the level text so that all the levels output at the same length
|
||||||
|
// PadLevelText is a superset of the DisableLevelTruncation option
|
||||||
|
PadLevelText bool
|
||||||
|
|
||||||
|
// QuoteEmptyFields will wrap empty fields in quotes if true
|
||||||
|
QuoteEmptyFields bool
|
||||||
|
|
||||||
|
// Whether the logger's out is to a terminal
|
||||||
|
isTerminal bool
|
||||||
|
|
||||||
|
// FieldMap allows users to customize the names of keys for default fields.
|
||||||
|
// As an example:
|
||||||
|
// formatter := &TextFormatter{
|
||||||
|
// FieldMap: FieldMap{
|
||||||
|
// FieldKeyTime: "@timestamp",
|
||||||
|
// FieldKeyLevel: "@level",
|
||||||
|
// FieldKeyMsg: "@message"}}
|
||||||
|
FieldMap FieldMap
|
||||||
|
|
||||||
|
// CallerPrettyfier can be set by the user to modify the content
|
||||||
|
// of the function and file keys in the data when ReportCaller is
|
||||||
|
// activated. If any of the returned value is the empty string the
|
||||||
|
// corresponding key will be removed from fields.
|
||||||
|
CallerPrettyfier func(*runtime.Frame) (function string, file string)
|
||||||
|
|
||||||
|
terminalInitOnce sync.Once
|
||||||
|
|
||||||
|
// The max length of the level text, generated dynamically on init
|
||||||
|
levelTextMaxLength int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *TextFormatter) init(entry *Entry) {
|
||||||
|
if entry.Logger != nil {
|
||||||
|
f.isTerminal = checkIfTerminal(entry.Logger.Out)
|
||||||
|
}
|
||||||
|
// Get the max length of the level text
|
||||||
|
for _, level := range AllLevels {
|
||||||
|
levelTextLength := utf8.RuneCount([]byte(level.String()))
|
||||||
|
if levelTextLength > f.levelTextMaxLength {
|
||||||
|
f.levelTextMaxLength = levelTextLength
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *TextFormatter) isColored() bool {
|
||||||
|
isColored := f.ForceColors || (f.isTerminal && (runtime.GOOS != "windows"))
|
||||||
|
|
||||||
|
if f.EnvironmentOverrideColors {
|
||||||
|
switch force, ok := os.LookupEnv("CLICOLOR_FORCE"); {
|
||||||
|
case ok && force != "0":
|
||||||
|
isColored = true
|
||||||
|
case ok && force == "0", os.Getenv("CLICOLOR") == "0":
|
||||||
|
isColored = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return isColored && !f.DisableColors
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format renders a single log entry
|
||||||
|
func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
|
||||||
|
data := make(Fields)
|
||||||
|
for k, v := range entry.Data {
|
||||||
|
data[k] = v
|
||||||
|
}
|
||||||
|
prefixFieldClashes(data, f.FieldMap, entry.HasCaller())
|
||||||
|
keys := make([]string, 0, len(data))
|
||||||
|
for k := range data {
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
|
|
||||||
|
var funcVal, fileVal string
|
||||||
|
|
||||||
|
fixedKeys := make([]string, 0, 4+len(data))
|
||||||
|
if !f.DisableTimestamp {
|
||||||
|
fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyTime))
|
||||||
|
}
|
||||||
|
fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyLevel))
|
||||||
|
if entry.Message != "" {
|
||||||
|
fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyMsg))
|
||||||
|
}
|
||||||
|
if entry.err != "" {
|
||||||
|
fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyLogrusError))
|
||||||
|
}
|
||||||
|
if entry.HasCaller() {
|
||||||
|
if f.CallerPrettyfier != nil {
|
||||||
|
funcVal, fileVal = f.CallerPrettyfier(entry.Caller)
|
||||||
|
} else {
|
||||||
|
funcVal = entry.Caller.Function
|
||||||
|
fileVal = fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line)
|
||||||
|
}
|
||||||
|
|
||||||
|
if funcVal != "" {
|
||||||
|
fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyFunc))
|
||||||
|
}
|
||||||
|
if fileVal != "" {
|
||||||
|
fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyFile))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !f.DisableSorting {
|
||||||
|
if f.SortingFunc == nil {
|
||||||
|
sort.Strings(keys)
|
||||||
|
fixedKeys = append(fixedKeys, keys...)
|
||||||
|
} else {
|
||||||
|
if !f.isColored() {
|
||||||
|
fixedKeys = append(fixedKeys, keys...)
|
||||||
|
f.SortingFunc(fixedKeys)
|
||||||
|
} else {
|
||||||
|
f.SortingFunc(keys)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fixedKeys = append(fixedKeys, keys...)
|
||||||
|
}
|
||||||
|
|
||||||
|
var b *bytes.Buffer
|
||||||
|
if entry.Buffer != nil {
|
||||||
|
b = entry.Buffer
|
||||||
|
} else {
|
||||||
|
b = &bytes.Buffer{}
|
||||||
|
}
|
||||||
|
|
||||||
|
f.terminalInitOnce.Do(func() { f.init(entry) })
|
||||||
|
|
||||||
|
timestampFormat := f.TimestampFormat
|
||||||
|
if timestampFormat == "" {
|
||||||
|
timestampFormat = defaultTimestampFormat
|
||||||
|
}
|
||||||
|
if f.isColored() {
|
||||||
|
f.printColored(b, entry, keys, data, timestampFormat)
|
||||||
|
} else {
|
||||||
|
|
||||||
|
for _, key := range fixedKeys {
|
||||||
|
var value interface{}
|
||||||
|
switch {
|
||||||
|
case key == f.FieldMap.resolve(FieldKeyTime):
|
||||||
|
value = entry.Time.Format(timestampFormat)
|
||||||
|
case key == f.FieldMap.resolve(FieldKeyLevel):
|
||||||
|
value = entry.Level.String()
|
||||||
|
case key == f.FieldMap.resolve(FieldKeyMsg):
|
||||||
|
value = entry.Message
|
||||||
|
case key == f.FieldMap.resolve(FieldKeyLogrusError):
|
||||||
|
value = entry.err
|
||||||
|
case key == f.FieldMap.resolve(FieldKeyFunc) && entry.HasCaller():
|
||||||
|
value = funcVal
|
||||||
|
case key == f.FieldMap.resolve(FieldKeyFile) && entry.HasCaller():
|
||||||
|
value = fileVal
|
||||||
|
default:
|
||||||
|
value = data[key]
|
||||||
|
}
|
||||||
|
f.appendKeyValue(b, key, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
b.WriteByte('\n')
|
||||||
|
return b.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []string, data Fields, timestampFormat string) {
|
||||||
|
var levelColor int
|
||||||
|
switch entry.Level {
|
||||||
|
case DebugLevel, TraceLevel:
|
||||||
|
levelColor = gray
|
||||||
|
case WarnLevel:
|
||||||
|
levelColor = yellow
|
||||||
|
case ErrorLevel, FatalLevel, PanicLevel:
|
||||||
|
levelColor = red
|
||||||
|
case InfoLevel:
|
||||||
|
levelColor = blue
|
||||||
|
default:
|
||||||
|
levelColor = blue
|
||||||
|
}
|
||||||
|
|
||||||
|
levelText := strings.ToUpper(entry.Level.String())
|
||||||
|
if !f.DisableLevelTruncation && !f.PadLevelText {
|
||||||
|
levelText = levelText[0:4]
|
||||||
|
}
|
||||||
|
if f.PadLevelText {
|
||||||
|
// Generates the format string used in the next line, for example "%-6s" or "%-7s".
|
||||||
|
// Based on the max level text length.
|
||||||
|
formatString := "%-" + strconv.Itoa(f.levelTextMaxLength) + "s"
|
||||||
|
// Formats the level text by appending spaces up to the max length, for example:
|
||||||
|
// - "INFO "
|
||||||
|
// - "WARNING"
|
||||||
|
levelText = fmt.Sprintf(formatString, levelText)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove a single newline if it already exists in the message to keep
|
||||||
|
// the behavior of logrus text_formatter the same as the stdlib log package
|
||||||
|
entry.Message = strings.TrimSuffix(entry.Message, "\n")
|
||||||
|
|
||||||
|
caller := ""
|
||||||
|
if entry.HasCaller() {
|
||||||
|
funcVal := fmt.Sprintf("%s()", entry.Caller.Function)
|
||||||
|
fileVal := fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line)
|
||||||
|
|
||||||
|
if f.CallerPrettyfier != nil {
|
||||||
|
funcVal, fileVal = f.CallerPrettyfier(entry.Caller)
|
||||||
|
}
|
||||||
|
|
||||||
|
if fileVal == "" {
|
||||||
|
caller = funcVal
|
||||||
|
} else if funcVal == "" {
|
||||||
|
caller = fileVal
|
||||||
|
} else {
|
||||||
|
caller = fileVal + " " + funcVal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case f.DisableTimestamp:
|
||||||
|
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m%s %-44s ", levelColor, levelText, caller, entry.Message)
|
||||||
|
case !f.FullTimestamp:
|
||||||
|
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%04d]%s %-44s ", levelColor, levelText, int(entry.Time.Sub(baseTimestamp)/time.Second), caller, entry.Message)
|
||||||
|
default:
|
||||||
|
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s]%s %-44s ", levelColor, levelText, entry.Time.Format(timestampFormat), caller, entry.Message)
|
||||||
|
}
|
||||||
|
for _, k := range keys {
|
||||||
|
v := data[k]
|
||||||
|
fmt.Fprintf(b, " \x1b[%dm%s\x1b[0m=", levelColor, k)
|
||||||
|
f.appendValue(b, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *TextFormatter) needsQuoting(text string) bool {
|
||||||
|
if f.ForceQuote {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if f.QuoteEmptyFields && len(text) == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if f.DisableQuote {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, ch := range text {
|
||||||
|
if !((ch >= 'a' && ch <= 'z') ||
|
||||||
|
(ch >= 'A' && ch <= 'Z') ||
|
||||||
|
(ch >= '0' && ch <= '9') ||
|
||||||
|
ch == '-' || ch == '.' || ch == '_' || ch == '/' || ch == '@' || ch == '^' || ch == '+') {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *TextFormatter) appendKeyValue(b *bytes.Buffer, key string, value interface{}) {
|
||||||
|
if b.Len() > 0 {
|
||||||
|
b.WriteByte(' ')
|
||||||
|
}
|
||||||
|
b.WriteString(key)
|
||||||
|
b.WriteByte('=')
|
||||||
|
f.appendValue(b, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *TextFormatter) appendValue(b *bytes.Buffer, value interface{}) {
|
||||||
|
stringVal, ok := value.(string)
|
||||||
|
if !ok {
|
||||||
|
stringVal = fmt.Sprint(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !f.needsQuoting(stringVal) {
|
||||||
|
b.WriteString(stringVal)
|
||||||
|
} else {
|
||||||
|
b.WriteString(fmt.Sprintf("%q", stringVal))
|
||||||
|
}
|
||||||
|
}
|
102
vendor/github.com/sirupsen/logrus/writer.go
generated
vendored
Normal file
102
vendor/github.com/sirupsen/logrus/writer.go
generated
vendored
Normal file
|
@ -0,0 +1,102 @@
|
||||||
|
package logrus
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"io"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Writer at INFO level. See WriterLevel for details.
|
||||||
|
func (logger *Logger) Writer() *io.PipeWriter {
|
||||||
|
return logger.WriterLevel(InfoLevel)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriterLevel returns an io.Writer that can be used to write arbitrary text to
|
||||||
|
// the logger at the given log level. Each line written to the writer will be
|
||||||
|
// printed in the usual way using formatters and hooks. The writer is part of an
|
||||||
|
// io.Pipe and it is the callers responsibility to close the writer when done.
|
||||||
|
// This can be used to override the standard library logger easily.
|
||||||
|
func (logger *Logger) WriterLevel(level Level) *io.PipeWriter {
|
||||||
|
return NewEntry(logger).WriterLevel(level)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Writer returns an io.Writer that writes to the logger at the info log level
|
||||||
|
func (entry *Entry) Writer() *io.PipeWriter {
|
||||||
|
return entry.WriterLevel(InfoLevel)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriterLevel returns an io.Writer that writes to the logger at the given log level
|
||||||
|
func (entry *Entry) WriterLevel(level Level) *io.PipeWriter {
|
||||||
|
reader, writer := io.Pipe()
|
||||||
|
|
||||||
|
var printFunc func(args ...interface{})
|
||||||
|
|
||||||
|
// Determine which log function to use based on the specified log level
|
||||||
|
switch level {
|
||||||
|
case TraceLevel:
|
||||||
|
printFunc = entry.Trace
|
||||||
|
case DebugLevel:
|
||||||
|
printFunc = entry.Debug
|
||||||
|
case InfoLevel:
|
||||||
|
printFunc = entry.Info
|
||||||
|
case WarnLevel:
|
||||||
|
printFunc = entry.Warn
|
||||||
|
case ErrorLevel:
|
||||||
|
printFunc = entry.Error
|
||||||
|
case FatalLevel:
|
||||||
|
printFunc = entry.Fatal
|
||||||
|
case PanicLevel:
|
||||||
|
printFunc = entry.Panic
|
||||||
|
default:
|
||||||
|
printFunc = entry.Print
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start a new goroutine to scan the input and write it to the logger using the specified print function.
|
||||||
|
// It splits the input into chunks of up to 64KB to avoid buffer overflows.
|
||||||
|
go entry.writerScanner(reader, printFunc)
|
||||||
|
|
||||||
|
// Set a finalizer function to close the writer when it is garbage collected
|
||||||
|
runtime.SetFinalizer(writer, writerFinalizer)
|
||||||
|
|
||||||
|
return writer
|
||||||
|
}
|
||||||
|
|
||||||
|
// writerScanner scans the input from the reader and writes it to the logger
|
||||||
|
func (entry *Entry) writerScanner(reader *io.PipeReader, printFunc func(args ...interface{})) {
|
||||||
|
scanner := bufio.NewScanner(reader)
|
||||||
|
|
||||||
|
// Set the buffer size to the maximum token size to avoid buffer overflows
|
||||||
|
scanner.Buffer(make([]byte, bufio.MaxScanTokenSize), bufio.MaxScanTokenSize)
|
||||||
|
|
||||||
|
// Define a split function to split the input into chunks of up to 64KB
|
||||||
|
chunkSize := bufio.MaxScanTokenSize // 64KB
|
||||||
|
splitFunc := func(data []byte, atEOF bool) (int, []byte, error) {
|
||||||
|
if len(data) >= chunkSize {
|
||||||
|
return chunkSize, data[:chunkSize], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return bufio.ScanLines(data, atEOF)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use the custom split function to split the input
|
||||||
|
scanner.Split(splitFunc)
|
||||||
|
|
||||||
|
// Scan the input and write it to the logger using the specified print function
|
||||||
|
for scanner.Scan() {
|
||||||
|
printFunc(strings.TrimRight(scanner.Text(), "\r\n"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there was an error while scanning the input, log an error
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
entry.Errorf("Error while reading from Writer: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close the reader when we are done
|
||||||
|
reader.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriterFinalizer is a finalizer function that closes then given writer when it is garbage collected
|
||||||
|
func writerFinalizer(writer *io.PipeWriter) {
|
||||||
|
writer.Close()
|
||||||
|
}
|
27
vendor/golang.org/x/crypto/LICENSE
generated
vendored
Normal file
27
vendor/golang.org/x/crypto/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
Copyright 2009 The Go Authors.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are
|
||||||
|
met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer.
|
||||||
|
* 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.
|
||||||
|
* Neither the name of Google LLC 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
|
||||||
|
OWNER 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.
|
22
vendor/golang.org/x/crypto/PATENTS
generated
vendored
Normal file
22
vendor/golang.org/x/crypto/PATENTS
generated
vendored
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
Additional IP Rights Grant (Patents)
|
||||||
|
|
||||||
|
"This implementation" means the copyrightable works distributed by
|
||||||
|
Google as part of the Go project.
|
||||||
|
|
||||||
|
Google hereby grants to You a perpetual, worldwide, non-exclusive,
|
||||||
|
no-charge, royalty-free, irrevocable (except as stated in this section)
|
||||||
|
patent license to make, have made, use, offer to sell, sell, import,
|
||||||
|
transfer and otherwise run, modify and propagate the contents of this
|
||||||
|
implementation of Go, where such license applies only to those patent
|
||||||
|
claims, both currently owned or controlled by Google and acquired in
|
||||||
|
the future, licensable by Google that are necessarily infringed by this
|
||||||
|
implementation of Go. This grant does not include claims that would be
|
||||||
|
infringed only as a consequence of further modification of this
|
||||||
|
implementation. If you or your agent or exclusive licensee institute or
|
||||||
|
order or agree to the institution of patent litigation against any
|
||||||
|
entity (including a cross-claim or counterclaim in a lawsuit) alleging
|
||||||
|
that this implementation of Go or any code incorporated within this
|
||||||
|
implementation of Go constitutes direct or contributory patent
|
||||||
|
infringement, or inducement of patent infringement, then any patent
|
||||||
|
rights granted to you under this License for this implementation of Go
|
||||||
|
shall terminate as of the date such litigation is filed.
|
35
vendor/golang.org/x/crypto/bcrypt/base64.go
generated
vendored
Normal file
35
vendor/golang.org/x/crypto/bcrypt/base64.go
generated
vendored
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
// Copyright 2011 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.
|
||||||
|
|
||||||
|
package bcrypt
|
||||||
|
|
||||||
|
import "encoding/base64"
|
||||||
|
|
||||||
|
const alphabet = "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
||||||
|
|
||||||
|
var bcEncoding = base64.NewEncoding(alphabet)
|
||||||
|
|
||||||
|
func base64Encode(src []byte) []byte {
|
||||||
|
n := bcEncoding.EncodedLen(len(src))
|
||||||
|
dst := make([]byte, n)
|
||||||
|
bcEncoding.Encode(dst, src)
|
||||||
|
for dst[n-1] == '=' {
|
||||||
|
n--
|
||||||
|
}
|
||||||
|
return dst[:n]
|
||||||
|
}
|
||||||
|
|
||||||
|
func base64Decode(src []byte) ([]byte, error) {
|
||||||
|
numOfEquals := 4 - (len(src) % 4)
|
||||||
|
for i := 0; i < numOfEquals; i++ {
|
||||||
|
src = append(src, '=')
|
||||||
|
}
|
||||||
|
|
||||||
|
dst := make([]byte, bcEncoding.DecodedLen(len(src)))
|
||||||
|
n, err := bcEncoding.Decode(dst, src)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return dst[:n], nil
|
||||||
|
}
|
304
vendor/golang.org/x/crypto/bcrypt/bcrypt.go
generated
vendored
Normal file
304
vendor/golang.org/x/crypto/bcrypt/bcrypt.go
generated
vendored
Normal file
|
@ -0,0 +1,304 @@
|
||||||
|
// Copyright 2011 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.
|
||||||
|
|
||||||
|
// Package bcrypt implements Provos and Mazières's bcrypt adaptive hashing
|
||||||
|
// algorithm. See http://www.usenix.org/event/usenix99/provos/provos.pdf
|
||||||
|
package bcrypt
|
||||||
|
|
||||||
|
// The code is a port of Provos and Mazières's C implementation.
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/subtle"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/blowfish"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
MinCost int = 4 // the minimum allowable cost as passed in to GenerateFromPassword
|
||||||
|
MaxCost int = 31 // the maximum allowable cost as passed in to GenerateFromPassword
|
||||||
|
DefaultCost int = 10 // the cost that will actually be set if a cost below MinCost is passed into GenerateFromPassword
|
||||||
|
)
|
||||||
|
|
||||||
|
// The error returned from CompareHashAndPassword when a password and hash do
|
||||||
|
// not match.
|
||||||
|
var ErrMismatchedHashAndPassword = errors.New("crypto/bcrypt: hashedPassword is not the hash of the given password")
|
||||||
|
|
||||||
|
// The error returned from CompareHashAndPassword when a hash is too short to
|
||||||
|
// be a bcrypt hash.
|
||||||
|
var ErrHashTooShort = errors.New("crypto/bcrypt: hashedSecret too short to be a bcrypted password")
|
||||||
|
|
||||||
|
// The error returned from CompareHashAndPassword when a hash was created with
|
||||||
|
// a bcrypt algorithm newer than this implementation.
|
||||||
|
type HashVersionTooNewError byte
|
||||||
|
|
||||||
|
func (hv HashVersionTooNewError) Error() string {
|
||||||
|
return fmt.Sprintf("crypto/bcrypt: bcrypt algorithm version '%c' requested is newer than current version '%c'", byte(hv), majorVersion)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The error returned from CompareHashAndPassword when a hash starts with something other than '$'
|
||||||
|
type InvalidHashPrefixError byte
|
||||||
|
|
||||||
|
func (ih InvalidHashPrefixError) Error() string {
|
||||||
|
return fmt.Sprintf("crypto/bcrypt: bcrypt hashes must start with '$', but hashedSecret started with '%c'", byte(ih))
|
||||||
|
}
|
||||||
|
|
||||||
|
type InvalidCostError int
|
||||||
|
|
||||||
|
func (ic InvalidCostError) Error() string {
|
||||||
|
return fmt.Sprintf("crypto/bcrypt: cost %d is outside allowed range (%d,%d)", int(ic), MinCost, MaxCost)
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
majorVersion = '2'
|
||||||
|
minorVersion = 'a'
|
||||||
|
maxSaltSize = 16
|
||||||
|
maxCryptedHashSize = 23
|
||||||
|
encodedSaltSize = 22
|
||||||
|
encodedHashSize = 31
|
||||||
|
minHashSize = 59
|
||||||
|
)
|
||||||
|
|
||||||
|
// magicCipherData is an IV for the 64 Blowfish encryption calls in
|
||||||
|
// bcrypt(). It's the string "OrpheanBeholderScryDoubt" in big-endian bytes.
|
||||||
|
var magicCipherData = []byte{
|
||||||
|
0x4f, 0x72, 0x70, 0x68,
|
||||||
|
0x65, 0x61, 0x6e, 0x42,
|
||||||
|
0x65, 0x68, 0x6f, 0x6c,
|
||||||
|
0x64, 0x65, 0x72, 0x53,
|
||||||
|
0x63, 0x72, 0x79, 0x44,
|
||||||
|
0x6f, 0x75, 0x62, 0x74,
|
||||||
|
}
|
||||||
|
|
||||||
|
type hashed struct {
|
||||||
|
hash []byte
|
||||||
|
salt []byte
|
||||||
|
cost int // allowed range is MinCost to MaxCost
|
||||||
|
major byte
|
||||||
|
minor byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// ErrPasswordTooLong is returned when the password passed to
|
||||||
|
// GenerateFromPassword is too long (i.e. > 72 bytes).
|
||||||
|
var ErrPasswordTooLong = errors.New("bcrypt: password length exceeds 72 bytes")
|
||||||
|
|
||||||
|
// GenerateFromPassword returns the bcrypt hash of the password at the given
|
||||||
|
// cost. If the cost given is less than MinCost, the cost will be set to
|
||||||
|
// DefaultCost, instead. Use CompareHashAndPassword, as defined in this package,
|
||||||
|
// to compare the returned hashed password with its cleartext version.
|
||||||
|
// GenerateFromPassword does not accept passwords longer than 72 bytes, which
|
||||||
|
// is the longest password bcrypt will operate on.
|
||||||
|
func GenerateFromPassword(password []byte, cost int) ([]byte, error) {
|
||||||
|
if len(password) > 72 {
|
||||||
|
return nil, ErrPasswordTooLong
|
||||||
|
}
|
||||||
|
p, err := newFromPassword(password, cost)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return p.Hash(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CompareHashAndPassword compares a bcrypt hashed password with its possible
|
||||||
|
// plaintext equivalent. Returns nil on success, or an error on failure.
|
||||||
|
func CompareHashAndPassword(hashedPassword, password []byte) error {
|
||||||
|
p, err := newFromHash(hashedPassword)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
otherHash, err := bcrypt(password, p.cost, p.salt)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
otherP := &hashed{otherHash, p.salt, p.cost, p.major, p.minor}
|
||||||
|
if subtle.ConstantTimeCompare(p.Hash(), otherP.Hash()) == 1 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return ErrMismatchedHashAndPassword
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cost returns the hashing cost used to create the given hashed
|
||||||
|
// password. When, in the future, the hashing cost of a password system needs
|
||||||
|
// to be increased in order to adjust for greater computational power, this
|
||||||
|
// function allows one to establish which passwords need to be updated.
|
||||||
|
func Cost(hashedPassword []byte) (int, error) {
|
||||||
|
p, err := newFromHash(hashedPassword)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return p.cost, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func newFromPassword(password []byte, cost int) (*hashed, error) {
|
||||||
|
if cost < MinCost {
|
||||||
|
cost = DefaultCost
|
||||||
|
}
|
||||||
|
p := new(hashed)
|
||||||
|
p.major = majorVersion
|
||||||
|
p.minor = minorVersion
|
||||||
|
|
||||||
|
err := checkCost(cost)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
p.cost = cost
|
||||||
|
|
||||||
|
unencodedSalt := make([]byte, maxSaltSize)
|
||||||
|
_, err = io.ReadFull(rand.Reader, unencodedSalt)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
p.salt = base64Encode(unencodedSalt)
|
||||||
|
hash, err := bcrypt(password, p.cost, p.salt)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
p.hash = hash
|
||||||
|
return p, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func newFromHash(hashedSecret []byte) (*hashed, error) {
|
||||||
|
if len(hashedSecret) < minHashSize {
|
||||||
|
return nil, ErrHashTooShort
|
||||||
|
}
|
||||||
|
p := new(hashed)
|
||||||
|
n, err := p.decodeVersion(hashedSecret)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
hashedSecret = hashedSecret[n:]
|
||||||
|
n, err = p.decodeCost(hashedSecret)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
hashedSecret = hashedSecret[n:]
|
||||||
|
|
||||||
|
// The "+2" is here because we'll have to append at most 2 '=' to the salt
|
||||||
|
// when base64 decoding it in expensiveBlowfishSetup().
|
||||||
|
p.salt = make([]byte, encodedSaltSize, encodedSaltSize+2)
|
||||||
|
copy(p.salt, hashedSecret[:encodedSaltSize])
|
||||||
|
|
||||||
|
hashedSecret = hashedSecret[encodedSaltSize:]
|
||||||
|
p.hash = make([]byte, len(hashedSecret))
|
||||||
|
copy(p.hash, hashedSecret)
|
||||||
|
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func bcrypt(password []byte, cost int, salt []byte) ([]byte, error) {
|
||||||
|
cipherData := make([]byte, len(magicCipherData))
|
||||||
|
copy(cipherData, magicCipherData)
|
||||||
|
|
||||||
|
c, err := expensiveBlowfishSetup(password, uint32(cost), salt)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 24; i += 8 {
|
||||||
|
for j := 0; j < 64; j++ {
|
||||||
|
c.Encrypt(cipherData[i:i+8], cipherData[i:i+8])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bug compatibility with C bcrypt implementations. We only encode 23 of
|
||||||
|
// the 24 bytes encrypted.
|
||||||
|
hsh := base64Encode(cipherData[:maxCryptedHashSize])
|
||||||
|
return hsh, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func expensiveBlowfishSetup(key []byte, cost uint32, salt []byte) (*blowfish.Cipher, error) {
|
||||||
|
csalt, err := base64Decode(salt)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bug compatibility with C bcrypt implementations. They use the trailing
|
||||||
|
// NULL in the key string during expansion.
|
||||||
|
// We copy the key to prevent changing the underlying array.
|
||||||
|
ckey := append(key[:len(key):len(key)], 0)
|
||||||
|
|
||||||
|
c, err := blowfish.NewSaltedCipher(ckey, csalt)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var i, rounds uint64
|
||||||
|
rounds = 1 << cost
|
||||||
|
for i = 0; i < rounds; i++ {
|
||||||
|
blowfish.ExpandKey(ckey, c)
|
||||||
|
blowfish.ExpandKey(csalt, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *hashed) Hash() []byte {
|
||||||
|
arr := make([]byte, 60)
|
||||||
|
arr[0] = '$'
|
||||||
|
arr[1] = p.major
|
||||||
|
n := 2
|
||||||
|
if p.minor != 0 {
|
||||||
|
arr[2] = p.minor
|
||||||
|
n = 3
|
||||||
|
}
|
||||||
|
arr[n] = '$'
|
||||||
|
n++
|
||||||
|
copy(arr[n:], []byte(fmt.Sprintf("%02d", p.cost)))
|
||||||
|
n += 2
|
||||||
|
arr[n] = '$'
|
||||||
|
n++
|
||||||
|
copy(arr[n:], p.salt)
|
||||||
|
n += encodedSaltSize
|
||||||
|
copy(arr[n:], p.hash)
|
||||||
|
n += encodedHashSize
|
||||||
|
return arr[:n]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *hashed) decodeVersion(sbytes []byte) (int, error) {
|
||||||
|
if sbytes[0] != '$' {
|
||||||
|
return -1, InvalidHashPrefixError(sbytes[0])
|
||||||
|
}
|
||||||
|
if sbytes[1] > majorVersion {
|
||||||
|
return -1, HashVersionTooNewError(sbytes[1])
|
||||||
|
}
|
||||||
|
p.major = sbytes[1]
|
||||||
|
n := 3
|
||||||
|
if sbytes[2] != '$' {
|
||||||
|
p.minor = sbytes[2]
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// sbytes should begin where decodeVersion left off.
|
||||||
|
func (p *hashed) decodeCost(sbytes []byte) (int, error) {
|
||||||
|
cost, err := strconv.Atoi(string(sbytes[0:2]))
|
||||||
|
if err != nil {
|
||||||
|
return -1, err
|
||||||
|
}
|
||||||
|
err = checkCost(cost)
|
||||||
|
if err != nil {
|
||||||
|
return -1, err
|
||||||
|
}
|
||||||
|
p.cost = cost
|
||||||
|
return 3, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *hashed) String() string {
|
||||||
|
return fmt.Sprintf("&{hash: %#v, salt: %#v, cost: %d, major: %c, minor: %c}", string(p.hash), p.salt, p.cost, p.major, p.minor)
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkCost(cost int) error {
|
||||||
|
if cost < MinCost || cost > MaxCost {
|
||||||
|
return InvalidCostError(cost)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
159
vendor/golang.org/x/crypto/blowfish/block.go
generated
vendored
Normal file
159
vendor/golang.org/x/crypto/blowfish/block.go
generated
vendored
Normal file
|
@ -0,0 +1,159 @@
|
||||||
|
// Copyright 2010 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.
|
||||||
|
|
||||||
|
package blowfish
|
||||||
|
|
||||||
|
// getNextWord returns the next big-endian uint32 value from the byte slice
|
||||||
|
// at the given position in a circular manner, updating the position.
|
||||||
|
func getNextWord(b []byte, pos *int) uint32 {
|
||||||
|
var w uint32
|
||||||
|
j := *pos
|
||||||
|
for i := 0; i < 4; i++ {
|
||||||
|
w = w<<8 | uint32(b[j])
|
||||||
|
j++
|
||||||
|
if j >= len(b) {
|
||||||
|
j = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*pos = j
|
||||||
|
return w
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExpandKey performs a key expansion on the given *Cipher. Specifically, it
|
||||||
|
// performs the Blowfish algorithm's key schedule which sets up the *Cipher's
|
||||||
|
// pi and substitution tables for calls to Encrypt. This is used, primarily,
|
||||||
|
// by the bcrypt package to reuse the Blowfish key schedule during its
|
||||||
|
// set up. It's unlikely that you need to use this directly.
|
||||||
|
func ExpandKey(key []byte, c *Cipher) {
|
||||||
|
j := 0
|
||||||
|
for i := 0; i < 18; i++ {
|
||||||
|
// Using inlined getNextWord for performance.
|
||||||
|
var d uint32
|
||||||
|
for k := 0; k < 4; k++ {
|
||||||
|
d = d<<8 | uint32(key[j])
|
||||||
|
j++
|
||||||
|
if j >= len(key) {
|
||||||
|
j = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.p[i] ^= d
|
||||||
|
}
|
||||||
|
|
||||||
|
var l, r uint32
|
||||||
|
for i := 0; i < 18; i += 2 {
|
||||||
|
l, r = encryptBlock(l, r, c)
|
||||||
|
c.p[i], c.p[i+1] = l, r
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 256; i += 2 {
|
||||||
|
l, r = encryptBlock(l, r, c)
|
||||||
|
c.s0[i], c.s0[i+1] = l, r
|
||||||
|
}
|
||||||
|
for i := 0; i < 256; i += 2 {
|
||||||
|
l, r = encryptBlock(l, r, c)
|
||||||
|
c.s1[i], c.s1[i+1] = l, r
|
||||||
|
}
|
||||||
|
for i := 0; i < 256; i += 2 {
|
||||||
|
l, r = encryptBlock(l, r, c)
|
||||||
|
c.s2[i], c.s2[i+1] = l, r
|
||||||
|
}
|
||||||
|
for i := 0; i < 256; i += 2 {
|
||||||
|
l, r = encryptBlock(l, r, c)
|
||||||
|
c.s3[i], c.s3[i+1] = l, r
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is similar to ExpandKey, but folds the salt during the key
|
||||||
|
// schedule. While ExpandKey is essentially expandKeyWithSalt with an all-zero
|
||||||
|
// salt passed in, reusing ExpandKey turns out to be a place of inefficiency
|
||||||
|
// and specializing it here is useful.
|
||||||
|
func expandKeyWithSalt(key []byte, salt []byte, c *Cipher) {
|
||||||
|
j := 0
|
||||||
|
for i := 0; i < 18; i++ {
|
||||||
|
c.p[i] ^= getNextWord(key, &j)
|
||||||
|
}
|
||||||
|
|
||||||
|
j = 0
|
||||||
|
var l, r uint32
|
||||||
|
for i := 0; i < 18; i += 2 {
|
||||||
|
l ^= getNextWord(salt, &j)
|
||||||
|
r ^= getNextWord(salt, &j)
|
||||||
|
l, r = encryptBlock(l, r, c)
|
||||||
|
c.p[i], c.p[i+1] = l, r
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 256; i += 2 {
|
||||||
|
l ^= getNextWord(salt, &j)
|
||||||
|
r ^= getNextWord(salt, &j)
|
||||||
|
l, r = encryptBlock(l, r, c)
|
||||||
|
c.s0[i], c.s0[i+1] = l, r
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 256; i += 2 {
|
||||||
|
l ^= getNextWord(salt, &j)
|
||||||
|
r ^= getNextWord(salt, &j)
|
||||||
|
l, r = encryptBlock(l, r, c)
|
||||||
|
c.s1[i], c.s1[i+1] = l, r
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 256; i += 2 {
|
||||||
|
l ^= getNextWord(salt, &j)
|
||||||
|
r ^= getNextWord(salt, &j)
|
||||||
|
l, r = encryptBlock(l, r, c)
|
||||||
|
c.s2[i], c.s2[i+1] = l, r
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 256; i += 2 {
|
||||||
|
l ^= getNextWord(salt, &j)
|
||||||
|
r ^= getNextWord(salt, &j)
|
||||||
|
l, r = encryptBlock(l, r, c)
|
||||||
|
c.s3[i], c.s3[i+1] = l, r
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func encryptBlock(l, r uint32, c *Cipher) (uint32, uint32) {
|
||||||
|
xl, xr := l, r
|
||||||
|
xl ^= c.p[0]
|
||||||
|
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[1]
|
||||||
|
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[2]
|
||||||
|
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[3]
|
||||||
|
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[4]
|
||||||
|
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[5]
|
||||||
|
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[6]
|
||||||
|
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[7]
|
||||||
|
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[8]
|
||||||
|
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[9]
|
||||||
|
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[10]
|
||||||
|
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[11]
|
||||||
|
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[12]
|
||||||
|
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[13]
|
||||||
|
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[14]
|
||||||
|
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[15]
|
||||||
|
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[16]
|
||||||
|
xr ^= c.p[17]
|
||||||
|
return xr, xl
|
||||||
|
}
|
||||||
|
|
||||||
|
func decryptBlock(l, r uint32, c *Cipher) (uint32, uint32) {
|
||||||
|
xl, xr := l, r
|
||||||
|
xl ^= c.p[17]
|
||||||
|
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[16]
|
||||||
|
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[15]
|
||||||
|
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[14]
|
||||||
|
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[13]
|
||||||
|
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[12]
|
||||||
|
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[11]
|
||||||
|
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[10]
|
||||||
|
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[9]
|
||||||
|
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[8]
|
||||||
|
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[7]
|
||||||
|
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[6]
|
||||||
|
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[5]
|
||||||
|
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[4]
|
||||||
|
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[3]
|
||||||
|
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[2]
|
||||||
|
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[1]
|
||||||
|
xr ^= c.p[0]
|
||||||
|
return xr, xl
|
||||||
|
}
|
99
vendor/golang.org/x/crypto/blowfish/cipher.go
generated
vendored
Normal file
99
vendor/golang.org/x/crypto/blowfish/cipher.go
generated
vendored
Normal file
|
@ -0,0 +1,99 @@
|
||||||
|
// Copyright 2010 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.
|
||||||
|
|
||||||
|
// Package blowfish implements Bruce Schneier's Blowfish encryption algorithm.
|
||||||
|
//
|
||||||
|
// Blowfish is a legacy cipher and its short block size makes it vulnerable to
|
||||||
|
// birthday bound attacks (see https://sweet32.info). It should only be used
|
||||||
|
// where compatibility with legacy systems, not security, is the goal.
|
||||||
|
//
|
||||||
|
// Deprecated: any new system should use AES (from crypto/aes, if necessary in
|
||||||
|
// an AEAD mode like crypto/cipher.NewGCM) or XChaCha20-Poly1305 (from
|
||||||
|
// golang.org/x/crypto/chacha20poly1305).
|
||||||
|
package blowfish
|
||||||
|
|
||||||
|
// The code is a port of Bruce Schneier's C implementation.
|
||||||
|
// See https://www.schneier.com/blowfish.html.
|
||||||
|
|
||||||
|
import "strconv"
|
||||||
|
|
||||||
|
// The Blowfish block size in bytes.
|
||||||
|
const BlockSize = 8
|
||||||
|
|
||||||
|
// A Cipher is an instance of Blowfish encryption using a particular key.
|
||||||
|
type Cipher struct {
|
||||||
|
p [18]uint32
|
||||||
|
s0, s1, s2, s3 [256]uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
type KeySizeError int
|
||||||
|
|
||||||
|
func (k KeySizeError) Error() string {
|
||||||
|
return "crypto/blowfish: invalid key size " + strconv.Itoa(int(k))
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewCipher creates and returns a Cipher.
|
||||||
|
// The key argument should be the Blowfish key, from 1 to 56 bytes.
|
||||||
|
func NewCipher(key []byte) (*Cipher, error) {
|
||||||
|
var result Cipher
|
||||||
|
if k := len(key); k < 1 || k > 56 {
|
||||||
|
return nil, KeySizeError(k)
|
||||||
|
}
|
||||||
|
initCipher(&result)
|
||||||
|
ExpandKey(key, &result)
|
||||||
|
return &result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewSaltedCipher creates a returns a Cipher that folds a salt into its key
|
||||||
|
// schedule. For most purposes, NewCipher, instead of NewSaltedCipher, is
|
||||||
|
// sufficient and desirable. For bcrypt compatibility, the key can be over 56
|
||||||
|
// bytes.
|
||||||
|
func NewSaltedCipher(key, salt []byte) (*Cipher, error) {
|
||||||
|
if len(salt) == 0 {
|
||||||
|
return NewCipher(key)
|
||||||
|
}
|
||||||
|
var result Cipher
|
||||||
|
if k := len(key); k < 1 {
|
||||||
|
return nil, KeySizeError(k)
|
||||||
|
}
|
||||||
|
initCipher(&result)
|
||||||
|
expandKeyWithSalt(key, salt, &result)
|
||||||
|
return &result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// BlockSize returns the Blowfish block size, 8 bytes.
|
||||||
|
// It is necessary to satisfy the Block interface in the
|
||||||
|
// package "crypto/cipher".
|
||||||
|
func (c *Cipher) BlockSize() int { return BlockSize }
|
||||||
|
|
||||||
|
// Encrypt encrypts the 8-byte buffer src using the key k
|
||||||
|
// and stores the result in dst.
|
||||||
|
// Note that for amounts of data larger than a block,
|
||||||
|
// it is not safe to just call Encrypt on successive blocks;
|
||||||
|
// instead, use an encryption mode like CBC (see crypto/cipher/cbc.go).
|
||||||
|
func (c *Cipher) Encrypt(dst, src []byte) {
|
||||||
|
l := uint32(src[0])<<24 | uint32(src[1])<<16 | uint32(src[2])<<8 | uint32(src[3])
|
||||||
|
r := uint32(src[4])<<24 | uint32(src[5])<<16 | uint32(src[6])<<8 | uint32(src[7])
|
||||||
|
l, r = encryptBlock(l, r, c)
|
||||||
|
dst[0], dst[1], dst[2], dst[3] = byte(l>>24), byte(l>>16), byte(l>>8), byte(l)
|
||||||
|
dst[4], dst[5], dst[6], dst[7] = byte(r>>24), byte(r>>16), byte(r>>8), byte(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decrypt decrypts the 8-byte buffer src using the key k
|
||||||
|
// and stores the result in dst.
|
||||||
|
func (c *Cipher) Decrypt(dst, src []byte) {
|
||||||
|
l := uint32(src[0])<<24 | uint32(src[1])<<16 | uint32(src[2])<<8 | uint32(src[3])
|
||||||
|
r := uint32(src[4])<<24 | uint32(src[5])<<16 | uint32(src[6])<<8 | uint32(src[7])
|
||||||
|
l, r = decryptBlock(l, r, c)
|
||||||
|
dst[0], dst[1], dst[2], dst[3] = byte(l>>24), byte(l>>16), byte(l>>8), byte(l)
|
||||||
|
dst[4], dst[5], dst[6], dst[7] = byte(r>>24), byte(r>>16), byte(r>>8), byte(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func initCipher(c *Cipher) {
|
||||||
|
copy(c.p[0:], p[0:])
|
||||||
|
copy(c.s0[0:], s0[0:])
|
||||||
|
copy(c.s1[0:], s1[0:])
|
||||||
|
copy(c.s2[0:], s2[0:])
|
||||||
|
copy(c.s3[0:], s3[0:])
|
||||||
|
}
|
199
vendor/golang.org/x/crypto/blowfish/const.go
generated
vendored
Normal file
199
vendor/golang.org/x/crypto/blowfish/const.go
generated
vendored
Normal file
|
@ -0,0 +1,199 @@
|
||||||
|
// Copyright 2010 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.
|
||||||
|
|
||||||
|
// The startup permutation array and substitution boxes.
|
||||||
|
// They are the hexadecimal digits of PI; see:
|
||||||
|
// https://www.schneier.com/code/constants.txt.
|
||||||
|
|
||||||
|
package blowfish
|
||||||
|
|
||||||
|
var s0 = [256]uint32{
|
||||||
|
0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96,
|
||||||
|
0xba7c9045, 0xf12c7f99, 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16,
|
||||||
|
0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e, 0x0d95748f, 0x728eb658,
|
||||||
|
0x718bcd58, 0x82154aee, 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013,
|
||||||
|
0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef, 0x8e79dcb0, 0x603a180e,
|
||||||
|
0x6c9e0e8b, 0xb01e8a3e, 0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60,
|
||||||
|
0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440, 0x55ca396a, 0x2aab10b6,
|
||||||
|
0xb4cc5c34, 0x1141e8ce, 0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a,
|
||||||
|
0x2ba9c55d, 0x741831f6, 0xce5c3e16, 0x9b87931e, 0xafd6ba33, 0x6c24cf5c,
|
||||||
|
0x7a325381, 0x28958677, 0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193,
|
||||||
|
0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032, 0xef845d5d, 0xe98575b1,
|
||||||
|
0xdc262302, 0xeb651b88, 0x23893e81, 0xd396acc5, 0x0f6d6ff3, 0x83f44239,
|
||||||
|
0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e, 0x21c66842, 0xf6e96c9a,
|
||||||
|
0x670c9c61, 0xabd388f0, 0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3,
|
||||||
|
0x6eef0b6c, 0x137a3be4, 0xba3bf050, 0x7efb2a98, 0xa1f1651d, 0x39af0176,
|
||||||
|
0x66ca593e, 0x82430e88, 0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe,
|
||||||
|
0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6, 0x4ed3aa62, 0x363f7706,
|
||||||
|
0x1bfedf72, 0x429b023d, 0x37d0d724, 0xd00a1248, 0xdb0fead3, 0x49f1c09b,
|
||||||
|
0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7, 0xe3fe501a, 0xb6794c3b,
|
||||||
|
0x976ce0bd, 0x04c006ba, 0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463,
|
||||||
|
0x68fb6faf, 0x3e6c53b5, 0x1339b2eb, 0x3b52ec6f, 0x6dfc511f, 0x9b30952c,
|
||||||
|
0xcc814544, 0xaf5ebd09, 0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3,
|
||||||
|
0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb, 0x5579c0bd, 0x1a60320a,
|
||||||
|
0xd6a100c6, 0x402c7279, 0x679f25fe, 0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8,
|
||||||
|
0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab, 0x323db5fa, 0xfd238760,
|
||||||
|
0x53317b48, 0x3e00df82, 0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db,
|
||||||
|
0xd542a8f6, 0x287effc3, 0xac6732c6, 0x8c4f5573, 0x695b27b0, 0xbbca58c8,
|
||||||
|
0xe1ffa35d, 0xb8f011a0, 0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b,
|
||||||
|
0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790, 0xe1ddf2da, 0xa4cb7e33,
|
||||||
|
0x62fb1341, 0xcee4c6e8, 0xef20cada, 0x36774c01, 0xd07e9efe, 0x2bf11fb4,
|
||||||
|
0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0, 0xd08ed1d0, 0xafc725e0,
|
||||||
|
0x8e3c5b2f, 0x8e7594b7, 0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c,
|
||||||
|
0x4fad5ea0, 0x688fc31c, 0xd1cff191, 0xb3a8c1ad, 0x2f2f2218, 0xbe0e1777,
|
||||||
|
0xea752dfe, 0x8b021fa1, 0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299,
|
||||||
|
0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9, 0x165fa266, 0x80957705,
|
||||||
|
0x93cc7314, 0x211a1477, 0xe6ad2065, 0x77b5fa86, 0xc75442f5, 0xfb9d35cf,
|
||||||
|
0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49, 0x00250e2d, 0x2071b35e,
|
||||||
|
0x226800bb, 0x57b8e0af, 0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa,
|
||||||
|
0x78c14389, 0xd95a537f, 0x207d5ba2, 0x02e5b9c5, 0x83260376, 0x6295cfa9,
|
||||||
|
0x11c81968, 0x4e734a41, 0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915,
|
||||||
|
0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400, 0x08ba6fb5, 0x571be91f,
|
||||||
|
0xf296ec6b, 0x2a0dd915, 0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664,
|
||||||
|
0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a,
|
||||||
|
}
|
||||||
|
|
||||||
|
var s1 = [256]uint32{
|
||||||
|
0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623, 0xad6ea6b0, 0x49a7df7d,
|
||||||
|
0x9cee60b8, 0x8fedb266, 0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1,
|
||||||
|
0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e, 0x3f54989a, 0x5b429d65,
|
||||||
|
0x6b8fe4d6, 0x99f73fd6, 0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1,
|
||||||
|
0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e, 0x09686b3f, 0x3ebaefc9,
|
||||||
|
0x3c971814, 0x6b6a70a1, 0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737,
|
||||||
|
0x3e07841c, 0x7fdeae5c, 0x8e7d44ec, 0x5716f2b8, 0xb03ada37, 0xf0500c0d,
|
||||||
|
0xf01c1f04, 0x0200b3ff, 0xae0cf51a, 0x3cb574b2, 0x25837a58, 0xdc0921bd,
|
||||||
|
0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701, 0x3ae5e581, 0x37c2dadc,
|
||||||
|
0xc8b57634, 0x9af3dda7, 0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41,
|
||||||
|
0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331, 0x4e548b38, 0x4f6db908,
|
||||||
|
0x6f420d03, 0xf60a04bf, 0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af,
|
||||||
|
0xde9a771f, 0xd9930810, 0xb38bae12, 0xdccf3f2e, 0x5512721f, 0x2e6b7124,
|
||||||
|
0x501adde6, 0x9f84cd87, 0x7a584718, 0x7408da17, 0xbc9f9abc, 0xe94b7d8c,
|
||||||
|
0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2, 0xef1c1847, 0x3215d908,
|
||||||
|
0xdd433b37, 0x24c2ba16, 0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd,
|
||||||
|
0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b, 0x043556f1, 0xd7a3c76b,
|
||||||
|
0x3c11183b, 0x5924a509, 0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e,
|
||||||
|
0x86e34570, 0xeae96fb1, 0x860e5e0a, 0x5a3e2ab3, 0x771fe71c, 0x4e3d06fa,
|
||||||
|
0x2965dcb9, 0x99e71d0f, 0x803e89d6, 0x5266c825, 0x2e4cc978, 0x9c10b36a,
|
||||||
|
0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4, 0xf2f74ea7, 0x361d2b3d,
|
||||||
|
0x1939260f, 0x19c27960, 0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66,
|
||||||
|
0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28, 0xc332ddef, 0xbe6c5aa5,
|
||||||
|
0x65582185, 0x68ab9802, 0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84,
|
||||||
|
0x1521b628, 0x29076170, 0xecdd4775, 0x619f1510, 0x13cca830, 0xeb61bd96,
|
||||||
|
0x0334fe1e, 0xaa0363cf, 0xb5735c90, 0x4c70a239, 0xd59e9e0b, 0xcbaade14,
|
||||||
|
0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e, 0x648b1eaf, 0x19bdf0ca,
|
||||||
|
0xa02369b9, 0x655abb50, 0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7,
|
||||||
|
0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8, 0xf837889a, 0x97e32d77,
|
||||||
|
0x11ed935f, 0x16681281, 0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99,
|
||||||
|
0x57f584a5, 0x1b227263, 0x9b83c3ff, 0x1ac24696, 0xcdb30aeb, 0x532e3054,
|
||||||
|
0x8fd948e4, 0x6dbc3128, 0x58ebf2ef, 0x34c6ffea, 0xfe28ed61, 0xee7c3c73,
|
||||||
|
0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0, 0x45eee2b6, 0xa3aaabea,
|
||||||
|
0xdb6c4f15, 0xfacb4fd0, 0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105,
|
||||||
|
0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250, 0xcf62a1f2, 0x5b8d2646,
|
||||||
|
0xfc8883a0, 0xc1c7b6a3, 0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285,
|
||||||
|
0x095bbf00, 0xad19489d, 0x1462b174, 0x23820e00, 0x58428d2a, 0x0c55f5ea,
|
||||||
|
0x1dadf43e, 0x233f7061, 0x3372f092, 0x8d937e41, 0xd65fecf1, 0x6c223bdb,
|
||||||
|
0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e, 0xa6078084, 0x19f8509e,
|
||||||
|
0xe8efd855, 0x61d99735, 0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc,
|
||||||
|
0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9, 0xdb73dbd3, 0x105588cd,
|
||||||
|
0x675fda79, 0xe3674340, 0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20,
|
||||||
|
0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7,
|
||||||
|
}
|
||||||
|
|
||||||
|
var s2 = [256]uint32{
|
||||||
|
0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934, 0x411520f7, 0x7602d4f7,
|
||||||
|
0xbcf46b2e, 0xd4a20068, 0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af,
|
||||||
|
0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840, 0x4d95fc1d, 0x96b591af,
|
||||||
|
0x70f4ddd3, 0x66a02f45, 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504,
|
||||||
|
0x96eb27b3, 0x55fd3941, 0xda2547e6, 0xabca0a9a, 0x28507825, 0x530429f4,
|
||||||
|
0x0a2c86da, 0xe9b66dfb, 0x68dc1462, 0xd7486900, 0x680ec0a4, 0x27a18dee,
|
||||||
|
0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6, 0xaace1e7c, 0xd3375fec,
|
||||||
|
0xce78a399, 0x406b2a42, 0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b,
|
||||||
|
0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2, 0x3a6efa74, 0xdd5b4332,
|
||||||
|
0x6841e7f7, 0xca7820fb, 0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527,
|
||||||
|
0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b, 0x55a867bc, 0xa1159a58,
|
||||||
|
0xcca92963, 0x99e1db33, 0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c,
|
||||||
|
0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3, 0x95c11548, 0xe4c66d22,
|
||||||
|
0x48c1133f, 0xc70f86dc, 0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17,
|
||||||
|
0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564, 0x257b7834, 0x602a9c60,
|
||||||
|
0xdff8e8a3, 0x1f636c1b, 0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115,
|
||||||
|
0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922, 0x85b2a20e, 0xe6ba0d99,
|
||||||
|
0xde720c8c, 0x2da2f728, 0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0,
|
||||||
|
0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e, 0x0a476341, 0x992eff74,
|
||||||
|
0x3a6f6eab, 0xf4f8fd37, 0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d,
|
||||||
|
0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804, 0xf1290dc7, 0xcc00ffa3,
|
||||||
|
0xb5390f92, 0x690fed0b, 0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3,
|
||||||
|
0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb, 0x37392eb3, 0xcc115979,
|
||||||
|
0x8026e297, 0xf42e312d, 0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c,
|
||||||
|
0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350, 0x1a6b1018, 0x11caedfa,
|
||||||
|
0x3d25bdd8, 0xe2e1c3c9, 0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a,
|
||||||
|
0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe, 0x9dbc8057, 0xf0f7c086,
|
||||||
|
0x60787bf8, 0x6003604d, 0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc,
|
||||||
|
0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f, 0x77a057be, 0xbde8ae24,
|
||||||
|
0x55464299, 0xbf582e61, 0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2,
|
||||||
|
0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9, 0x7aeb2661, 0x8b1ddf84,
|
||||||
|
0x846a0e79, 0x915f95e2, 0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c,
|
||||||
|
0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e, 0xb77f19b6, 0xe0a9dc09,
|
||||||
|
0x662d09a1, 0xc4324633, 0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10,
|
||||||
|
0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169, 0xdcb7da83, 0x573906fe,
|
||||||
|
0xa1e2ce9b, 0x4fcd7f52, 0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027,
|
||||||
|
0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5, 0xf0177a28, 0xc0f586e0,
|
||||||
|
0x006058aa, 0x30dc7d62, 0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634,
|
||||||
|
0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76, 0x6f05e409, 0x4b7c0188,
|
||||||
|
0x39720a3d, 0x7c927c24, 0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc,
|
||||||
|
0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4, 0x1e50ef5e, 0xb161e6f8,
|
||||||
|
0xa28514d9, 0x6c51133c, 0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837,
|
||||||
|
0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0,
|
||||||
|
}
|
||||||
|
|
||||||
|
var s3 = [256]uint32{
|
||||||
|
0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b, 0x5cb0679e, 0x4fa33742,
|
||||||
|
0xd3822740, 0x99bc9bbe, 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b,
|
||||||
|
0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4, 0x5748ab2f, 0xbc946e79,
|
||||||
|
0xc6a376d2, 0x6549c2c8, 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6,
|
||||||
|
0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304, 0xa1fad5f0, 0x6a2d519a,
|
||||||
|
0x63ef8ce2, 0x9a86ee22, 0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4,
|
||||||
|
0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6, 0x2826a2f9, 0xa73a3ae1,
|
||||||
|
0x4ba99586, 0xef5562e9, 0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59,
|
||||||
|
0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593, 0xe990fd5a, 0x9e34d797,
|
||||||
|
0x2cf0b7d9, 0x022b8b51, 0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28,
|
||||||
|
0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c, 0xe029ac71, 0xe019a5e6,
|
||||||
|
0x47b0acfd, 0xed93fa9b, 0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28,
|
||||||
|
0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c, 0x15056dd4, 0x88f46dba,
|
||||||
|
0x03a16125, 0x0564f0bd, 0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a,
|
||||||
|
0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319, 0x7533d928, 0xb155fdf5,
|
||||||
|
0x03563482, 0x8aba3cbb, 0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f,
|
||||||
|
0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991, 0xea7a90c2, 0xfb3e7bce,
|
||||||
|
0x5121ce64, 0x774fbe32, 0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680,
|
||||||
|
0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166, 0xb39a460a, 0x6445c0dd,
|
||||||
|
0x586cdecf, 0x1c20c8ae, 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb,
|
||||||
|
0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5, 0x72eacea8, 0xfa6484bb,
|
||||||
|
0x8d6612ae, 0xbf3c6f47, 0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370,
|
||||||
|
0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d, 0x4040cb08, 0x4eb4e2cc,
|
||||||
|
0x34d2466a, 0x0115af84, 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048,
|
||||||
|
0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8, 0x611560b1, 0xe7933fdc,
|
||||||
|
0xbb3a792b, 0x344525bd, 0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9,
|
||||||
|
0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7, 0x1a908749, 0xd44fbd9a,
|
||||||
|
0xd0dadecb, 0xd50ada38, 0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f,
|
||||||
|
0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c, 0xbf97222c, 0x15e6fc2a,
|
||||||
|
0x0f91fc71, 0x9b941525, 0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1,
|
||||||
|
0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442, 0xe0ec6e0e, 0x1698db3b,
|
||||||
|
0x4c98a0be, 0x3278e964, 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e,
|
||||||
|
0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8, 0xdf359f8d, 0x9b992f2e,
|
||||||
|
0xe60b6f47, 0x0fe3f11d, 0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f,
|
||||||
|
0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299, 0xf523f357, 0xa6327623,
|
||||||
|
0x93a83531, 0x56cccd02, 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc,
|
||||||
|
0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614, 0xe6c6c7bd, 0x327a140a,
|
||||||
|
0x45e1d006, 0xc3f27b9a, 0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6,
|
||||||
|
0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b, 0x53113ec0, 0x1640e3d3,
|
||||||
|
0x38abbd60, 0x2547adf0, 0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060,
|
||||||
|
0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e, 0x1948c25c, 0x02fb8a8c,
|
||||||
|
0x01c36ae4, 0xd6ebe1f9, 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f,
|
||||||
|
0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6,
|
||||||
|
}
|
||||||
|
|
||||||
|
var p = [18]uint32{
|
||||||
|
0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 0xa4093822, 0x299f31d0,
|
||||||
|
0x082efa98, 0xec4e6c89, 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c,
|
||||||
|
0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917, 0x9216d5d9, 0x8979fb1b,
|
||||||
|
}
|
27
vendor/golang.org/x/sys/LICENSE
generated
vendored
Normal file
27
vendor/golang.org/x/sys/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
Copyright 2009 The Go Authors.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are
|
||||||
|
met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer.
|
||||||
|
* 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.
|
||||||
|
* Neither the name of Google LLC 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
|
||||||
|
OWNER 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.
|
22
vendor/golang.org/x/sys/PATENTS
generated
vendored
Normal file
22
vendor/golang.org/x/sys/PATENTS
generated
vendored
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
Additional IP Rights Grant (Patents)
|
||||||
|
|
||||||
|
"This implementation" means the copyrightable works distributed by
|
||||||
|
Google as part of the Go project.
|
||||||
|
|
||||||
|
Google hereby grants to You a perpetual, worldwide, non-exclusive,
|
||||||
|
no-charge, royalty-free, irrevocable (except as stated in this section)
|
||||||
|
patent license to make, have made, use, offer to sell, sell, import,
|
||||||
|
transfer and otherwise run, modify and propagate the contents of this
|
||||||
|
implementation of Go, where such license applies only to those patent
|
||||||
|
claims, both currently owned or controlled by Google and acquired in
|
||||||
|
the future, licensable by Google that are necessarily infringed by this
|
||||||
|
implementation of Go. This grant does not include claims that would be
|
||||||
|
infringed only as a consequence of further modification of this
|
||||||
|
implementation. If you or your agent or exclusive licensee institute or
|
||||||
|
order or agree to the institution of patent litigation against any
|
||||||
|
entity (including a cross-claim or counterclaim in a lawsuit) alleging
|
||||||
|
that this implementation of Go or any code incorporated within this
|
||||||
|
implementation of Go constitutes direct or contributory patent
|
||||||
|
infringement, or inducement of patent infringement, then any patent
|
||||||
|
rights granted to you under this License for this implementation of Go
|
||||||
|
shall terminate as of the date such litigation is filed.
|
2
vendor/golang.org/x/sys/unix/.gitignore
generated
vendored
Normal file
2
vendor/golang.org/x/sys/unix/.gitignore
generated
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
_obj/
|
||||||
|
unix.test
|
184
vendor/golang.org/x/sys/unix/README.md
generated
vendored
Normal file
184
vendor/golang.org/x/sys/unix/README.md
generated
vendored
Normal file
|
@ -0,0 +1,184 @@
|
||||||
|
# Building `sys/unix`
|
||||||
|
|
||||||
|
The sys/unix package provides access to the raw system call interface of the
|
||||||
|
underlying operating system. See: https://godoc.org/golang.org/x/sys/unix
|
||||||
|
|
||||||
|
Porting Go to a new architecture/OS combination or adding syscalls, types, or
|
||||||
|
constants to an existing architecture/OS pair requires some manual effort;
|
||||||
|
however, there are tools that automate much of the process.
|
||||||
|
|
||||||
|
## Build Systems
|
||||||
|
|
||||||
|
There are currently two ways we generate the necessary files. We are currently
|
||||||
|
migrating the build system to use containers so the builds are reproducible.
|
||||||
|
This is being done on an OS-by-OS basis. Please update this documentation as
|
||||||
|
components of the build system change.
|
||||||
|
|
||||||
|
### Old Build System (currently for `GOOS != "linux"`)
|
||||||
|
|
||||||
|
The old build system generates the Go files based on the C header files
|
||||||
|
present on your system. This means that files
|
||||||
|
for a given GOOS/GOARCH pair must be generated on a system with that OS and
|
||||||
|
architecture. This also means that the generated code can differ from system
|
||||||
|
to system, based on differences in the header files.
|
||||||
|
|
||||||
|
To avoid this, if you are using the old build system, only generate the Go
|
||||||
|
files on an installation with unmodified header files. It is also important to
|
||||||
|
keep track of which version of the OS the files were generated from (ex.
|
||||||
|
Darwin 14 vs Darwin 15). This makes it easier to track the progress of changes
|
||||||
|
and have each OS upgrade correspond to a single change.
|
||||||
|
|
||||||
|
To build the files for your current OS and architecture, make sure GOOS and
|
||||||
|
GOARCH are set correctly and run `mkall.sh`. This will generate the files for
|
||||||
|
your specific system. Running `mkall.sh -n` shows the commands that will be run.
|
||||||
|
|
||||||
|
Requirements: bash, go
|
||||||
|
|
||||||
|
### New Build System (currently for `GOOS == "linux"`)
|
||||||
|
|
||||||
|
The new build system uses a Docker container to generate the go files directly
|
||||||
|
from source checkouts of the kernel and various system libraries. This means
|
||||||
|
that on any platform that supports Docker, all the files using the new build
|
||||||
|
system can be generated at once, and generated files will not change based on
|
||||||
|
what the person running the scripts has installed on their computer.
|
||||||
|
|
||||||
|
The OS specific files for the new build system are located in the `${GOOS}`
|
||||||
|
directory, and the build is coordinated by the `${GOOS}/mkall.go` program. When
|
||||||
|
the kernel or system library updates, modify the Dockerfile at
|
||||||
|
`${GOOS}/Dockerfile` to checkout the new release of the source.
|
||||||
|
|
||||||
|
To build all the files under the new build system, you must be on an amd64/Linux
|
||||||
|
system and have your GOOS and GOARCH set accordingly. Running `mkall.sh` will
|
||||||
|
then generate all of the files for all of the GOOS/GOARCH pairs in the new build
|
||||||
|
system. Running `mkall.sh -n` shows the commands that will be run.
|
||||||
|
|
||||||
|
Requirements: bash, go, docker
|
||||||
|
|
||||||
|
## Component files
|
||||||
|
|
||||||
|
This section describes the various files used in the code generation process.
|
||||||
|
It also contains instructions on how to modify these files to add a new
|
||||||
|
architecture/OS or to add additional syscalls, types, or constants. Note that
|
||||||
|
if you are using the new build system, the scripts/programs cannot be called normally.
|
||||||
|
They must be called from within the docker container.
|
||||||
|
|
||||||
|
### asm files
|
||||||
|
|
||||||
|
The hand-written assembly file at `asm_${GOOS}_${GOARCH}.s` implements system
|
||||||
|
call dispatch. There are three entry points:
|
||||||
|
```
|
||||||
|
func Syscall(trap, a1, a2, a3 uintptr) (r1, r2, err uintptr)
|
||||||
|
func Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err uintptr)
|
||||||
|
func RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2, err uintptr)
|
||||||
|
```
|
||||||
|
The first and second are the standard ones; they differ only in how many
|
||||||
|
arguments can be passed to the kernel. The third is for low-level use by the
|
||||||
|
ForkExec wrapper. Unlike the first two, it does not call into the scheduler to
|
||||||
|
let it know that a system call is running.
|
||||||
|
|
||||||
|
When porting Go to a new architecture/OS, this file must be implemented for
|
||||||
|
each GOOS/GOARCH pair.
|
||||||
|
|
||||||
|
### mksysnum
|
||||||
|
|
||||||
|
Mksysnum is a Go program located at `${GOOS}/mksysnum.go` (or `mksysnum_${GOOS}.go`
|
||||||
|
for the old system). This program takes in a list of header files containing the
|
||||||
|
syscall number declarations and parses them to produce the corresponding list of
|
||||||
|
Go numeric constants. See `zsysnum_${GOOS}_${GOARCH}.go` for the generated
|
||||||
|
constants.
|
||||||
|
|
||||||
|
Adding new syscall numbers is mostly done by running the build on a sufficiently
|
||||||
|
new installation of the target OS (or updating the source checkouts for the
|
||||||
|
new build system). However, depending on the OS, you may need to update the
|
||||||
|
parsing in mksysnum.
|
||||||
|
|
||||||
|
### mksyscall.go
|
||||||
|
|
||||||
|
The `syscall.go`, `syscall_${GOOS}.go`, `syscall_${GOOS}_${GOARCH}.go` are
|
||||||
|
hand-written Go files which implement system calls (for unix, the specific OS,
|
||||||
|
or the specific OS/Architecture pair respectively) that need special handling
|
||||||
|
and list `//sys` comments giving prototypes for ones that can be generated.
|
||||||
|
|
||||||
|
The mksyscall.go program takes the `//sys` and `//sysnb` comments and converts
|
||||||
|
them into syscalls. This requires the name of the prototype in the comment to
|
||||||
|
match a syscall number in the `zsysnum_${GOOS}_${GOARCH}.go` file. The function
|
||||||
|
prototype can be exported (capitalized) or not.
|
||||||
|
|
||||||
|
Adding a new syscall often just requires adding a new `//sys` function prototype
|
||||||
|
with the desired arguments and a capitalized name so it is exported. However, if
|
||||||
|
you want the interface to the syscall to be different, often one will make an
|
||||||
|
unexported `//sys` prototype, and then write a custom wrapper in
|
||||||
|
`syscall_${GOOS}.go`.
|
||||||
|
|
||||||
|
### types files
|
||||||
|
|
||||||
|
For each OS, there is a hand-written Go file at `${GOOS}/types.go` (or
|
||||||
|
`types_${GOOS}.go` on the old system). This file includes standard C headers and
|
||||||
|
creates Go type aliases to the corresponding C types. The file is then fed
|
||||||
|
through godef to get the Go compatible definitions. Finally, the generated code
|
||||||
|
is fed though mkpost.go to format the code correctly and remove any hidden or
|
||||||
|
private identifiers. This cleaned-up code is written to
|
||||||
|
`ztypes_${GOOS}_${GOARCH}.go`.
|
||||||
|
|
||||||
|
The hardest part about preparing this file is figuring out which headers to
|
||||||
|
include and which symbols need to be `#define`d to get the actual data
|
||||||
|
structures that pass through to the kernel system calls. Some C libraries
|
||||||
|
preset alternate versions for binary compatibility and translate them on the
|
||||||
|
way in and out of system calls, but there is almost always a `#define` that can
|
||||||
|
get the real ones.
|
||||||
|
See `types_darwin.go` and `linux/types.go` for examples.
|
||||||
|
|
||||||
|
To add a new type, add in the necessary include statement at the top of the
|
||||||
|
file (if it is not already there) and add in a type alias line. Note that if
|
||||||
|
your type is significantly different on different architectures, you may need
|
||||||
|
some `#if/#elif` macros in your include statements.
|
||||||
|
|
||||||
|
### mkerrors.sh
|
||||||
|
|
||||||
|
This script is used to generate the system's various constants. This doesn't
|
||||||
|
just include the error numbers and error strings, but also the signal numbers
|
||||||
|
and a wide variety of miscellaneous constants. The constants come from the list
|
||||||
|
of include files in the `includes_${uname}` variable. A regex then picks out
|
||||||
|
the desired `#define` statements, and generates the corresponding Go constants.
|
||||||
|
The error numbers and strings are generated from `#include <errno.h>`, and the
|
||||||
|
signal numbers and strings are generated from `#include <signal.h>`. All of
|
||||||
|
these constants are written to `zerrors_${GOOS}_${GOARCH}.go` via a C program,
|
||||||
|
`_errors.c`, which prints out all the constants.
|
||||||
|
|
||||||
|
To add a constant, add the header that includes it to the appropriate variable.
|
||||||
|
Then, edit the regex (if necessary) to match the desired constant. Avoid making
|
||||||
|
the regex too broad to avoid matching unintended constants.
|
||||||
|
|
||||||
|
### internal/mkmerge
|
||||||
|
|
||||||
|
This program is used to extract duplicate const, func, and type declarations
|
||||||
|
from the generated architecture-specific files listed below, and merge these
|
||||||
|
into a common file for each OS.
|
||||||
|
|
||||||
|
The merge is performed in the following steps:
|
||||||
|
1. Construct the set of common code that is identical in all architecture-specific files.
|
||||||
|
2. Write this common code to the merged file.
|
||||||
|
3. Remove the common code from all architecture-specific files.
|
||||||
|
|
||||||
|
|
||||||
|
## Generated files
|
||||||
|
|
||||||
|
### `zerrors_${GOOS}_${GOARCH}.go`
|
||||||
|
|
||||||
|
A file containing all of the system's generated error numbers, error strings,
|
||||||
|
signal numbers, and constants. Generated by `mkerrors.sh` (see above).
|
||||||
|
|
||||||
|
### `zsyscall_${GOOS}_${GOARCH}.go`
|
||||||
|
|
||||||
|
A file containing all the generated syscalls for a specific GOOS and GOARCH.
|
||||||
|
Generated by `mksyscall.go` (see above).
|
||||||
|
|
||||||
|
### `zsysnum_${GOOS}_${GOARCH}.go`
|
||||||
|
|
||||||
|
A list of numeric constants for all the syscall number of the specific GOOS
|
||||||
|
and GOARCH. Generated by mksysnum (see above).
|
||||||
|
|
||||||
|
### `ztypes_${GOOS}_${GOARCH}.go`
|
||||||
|
|
||||||
|
A file containing Go types for passing into (or returning from) syscalls.
|
||||||
|
Generated by godefs and the types file (see above).
|
86
vendor/golang.org/x/sys/unix/affinity_linux.go
generated
vendored
Normal file
86
vendor/golang.org/x/sys/unix/affinity_linux.go
generated
vendored
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
// Copyright 2018 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.
|
||||||
|
|
||||||
|
// CPU affinity functions
|
||||||
|
|
||||||
|
package unix
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/bits"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
const cpuSetSize = _CPU_SETSIZE / _NCPUBITS
|
||||||
|
|
||||||
|
// CPUSet represents a CPU affinity mask.
|
||||||
|
type CPUSet [cpuSetSize]cpuMask
|
||||||
|
|
||||||
|
func schedAffinity(trap uintptr, pid int, set *CPUSet) error {
|
||||||
|
_, _, e := RawSyscall(trap, uintptr(pid), uintptr(unsafe.Sizeof(*set)), uintptr(unsafe.Pointer(set)))
|
||||||
|
if e != 0 {
|
||||||
|
return errnoErr(e)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SchedGetaffinity gets the CPU affinity mask of the thread specified by pid.
|
||||||
|
// If pid is 0 the calling thread is used.
|
||||||
|
func SchedGetaffinity(pid int, set *CPUSet) error {
|
||||||
|
return schedAffinity(SYS_SCHED_GETAFFINITY, pid, set)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SchedSetaffinity sets the CPU affinity mask of the thread specified by pid.
|
||||||
|
// If pid is 0 the calling thread is used.
|
||||||
|
func SchedSetaffinity(pid int, set *CPUSet) error {
|
||||||
|
return schedAffinity(SYS_SCHED_SETAFFINITY, pid, set)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Zero clears the set s, so that it contains no CPUs.
|
||||||
|
func (s *CPUSet) Zero() {
|
||||||
|
for i := range s {
|
||||||
|
s[i] = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func cpuBitsIndex(cpu int) int {
|
||||||
|
return cpu / _NCPUBITS
|
||||||
|
}
|
||||||
|
|
||||||
|
func cpuBitsMask(cpu int) cpuMask {
|
||||||
|
return cpuMask(1 << (uint(cpu) % _NCPUBITS))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set adds cpu to the set s.
|
||||||
|
func (s *CPUSet) Set(cpu int) {
|
||||||
|
i := cpuBitsIndex(cpu)
|
||||||
|
if i < len(s) {
|
||||||
|
s[i] |= cpuBitsMask(cpu)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear removes cpu from the set s.
|
||||||
|
func (s *CPUSet) Clear(cpu int) {
|
||||||
|
i := cpuBitsIndex(cpu)
|
||||||
|
if i < len(s) {
|
||||||
|
s[i] &^= cpuBitsMask(cpu)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsSet reports whether cpu is in the set s.
|
||||||
|
func (s *CPUSet) IsSet(cpu int) bool {
|
||||||
|
i := cpuBitsIndex(cpu)
|
||||||
|
if i < len(s) {
|
||||||
|
return s[i]&cpuBitsMask(cpu) != 0
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Count returns the number of CPUs in the set s.
|
||||||
|
func (s *CPUSet) Count() int {
|
||||||
|
c := 0
|
||||||
|
for _, b := range s {
|
||||||
|
c += bits.OnesCount64(uint64(b))
|
||||||
|
}
|
||||||
|
return c
|
||||||
|
}
|
13
vendor/golang.org/x/sys/unix/aliases.go
generated
vendored
Normal file
13
vendor/golang.org/x/sys/unix/aliases.go
generated
vendored
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
// Copyright 2018 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:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos
|
||||||
|
|
||||||
|
package unix
|
||||||
|
|
||||||
|
import "syscall"
|
||||||
|
|
||||||
|
type Signal = syscall.Signal
|
||||||
|
type Errno = syscall.Errno
|
||||||
|
type SysProcAttr = syscall.SysProcAttr
|
17
vendor/golang.org/x/sys/unix/asm_aix_ppc64.s
generated
vendored
Normal file
17
vendor/golang.org/x/sys/unix/asm_aix_ppc64.s
generated
vendored
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
// Copyright 2018 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:build gc
|
||||||
|
|
||||||
|
#include "textflag.h"
|
||||||
|
|
||||||
|
//
|
||||||
|
// System calls for ppc64, AIX are implemented in runtime/syscall_aix.go
|
||||||
|
//
|
||||||
|
|
||||||
|
TEXT ·syscall6(SB),NOSPLIT,$0-88
|
||||||
|
JMP syscall·syscall6(SB)
|
||||||
|
|
||||||
|
TEXT ·rawSyscall6(SB),NOSPLIT,$0-88
|
||||||
|
JMP syscall·rawSyscall6(SB)
|
27
vendor/golang.org/x/sys/unix/asm_bsd_386.s
generated
vendored
Normal file
27
vendor/golang.org/x/sys/unix/asm_bsd_386.s
generated
vendored
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
// Copyright 2021 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:build (freebsd || netbsd || openbsd) && gc
|
||||||
|
|
||||||
|
#include "textflag.h"
|
||||||
|
|
||||||
|
// System call support for 386 BSD
|
||||||
|
|
||||||
|
// Just jump to package syscall's implementation for all these functions.
|
||||||
|
// The runtime may know about them.
|
||||||
|
|
||||||
|
TEXT ·Syscall(SB),NOSPLIT,$0-28
|
||||||
|
JMP syscall·Syscall(SB)
|
||||||
|
|
||||||
|
TEXT ·Syscall6(SB),NOSPLIT,$0-40
|
||||||
|
JMP syscall·Syscall6(SB)
|
||||||
|
|
||||||
|
TEXT ·Syscall9(SB),NOSPLIT,$0-52
|
||||||
|
JMP syscall·Syscall9(SB)
|
||||||
|
|
||||||
|
TEXT ·RawSyscall(SB),NOSPLIT,$0-28
|
||||||
|
JMP syscall·RawSyscall(SB)
|
||||||
|
|
||||||
|
TEXT ·RawSyscall6(SB),NOSPLIT,$0-40
|
||||||
|
JMP syscall·RawSyscall6(SB)
|
27
vendor/golang.org/x/sys/unix/asm_bsd_amd64.s
generated
vendored
Normal file
27
vendor/golang.org/x/sys/unix/asm_bsd_amd64.s
generated
vendored
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
// Copyright 2021 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:build (darwin || dragonfly || freebsd || netbsd || openbsd) && gc
|
||||||
|
|
||||||
|
#include "textflag.h"
|
||||||
|
|
||||||
|
// System call support for AMD64 BSD
|
||||||
|
|
||||||
|
// Just jump to package syscall's implementation for all these functions.
|
||||||
|
// The runtime may know about them.
|
||||||
|
|
||||||
|
TEXT ·Syscall(SB),NOSPLIT,$0-56
|
||||||
|
JMP syscall·Syscall(SB)
|
||||||
|
|
||||||
|
TEXT ·Syscall6(SB),NOSPLIT,$0-80
|
||||||
|
JMP syscall·Syscall6(SB)
|
||||||
|
|
||||||
|
TEXT ·Syscall9(SB),NOSPLIT,$0-104
|
||||||
|
JMP syscall·Syscall9(SB)
|
||||||
|
|
||||||
|
TEXT ·RawSyscall(SB),NOSPLIT,$0-56
|
||||||
|
JMP syscall·RawSyscall(SB)
|
||||||
|
|
||||||
|
TEXT ·RawSyscall6(SB),NOSPLIT,$0-80
|
||||||
|
JMP syscall·RawSyscall6(SB)
|
27
vendor/golang.org/x/sys/unix/asm_bsd_arm.s
generated
vendored
Normal file
27
vendor/golang.org/x/sys/unix/asm_bsd_arm.s
generated
vendored
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
// Copyright 2021 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:build (freebsd || netbsd || openbsd) && gc
|
||||||
|
|
||||||
|
#include "textflag.h"
|
||||||
|
|
||||||
|
// System call support for ARM BSD
|
||||||
|
|
||||||
|
// Just jump to package syscall's implementation for all these functions.
|
||||||
|
// The runtime may know about them.
|
||||||
|
|
||||||
|
TEXT ·Syscall(SB),NOSPLIT,$0-28
|
||||||
|
B syscall·Syscall(SB)
|
||||||
|
|
||||||
|
TEXT ·Syscall6(SB),NOSPLIT,$0-40
|
||||||
|
B syscall·Syscall6(SB)
|
||||||
|
|
||||||
|
TEXT ·Syscall9(SB),NOSPLIT,$0-52
|
||||||
|
B syscall·Syscall9(SB)
|
||||||
|
|
||||||
|
TEXT ·RawSyscall(SB),NOSPLIT,$0-28
|
||||||
|
B syscall·RawSyscall(SB)
|
||||||
|
|
||||||
|
TEXT ·RawSyscall6(SB),NOSPLIT,$0-40
|
||||||
|
B syscall·RawSyscall6(SB)
|
27
vendor/golang.org/x/sys/unix/asm_bsd_arm64.s
generated
vendored
Normal file
27
vendor/golang.org/x/sys/unix/asm_bsd_arm64.s
generated
vendored
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
// Copyright 2021 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:build (darwin || freebsd || netbsd || openbsd) && gc
|
||||||
|
|
||||||
|
#include "textflag.h"
|
||||||
|
|
||||||
|
// System call support for ARM64 BSD
|
||||||
|
|
||||||
|
// Just jump to package syscall's implementation for all these functions.
|
||||||
|
// The runtime may know about them.
|
||||||
|
|
||||||
|
TEXT ·Syscall(SB),NOSPLIT,$0-56
|
||||||
|
JMP syscall·Syscall(SB)
|
||||||
|
|
||||||
|
TEXT ·Syscall6(SB),NOSPLIT,$0-80
|
||||||
|
JMP syscall·Syscall6(SB)
|
||||||
|
|
||||||
|
TEXT ·Syscall9(SB),NOSPLIT,$0-104
|
||||||
|
JMP syscall·Syscall9(SB)
|
||||||
|
|
||||||
|
TEXT ·RawSyscall(SB),NOSPLIT,$0-56
|
||||||
|
JMP syscall·RawSyscall(SB)
|
||||||
|
|
||||||
|
TEXT ·RawSyscall6(SB),NOSPLIT,$0-80
|
||||||
|
JMP syscall·RawSyscall6(SB)
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Reference in a new issue