add namedError in addition to baseError (#1195)
This commit is contained in:
@@ -26,8 +26,9 @@ type LicenseStore interface {
|
||||
}
|
||||
|
||||
type LicenseService interface {
|
||||
// LicenseClaims returns details of a customer license that determine authorization
|
||||
// to use the Kolide product.
|
||||
// License returns details of a customer license that determine authorization
|
||||
// to use the Kolide product. If the customer has not uploaded a token,
|
||||
// the license Token will be nil.
|
||||
License(ctx context.Context) (*License, error)
|
||||
|
||||
// SaveLicense writes jwt token string to database after performing
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
@@ -81,27 +80,20 @@ func makeGetLicenseEndpoint(svc kolide.Service) endpoint.Endpoint {
|
||||
}
|
||||
}
|
||||
|
||||
// makePostLicenseEndpoint is only to be used once, if a license is successfully
|
||||
// makeSetupLicenseEndpoint is only to be used once, if a license is successfully
|
||||
// installed we return an error if it is called again. Note that this endpoint
|
||||
// requires no authentication. Once a license is installed the user will be
|
||||
// redirected to login or setup, depending on whether setup is complete or not
|
||||
func makePostLicenseEndpoint(svc kolide.Service) endpoint.Endpoint {
|
||||
// redirected to login or setup, depending on whether setup is complete or not.
|
||||
func makeSetupLicenseEndpoint(svc kolide.Service) endpoint.Endpoint {
|
||||
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
||||
req := request.(licenseRequest)
|
||||
lic, err := svc.License(ctx)
|
||||
if err != nil {
|
||||
return licenseResponse{Err: err}, nil
|
||||
}
|
||||
if lic.Token != nil {
|
||||
return licenseResponse{Err: errors.New("license can only be uploaded once")}, nil
|
||||
}
|
||||
saved, err := svc.SaveLicense(ctx, req.License)
|
||||
if err != nil {
|
||||
return licenseResponse{Err: err}, nil
|
||||
}
|
||||
claims, err := saved.Claims()
|
||||
if err != nil {
|
||||
return licenseResponse{Err: err}, nil
|
||||
return licenseResponse{Err: licensingError{err.Error()}}, nil
|
||||
}
|
||||
response := licenseResponse{
|
||||
License: license{
|
||||
|
||||
@@ -422,7 +422,7 @@ func WithSetup(svc kolide.Service, logger kitlog.Logger, next http.Handler) http
|
||||
))
|
||||
configRouter.Handle("/api/v1/license", kithttp.NewServer(
|
||||
context.Background(),
|
||||
makePostLicenseEndpoint(svc),
|
||||
makeSetupLicenseEndpoint(svc),
|
||||
decodeLicenseRequest,
|
||||
encodeResponse,
|
||||
))
|
||||
|
||||
@@ -28,6 +28,14 @@ func baseError(err string) []map[string]string {
|
||||
}
|
||||
}
|
||||
|
||||
// same as baseError, but replaces "base" with different name.
|
||||
func namedError(name string, err string) []map[string]string {
|
||||
return []map[string]string{map[string]string{
|
||||
"name": name,
|
||||
"reason": err},
|
||||
}
|
||||
}
|
||||
|
||||
// encode error and status header to the client
|
||||
func encodeError(ctx context.Context, err error, w http.ResponseWriter) {
|
||||
// Unwrap Go-Kit Error
|
||||
@@ -46,7 +54,7 @@ func encodeError(ctx context.Context, err error, w http.ResponseWriter) {
|
||||
if e, ok := err.(licensingError); ok {
|
||||
le := jsonError{
|
||||
Message: "Licensing Error",
|
||||
Errors: baseError(e.LicensingError()),
|
||||
Errors: namedError(e.LicensingError(), "license"),
|
||||
}
|
||||
w.WriteHeader(http.StatusPaymentRequired)
|
||||
enc.Encode(le)
|
||||
|
||||
Reference in New Issue
Block a user