-
Notifications
You must be signed in to change notification settings - Fork 28
/
catalog_filter_by_visibility_plugin.go
164 lines (137 loc) · 4.86 KB
/
catalog_filter_by_visibility_plugin.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package osb
import (
"context"
"fmt"
"net/http"
"github.com/Peripli/service-manager/pkg/query"
"github.com/Peripli/service-manager/pkg/security"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
"github.com/Peripli/service-manager/pkg/log"
"github.com/Peripli/service-manager/pkg/types"
"github.com/Peripli/service-manager/pkg/web"
"github.com/Peripli/service-manager/storage"
)
const CatalogFilterByVisibilityPluginName = "CatalogFilterByVisibilityPlugin"
func NewCatalogFilterByVisibilityPlugin(repository storage.Repository) *CatalogFilterByVisibilityPlugin {
return &CatalogFilterByVisibilityPlugin{
repository: repository,
}
}
func (c *CatalogFilterByVisibilityPlugin) Name() string {
return CatalogFilterByVisibilityPluginName
}
type CatalogFilterByVisibilityPlugin struct {
repository storage.Repository
}
func (c *CatalogFilterByVisibilityPlugin) FetchCatalog(req *web.Request, next web.Handler) (*web.Response, error) {
ctx := req.Context()
userCtx, ok := web.UserFromContext(ctx)
if !ok {
return nil, security.UnauthorizedHTTPError("no user found")
}
res, err := next.Handle(req)
if err != nil || res.StatusCode != http.StatusOK {
return res, err
}
if userCtx.AuthenticationType != web.Basic {
log.C(ctx).Debugf("Authentication is %s, not basic. Skip filtering on visibilities", userCtx.AuthenticationType)
return res, nil
}
platform := &types.Platform{}
if err := userCtx.Data(platform); err != nil {
return nil, err
}
brokerID := req.PathParams[BrokerIDPathParam]
visibleCatalogPlans, err := getVisiblePlansByBrokerIDAndPlatformID(ctx, c.repository, brokerID, platform)
if err != nil {
return nil, err
}
res.Body, err = filterCatalogByVisiblePlans(res.Body, visibleCatalogPlans)
return res, err
}
func getVisiblePlansByBrokerIDAndPlatformID(ctx context.Context, repository storage.Repository, brokerID string, platform *types.Platform) (map[string]bool, error) {
offeringIDs, err := getOfferingIDsByBrokerID(ctx, repository, brokerID)
if err != nil {
return nil, err
}
if len(offeringIDs) == 0 {
return map[string]bool{}, nil
}
plansList, err := repository.List(ctx, types.ServicePlanType, query.ByField(query.InOperator, "service_offering_id", offeringIDs...))
if err != nil {
log.C(ctx).Errorf("Could not get %s: %v", types.ServicePlanType, err)
return nil, err
}
visibleCatalogPlans := make(map[string]bool)
if platform.Type == types.CFPlatformType {
for i := 0; i < plansList.Len(); i++ {
plan := plansList.ItemAt(i).(*types.ServicePlan)
if plan.SupportsPlatformInstance(*platform) {
visibleCatalogPlans[plan.CatalogID] = true
}
}
return visibleCatalogPlans, nil
}
planIDs := make([]string, 0, plansList.Len())
for i := 0; i < plansList.Len(); i++ {
planIDs = append(planIDs, plansList.ItemAt(i).GetID())
}
visibilitiesList, err := repository.ListNoLabels(ctx, types.VisibilityType,
query.ByField(query.EqualsOrNilOperator, "platform_id", platform.ID),
query.ByField(query.InOperator, "service_plan_id", planIDs...))
if err != nil {
log.C(ctx).Errorf("Could not get %s: %v", types.VisibilityType, err)
return nil, err
}
visibilities := (visibilitiesList.(*types.Visibilities)).Visibilities
visiblePlans := make(map[string]bool)
for _, v := range visibilities {
visiblePlans[v.ServicePlanID] = true
}
plans := (plansList.(*types.ServicePlans)).ServicePlans
for _, p := range plans {
if visiblePlans[p.ID] {
visibleCatalogPlans[p.CatalogID] = true
}
}
return visibleCatalogPlans, nil
}
func getOfferingIDsByBrokerID(ctx context.Context, repository storage.Repository, brokerID string) ([]string, error) {
offerings, err := repository.List(ctx, types.ServiceOfferingType, query.ByField(query.EqualsOperator, "broker_id", brokerID))
if err != nil {
log.C(ctx).Errorf("Could not get %s: %v", types.ServiceOfferingType, err)
return nil, err
}
offeringIDs := make([]string, 0, offerings.Len())
for i := 0; i < offerings.Len(); i++ {
offeringIDs = append(offeringIDs, offerings.ItemAt(i).GetID())
}
return offeringIDs, nil
}
func filterCatalogByVisiblePlans(catalog []byte, visibleCatalogPlans map[string]bool) ([]byte, error) {
var err error
services := gjson.GetBytes(catalog, "services").Array()
// loop services in reverse order to ease the removal of elements
for i := len(services) - 1; i >= 0; i-- {
servicePath := fmt.Sprintf(`services.%d`, i)
plans := services[i].Get("plans").Array()
for j := len(plans) - 1; j >= 0; j-- {
catalogPlanID := plans[j].Get("id").String()
if !visibleCatalogPlans[catalogPlanID] {
planPath := fmt.Sprintf(servicePath+`.plans.%d`, j)
catalog, err = sjson.DeleteBytes(catalog, planPath)
if err != nil {
return nil, err
}
}
}
if len(gjson.GetBytes(catalog, servicePath+".plans").Array()) == 0 {
catalog, err = sjson.DeleteBytes(catalog, servicePath)
if err != nil {
return nil, err
}
}
}
return catalog, nil
}