Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow non-admins to use the checkRelation #1111

Merged
merged 5 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/jimmctl/cmd/relation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,9 +612,9 @@ func (s *relationSuite) TestCheckRelation(c *gc.C) {
_, err := cmdtesting.RunCommand(
c,
cmd.NewCheckRelationCommandForTesting(s.ClientStore(), bClient),
"diglett",
"user-diglett",
"reader",
"dugtrio",
"controller-jimm",
)
c.Assert(err, gc.ErrorMatches, `unauthorized \(unauthorized access\)`)
}
10 changes: 6 additions & 4 deletions internal/jujuapi/access_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,15 +358,17 @@ func (r *controllerRoot) CheckRelation(ctx context.Context, req apiparams.CheckR
const op = errors.Op("jujuapi.CheckRelation")
checkResp := apiparams.CheckRelationResponse{Allowed: false}

if !r.user.JimmAdmin {
return checkResp, errors.E(op, errors.CodeUnauthorized, "unauthorized")
}

parsedTuple, err := r.parseTuple(ctx, req.Tuple)
if err != nil {
return checkResp, errors.E(op, errors.CodeFailedToParseTupleKey, err)
}

userCheckingSelf := parsedTuple.Object.Kind == openfga.UserType && parsedTuple.Object.ID == r.user.Username

if !(r.user.JimmAdmin || userCheckingSelf) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps a comment that:

  • admins are allowed to check any relations
  • users are only allowed to check relations of themselves to any resource

return checkResp, errors.E(op, errors.CodeUnauthorized, "unauthorized")
}

allowed, err := r.jimm.AuthorizationClient().CheckRelation(ctx, *parsedTuple, false)
if err != nil {
zapctx.Error(ctx, "failed to check relation", zap.NamedError("check-relation-error", err))
Expand Down
29 changes: 28 additions & 1 deletion internal/jujuapi/access_control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,6 @@ func (s *accessControlSuite) TestJAASTag(c *gc.C) {
c.Assert(err, gc.IsNil)
c.Assert(t, gc.Equals, test.expectedJAASTag)
}

}
}

Expand Down Expand Up @@ -877,6 +876,34 @@ func (s *accessControlSuite) TestListRelationshipTuples(c *gc.C) {

}

func (s *accessControlSuite) TestCheckRelationAsNonAdmin(c *gc.C) {
conn := s.open(c, nil, "bob")
defer conn.Close()
client := api.NewClient(conn)

userAliceKey := "user-alice@external"
userBobKey := "user-bob@external"

// Verify Bob checking for Alice's permission fails
input := apiparams.RelationshipTuple{
Object: userAliceKey,
Relation: "administrator",
TargetObject: "controller-jimm",
}
req := apiparams.CheckRelationRequest{Tuple: input}
_, err := client.CheckRelation(&req)
c.Assert(err, gc.ErrorMatches, `unauthorized \(unauthorized access\)`)
// Verify Bob can check for his own permission.
input = apiparams.RelationshipTuple{
Object: userBobKey,
Relation: "administrator",
TargetObject: "controller-jimm",
}
req = apiparams.CheckRelationRequest{Tuple: input}
_, err = client.CheckRelation(&req)
c.Assert(err, gc.IsNil)
}

func (s *accessControlSuite) TestCheckRelationOfferReaderFlow(c *gc.C) {
ctx := context.Background()
ofgaClient := s.JIMM.OpenFGAClient
Expand Down