Skip to content
This repository was archived by the owner on Jan 7, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions uchiwa/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ package uchiwa
import (
"fmt"

"github.com/sensu/uchiwa/uchiwa/logger"
log "github.com/Sirupsen/logrus"
)

// DeleteAggregate deletes a specific aggregate
func (u *Uchiwa) DeleteAggregate(name, dc string) error {
api, err := getAPI(u.Datacenters, dc)
if err != nil {
logger.Warning(err)
log.Warn(err)
return err
}

err = api.DeleteAggregate(name)
if err != nil {
logger.Warning(err)
log.Warn(err)
return err
}

Expand All @@ -27,13 +27,13 @@ func (u *Uchiwa) DeleteAggregate(name, dc string) error {
func (u *Uchiwa) GetAggregate(name, dc string) (*map[string]interface{}, error) {
api, err := getAPI(u.Datacenters, dc)
if err != nil {
logger.Warning(err)
log.Warn(err)
return nil, err
}

aggregate, err := api.GetAggregate(name)
if err != nil {
logger.Warning(err)
log.Warn(err)
return nil, err
}

Expand All @@ -44,13 +44,13 @@ func (u *Uchiwa) GetAggregate(name, dc string) (*map[string]interface{}, error)
func (u *Uchiwa) GetAggregateChecks(name, dc string) (*[]interface{}, error) {
api, err := getAPI(u.Datacenters, dc)
if err != nil {
logger.Warning(err)
log.Warn(err)
return nil, err
}

checks, err := api.GetAggregateChecks(name)
if err != nil {
logger.Warning(err)
log.Warn(err)
return nil, err
}

Expand All @@ -61,13 +61,13 @@ func (u *Uchiwa) GetAggregateChecks(name, dc string) (*[]interface{}, error) {
func (u *Uchiwa) GetAggregateClients(name, dc string) (*[]interface{}, error) {
api, err := getAPI(u.Datacenters, dc)
if err != nil {
logger.Warning(err)
log.Warn(err)
return nil, err
}

clients, err := api.GetAggregateClients(name)
if err != nil {
logger.Warning(err)
log.Warn(err)
return nil, err
}

Expand All @@ -78,13 +78,13 @@ func (u *Uchiwa) GetAggregateClients(name, dc string) (*[]interface{}, error) {
func (u *Uchiwa) GetAggregateResults(name, severity, dc string) (*[]interface{}, error) {
api, err := getAPI(u.Datacenters, dc)
if err != nil {
logger.Warning(err)
log.Warn(err)
return nil, err
}

results, err := api.GetAggregateResults(name, severity)
if err != nil {
logger.Warning(err)
log.Warn(err)
return nil, err
}

Expand All @@ -96,7 +96,9 @@ func (u *Uchiwa) findAggregate(name string) ([]interface{}, error) {
for _, c := range u.Data.Aggregates {
m, ok := c.(map[string]interface{})
if !ok {
logger.Warningf("Could not assert this check to an interface %+v", c)
log.WithFields(log.Fields{
"interface": c,
}).Warn("Could not assert this check to an interface.")
continue
}
if m["name"] == name {
Expand Down
4 changes: 2 additions & 2 deletions uchiwa/authentication/accessToken.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

"github.com/dgrijalva/jwt-go"
"github.com/sensu/uchiwa/uchiwa/logger"
log "github.com/Sirupsen/logrus"
)

// TokenLocation represents a function that accepts a request as input and returns
Expand All @@ -24,7 +24,7 @@ func accessTokenFromAuthHeader(r *http.Request) (string, error) {

authorizationComponents := strings.Split(authorization, " ")
if len(authorizationComponents) != 2 || strings.ToLower(authorizationComponents[0]) != "token" {
logger.Debug("Invalid authorization header. The format must be: token {token}")
log.Debug("Invalid authorization header. The format must be: token {token}")
return "", errors.New("")
}

Expand Down
18 changes: 11 additions & 7 deletions uchiwa/authentication/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/gorilla/context"
"github.com/sensu/uchiwa/uchiwa/audit"
"github.com/sensu/uchiwa/uchiwa/helpers"
"github.com/sensu/uchiwa/uchiwa/logger"
"github.com/sensu/uchiwa/uchiwa/structs"
log "github.com/Sirupsen/logrus"
)

// New function initalizes and returns a Config struct
Expand All @@ -33,7 +33,7 @@ func restrictedHandler(next http.Handler) http.Handler {
// Verify the JWT
token, err := verifyJWT(r)
if err != nil {
logger.Debug("No JWT token provided")
log.Debug("No JWT token provided")
}

// Verify the access token if no JWT was provided
Expand All @@ -43,7 +43,7 @@ func restrictedHandler(next http.Handler) http.Handler {

// If no JWT or access token found
if err != nil {
logger.Debug("No access token provided")
log.Debug("No access token provided")
http.Error(w, "Request unauthorized", http.StatusUnauthorized)
return
}
Expand Down Expand Up @@ -71,29 +71,33 @@ func (a *Config) Login() http.Handler {
var data interface{}
err := decoder.Decode(&data)
if err != nil {
logger.Warningf("Could not decode the body: %s", err)
log.WithFields(log.Fields{
"error": err,
}).Warn("Could not decode the body.")
http.Error(w, "", http.StatusInternalServerError)
return
}

m, ok := data.(map[string]interface{})
if !ok {
logger.Warningf("Could not assert the body: %s", err)
log.WithFields(log.Fields{
"error": err,
}).Warn("Could not assert the body.")
http.Error(w, "", http.StatusInternalServerError)
return
}

u := m["user"].(string)
p := m["pass"].(string)
if u == "" || p == "" {
logger.Info("Authentication failed: user and password must not be empty")
log.Info("Authentication failed: user and password must not be empty")
http.Error(w, "", http.StatusUnauthorized)
return
}

user, err := a.login(u, p)
if err != nil {
logger.Info(err)
log.Info(err)

// Output to audit log
log := structs.AuditLog{Action: "loginfailure", Level: "default", Output: err.Error()}
Expand Down
36 changes: 24 additions & 12 deletions uchiwa/authentication/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/dgrijalva/jwt-go"
"github.com/gorilla/context"
"github.com/mitchellh/mapstructure"
"github.com/sensu/uchiwa/uchiwa/logger"
"github.com/sensu/uchiwa/uchiwa/structs"
log "github.com/Sirupsen/logrus"
)

// JWTToken constant
Expand Down Expand Up @@ -66,7 +66,9 @@ func GetToken(role *Role, username string) (string, error) {
func generateKeyPair() *rsa.PrivateKey {
keypair, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
logger.Fatalf("Could not generate an RSA keypair: %s", err)
log.WithFields(log.Fields{
"error": err,
}).Fatal("Could not generate an RSA keypair.")
}

return keypair
Expand All @@ -75,7 +77,7 @@ func generateKeyPair() *rsa.PrivateKey {
// generateToken generates a private and public RSA keys
// in order to be used for the JWT signature
func generateToken() (*rsa.PrivateKey, *rsa.PublicKey) {
logger.Debug("Generating new temporary RSA keys")
log.Debug("Generating new temporary RSA keys")
privateKey := generateKeyPair()
// Precompute some calculations
privateKey.Precompute()
Expand All @@ -92,15 +94,15 @@ func initToken(a structs.Auth) {
privateKey, publicKey, err = loadToken(a)
if err != nil {
// At this point we need to generate temporary RSA keys
logger.Debug(err)
log.Debug(err)
privateKey, publicKey = generateToken()
}
}

// loadToken loads a private and public RSA keys from the filesystem
// in order to be used for the JWT signature
func loadToken(a structs.Auth) (*rsa.PrivateKey, *rsa.PublicKey, error) {
logger.Debug("Attempting to load the RSA keys from the filesystem")
log.Debug("Attempting to load the RSA keys from the filesystem")

if a.PrivateKey == "" || a.PublicKey == "" {
return nil, nil, errors.New("The paths to the private and public RSA keys were not provided")
Expand All @@ -109,24 +111,32 @@ func loadToken(a structs.Auth) (*rsa.PrivateKey, *rsa.PublicKey, error) {
// Read the files from the filesystem
prv, err := ioutil.ReadFile(a.PrivateKey)
if err != nil {
logger.Fatalf("Unable to open the private key file: %v", err)
log.WithFields(log.Fields{
"error": err,
}).Fatal("Unable to open the private key file.")
}
pub, err := ioutil.ReadFile(a.PublicKey)
if err != nil {
logger.Fatalf("Unable to open the public key file: %v", err)
log.WithFields(log.Fields{
"error": err,
}).Fatal("Unable to open the public key file.")
}

// Parse the RSA keys
privateKey, err := jwt.ParseRSAPrivateKeyFromPEM(prv)
if err != nil {
logger.Fatalf("Unable to parse the private key: %v", err)
log.WithFields(log.Fields{
"error": err,
}).Fatal("Unable to parse the private key file.")
}
publicKey, err := jwt.ParseRSAPublicKeyFromPEM(pub)
if err != nil {
logger.Fatalf("Unable to parse the public key: %v", err)
log.WithFields(log.Fields{
"error": err,
}).Fatal("Unable to parse the public key.")
}

logger.Info("Provided RSA keys successfully loaded")
log.Info("Provided RSA keys successfully loaded")
return privateKey, publicKey, nil
}

Expand All @@ -139,7 +149,9 @@ func setJWTInContext(r *http.Request, token *jwt.Token) {
func verifyJWT(r *http.Request) (*jwt.Token, error) {
token, err := jwt.ParseFromRequest(r, func(t *jwt.Token) (interface{}, error) {
if _, ok := t.Method.(*jwt.SigningMethodRSA); !ok {
logger.Debugf("Unexpected signing method: %v", t.Header["alg"])
log.WithFields(log.Fields{
"method": t.Header["alg"],
}).Debug("Unexpected signing method.")
return nil, errors.New("")
}
return publicKey, nil
Expand All @@ -150,7 +162,7 @@ func verifyJWT(r *http.Request) (*jwt.Token, error) {
}

if !token.Valid {
logger.Debug("Invalid JWT")
log.Debug("Invalid JWT")
return nil, errors.New("")
}

Expand Down
6 changes: 3 additions & 3 deletions uchiwa/authorization/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"net/http"

"github.com/sensu/uchiwa/uchiwa/authentication"
"github.com/sensu/uchiwa/uchiwa/logger"
log "github.com/Sirupsen/logrus"
)

// Authorization contains the different methods used for authorizing
Expand Down Expand Up @@ -44,13 +44,13 @@ func isReadOnly(r *http.Request) bool {

token := authentication.GetJWTFromContext(r)
if token == nil { // authentication is not enabled
logger.Debug("No JWT found in context")
log.Debug("No JWT found in context")
return false
}

role, err := authentication.GetRoleFromToken(token)
if err != nil {
logger.Debug("Invalid token: %s", err)
log.Debug("Invalid token: %s", err)
return true
}

Expand Down
10 changes: 6 additions & 4 deletions uchiwa/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ package uchiwa
import (
"fmt"

"github.com/sensu/uchiwa/uchiwa/logger"
"github.com/sensu/uchiwa/uchiwa/structs"
log "github.com/Sirupsen/logrus"
)

// IssueCheckExecution sends a POST request to the /stashes endpoint in order to create a stash
func (u *Uchiwa) IssueCheckExecution(data structs.CheckExecution) error {
api, err := getAPI(u.Datacenters, data.Dc)
if err != nil {
logger.Warning(err)
log.Warn(err)
return err
}

_, err = api.IssueCheckExecution(data)
if err != nil {
logger.Warning(err)
log.Warn(err)
return err
}

Expand All @@ -29,7 +29,9 @@ func (u *Uchiwa) findCheck(name string) ([]interface{}, error) {
for _, c := range u.Data.Checks {
m, ok := c.(map[string]interface{})
if !ok {
logger.Warningf("Could not assert this check to an interface %+v", c)
log.WithFields(log.Fields{
"interface": c,
}).Warn("Could not assert this check to an interface.")
continue
}
if m["name"] == name {
Expand Down
Loading