Skip to content

Commit 0131b88

Browse files
authored
Added dataset options for setting password hashing algorithm and iterations. (#778) (#780)
* Added options for setting password hashing algorithm and iterations to the dataset provider module. Signed-off-by: Tomas Kyjovsky <[email protected]> * updating docs Signed-off-by: Kamesh Akella <[email protected]> --------- Signed-off-by: Tomas Kyjovsky <[email protected]> Signed-off-by: Kamesh Akella <[email protected]> Co-authored-by: Kamesh Akella <[email protected]> # Conflicts: # dataset/dataset-import.sh # dataset/src/main/java/org/keycloak/benchmark/dataset/DatasetResourceProvider.java # dataset/src/main/java/org/keycloak/benchmark/dataset/config/DatasetConfig.java # doc/dataset/modules/ROOT/pages/using-provider.adoc
1 parent bcdc240 commit 0131b88

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

dataset/dataset-import.sh

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ set_environment_variables () {
1313
USERS_COUNT="100"
1414
EVENTS_COUNT="100"
1515
SESSIONS_COUNT="100"
16-
HASH_ITERATIONS="27500"
1716
if ( minikube version &>/dev/null ); then
1817
KEYCLOAK_URI="https://keycloak-keycloak.$(minikube ip || echo 'unknown').nip.io/realms/master/dataset"
1918
fi
2019
REALM_PREFIX="realm"
2120
STATUS_TIMEOUT="120"
2221

23-
while getopts ":a:r:n:c:u:e:o:i:p:l:t:" OPT
22+
while getopts ":a:r:n:c:u:e:o:g:i:p:l:t:" OPT
2423
do
2524
case $OPT in
2625
a)
@@ -44,6 +43,9 @@ set_environment_variables () {
4443
o)
4544
SESSIONS_COUNT=$OPTARG
4645
;;
46+
g)
47+
HASH_ALGORITHM=$OPTARG
48+
;;
4749
i)
4850
HASH_ITERATIONS=$OPTARG
4951
;;
@@ -64,11 +66,6 @@ set_environment_variables () {
6466
done
6567
}
6668

67-
create_realms () {
68-
echo "Creating $1 realm/s with $2 client/s and $3 user/s with $4 password hash iterations."
69-
execute_command "create-realms?count=$1&clients-per-realm=$2&users-per-realm=$3&password-hash-iterations=$4"
70-
}
71-
7269
create_clients () {
7370
echo "Creating $1 client/s in realm $2"
7471
execute_command "create-clients?count=$1&realm-name=$2"
@@ -163,7 +160,7 @@ check_dataset_status () {
163160

164161
help () {
165162
echo "Dataset import to the local minikube Keycloak application - usage:"
166-
echo "1) create realm/s with clients, users and password hash iterations - run -a (action) with or without other arguments: -a create-realms -r 10 -c 100 -u 100 -i 20000 -l 'https://keycloak.url.com'"
163+
echo "1) create realm/s with clients, users and password hash algorithm & iterations - run -a (action) with or without other arguments: -a create-realms -r 10 -g argon2 -i 5 -c 100 -u 100 -l 'https://keycloak.url.com'"
167164
echo "2) create clients in specific realm: -a create-clients -c 100 -n realm-0 -l 'https://keycloak.url.com'"
168165
echo "3) create users in specific realm: -a create-users -u 100 -n realm-0 -l 'https://keycloak.url.com'"
169166
echo "4) create events in specific realm: -a create-events -e 100 -n realm-0 -l 'https://keycloak.url.com'"
@@ -181,7 +178,10 @@ main () {
181178
echo "Action: [$ACTION] "
182179
case "$ACTION" in
183180
create-realms)
184-
create_realms $REALM_COUNT $CLIENTS_COUNT $USERS_COUNT $HASH_ITERATIONS
181+
if [ -z "$HASH_ALGORITHM" ]; then HA_PARAM=""; HASH_ALGORITHM="default"; else HA_PARAM="&password-hash-algorithm=$HASH_ALGORITHM"; fi
182+
if [ -z "$HASH_ITERATIONS" ]; then HI_PARAM=""; HASH_ITERATIONS="default"; else HI_PARAM="&password-hash-iterations=$HASH_ITERATIONS"; fi
183+
echo "Creating $REALM_COUNT realms with $CLIENTS_COUNT clients and $USERS_COUNT users with $HASH_ITERATIONS password-hashing iterations using the $HASH_ALGORITHM algorithm."
184+
execute_command "create-realms?count=$REALM_COUNT&clients-per-realm=$CLIENTS_COUNT&users-per-realm=$USERS_COUNT$HI_PARAM$HA_PARAM"
185185
exit 0
186186
;;
187187
create-clients)

dataset/src/main/java/org/keycloak/benchmark/dataset/DatasetResourceProvider.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,14 @@ private void createAndSetRealm(RealmContext context, int index, KeycloakSession
913913
realm.setEnabled(true);
914914
realm.setRegistrationAllowed(true);
915915
realm.setAccessCodeLifespan(60);
916-
realm.setPasswordPolicy(PasswordPolicy.parse(session, "hashIterations(" + config.getPasswordHashIterations() + ")"));
916+
PasswordPolicy.Builder b = PasswordPolicy.build();
917+
if (!config.getPasswordHashAlgorithm().isEmpty()) { // only set if parameter explicitly provided, see QueryParamFill.defaultValue()
918+
b.put("hashAlgorithm", config.getPasswordHashAlgorithm());
919+
}
920+
if (config.getPasswordHashIterations() != -1) { // only set if parameter explicitly provided, see QueryParamIntFill.defaultValue()
921+
b.put("hashIterations", config.getPasswordHashIterations().toString());
922+
}
923+
realm.setPasswordPolicy(b.build(session));
917924

918925
if (config.getEventsEnabled()) {
919926
realm.setEventsEnabled(true);

dataset/src/main/java/org/keycloak/benchmark/dataset/config/DatasetConfig.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,12 @@ public class DatasetConfig {
153153
@QueryParamIntFill(paramName = "client-roles-per-user", defaultValue = 4, operations = { CREATE_REALMS, CREATE_USERS })
154154
private Integer clientRolesPerUser;
155155

156-
// Password policy with the number of password hash iterations. It is 27500 by default
157-
@QueryParamIntFill(paramName = "password-hash-iterations", defaultValue = PasswordPolicy.HASH_ITERATIONS_DEFAULT, operations = { CREATE_REALMS })
156+
// Password policy with the password hash algorithm.
157+
@QueryParamFill(paramName = "password-hash-algorithm", operations = { CREATE_REALMS })
158+
private String passwordHashAlgorithm;
159+
160+
// Password policy with the number of password hash iterations.
161+
@QueryParamIntFill(paramName = "password-hash-iterations", operations = { CREATE_REALMS })
158162
private Integer passwordHashIterations;
159163

160164
// Check if eventStorage will be enabled for newly created realms
@@ -304,6 +308,10 @@ public Integer getClientRolesPerUser() {
304308
return clientRolesPerUser;
305309
}
306310

311+
public String getPasswordHashAlgorithm() {
312+
return passwordHashAlgorithm;
313+
}
314+
307315
public Integer getPasswordHashIterations() {
308316
return passwordHashIterations;
309317
}

doc/dataset/modules/ROOT/pages/using-provider.adoc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ You will see these options:
3131

3232
----
3333
Dataset import to the local minikube Keycloak application - usage:
34-
1) create realm/s with clients, users and password hash iterations - run -a (action) with or without other arguments: -a create-realms -r 10 -c 100 -u 100 -h 20000 -l 'https://keycloak.url.com'
34+
1) create realm/s with clients, users and password hash algorithm & iterations - run -a (action) with or without other arguments: -a create-realms -r 10 -g argon2 -i 5 -c 100 -u 100 -l 'https://keycloak.url.com'
3535
2) create clients in specific realm: -a create-clients -c 100 -n realm-0 -l 'https://keycloak.url.com'
3636
3) create users in specific realm: -a create-users -u 100 -n realm-0 -l 'https://keycloak.url.com'
3737
4) create events in specific realm: -a create-events -e 100 -n realm-0 -l 'https://keycloak.url.com'
@@ -168,12 +168,18 @@ You can use parameters to remove all realms for example just from `foorealm5` to
168168

169169
For change the parameters, take a look at link:{github-files}/dataset/src/main/java/org/keycloak/benchmark/dataset/config/DatasetConfig.java[DataSetConfig class]
170170
to see available parameters and default values and which endpoint the particular parameter is applicable.
171-
For example to create realms with prefix `foo` and with just 1000 hash iterations used for the password policy, you can use these parameters:
171+
For example to create realms with prefix `foo` and with just 1000 hash iterations (with the default hashing algorithm) used for the password policy you can use these parameters:
172172

173173
----
174174
.../realms/master/dataset/create-realms?count=10&realm-prefix=foo&password-hash-iterations=1000
175175
----
176176

177+
Another example would be, to specify a particular hashing algorithm in combination with the hashing iterations with the below parameters:
178+
179+
----
180+
.../realms/master/dataset/create-realms?count=10&realm-prefix=foo&password-hash-algorithm=argon2&password-hash-iterations=1000
181+
----
182+
177183
The configuration is written to the server log when HTTP endpoint is triggered, so you can monitor the progress and what parameters were effectively applied.
178184

179185
Note that creation of new objects will automatically start from the next available index.

0 commit comments

Comments
 (0)