mirror of
https://github.com/openfaas/faas.git
synced 2025-06-10 01:06:47 +00:00
Add error handling to basic auth injector
Fixes a problem where basic auth was disabled and a nill pointer was hit, causing a panic. Signed-off-by: Alex Ellis <alexellis2@gmail.com>
This commit is contained in:
parent
d2965df9f2
commit
e3b77514d0
@ -3,7 +3,7 @@ services:
|
|||||||
gateway:
|
gateway:
|
||||||
ports:
|
ports:
|
||||||
- 8080:8080
|
- 8080:8080
|
||||||
image: openfaas/gateway:0.13.7-rc4
|
image: openfaas/gateway:0.13.7-rc5
|
||||||
networks:
|
networks:
|
||||||
- functions
|
- functions
|
||||||
environment:
|
environment:
|
||||||
|
20
gateway/handlers/basic_auth_injector.go
Normal file
20
gateway/handlers/basic_auth_injector.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
// Copyright (c) OpenFaaS Author(s). All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
|
||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/openfaas/faas-provider/auth"
|
||||||
|
)
|
||||||
|
|
||||||
|
type BasicAuthInjector struct {
|
||||||
|
Credentials *auth.BasicAuthCredentials
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b BasicAuthInjector) Inject(r *http.Request) {
|
||||||
|
if r != nil && b.Credentials != nil {
|
||||||
|
r.SetBasicAuth(b.Credentials.User, b.Credentials.Password)
|
||||||
|
}
|
||||||
|
}
|
21
gateway/handlers/basic_auth_injector_test.go
Normal file
21
gateway/handlers/basic_auth_injector_test.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
// Copyright (c) OpenFaaS Author(s). All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
|
||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Inject_WithNilRequestAndNilCredentials(t *testing.T) {
|
||||||
|
injector := BasicAuthInjector{}
|
||||||
|
injector.Inject(nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Inject_WithRequestButNilCredentials(t *testing.T) {
|
||||||
|
injector := BasicAuthInjector{}
|
||||||
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||||
|
injector.Inject(req)
|
||||||
|
}
|
@ -89,7 +89,11 @@ func main() {
|
|||||||
functionURLTransformer = nilURLTransformer
|
functionURLTransformer = nilURLTransformer
|
||||||
}
|
}
|
||||||
|
|
||||||
serviceAuthInjector := &BasicAuthInjector{Credentials: credentials}
|
var serviceAuthInjector handlers.AuthInjector
|
||||||
|
|
||||||
|
if config.UseBasicAuth {
|
||||||
|
serviceAuthInjector = &handlers.BasicAuthInjector{Credentials: credentials}
|
||||||
|
}
|
||||||
|
|
||||||
decorateExternalAuth := handlers.MakeExternalAuthHandler
|
decorateExternalAuth := handlers.MakeExternalAuthHandler
|
||||||
|
|
||||||
@ -257,11 +261,3 @@ func runMetricsServer() {
|
|||||||
|
|
||||||
log.Fatal(s.ListenAndServe())
|
log.Fatal(s.ListenAndServe())
|
||||||
}
|
}
|
||||||
|
|
||||||
type BasicAuthInjector struct {
|
|
||||||
Credentials *auth.BasicAuthCredentials
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b BasicAuthInjector) Inject(r *http.Request) {
|
|
||||||
r.SetBasicAuth(b.Credentials.User, b.Credentials.Password)
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user