Skip to content

Commit

Permalink
[relates #4956] Add netty-http JAAS test case
Browse files Browse the repository at this point in the history
  • Loading branch information
avano committed Jun 21, 2023
1 parent 9801f18 commit 18b723e
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;

class NettyHTTPProcessor {

Expand All @@ -27,4 +28,10 @@ class NettyHTTPProcessor {
FeatureBuildItem feature() {
return new FeatureBuildItem(FEATURE);
}

// Needed when using the default JaasSecurityAuthenticator, otherwise fails with ClassNotFoundException
@BuildStep
ReflectiveClassBuildItem registerForReflection() {
return ReflectiveClassBuildItem.builder("sun.security.provider.ConfigFile").build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* 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.apache.camel.quarkus.component.netty.http.auth;

import java.util.Map;

import javax.security.auth.Subject;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.login.LoginException;
import javax.security.auth.spi.LoginModule;

import io.quarkus.runtime.annotations.RegisterForReflection;

@RegisterForReflection
public class JaasLoginModule implements LoginModule {
private CallbackHandler callbackHandler;

@Override
public void initialize(
Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options) {
this.callbackHandler = callbackHandler;
}

@Override
public boolean login() throws LoginException {
Callback[] callbacks = new Callback[1];
callbacks[0] = new PasswordCallback("password", false);

try {
callbackHandler.handle(callbacks);
char[] tmpPassword = ((PasswordCallback) callbacks[0]).getPassword();
String password = new String(tmpPassword);
((PasswordCallback) callbacks[0]).clearPassword();
if (!"adminjaaspass".equals(password)) {
throw new LoginException("Login denied");
}
} catch (Exception e) {
LoginException le = new LoginException(e.getMessage());
le.initCause(e);
throw le;
}

return true;
}

@Override
public boolean commit() {
return true;
}

@Override
public boolean abort() {
return true;
}

@Override
public boolean logout() {
callbackHandler = null;
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* 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.apache.camel.quarkus.component.netty.http;

import io.quarkus.test.junit.QuarkusIntegrationTest;

@QuarkusIntegrationTest
public class NettyHttpJaasIT extends NettyHttpJaasTest {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* 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.apache.camel.quarkus.component.netty.http;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

@QuarkusTest
@QuarkusTestResource(NettyHttpJaasTestResource.class)
public class NettyHttpJaasTest {
@ParameterizedTest
@CsvSource({
"admin,wrongjaaspass,401",
"admin,adminjaaspass,200"
})
public void testJaas(String user, String password, int responseCode) {
RestAssured
.when()
.get("/netty/http/jaas/{user}/{password}", user, password)
.then()
.statusCode(responseCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* 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.apache.camel.quarkus.component.netty.http;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;

public class NettyHttpJaasTestResource extends NettyHttpTestResource {
private static final String JAAS_FILE_NAME = "config.jaas";
private static final Path SOURCE_FILE = Paths.get("src/test/resources/").resolve(JAAS_FILE_NAME);
private static final Path TARGET_FILE = Paths.get("target/").resolve(JAAS_FILE_NAME);

@Override
public Map<String, String> start() {
if (!Files.exists(TARGET_FILE)) {
try {
Files.copy(SOURCE_FILE, TARGET_FILE);
} catch (IOException e) {
throw new RuntimeException("Unable to copy " + JAAS_FILE_NAME, e);
}
}

// for JVM
System.setProperty("java.security.auth.login.config", TARGET_FILE.toAbsolutePath().toString());
final Map<String, String> properties = super.start();
// for native
properties.put("java.security.auth.login.config", TARGET_FILE.toAbsolutePath().toString());
return properties;
}

@Override
public void stop() {
System.clearProperty("java.security.auth.login.config");
super.stop();
}
}
3 changes: 3 additions & 0 deletions integration-tests/netty-http/src/test/resources/config.jaas
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Quarkus {
org.apache.camel.quarkus.component.netty.http.auth.JaasLoginModule required debug=true;
};
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@
<exclude>**/*.ftl</exclude>
<exclude>**/*.graphql</exclude>
<exclude>**/*.ics</exclude>
<exclude>**/*.jaas</exclude>
<exclude>**/*.jks</exclude>
<exclude>**/*.jpeg</exclude>
<exclude>**/*.key</exclude>
Expand Down

0 comments on commit 18b723e

Please sign in to comment.