2019-01-24 16:26:36 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package consoleweb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2019-04-10 00:14:19 +01:00
|
|
|
"html/template"
|
2019-01-24 16:26:36 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2019-04-10 00:14:19 +01:00
|
|
|
"path"
|
2019-01-24 16:26:36 +00:00
|
|
|
"path/filepath"
|
2019-03-19 17:55:43 +00:00
|
|
|
"strconv"
|
2019-03-26 15:56:16 +00:00
|
|
|
"strings"
|
2019-04-10 00:14:19 +01:00
|
|
|
"time"
|
2019-01-24 16:26:36 +00:00
|
|
|
|
|
|
|
"github.com/graphql-go/graphql"
|
2019-04-10 00:14:19 +01:00
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
2019-01-24 16:26:36 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
2019-02-06 13:19:14 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2019-01-24 16:26:36 +00:00
|
|
|
|
|
|
|
"storj.io/storj/pkg/auth"
|
|
|
|
"storj.io/storj/satellite/console"
|
|
|
|
"storj.io/storj/satellite/console/consoleweb/consoleql"
|
2019-03-02 15:22:20 +00:00
|
|
|
"storj.io/storj/satellite/mailservice"
|
2019-01-24 16:26:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
authorization = "Authorization"
|
|
|
|
contentType = "Content-Type"
|
|
|
|
|
|
|
|
authorizationBearer = "Bearer "
|
|
|
|
|
|
|
|
applicationJSON = "application/json"
|
|
|
|
applicationGraphql = "application/graphql"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Error is satellite console error type
|
|
|
|
var Error = errs.Class("satellite console error")
|
|
|
|
|
|
|
|
// Config contains configuration for console web server
|
|
|
|
type Config struct {
|
2019-03-26 15:56:16 +00:00
|
|
|
Address string `help:"server address of the graphql api gateway and frontend app" default:"127.0.0.1:8081"`
|
|
|
|
StaticDir string `help:"path to static resources" default:""`
|
|
|
|
ExternalAddress string `help:"external endpoint of the satellite if hosted" default:""`
|
2019-02-05 17:31:53 +00:00
|
|
|
|
2019-03-19 17:55:43 +00:00
|
|
|
// TODO: remove after Vanguard release
|
2019-05-28 15:32:51 +01:00
|
|
|
AuthToken string `help:"auth token needed for access to registration token creation endpoint" default:""`
|
|
|
|
AuthTokenSecret string `help:"secret used to sign auth tokens" releaseDefault:"" devDefault:"my-suppa-secret-key"`
|
2019-03-19 17:55:43 +00:00
|
|
|
|
2019-02-05 17:31:53 +00:00
|
|
|
PasswordCost int `internal:"true" help:"password hashing cost (0=automatic)" default:"0"`
|
2019-01-24 16:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Server represents console web server
|
|
|
|
type Server struct {
|
|
|
|
log *zap.Logger
|
|
|
|
|
2019-03-02 15:22:20 +00:00
|
|
|
config Config
|
|
|
|
service *console.Service
|
|
|
|
mailService *mailservice.Service
|
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
listener net.Listener
|
2019-03-02 15:22:20 +00:00
|
|
|
server http.Server
|
2019-01-24 16:26:36 +00:00
|
|
|
|
|
|
|
schema graphql.Schema
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer creates new instance of console server
|
2019-03-02 15:22:20 +00:00
|
|
|
func NewServer(logger *zap.Logger, config Config, service *console.Service, mailService *mailservice.Service, listener net.Listener) *Server {
|
2019-01-24 16:26:36 +00:00
|
|
|
server := Server{
|
2019-03-02 15:22:20 +00:00
|
|
|
log: logger,
|
|
|
|
config: config,
|
|
|
|
listener: listener,
|
|
|
|
service: service,
|
|
|
|
mailService: mailService,
|
2019-01-24 16:26:36 +00:00
|
|
|
}
|
|
|
|
|
2019-05-14 16:13:18 +01:00
|
|
|
logger.Sugar().Debugf("Starting Satellite UI on %s...", server.listener.Addr().String())
|
2019-02-28 20:12:52 +00:00
|
|
|
|
2019-03-26 15:56:16 +00:00
|
|
|
if server.config.ExternalAddress != "" {
|
|
|
|
if !strings.HasSuffix(server.config.ExternalAddress, "/") {
|
|
|
|
server.config.ExternalAddress = server.config.ExternalAddress + "/"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
server.config.ExternalAddress = "http://" + server.listener.Addr().String() + "/"
|
|
|
|
}
|
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
fs := http.FileServer(http.Dir(server.config.StaticDir))
|
|
|
|
|
|
|
|
mux.Handle("/api/graphql/v0", http.HandlerFunc(server.grapqlHandler))
|
|
|
|
|
|
|
|
if server.config.StaticDir != "" {
|
2019-03-08 14:01:11 +00:00
|
|
|
mux.Handle("/activation/", http.HandlerFunc(server.accountActivationHandler))
|
2019-04-10 20:16:10 +01:00
|
|
|
mux.Handle("/password-recovery/", http.HandlerFunc(server.passwordRecoveryHandler))
|
2019-05-13 16:53:52 +01:00
|
|
|
mux.Handle("/cancel-password-recovery/", http.HandlerFunc(server.cancelPasswordRecoveryHandler))
|
2019-03-19 17:55:43 +00:00
|
|
|
mux.Handle("/registrationToken/", http.HandlerFunc(server.createRegistrationTokenHandler))
|
2019-04-10 00:14:19 +01:00
|
|
|
mux.Handle("/usage-report/", http.HandlerFunc(server.bucketUsageReportHandler))
|
2019-01-24 16:26:36 +00:00
|
|
|
mux.Handle("/static/", http.StripPrefix("/static", fs))
|
2019-03-08 14:01:11 +00:00
|
|
|
mux.Handle("/", http.HandlerFunc(server.appHandler))
|
2019-01-24 16:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
server.server = http.Server{
|
|
|
|
Handler: mux,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &server
|
|
|
|
}
|
|
|
|
|
|
|
|
// appHandler is web app http handler function
|
|
|
|
func (s *Server) appHandler(w http.ResponseWriter, req *http.Request) {
|
|
|
|
http.ServeFile(w, req, filepath.Join(s.config.StaticDir, "dist", "public", "index.html"))
|
|
|
|
}
|
|
|
|
|
2019-04-10 00:14:19 +01:00
|
|
|
// bucketUsageReportHandler generate bucket usage report page for project
|
|
|
|
func (s *Server) bucketUsageReportHandler(w http.ResponseWriter, req *http.Request) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
var projectID *uuid.UUID
|
|
|
|
var since, before time.Time
|
|
|
|
|
|
|
|
tokenCookie, err := req.Cookie("tokenKey")
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error("bucket usage report error", zap.Error(err))
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
http.ServeFile(w, req, filepath.Join(s.config.StaticDir, "static", "errors", "404.html"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
auth, err := s.service.Authorize(auth.WithAPIKey(req.Context(), []byte(tokenCookie.Value)))
|
|
|
|
if err != nil {
|
2019-04-23 13:56:15 +01:00
|
|
|
s.log.Error("bucket usage report error", zap.Error(err))
|
|
|
|
|
2019-04-10 00:14:19 +01:00
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
http.ServeFile(w, req, filepath.Join(s.config.StaticDir, "static", "errors", "404.html"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2019-04-23 13:56:15 +01:00
|
|
|
s.log.Error("bucket usage report error", zap.Error(err))
|
|
|
|
|
2019-04-10 00:14:19 +01:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
http.ServeFile(w, req, filepath.Join(s.config.StaticDir, "static", "errors", "404.html"))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// parse query params
|
|
|
|
projectID, err = uuid.Parse(req.URL.Query().Get("projectID"))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-04-23 13:56:15 +01:00
|
|
|
sinceStamp, err := strconv.ParseInt(req.URL.Query().Get("since"), 10, 64)
|
2019-04-10 00:14:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-04-23 13:56:15 +01:00
|
|
|
beforeStamp, err := strconv.ParseInt(req.URL.Query().Get("before"), 10, 64)
|
2019-04-10 00:14:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-23 13:56:15 +01:00
|
|
|
since = time.Unix(sinceStamp, 0)
|
|
|
|
before = time.Unix(beforeStamp, 0)
|
|
|
|
|
2019-04-10 00:14:19 +01:00
|
|
|
s.log.Debug("querying bucket usage report",
|
|
|
|
zap.String("projectID", projectID.String()),
|
|
|
|
zap.String("since", since.String()),
|
|
|
|
zap.String("before", before.String()))
|
|
|
|
|
|
|
|
ctx := console.WithAuth(context.Background(), auth)
|
|
|
|
bucketRollups, err := s.service.GetBucketUsageRollups(ctx, *projectID, since, before)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
report, err := template.ParseFiles(path.Join(s.config.StaticDir, "static", "reports", "UsageReport.html"))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = report.Execute(w, bucketRollups)
|
|
|
|
}
|
|
|
|
|
2019-03-19 17:55:43 +00:00
|
|
|
// accountActivationHandler is web app http handler function
|
|
|
|
func (s *Server) createRegistrationTokenHandler(w http.ResponseWriter, req *http.Request) {
|
|
|
|
w.Header().Set(contentType, applicationJSON)
|
|
|
|
|
|
|
|
var response struct {
|
|
|
|
Secret string `json:"secret"`
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err := json.NewEncoder(w).Encode(&response)
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error(err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
authToken := req.Header.Get("Authorization")
|
|
|
|
if authToken != s.config.AuthToken {
|
|
|
|
w.WriteHeader(401)
|
|
|
|
response.Error = "unauthorized"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
projectsLimitInput := req.URL.Query().Get("projectsLimit")
|
|
|
|
|
|
|
|
projectsLimit, err := strconv.Atoi(projectsLimitInput)
|
|
|
|
if err != nil {
|
|
|
|
response.Error = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
token, err := s.service.CreateRegToken(context.Background(), projectsLimit)
|
|
|
|
if err != nil {
|
|
|
|
response.Error = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Secret = token.Secret.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// accountActivationHandler is web app http handler function
|
2019-03-08 14:01:11 +00:00
|
|
|
func (s *Server) accountActivationHandler(w http.ResponseWriter, req *http.Request) {
|
|
|
|
activationToken := req.URL.Query().Get("token")
|
|
|
|
|
|
|
|
err := s.service.ActivateAccount(context.Background(), activationToken)
|
|
|
|
if err != nil {
|
2019-04-09 13:20:29 +01:00
|
|
|
s.log.Error("activation: failed to activate account",
|
|
|
|
zap.String("token", activationToken),
|
|
|
|
zap.Error(err))
|
|
|
|
|
2019-04-10 20:16:10 +01:00
|
|
|
s.serveError(w, req)
|
2019-03-08 14:01:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.ServeFile(w, req, filepath.Join(s.config.StaticDir, "static", "activation", "success.html"))
|
|
|
|
}
|
|
|
|
|
2019-04-10 20:16:10 +01:00
|
|
|
func (s *Server) passwordRecoveryHandler(w http.ResponseWriter, req *http.Request) {
|
|
|
|
recoveryToken := req.URL.Query().Get("token")
|
|
|
|
if len(recoveryToken) == 0 {
|
|
|
|
s.serveError(w, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch req.Method {
|
|
|
|
case "POST":
|
|
|
|
err := req.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
s.serveError(w, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
password := req.FormValue("password")
|
|
|
|
passwordRepeat := req.FormValue("passwordRepeat")
|
|
|
|
if strings.Compare(password, passwordRepeat) != 0 {
|
|
|
|
s.serveError(w, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.service.ResetPassword(context.Background(), recoveryToken, password)
|
|
|
|
if err != nil {
|
|
|
|
s.serveError(w, req)
|
|
|
|
}
|
2019-05-13 16:53:52 +01:00
|
|
|
http.ServeFile(w, req, filepath.Join(s.config.StaticDir, "static", "resetPassword", "success.html"))
|
2019-04-10 20:16:10 +01:00
|
|
|
default:
|
|
|
|
t, err := template.ParseFiles(filepath.Join(s.config.StaticDir, "static", "resetPassword", "resetPassword.html"))
|
|
|
|
if err != nil {
|
|
|
|
s.serveError(w, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = t.Execute(w, nil)
|
|
|
|
if err != nil {
|
|
|
|
s.serveError(w, req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-13 16:53:52 +01:00
|
|
|
func (s *Server) cancelPasswordRecoveryHandler(w http.ResponseWriter, req *http.Request) {
|
|
|
|
recoveryToken := req.URL.Query().Get("token")
|
|
|
|
if len(recoveryToken) == 0 {
|
|
|
|
http.Redirect(w, req, "https://storjlabs.atlassian.net/servicedesk/customer/portals", http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
// No need to check error as we anyway redirect user to support page
|
|
|
|
_ = s.service.RevokeResetPasswordToken(context.Background(), recoveryToken)
|
|
|
|
|
|
|
|
http.Redirect(w, req, "https://storjlabs.atlassian.net/servicedesk/customer/portals", http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
2019-04-10 20:16:10 +01:00
|
|
|
func (s *Server) serveError(w http.ResponseWriter, req *http.Request) {
|
|
|
|
http.ServeFile(w, req, filepath.Join(s.config.StaticDir, "static", "errors", "404.html"))
|
|
|
|
}
|
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
// grapqlHandler is graphql endpoint http handler function
|
|
|
|
func (s *Server) grapqlHandler(w http.ResponseWriter, req *http.Request) {
|
|
|
|
w.Header().Set(contentType, applicationJSON)
|
|
|
|
|
|
|
|
token := getToken(req)
|
|
|
|
query, err := getQuery(req)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := auth.WithAPIKey(context.Background(), []byte(token))
|
|
|
|
auth, err := s.service.Authorize(ctx)
|
|
|
|
if err != nil {
|
|
|
|
ctx = console.WithAuthFailure(ctx, err)
|
|
|
|
} else {
|
|
|
|
ctx = console.WithAuth(ctx, auth)
|
|
|
|
}
|
|
|
|
|
2019-03-02 15:22:20 +00:00
|
|
|
rootObject := make(map[string]interface{})
|
2019-03-26 15:56:16 +00:00
|
|
|
|
|
|
|
rootObject["origin"] = s.config.ExternalAddress
|
2019-03-08 14:01:11 +00:00
|
|
|
rootObject[consoleql.ActivationPath] = "activation/?token="
|
2019-04-10 20:16:10 +01:00
|
|
|
rootObject[consoleql.PasswordRecoveryPath] = "password-recovery/?token="
|
2019-05-13 16:53:52 +01:00
|
|
|
rootObject[consoleql.CancelPasswordRecoveryPath] = "cancel-password-recovery/?token="
|
2019-03-26 15:56:16 +00:00
|
|
|
rootObject[consoleql.SignInPath] = "login"
|
2019-03-02 15:22:20 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
result := graphql.Do(graphql.Params{
|
|
|
|
Schema: s.schema,
|
|
|
|
Context: ctx,
|
|
|
|
RequestString: query.Query,
|
|
|
|
VariableValues: query.Variables,
|
|
|
|
OperationName: query.OperationName,
|
2019-03-02 15:22:20 +00:00
|
|
|
RootObject: rootObject,
|
2019-01-24 16:26:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(result)
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sugar := s.log.Sugar()
|
|
|
|
sugar.Debug(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts the server that host webapp and api endpoint
|
|
|
|
func (s *Server) Run(ctx context.Context) error {
|
2019-02-05 19:44:00 +00:00
|
|
|
var err error
|
2019-03-05 10:38:21 +00:00
|
|
|
|
2019-03-26 15:56:16 +00:00
|
|
|
s.schema, err = consoleql.CreateSchema(s.log, s.service, s.mailService)
|
2019-01-24 16:26:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-02-06 13:19:14 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
var group errgroup.Group
|
|
|
|
group.Go(func() error {
|
|
|
|
<-ctx.Done()
|
|
|
|
return s.server.Shutdown(nil)
|
|
|
|
})
|
|
|
|
group.Go(func() error {
|
|
|
|
defer cancel()
|
|
|
|
return s.server.Serve(s.listener)
|
|
|
|
})
|
|
|
|
|
|
|
|
return group.Wait()
|
2019-01-24 16:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes server and underlying listener
|
|
|
|
func (s *Server) Close() error {
|
|
|
|
return s.server.Close()
|
|
|
|
}
|