-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ws): add auth to backend (#202)
* feat(ws): add auth to backend Signed-off-by: Mathew Wicks <5735406+thesuperzapper@users.noreply.github.com> * add `DISABLE_AUTH` for interim testing (enabled by default) Signed-off-by: Mathew Wicks <5735406+thesuperzapper@users.noreply.github.com> --------- Signed-off-by: Mathew Wicks <5735406+thesuperzapper@users.noreply.github.com>
- Loading branch information
1 parent
4cbc26e
commit bc6f311
Showing
18 changed files
with
725 additions
and
134 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
Copyright 2024. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package api | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"k8s.io/apiserver/pkg/authorization/authorizer" | ||
|
||
"github.com/kubeflow/notebooks/workspaces/backend/internal/auth" | ||
) | ||
|
||
// requireAuth verifies that the request is authenticated and authorized to take the actions specified by the given policies. | ||
// If this method returns false, the request has been handled and the caller should return immediately. | ||
// If this method returns true, the request is authenticated and authorized to proceed. | ||
// This method should only be called once per request. | ||
func (a *App) requireAuth(w http.ResponseWriter, r *http.Request, policies []*auth.ResourcePolicy) bool { | ||
ctx := r.Context() | ||
|
||
// if auth is disabled, allow the request to proceed | ||
if a.Config.DisableAuth { | ||
return true | ||
} | ||
|
||
// authenticate the request (extract user and groups from the request headers) | ||
res, ok, err := a.RequestAuthN.AuthenticateRequest(r) | ||
if err != nil { | ||
err = fmt.Errorf("failed to authenticate request: %w", err) | ||
a.serverErrorResponse(w, r, err) | ||
return false | ||
} | ||
if !ok { | ||
a.unauthorizedResponse(w, r) | ||
return false | ||
} | ||
|
||
// for each policy, check if the user is authorized to take the requested action | ||
for _, policy := range policies { | ||
attributes := policy.AttributesFor(res.User) | ||
authorized, reason, err := a.RequestAuthZ.Authorize(ctx, attributes) | ||
if err != nil { | ||
err = fmt.Errorf("failed to authorize request for user %q: %w", res.User.GetName(), err) | ||
a.serverErrorResponse(w, r, err) | ||
return false | ||
} | ||
|
||
if authorized != authorizer.DecisionAllow { | ||
msg := fmt.Sprintf("authorization was denied for user %q", res.User.GetName()) | ||
if reason != "" { | ||
msg = fmt.Sprintf("%s: %s", msg, reason) | ||
} | ||
a.forbiddenResponse(w, r, msg) | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} |
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
Oops, something went wrong.