8000 Operator logic for clients in admin api v2 by shawkins · Pull Request #45316 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Operator logic for clients in admin api v2 - #45316

Merged
vmuzikar merged 12 commits into
keycloak:mainfrom
shawkins:iss43168
Feb 5, 2026
Merged

vmuzikar merged 12 commits into
keycloak:mainfrom
shawkins:iss43168

Conversation

@shawkins
@shawkins shawkins commented Jan 9, 2026 •
Copy link
Copy Markdown
Contributor

Closes #46022

  • Creates base controller logic that is really just performing puts or deletes.

    • Delete handling is enforced via a finalizer
    • These operations are in response to CR changes only - the state of the server side client is not actively monitored.
    • It is not clear yet how the notion of ownership / control factors in here. Since we always are just performing a PUT we aren't differentiating the case of whether the client already exists - if it already exists, are we somehow asserting ownership of it?
  • Differences between the CRD and admin api resources are handled via subclasses that add targeted JsonIgnores and new fields. There may be a way to do this Jackson views, but none of the serialization logic is view aware.

    • The OIDC Auth.secret field is not present in the CRD. Instead there is a "secretRef" field. The naming can of course change - e.g. secretSecret?
  • Once set, the CRD enforces that you cannot change the Keycloak instance nor the realm name in the client CRs because these should effectively trigger cleanups, which we're not guaranteed to get the event for.

  • Assumes that a "keycloak cr name"-admin Secret will contain a clientId and clientSecret. This is just a placeholder - it is likely that it will get refined, such as the operator invoking bootstrap admin to create the necessary service account.

  • Puts only a minimal amount of information in the status. If the returned representation (because of defaulting or normalizations) is of interest, then we'll need to add a sanitized version of that to the status.

  • Getting the admin client is hacked in.

    • I'm temporarily using reflection to get at the client WebTarget, then use that along with our rest interfaces to make the remote calls.
    • There's no optimization yet for pooling the client
    • https trust has been added, and as discussed below - hostname verification is disabled, rather than introducing hostname overrides and additional logic.
    • mTLS handling is TDB - we can add an new admin.tlsSecret or even http.adminTlsSecret (if we want it close to the existing http tlsSecret).
  • No attempt has been made to separate the client crds from the existing ones.

  • We are always attempting to use the service hostname and http if available. This may not always be desirable. A user has even requested targeting arbitrary Keycloak instances: Support creating a client CR in cluster where keycloak is not deployed #44613

  • Do we need a validation that the client-admin-api:v2 feature is enabled?

    • How does this relate to resource ownership - if I create some client CRs, then toggle client-admin-api:v2 from enabled to disabled, does that effectively lock the state of those clients on the server-side and prevent the deletion of the CRs?

Let me know your early thoughts.

@shawkins
shawkins force-pushed the iss43168 branch 4 times, most recently from 24fc40f to c6df9af Compare January 14, 2026 16:21
@shawkins
shawkins commented Jan 14, 2026 •
Copy link
Copy Markdown
Contributor Author

Had to tweak things further to get the CRD definition as expected.

@keycloak/cloud-native just wanted to check more on how we want to access the servers. http/edge scenarios are not a problem, so my initial thinking is that we'd go directly to the service endpoints.

For pass-through that's a problem as we'll likely fail any hostname validation checks.
For reencrypt there is probably a path where service serving certficiates could be used.

Also this approach doesn't work for testing in local mode as the operator is not running in cluster - I've hacked in a workaround with access via a NodePort.

I can either:

  • relax hostname checks
  • instead try to determine the appropriate hostname or hostname-admin. However assuming that the user has specified hostname-admin to restrict ingress, they will need to ensure that access via the operator is allowed.

@shawkins
shawkins force-pushed the iss43168 branch 3 times, most recently from 70efeb1 to 27c62df Compare January 14, 2026 22:08
@vmuzikar
Copy link
Copy Markdown
Contributor

I can either:

  • relax hostname checks
  • instead try to determine the appropriate hostname or hostname-admin. However assuming that the user has specified hostname-admin to restrict ingress, they will need to ensure that access via the operator is allowed.

I'm not sure about this. Relaxing hostname checks feels kinda insecure... for a secure product like an IAM solution. Using hostname-admin could lead to some unexpected routing issues as that's supposed to be a public host. Still, feels like a more proper solution, a lesser evil.

@shawkins
shawkins commented Jan 15, 2026 •
Copy link
Copy Markdown
Contributor Author

I'm not sure about this. Relaxing hostname checks feels kinda insecure... for a secure product like an IAM solution.

I realize that it's not great, but I was wondering if this situation already comes up - if someone configures keycloak for tls passthrough, and does not enable http, does that mean dynamic back channel requests:

  • aren't really a supported case because your using tls either way, so you might as well go through the public hostname
  • require the cert to expose some internal host
  • must disable hostname verification

Using hostname-admin could lead to some unexpected routing issues as that's supposed to be a public host. Still, feels like a more proper solution, a lesser evil.

I'd like to say this only needs done in the passthrough case - but we can't reliably differentiate between passthrough and reencrypt. So I think the logic would need to be that if tls is configured, but http is not enabled, then we'll have to access via hostname / hostname-admin.

Thinking about it more and looking at customizability of the client, I think we should be able to still access internally, but validate against the expected external hostname - although it would be better to cleanly know when we in a passthrough vs a reencrypt scenario.

@vmuzikar
vmuzikar commented Jan 16, 2026 •
Copy link
Copy Markdown
Contributor

if someone configures keycloak for tls passthrough, and does not enable http, does that mean dynamic back channel requests

That's a good point – that the Operator is in the same position as any app doing a backchannel request through the internal network.

Thinking about it more, I believe we'll need to document that passthrough TLS (without HTTP enabled) is generally incompatible with the backchannel requests. Exposing internal hostnames in the cert might be considered a security vulnerability, IMHO. Similarly to relaxing hostname checks.

For now, let's make the Operator use hostname/hostname-admin. If we ever see issues with that, we can add some explicit setting for telling the Operator whether passthrough or reencrypt is used, and based on that we'll decide whether to use internal host.

@shawkins
Copy link
Copy Markdown
Contributor Author

@vmuzikar what do you think of the approach in the last commit of validating against the hostname admin and the service hostname?

The biggest concern I can think of is whether the reencrypt hostname will actually be the service hostname.

Another corner case - if you setup the instance with hostname-strict=false and don't enable http. I realize this is generally not something we recommend or support, but in passthrough mode, we may not have any idea what hostname to use.

@shawkins
Copy link
Copy Markdown
Contributor Author

Thinking about it more, I believe we'll need to document that passthrough TLS (without HTTP enabled) is generally incompatible with the backchannel requests.

Captured as #45606

@shawkins
shawkins commented Jan 20, 2026 •
Copy link
Copy Markdown
Contributor Author

@keycloak/cloud-native I know we talked about this extensively in our meeting, but after more reflection I believe we should simply disable hostname verification.

Since we are only trusting only the server certificate, which we could even enforce that it's not allowed to be a ca certificate, the tlsSecret would have to be compromised for any kind of attack.

@shawkins
shawkins force-pushed the iss43168 branch 6 times, most recently from b867772 to 119c692 Compare January 23, 2026 14:57
@vmuzikar vmuzikar 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.

@shawkins I am sorry for the late response. I went through the changes and thought about it more as well. IMHO the approach is good as is.

No attempt has been made to separate the client crds from the existing ones.

Do you mean CRDs from the old Operator? I think it's enough the API group and version differ.

We are always attempting to use the service hostname and http if available. This may not always be desirable.

I think that's ok. If we keep the control of the address on the Operator which always uses an internal hostname (without the user option to override it), it should be safe to also disable the hostname validation.

Do we need a validation that the client-admin-api:v2 feature is enabled?

Yes, we should. The controller should be noop when this feature is disabled.

How does this relate to resource ownership - if I create some client CRs, then toggle client-admin-api:v2 from enabled to disabled, does that effectively lock the state of those clients on the server-side and prevent the deletion of the CRs?

We don't have resource ownership just not. Later I reckon any resource can be managed only by its owner, or a master realm admin. But that's for a later discussion.


The follow-ups then would be:

  • mTLS support
  • Usage of proper Admin Client (#45364)
  • Admin Client pooling
  • Other auth mechanism (K8s service accounts)

// TODO allows for local testing - could be promoted in some way to the CR if needed
public static final String KEYCLOAK_TEST_ADDRESS = "KEYCLOAK_TEST_ADDRESS";

static class KeycloakClientStatusAggregator {
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.

It would be good to have this as a separate class similarly to the KeycloakStatusAggregator.

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.

The comment above is still valid but it's not blocking.

Comment on lines +19 to +29
private SecretKeySelector secretRef;

@JsonPropertyDescription("Secret containing the client secret")
public SecretKeySelector getSecretRef() {
return secretRef;
}

public void setSecretRef(SecretKeySelector secretRef) {
this.secretRef = secretRef;
}

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.

Thinking about this more, I'm afraid we can't do that. The Keycloak admin and Client admin are two different personas. This would allow a Client admin to read any Secret in the namespace, incl. e.g. the DB Secret, or admin bootstrap Secret.

I wonder if we should wait for #13102 to rather fully leverage Vault with security sensitive values. On the other hand, we could potentially end up in a similar situation – Client admin reading any secret from the Vault.

We'll probably need to lock this down. Maybe annotate Secrets with the CR name that they can be referenced in?

CC @stianst

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.

Potential solution would be if we allowed referencing KC instances in other namespaces, so that we'd allow referencing Secrets only in the same namespace.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The Keycloak admin and Client admin are two different personas. This would allow a Client admin to read any Secret in the namespace, incl. e.g. the DB Secret, or admin bootstrap Secret.

They are different personas, but is that needing the be inforced a the Kubernetes level? Aren't we mostly targetting gitops flow such that there would be controls already on who can modify the client and other admin api style resources?

If you want this to be enforced a the Kubernetes level, then we'll need to probably think of this like a separate operator, which functions more like what is being requested in #44613 - but we need to then have some other notion of how to find the Keycloak cluster other than a reference to the CR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

As discussed off-line there are two conserns here. A Client admin persona directly administering clients in the Keycloak namespace being given read permission to secrets in that namespace, and the Client admin persona being able to reference and exfiltrate Secrets they shouldn't have access to.

The proper solution to both is to introduce the ability to use separate namespaces per Client admin. Since we are reluctant to let the operator install in cluster scoped mode, this will lead to an even greater proliforation of operator instances - unless we consider splitting this into a separate operator.

For now that seems to be large shift in approach, so we'll continue how this PR currently is. At worst case this will mean initially documenting that those with the ability to create / edit Client CRs must also be namespace level admins.

@shawkins
Copy link
Copy Markdown
Contributor Author

@shawkins I am sorry for the late response. I went through the changes and thought about it more as well. IMHO the approach is good as is.

No attempt has been made to separate the client crds from the existing ones.

Do you mean CRDs from the old Operator? I think it's enough the API group and version differ.

No, the existing CRDs from the new operator. Alternatively we can come back to this if we get much closer to a release and don't feel good about the stability or usability of the new CRDs.

Yes, we should. The controller should be noop when this feature is disabled.

This relates to some of long-standing configuration related issues. Validating build-time options will generally mean that users will need to use unoptimized images, or to replicate the necessary build-time options on the CR.

I'm generally leary of the complexity that features per api and version are bringing into this.

How does this relate to resource ownership - if I create some client CRs, then toggle client-admin-api:v2 from enabled to disabled, does that effectively lock the state of those clients on the server-side and prevent the deletion of the CRs?

We don't have resource ownership just not. Later I reckon any resource can be managed only by its owner, or a master realm admin. But that's for a later discussion.

Until that is implemented, we'd have to document that the operator can assume ownership of existing clients, and that client modifications not done by the operator will be undone - but only based upon CR changes.

The follow-ups then would be:

  • mTLS support

This can be done in this PR.

  • Additional status information

@shawkins
Copy link
Copy Markdown
Contributor Author

I'm capturing the follow-ups to this PR under the MVP issue. The admin client pooling isn't necessary for the MVP, so it will be created as a stand-alone issue for later.

@shawkins
shawkins force-pushed the iss43168 branch 2 times, most recently from 7e7c952 to 649ae48 Compare January 28, 2026 12:31
vmuzikar
vmuzikar previously approved these changes Feb 4, 2026
@vmuzikar vmuzikar 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.

@shawkins Sorry for the late review.

Added a few comments but nothing blocking. Approving, leaving up to you if you want to merge it and address those in follow-ups.

Also, created and linked #46022.

// TODO allows for local testing - could be promoted in some way to the CR if needed
public static final String KEYCLOAK_TEST_ADDRESS = "KEYCLOAK_TEST_ADDRESS";

static class KeycloakClientStatusAggregator {
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.

The comment above is still valid but it's not blocking.

Config config;

@Override
public UpdateControl<R> reconcile(R resource, Context<R> context) throws Exception {
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.

I'll capture this as a follow-up as it will need documentation as well.

jFTR – this is tracked under #45810.

Related to this is the consideration of checking the server state vs the CR state. You can set the feature in the CR and that not actually represent the state of the server for a while (in particular if some other invalid change is paired with it).

I'm fine with chalking this up to an eventual consistency issue and just letting the operator fail at the admin operation until the cluster is up-to-date. There is also no check currently of whether the server is in a ready state in general - we'll simply try the admin operation, record the failure in the status, and try again in 10 seconds.

That is fine. More concerning is that we ignore the actual config that might be baked in a custom image, but that's a broader issue and not specific to Client v2 and features.

Comment on lines +174 to +176
@JsonProperty("admin")
@JsonPropertyDescription("In this section you can find all properties related to making admin connections from the operator to the server. These settings are not used by the server.")
private AdminSpec adminSpec;
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.

Thinking about this more, I wonder if we're not jumping the gun here with introducing the adminSpec. We might later introduce a dedicated mTLS field that would also configure the server. But I reckon ithe adminSpec will have other uses too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thinking about this more, I wonder if we're not jumping the gun here with introducing the adminSpec.

We can use an annotation instead of spec if we're having qualms about this.

We might later introduce a dedicated mTLS field that would also configure the server.

I'm not sure what you mean - why whould you need to include a client private cert with the server configuration in general?

We might later introduce a dedicated mTLS field that would also configure the server.

Honestly I'm not sure if it will have other uses - or if we'd want to go the other direction given the secret discussion and introduce a whole other CR that configures Keycloak instance we're administering.

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.

We can use an annotation instead of spec if we're having qualms about this.

You mean annotation directly on the CR? That would feel a bit weird and inconsistent.

I'm not sure what you mean - why whould you need to include a client private cert with the server configuration in general?

Sorry, right, server needs only truststore, that's different config from what admin client needs.

Honestly I'm not sure if it will have other uses

Auth method might be one of the additional uses.

or if we'd want to go the other direction given the secret discussion and introduce a whole other CR that configures Keycloak instance we're administering.

Thinking about it further, I don't think we should go this direction. Except for auth credentials (for different tenants), I don't think there would be differences in admin client settings for a single KC instance.

@shawkins
shawkins marked this pull request as ready for review February 4, 2026 21:25
@shawkins
shawkins requested review from a team as code owners February 4, 2026 21:25
shawkins and others added 12 commits February 5, 2026 09:17
closes: keycloak#43168

Signed-off-by: Steve Hawkins <shawkins@redhat.com>
Signed-off-by: Steve Hawkins <shawkins@redhat.com>
Signed-off-by: Steve Hawkins <shawkins@redhat.com>
also starting to add tests

local testing will need alternative logic for getting the admin url

Signed-off-by: Steve Hawkins <shawkins@redhat.com>
Signed-off-by: Steve Hawkins <shawkins@redhat.com>
Signed-off-by: Steve Hawkins <shawkins@redhat.com>
Signed-off-by: Steve Hawkins <shawkins@redhat.com>
Signed-off-by: Steve Hawkins <shawkins@redhat.com>
Signed-off-by: Steve Hawkins <shawkins@redhat.com>
Signed-off-by: Steve Hawkins <shawkins@redhat.com>
< 8 div clas 10BC8 s="AvatarStack flex-self-start AvatarStack--two " >
Signed-off-by: Steve Hawkins <shawkins@redhat.com>
…oakClientBaseController.java

Co-authored-by: Václav Muzikář <vaclav@muzikari.cz>
Signed-off-by: Steven Hawkins <shawkins@redhat.com>
@vmuzikar
vmuzikar enabled auto-merge (squash) February 5, 2026 08:19
@mabartos
mabartos commented Feb 5, 2026
Copy link
Copy Markdown
Member

Just information from my end - I built it locally, and it looks very good. I think the missing parts were already mentioned here - like there should be improvements in the status area for sure. Then the Client CR name is used as the clientId of the client (which is nice), but would it be possible to warn users that changing the name of the CR does not do anything?

But overall, very good job!

@vmuzikar
vmuzikar merged commit 8a471bb into keycloak:main Feb 5, 2026
87 checks passed
sashyo pushed a commit to tide-foundation/keycloak-IGA that referenced this pull request Jul 20, 2026
Operator logic for clients in admin api v2

Closes keycloak#46022

Signed-off-by: Steven Hawkins <shawkins@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Initial Client CR implementation Consider having the explicit discriminator field in Client v2 representations

3 participants

0