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

List service accounts in the organization members API #244

Closed
wants to merge 5 commits into from

Conversation

kedare
Copy link

@kedare kedare commented Jun 10, 2024

Fixes #163

Update the https://{instance}/realms/{realm}/orgs/{realm_id}/members/ endpoint to allow listing service-account users (removing the filter condition that would hide them)

Before:

[
	{
		"id": "2f8ba25c-db51-4525-87b2-b63bd9d58cc7",
		"createdTimestamp": 1718032779879,
		"username": "org-admin-1c074437-7429-435c-b769-d830ac7dcad2",
		"enabled": true,
		"totp": false,
		"emailVerified": true,
		"email": "org-admin-1c074437-7429-435c-b769-d830ac7dcad2@noreply.phasetwo.io",
		"disableableCredentialTypes": [],
		"requiredActions": [],
		"notBefore": 0
	},
	{
		"id": "87bdcee7-cd52-4662-b217-b205def4257b",
		"createdTimestamp": 1709729483380,
		"username": "mathieu.poussin@xxx.com",
		"enabled": true,
		"totp": false,
		"emailVerified": true,
		"firstName": "Mathieu",
		"lastName": "Poussin",
		"email": "mathieu.poussin@xxx.com",
		"disableableCredentialTypes": [],
		"requiredActions": [],
		"notBefore": 0
	}
]

After:

[
	{
		"id": "2f8ba25c-db51-4525-87b2-b63bd9d58cc7",
		"username": "org-admin-1c074437-7429-435c-b769-d830ac7dcad2",
		"email": "org-admin-1c074437-7429-435c-b769-d830ac7dcad2@noreply.phasetwo.io",
		"emailVerified": true,
		"createdTimestamp": 1718032779879,
		"enabled": true,
		"totp": false,
		"disableableCredentialTypes": [],
		"requiredActions": [],
		"notBefore": 0
	},
	{
		"id": "87bdcee7-cd52-4662-b217-b205def4257b",
		"username": "mathieu.poussin@xxx.com",
		"firstName": "Mathieu",
		"lastName": "Poussin",
		"email": "mathieu.poussin@xxx.com",
		"emailVerified": true,
		"createdTimestamp": 1709729483380,
		"enabled": true,
		"totp": false,
		"disableableCredentialTypes": [],
		"requiredActions": [],
		"notBefore": 0
	},
	{
		"id": "f9a3fba5-85d5-4ffa-97b7-ff2d5b1364e0",
		"username": "service-account-insomnia-m2m",
		"emailVerified": false,
		"createdTimestamp": 1709729428736,
		"enabled": true,
		"totp": false,
		"disableableCredentialTypes": [],
		"requiredActions": [],
		"notBefore": 0
	}
]

The service user will also appear in the admin UI on the members list

This did not impact the tests, it looks like this part is not covered by unit testing ?

@kedare
Copy link
Author

kedare commented Jun 10, 2024

It may be interesting to do also a second PR to allow the service accounts to be listed in the "Add members" part, but as it's managed by the Keycloak API I guess it's more a frontend modification ? (Not really something I know well)

@xgp
Copy link
Member

xgp commented Jun 10, 2024

We'd need to make sure any upstream users of this method aren't expecting it to be filtered. I know that the APIs assume that service accounts will not be returned/counted.

@kedare
Copy link
Author

kedare commented Jun 11, 2024

Should it then be a new API endpoint or a parameter on the endpoint ?

@xgp
Copy link
Member

xgp commented Jun 11, 2024

It should be a parameter that defaults to not showing/including them so that calling them as now produces the same result.

@kedare
Copy link
Author

kedare commented Jun 27, 2024

Any preference for the name of the parameters ? Something like includesServiceAccounts ? I will put the same filter on the API endpoint directly based on this conditional.

How should we do for the keycloak API endpoint used to display the list of users that can be added ? Likely it would be a frontend change instead ?

@xgp
Copy link
Member

xgp commented Jun 27, 2024

Any preference for the name of the parameters ? Something like includesServiceAccounts ? I will put the same filter on the API endpoint directly based on this conditional.

includeServiceAccounts

How should we do for the keycloak API endpoint used to display the list of users that can be added ? Likely it would be a frontend change instead?

You would need to extend Keycloak to do that.

@kedare
Copy link
Author

kedare commented Jul 1, 2024

This should be ok with this updated endpoint ?

For the frontend part I guess I should update this ?
https://github.com/p2-inc/keycloak/blob/23.0.1_orgs_admin_ui/js/apps/admin-ui/src/phaseII/orgs/useOrgFetcher.ts#L169-L181
Should I get the service accounts by default on the frontend part ?

I took a look at the keycloak source code also to add the same parameter to the users endpoints but no luck so far finding where to add is, the code structure is much more complex.

@kedare
Copy link
Author

kedare commented Jul 8, 2024

@xgp Let me know if you have any hints about anything else I would need to update.

For the current API part, does that looks ok to you ?

@xgp
Copy link
Member

xgp commented Jul 9, 2024

For the frontend part I guess I should update this ?

Yes. Current branch for 25 to be updated is here: https://github.com/p2-inc/keycloak/blob/25.0.0_orgs_admin_ui/js/apps/admin-ui/src/phaseII/orgs/useOrgFetcher.ts#L171-L183

We also need to update the openapi spec

https://github.com/p2-inc/phasetwo-docs/blob/main/openapi.yaml#L613

For the current API part, does that looks ok to you ?

Yes. As long as all tests are passing.

@kedare
Copy link
Author

kedare commented Jul 10, 2024

For the frontend part, what should be the behaviour ? Display the service accounts by default ?

@xgp
Copy link
Member

xgp commented Jul 10, 2024 via email

@xgp xgp requested a review from rtufisi July 15, 2024 17:55
@@ -198,8 +198,7 @@ public Long getMembersCount() {
public Stream<UserModel> getMembersStream() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

@xpg do we need to update getMembersCount?

I couldn't find the usage of /members/count endpoint

Copy link
Member

Choose a reason for hiding this comment

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

Yes, this should be updated and tested also.

Copy link
Member

Choose a reason for hiding this comment

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

@kedare Please update the /members/count method also

Copy link
Author

Choose a reason for hiding this comment

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

Do you know if by default it already contains the service account ? Because it's a named query that counds entries on OrganizationMemberEntity so I guess yes ? (if not that would mean the service accounts are on another table?)

Copy link
Collaborator

Choose a reason for hiding this comment

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

By default the /members/count endpoint contains both users + service account users.

Copy link
Author

Choose a reason for hiding this comment

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

I guess I have to check if getServiceAccountClientLink() is null ?
Also I see there are no relation defined between OrganizationMemberEntity and UserEntity (just the ID field buy no FK), is there any reason for this ? Should this be added? It's not 100% clear how to join them via HQL, it looks like the usual SQL join syntax would not work on this case ?

Copy link
Member

Choose a reason for hiding this comment

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

You can join them on the id

Copy link
Author

Choose a reason for hiding this comment

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

What would be the best way to test HQL named queries ? Or I need to compile and run the whole test suite every time ?

Copy link
Author

Choose a reason for hiding this comment

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

Maybe someone could write the HQL query directly ? I guess it would take less than a few minutes for someone that knows Hibernate well.

Copy link
Member

Choose a reason for hiding this comment

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

Please let me know when you are able to find someone to complete this feature for you. I am closing the PR until that time.

@pierre-tranchard
Copy link

@rtufisi @xgp is there anything remaining to do before you can merge it? Thank you in advance 🙏

@xgp
Copy link
Member

xgp commented Jul 31, 2024

Closing until it is possible to submit a completed PR.

@xgp xgp closed this Jul 31, 2024
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.

Client Credentials Grant / machine-to-machine auth in organizations
4 participants