-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bff): user info endpoint (#630)
* feat(bff): user info endpoint Signed-off-by: Eder Ignatowicz <ignatowicz@gmail.com> * Update clients/ui/bff/README.md Co-authored-by: Griffin Sullivan <48397354+Griffin-Sullivan@users.noreply.github.com> Signed-off-by: Eder Ignatowicz <ignatowicz@gmail.com> --------- Signed-off-by: Eder Ignatowicz <ignatowicz@gmail.com> Co-authored-by: Griffin Sullivan <48397354+Griffin-Sullivan@users.noreply.github.com>
- Loading branch information
1 parent
20c04c7
commit 1449fb3
Showing
11 changed files
with
333 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package api | ||
|
||
import ( | ||
"errors" | ||
"github.com/julienschmidt/httprouter" | ||
"github.com/kubeflow/model-registry/ui/bff/internal/models" | ||
"net/http" | ||
) | ||
|
||
type UserEnvelope Envelope[*models.User, None] | ||
|
||
func (app *App) UserHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { | ||
|
||
userHeader := r.Header.Get(kubeflowUserId) | ||
if userHeader == "" { | ||
app.serverErrorResponse(w, r, errors.New("kubeflow-userid not present on header")) | ||
return | ||
} | ||
|
||
user, err := app.repositories.User.GetUser(app.kubernetesClient, userHeader) | ||
if err != nil { | ||
app.serverErrorResponse(w, r, err) | ||
return | ||
} | ||
|
||
userRes := UserEnvelope{ | ||
Data: user, | ||
} | ||
|
||
err = app.WriteJSON(w, http.StatusOK, userRes, nil) | ||
|
||
if err != nil { | ||
app.serverErrorResponse(w, r, err) | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
|
||
"github.com/kubeflow/model-registry/ui/bff/internal/repositories" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
const ( | ||
KubeflowUserIDHeaderValue = "user@example.com" | ||
DoraNonAdminUser = "doraNonAdmin@example.com" | ||
) | ||
|
||
var _ = Describe("TestUserHandler", func() { | ||
Context("fetching user details", Ordered, func() { | ||
var testApp App | ||
|
||
BeforeAll(func() { | ||
By("creating the test app") | ||
testApp = App{ | ||
kubernetesClient: k8sClient, | ||
repositories: repositories.NewRepositories(mockMRClient), | ||
logger: logger, | ||
} | ||
}) | ||
|
||
It("should show that KubeflowUserIDHeaderValue (user@example.com) is a cluster-admin", func() { | ||
By("creating the http request") | ||
req, err := http.NewRequest(http.MethodGet, UserPath, nil) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
req.Header.Set(kubeflowUserId, KubeflowUserIDHeaderValue) | ||
|
||
By("creating the http test infrastructure") | ||
rr := httptest.NewRecorder() | ||
|
||
By("invoking the UserHandler") | ||
testApp.UserHandler(rr, req, nil) | ||
rs := rr.Result() | ||
defer rs.Body.Close() | ||
body, err := io.ReadAll(rs.Body) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("unmarshalling the user response") | ||
var actual UserEnvelope | ||
err = json.Unmarshal(body, &actual) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(rr.Code).To(Equal(http.StatusOK)) | ||
|
||
By("checking that the user is cluster-admin") | ||
Expect(actual.Data.UserID).To(Equal(KubeflowUserIDHeaderValue)) | ||
Expect(actual.Data.ClusterAdmin).To(BeTrue(), "Expected this user to be cluster-admin") | ||
}) | ||
|
||
It("should show that DoraNonAdminUser (doraNonAdmin@example.com) is not a cluster-admin", func() { | ||
By("creating the http request") | ||
req, err := http.NewRequest(http.MethodGet, UserPath, nil) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
req.Header.Set(kubeflowUserId, DoraNonAdminUser) | ||
|
||
By("creating the http test infrastructure") | ||
rr := httptest.NewRecorder() | ||
|
||
By("invoking the UserHandler") | ||
testApp.UserHandler(rr, req, nil) | ||
rs := rr.Result() | ||
defer rs.Body.Close() | ||
body, err := io.ReadAll(rs.Body) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("unmarshalling the user response") | ||
var actual UserEnvelope | ||
err = json.Unmarshal(body, &actual) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(rr.Code).To(Equal(http.StatusOK)) | ||
|
||
By("checking that the user is not cluster-admin") | ||
Expect(actual.Data.UserID).To(Equal(DoraNonAdminUser)) | ||
Expect(actual.Data.ClusterAdmin).To(BeFalse(), "Expected this user to not be cluster-admin") | ||
}) | ||
|
||
It("should show that a random non-existent user is not a cluster-admin", func() { | ||
randomUser := "bellaUser@example.com" | ||
|
||
By("creating the http request") | ||
req, err := http.NewRequest(http.MethodGet, UserPath, nil) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
req.Header.Set(kubeflowUserId, randomUser) | ||
|
||
By("creating the http test infrastructure") | ||
rr := httptest.NewRecorder() | ||
|
||
By("invoking the UserHandler") | ||
testApp.UserHandler(rr, req, nil) | ||
rs := rr.Result() | ||
defer rs.Body.Close() | ||
body, err := io.ReadAll(rs.Body) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("unmarshalling the user response") | ||
var actual UserEnvelope | ||
err = json.Unmarshal(body, &actual) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(rr.Code).To(Equal(http.StatusOK)) | ||
|
||
By("checking that the user is not cluster-admin") | ||
Expect(actual.Data.UserID).To(Equal(randomUser)) | ||
Expect(actual.Data.ClusterAdmin).To(BeFalse(), "Expected this user to not be cluster-admin") | ||
}) | ||
}) | ||
|
||
}) |
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
Oops, something went wrong.