Update to chronograf from mrfusion

pull/10616/head
Chris Goller 2016-10-20 18:01:14 -05:00
parent 0c97550e2c
commit deed69a946
8 changed files with 52 additions and 52 deletions

View File

@ -3,8 +3,8 @@ package handlers
import ( import (
"net/http" "net/http"
"github.com/influxdata/mrfusion" "github.com/influxdata/chronograf"
"github.com/influxdata/mrfusion/dist" "github.com/influxdata/chronograf/dist"
) )
const ( const (
@ -17,12 +17,12 @@ type AssetsOpts struct {
// Develop when true serves assets from ui/build directory directly; false will use internal bindata. // Develop when true serves assets from ui/build directory directly; false will use internal bindata.
Develop bool Develop bool
// Logger will log the asset served // Logger will log the asset served
Logger mrfusion.Logger Logger chronograf.Logger
} }
// Assets creates a middleware that will serve a single page app. // Assets creates a middleware that will serve a single page app.
func Assets(opts AssetsOpts) http.Handler { func Assets(opts AssetsOpts) http.Handler {
var assets mrfusion.Assets var assets chronograf.Assets
if opts.Develop { if opts.Develop {
assets = &dist.DebugAssets{ assets = &dist.DebugAssets{
Dir: Dir, Dir: Dir,

View File

@ -5,7 +5,7 @@ import (
"net/http" "net/http"
"strings" "strings"
"github.com/influxdata/mrfusion" "github.com/influxdata/chronograf"
) )
// CookieExtractor extracts the token from the value of the Name cookie. // 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) { func (c *CookieExtractor) Extract(r *http.Request) (string, error) {
cookie, err := r.Cookie(c.Name) cookie, err := r.Cookie(c.Name)
if err != nil { if err != nil {
return "", mrfusion.ErrAuthentication return "", chronograf.ErrAuthentication
} }
return cookie.Value, nil return cookie.Value, nil
} }
@ -29,14 +29,14 @@ type BearerExtractor struct{}
func (b *BearerExtractor) Extract(r *http.Request) (string, error) { func (b *BearerExtractor) Extract(r *http.Request) (string, error) {
s := r.Header.Get("Authorization") s := r.Header.Get("Authorization")
if s == "" { if s == "" {
return "", mrfusion.ErrAuthentication return "", chronograf.ErrAuthentication
} }
// Check for Bearer token. // Check for Bearer token.
strs := strings.Split(s, " ") strs := strings.Split(s, " ")
if len(strs) != 2 || strs[0] != "Bearer" { if len(strs) != 2 || strs[0] != "Bearer" {
return "", mrfusion.ErrAuthentication return "", chronograf.ErrAuthentication
} }
return strs[1], nil 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 // 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. // Context. It is up to the next handler to determine if the principal has access.
// On failure, will return http.StatusUnauthorized. // 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) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log := logger. log := logger.
WithField("component", "auth"). WithField("component", "auth").
@ -69,7 +69,7 @@ func AuthorizedToken(auth mrfusion.Authenticator, te mrfusion.TokenExtractor, lo
} }
// Send the principal to the next handler // 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)) next.ServeHTTP(w, r.WithContext(ctx))
return return
}) })

View File

@ -9,9 +9,9 @@ import (
"golang.org/x/net/context" "golang.org/x/net/context"
"github.com/influxdata/mrfusion" "github.com/influxdata/chronograf"
"github.com/influxdata/mrfusion/handlers" "github.com/influxdata/chronograf/handlers"
fusionlog "github.com/influxdata/mrfusion/log" clog "github.com/influxdata/chronograf/log"
) )
func TestCookieExtractor(t *testing.T) { func TestCookieExtractor(t *testing.T) {
@ -29,7 +29,7 @@ func TestCookieExtractor(t *testing.T) {
Value: "reallyimportant", Value: "reallyimportant",
Lookup: "Doesntexist", Lookup: "Doesntexist",
Expected: "", Expected: "",
Err: mrfusion.ErrAuthentication, Err: chronograf.ErrAuthentication,
}, },
{ {
Desc: "Cookie token extracted", Desc: "Cookie token extracted",
@ -47,7 +47,7 @@ func TestCookieExtractor(t *testing.T) {
Value: test.Value, Value: test.Value,
}) })
var e mrfusion.TokenExtractor = &handlers.CookieExtractor{ var e chronograf.TokenExtractor = &handlers.CookieExtractor{
Name: test.Lookup, Name: test.Lookup,
} }
actual, err := e.Extract(req) actual, err := e.Extract(req)
@ -75,21 +75,21 @@ func TestBearerExtractor(t *testing.T) {
Header: "Doesntexist", Header: "Doesntexist",
Value: "reallyimportant", Value: "reallyimportant",
Expected: "", Expected: "",
Err: mrfusion.ErrAuthentication, Err: chronograf.ErrAuthentication,
}, },
{ {
Desc: "Auth header doesn't have Bearer", Desc: "Auth header doesn't have Bearer",
Header: "Authorization", Header: "Authorization",
Value: "Bad Value", Value: "Bad Value",
Expected: "", Expected: "",
Err: mrfusion.ErrAuthentication, Err: chronograf.ErrAuthentication,
}, },
{ {
Desc: "Auth header doesn't have Bearer token", Desc: "Auth header doesn't have Bearer token",
Header: "Authorization", Header: "Authorization",
Value: "Bearer", Value: "Bearer",
Expected: "", Expected: "",
Err: mrfusion.ErrAuthentication, Err: chronograf.ErrAuthentication,
}, },
{ {
Desc: "Authorization Bearer token success", Desc: "Authorization Bearer token success",
@ -103,7 +103,7 @@ func TestBearerExtractor(t *testing.T) {
req, _ := http.NewRequest("", "http://howdy.com", nil) req, _ := http.NewRequest("", "http://howdy.com", nil)
req.Header.Add(test.Header, test.Value) req.Header.Add(test.Header, test.Value)
var e mrfusion.TokenExtractor = &handlers.BearerExtractor{} var e chronograf.TokenExtractor = &handlers.BearerExtractor{}
actual, err := e.Extract(req) actual, err := e.Extract(req)
if err != test.Err { if err != test.Err {
t.Errorf("Bearer extract error; expected %v actual %v", test.Err, 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 { type MockAuthenticator struct {
Principal mrfusion.Principal Principal chronograf.Principal
Err error 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 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 return "", m.Err
} }
@ -140,7 +140,7 @@ func TestAuthorizedToken(t *testing.T) {
var tests = []struct { var tests = []struct {
Desc string Desc string
Code int Code int
Principal mrfusion.Principal Principal chronograf.Principal
ExtractorErr error ExtractorErr error
AuthErr error AuthErr error
Expected string Expected string
@ -165,10 +165,10 @@ func TestAuthorizedToken(t *testing.T) {
for _, test := range tests { for _, test := range tests {
// next is a sentinel StatusOK and // next is a sentinel StatusOK and
// principal recorder. // principal recorder.
var principal mrfusion.Principal var principal chronograf.Principal
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) 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) req, _ := http.NewRequest("GET", "", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@ -181,7 +181,7 @@ func TestAuthorizedToken(t *testing.T) {
Principal: test.Principal, Principal: test.Principal,
} }
logger := fusionlog.New() logger := clog.New()
handler := handlers.AuthorizedToken(a, e, logger, next) handler := handlers.AuthorizedToken(a, e, logger, next)
handler.ServeHTTP(w, req) handler.ServeHTTP(w, req)
if w.Code != test.Code { if w.Code != test.Code {

View File

@ -9,7 +9,7 @@ import (
"time" "time"
"github.com/google/go-github/github" "github.com/google/go-github/github"
"github.com/influxdata/mrfusion" "github.com/influxdata/chronograf"
"golang.org/x/oauth2" "golang.org/x/oauth2"
ogh "golang.org/x/oauth2/github" ogh "golang.org/x/oauth2/github"
) )
@ -38,18 +38,18 @@ func NewCookie() Cookie {
// the user's primary Github email address. // the user's primary Github email address.
type Github struct { type Github struct {
Cookie Cookie Cookie Cookie
Authenticator mrfusion.Authenticator Authenticator chronograf.Authenticator
ClientID string ClientID string
ClientSecret string ClientSecret string
Scopes []string Scopes []string
SuccessURL string // SuccessURL is redirect location after successful authorization SuccessURL string // SuccessURL is redirect location after successful authorization
FailureURL string // FailureURL is redirect location after authorization failure FailureURL string // FailureURL is redirect location after authorization failure
Now func() time.Time Now func() time.Time
Logger mrfusion.Logger Logger chronograf.Logger
} }
// NewGithub constructs a Github with default cookie behavior and scopes. // 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{ return Github{
ClientID: clientID, ClientID: clientID,
ClientSecret: clientSecret, 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. // 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. // If the callback is not received within 10 minutes, then authorization will fail.
csrf := randomString(32) // 32 is not important... just long 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 // This is likely an internal server error
if err != nil { if err != nil {
g.Logger. 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 // 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 { if err != nil {
log.Error("Unable to create cookie auth token ", err.Error()) log.Error("Unable to create cookie auth token ", err.Error())
http.Redirect(w, r, g.FailureURL, http.StatusTemporaryRedirect) http.Redirect(w, r, g.FailureURL, http.StatusTemporaryRedirect)

View File

@ -7,11 +7,11 @@ import (
"golang.org/x/net/context" "golang.org/x/net/context"
gojwt "github.com/dgrijalva/jwt-go" gojwt "github.com/dgrijalva/jwt-go"
"github.com/influxdata/mrfusion" "github.com/influxdata/chronograf"
) )
// Test if JWT implements Authenticator // 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. // JWT represents a javascript web token that can be validated or marshaled into string.
type JWT struct { type JWT struct {
@ -46,7 +46,7 @@ func (c *Claims) Valid() error {
} }
// Authenticate checks if the jwtToken is signed correctly and validates with Claims. // 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 gojwt.TimeFunc = j.Now
// Check for expected signing method. // 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 "", 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 // 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 // Create a new token object, specifying signing method and the claims
// you would like it to contain. // you would like it to contain.
now := j.Now() now := j.Now()

View File

@ -6,8 +6,8 @@ import (
"testing" "testing"
"time" "time"
"github.com/influxdata/mrfusion" "github.com/influxdata/chronograf"
"github.com/influxdata/mrfusion/jwt" "github.com/influxdata/chronograf/jwt"
) )
func TestAuthenticate(t *testing.T) { func TestAuthenticate(t *testing.T) {
@ -15,7 +15,7 @@ func TestAuthenticate(t *testing.T) {
Desc string Desc string
Secret string Secret string
Token string Token string
User mrfusion.Principal User chronograf.Principal
Err error Err error
}{ }{
{ {
@ -83,7 +83,7 @@ func TestToken(t *testing.T) {
return time.Unix(-446774400, 0) 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) t.Errorf("Error creating token for user: %v", err)
} else if token != expected { } else if token != expected {
t.Errorf("Error creating token; expected: %s actual: %s", "", token) t.Errorf("Error creating token; expected: %s actual: %s", "", token)

View File

@ -5,11 +5,11 @@ import (
"golang.org/x/net/context" "golang.org/x/net/context"
"github.com/influxdata/mrfusion" "github.com/influxdata/chronograf"
uuid "github.com/satori/go.uuid" uuid "github.com/satori/go.uuid"
) )
// V4 implements mrfusion.ID // V4 implements chronograf.ID
type V4 struct{} type V4 struct{}
// Generate creates a UUID v4 string // Generate creates a UUID v4 string
@ -17,13 +17,13 @@ func (i *V4) Generate() (string, error) {
return uuid.NewV4().String(), nil return uuid.NewV4().String(), nil
} }
// APIKey implements mrfusion.Authenticator using V4 // APIKey implements chronograf.Authenticator using V4
type APIKey struct { type APIKey struct {
Key string Key string
} }
// NewAPIKey creates an APIKey with a UUID v4 Key // NewAPIKey creates an APIKey with a UUID v4 Key
func NewAPIKey() mrfusion.Authenticator { func NewAPIKey() chronograf.Authenticator {
v4 := V4{} v4 := V4{}
key, _ := v4.Generate() key, _ := v4.Generate()
return &APIKey{ return &APIKey{
@ -32,14 +32,14 @@ func NewAPIKey() mrfusion.Authenticator {
} }
// Authenticate checks the key against the UUID v4 key // 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 { if key != k.Key {
return "", mrfusion.ErrAuthentication return "", chronograf.ErrAuthentication
} }
return "admin", nil return "admin", nil
} }
// Token returns the UUID v4 key // 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 return k.Key, nil
} }

View File

@ -4,8 +4,8 @@ import (
"context" "context"
"testing" "testing"
"github.com/influxdata/mrfusion" "github.com/influxdata/chronograf"
"github.com/influxdata/mrfusion/uuid" "github.com/influxdata/chronograf/uuid"
) )
func TestAuthenticate(t *testing.T) { func TestAuthenticate(t *testing.T) {
@ -14,14 +14,14 @@ func TestAuthenticate(t *testing.T) {
APIKey string APIKey string
Key string Key string
Err error Err error
User mrfusion.Principal User chronograf.Principal
}{ }{
{ {
Desc: "Test auth err when keys are different", Desc: "Test auth err when keys are different",
APIKey: "key", APIKey: "key",
Key: "badkey", Key: "badkey",
Err: mrfusion.ErrAuthentication, Err: chronograf.ErrAuthentication,
User: "", User: "",
}, },
{ {