mirror of
https://github.com/openfaas/faas.git
synced 2025-06-29 10:13:26 +00:00
Differentiate external service auth from user auth
Signed-off-by: Alex Ellis <alexellis2@gmail.com>
This commit is contained in:
@ -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()
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/openfaas/faas-provider/auth"
|
||||
"github.com/openfaas/faas/gateway/handlers"
|
||||
"github.com/openfaas/faas/gateway/scaling"
|
||||
)
|
||||
|
||||
@ -47,11 +47,10 @@ func TestGetReplicasNonExistentFn(t *testing.T) {
|
||||
}))
|
||||
defer testServer.Close()
|
||||
|
||||
var creds auth.BasicAuthCredentials
|
||||
|
||||
var injector handlers.AuthInjector
|
||||
url, _ := url.Parse(testServer.URL + "/")
|
||||
|
||||
esq := NewExternalServiceQuery(*url, &creds)
|
||||
esq := NewExternalServiceQuery(*url, injector)
|
||||
|
||||
svcQryResp, err := esq.GetReplicas("burt")
|
||||
|
||||
@ -78,11 +77,10 @@ func TestGetReplicasExistentFn(t *testing.T) {
|
||||
AvailableReplicas: 0,
|
||||
}
|
||||
|
||||
var creds auth.BasicAuthCredentials
|
||||
|
||||
var injector handlers.AuthInjector
|
||||
url, _ := url.Parse(testServer.URL + "/")
|
||||
|
||||
esq := NewExternalServiceQuery(*url, &creds)
|
||||
esq := NewExternalServiceQuery(*url, injector)
|
||||
|
||||
svcQryResp, err := esq.GetReplicas("burt")
|
||||
|
||||
@ -104,9 +102,9 @@ func TestSetReplicasNonExistentFn(t *testing.T) {
|
||||
}))
|
||||
defer testServer.Close()
|
||||
|
||||
var creds auth.BasicAuthCredentials
|
||||
var injector handlers.AuthInjector
|
||||
url, _ := url.Parse(testServer.URL + "/")
|
||||
esq := NewExternalServiceQuery(*url, &creds)
|
||||
esq := NewExternalServiceQuery(*url, injector)
|
||||
|
||||
err := esq.SetReplicas("burt", 1)
|
||||
|
||||
@ -126,9 +124,10 @@ func TestSetReplicasExistentFn(t *testing.T) {
|
||||
}))
|
||||
defer testServer.Close()
|
||||
|
||||
var creds auth.BasicAuthCredentials
|
||||
var injector handlers.AuthInjector
|
||||
|
||||
url, _ := url.Parse(testServer.URL + "/")
|
||||
esq := NewExternalServiceQuery(*url, &creds)
|
||||
esq := NewExternalServiceQuery(*url, injector)
|
||||
|
||||
err := esq.SetReplicas("burt", 1)
|
||||
|
||||
|
Reference in New Issue
Block a user