Skip to content

Commit 3497638

Browse files
committed
Make HTTP authentication as the middleware
Replace bogus HTTP header check with the real HTTP auth middleware, add the option to support various auth backend type, and remove unnecessary unit test for missing authorization header. Add `auth` as the option to config.yaml for API authentication and authorization mechanism configuration. If this is not specified, the `passthrough` backend will be used (i.e. no authentication will be performed). See auth config from [readme](https://github.com/veraison/services/blob/main/auth/README.md). Note `role` is not used in ratsd, and thus can be removed from config.yaml Signed-off-by: Ian Chin Wang <[email protected]>
1 parent c9f064f commit 3497638

File tree

3 files changed

+18
-44
lines changed

3 files changed

+18
-44
lines changed

api/server.go

-13
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
// Defines missing consts in the API Spec
1616
const (
1717
ApplicationvndVeraisonCharesJson string = "application/vnd.veraison.chares+json"
18-
ExpectedAuth string = "Bearer my.jwt.token"
1918
)
2019

2120
type Server struct {
@@ -38,18 +37,6 @@ func (s *Server) reportProblem(w http.ResponseWriter, prob *problems.DefaultProb
3837
func (s *Server) RatsdChares(w http.ResponseWriter, r *http.Request, param RatsdCharesParams) {
3938
var requestData ChaResRequest
4039

41-
auth := r.Header.Get("Authorization")
42-
if auth != ExpectedAuth {
43-
p := &problems.DefaultProblem{
44-
Type: string(TagGithubCom2024VeraisonratsdErrorUnauthorized),
45-
Title: string(AccessUnauthorized),
46-
Detail: "wrong or missing authorization header",
47-
Status: http.StatusUnauthorized,
48-
}
49-
s.reportProblem(w, p)
50-
return
51-
}
52-
5340
// Check if content type matches the expectation
5441
ct := r.Header.Get("Content-Type")
5542
if ct != ApplicationvndVeraisonCharesJson {

api/server_test.go

-29
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,6 @@ const (
1919
jsonType = "application/json"
2020
)
2121

22-
func TestRatsdChares_missing_auth_header(t *testing.T) {
23-
expectedCode := http.StatusUnauthorized
24-
expectedType := problems.ProblemMediaType
25-
expectedBody := &problems.DefaultProblem{
26-
Type: string(TagGithubCom2024VeraisonratsdErrorUnauthorized),
27-
Title: string(AccessUnauthorized),
28-
Status: http.StatusUnauthorized,
29-
Detail: "wrong or missing authorization header",
30-
}
31-
32-
var params RatsdCharesParams
33-
logger := log.Named("test")
34-
s := &Server{logger: logger}
35-
w := httptest.NewRecorder()
36-
r, _ := http.NewRequest(http.MethodPost, "/ratsd/chares", http.NoBody)
37-
s.RatsdChares(w, r, params)
38-
39-
var body problems.DefaultProblem
40-
_ = json.Unmarshal(w.Body.Bytes(), &body)
41-
42-
assert.Equal(t, expectedCode, w.Code)
43-
assert.Equal(t, expectedType, w.Result().Header.Get("Content-Type"))
44-
assert.Equal(t, expectedBody, &body)
45-
}
46-
4722
func TestRatsdChares_wrong_content_type(t *testing.T) {
4823
expectedCode := http.StatusBadRequest
4924
expectedType := problems.ProblemMediaType
@@ -59,7 +34,6 @@ func TestRatsdChares_wrong_content_type(t *testing.T) {
5934
s := &Server{logger: logger}
6035
w := httptest.NewRecorder()
6136
r, _ := http.NewRequest(http.MethodPost, "/ratsd/chares", http.NoBody)
62-
r.Header.Add("Authorization", ExpectedAuth)
6337
r.Header.Add("Content-Type", jsonType)
6438
s.RatsdChares(w, r, params)
6539

@@ -80,7 +54,6 @@ func TestRatsdChares_wrong_accept_type(t *testing.T) {
8054
s := &Server{logger: logger}
8155
w := httptest.NewRecorder()
8256
r, _ := http.NewRequest(http.MethodPost, "/ratsd/chares", http.NoBody)
83-
r.Header.Add("Authorization", ExpectedAuth)
8457
r.Header.Add("Content-Type", ApplicationvndVeraisonCharesJson)
8558
s.RatsdChares(w, r, params)
8659

@@ -108,7 +81,6 @@ func TestRatsdChares_missing_nonce(t *testing.T) {
10881
w := httptest.NewRecorder()
10982
rb := strings.NewReader("{\"noncee\": \"MIDBNH28iioisjPy\"}")
11083
r, _ := http.NewRequest(http.MethodPost, "/ratsd/chares", rb)
111-
r.Header.Add("Authorization", ExpectedAuth)
11284
r.Header.Add("Content-Type", ApplicationvndVeraisonCharesJson)
11385
s.RatsdChares(w, r, params)
11486

@@ -139,7 +111,6 @@ func TestRatsdChares_valid_request(t *testing.T) {
139111
w := httptest.NewRecorder()
140112
rb := strings.NewReader("{\"nonce\": \"MIDBNH28iioisjPy\"}")
141113
r, _ := http.NewRequest(http.MethodPost, "/ratsd/chares", rb)
142-
r.Header.Add("Authorization", ExpectedAuth)
143114
r.Header.Add("Content-Type", ApplicationvndVeraisonCharesJson)
144115
s.RatsdChares(w, r, params)
145116

cmd/main.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/veraison/ratsd/api"
1010
"github.com/veraison/ratsd/plugin"
11+
"github.com/veraison/ratsd/auth"
1112
"github.com/veraison/services/config"
1213
"github.com/veraison/services/log"
1314
)
@@ -45,7 +46,7 @@ func main() {
4546
Protocol: "https",
4647
}
4748

48-
subs, err := config.GetSubs(v, "ratsd", "*logging")
49+
subs, err := config.GetSubs(v, "ratsd", "*logging", "*auth")
4950
if err != nil {
5051
log.Fatal(err)
5152
}
@@ -55,6 +56,17 @@ func main() {
5556
log.Fatalf("could not configure logging: %v", err)
5657
}
5758

59+
authorizer, err := auth.NewAuthorizer(subs["auth"], log.Named("auth"))
60+
if err != nil {
61+
log.Fatalf("could not init authorizer: %v", err)
62+
}
63+
defer func() {
64+
err := authorizer.Close()
65+
if err != nil {
66+
log.Errorf("Could not close authorizer: %v", err)
67+
}
68+
}()
69+
5870
log.Infow("Initializing ratsd core")
5971

6072
loader := config.NewLoader(&cfg)
@@ -74,7 +86,11 @@ func main() {
7486

7587
svr := api.NewServer(log.Named("api"))
7688
r := http.NewServeMux()
77-
h := api.HandlerFromMux(svr, r)
89+
options := api.StdHTTPServerOptions{
90+
BaseRouter: r,
91+
Middlewares: []api.MiddlewareFunc{authorizer.GetMiddleware},
92+
}
93+
h := api.HandlerWithOptions(svr, options)
7894

7995
s := &http.Server{
8096
Handler: h,

0 commit comments

Comments
 (0)