Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.bifromq.type.ClientInfo;
import io.micrometer.core.instrument.Timer;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
Expand All @@ -64,17 +65,22 @@ public AuthProviderManager(String authProviderFQN,
this.settingProvider = settingProvider;
this.eventCollector = eventCollector;
Map<String, IAuthProvider> availAuthProviders = pluginMgr.getExtensions(IAuthProvider.class)
.stream().collect(Collectors.toMap(e -> e.getClass().getName(), e -> e));
.stream().collect(Collectors.toMap(e -> e.getClass().getName(), e -> e,
(k,v) -> v, TreeMap::new));
if (availAuthProviders.isEmpty()) {
pluginLog.warn("No auth provider plugin available, use DEV ONLY one instead");
delegate = new DevOnlyAuthProvider();
} else {
if (authProviderFQN == null) {
pluginLog.warn("Auth provider plugin type not specified, use DEV ONLY one instead");
delegate = new DevOnlyAuthProvider();
if (availAuthProviders.size() > 1) {
pluginLog.info("Auth provider plugin type not specified, use the first found");
}
String firstAuthProviderFQN = availAuthProviders.keySet().iterator().next();
pluginLog.info("Auth provider plugin loaded: {}", firstAuthProviderFQN);
delegate = availAuthProviders.get(firstAuthProviderFQN);
} else if (!availAuthProviders.containsKey(authProviderFQN)) {
pluginLog.warn("Auth provider plugin type '{}' not found, use DEV ONLY one instead", authProviderFQN);
delegate = new DevOnlyAuthProvider();
pluginLog.warn("Auth provider plugin type '{}' not found, so the system will shut down.", authProviderFQN);
throw new AuthProviderPluginException("Auth provider plugin type '%s' not found, so the system will shut down.", authProviderFQN);
} else {
pluginLog.info("Auth provider plugin type: {}", authProviderFQN);
delegate = availAuthProviders.get(authProviderFQN);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.bifromq.plugin.authprovider;

import org.pf4j.util.StringUtils;

public class AuthProviderPluginException extends RuntimeException {

public AuthProviderPluginException(String message, Object... args) {
super(StringUtils.format(message, args));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.bifromq.plugin.authprovider.type.MQTT5ExtendedAuthData;
import org.apache.bifromq.plugin.authprovider.type.MQTT5ExtendedAuthResult;
import org.apache.bifromq.plugin.authprovider.type.MQTTAction;
import org.apache.bifromq.plugin.authprovider.type.Ok;
import org.apache.bifromq.plugin.authprovider.type.PubAction;
import org.apache.bifromq.plugin.authprovider.type.Reject;
import org.apache.bifromq.plugin.authprovider.type.SubAction;
Expand All @@ -52,6 +53,8 @@
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;

import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -133,18 +136,52 @@ public void pluginSpecified() {
}

@Test
public void pluginNotSpecifiedWithSingleProvider() {
manager = new AuthProviderManager(null, pluginManager, settingProvider, eventCollector);
when(mockProvider.auth(mockAuth3Data)).thenReturn(
CompletableFuture.completedFuture(MQTT3AuthResult.newBuilder()
.setReject(Reject.newBuilder().setCode(Reject.Code.BadPass).build()).build()));
MQTT3AuthResult result = manager.auth(mockAuth3Data).join();
assertEquals(result.getTypeCase(), MQTT3AuthResult.TypeCase.REJECT);
assertEquals(result.getReject().getCode(), Reject.Code.BadPass);
manager.close();
}

@Test
public void pluginNotSpecifiedWithMultipleProviders() {
IAuthProvider provider1 = new FirstTestAuthProvider();
IAuthProvider provider2 = new SecondTestAuthProvider();

when(pluginManager.getExtensions(IAuthProvider.class)).thenReturn(
Arrays.asList(provider1, provider2));
manager = new AuthProviderManager(null, pluginManager, settingProvider, eventCollector);

MQTT3AuthResult result = manager.auth(mockAuth3Data).join();
// Deterministically selects the provider with lexicographically smallest class name
assertEquals(result.getOk().getTenantId(), "FirstProvider");
manager.close();
}

@Test
public void pluginNotSpecifiedWithMultipleSortByKeyProviders() {
IAuthProvider provider1 = new FirstTestAuthProvider();
IAuthProvider provider2 = new SecondTestAuthProvider();

when(pluginManager.getExtensions(IAuthProvider.class)).thenReturn(
Arrays.asList(provider2, provider1));
manager = new AuthProviderManager(null, pluginManager, settingProvider, eventCollector);

MQTT3AuthResult result = manager.auth(mockAuth3Data).join();
// Deterministically selects the provider with lexicographically smallest class name
assertEquals(result.getOk().getTenantId(), "FirstProvider");
manager.close();
}


@Test(expectedExceptions = AuthProviderPluginException.class)
public void pluginNotFound() {
manager = new AuthProviderManager("Fake", pluginManager, settingProvider, eventCollector);
MQTT3AuthResult result = manager.auth(mockAuth3Data).join();
assertEquals(result.getTypeCase(), MQTT3AuthResult.TypeCase.OK);
assertEquals(result.getOk().getTenantId(), "DevOnly");

boolean allow = manager.check(ClientInfo.getDefaultInstance(), MQTTAction.newBuilder()
.setSub(SubAction.getDefaultInstance()).build()).join();
assertTrue(allow);
allow = manager.check(ClientInfo.getDefaultInstance(), MQTTAction.newBuilder()
.setSub(SubAction.getDefaultInstance()).build()).join();
assertTrue(allow);
manager.close();
}

Expand Down Expand Up @@ -404,4 +441,31 @@ public void byPassCheckResultError() {
assertEquals(meterRegistry.find(CALL_FAIL_COUNTER).tag(TAG_METHOD, "AuthProvider/check").counter().count(),
0);
}

static class FirstTestAuthProvider implements IAuthProvider {
@Override
public CompletableFuture<MQTT3AuthResult> auth(MQTT3AuthData authData) {
return CompletableFuture.completedFuture(MQTT3AuthResult.newBuilder()
.setOk(Ok.newBuilder().setTenantId("FirstProvider").build()).build());
}

@Override
public CompletableFuture<Boolean> check(ClientInfo client, MQTTAction action) {
return CompletableFuture.completedFuture(true);
}
}

static class SecondTestAuthProvider implements IAuthProvider {
@Override
public CompletableFuture<MQTT3AuthResult> auth(MQTT3AuthData authData) {
return CompletableFuture.completedFuture(MQTT3AuthResult.newBuilder()
.setOk(Ok.newBuilder().setTenantId("SecondProvider").build()).build());
}

@Override
public CompletableFuture<Boolean> check(ClientInfo client, MQTTAction action) {
return CompletableFuture.completedFuture(true);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.bifromq.plugin.resourcethrottler;

import org.pf4j.util.StringUtils;

public class ResourceThrottlerException extends RuntimeException {

public ResourceThrottlerException(String message, Object... args) {
super(StringUtils.format(message, args));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.Timer;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -41,18 +42,24 @@ public class ResourceThrottlerManager implements IResourceThrottler, AutoCloseab
public ResourceThrottlerManager(String resourceThrottlerFQN, PluginManager pluginMgr) {
Map<String, IResourceThrottler> availResourceThrottlers =
pluginMgr.getExtensions(IResourceThrottler.class).stream()
.collect(Collectors.toMap(e -> e.getClass().getName(), e -> e));
.collect(Collectors.toMap(e -> e.getClass().getName(), e -> e,
(k,v) -> v, TreeMap::new));
if (availResourceThrottlers.isEmpty()) {
pluginLog.warn("No resource throttler plugin available, use DEV ONLY one instead");
delegate = new DevOnlyResourceThrottler();
} else {
if (resourceThrottlerFQN == null) {
pluginLog.warn("Resource throttler type class not specified, use DEV ONLY one instead");
delegate = new DevOnlyResourceThrottler();
if (availResourceThrottlers.size() > 1) {
pluginLog.info("Resource throttler plugin type not specified, use the first found");
}
String firstResourceThrottlerFQN = availResourceThrottlers.keySet().iterator().next();
pluginLog.info("Resource throttler plugin loaded: {}", firstResourceThrottlerFQN);
delegate = availResourceThrottlers.get(firstResourceThrottlerFQN);
} else if (!availResourceThrottlers.containsKey(resourceThrottlerFQN)) {
pluginLog.warn("Resource throttler type '{}' not found, use DEV ONLY one instead",
pluginLog.warn("Resource throttler type '{}' not found, so the system will shut down.",
resourceThrottlerFQN);
delegate = new DevOnlyResourceThrottler();
throw new ResourceThrottlerException("Resource throttler type '%s' not found, so the system will shut down.",
resourceThrottlerFQN);
} else {
pluginLog.info("Resource throttler loaded: {}", resourceThrottlerFQN);
delegate = availResourceThrottlers.get(resourceThrottlerFQN);
Expand Down
Loading
Loading