-
Notifications
You must be signed in to change notification settings - Fork 573
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[incubator-kie-issues-994] kafka auth feature with message header records #3037
Merged
elguardian
merged 4 commits into
kiegroup:main
from
elguardian:incubator-kie-issues-994
Mar 18, 2024
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -16,32 +16,83 @@ | |
|
||
package org.kie.server.common; | ||
|
||
import java.net.URI; | ||
import java.nio.file.Paths; | ||
|
||
import org.drools.core.util.KeyStoreConstants; | ||
import org.drools.core.util.KeyStoreHelper; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.kie.server.api.KieServerConstants; | ||
import org.kie.server.api.model.KieServerConfig; | ||
import org.kie.server.api.model.KieServerConfigItem; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.kie.server.common.KeyStoreHelperUtil.loadControllerPassword; | ||
|
||
public class KeyStoreHelperUtilTest { | ||
|
||
private static final String KEYSTORE_PATH = "target/keystore.jks"; | ||
private static final String KEYSTORE_PWD = "password"; | ||
private static final String KEYSTORE_ALIAS = "selfsigned"; | ||
|
||
@BeforeClass | ||
public static void init() throws Exception { | ||
// generate self signed certificate | ||
String[] cmd = { "keytool", "-genkey", | ||
"-keyalg", "RSA", | ||
"-alias", KEYSTORE_ALIAS, | ||
"-keystore", KEYSTORE_PATH, | ||
"-storepass", KEYSTORE_PWD, | ||
"-validity", "360", | ||
"-keysize", "1024", | ||
"-dname", "CN=root, OU=root, O=root, L=root, ST=root, C=root" | ||
}; | ||
|
||
ProcessBuilder builder = new ProcessBuilder(); | ||
builder.command(cmd); | ||
Process p = builder.start(); | ||
p.waitFor(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps better to add a timeout:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
|
||
@Test | ||
public void testKeyPairReading() throws Exception { | ||
try { | ||
// this test if we can read our own keys properly | ||
URI uri = Paths.get(KEYSTORE_PATH).toAbsolutePath().toUri(); | ||
System.setProperty(KeyStoreConstants.PROP_PVT_KS_URL, uri.toURL().toExternalForm()); | ||
System.setProperty(KeyStoreConstants.PROP_PVT_KS_PWD, KEYSTORE_PWD); | ||
System.setProperty(KeyStoreHelperUtil.PROP_PWD_JWT_ALIAS, KEYSTORE_ALIAS); | ||
|
||
KeyStoreHelper.reInit(); | ||
|
||
assertNotNull(KeyStoreHelperUtil.getJwtKeyPair()); | ||
|
||
} finally { | ||
System.clearProperty(KeyStoreConstants.PROP_PVT_KS_URL); | ||
System.clearProperty(KeyStoreConstants.PROP_PVT_KS_PWD); | ||
System.clearProperty(KeyStoreHelperUtil.PROP_PWD_JWT_ALIAS); | ||
} | ||
|
||
} | ||
|
||
@Test | ||
public void testDefaultPassword(){ | ||
public void testDefaultPassword() { | ||
final String defaultPassword = "default"; | ||
final String password = loadControllerPassword(defaultPassword); | ||
assertEquals(defaultPassword, password); | ||
} | ||
|
||
@Test | ||
public void testConfigDefaultPassword(){ | ||
public void testConfigDefaultPassword() { | ||
final KieServerConfig serverConfig = new KieServerConfig(); | ||
final String password = loadControllerPassword(serverConfig); | ||
assertEquals("kieserver1!", password); | ||
} | ||
|
||
@Test | ||
public void testConfigPassword(){ | ||
public void testConfigPassword() { | ||
final KieServerConfig serverConfig = new KieServerConfig(); | ||
final String defaultPassword = "default"; | ||
serverConfig.addConfigItem(new KieServerConfigItem(KieServerConstants.CFG_KIE_CONTROLLER_PASSWORD, defaultPassword, null)); | ||
|
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
39 changes: 39 additions & 0 deletions
39
...mmon/src/main/java/org/kie/server/services/impl/security/adapters/JwtSecurityAdaptor.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,39 @@ | ||
package org.kie.server.services.impl.security.adapters; | ||
|
||
import java.util.List; | ||
|
||
import org.kie.server.api.security.SecurityAdapter; | ||
import org.kie.server.services.impl.util.JwtUserDetails; | ||
|
||
public class JwtSecurityAdaptor implements SecurityAdapter { | ||
|
||
private static ThreadLocal<JwtUserDetails> threadLocal = new ThreadLocal<JwtUserDetails>() { | ||
@Override | ||
public JwtUserDetails initialValue() { | ||
return new JwtUserDetails(); | ||
} | ||
}; | ||
|
||
public static void login(JwtUserDetails userDetails) { | ||
threadLocal.set(userDetails); | ||
} | ||
|
||
@Override | ||
public String getUser(Object... params) { | ||
JwtUserDetails userDetails = threadLocal.get(); | ||
if (!userDetails.isLogged()) { | ||
return null; | ||
} | ||
return userDetails.getUser(); | ||
} | ||
|
||
@Override | ||
public List<String> getRoles(Object... params) { | ||
return threadLocal.get().getRoles(); | ||
} | ||
|
||
public static void logout() { | ||
threadLocal.set(null); | ||
} | ||
|
||
} |
106 changes: 106 additions & 0 deletions
106
...ie-server-services-common/src/main/java/org/kie/server/services/impl/util/JwtService.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,106 @@ | ||
/* | ||
* Copyright 2024 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.kie.server.services.impl.util; | ||
|
||
import java.security.KeyPair; | ||
import java.security.interfaces.RSAPrivateKey; | ||
import java.security.interfaces.RSAPublicKey; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import com.auth0.jwt.JWT; | ||
import com.auth0.jwt.algorithms.Algorithm; | ||
import com.auth0.jwt.exceptions.JWTVerificationException; | ||
import com.auth0.jwt.interfaces.Claim; | ||
import com.auth0.jwt.interfaces.DecodedJWT; | ||
import com.auth0.jwt.interfaces.JWTVerifier; | ||
|
||
public class JwtService { | ||
|
||
private JWTVerifier verifier; | ||
|
||
private Algorithm algorithm; | ||
private String issuer; | ||
|
||
private JwtService() { | ||
this(Algorithm.none()); | ||
} | ||
|
||
private JwtService(Algorithm algorithm) { | ||
this(algorithm, "jBPM"); | ||
} | ||
|
||
private JwtService(Algorithm algorithm, String issuer) { | ||
this.issuer = issuer; | ||
this.algorithm = algorithm; | ||
this.verifier = JWT.require(algorithm) | ||
.withIssuer(issuer) | ||
.build(); | ||
} | ||
|
||
public String getIssuer() { | ||
return issuer; | ||
} | ||
|
||
public String token(String user, String... roles) { | ||
return JWT.create().withIssuer(this.issuer).withSubject(user).withClaim("roles", Arrays.asList(roles)).sign(algorithm); | ||
} | ||
|
||
public static JwtServiceBuilder newJwtServiceBuilder() { | ||
return new JwtServiceBuilder(); | ||
} | ||
|
||
public static class JwtServiceBuilder { | ||
Algorithm algorithm; | ||
String issuer; | ||
|
||
public JwtServiceBuilder keys(RSAPublicKey publicKey, RSAPrivateKey privateKey) { | ||
this.algorithm = Algorithm.RSA256(publicKey, privateKey); | ||
return this; | ||
} | ||
|
||
public JwtServiceBuilder issuer(String issuer) { | ||
this.issuer = issuer; | ||
return this; | ||
} | ||
|
||
public JwtService build() { | ||
return new JwtService(algorithm != null ? algorithm : Algorithm.none(), issuer != null ? issuer : "jBPM"); | ||
} | ||
|
||
public JwtServiceBuilder keyPair(KeyPair keyPair) { | ||
if (keyPair != null) { | ||
this.algorithm = Algorithm.RSA256((RSAPublicKey) keyPair.getPublic(), (RSAPrivateKey) keyPair.getPrivate()); | ||
} | ||
return this; | ||
} | ||
|
||
} | ||
|
||
public JwtUserDetails decodeUserDetails(String token) { | ||
try { | ||
DecodedJWT decodedJWT = verifier.verify(token); | ||
String user = decodedJWT.getSubject(); | ||
Claim rolesClaim = decodedJWT.getClaim("roles"); | ||
List<String> roles = rolesClaim.asList(String.class); | ||
return new JwtUserDetails(user, roles != null ? roles : new ArrayList<>()); | ||
} catch (JWTVerificationException exception) { | ||
throw new IllegalArgumentException(exception); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I needed to add
otherwise the test was hanging for me infinitely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah after java 8 the keypass is mandatory. my bad for testing only in java 8