-
Notifications
You must be signed in to change notification settings - Fork 28
/
visibility_filtering_middleware.go
74 lines (62 loc) · 2.03 KB
/
visibility_filtering_middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package filters
import (
"context"
"errors"
"fmt"
"net/http"
"github.com/Peripli/service-manager/pkg/util"
"github.com/Peripli/service-manager/pkg/log"
"github.com/Peripli/service-manager/pkg/query"
"github.com/Peripli/service-manager/pkg/types"
"github.com/Peripli/service-manager/pkg/web"
)
type visibilityFilteringMiddleware struct {
IsResourceVisible func(ctx context.Context, resourceID, platformID string) (bool, error)
ListResourcesCriteria func(ctx context.Context, platformID string) (*query.Criterion, error)
}
func (m visibilityFilteringMiddleware) Run(req *web.Request, next web.Handler) (*web.Response, error) {
ctx := req.Context()
userCtx, ok := web.UserFromContext(ctx)
if !ok {
return nil, errors.New("no user found")
}
if userCtx.AuthenticationType != web.Basic {
log.C(ctx).Debugf("Authentication is %s, not basic so proceed without visibility filter criteria", userCtx.AuthenticationType)
return next.Handle(req)
}
platform := &types.Platform{}
if err := userCtx.Data(platform); err != nil {
return nil, err
}
if platform.Type != types.K8sPlatformType {
log.C(ctx).Debugf("Platform type is %s, which is not kubernetes. Skip filtering on visibilities", platform.Type)
return next.Handle(req)
}
resourceID := req.PathParams["resource_id"]
isSingleResource := resourceID != ""
if isSingleResource {
if isResourceVisible, err := m.IsResourceVisible(ctx, resourceID, platform.ID); err != nil {
return nil, err
} else if !isResourceVisible {
return nil, &util.HTTPError{
ErrorType: "NotFound",
Description: fmt.Sprintf("could not find resource"),
StatusCode: http.StatusNotFound,
}
}
return next.Handle(req)
}
finalQuery, err := m.ListResourcesCriteria(ctx, platform.ID)
if err != nil {
return nil, err
}
if finalQuery == nil {
return util.NewJSONResponse(http.StatusOK, types.ObjectPage{Items: make([]types.Object, 0)})
}
ctx, err = query.AddCriteria(ctx, *finalQuery)
if err != nil {
return nil, err
}
req.Request = req.WithContext(ctx)
return next.Handle(req)
}