Enable basic auth for service query / scaling on provider

- this is a blocking issue for auth with Docker Swarm
fixes #879

Signed-off-by: Alex Ellis (VMware) <alexellis2@gmail.com>
This commit is contained in:
Alex Ellis (VMware)
2018-09-19 20:41:28 +01:00
committed by Alex Ellis
parent e7cfaa1b9b
commit 3598da2e51
5 changed files with 25 additions and 8 deletions

View File

@ -17,12 +17,13 @@ import (
"io/ioutil"
"github.com/openfaas/faas-provider/auth"
"github.com/openfaas/faas/gateway/handlers"
"github.com/openfaas/faas/gateway/requests"
)
// NewExternalServiceQuery proxies service queries to external plugin via HTTP
func NewExternalServiceQuery(externalURL url.URL) handlers.ServiceQuery {
func NewExternalServiceQuery(externalURL url.URL, credentials *auth.BasicAuthCredentials) handlers.ServiceQuery {
proxyClient := http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
@ -40,6 +41,7 @@ func NewExternalServiceQuery(externalURL url.URL) handlers.ServiceQuery {
return ExternalServiceQuery{
URL: externalURL,
ProxyClient: proxyClient,
Credentials: credentials,
}
}
@ -47,6 +49,7 @@ func NewExternalServiceQuery(externalURL url.URL) handlers.ServiceQuery {
type ExternalServiceQuery struct {
URL url.URL
ProxyClient http.Client
Credentials *auth.BasicAuthCredentials
}
// ScaleServiceRequest request scaling of replica
@ -65,10 +68,13 @@ func (s ExternalServiceQuery) GetReplicas(serviceName string) (handlers.ServiceQ
req, _ := http.NewRequest(http.MethodGet, urlPath, nil)
if s.Credentials != nil {
req.SetBasicAuth(s.Credentials.User, s.Credentials.Password)
}
res, err := s.ProxyClient.Do(req)
if err != nil {
log.Println(urlPath, err)
} else {
@ -129,6 +135,11 @@ 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)
}
defer req.Body.Close()
res, err := s.ProxyClient.Do(req)