Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: tvallin <thibault.vallin@oracle.com>
  • Loading branch information
tvallin committed Jun 6, 2024
1 parent 2ff288b commit 155a8f2
Show file tree
Hide file tree
Showing 17 changed files with 285 additions and 141 deletions.
100 changes: 54 additions & 46 deletions docs/src/main/asciidoc/mp/guides/security-oidc.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ include::{rootdir}/includes/mp.adoc[]
This guide describes how to set up Keycloak and Helidon
to secure an application with OIDC security provider.
* <<Introduction, Introduction>>
* <<Keycloak-Installation, Keycloak Installation>>
* <<Setup-Keycloak, Setup Keycloak>>
* <<Setup-Helidon, Setup Helidon>>
* <<Test-Keycloak-process-with-Postman, Test Keycloak process with Postman>>
* <<Restrict-access-to-a-specific-role, Restrict access to a specific role>>
== What You Need
For this 20 minute tutorial, you will need the following:
Expand All @@ -41,7 +48,7 @@ This guide describes the steps required to protect your whole application or a s
OIDC is a secure mechanism for an application to contact an identity service.
It's built on top of OAuth 2.0 and provides full-fledged authentication and authorization protocols.
== Install Keycloak
== Keycloak Installation [[Keycloak-Installation]]
=== On Docker
Expand All @@ -50,15 +57,15 @@ To install Keycloak with Docker, open a terminal and make sure the port 8080 is
[source,bash]
.Enter the following command
----
docker run -p 8080:8080 -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin quay.io/keycloak/keycloak:11.0.2
docker run -p 8080:8080 -e KEYCLOAK_ADMIN=admin -e KEYCLOAK_ADMIN_PASSWORD=admin quay.io/keycloak/keycloak:24.0.5 start-dev
----
This will start Keycloak on local port 8080. It will create the admin user with username `admin` and password `admin`
Feel free to modify 11.0.2 by any keycloak version of your wish.
Feel free to modify 24.0.5 by another keycloak version.
If you are running docker behind a proxy server, make sure it is either configured into docker or
disabled. Otherwise, you might face a connection timeout because docker cannot download the required data.
To verify that Keycloak is running correctly, go to the admin console : http://localhost:8080/auth/admin
To verify that Keycloak is running correctly, go to the admin console : http://localhost:8080/admin
Log in using the username and password mentioned above: `admin`.
You should be logged in successfully, and it prompts the admin console.
Expand All @@ -70,52 +77,49 @@ In the table Server choose Standalone server distribution. ZIP or Tar format are
to download Keycloak.
After extracting the archive file, you should have a directory named keycloak followed by the version. For example,
if you chose version 11.0.2, the folder must be named keycloak-11.0.2.
if you chose version 24.0.5, the folder must be named keycloak-24.0.5.
Open keycloak folder to make it your current directory.
[source,bash]
.Run this command from command prompt to open the directory:
----
cd keycloak-11.0.2
cd keycloak-24.0.5
----
== Start Keycloak
==== Start Keycloak
To start keycloak and have it ready for further steps, run the following command.
[source,bash]
.On Linux run:
----
bin/standalone.sh
bin/kc.sh start-dev
----
[source,bash]
.On Windows run:
----
bin/standalone.bat
bin\kc.bat start-dev
----
Keycloak runs on localhost:8080 by default.
=== Create an Admin User
You need to create an admin user because it does not come by default when installing Keycloak.
To do this, open http://localhost:8080/auth in your favorite browser.
A window `Welcome to Keycloak` should be prompted. If not, check if any error appear in the terminal.
Fill the form by adding Username and Password. Click on `Create` to create the admin user.
To do this, open http://localhost:8080 in your favorite browser.
Above Administration Console should be printed "User created" in a green rectangle.
A window `Create an administrative user` should be prompted. If not, check if any error appear in the terminal.
To check that the admin user was created correctly, click on Administration user which should redirect you
to a Login form. Enter the Username and Password created earlier to log in.
Fill the form by adding Username and Password. Click on `Create user` to create the admin user. A confirmation message
is displayed and an administrative user is created. Press `Open Administration Console` and use the same credentials
to log in.
After successfully logged in, the admin console is prompted.
== Set up Keycloak
== Setup Keycloak [[Setup-Keycloak]]
To set up Keycloak properly, go to the admin console: http://localhost:8080/auth/admin
To set up Keycloak properly, go to the admin console: http://localhost:8080/admin
If you are using Docker, use Username `admin` and password `admin` as it is the default admin user.
Otherwise, use the username and password you used to create the admin user.
Expand All @@ -135,13 +139,13 @@ application with this realm as it could disturb Keycloak functioning.
To create a new realm to manage your application:
. Open Keycloak admin console http://localhost:8080/auth/admin.
. Hover the mouse over the dropdown in the top-left corner where it says `Master`, and press `Add realm`.
. Open Keycloak admin console http://localhost:8080/admin.
. Hover the mouse over the dropdown in the top-left corner where it says `Keycloack`, and press `Create realm`.
. Fill the form by adding the realm name, `myRealm` for example.
. Click on `Create` to create the new realm.
To verify that your realm is created, in the top-left corner where it said `Master` previously
should be now your realm name or `myRealm` is you followed the example.
To verify that your realm is created, you should see your realm name (or `myRealm` if you followed the example) in the top-left
corner where it said `Keycloack` previously.
To switch from a realm to another, hover the realm name, and the other realm created appear in the dropdown.
Click on any realm name to change the current realm. Make sure all configuration or modification are saved before changing
Expand All @@ -154,50 +158,54 @@ A realm contains resources such as client which can be accessed by users.
To create a new user:
. Open the Keycloak admin console: http://localhost:8080/auth/admin
. Open the Keycloak admin console: http://localhost:8080/admin
. Click on `Users` in the left menu
. Press `Add user`
. Press `Create new user`
. Fill the form (Username is the only mandatory field) with this value Username: `myUser`
. Click `Save`
. Click `Create`
A new user is just created, but it needs a password to be able to log in. To initialize it, do this:
. Click on `Credentials` at the top of the page, under `Myuser`.
. Click on `Credentials` at the top of the page, next to `Details`.
. Press on `Set Password`.
. Fill `Password` and `Password confirmation` with the user password of your choice.
. If the `Temporary` field is set to `ON`, the user has to update password on next login. Click `ON`
to make it `OFF` and prevent it.
. Press `Set Password`.
. Press `Save`.
. A pop-up window is popping off. Click on `Set Password` to confirm the new password.
To verify that the new user is created correctly:
. Open the Keycloak account console: http://localhost:8080/auth/realms/myRealm/account.
. Open the Keycloak account console: `http://localhost:8080/realms/myRealm/account`.
. Login with `myUser` and password chosen earlier.
. Fill the form with required data.
You should now be logged-in to the account console where users can manage their accounts.
=== Create a Client
To create your first client:
. Open the Keycloak admin console: http://localhost:8080/auth/admin.
. Open the Keycloak admin console: http://localhost:8080/admin.
. Make sure the current realm is `myRealm` and not `Master`.
. Navigate to the left menu, into configure section, click on `Clients`. This window displays a table with every client
from the realm.
. Click on `Create`.
. Click on `Create client`.
. Fill the following:
.. `Client ID` : `myClientID`
.. `Client Protocol` : `openid-connect`
. Press `Save`
.. Modify `Access type` : `confidential`
.. `Client Protocol` : `OpenID Connect`
. Press `Next`
. `Capability config` step
.. Enable `Client authentication`
.. Enable `Authorization`
. Press `Next`
.. Update `Valid Redirect URIs` : http://localhost:7987/*
.. Click on `+` to add the new URI.
. Click on `Save`.
A new tab named `Credentials` is created. Click on it to access this new tab.
- Select `Client Authenticator` : `Client ID and Secret`
- Click on `generate secret` to generate client secret.
- The client secret is displayed.
Keycloak is now configured and ready. Keep keycloak running on your terminal and open a new tab to
set up Helidon.
Expand Down Expand Up @@ -258,7 +266,7 @@ security:
client-id: "myClientID" # <1>
header-use: true
client-secret: "Client secret generated into Keycloak client credential" # <2>
identity-uri: "http://localhost:8080/auth/realms/myRealm" # <3>
identity-uri: "http://localhost:8080/realms/myRealm" # <3>
frontend-uri: "http://localhost:7987" # <4>
----
<1> `client-id` must be the same as the one configure in keycloak.
Expand Down Expand Up @@ -317,7 +325,7 @@ Helidon and Keycloak are now correctly configured and your application is safe.
[source,bash]
.Build the application, skipping unit tests, then run it:
----
mvn package -DskipTests=true
mvn package -DskipTests
java -jar target/helidon-quickstart-mp.jar
----
Expand Down Expand Up @@ -354,7 +362,7 @@ In the test folder `helidon-quickstart-mp/src/test`:
----
mkdir resources
cd resources
touch application.yaml
touch application-test.yaml
----
Open the application.yaml file you just created.
Expand All @@ -375,10 +383,10 @@ security:
- http-basic-auth:
users:
- login: "jack"
password: "jackIsGreat"
password: "changeit"
----
By adding this new application.yaml, it will append the properties to the application.yaml located into `java/resources`.
By adding this new application.yaml, it will append the properties to the application.yaml located into `main/resources`.
The oidc properties are not overridden, and the server cannot decide which security provider to choose.
Excluding oidc dependency during the test leaves only basic authentication security available for the tests.
Expand Down Expand Up @@ -465,15 +473,15 @@ username and password for authentication.
3. The authorization code is used to get access and refresh token from Keycloak token endpoint.
For the first step, paste the following URL into your browser:
`http://localhost:8080/auth/realms/myRealm/protocol/openid-connect/auth?client_id=myClientID&response_type=code`.
`http://localhost:8080/realms/myRealm/protocol/openid-connect/auth?client_id=myClientID&response_type=code`.
The first part of the url `http:/../auth` is the Keycloak endpoint to request an authorization code. Two query
parameters are provided, the client id and the response type.
Press enter and Keycloak responds with different URL containing a query parameter `code`. You successfully received
the authorization code.
In order to achieve the third step, we can use Postman to exchange the authorization code for tokens. In Postman,
select the Http POST method. Keycloak endpoint to get token is the following:
`http://localhost:8080/auth/realms/myRealm/protocol/openid-connect/token`.
`http://localhost:8080/realms/myRealm/protocol/openid-connect/token`.
In the body of the request, select `x-www-form-urlencoded` type. Add the following data:
[source,json]
Expand Down Expand Up @@ -504,7 +512,7 @@ sentence `Add authorization data to` with `Request Headers`, and complete the r
[
{"key":"Header Prefix","value":"bearer"},
{"key":"Grant type","value":"Password Credentials"},
{"key":"Access Token URL","value":"http://localhost:8080/auth/realms/myRealm/protocol/openid-connect/token"},
{"key":"Access Token URL","value":"http://localhost:8080/realms/myRealm/protocol/openid-connect/token"},
{"key":"Client ID","value":"myClientID"},
{"key":"Client Secret","value":"client secret"},
{"key":"Username","value":"myuser"},
Expand Down Expand Up @@ -543,10 +551,10 @@ Then, add a user and roles to the `helidon-quickstart-mp/src/test/resources/appl
- http-basic-auth:
users:
- login: "jack"
password: "jackIsGreat"
password: "changeit"
roles: [ "admin", "user" ]
- login: "john"
password: "johnPassword"
password: "changeit"
roles: [ "user" ]
----
Expand Down
2 changes: 1 addition & 1 deletion docs/src/main/asciidoc/se/config/config-profiles.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ sources:
path: "config/config-prod.yaml"
- type: "classpath"
properties:
resource: "application.yaml"
resource: "application-test.yaml"
----
==== Built-in Types
Expand Down
18 changes: 9 additions & 9 deletions docs/src/main/asciidoc/se/guides/config.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ There are no environment variable or system property overrides defined.
.Change `app.greeting` in `resources/application.yaml` as follows:
----
app:
greeting: HelloFrom-application.yaml
greeting: HelloFrom-application-test.yaml
----
[source,bash]
Expand All @@ -141,7 +141,7 @@ curl http://localhost:8080/greet
.The new `app.greeting` value in `application.yaml` is used.
----
{
"message": "HelloFrom-application.yaml World!"
"message": "HelloFrom-application-test.yaml World!"
}
----
Expand Down Expand Up @@ -426,7 +426,7 @@ Profile file can use any supported format, following example is using `YAML`.
sources:
- type: "classpath" # <1>
properties:
resource: "application.yaml" # <2>
resource: "application-test.yaml" # <2>
----
<1> The source type.
<2> The name of the mandatory configuration resource.
Expand Down Expand Up @@ -466,7 +466,7 @@ sources:
path: "./config-file.properties" # <2>
- type: "classpath"
properties:
resource: "application.yaml"
resource: "application-test.yaml"
- type: "file"
properties:
path: "optional-config-file"
Expand Down Expand Up @@ -502,7 +502,7 @@ sources:
- type: "system-properties" # <2>
- type: "classpath"
properties:
resource: "application.yaml"
resource: "application-test.yaml"
- type: "file"
properties:
path: "./config-file.properties"
Expand Down Expand Up @@ -556,7 +556,7 @@ curl http://localhost:8080/greet
.JSON response:
----
{
"message": "HelloFrom-application.yaml World!"
"message": "HelloFrom-application-test.yaml World!"
}
----
Expand All @@ -582,7 +582,7 @@ app:
child1: child1-node
child2:
child2a:
greeting: HelloFrom-application.yaml under child2a
greeting: HelloFrom-application-test.yaml under child2a
child3: child3-node
----
Expand All @@ -606,7 +606,7 @@ curl http://localhost:8080/greet
.JSON response:
----
{
"message": "HelloFrom-application.yaml under child2a World!"
"message": "HelloFrom-application-test.yaml under child2a World!"
}
----
Expand All @@ -630,7 +630,7 @@ sources:
type: "file"
- type: "classpath"
properties:
resource: "application.yaml"
resource: "application-test.yaml"
----
[source,java]
Expand Down
Loading

0 comments on commit 155a8f2

Please sign in to comment.