8000 KEYCLOAK-16805 Client Policy : Support New Admin REST API (Implementation) by tnorimat · Pull Request #7780 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

KEYCLOAK-16805 Client Policy : Support New Admin REST API (Implementation) - #7780

Merged
mposolda merged 6 commits into
keycloak:masterfrom
Hitachi:KEYCLOAK-16805
Apr 6, 2021
Merged

mposolda merged 6 commits into
keycloak:masterfrom
Hitachi:KEYCLOAK-16805

Conversation

@tnorimat
Copy link
Copy Markdown
Contributor

This PR is for KEYCLOAK-13933 Client Policies, also is the part of the project Client Policy Official Support of FAPI-SIG activity.

The details can be found in its design document and KEYCLOAK-16805.

@tnorimat
Copy link
Copy Markdown
Contributor Author

@mposolda This PR is about Client Policy Official Support I mentioned in FAPI-SIG meeting. Could you or your team review this PR?

@tnorimat
Copy link
Copy Markdown
Contributor Author

Scope of this PR

This PR tries to realize External Interface - Admin REST API of the design document.

However, this PR only covers part of it. The supported API by this PR are as follows.

The operations on each client profile and client policy described in the design document will be treated by other follow-up PR.

@tnorimat
Copy link
Copy Markdown
Contributor Author

Admin Console UI

The existing admin console UI for client policies will be treated by other follow-up PR relating to KEYCLOAK-14209 .

As discussed in FAPI-SIG, we only support simple JSON editor for client policies and client profiles on the existing Admin Console.

@tnorimat
Copy link
Copy Markdown
Contributor Author

Default Built-in Client Policies

This PR supports the mechanism of default read-only built-in client profiles and policies. Those are enclosed in keycloak-service's jar file.

However, the contents of these built-in client profiles/policies have not yet been determined. It will be determined and realized by other follow-up PR relating to KEYCLOAK-14208

Temporarily, this PR set the following built-in client profiles/policies.

keycloak-default-client-profiles.json :

  "profiles": [
    {
      "name": "builtin-default-profile",
      "description": "The built-in default profile for enforcing basic security level to clients.",
      "builtin": true,
      "executors": [
        {
          "secure-session-enforce-executor": {}
        }
      ]
    }
  ]

keycloak-default-client-policies.json :

{
  "policies": [
    {
      "name": "builtin-default-policy",
      "description": "The built-in default policy applied to all clients.",
      "builtin": true,
      "enable": false,
      "conditions": [
        {
          "anyclient-condition": {}
        }
      ],
      "profiles": [
        "builtin-default-profile"
      ]
    }
  ]
}

It means that keycloak rejects any client that does not use state or nonce parameter mentioned in KEYCLOAK-14206. However, this built-in client policy is disabled as default.

@tnorimat
Copy link
Copy Markdown
Contributor Author

Backward Compatibility

The current keycloak has already supported client policies. Its implementation uses ComponentModel and the keycloak administrator can control client policies via Admin REST API for general ComponentModel.

However, this PR's client policies implementation use pure JSON instead of ComponentModel and provides dedicated Admin REST API.

The existing ComponentModel based client policies can not be taken over to JSON based client policies.

@tnorimat
tnorimat commented Feb 16, 2021 •
Copy link
Copy Markdown
Contributor Author

Resource, Representation, Model, Adapter, Entity

Usually, keycloak's major concept is realized by resource, representation, model, adapter and entity. (e.g. realm, client, user)


                                                                                                            <-(getter/setter)-> Some logic process class
External component <-(HTTP Req/Res)-> Resource <-(getter/setter)-> Representation <-(getter/setter)-> Model <-(getter/setter)-> Adapter[Infinispan Invalidation Cache]
                                                                                                            <-(getter/setter)-> Adapter[JPA] <-(getter/setter)-> Entity


If we want to add a new concept to the realm, we may take the following formal way.

  • create new resource
  • modify realm resource to access newly added resource
  • create new representation
  • modify realm representation to access newly added representation
  • create new model
  • modify realm model to access newly added model
  • add new setter/getter on both realm adapters to access newly added model and entity
  • create new entity
  • modify realm entity to access newly added entity
  • add new schema if required (liquibase)
  • modify realm schema (liquibase) for referencing newly added schema

It is formal and rigid. Therefore we can take the following alternative way for using realm attribute on JPA.


                                                                                                            <-(getter/setter)-> Some logic process class
External component <-(HTTP Req/Res)-> Resource <-(getter/setter)-> Representation <-(getter/setter)-> Model <-(getter/setter)-> Adapter[Infinispan Invalidation Cache]
                                                                                                            <-(get Attribute/set Attribute)-> Adapter[JPA] <-(getter/setter-> Entity


  • create new resource
  • modify realm resource to access newly added resource
  • create new representation
  • modify realm representation to access newly added representation
  • create new model
  • modify realm model to access newly added model
  • add new setter/getter on both realm adapters
  • define key for accessing realm attribute
  • add logic on JPA realm adapter for accessing realm attribute

This way can avoid adding and modifying schemas. In order to minimize the task we need to take, we can take the following alternative way for using realm attribute on Representation/Model/Adapter/JPA.


                                                                                                                                        <-(get Attribute/set Attribute)-> Some logic process class
External component <-(HTTP Req/Res)-> Resource <-(get Attribute/set Attribute)-> Representation <-(get Attribute/set Attribute)-> Model <-(get Attribute/set Attribute)-> Adapter[Infinispan Invalidation Cache]
                                                                                                                                        <-(get Attribute/set Attribute)-> Adapter[JPA] <-(get Attribute/set Attribute)-> Entity


  • create new resource
  • modify realm resource to access newly added resource
  • define key for accessing realm attribute
  • add logic on new resource for accessing realm attribute

IMO, there is the one point that seems to be problem. For exporting/importing realm, the newly added concept (here, client policies and profiles) is set under "attributes" field and put onto String field as follows. It might be difficult to read/add/modify it in this style.

  "attributes" : {
    "client-policies.profiles" : "{\"profiles\" : [ {\"name\" : \"ordinal-test-profile\",\"description\" : \"The profile that can be loaded.\",\"builtin\" : false,\"executors\" : [ {\"secure-client-authn-executor\" : {\"client-authns\" : [ \"client-jwt\" ],\"client-authns-augment\" : \"client-jwt\",\"is-augment\" : true}} ]}, {\"name\" : \"lack-of-builtin-field-test-profile\",\"description\" : \"Without builtin field that is treated as builtin=false.\",\"builtin\" : false,\"executors\" : [ {\"secure-client-authn-executor\" : {\"client-authns\" : [ \"client-jwt\" ],\"client-authns-augment\" : \"client-jwt\",\"is-augment\" : true}}, {\"holder-of-key-enforce-executor\" : {\"is-augment\" : true}}, {\"secure-redirecturi-enforce-executor\" : { }}, {\"secure-reqobj-executor\" : { }}, {\"secure-responsetype-executor\" : { }}, {\"secure-session-enforce-executor\" : { }}, {\"securesignalg-enforce-executor\" : { }}, {\"securesignalgjwt-enforce-executor\" : { }} ]} ]}",
    "client-policies.policies" : "{\"policies\" : [ {\"name\" : \"builtin-default-policy\",\"builtin\" : true,\"enable\" : true}, {\"name\" : \"new-policy\",\"description\" : \"not existed and duplicated profiles are ignored.\",\"builtin\" : false,\"enable\" : true,\"conditions\" : [ {\"client-accesstype-condition\" : {\"type\" : [ \"public\", \"bearer-only\" ]}}, {\"clientroles-condition\" : {\"roles\" : [ \"sample-client-role\" ]}}, {\"clientscopes-condition\" : {\"type\" : \"Optional\",\"scope\" : [ \"sample-client-role\" ]}} ],\"profiles\" : [ \"builtin-default-profile\", \"ordinal-test-profile\", \"lack-of-builtin-field-test-profile\" ]}, {\"name\" : \"lack-of-builtin-field-test-policy\",\"description\" : \"Without builtin field that is treated as builtin=false.\",\"builtin\" : false,\"enable\" : false,\"conditions\" : [ {\"clientupdatecontext-condition\" : {\"update-client-source\" : [ \"ByAuthenticatedUser\" ]}}, {\"clientupdatesourcegroups-condition\" : {\"groups\" : [ \"topGroup\" ]}}, {\"clientupdatesourcehost-condition\" : {\"trusted-hosts\" : [ \"localhost\", \"127.0.0.1\" ],\"host-sending-request-must-match\" : [ true, true ]}}, {\"clientupdatesourceroles-condition\" : {\"roles\" : [ \"create-client\" ]}} ],\"profiles\" : [ \"lack-of-builtin-field-test-profile\" ]} ]}"
  },

To get around this issue, I can take the following alternative way for using realm attribute on Model/Adapter/JPA.

                                                                                                                          <-(get Attribute/set Attribute)-> Some logic process class
External component <-(HTTP Req/Res)-> Resource <-(getter/setter)-> Representation <-(get Attribute/set Attribute)-> Model <-(get Attribute/set Attribute)-> Adapter[Infinispan Invalidation Cache]
                                                                                                                          <-(get Attribute/set Attribute)-> Adapter[JPA] <-(get Attribute/set Attribute)-> Entity


  • create new resource
  • modify realm resource to access newly added resource
  • create new representation
  • modify realm representation to access newly added representation
  • define key for accessing realm attribute
  • add logic on new resource for accessing realm attribute

By doing them, the exporting/importing realm is represented as follow in the familiar style.

  "clientProfiles" : {
    "profiles" : [ {
      "name" : "ordinal-test-profile",
      "description" : "The profile that can be loaded.",
      "builtin" : false,
      "executors" : [ {
        "secure-client-authn-executor" : {
          "client-authns" : [ "client-jwt" ],
          "client-authns-augment" : "client-jwt",
          "is-augment" : true
        }
      } ]
    }, {
      "name" : "lack-of-builtin-field-test-profile",
      "description" : "Without builtin field that is treated as builtin=false.",
      "builtin" : false,
      "executors" : [ {
        "secure-client-authn-executor" : {
          "client-authns" : [ "client-jwt" ],
          "client-authns-augment" : "client-jwt",
          "is-augment" : true
        }
      }, {
        "holder-of-key-enforce-executor" : {
          "is-augment" : true
        }
      }, {
        "secure-redirecturi-enforce-executor" : { }
      }, {
        "secure-reqobj-executor" : { }
      }, {
        "secure-responsetype-executor" : { }
      }, {
        "secure-session-enforce-executor" : { }
      }, {
        "securesignalg-enforce-executor" : { }
      }, {
        "securesignalgjwt-enforce-executor" : { }
      } ]
    } ]
  },
  "clientPolicies" : {
    "policies" : [ {
      "name" : "builtin-default-policy",
      "builtin" : true,
      "enable" : true
    }, {
      "name" : "new-policy",
      "description" : "not existed and duplicated profiles are ignored.",
      "builtin" : false,
      "enable" : true,
      "conditions" : [ {
        "client-accesstype-condition" : {
          "type" : [ "public", "bearer-only" ]
        }
      }, {
        "clientroles-condition" : {
          "roles" : [ "sample-client-role" ]
        }
      }, {
        "clientscopes-condition" : {
          "type" : "Optional",
          "scope" : [ "sample-client-role" ]
        }
      } ],
      "profiles" : [ "builtin-default-profile", "ordinal-test-profile", "lack-of-builtin-field-test-profile" ]
    }, {
      "name" : "lack-of-builtin-field-test-policy",
      "description" : "Without builtin field that is treated as builtin=false.",
      "builtin" : false,
      "enable" : false,
      "conditions" : [ {
        "clientupdatecontext-condition" : {
          "update-client-source" : [ "ByAuthenticatedUser" ]
        }
      }, {
        "clientupdatesourcegroups-condition" : {
          "groups" : [ "topGroup" ]
        }
      }, {
        "clientupdatesourcehost-condition" : {
          "trusted-hosts" : [ "localhost", "127.0.0.1" ],
          "host-sending-request-must-match" : [ true, true ]
        }
      }, {
        "clientupdatesourceroles-condition" : {
          "roles" : [ "create-client" ]
        }
      } ],
      "profiles" : [ "lack-of-builtin-field-test-profile" ]
    } ]
  },

There is the other reason why I took this way. Client profiles and policies are represented as JSON string at first. It is easy for the logic processing classes to treat client profiles and policies as POJO instead of JSON string. Therefore, I introduced representation as POJO for client profiles and policies JSON string.

@tnorimat
Copy link
Copy Markdown
Contributor Author

Realm Attribute Data Size in Schema

Client profiles and policies are persisted in realm attributes as JSON string.
The current keycloak's data size for realm attribute schema is small to accomodate client profiles and policies JSON string. Therefore I enlarged its size as follows.

    <changeSet author="keycloak" id="json-string-accomodation">
        <modifyDataType tableName="REALM_ATTRIBUTE" columnName="VALUE" newDataType="NVARCHAR(65535)"/>
    </changeSet>

However, I'm not sure what is the appropriate size for realm attribute. This PR specifies 65535 bytes but I do not have confidence that 65535 bytes is appropriate.

@tnorimat
Copy link
Copy Markdown
Contributor Author

Condition/Executor Configuration

The current keycloak realizes condition/executor configuration by ComponentModel that is persisted on its dedicated schema and cached in Model layer.

This PR makes conditions/executors hold its configuration by itself. The contents of this configuration itself is persisted on realm attribute schema and cached in Model layer.

@tnorimat
Copy link
Copy Markdown
Contributor Author

Client Policies Operation

ClientPolicyManager and its utility class ClientPoliciesUtil treat all client policies related operations by handling following client policies major components.

  • ClientProfileModel : holding ClientPolicyExecutorProvider
  • ClientPolicyModel: holding ClientPolicyConditionProvider and referring ClientProfileModel

@tnorimat
tnorimat force-pushed the KEYCLOAK-16805 branch 2 times, most recently from 650deb8 to 3837e22 Compare February 16, 2021 09:02
@tnorimat
Copy link
Copy Markdown
Contributor Author

Admin REST API Access Control

As the same as other Admin REST API access, the following RBACs are imposed.

  • get client profiles : requires view-realm or manage-realm role
  • update client profiles : requires manage-realm role
  • get client policies : requires view-realm or manage-realm role
  • update client policies : requires realm-manage role

@tnorimat
Copy link
Copy Markdown
Contributor Author

@mposolda I've revised code to remove unused codes and rename ambiguous classes.

@mposolda mposolda self-assigned this Mar 1, 2021
@mposolda mposolda added area/oidc OpenID Connect / OAuth 2.0 — tokens, endpoints, scopes, DCR, PAR, DPoP kind/feature Categorizes a PR related to a new feature priority/important Must be worked on very soon status/in-review labels Mar 1, 2021
@tnorimat
tnorimat commented Mar 3, 2021
Copy link
Copy Markdown
Contributor Author

@mposolda I've rebased the master with which #7732 and #7723 have been merged.

@tnorimat
tnorimat force-pushed the KEYCLOAK-16805 branch 3 times, most recently from 682d750 to ba141d5 Compare March 5, 2021 23:57
@tnorimat
Copy link
Copy Markdown
Contributor Author

@mposolda
resolved the conflict and support tests by auth-server-quarkus. However, I'm not sure this modification is appropriate or not on QuarkusKeycloakApplication. Could you please check it afterwards?

@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.

@tnorimat Thanks a lot for the PR! And sorry for the delay in the review.

I've added some comments inline. Few points:

  • I've did some changes directly on this PR and introduced my branch with some changes https://github.com/mposolda/keycloak/commits/KEYCLOAK-16805-client-policies-db-change . See the comments inline for the details.
  • In general, I suggest to throw the errors in case of REST updates with the invalid JSON (or referencing name of invalid executor providers or invalid configuration options etc) instead of just print the WARNING. Added some comments inline regarding this, but in this PR, there are more such places (not just those, which I've referenced)
  • We may need to update old admin console due the fact that new admin console probably won't be ready for RHSSO 7.5. However I suggest to do that as a follow-up PR to not block this PR anymore. Just a note to let you know.

@mposolda
mposolda commented Mar 31, 2021 •
Copy link
Copy Markdown
Contributor

@tnorimat Do you have the time to work on this in next days/weeks? I suppose it can be nice to have this PR sorted ASAP to have it available in Keycloak 13, so we are able to align the OIDC conformance testsuite inside the FAPI repo https://github.com/keycloak/kc-sig-fapi with the new format of client policies for Keycloak 13 once it is released. WDYT?

I will be out from tomorrow (Thursday Apirl 1), but will return on Tuesday 6th April. I should have more time to help on the FAPI more actively from now on. If you don't need the help with this, perhaps I can maybe look at the admin console changes for new format of client policies?

@tnorimat
tnorimat commented Apr 1, 2021 •
Copy link
Copy Markdown
Contributor Author

@mposolda Yes, it might be preferable for this PR to be in high priority in order to catch keycloak 13. I've already resolved almost all points you've commented except for quarkus related points. I will push additional commits that resolves all point's you've commented before you return. Hopefully you could review them ASAP after you return.

And as you've pointed out, we need to revise the FAPI conformance test settings after this PR being merged. I've already cleared the new Admin REST API version's client policies settings for FAPI conformance test. I'll revise FAPI-SIG's repository to do so after this PR is merged.

For the existing Admin Console UI support for client policies, I will also try to do it. However, if you have spare time to do it, it is happy for me if you would work on it. IMO, all we need to do it add JSON editor to admin console for each entire profiles and policies.

@tnorimat
tnorimat commented Apr 4, 2021
Copy link
Copy Markdown
Contributor Author

@mposolda I've resolved all points of your review comments we've discussed. Could you please check them?

@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.

@tnorimat Thanks for all your work on this!

+1 for update the FAPI repository with the new client policies. But I guess that we will probably need to wait until Keycloak 13 release until this is available in the released version of Keycloak, before we update the https://github.com/keycloak/kc-sig-fapi repository?

Regarding the admin console UI, I have some work for this week, but hopefully should be able to start on it on Thursday this week. Maybe we can sync on Thursday whoever from us can look into it based on who has time for it?

@mposolda
mposolda merged commit 42dec08 into keycloak:master Apr 6, 2021
@tnorimat
tnorimat commented Apr 6, 2021 •
Copy link
Copy Markdown
Contributor Author

@mposolda Yes, we will discuss it on Thursday or later. If you do not have spare time to do it, I could work on supporting existing Admin Console UI.

The following FAPI 1.0 advanced final version support related JIRA tickets might be started due to this PR being merged.

I will start working on them.

For the conformance test run environment, as you stated, we will wait for the keycloak 13 release. After that, I will update this test run environment. I've already run the FAPI conformance tests against the latest keycloak locally built incorporating this PR and found that it could pass all tests.

@tnorimat
tnorimat deleted the KEYCLOAK-16805 branch April 7, 2021 00:12
@mposolda
mposolda commented Apr 7, 2021
Copy link
Copy Markdown
Contributor

@tnorimat Cool, I've started some work on the old admin console. I hope to share something soon for the feedback.

SergiusAC pushed a commit to SergiusAC/keycloak that referenced this pull request May 20, 2021
…tion) (keycloak#7780)

* KEYCLOAK-16805 Client Policy : Support New Admin REST API (Implementation)

* support tests using auth-server-quarkus

* Configuration changes for ClientPolicyExecutorProvider

* Change VALUE of table REALM_ATTRIBUTES to NCLOB

* add author tag

* incorporate all review comments

Co-authored-by: mposolda <mposolda@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/oidc OpenID Connect / OAuth 2.0 — tokens, endpoints, scopes, DCR, PAR, DPoP kind/feature Categorizes a PR related to a new feature priority/important Must be worked on very soon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

0