Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into embedded-cluster-work…
Browse files Browse the repository at this point in the history
…ing-group
  • Loading branch information
laverya committed Oct 22, 2023
2 parents 1c3e657 + 4e53a6c commit 166b1fe
Show file tree
Hide file tree
Showing 8 changed files with 0 additions and 122 deletions.
6 changes: 0 additions & 6 deletions pkg/apiserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,6 @@ func Start(params *APIServerParams) {

handlers.RegisterTokenAuthRoutes(handler, debugRouter, loggingRouter)

/**********************************************************************
* License ID auth routes
**********************************************************************/

handlers.RegisterLicenseIDAuthRoutes(r.PathPrefix("").Subrouter(), kotsStore, handler)

/**********************************************************************
* Session auth routes
**********************************************************************/
Expand Down
6 changes: 0 additions & 6 deletions pkg/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,6 @@ func RegisterUnauthenticatedRoutes(handler *Handler, kotsStore store.Store, debu
loggingRouter.Path("/api/v1/embedded-cluster/join").Methods("GET").HandlerFunc(handler.GetK0sNodeJoinCommand)
}

func RegisterLicenseIDAuthRoutes(r *mux.Router, kotsStore store.Store, handler KOTSHandler) {
r.Use(LoggingMiddleware, RequireValidLicenseMiddleware(kotsStore))

r.Name("GetAppMetrics").Path("/api/v1/app/metrics").Methods("GET").HandlerFunc(handler.GetAppMetrics)
}

func StreamJSON(c *websocket.Conn, payload interface{}) {
response, err := json.Marshal(payload)
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions pkg/handlers/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,4 @@ type KOTSHandler interface {
// Helm
IsHelmManaged(w http.ResponseWriter, r *http.Request)
GetAppValuesFile(w http.ResponseWriter, r *http.Request)

// APIs available to applications (except legacy /license/v1/license)
GetAppMetrics(w http.ResponseWriter, r *http.Request)
}
10 changes: 0 additions & 10 deletions pkg/handlers/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,9 @@ import (
"github.com/replicatedhq/kots/pkg/kotsutil"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/replicatedapp"
"github.com/replicatedhq/kots/pkg/reporting"
"github.com/replicatedhq/kots/pkg/session"
"github.com/replicatedhq/kots/pkg/store"
)

func (h *Handler) GetAppMetrics(w http.ResponseWriter, r *http.Request) {
app := session.ContextGetApp(r)
reportingInfo := reporting.GetReportingInfo(app.ID)
headers := reporting.GetReportingInfoHeaders(reportingInfo)

JSON(w, http.StatusOK, headers)
}

type SendCustomAppMetricsRequest struct {
Data CustomAppMetricsData `json:"data"`
}
Expand Down
18 changes: 0 additions & 18 deletions pkg/handlers/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,3 @@ func RequireValidSessionQuietMiddleware(kotsStore store.Store) mux.MiddlewareFun
})
}
}

func RequireValidLicenseMiddleware(kotsStore store.Store) mux.MiddlewareFunc {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
license, app, err := requireValidLicense(kotsStore, w, r)
if err != nil {
if !kotsStore.IsNotFound(err) {
logger.Error(errors.Wrapf(err, "request %q", r.RequestURI))
}
return
}

r = session.ContextSetLicense(r, license)
r = session.ContextSetApp(r, app)
next.ServeHTTP(w, r)
})
}
}
12 changes: 0 additions & 12 deletions pkg/handlers/mock/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 0 additions & 47 deletions pkg/handlers/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ import (
"time"

"github.com/pkg/errors"
apptypes "github.com/replicatedhq/kots/pkg/app/types"
"github.com/replicatedhq/kots/pkg/handlers/types"
"github.com/replicatedhq/kots/pkg/k8sutil"
"github.com/replicatedhq/kots/pkg/kotsutil"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/session"
sessiontypes "github.com/replicatedhq/kots/pkg/session/types"
"github.com/replicatedhq/kots/pkg/store"
"github.com/replicatedhq/kots/pkg/util"
kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -158,47 +155,3 @@ func requireValidKOTSToken(w http.ResponseWriter, r *http.Request) error {

return errors.New("invalid auth")
}

func requireValidLicense(kotsStore store.Store, w http.ResponseWriter, r *http.Request) (*kotsv1beta1.License, *apptypes.App, error) {
if r.Method == "OPTIONS" {
return nil, nil, nil
}

licenseID := r.Header.Get("authorization")
if licenseID == "" {
err := errors.New("missing authorization header")
response := types.ErrorResponse{Error: util.StrPointer(err.Error())}
JSON(w, http.StatusUnauthorized, response)
return nil, nil, err
}

apps, err := kotsStore.ListInstalledApps()
if err != nil {
return nil, nil, errors.Wrap(err, "get all apps")
}

var license *kotsv1beta1.License
var app *apptypes.App

for _, a := range apps {
l, err := kotsutil.LoadLicenseFromBytes([]byte(a.License))
if err != nil {
return nil, nil, errors.Wrap(err, "load license")
}

if l.Spec.LicenseID == licenseID {
license = l
app = a
break
}
}

if license == nil {
err := errors.New("license ID is not valid")
response := types.ErrorResponse{Error: util.StrPointer(err.Error())}
JSON(w, http.StatusUnauthorized, response)
return nil, nil, err
}

return license, app, nil
}
20 changes: 0 additions & 20 deletions pkg/session/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"context"
"net/http"

apptypes "github.com/replicatedhq/kots/pkg/app/types"
"github.com/replicatedhq/kots/pkg/session/types"
"github.com/replicatedhq/kotskinds/apis/kots/v1beta1"
)

type sessionKey struct{}
Expand All @@ -20,21 +18,3 @@ func ContextGetSession(r *http.Request) *types.Session {
sess, _ := val.(*types.Session)
return sess
}

func ContextSetLicense(r *http.Request, license *v1beta1.License) *http.Request {
return r.WithContext(context.WithValue(r.Context(), "kotsLicense", license))
}

func ContextGetLicense(r *http.Request) *v1beta1.License {
val := r.Context().Value("kotsLicense")
return val.(*v1beta1.License)
}

func ContextSetApp(r *http.Request, app *apptypes.App) *http.Request {
return r.WithContext(context.WithValue(r.Context(), "kotsApp", app))
}

func ContextGetApp(r *http.Request) *apptypes.App {
val := r.Context().Value("kotsApp")
return val.(*apptypes.App)
}

0 comments on commit 166b1fe

Please sign in to comment.