-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ignore auth for
/swagger.json
endpoint (#1377)
* Ignore authentication for `/swagger.json` in ReBAC admin API Signed-off-by: Babak K. Shandiz <[email protected]> * Add test case to verify skipping auth for `/swagger.json` Signed-off-by: Babak K. Shandiz <[email protected]> * Fix ReBAC Admin test Signed-off-by: Babak K. Shandiz <[email protected]> * Use a map to lookup for endpoints Signed-off-by: Babak K. Shandiz <[email protected]> * Upgrade `rebac-admin-ui-handlers` to `v0.1.2` Signed-off-by: Babak K. Shandiz <[email protected]> * Use zero-length struct in map/set declaration Signed-off-by: Babak K. Shandiz <[email protected]> --------- Signed-off-by: Babak K. Shandiz <[email protected]>
- Loading branch information
Showing
6 changed files
with
82 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ package middleware_test | |
import ( | ||
"context" | ||
"errors" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
@@ -22,12 +23,18 @@ import ( | |
|
||
// Checks if the authenticator responsible for access control to rebac admin handlers works correctly. | ||
func TestAuthenticateRebac(t *testing.T) { | ||
c := qt.New(t) | ||
baseURL := "/rebac" | ||
|
||
testUser := "[email protected]" | ||
tests := []struct { | ||
name string | ||
setupRequest func() *http.Request | ||
setupHandler func() http.Handler | ||
mockAuthBrowserSession func(ctx context.Context, w http.ResponseWriter, req *http.Request) (context.Context, error) | ||
jimmAdmin bool | ||
expectedStatus int | ||
expectedBody string | ||
}{ | ||
{ | ||
name: "success", | ||
|
@@ -59,12 +66,26 @@ func TestAuthenticateRebac(t *testing.T) { | |
jimmAdmin: false, | ||
expectedStatus: http.StatusUnauthorized, | ||
}, | ||
{ | ||
name: "should skip auth for /swagger.json", | ||
setupRequest: func() *http.Request { | ||
req, _ := http.NewRequest(http.MethodGet, baseURL+"/v1/swagger.json", nil) | ||
return req | ||
}, | ||
setupHandler: func() http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
_, _ = w.Write([]byte("the-body")) | ||
}) | ||
}, | ||
jimmAdmin: false, | ||
expectedStatus: http.StatusOK, | ||
expectedBody: "the-body", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
c := qt.New(t) | ||
|
||
c.Run(tt.name, func(c *qt.C) { | ||
j := jimmtest.JIMM{ | ||
LoginService: mocks.LoginService{ | ||
AuthenticateBrowserSession_: func(ctx context.Context, w http.ResponseWriter, req *http.Request) (context.Context, error) { | ||
|
@@ -76,23 +97,41 @@ func TestAuthenticateRebac(t *testing.T) { | |
return &openfga.User{Identity: &user, JimmAdmin: tt.jimmAdmin}, nil | ||
}, | ||
} | ||
req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
|
||
var req *http.Request | ||
if tt.setupRequest != nil { | ||
req = tt.setupRequest() | ||
} else { | ||
req = httptest.NewRequest(http.MethodGet, baseURL, nil) | ||
} | ||
|
||
w := httptest.NewRecorder() | ||
|
||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
identity, err := rebac_handlers.GetIdentityFromContext(r.Context()) | ||
c.Assert(err, qt.IsNil) | ||
var handler http.Handler | ||
if tt.setupHandler != nil { | ||
handler = tt.setupHandler() | ||
} else { | ||
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
identity, err := rebac_handlers.GetIdentityFromContext(r.Context()) | ||
c.Assert(err, qt.IsNil) | ||
|
||
user, ok := identity.(*openfga.User) | ||
c.Assert(ok, qt.IsTrue) | ||
c.Assert(user.Name, qt.Equals, testUser) | ||
user, ok := identity.(*openfga.User) | ||
c.Assert(ok, qt.IsTrue) | ||
c.Assert(user.Name, qt.Equals, testUser) | ||
|
||
w.WriteHeader(http.StatusOK) | ||
}) | ||
middleware := middleware.AuthenticateRebac(handler, &j) | ||
w.WriteHeader(http.StatusOK) | ||
}) | ||
} | ||
|
||
middleware := middleware.AuthenticateRebac(baseURL, handler, &j) | ||
middleware.ServeHTTP(w, req) | ||
|
||
c.Assert(w.Code, qt.Equals, tt.expectedStatus) | ||
if tt.expectedBody != "" { | ||
body, err := io.ReadAll(w.Body) | ||
c.Assert(err, qt.IsNil) | ||
c.Assert(string(body), qt.Equals, tt.expectedBody) | ||
} | ||
}) | ||
} | ||
} |