-
Notifications
You must be signed in to change notification settings - Fork 28
/
api.go
195 lines (175 loc) · 8.26 KB
/
api.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright 2018 The Service Manager Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Package api contains logic for building the Service Manager API business logic
package api
import (
"context"
"fmt"
"github.com/Peripli/service-manager/api/configuration"
"github.com/Peripli/service-manager/api/profile"
"github.com/Peripli/service-manager/operations"
"github.com/Peripli/service-manager/pkg/agents"
"github.com/Peripli/service-manager/pkg/env"
"sync"
"github.com/Peripli/service-manager/pkg/query"
"github.com/Peripli/service-manager/pkg/util"
"github.com/Peripli/service-manager/pkg/types"
"github.com/Peripli/service-manager/pkg/ws"
apiNotifications "github.com/Peripli/service-manager/api/notifications"
"github.com/Peripli/service-manager/api/filters"
"github.com/Peripli/service-manager/api/info"
"github.com/Peripli/service-manager/api/osb"
"github.com/Peripli/service-manager/pkg/health"
"github.com/Peripli/service-manager/pkg/web"
"github.com/Peripli/service-manager/storage"
)
const osbVersion = "2.13"
// Settings type to be loaded from the environment
type Settings struct {
TokenIssuerURL string `mapstructure:"token_issuer_url" description:"url of the token issuer which to use for validating tokens"`
ClientID string `mapstructure:"client_id" description:"id of the client from which the token must be issued"`
TokenBasicAuth bool `mapstructure:"token_basic_auth" description:"specifies if client credentials to the authorization server should be sent in the header as basic auth (true) or in the body (false)"`
ProtectedLabels []string `mapstructure:"protected_labels" description:"defines labels which cannot be modified/added by REST API requests"`
OSBVersion string `mapstructure:"-"`
MaxPageSize int `mapstructure:"max_page_size" description:"maximum number of items that could be returned in a single page"`
DefaultPageSize int `mapstructure:"default_page_size" description:"default number of items returned in a single page if not specified in request"`
EnableInstanceTransfer bool `mapstructure:"enable_instance_transfer" description:"whether service instance transfer is enabled or not"`
RateLimit string `mapstructure:"rate_limit" description:"rate limiter configuration defined in format: rate<:path><,rate<:path>,...>"`
RateLimitingEnabled bool `mapstructure:"rate_limiting_enabled" description:"enable rate limiting"`
RateLimitExcludeClients []string `mapstructure:"rate_limit_exclude_clients" description:"define client users that should be excluded from the rate limiter processing"`
RateLimitExcludePaths []string `mapstructure:"rate_limit_exclude_paths" description:"define paths that should be excluded from the rate limiter processing"`
RateLimitUsageLogThreshold int64 `mapstructure:"rate_limiting_usage_log_threshold" description:"defines a threshold for log notification trigger about requests limit usage. Accepts value in range from 0 to 100 (percents)"`
DisabledQueryParameters []string `mapstructure:"disabled_query_parameters" description:"which query parameters are not implemented by service manager and should be extended"`
}
// DefaultSettings returns default values for API settings
func DefaultSettings() *Settings {
return &Settings{
TokenIssuerURL: "",
ClientID: "",
TokenBasicAuth: true, // RFC 6749 section 2.3.1
ProtectedLabels: []string{},
OSBVersion: osbVersion,
MaxPageSize: 200,
DefaultPageSize: 50,
EnableInstanceTransfer: false,
RateLimit: "10000-H,1000-M",
RateLimitingEnabled: false,
RateLimitExcludeClients: []string{},
RateLimitUsageLogThreshold: 10,
DisabledQueryParameters: []string{},
}
}
// Validate validates the API settings
func (s *Settings) Validate() error {
if (len(s.TokenIssuerURL)) == 0 {
return fmt.Errorf("validate Settings: APITokenIssuerURL missing")
}
return validateRateLimiterConfiguration(s.RateLimit)
}
type Options struct {
Repository storage.TransactionalRepository
APISettings *Settings
OperationSettings *operations.Settings
WSSettings *ws.Settings
Notificator storage.Notificator
WaitGroup *sync.WaitGroup
TenantLabelKey string
Agents *agents.Settings
}
// New returns the minimum set of REST APIs needed for the Service Manager
func New(ctx context.Context, e env.Environment, options *Options) (*web.API, error) {
rateLimiters, err := initRateLimiters(options)
if err != nil {
return nil, err
}
api := &web.API{
// Default controllers - more filters can be registered using the relevant API methods
Controllers: []web.Controller{
NewAsyncController(ctx, options, web.ServiceBrokersURL, types.ServiceBrokerType, false, func() types.Object {
return &types.ServiceBroker{}
}, false),
NewController(ctx, options, web.PlatformsURL, types.PlatformType, func() types.Object {
return &types.Platform{}
}, true),
NewController(ctx, options, web.VisibilitiesURL, types.VisibilityType, func() types.Object {
return &types.Visibility{}
}, false),
NewTenantController(options.Repository),
NewServiceInstanceController(ctx, options),
NewServiceBindingController(ctx, options),
apiNotifications.NewController(ctx, options.Repository, options.WSSettings, options.Notificator),
NewServiceOfferingController(ctx, options),
NewServicePlanController(ctx, options),
NewOperationsController(ctx, options),
NewAgentsController(options.Agents),
&credentialsController{
repository: options.Repository,
},
&info.Controller{
TokenIssuer: options.APISettings.TokenIssuerURL,
TokenBasicAuth: options.APISettings.TokenBasicAuth,
},
&osb.Controller{
BrokerFetcher: func(ctx context.Context, brokerID string) (*types.ServiceBroker, error) {
byID := query.ByField(query.EqualsOperator, "id", brokerID)
br, err := options.Repository.Get(ctx, types.ServiceBrokerType, byID)
if err != nil {
return nil, util.HandleStorageError(err, "broker")
}
return br.(*types.ServiceBroker), nil
},
},
&configuration.Controller{
Environment: e,
},
&profile.Controller{},
},
// Default filters - more filters can be registered using the relevant API methods
Filters: []web.Filter{
&filters.Logging{},
&filters.SupportedEncodingsFilter{},
&filters.SelectionCriteria{},
&filters.ServiceInstanceStripFilter{},
&filters.ServiceBindingStripFilter{},
filters.NewProtectedLabelsFilter(options.APISettings.ProtectedLabels),
&filters.ProtectedSMPlatformFilter{},
&filters.PlatformIDInstanceValidationFilter{},
&filters.PlatformAwareVisibilityFilter{},
&filters.PatchOnlyLabelsFilter{},
filters.NewPlansFilterByVisibility(options.Repository),
filters.NewServicesFilterByVisibility(options.Repository),
filters.NewBrokersFilterByVisibility(options.Repository),
&filters.CheckBrokerCredentialsFilter{},
filters.NewServiceInstanceTransferFilter(options.Repository, options.APISettings.EnableInstanceTransfer),
filters.NewPlatformTerminationFilter(options.Repository),
},
Registry: health.NewDefaultRegistry(),
}
api.RegisterFiltersBefore(filters.ProtectedLabelsFilterName, &filters.DisabledQueryParametersFilter{DisabledQueryParameters: options.APISettings.DisabledQueryParameters})
if rateLimiters != nil {
api.RegisterFiltersAfter(
filters.LoggingFilterName,
filters.NewRateLimiterFilter(
rateLimiters,
options.APISettings.RateLimitExcludeClients,
options.APISettings.RateLimitExcludePaths,
options.APISettings.RateLimitUsageLogThreshold,
options.TenantLabelKey,
),
)
}
return api, nil
}