-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
51a74f6
commit 130c4fa
Showing
13 changed files
with
582 additions
and
184 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package authz | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/obot-platform/nah/pkg/router" | ||
v1 "github.com/obot-platform/obot/pkg/storage/apis/obot.obot.ai/v1" | ||
"github.com/obot-platform/obot/pkg/system" | ||
"k8s.io/apiserver/pkg/authentication/user" | ||
) | ||
|
||
func (a *Authorizer) checkPendingAuthorization(req *http.Request, resources *Resources, user user.Info) (bool, error) { | ||
if resources.PendingAuthorizationID == "" { | ||
return true, nil | ||
} | ||
|
||
var ( | ||
threadAuth v1.ThreadAuthorization | ||
) | ||
|
||
if err := a.storage.Get(req.Context(), router.Key(system.DefaultNamespace, resources.PendingAuthorizationID), &threadAuth); err != nil { | ||
return false, err | ||
} | ||
|
||
for _, uid := range getValidUserIDs(user) { | ||
if threadAuth.Spec.UserID == uid { | ||
resources.Authorizated.PendingAuthorization = &threadAuth | ||
return true, nil | ||
} | ||
} | ||
|
||
return true, nil | ||
} |
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,71 @@ | ||
package authz | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"slices" | ||
"strings" | ||
|
||
"github.com/obot-platform/nah/pkg/router" | ||
v1 "github.com/obot-platform/obot/pkg/storage/apis/obot.obot.ai/v1" | ||
"github.com/obot-platform/obot/pkg/system" | ||
"k8s.io/apiserver/pkg/authentication/user" | ||
kclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func (a *Authorizer) checkProject(req *http.Request, resources *Resources, user user.Info) (bool, error) { | ||
if resources.ProjectID == "" { | ||
return true, nil | ||
} | ||
|
||
var ( | ||
agentID string | ||
validUserIDs = getValidUserIDs(user) | ||
thread v1.Thread | ||
projectThreadID = strings.Replace(resources.ProjectID, system.ProjectPrefix, system.ThreadPrefix, 1) | ||
) | ||
|
||
if err := a.storage.Get(req.Context(), router.Key(system.DefaultNamespace, projectThreadID), &thread); err != nil { | ||
return false, err | ||
} | ||
|
||
if resources.Authorizated.Assistant != nil { | ||
agentID = resources.Authorizated.Assistant.Name | ||
} | ||
|
||
if !a.projectIsAuthorized(req.Context(), agentID, &thread, validUserIDs) { | ||
return false, nil | ||
} | ||
|
||
resources.Authorizated.Project = &thread | ||
return true, nil | ||
} | ||
|
||
func (a *Authorizer) projectIsAuthorized(ctx context.Context, agentID string, thread *v1.Thread, validUserIDs []string) bool { | ||
if !thread.Spec.Project { | ||
return false | ||
} | ||
if agentID != "" { | ||
// If agent is available, make sure it's related | ||
if thread.Spec.AgentName != agentID { | ||
return false | ||
} | ||
} | ||
|
||
if slices.Contains(validUserIDs, thread.Spec.UserUID) { | ||
return true | ||
} | ||
|
||
for _, userID := range validUserIDs { | ||
var access v1.ThreadAuthorizationList | ||
err := a.storage.List(ctx, &access, kclient.InNamespace(system.DefaultNamespace), kclient.MatchingFields{ | ||
"spec.userID": userID, | ||
"spec.threadID": thread.Name, | ||
"spec.accepted": "true", | ||
}) | ||
if err == nil && len(access.Items) == 1 { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
Oops, something went wrong.