Differentiate external service auth from user auth

Signed-off-by: Alex Ellis <alexellis2@gmail.com>
This commit is contained in:
Alex Ellis
2019-06-08 09:56:21 +01:00
parent 0758e484f7
commit 1cf030da48
5 changed files with 76 additions and 39 deletions

View File

@ -15,13 +15,13 @@ import (
"strconv"
"time"
"github.com/openfaas/faas-provider/auth"
"github.com/openfaas/faas/gateway/handlers"
"github.com/openfaas/faas/gateway/requests"
"github.com/openfaas/faas/gateway/scaling"
)
// NewExternalServiceQuery proxies service queries to external plugin via HTTP
func NewExternalServiceQuery(externalURL url.URL, credentials *auth.BasicAuthCredentials) scaling.ServiceQuery {
func NewExternalServiceQuery(externalURL url.URL, authInjector handlers.AuthInjector) scaling.ServiceQuery {
timeout := 3 * time.Second
proxyClient := http.Client{
@ -39,17 +39,17 @@ func NewExternalServiceQuery(externalURL url.URL, credentials *auth.BasicAuthCre
}
return ExternalServiceQuery{
URL: externalURL,
ProxyClient: proxyClient,
Credentials: credentials,
URL: externalURL,
ProxyClient: proxyClient,
AuthInjector: authInjector,
}
}
// ExternalServiceQuery proxies service queries to external plugin via HTTP
type ExternalServiceQuery struct {
URL url.URL
ProxyClient http.Client
Credentials *auth.BasicAuthCredentials
URL url.URL
ProxyClient http.Client
AuthInjector handlers.AuthInjector
}
// ScaleServiceRequest request scaling of replica
@ -71,8 +71,8 @@ func (s ExternalServiceQuery) GetReplicas(serviceName string) (scaling.ServiceQu
req, _ := http.NewRequest(http.MethodGet, urlPath, nil)
if s.Credentials != nil {
req.SetBasicAuth(s.Credentials.User, s.Credentials.Password)
if s.AuthInjector != nil {
s.AuthInjector.Inject(req)
}
res, err := s.ProxyClient.Do(req)
@ -144,8 +144,8 @@ func (s ExternalServiceQuery) SetReplicas(serviceName string, count uint64) erro
urlPath := fmt.Sprintf("%ssystem/scale-function/%s", s.URL.String(), serviceName)
req, _ := http.NewRequest(http.MethodPost, urlPath, bytes.NewReader(requestBody))
if s.Credentials != nil {
req.SetBasicAuth(s.Credentials.User, s.Credentials.Password)
if s.AuthInjector != nil {
s.AuthInjector.Inject(req)
}
defer req.Body.Close()