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

add ability to list all groups #612

Merged
merged 4 commits into from
Oct 23, 2017
Merged

add ability to list all groups #612

merged 4 commits into from
Oct 23, 2017

Conversation

joshuarubin
Copy link

This is to satisfy #594

I'm not sure if the api.swagger.yaml file is complete/correct since the gen-swagger.sh script produces the uncommitted api.swagger.json file.

The API is also a little strange as the GET /warden/groups endpoint already existed and previously required a member param (and GET /warden/groups/<id> refers to the group id, not the member id).

Now GET /warden/groups OPTIONALLY uses the member param. If it exists, the behavior is the same as before so there are no API regressions.

However, if member does not exist, then it is assumed that the user wants to list all groups. In this case, the required policy resource changes (from rn:hydra:warden:groups:<member> to rn:hydra:warden:groups). Additionally, the limit and offset parameters are used if they exist. limit defaults to 500 if not specified (the same as GET /policies).

Signed-off-by: Joshua Rubin <[email protected]>
@aeneasr
Copy link
Member

aeneasr commented Oct 18, 2017

Hey @joshuarubin the way you solved it is perfect. It is common to have a resource endpoints /resources which lists everything, and then query options like /resources?owner=1234 to request a subset (similar to limit & offset).

The only thing I would like to change is to remove rn:hydra:warden:groups:<member> and only do rn:hydra:warden:groups AC.

I will now review your code changes and leave comments where they fit.

@aeneasr
Copy link
Member

aeneasr commented Oct 18, 2017

Damn, halfway through the review I noticed that you made this against the 0.9.x branch. I would much rather see it done against the 0.10.0 one, is that an issue for you?

@joshuarubin
Copy link
Author

We would like to be able to get this functionality ASAP which would be less risky if we can stay on 0.9.x. However, I don't think it would be too hard to cherry pick the commits and get it working on master. If we can merge this into 0.9.x, I'll set up another PR to get it into master.

@@ -1376,8 +1376,8 @@ paths:
/warden/groups:
get:
description: >-
The subject making the request needs to be assigned to a policy
containing:
The subject making the request, if member is specified, needs to be
Copy link
Member

Choose a reason for hiding this comment

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

As discussed in my comment, I'd like to revert this and replace this with a policy check against rn:hydra:warden:groups

The subject making the request needs to be assigned to a policy
containing:
The subject making the request, if member is specified, needs to be
assigned to a policy containing:
Copy link
Member

Choose a reason for hiding this comment

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

As discussed in my comment, replace this with one check to rn:hydra:warden:groups

@@ -1403,8 +1420,8 @@ paths:
tags:
- warden
- groups
summary: Find group IDs by member
operationId: findGroupsByMember
summary: Find group IDs
Copy link
Member

Choose a reason for hiding this comment

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

In fact, we're receiving the full group payload (no longer only their IDs). This isn't your fault, but mine since I forgot to update the header title. But since you're already at it, maybe you could change that too ... :)

Copy link
Member

Choose a reason for hiding this comment

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

This applies to the 0.10.x branch - for the 0.9.x branch this is correct.

@@ -40,11 +41,11 @@ func (h *Handler) SetRoutes(r *httprouter.Router) {
r.DELETE(GroupsHandlerPath+"/:id/members", h.RemoveGroupMembers)
}

// swagger:route GET /warden/groups warden groups findGroupsByMember
// swagger:route GET /warden/groups warden groups findGroups
Copy link
Member

Choose a reason for hiding this comment

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

This should actually be renamed to listGroups

Copy link
Member

Choose a reason for hiding this comment

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

ping

//
// Find group IDs by member
// Find group IDs
Copy link
Member

Choose a reason for hiding this comment

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

List groups

Copy link
Member

Choose a reason for hiding this comment

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

For 0.9.x this should be List group IDs

Copy link
Member

Choose a reason for hiding this comment

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

ping

@@ -123,6 +125,28 @@ func (m *SQLManager) RemoveGroupMembers(group string, subjects []string) error {
return nil
}

func (m *SQLManager) ListGroups(limit, offset int64) ([]string, error) {
if limit < 0 {
Copy link
Member

Choose a reason for hiding this comment

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

I think it should be fine to set limit to 0 (or 1) if it's below 0

Copy link
Author

Choose a reason for hiding this comment

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

OK

return nil, errors.New("limit can't be less than 0")
}

if offset < 0 {
Copy link
Member

Choose a reason for hiding this comment

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

I think it should be fine to set offset to 0 if it's below 0

Copy link
Author

Choose a reason for hiding this comment

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

OK

@@ -85,6 +85,8 @@ func connectToPG() {
}

func TestManagers(t *testing.T) {
t.Parallel()
Copy link
Member

Choose a reason for hiding this comment

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

This is nice, I totally did not know about that flag. That way, postgres and mysql will be run alongside. This should be everywhere. Thank you!

#617

limit = 500
}

if err := m.DB.Select(&q, m.DB.Rebind("SELECT id from hydra_warden_group ORDER BY id LIMIT ? OFFSET ?"), limit, offset); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure about the order clause here, what it's goal?

Copy link
Author

Choose a reason for hiding this comment

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

ORDER BY is required to ensure that the paging is consistent.
https://www.postgresql.org/docs/8.2/static/queries-limit.html

Copy link
Member

@aeneasr aeneasr Oct 18, 2017

Choose a reason for hiding this comment

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

Oh, I did not know that! Then this is fine!

res = append(res, g.ID)
}

sort.Strings(res)
Copy link
Member

Choose a reason for hiding this comment

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

Sorting makes sense here because maps don't have an order.

@aeneasr
Copy link
Member

aeneasr commented Oct 18, 2017

We would like to be able to get this functionality ASAP which would be less risky if we can stay on 0.9.x. However, I don't think it would be too hard to cherry pick the commits and get it working on master. If we can merge this into 0.9.x, I'll set up another PR to get it into master.

I would love to see that. If you find the time, I'd also love to see that for #605 :)

Signed-off-by: Joshua Rubin <[email protected]>
@joshuarubin
Copy link
Author

OK. Updated with changes from review. Still don't know how to generate the api.swagger.yaml so I hope it's correct.

@aeneasr
Copy link
Member

aeneasr commented Oct 19, 2017

Nice! I'll go over the changes one more time. Regarding swagger, check out this script from the 0.10.x branch. Please make sure to use the latest stable version of go-swagger.

Copy link
Member

@aeneasr aeneasr left a comment

Choose a reason for hiding this comment

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

Just a couple of more things, then good to go

@@ -40,11 +41,11 @@ func (h *Handler) SetRoutes(r *httprouter.Router) {
r.DELETE(GroupsHandlerPath+"/:id/members", h.RemoveGroupMembers)
}

// swagger:route GET /warden/groups warden groups findGroupsByMember
// swagger:route GET /warden/groups warden groups findGroups
Copy link
Member

Choose a reason for hiding this comment

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

ping

//
// Find group IDs by member
// Find group IDs
Copy link
Member

Choose a reason for hiding this comment

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

ping

//
// The subject making the request needs to be assigned to a policy containing:
// The subject making the request, if member is specified, needs to be assigned to a policy containing:
Copy link
Member

Choose a reason for hiding this comment

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

This line and the following policy can be removed, as discussed in the comments

@@ -66,24 +77,65 @@ func (h *Handler) SetRoutes(r *httprouter.Router) {
// oauth2: hydra.groups
//
// Responses:
// 200: findGroupsByMemberResponse
// 200: findGroupsResponse
Copy link
Member

Choose a reason for hiding this comment

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

ping

return
}

offset, err := intFromQuery(r, "offset")
Copy link
Member

Choose a reason for hiding this comment

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

If you supply a default value, no error checking is requried and you can save the limit == 0 check!

Signed-off-by: Joshua Rubin <[email protected]>
@joshuarubin
Copy link
Author

OK. Updated again.

Copy link
Member

@aeneasr aeneasr left a comment

Choose a reason for hiding this comment

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

I missed one thing, which is unfortunately ever so important. After that I hope we're good to go!

offset = 0
}

var q []string
Copy link
Member

Choose a reason for hiding this comment

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

If no results are returned, this will be nil which in turn will return null in the JSON response (see https://play.golang.org/p/7H6y1t2mdd ). To avoid this please initialize the slice (q := []string{})

return nil, nil
}

var res []string
Copy link
Member

Choose a reason for hiding this comment

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

If no results are returned, this will be nil which in turn will return null in the JSON response (see https://play.golang.org/p/7H6y1t2mdd ). To avoid this please initialize the slice (q := []string{})

@joshuarubin
Copy link
Author

Nice catch. I think everything should be ready now.

@aeneasr
Copy link
Member

aeneasr commented Oct 23, 2017

Thank you!

@aeneasr aeneasr merged commit e248e83 into ory:v0.9.x Oct 23, 2017
@aeneasr
Copy link
Member

aeneasr commented Oct 23, 2017

aeneasr pushed a commit that referenced this pull request Oct 25, 2017
* vendor: support dep (#606)

* fix go-jose

Signed-off-by: Joshua Rubin <[email protected]>

* add Gopkg.toml

Signed-off-by: Joshua Rubin <[email protected]>

* add tests for dep

Signed-off-by: Joshua Rubin <[email protected]>

* fix glide build

Signed-off-by: Joshua Rubin <[email protected]>

* warden/groups: add ability to list all groups (#612)

* add ability to list all groups

Signed-off-by: Joshua Rubin <[email protected]>

* update based on review comments

Signed-off-by: Joshua Rubin <[email protected]>

* a few more updates from review

Signed-off-by: Joshua Rubin <[email protected]>

* ensure group lists dont return nil

Signed-off-by: Joshua Rubin <[email protected]>

* support es256

Signed-off-by: Joshua Rubin <[email protected]>

* update jwk tests

Signed-off-by: Joshua Rubin <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants