Skip to content

Commit edf4f37

Browse files
committed
Generated keycloat client secret #4191
1 parent ddad402 commit edf4f37

File tree

8 files changed

+106
-6
lines changed

8 files changed

+106
-6
lines changed

sechub-developertools/README.adoc

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Look into
2424
for an example how you can simply implement a feature.
2525
====
2626

27-
===== Build
27+
==== Build
2828
Inside sechub root folder execute:
2929

3030
[source, bash]
@@ -36,7 +36,7 @@ This will build
3636
`/sechub-developertools/build/libs/sechub-developertools-0.0.0.jar`
3737

3838

39-
===== Start
39+
==== Start
4040
[source, bash]
4141
----
4242
export SECHUB_ADMIN_APITOKEN=int-test_superadmin-pwd
@@ -48,3 +48,25 @@ export SECHUB_ADMIN_SERVER_PORT=8443
4848
java -jar ./sechub-developertools/build/libs/sechub-developertools-0.0.0.jar
4949
----
5050

51+
=== Local Keycloak Server (as docker container)
52+
53+
LocalTestKeycloakStarter is a simple keycloak server starter for local development. (starting docker)
54+
It is not intended to be used in production!
55+
56+
==== Start
57+
58+
Keycloak is by default started on http://localhost:8080.
59+
If you do not set a port, admin and an admin password, the values are generated randomly and can be taken from the /build/tmp/keycloak_container_<port>.info file.
60+
This will generate a keycloak oauth properties file for your local sechub server. Add -Dspring.profiles.active=local to your spring properties to use the generated properties file.
61+
62+
[source, bash]
63+
----
64+
./gradlew runLocalTestKeycloakStarter
65+
----
66+
67+
==== Stop
68+
69+
[source, bash]
70+
----
71+
./gradlew stopLocalTestKeycloakStarter
72+
----

sechub-developertools/build.gradle

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ dependencies {
2626
}
2727

2828
task importEclipseProjectsNeedingOpenApiFile(type: Exec){
29+
2930
workingDir "$rootDir"
3031
commandLine './gradlew', ':sechub-systemtest:cleanEclipse',':sechub-systemtest:eclipse', ':sechub-api-java:cleanEclipse',':sechub-api-java:eclipse', ':sechub-pds-tools:cleanEclipse',':sechub-pds-tools:eclipse',':sechub-examples:example-sechub-api-java:cleanEclipse',':sechub-examples:example-sechub-api-java:eclipse','-Dsechub.build.stage=all'
3132
}
@@ -44,4 +45,27 @@ if (!secHubBuildStage.providesGeneratedOpenApiFile()){
4445
tasks.eclipse.dependsOn(importEclipseProjectsNeedingOpenApiFile)
4546
}
4647

48+
tasks.register('runLocalTestKeycloakStarter', JavaExec) {
49+
group = "Keycloak"
50+
description = "Run LocalTestKeycloakStarter with optional arguments (use -PkeycloakArgs=\"8081 myadmin mypass\")"
51+
classpath = sourceSets.main.runtimeClasspath
52+
mainClass = 'com.mercedesbenz.sechub.developertools.container.keycloak.LocalTestKeycloakStarter'
53+
if (project.hasProperty('keycloakArgs')) {
54+
args project.property('keycloakArgs').split("\\s+")
55+
}
56+
}
4757

58+
tasks.register('stopLocalTestKeycloakStarter', Delete) {
59+
group = "Keycloak"
60+
description = "delete the .info file to stop the running keycloak instance (use -PkeycloakPort=8081)"
61+
def port = project.hasProperty('keycloakPort') ? project.property('keycloakPort') : '8080'
62+
def infoFile = file("$rootDir/sechub-developertools/build/tmp/keycloak_container_${port}.info")
63+
doFirst {
64+
if (infoFile.exists()) {
65+
println "Stopping Keycloak by deleting info file: ${infoFile.absolutePath}"
66+
infoFile.delete()
67+
} else {
68+
println "No Keycloak instance is running on port ${port}, nothing to stop."
69+
}
70+
}
71+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
sechub:
2+
security:
3+
server:
4+
modes: oauth2, classic
5+
oauth2:
6+
mode: opaque-token
7+
opaque-token:
8+
introspection-uri: http://localhost:8080/realms/web-ui-server-local/protocol/openid-connect/token/introspect
9+
client-id: web-ui-server-local
10+
client-secret: n8Ka5Wnkl
11+
max-cache-duration: 300s
12+
login:
13+
enabled: true
14+
login-page: /login
15+
redirect-uri: http://localhost:3000
16+
modes: oauth2, classic
17+
oauth2:
18+
client-id: web-ui-server-local
19+
client-secret: ${SECHUB_SECURITY_SERVER_OAUTH2_CLIENT_SECRET}
20+
provider: keycloak
21+
redirect-uri: https://localhost:8443/login/oauth2/code/keycloak
22+
issuer-uri: http://localhost:8080/realms/web-ui-server-local
23+
authorization-uri: http://localhost:8080/realms/web-ui-server-local/protocol/openid-connect/auth
24+
token-uri: http://localhost:8080/realms/web-ui-server-local/protocol/openid-connect/token
25+
user-info-uri: http://localhost:8080/realms/web-ui-server-local/protocol/openid-connect/userinfo
26+
jwk-set-uri: http://localhost:8080/realms/web-ui-server-local/protocol/openid-connect/certs
27+
classic:
28+
cookie-age-seconds: 3600
29+
encryption:
30+
secret-key: aB3xYz8KpL9mQw2VcT1sNj7FuW4vEp0Z
31+
minimumTokenValidity: 24h

sechub-developertools/scripts/container/keycloak/sechub-int-keycloak-realm.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@
809809
"enabled": true,
810810
"alwaysDisplayInConsole": false,
811811
"clientAuthenticatorType": "client-secret",
812-
"secret": "**********",
812+
"secret": "${SECHUB_SECURITY_SERVER_OAUTH2_CLIENT_SECRET}",
813813
"redirectUris": [
814814
"*"
815815
],

sechub-developertools/scripts/container/keycloak/start.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ function usage() {
1010
# setting default values for keycloak admin user and password
1111
export KEYCLOAK_ADMIN=${KEYCLOAK_ADMIN:-admin}
1212
export KEYCLOAK_ADMIN_PASSWORD=${KEYCLOAK_ADMIN_PASSWORD:-admin}
13+
export SECHUB_SECURITY_SERVER_OAUTH2_CLIENT_SECRET=${SECHUB_SECURITY_SERVER_OAUTH2_CLIENT_SECRET:-$(uuidgen)}
1314

1415
echo "${KEYCLOAK_ADMIN}:${KEYCLOAK_ADMIN_PASSWORD}"
1516
addEnv "DATABASE_START_MODE=server"
1617
addEnv "KEYCLOAK_ADMIN=$KEYCLOAK_ADMIN"
1718
addEnv "KEYCLOAK_ADMIN_PASSWORD=$KEYCLOAK_ADMIN_PASSWORD"
19+
addEnv "SECHUB_SECURITY_SERVER_OAUTH2_CLIENT_SECRET=$SECHUB_SECURITY_SERVER_OAUTH2_CLIENT_SECRET"
1820

1921
defineContainerPort 8080
2022
if [[ -z "$1" ]]; then
@@ -32,3 +34,15 @@ ensureImageBuild
3234
ensureContainerNotRunning
3335

3436
startContainer
37+
38+
# Copy keycloak properties as local sechub-server properties using envsubst
39+
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
40+
local_template="${script_dir}/application-local-test-keycloak-template.yaml"
41+
sechub_properties_local_keycloak="${script_dir}/../../../../sechub-server/src/main/resources/application-local-test-keycloak-gen.${USER}.yaml"
42+
43+
if [ -f "${sechub_properties_local_keycloak}" ]; then
44+
echo "Removing existing local Keycloak properties file: ${sechub_properties_local_keycloak}"
45+
rm -f "${sechub_properties_local_keycloak}"
46+
fi
47+
48+
envsubst < "${local_template}" > "${sechub_properties_local_keycloak}"

sechub-developertools/src/main/java/com/mercedesbenz/sechub/developertools/container/keycloak/KeycloakTestContainer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.IOException;
55
import java.nio.file.Files;
66
import java.nio.file.Path;
7+
import java.nio.file.Paths;
78

89
import com.mercedesbenz.sechub.developertools.container.BashScriptContainerLaunchConfig;
910
import com.mercedesbenz.sechub.developertools.container.BashScriptContainerLauncher;
@@ -24,7 +25,12 @@ public KeycloakTestContainer(int testPort, String admin, String password) {
2425
}
2526

2627
private static Path resolveScript(String scriptName) {
27-
return Path.of("../scripts", "container", "keycloak", scriptName).toAbsolutePath().normalize();
28+
// Resolve the script path relative to the class location
29+
// It should not matter where the TestContainer is executed from
30+
String classLocation = KeycloakTestContainer.class.getProtectionDomain().getCodeSource().getLocation().getPath();
31+
Path classPath = Paths.get(classLocation).getParent();
32+
33+
return classPath.resolve(Paths.get("../../../scripts/container/keycloak", scriptName)).normalize();
2834
}
2935

3036
public void start() throws Exception {

sechub-server/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33

44
# Ignore local property files
55
application-local.*.yaml
6-
application-local.*.yml
6+
application-local.*.yml
7+
application-local-test-keycloak-gen.*.yaml

sechub-server/src/main/resources/application-local.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
spring:
88
config:
99
import:
10-
- optional:classpath:application-local.${USER}.yml
10+
- optional:classpath:application-local.${USER}.yml
11+
- optional:classpath:application-local.${USER}.yaml
12+
- optional:classpath:application-local-test-keycloak-gen.${USER}.yaml

0 commit comments

Comments
 (0)