-
Notifications
You must be signed in to change notification settings - Fork 28
/
check_platform_id_plugin.go
107 lines (88 loc) · 4.15 KB
/
check_platform_id_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
package osb
import (
"net/http"
"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/util"
"github.com/Peripli/service-manager/pkg/web"
"github.com/Peripli/service-manager/storage"
)
const CheckPlatformIDPluginName = "CheckPlatformIDPlugin"
type checkPlatformIDPlugin struct {
repository storage.Repository
}
// NewCheckPlatformIDPlugin creates new plugin that checks the platform_id of the instance
func NewCheckPlatformIDPlugin(repository storage.Repository) *checkPlatformIDPlugin {
return &checkPlatformIDPlugin{
repository: repository,
}
}
// Name returns the name of the plugin
func (p *checkPlatformIDPlugin) Name() string {
return CheckPlatformIDPluginName
}
// Deprovision intercepts deprovision requests and check if the instance is in the platform from where the request comes
func (p *checkPlatformIDPlugin) Deprovision(req *web.Request, next web.Handler) (*web.Response, error) {
return p.assertPlatformID(req, next)
}
// UpdateService intercepts update service instance requests and check if the instance is in the platform from where the request comes
func (p *checkPlatformIDPlugin) UpdateService(req *web.Request, next web.Handler) (*web.Response, error) {
return p.assertPlatformID(req, next)
}
// PollInstance intercepts poll instance operation requests and check if the instance is in the platform from where the request comes
func (p *checkPlatformIDPlugin) PollInstance(req *web.Request, next web.Handler) (*web.Response, error) {
return p.assertPlatformID(req, next)
}
// Bind intercepts bind requests and check if the instance is in the platform from where the request comes
func (p *checkPlatformIDPlugin) Bind(req *web.Request, next web.Handler) (*web.Response, error) {
return p.assertPlatformID(req, next)
}
// Unbind intercepts unbind requests and check if the instance is in the platform from where the request comes
func (p *checkPlatformIDPlugin) Unbind(req *web.Request, next web.Handler) (*web.Response, error) {
return p.assertPlatformID(req, next)
}
// PollBinding intercepts poll binding operation requests and check if the instance is in the platform from where the request comes
func (p *checkPlatformIDPlugin) PollBinding(req *web.Request, next web.Handler) (*web.Response, error) {
return p.assertPlatformID(req, next)
}
// FetchService intercepts get service instance requests and check if the instance owner is the same as the one requesting the operation
func (p *checkPlatformIDPlugin) FetchService(req *web.Request, next web.Handler) (*web.Response, error) {
return p.assertPlatformID(req, next)
}
// FetchBinding intercepts get service binding requests and check if the instance owner is the same as the one requesting the operation
func (p *checkPlatformIDPlugin) FetchBinding(req *web.Request, next web.Handler) (*web.Response, error) {
return p.assertPlatformID(req, next)
}
func (p *checkPlatformIDPlugin) assertPlatformID(req *web.Request, next web.Handler) (*web.Response, error) {
ctx := req.Context()
user, _ := web.UserFromContext(ctx)
platform := &types.Platform{}
if err := user.Data(platform); err != nil {
return nil, err
}
if err := platform.Validate(); err != nil {
log.C(ctx).WithError(err).Errorf("Invalid platform found in context")
return nil, err
}
instanceID := req.PathParams["instance_id"]
byID := query.ByField(query.EqualsOperator, "id", instanceID)
object, err := p.repository.Get(ctx, types.ServiceInstanceType, byID)
if err != nil {
if err == util.ErrNotFoundInStorage {
return next.Handle(req)
}
return nil, util.HandleStorageError(err, string(types.ServiceInstanceType))
}
instance := object.(*types.ServiceInstance)
req.Request = req.WithContext(types.ContextWithInstance(req.Context(), instance))
if platform.ID != instance.PlatformID {
log.C(ctx).Errorf("Instance with id %s and platform id %s does not belong to platform with id %s", instance.ID, instance.PlatformID, platform.ID)
return nil, &util.HTTPError{
ErrorType: "NotFound",
Description: "could not find such service instance",
StatusCode: http.StatusNotFound,
}
}
return next.Handle(req)
}