2021-03-31 19:34:44 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package consoleapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-10-11 12:39:08 +01:00
|
|
|
"io"
|
2021-03-31 19:34:44 +01:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2022-11-21 18:58:42 +00:00
|
|
|
"storj.io/storj/private/web"
|
2021-03-31 19:34:44 +01:00
|
|
|
"storj.io/storj/satellite/analytics"
|
|
|
|
"storj.io/storj/satellite/console"
|
|
|
|
)
|
|
|
|
|
satellite/.../consoleapi: Respond with 401 on unauth req (#4781)
Respond with the appropriate HTTP status code when a request to the
analytics trigger event handler receive an authorized request.
A part of fixing the response status code this will stop to log these
response with ERROR level in our satellite logs.
Example of error message found in our satellite logs:
{
"insertId": "0ljf1cfn4xroxfd6",
"jsonPayload": {
"N": "console:endpoint",
"T": "2022-05-06T13:31:35.415Z",
"errorVerbose": "unauthorized: http: named cookie not present\n\tstorj.io/storj/satellite/console.GetAuth:72\n\tstorj.io/storj/satellite/console/consoleweb/consoleapi.(*Analytics).EventTriggered:60\n\tnet/http.HandlerFunc.ServeHTTP:2047\n\tstorj.io/storj/satellite/console/consoleweb.(*Server).withAuth.func1:488\n\tnet/http.HandlerFunc.ServeHTTP:2047\n\tgithub.com/gorilla/mux.(*Router).ServeHTTP:210\n\tstorj.io/storj/satellite/console/consoleweb.(*Server).withRequest.func1:495\n\tnet/http.HandlerFunc.ServeHTTP:2047\n\tnet/http.serverHandler.ServeHTTP:2879\n\tnet/http.(*conn).serve:1930",
"L": "ERROR",
"error": "unauthorized: http: named cookie not present",
"message": "unauthorized: http: named cookie not present",
"code": 500,
"S": "storj.io/storj/satellite/console/consoleweb/consoleapi.serveCustomJSONError\n\t/go/src/storj.io/storj/satellite/console/consoleweb/consoleapi/common.go:37\nstorj.io/storj/satellite/console/consoleweb/consoleapi.serveJSONError\n\t/go/src/storj.io/storj/satellite/console/consoleweb/consoleapi/common.go:23\nstorj.io/storj/satellite/console/consoleweb/consoleapi.(*Analytics).serveJSONError\n\t/go/src/storj.io/storj/satellite/console/consoleweb/consoleapi/analytics.go:75\nstorj.io/storj/satellite/console/consoleweb/consoleapi.(*Analytics).EventTriggered\n\t/go/src/storj.io/storj/satellite/console/consoleweb/consoleapi/analytics.go:62\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2047\nstorj.io/storj/satellite/console/consoleweb.(*Server).withAuth.func1\n\t/go/src/storj.io/storj/satellite/console/consoleweb/server.go:488\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2047\ngithub.com/gorilla/mux.(*Router).ServeHTTP\n\t/go/pkg/mod/github.com/gorilla/mux@v1.8.0/mux.go:210\nstorj.io/storj/satellite/console/consoleweb.(*Server).withRequest.func1\n\t/go/src/storj.io/storj/satellite/console/consoleweb/server.go:495\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2047\nnet/http.serverHandler.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2879\nnet/http.(*conn).serve\n\t/usr/local/go/src/net/http/server.go:1930",
"M": "returning error to client"
},
"resource": {
"type": "k8s_container",
"labels": {
"location": "us-central1",
"pod_name": "us-central1-satellite-api-77c47f5c5-dzrpj",
"project_id": "storj-prod",
"namespace_name": "satellite",
"container_name": "satellite",
"cluster_name": "us-central1-gke-manatee"
}
},
"timestamp": "2022-05-06T13:31:35.416050390Z",
"severity": "ERROR",
"labels": {
"k8s-pod/version": "v3",
"k8s-pod/app": "us-central1-satellite-api",
"compute.googleapis.com/resource_name": "gke-us-central1-gke--terraform-202110-97ff1891-t0fv",
"k8s-pod/service": "api",
"k8s-pod/pod-template-hash": "77c47f5c5"
},
"logName": "projects/storj-prod/logs/stderr",
"receiveTimestamp": "2022-05-06T13:31:37.419991630Z"
}
Change-Id: I7cfcfb500b7878c59b1d259683c92e8963e2dc3f
Co-authored-by: Stefan Benten <mail@stefan-benten.de>
2022-05-08 11:35:42 +01:00
|
|
|
// ErrAnalyticsAPI - console analytics api error type.
|
|
|
|
var ErrAnalyticsAPI = errs.Class("consoleapi analytics")
|
2021-03-31 19:34:44 +01:00
|
|
|
|
|
|
|
// Analytics is an api controller that exposes analytics related functionality.
|
|
|
|
type Analytics struct {
|
|
|
|
log *zap.Logger
|
|
|
|
service *console.Service
|
|
|
|
analytics *analytics.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAnalytics is a constructor for api analytics controller.
|
|
|
|
func NewAnalytics(log *zap.Logger, service *console.Service, a *analytics.Service) *Analytics {
|
|
|
|
return &Analytics{
|
|
|
|
log: log,
|
|
|
|
service: service,
|
|
|
|
analytics: a,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type eventTriggeredBody struct {
|
2023-06-22 12:18:21 +01:00
|
|
|
EventName string `json:"eventName"`
|
|
|
|
Link string `json:"link"`
|
|
|
|
ErrorEventSource string `json:"errorEventSource"`
|
|
|
|
Props map[string]string `json:"props"`
|
2021-03-31 19:34:44 +01:00
|
|
|
}
|
|
|
|
|
2022-06-09 19:54:23 +01:00
|
|
|
type pageVisitBody struct {
|
|
|
|
PageName string `json:"pageName"`
|
|
|
|
}
|
|
|
|
|
2021-03-31 19:34:44 +01:00
|
|
|
// EventTriggered tracks the occurrence of an arbitrary event on the client.
|
|
|
|
func (a *Analytics) EventTriggered(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2022-10-11 12:39:08 +01:00
|
|
|
body, err := io.ReadAll(r.Body)
|
2021-03-31 19:34:44 +01:00
|
|
|
if err != nil {
|
|
|
|
a.serveJSONError(w, http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
var et eventTriggeredBody
|
|
|
|
err = json.Unmarshal(body, &et)
|
|
|
|
if err != nil {
|
|
|
|
a.serveJSONError(w, http.StatusInternalServerError, err)
|
|
|
|
}
|
2021-12-09 20:52:51 +00:00
|
|
|
|
2022-06-05 23:41:38 +01:00
|
|
|
user, err := console.GetUser(ctx)
|
2021-03-31 19:34:44 +01:00
|
|
|
if err != nil {
|
satellite/.../consoleapi: Respond with 401 on unauth req (#4781)
Respond with the appropriate HTTP status code when a request to the
analytics trigger event handler receive an authorized request.
A part of fixing the response status code this will stop to log these
response with ERROR level in our satellite logs.
Example of error message found in our satellite logs:
{
"insertId": "0ljf1cfn4xroxfd6",
"jsonPayload": {
"N": "console:endpoint",
"T": "2022-05-06T13:31:35.415Z",
"errorVerbose": "unauthorized: http: named cookie not present\n\tstorj.io/storj/satellite/console.GetAuth:72\n\tstorj.io/storj/satellite/console/consoleweb/consoleapi.(*Analytics).EventTriggered:60\n\tnet/http.HandlerFunc.ServeHTTP:2047\n\tstorj.io/storj/satellite/console/consoleweb.(*Server).withAuth.func1:488\n\tnet/http.HandlerFunc.ServeHTTP:2047\n\tgithub.com/gorilla/mux.(*Router).ServeHTTP:210\n\tstorj.io/storj/satellite/console/consoleweb.(*Server).withRequest.func1:495\n\tnet/http.HandlerFunc.ServeHTTP:2047\n\tnet/http.serverHandler.ServeHTTP:2879\n\tnet/http.(*conn).serve:1930",
"L": "ERROR",
"error": "unauthorized: http: named cookie not present",
"message": "unauthorized: http: named cookie not present",
"code": 500,
"S": "storj.io/storj/satellite/console/consoleweb/consoleapi.serveCustomJSONError\n\t/go/src/storj.io/storj/satellite/console/consoleweb/consoleapi/common.go:37\nstorj.io/storj/satellite/console/consoleweb/consoleapi.serveJSONError\n\t/go/src/storj.io/storj/satellite/console/consoleweb/consoleapi/common.go:23\nstorj.io/storj/satellite/console/consoleweb/consoleapi.(*Analytics).serveJSONError\n\t/go/src/storj.io/storj/satellite/console/consoleweb/consoleapi/analytics.go:75\nstorj.io/storj/satellite/console/consoleweb/consoleapi.(*Analytics).EventTriggered\n\t/go/src/storj.io/storj/satellite/console/consoleweb/consoleapi/analytics.go:62\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2047\nstorj.io/storj/satellite/console/consoleweb.(*Server).withAuth.func1\n\t/go/src/storj.io/storj/satellite/console/consoleweb/server.go:488\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2047\ngithub.com/gorilla/mux.(*Router).ServeHTTP\n\t/go/pkg/mod/github.com/gorilla/mux@v1.8.0/mux.go:210\nstorj.io/storj/satellite/console/consoleweb.(*Server).withRequest.func1\n\t/go/src/storj.io/storj/satellite/console/consoleweb/server.go:495\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2047\nnet/http.serverHandler.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2879\nnet/http.(*conn).serve\n\t/usr/local/go/src/net/http/server.go:1930",
"M": "returning error to client"
},
"resource": {
"type": "k8s_container",
"labels": {
"location": "us-central1",
"pod_name": "us-central1-satellite-api-77c47f5c5-dzrpj",
"project_id": "storj-prod",
"namespace_name": "satellite",
"container_name": "satellite",
"cluster_name": "us-central1-gke-manatee"
}
},
"timestamp": "2022-05-06T13:31:35.416050390Z",
"severity": "ERROR",
"labels": {
"k8s-pod/version": "v3",
"k8s-pod/app": "us-central1-satellite-api",
"compute.googleapis.com/resource_name": "gke-us-central1-gke--terraform-202110-97ff1891-t0fv",
"k8s-pod/service": "api",
"k8s-pod/pod-template-hash": "77c47f5c5"
},
"logName": "projects/storj-prod/logs/stderr",
"receiveTimestamp": "2022-05-06T13:31:37.419991630Z"
}
Change-Id: I7cfcfb500b7878c59b1d259683c92e8963e2dc3f
Co-authored-by: Stefan Benten <mail@stefan-benten.de>
2022-05-08 11:35:42 +01:00
|
|
|
a.serveJSONError(w, http.StatusUnauthorized, err)
|
2021-03-31 19:34:44 +01:00
|
|
|
return
|
|
|
|
}
|
2022-12-08 12:10:25 +00:00
|
|
|
|
|
|
|
if et.ErrorEventSource != "" {
|
|
|
|
a.analytics.TrackErrorEvent(user.ID, user.Email, et.ErrorEventSource)
|
|
|
|
} else if et.Link != "" {
|
2022-06-05 23:41:38 +01:00
|
|
|
a.analytics.TrackLinkEvent(et.EventName, user.ID, user.Email, et.Link)
|
2021-04-12 17:58:36 +01:00
|
|
|
} else {
|
2023-06-22 12:18:21 +01:00
|
|
|
a.analytics.TrackEvent(et.EventName, user.ID, user.Email, et.Props)
|
2021-04-12 17:58:36 +01:00
|
|
|
}
|
2021-03-31 19:34:44 +01:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2022-06-09 19:54:23 +01:00
|
|
|
// PageEventTriggered tracks the occurrence of an arbitrary page visit event on the client.
|
|
|
|
func (a *Analytics) PageEventTriggered(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2022-10-11 12:39:08 +01:00
|
|
|
body, err := io.ReadAll(r.Body)
|
2022-06-09 19:54:23 +01:00
|
|
|
if err != nil {
|
|
|
|
a.serveJSONError(w, http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
var pv pageVisitBody
|
|
|
|
err = json.Unmarshal(body, &pv)
|
|
|
|
if err != nil {
|
|
|
|
a.serveJSONError(w, http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
|
2022-06-05 23:41:38 +01:00
|
|
|
user, err := console.GetUser(ctx)
|
2022-06-09 19:54:23 +01:00
|
|
|
if err != nil {
|
|
|
|
a.serveJSONError(w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-05 23:41:38 +01:00
|
|
|
a.analytics.PageVisitEvent(pv.PageName, user.ID, user.Email)
|
2022-06-09 19:54:23 +01:00
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2021-03-31 19:34:44 +01:00
|
|
|
// serveJSONError writes JSON error to response output stream.
|
|
|
|
func (a *Analytics) serveJSONError(w http.ResponseWriter, status int, err error) {
|
2022-11-21 18:58:42 +00:00
|
|
|
web.ServeJSONError(a.log, w, status, err)
|
2021-03-31 19:34:44 +01:00
|
|
|
}
|