-
Notifications
You must be signed in to change notification settings - Fork 28
/
check_instance_owner_plugin.go
88 lines (74 loc) · 2.93 KB
/
check_instance_owner_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
package osb
import (
"fmt"
"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"
"github.com/tidwall/gjson"
)
const CheckInstanceOwnerhipPluginName = "CheckInstanceOwnershipPlugin"
type checkInstanceOwnershipPlugin struct {
repository storage.Repository
tenantIdentifier string
}
// NewCheckInstanceOwnershipPlugin creates new plugin that checks the owner of the instance
func NewCheckInstanceOwnershipPlugin(repository storage.Repository, tenantIdentifier string) *checkInstanceOwnershipPlugin {
return &checkInstanceOwnershipPlugin{
repository: repository,
tenantIdentifier: tenantIdentifier,
}
}
// Name returns the name of the plugin
func (p *checkInstanceOwnershipPlugin) Name() string {
return CheckInstanceOwnerhipPluginName
}
// Bind intercepts bind requests and check if the instance owner is the same as the one requesting the bind operation
func (p *checkInstanceOwnershipPlugin) Bind(req *web.Request, next web.Handler) (*web.Response, error) {
return p.assertOwner(req, next)
}
// UpdateService intercepts update service instance requests and check if the instance owner is the same as the one requesting the operation
func (p *checkInstanceOwnershipPlugin) UpdateService(req *web.Request, next web.Handler) (*web.Response, error) {
return p.assertOwner(req, next)
}
func (p *checkInstanceOwnershipPlugin) assertOwner(req *web.Request, next web.Handler) (*web.Response, error) {
ctx := req.Context()
path := fmt.Sprintf("context.%s", p.tenantIdentifier)
callerTenantID := gjson.GetBytes(req.Body, path).String()
if len(callerTenantID) == 0 {
log.C(ctx).Info("Tenant identifier not found in request context.")
return next.Handle(req)
}
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)
var instanceOwnerTenantID string
if instance.Labels != nil {
if tenantIDLabel, ok := instance.Labels[p.tenantIdentifier]; ok {
instanceOwnerTenantID = tenantIDLabel[0]
}
}
if instanceOwnerTenantID == "" {
log.C(ctx).Infof("Tenant label for instance with id %s is missing.", instanceID)
return next.Handle(req)
}
if instanceOwnerTenantID != callerTenantID {
log.C(ctx).Errorf("Instance owner %s is not the same as the caller %s", instanceOwnerTenantID, callerTenantID)
return nil, &util.HTTPError{
ErrorType: "NotFound",
Description: "could not find such service instance",
StatusCode: http.StatusNotFound,
}
}
return next.Handle(req)
}