This repository has been archived by the owner on Mar 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the customized valve to enable bypassing Keycloak authentication
Signed-off-by: David Festal <dfestal@redhat.com>
- Loading branch information
1 parent
5851878
commit 37f1862
Showing
7 changed files
with
170 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
Copyright (c) 2017 Red Hat Inc. | ||
All rights reserved. This program and the accompanying materials | ||
are made available under the terms of the Eclipse Public License v1.0 | ||
which accompanies this distribution, and is available at | ||
http://www.eclipse.org/legal/epl-v10.html | ||
Contributors: | ||
Red Hat Inc. - initial API and implementation | ||
--> | ||
|
||
<project | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" | ||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<artifactId>fabric8-ide-assembly-parent</artifactId> | ||
<groupId>com.redhat.che</groupId> | ||
<version>@redhat.che.version@</version> | ||
</parent> | ||
<artifactId>user-auth-valve</artifactId> | ||
<name>user-auth-valve</name> | ||
<url>http://maven.apache.org</url> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.tomcat</groupId> | ||
<artifactId>tomcat-catalina</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.keycloak</groupId> | ||
<artifactId>keycloak-tomcat8-adapter</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
68 changes: 68 additions & 0 deletions
68
...abric8-che/assembly/user-auth-valve/src/main/java/com/redhat/che/valve/UserAuthValve.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.redhat.che.valve; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.util.Arrays; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.apache.catalina.connector.Request; | ||
import org.apache.catalina.realm.GenericPrincipal; | ||
import org.apache.juli.logging.Log; | ||
import org.apache.juli.logging.LogFactory; | ||
import org.keycloak.adapters.tomcat.KeycloakAuthenticatorValve; | ||
|
||
public class UserAuthValve extends KeycloakAuthenticatorValve { | ||
|
||
private static final String KEYCLOAK_SETTINGS_ENDPOINT = "http://che-host:8080/api/keycloak/settings"; | ||
private static final Pattern DISABLED_SETTING_PATTERN = Pattern.compile(".*\"che\\.keycloak\\.disabled\":\"([^ \"]+)\".*"); | ||
private static final Log LOG = LogFactory.getLog(UserAuthValve.class); | ||
|
||
@Override | ||
public boolean authenticate(Request request, HttpServletResponse response) throws IOException { | ||
if (isKeycloakDisabled()) { | ||
LOG.info("Keycloak is disabled => Bypassing authentification"); | ||
GenericPrincipal genericPrincipal = new GenericPrincipal("developer", "developer", Arrays.asList("developer")); | ||
request.setUserPrincipal(genericPrincipal); | ||
request.setAuthType(HttpServletRequest.BASIC_AUTH); | ||
return true; | ||
} | ||
|
||
return super.authenticate(request, response); | ||
} | ||
|
||
|
||
public boolean isKeycloakDisabled() { | ||
URL url; | ||
HttpURLConnection conn; | ||
try { | ||
url = new URL(KEYCLOAK_SETTINGS_ENDPOINT); | ||
conn = (HttpURLConnection) url.openConnection(); | ||
conn.setRequestMethod("GET"); | ||
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); | ||
StringBuilder response = new StringBuilder(); | ||
String line; | ||
while ((line = in.readLine()) != null) { | ||
response.append(line); | ||
} | ||
LOG.info("Keycloak settings: " + response); | ||
Matcher matcher = DISABLED_SETTING_PATTERN.matcher(response.toString()); | ||
if (matcher.matches()) { | ||
String value = matcher.group(1); | ||
return "true".equals(value); | ||
} | ||
return false; | ||
|
||
} catch (IOException e) { | ||
LOG.error("Exception during Keycloak settings retrieval", e); | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters