-
Notifications
You must be signed in to change notification settings - Fork 28
/
checkBrokerCredentials.go
67 lines (59 loc) · 2.36 KB
/
checkBrokerCredentials.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
package filters
import (
"errors"
"fmt"
"github.com/Peripli/service-manager/pkg/httpclient"
"net/http"
"github.com/Peripli/service-manager/pkg/util"
"github.com/Peripli/service-manager/pkg/web"
"github.com/tidwall/gjson"
)
const (
CheckBrokerCredentialsFilterName = "CheckBrokerCredentialsFilter"
basicCredentialsPath = "credentials.basic.%s"
tlsCredentialsPath = "credentials.tls.%s"
)
// CheckBrokerCredentialsFilter checks patch request for the broker basic credentials
type CheckBrokerCredentialsFilter struct {
}
func (*CheckBrokerCredentialsFilter) Name() string {
return CheckBrokerCredentialsFilterName
}
func (*CheckBrokerCredentialsFilter) Run(req *web.Request, next web.Handler) (*web.Response, error) {
brokerUrl := gjson.GetBytes(req.Body, "broker_url")
smBrokerCredentials := gjson.GetBytes(req.Body, fmt.Sprintf(tlsCredentialsPath, "sm_provided_tls_credentials"))
basicFields := gjson.GetManyBytes(req.Body, fmt.Sprintf(basicCredentialsPath, "username"), fmt.Sprintf(basicCredentialsPath, "password"))
tlsFields := gjson.GetManyBytes(req.Body, fmt.Sprintf(tlsCredentialsPath, "client_certificate"), fmt.Sprintf(tlsCredentialsPath, "client_key"))
err := credentialsMissing(smBrokerCredentials, basicFields, tlsFields)
if brokerUrl.Exists() && err != nil {
return nil, &util.HTTPError{
ErrorType: "BadRequest",
Description: err.Error(),
StatusCode: http.StatusBadRequest,
}
}
return next.Handle(req)
}
func credentialsMissing(smBrokerCredentials gjson.Result, basicFields []gjson.Result, tlsFields []gjson.Result) error {
httpSettings := httpclient.GetHttpClientGlobalSettings()
if smBrokerCredentials.Exists() && smBrokerCredentials.Bool() && len(httpSettings.ServerCertificate) == 0 {
return errors.New("no sm provided credentials available, provide another type of credentials")
}
smProvided := smBrokerCredentials.Exists() && smBrokerCredentials.Bool()
basic := basicFields[0].Exists() && basicFields[1].Exists()
tls := tlsFields[0].Exists() && tlsFields[1].Exists()
if tls || basic || smProvided {
return nil
}
return errors.New("updating a url of a broker requires its credentials")
}
func (*CheckBrokerCredentialsFilter) FilterMatchers() []web.FilterMatcher {
return []web.FilterMatcher{
{
Matchers: []web.Matcher{
web.Path(web.ServiceBrokersURL + "/**"),
web.Methods(http.MethodPatch),
},
},
}
}