Create a realm.
Create a clients like jenkins,sonar…
Here is the list of clients

Here is the screenshots how to set jenkins as client.



List of client scopes for jenkins.One can add some specific client scope in addition to the ones which are added from the realm.

Here is the list of client scopes available to the clients in the realm.

Group-membership client scope should be added as:



Groups client scope should be added as:



Realm roles are created for clients which support it (e.g. jenkins).

Some example groups are added as admins,devs,qas.Realm roles are assigned to the related groups.





On keycloak create realm and client as:
{
"id" : "af27f3af-ea50-452d-bc56-d28069cdbf29",
"clientId" : "jenkins",
"rootUrl" : "http://jenkins.computingforgeeks.com:8080/",
"adminUrl" : "http://jenkins.computingforgeeks.com:8080/",
"surrogateAuthRequired" : false,
"enabled" : true,
"alwaysDisplayInConsole" : false,
"clientAuthenticatorType" : "client-secret",
"redirectUris" : [ "http://jenkins.computingforgeeks.com:8080/*" ],
"webOrigins" : [ "http://jenkins.computingforgeeks.com:8080/" ],
"notBefore" : 0,
"bearerOnly" : false,
"consentRequired" : false,
"standardFlowEnabled" : true,
"implicitFlowEnabled" : false,
"directAccessGrantsEnabled" : true,
"serviceAccountsEnabled" : false,
"publicClient" : true,
"frontchannelLogout" : false,
"protocol" : "openid-connect",
"attributes" : {
"saml.assertion.signature" : "false",
"saml.force.post.binding" : "false",
"saml.multivalued.roles" : "false",
"saml.encrypt" : "false",
"saml.server.signature" : "false",
"saml.server.signature.keyinfo.ext" : "false",
"exclude.session.state.from.auth.response" : "false",
"saml_force_name_id_format" : "false",
"saml.client.signature" : "false",
"tls.client.certificate.bound.access.tokens" : "false",
"saml.authnstatement" : "false",
"display.on.consent.screen" : "false",
"saml.onetimeuse.condition" : "false"
},
"authenticationFlowBindingOverrides" : { },
"fullScopeAllowed" : true,
"nodeReRegistrationTimeout" : -1,
"defaultClientScopes" : [ "web-origins", "role_list", "profile", "roles", "email" ],
"optionalClientScopes" : [ "address", "phone", "offline_access", "microprofile-jwt" ],
"access" : {
"view" : true,
"configure" : true,
"manage" : true
}
In client “jenkins” select tab “Installation” - format “Keycloak OIDC JSON” - copy to clipboard.
[Optional] Create roles jenkins_admins/jenkins_readonly.If ldap is used those roles can be pulled from ldap and thus one doesn’t need to create them.
Install java and setup jenkins.
yum install java-1.8.0-openjdk-devel -y
curl --silent --location http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo | sudo tee /etc/yum.repos.d/jenkins.repo
rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
yum install jenkins mc vim -y
systemctl start jenkins
systemctl enable jenkins
docker run -d -p 8080:8080 -p 50000:50000 jenkins/jenkins:lts
Install following plugins on jenkins:
OpenId Connect Authentication Plugin - it will be used for authentication to keycloakRole-based Authorization Strategy - it will be used for role based auth with keycloakGoto Manage Jenkins->Manage and Assign Roles->Manage Roles->Global Roles - (if not present) add role “admin” with all checkboxes selected - SAVE. Goto Manage Jenkins->Manage and Assign Roles->Manage Roles->Global Roles - add role “read_only” with “Overall Read” selected - SAVE
Goto Manage Jenkins->Manage and Assign Roles->Assign Roles - add group “jenkins_admin” to global roles and select “admin” - SAVE Goto Manage Jenkins->Manage and Assign Roles->Assign Roles - add group “jenkins_readonly” to global roles and select “read_only” - SAVE
Goto “Configure Global Security” and configure Security Realm following howto on https://plugins.jenkins.io/oic-auth/ and under Jenkins/Keycloak integration.
Goto “Configure Global Security” and select “Authorization”: “Role-Based Strategy” - SAVE
You should be redirected now to the Keycloak login. Try to log in as admin with admin rights, and as named user with read only rights.
https://stackoverflow.com/questions/56042425/how-to-set-role-based-login-for-jenkins-in-keycloak
https://joostvdg.github.io/blogs/kubernetes-sso-keycloak/#sso-with-jenkins
https://www.janua.fr/mapping-ldap-group-and-roles-to-redhat-sso-keycloak/