Update to chronograf from mrfusion
parent
0c97550e2c
commit
deed69a946
|
@ -3,8 +3,8 @@ package handlers
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/influxdata/mrfusion"
|
||||
"github.com/influxdata/mrfusion/dist"
|
||||
"github.com/influxdata/chronograf"
|
||||
"github.com/influxdata/chronograf/dist"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -17,12 +17,12 @@ type AssetsOpts struct {
|
|||
// Develop when true serves assets from ui/build directory directly; false will use internal bindata.
|
||||
Develop bool
|
||||
// Logger will log the asset served
|
||||
Logger mrfusion.Logger
|
||||
Logger chronograf.Logger
|
||||
}
|
||||
|
||||
// Assets creates a middleware that will serve a single page app.
|
||||
func Assets(opts AssetsOpts) http.Handler {
|
||||
var assets mrfusion.Assets
|
||||
var assets chronograf.Assets
|
||||
if opts.Develop {
|
||||
assets = &dist.DebugAssets{
|
||||
Dir: Dir,
|
||||
|
|
|
@ -5,7 +5,7 @@ import (
|
|||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/influxdata/mrfusion"
|
||||
"github.com/influxdata/chronograf"
|
||||
)
|
||||
|
||||
// CookieExtractor extracts the token from the value of the Name cookie.
|
||||
|
@ -17,7 +17,7 @@ type CookieExtractor struct {
|
|||
func (c *CookieExtractor) Extract(r *http.Request) (string, error) {
|
||||
cookie, err := r.Cookie(c.Name)
|
||||
if err != nil {
|
||||
return "", mrfusion.ErrAuthentication
|
||||
return "", chronograf.ErrAuthentication
|
||||
}
|
||||
return cookie.Value, nil
|
||||
}
|
||||
|
@ -29,14 +29,14 @@ type BearerExtractor struct{}
|
|||
func (b *BearerExtractor) Extract(r *http.Request) (string, error) {
|
||||
s := r.Header.Get("Authorization")
|
||||
if s == "" {
|
||||
return "", mrfusion.ErrAuthentication
|
||||
return "", chronograf.ErrAuthentication
|
||||
}
|
||||
|
||||
// Check for Bearer token.
|
||||
strs := strings.Split(s, " ")
|
||||
|
||||
if len(strs) != 2 || strs[0] != "Bearer" {
|
||||
return "", mrfusion.ErrAuthentication
|
||||
return "", chronograf.ErrAuthentication
|
||||
}
|
||||
return strs[1], nil
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ func (b *BearerExtractor) Extract(r *http.Request) (string, error) {
|
|||
// will be run. The principal will be sent to the next handler via the request's
|
||||
// Context. It is up to the next handler to determine if the principal has access.
|
||||
// On failure, will return http.StatusUnauthorized.
|
||||
func AuthorizedToken(auth mrfusion.Authenticator, te mrfusion.TokenExtractor, logger mrfusion.Logger, next http.Handler) http.Handler {
|
||||
func AuthorizedToken(auth chronograf.Authenticator, te chronograf.TokenExtractor, logger chronograf.Logger, next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
log := logger.
|
||||
WithField("component", "auth").
|
||||
|
@ -69,7 +69,7 @@ func AuthorizedToken(auth mrfusion.Authenticator, te mrfusion.TokenExtractor, lo
|
|||
}
|
||||
|
||||
// Send the principal to the next handler
|
||||
ctx := context.WithValue(r.Context(), mrfusion.PrincipalKey, principal)
|
||||
ctx := context.WithValue(r.Context(), chronograf.PrincipalKey, principal)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
return
|
||||
})
|
||||
|
|
|
@ -9,9 +9,9 @@ import (
|
|||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/influxdata/mrfusion"
|
||||
"github.com/influxdata/mrfusion/handlers"
|
||||
fusionlog "github.com/influxdata/mrfusion/log"
|
||||
"github.com/influxdata/chronograf"
|
||||
"github.com/influxdata/chronograf/handlers"
|
||||
clog "github.com/influxdata/chronograf/log"
|
||||
)
|
||||
|
||||
func TestCookieExtractor(t *testing.T) {
|
||||
|
@ -29,7 +29,7 @@ func TestCookieExtractor(t *testing.T) {
|
|||
Value: "reallyimportant",
|
||||
Lookup: "Doesntexist",
|
||||
Expected: "",
|
||||
Err: mrfusion.ErrAuthentication,
|
||||
Err: chronograf.ErrAuthentication,
|
||||
},
|
||||
{
|
||||
Desc: "Cookie token extracted",
|
||||
|
@ -47,7 +47,7 @@ func TestCookieExtractor(t *testing.T) {
|
|||
Value: test.Value,
|
||||
})
|
||||
|
||||
var e mrfusion.TokenExtractor = &handlers.CookieExtractor{
|
||||
var e chronograf.TokenExtractor = &handlers.CookieExtractor{
|
||||
Name: test.Lookup,
|
||||
}
|
||||
actual, err := e.Extract(req)
|
||||
|
@ -75,21 +75,21 @@ func TestBearerExtractor(t *testing.T) {
|
|||
Header: "Doesntexist",
|
||||
Value: "reallyimportant",
|
||||
Expected: "",
|
||||
Err: mrfusion.ErrAuthentication,
|
||||
Err: chronograf.ErrAuthentication,
|
||||
},
|
||||
{
|
||||
Desc: "Auth header doesn't have Bearer",
|
||||
Header: "Authorization",
|
||||
Value: "Bad Value",
|
||||
Expected: "",
|
||||
Err: mrfusion.ErrAuthentication,
|
||||
Err: chronograf.ErrAuthentication,
|
||||
},
|
||||
{
|
||||
Desc: "Auth header doesn't have Bearer token",
|
||||
Header: "Authorization",
|
||||
Value: "Bearer",
|
||||
Expected: "",
|
||||
Err: mrfusion.ErrAuthentication,
|
||||
Err: chronograf.ErrAuthentication,
|
||||
},
|
||||
{
|
||||
Desc: "Authorization Bearer token success",
|
||||
|
@ -103,7 +103,7 @@ func TestBearerExtractor(t *testing.T) {
|
|||
req, _ := http.NewRequest("", "http://howdy.com", nil)
|
||||
req.Header.Add(test.Header, test.Value)
|
||||
|
||||
var e mrfusion.TokenExtractor = &handlers.BearerExtractor{}
|
||||
var e chronograf.TokenExtractor = &handlers.BearerExtractor{}
|
||||
actual, err := e.Extract(req)
|
||||
if err != test.Err {
|
||||
t.Errorf("Bearer extract error; expected %v actual %v", test.Err, err)
|
||||
|
@ -124,15 +124,15 @@ func (m *MockExtractor) Extract(*http.Request) (string, error) {
|
|||
}
|
||||
|
||||
type MockAuthenticator struct {
|
||||
Principal mrfusion.Principal
|
||||
Principal chronograf.Principal
|
||||
Err error
|
||||
}
|
||||
|
||||
func (m *MockAuthenticator) Authenticate(context.Context, string) (mrfusion.Principal, error) {
|
||||
func (m *MockAuthenticator) Authenticate(context.Context, string) (chronograf.Principal, error) {
|
||||
return m.Principal, m.Err
|
||||
}
|
||||
|
||||
func (m *MockAuthenticator) Token(context.Context, mrfusion.Principal, time.Duration) (string, error) {
|
||||
func (m *MockAuthenticator) Token(context.Context, chronograf.Principal, time.Duration) (string, error) {
|
||||
return "", m.Err
|
||||
}
|
||||
|
||||
|
@ -140,7 +140,7 @@ func TestAuthorizedToken(t *testing.T) {
|
|||
var tests = []struct {
|
||||
Desc string
|
||||
Code int
|
||||
Principal mrfusion.Principal
|
||||
Principal chronograf.Principal
|
||||
ExtractorErr error
|
||||
AuthErr error
|
||||
Expected string
|
||||
|
@ -165,10 +165,10 @@ func TestAuthorizedToken(t *testing.T) {
|
|||
for _, test := range tests {
|
||||
// next is a sentinel StatusOK and
|
||||
// principal recorder.
|
||||
var principal mrfusion.Principal
|
||||
var principal chronograf.Principal
|
||||
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
principal = r.Context().Value(mrfusion.PrincipalKey).(mrfusion.Principal)
|
||||
principal = r.Context().Value(chronograf.PrincipalKey).(chronograf.Principal)
|
||||
})
|
||||
req, _ := http.NewRequest("GET", "", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
@ -181,7 +181,7 @@ func TestAuthorizedToken(t *testing.T) {
|
|||
Principal: test.Principal,
|
||||
}
|
||||
|
||||
logger := fusionlog.New()
|
||||
logger := clog.New()
|
||||
handler := handlers.AuthorizedToken(a, e, logger, next)
|
||||
handler.ServeHTTP(w, req)
|
||||
if w.Code != test.Code {
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/google/go-github/github"
|
||||
"github.com/influxdata/mrfusion"
|
||||
"github.com/influxdata/chronograf"
|
||||
"golang.org/x/oauth2"
|
||||
ogh "golang.org/x/oauth2/github"
|
||||
)
|
||||
|
@ -38,18 +38,18 @@ func NewCookie() Cookie {
|
|||
// the user's primary Github email address.
|
||||
type Github struct {
|
||||
Cookie Cookie
|
||||
Authenticator mrfusion.Authenticator
|
||||
Authenticator chronograf.Authenticator
|
||||
ClientID string
|
||||
ClientSecret string
|
||||
Scopes []string
|
||||
SuccessURL string // SuccessURL is redirect location after successful authorization
|
||||
FailureURL string // FailureURL is redirect location after authorization failure
|
||||
Now func() time.Time
|
||||
Logger mrfusion.Logger
|
||||
Logger chronograf.Logger
|
||||
}
|
||||
|
||||
// NewGithub constructs a Github with default cookie behavior and scopes.
|
||||
func NewGithub(clientID, clientSecret, successURL, failureURL string, auth mrfusion.Authenticator, log mrfusion.Logger) Github {
|
||||
func NewGithub(clientID, clientSecret, successURL, failureURL string, auth chronograf.Authenticator, log chronograf.Logger) Github {
|
||||
return Github{
|
||||
ClientID: clientID,
|
||||
ClientSecret: clientSecret,
|
||||
|
@ -84,7 +84,7 @@ func (g *Github) Login() http.Handler {
|
|||
// We'll give our users 10 minutes from this point to type in their github password.
|
||||
// If the callback is not received within 10 minutes, then authorization will fail.
|
||||
csrf := randomString(32) // 32 is not important... just long
|
||||
state, err := g.Authenticator.Token(r.Context(), mrfusion.Principal(csrf), 10*time.Minute)
|
||||
state, err := g.Authenticator.Token(r.Context(), chronograf.Principal(csrf), 10*time.Minute)
|
||||
// This is likely an internal server error
|
||||
if err != nil {
|
||||
g.Logger.
|
||||
|
@ -170,7 +170,7 @@ func (g *Github) Callback() http.Handler {
|
|||
}
|
||||
|
||||
// We create an auth token that will be used by all other endpoints to validate the principal has a claim
|
||||
authToken, err := g.Authenticator.Token(r.Context(), mrfusion.Principal(email), g.Cookie.Duration)
|
||||
authToken, err := g.Authenticator.Token(r.Context(), chronograf.Principal(email), g.Cookie.Duration)
|
||||
if err != nil {
|
||||
log.Error("Unable to create cookie auth token ", err.Error())
|
||||
http.Redirect(w, r, g.FailureURL, http.StatusTemporaryRedirect)
|
||||
|
|
10
jwt/jwt.go
10
jwt/jwt.go
|
@ -7,11 +7,11 @@ import (
|
|||
"golang.org/x/net/context"
|
||||
|
||||
gojwt "github.com/dgrijalva/jwt-go"
|
||||
"github.com/influxdata/mrfusion"
|
||||
"github.com/influxdata/chronograf"
|
||||
)
|
||||
|
||||
// Test if JWT implements Authenticator
|
||||
var _ mrfusion.Authenticator = &JWT{}
|
||||
var _ chronograf.Authenticator = &JWT{}
|
||||
|
||||
// JWT represents a javascript web token that can be validated or marshaled into string.
|
||||
type JWT struct {
|
||||
|
@ -46,7 +46,7 @@ func (c *Claims) Valid() error {
|
|||
}
|
||||
|
||||
// Authenticate checks if the jwtToken is signed correctly and validates with Claims.
|
||||
func (j *JWT) Authenticate(ctx context.Context, jwtToken string) (mrfusion.Principal, error) {
|
||||
func (j *JWT) Authenticate(ctx context.Context, jwtToken string) (chronograf.Principal, error) {
|
||||
gojwt.TimeFunc = j.Now
|
||||
|
||||
// Check for expected signing method.
|
||||
|
@ -73,11 +73,11 @@ func (j *JWT) Authenticate(ctx context.Context, jwtToken string) (mrfusion.Princ
|
|||
return "", fmt.Errorf("unable to convert claims to standard claims")
|
||||
}
|
||||
|
||||
return mrfusion.Principal(claims.Subject), nil
|
||||
return chronograf.Principal(claims.Subject), nil
|
||||
}
|
||||
|
||||
// Token creates a signed JWT token from user that expires at Now + duration
|
||||
func (j *JWT) Token(ctx context.Context, user mrfusion.Principal, duration time.Duration) (string, error) {
|
||||
func (j *JWT) Token(ctx context.Context, user chronograf.Principal, duration time.Duration) (string, error) {
|
||||
// Create a new token object, specifying signing method and the claims
|
||||
// you would like it to contain.
|
||||
now := j.Now()
|
||||
|
|
|
@ -6,8 +6,8 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/influxdata/mrfusion"
|
||||
"github.com/influxdata/mrfusion/jwt"
|
||||
"github.com/influxdata/chronograf"
|
||||
"github.com/influxdata/chronograf/jwt"
|
||||
)
|
||||
|
||||
func TestAuthenticate(t *testing.T) {
|
||||
|
@ -15,7 +15,7 @@ func TestAuthenticate(t *testing.T) {
|
|||
Desc string
|
||||
Secret string
|
||||
Token string
|
||||
User mrfusion.Principal
|
||||
User chronograf.Principal
|
||||
Err error
|
||||
}{
|
||||
{
|
||||
|
@ -83,7 +83,7 @@ func TestToken(t *testing.T) {
|
|||
return time.Unix(-446774400, 0)
|
||||
},
|
||||
}
|
||||
if token, err := j.Token(context.Background(), mrfusion.Principal("/chronograf/v1/users/1"), duration); err != nil {
|
||||
if token, err := j.Token(context.Background(), chronograf.Principal("/chronograf/v1/users/1"), duration); err != nil {
|
||||
t.Errorf("Error creating token for user: %v", err)
|
||||
} else if token != expected {
|
||||
t.Errorf("Error creating token; expected: %s actual: %s", "", token)
|
||||
|
|
14
uuid/v4.go
14
uuid/v4.go
|
@ -5,11 +5,11 @@ import (
|
|||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/influxdata/mrfusion"
|
||||
"github.com/influxdata/chronograf"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
)
|
||||
|
||||
// V4 implements mrfusion.ID
|
||||
// V4 implements chronograf.ID
|
||||
type V4 struct{}
|
||||
|
||||
// Generate creates a UUID v4 string
|
||||
|
@ -17,13 +17,13 @@ func (i *V4) Generate() (string, error) {
|
|||
return uuid.NewV4().String(), nil
|
||||
}
|
||||
|
||||
// APIKey implements mrfusion.Authenticator using V4
|
||||
// APIKey implements chronograf.Authenticator using V4
|
||||
type APIKey struct {
|
||||
Key string
|
||||
}
|
||||
|
||||
// NewAPIKey creates an APIKey with a UUID v4 Key
|
||||
func NewAPIKey() mrfusion.Authenticator {
|
||||
func NewAPIKey() chronograf.Authenticator {
|
||||
v4 := V4{}
|
||||
key, _ := v4.Generate()
|
||||
return &APIKey{
|
||||
|
@ -32,14 +32,14 @@ func NewAPIKey() mrfusion.Authenticator {
|
|||
}
|
||||
|
||||
// Authenticate checks the key against the UUID v4 key
|
||||
func (k *APIKey) Authenticate(ctx context.Context, key string) (mrfusion.Principal, error) {
|
||||
func (k *APIKey) Authenticate(ctx context.Context, key string) (chronograf.Principal, error) {
|
||||
if key != k.Key {
|
||||
return "", mrfusion.ErrAuthentication
|
||||
return "", chronograf.ErrAuthentication
|
||||
}
|
||||
return "admin", nil
|
||||
}
|
||||
|
||||
// Token returns the UUID v4 key
|
||||
func (k *APIKey) Token(context.Context, mrfusion.Principal, time.Duration) (string, error) {
|
||||
func (k *APIKey) Token(context.Context, chronograf.Principal, time.Duration) (string, error) {
|
||||
return k.Key, nil
|
||||
}
|
||||
|
|
|
@ -4,8 +4,8 @@ import (
|
|||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/influxdata/mrfusion"
|
||||
"github.com/influxdata/mrfusion/uuid"
|
||||
"github.com/influxdata/chronograf"
|
||||
"github.com/influxdata/chronograf/uuid"
|
||||
)
|
||||
|
||||
func TestAuthenticate(t *testing.T) {
|
||||
|
@ -14,14 +14,14 @@ func TestAuthenticate(t *testing.T) {
|
|||
APIKey string
|
||||
Key string
|
||||
Err error
|
||||
User mrfusion.Principal
|
||||
User chronograf.Principal
|
||||
}{
|
||||
|
||||
{
|
||||
Desc: "Test auth err when keys are different",
|
||||
APIKey: "key",
|
||||
Key: "badkey",
|
||||
Err: mrfusion.ErrAuthentication,
|
||||
Err: chronograf.ErrAuthentication,
|
||||
User: "",
|
||||
},
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue