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 20, 2023
1 parent 5faecb3 commit 360e68c
Show file tree
Hide file tree
Showing 7 changed files with 188 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,37 @@
/*
* 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.util.Map;

public class NettyHttpJaasTestResource extends NettyHttpTestResource {
@Override
public Map<String, String> start() {
// for JVM
System.setProperty("java.security.auth.login.config", "src/test/resources/config.jaas");
final Map<String, String> properties = super.start();
// for native
properties.put("java.security.auth.login.config", "src/test/resources/config.jaas");
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 @@ -485,6 +485,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 360e68c

Please sign in to comment.