8000 Keycloak 10489 support for client secret rotation by marcelomrwin · Pull Request #10603 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Keycloak 10489 support for client secret rotation - #10603

Merged
mposolda merged 31 commits into
keycloak:mainfrom
marcelomrwin:KEYCLOAK-10489-support-for-client-secret-rotation
Mar 8, 2022
Merged

mposolda merged 31 commits into
keycloak:mainfrom
marcelomrwin:KEYCLOAK-10489-support-for-client-secret-rotation

Conversation

@marcelomrwin
@marcelomrwin marcelomrwin commented Mar 4, 2022 •
Copy link
Copy Markdown
Contributor

#10489

  • Updates the authentication flow considering secret rotation.
  • Allows rotation directly from the administrative interface
  • Allows creation of secret rotation policy
  • Allows validation of policy executor settings
  • Added two new endpoints "/admin/realms/:realm/clients/:client/client-secret/rotated"

masales and others added 19 commits February 26, 2022 23:56
@marcelomrwin
marcelomrwin requested review from mposolda and stianst March 4, 2022 16:11
@marcelomrwin
marcelomrwin marked this pull request as draft March 4, 2022 16:11
masales and others added 5 commits March 5, 2022 12:21
…iptionConverter, ensure when client creation and policy is enabled the policy is executed
…iptionConverter, ensure when client creation and policy is enabled the policy is executed
…iptionConverter, ensure when client creation and policy is enabled the policy is executed
…iptionConverter, ensure when client creation and policy is enabled the policy is executed
@mposolda mposolda self-assigned this Mar 7, 2022
@mposolda mposolda added team/core-features priority/important Must be worked on very soon labels Mar 7, 2022
@mposolda mposolda left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@marcelomrwin This is nice work and great start!

I've added some inline comments. But I think nothing major.

Also I have few other concerns in relation when I was reviewing and testing this. I am not sure if to address them in this PR or rather in the follow-up PRs? I vote that 1 and 2 are addressed in this PR as those are relatively minor things. The issues 3, 4, 5 I vote that you create follow-up tasks for them into "client secret rotation" epic and send separate PRs for each of them. WDYT?

Then other issues (

  1. Formatting: Keycloak uses indentiation of 4 spaces inside Java file. I noticed you used 2 spaces in most of the places in this PR. Would you mind to update it?

  2. Logging: Maybe a bit of debug logging will be nice to add for example to ClientSecretRotationExecutor and maybe some other places? For example add some debug message when client secret rotated for client X and that new secret expires at time Y and old secret expires at time Z etc.

  3. It may be good to add some automated test that trying to configure executor with the invalid configuration will fail. Also will be nice to have some admin REST API tests for "invalidate secret" and "get rotated secret" (I commented inline).

3.a) I think I've just found a bug in relation to the point above :-) I've tried this in the admin console:

  • Create new client profile
  • Click to create "secret-rotation" executor inside this new profile
  • When I was requested to fill the configuration, I filled "Rotated Secret expiration" to be bigger than "Secret Expiration" . Which is invalid configuration. Saving of provider was not allowed and 8000 admin console thrown the error that configuration is invalid (which is correct behaviour). So I fixed it and filled correct "Rotated Secret Expiration" .
  • After click "Save", I can see that there are 2 executors of "secret-expiration" in my client profile.

It seems that failed validation of executor still managed to create this executor, which is a bug IMO.

FYI. I've tried also to update existing executor with the invalid configuration and it worked as expected. So it seems just the creation of new executor has this bug, but update works fine.

  1. Another point is related to the fact when client secret expiration was enabled, but now administrator wants to disable it. Example scenario:
  • I have client policy enabled with the executor for client secret rotation
  • I click to some existing client and click "Credentials" and then "Re-generate secret" . The secret is rotated as expected
  • Now I go to disable client policy (or remove executor from it, or change the condition so it is not applicable anymore for my client)
  • Now I click again to my client and click "Re-generate secret" . The new secret is generated, but there is still old secret displayed (The very old secret from rotation 1). And times of client secret rotation are not updated.

IMO, the correct behaviour should be that when client secret rotation is not enabled, then all attributes related to client secret rotiation (secret expiration, old secret, old secret expiration, old secret creation) should be removed from the client during "Re-generate secret" or client update request. WDYT?

  1. There is no change in the JWTClientSecretAuthenticator, only in the ClientIdAndSecretAuthenticator. So JWTClientSecretAuthenticator currently does not have support for client secret rotation. IMO it will be good to add this, but I vote to do this in the separate PR, which will be follow-up of this PR.

@mposolda
mposolda commented Mar 7, 2022
Copy link
Copy Markdown
Contributor

@marcelomrwin One required thing for this PR is, that all the tests must pass in the GH actions. I see that there are failed tests: BrokerLinkAndTokenExchangeTest, CredentialsTest, ClientPoliciesImportExportTest, CookieTest, ComponentExportImportTest, FederatedStorageExportImportTest (I guess some of the failures might be related to the fact that imported realms don't have "client secret creation time", but not 100% sure. The case when the client secret don't have "Client secret creation time" filled must be correctly handled)

@marcelomrwin
Copy link
Copy Markdown
Contributor Author

Thank you so much for the comments Marek. We're lined up.
Here we go, point by point:

  1. I believe I'm using Intellij's standard formatter and in my IDE the formatting is automatic. Do we have a template or standardized template that can be shared for the keycloak project? I vote to keep the default but for that it would be ideal if we could share a source code formatting template for IDEs, Intellij, Eclipse, etc. Do we have anything like that?
  2. I totally agree with you. I will make use of more logs in the code to make tracking and monitoring easier.
  3. Really, I realized that I didn't add test flow for some endpoints and new flows. I will add for sure. Regarding the bug mentioned in 3A, I remember having encountered this situation only once, but I refreshed the page and in fact there was only one executor. In any case, from the analyzed code it seemed to me that it was executed before the commit. transaction, but I will investigate the create flow further and correct it if that is the case.
  4. This is a good talking point and I've already mapped it out. Even with suggestions that would be very quick to implement. In my opinion, the reason for the problem is precisely because we don't get an update from customers when we make policy changes. The client is only notified when it undergoes an update. I even created a discussion about it Trigger update clients after policy definitions. #10591.

The main question is that there is no simple way to ask: Is secret rotation active? we only have one way to know this and that is to throw one of the eve 8BD0 nts predicted in the secret rotation executor handling. But this doesn't answer us when the executor is not active. A suggestion would be:

During the client update flow or regenerate the secret or during the next login (after deactivating the policy) at the beginning of the flow include a flag on the client as an attribute for example CLIENT_SECRET_ROTATION_ENABLE = false. Inside the executor, when it was executed, edit the client and update this flag to true. At the end of the flow, if this flag was kept false, we would invoke a function in the wrapper that would remove all information related to rotation.

My concern with this flow is to know how much we are going to influence database operations. I haven't had time to test it yet, but I'm worried about generating an update in the database every time you want to login, for example. The inclusion of a new attribute in the client normally generates an update in the base, perhaps if we clear this attribute before the end of the flow, the transaction does not need to update the base. I'm not sure, but I need to test.

  1. I totally agree with the case of the JWTClientSecretAuthenticator class. It has not yet been mapped and the flow has not yet been contemplated. I'll wait for the next PR to send this fix, but apparently it doesn't seem complex to me.

I agree with your plan, I can easily attack points 1-3 within this PR and keep 4 and 5 for the next one. In my opinion, they seem to be quick to implement, but let's keep this PR with the problems already mentioned and avoid inserting new variables.

@marcelomrwin
Copy link
Copy Markdown
Contributor Author

Regarding the tests I am also quite apprehensive about the test results. It doesn't make sense to get errors because of the "Client secret creation time" attribute since it is no longer used after the proposed solution and is merely informative. there is no validation in the code or throwing an exception if it is not found. I couldn't find any exceptions that pointed out something to do with secret rotation. By the way, I pulled from main and submitted a simple push and the tests failed too. I will try to identify what could be wrong.

@marcelomrwin
marcelomrwin commented Mar 7, 2022 •
Copy link
Copy Markdown
Contributor Author

@mposolda Some notes about the tests.
For example the CredentialsTest class and its testGetAndRegenerateSecret method. At the end, they will invoke the getClientSecret method of the ClientResource class, here is the body of this method:

UserCredentialModel model = UserCredentialModel.secret(client.getSecret());
if (model == null) throw new NotFoundException("Client does not have a secret");
return ModelToRepresentation.toRepresentation(model);

this snippet model== null will never be true, because UserCredentialModel.secret(client.getSecret()) always returns an object whose secret is null, in the case of the test in question. But even so, the test works because it is not checked if the secret value is null.

public void testGetAndRegenerateSecret() {
CredentialRepresentation oldCredential = accountClient.getSecret();
CredentialRepresentation newCredential = accountClient.generateNewSecret();

When we updated the method getClientSecret and actually started throwing a 404 exception if the secret was empty or null, then some tests started to fail.

As this change can be rolled back with no impact on PR, we return the code above. We can open a new PR to correct this test, which in my opinion is validating a wrong KC behavior.

@mposolda
mposolda commented Mar 8, 2022 •
Copy link
Copy Markdown
Contributor

Regarding the tests I am also quite apprehensive about the test results. It doesn't make sense to get errors because of the "Client secret creation time" attribute since it is no longer used after the proposed solution and is merely informative. there is no validation in the code or throwing an exception if it is not found. I couldn't find any exceptions that pointed out something to do with secret rotation. By the way, I pulled from main and submitted a simple push and the tests failed too. I will try to identify what could be wrong.

Yeah. It is highly possible that it is not related to "Client secret creation time" attribute at all. This was just my guess (blind tip) that it can be related to that, but maybe I am completely wrong :-) I am leaving to you to investigate.

@mposolda
mposolda commented Mar 8, 2022
Copy link
Copy Markdown
Contributor

As this change can be rolled back with no impact on PR, we return the code above. We can open a new PR to correct this test, which in my opinion is validating a wrong KC behavior.

From my point of view, It is completely fine to either keep the behaviour you have in the PR and fix the tests. But I am leaving to you. If you think it is easier to change it to keep as a follow-up task, it is also fine for me.

I think you know this, but just a note that we cannot merge PR, which introduce some test failures. Every PR must have tests passing in the GH actions.

@mposolda mposolda left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@marcelomrwin Thanks for all the updates Marcelo, that was quick! :-)

I've added 3 minor comments inline. Besides this, the only remaining thing for this PR is to add a bit logging into the executor class. After that, this PR is good to go from my point of view.

We just need a follow-up tasks for the issues 3,4,5 from above and possibly follow-up task for the documentation (if you didn't create it already).

@mposolda
mposolda commented Mar 8, 2022
Copy link
Copy Markdown
Contributor

@marcelomrwin Thanks for all the updates Marcelo, that was quick! :-)

I've added 3 minor comments inline. Besides this, the only remaining thing for this PR is to add a bit logging into the executor class. After that, this PR is good to go from my point of view.

We just need a follow-up tasks for the issues 3,4,5 from above and possibly follow-up task for the documentation (if you didn't create it already).

I missed you mentioned that you will also tackle point 3 inside this PR. So I will wait for that and it means that we need follow-up only for points 4 and 5.

@mposolda
mposolda marked this pull request as ready for review March 8, 2022 09:21
@mposolda
mposolda commented Mar 8, 2022
Copy link
Copy Markdown
Contributor

4. This is a good talking point and I've already mapped it out. Even with suggestions that would be very quick to implement. In my opinion, the reason for the problem is precisely because we don't get an update from customers when we make policy changes. The client is only notified when it undergoes an update. I even created a discussion about it

The main question is that there is no simple way to ask: Is secret rotation active? we only have one way to know this and that is to throw one of the events predicted in the secret rotation executor handling. But this doesn't answer us when the executor is not active. A suggestion would be:

During the client update flow or regenerate the secret or during the next login (after deactivating the policy) at the beginning of the flow include a flag on the client as an attribute for example CLIENT_SECRET_ROTATION_ENABLE = false. Inside the executor, when it was executed, edit the client and update this flag to true. At the end of the flow, if this flag was kept false, we would invoke a function in the wrapper that would remove all information related to rotation.

My concern with this flow is to know how much we are going to influence database operations. I haven't had time to test it yet, but I'm worried about generating an update in the database every time you want to login, for example. The inclusion of a new attribute in the client normally generates an update in the base, perhaps if we clear this attribute before the end of the flow, the transaction does not need to update the base. I'm not sure, but I need to test.

I wonder that maybe the simplest solution is, that at the beginning of the ClientResource.regenerateSecret() you can just delete the attribute CLIENT_SECRET_EXPIRATION from the client? In case the CLIENT_SECRET_EXPIRATION is always deleted it should be fine as:

  • In case client rotation is enabled through the client policy, the executor will fill the attribute again with the up-to-date value
  • In case client rotation is not enabled, the executor won't be triggered and hence will remain deleted

Alternatively also all the attributes related to the old secret can be deleted at this point (however those are not strictly necessary to delete as administrator can do separate click to "Invalidate old secret", which will delete the old secret).

I think it is not needed to do any changes during client authentication itself. I think that client authentication should always rely on the CLIENT_SECRET_EXPIRATION attribute filled previously on the client. In case that secret is expired during client authentication, administrator will be required to manually "Re-generate secret", which will create new client secret without expiration (assuming client secret rotation is disabled after it was previously enabled).

Also I think that we don't need to do any changes in the "Client Update" .

IMO the most important thing, that when client secret rotation becomes disabled after it was previously enabled, administrators are not completely stucked and have some way to recover from this. However IMO it is completely fine if this requires manual action from them and that they need to manually "Re-generate secret" for all the clients.

@marcelomrwin
Copy link
Copy Markdown
Contributor Author

After we finish this PR I will look into this scenario to identify the best solution. It is important to point out that a manual effort by the administrator can be considered and is not all bad.

@stianst stianst modified the milestones: 18.0.0, 17.1.0 Mar 8, 2022
@marcelomrwin
Copy link
Copy Markdown
Contributor Author

I missed you mentioned that you will also tackle point 3 inside this PR. So I will wait for that and it means that we need follow-up only for points 4 and 5.

Done.

@marcelomrwin
marcelomrwin requested a review from mposolda March 8, 2022 15:36
@mposolda mposolda left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@marcelomrwin Thanks! I am approving the PR.

I hope GH actions pass, so it is possible to merge the PR.

I am not 100% sure I will be able to squash and merge it as it has lots of commits, but hopefully it is not the issue. I will ping you if there are any issues during the merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority/important Must be worked on very soon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

0