Skip to content
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

Add ability to deny service proxy values for places #505

Merged
merged 12 commits into from
Aug 24, 2023
2 changes: 1 addition & 1 deletion src/main/java/emissary/core/HDMobileAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ protected void switchPrimaryPayload(final int i) {
}

/**
* Do work now that we have arrived a the specified place
* Do work now that we have arrived at the specified place
*
* @param place the place we are asking to work for us
* @param payloadListArg list of IBaseDataObject for the place to operate on
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/emissary/core/MobileAgent.java
drivenflywheel marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ protected void agentControl(final IServiceProviderPlace currentPlaceArg) {
}

/**
* Do work now that we have arrived a the specified place
* Do work now that we have arrived at the specified place
*
* @param place the place we are asking to work for us
* @param payloadArg the data for the place to operate on
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/emissary/core/constants/Configurations.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class Configurations {
public static final String SERVICE_QUALITY = "SERVICE_QUALITY";
public static final String SERVICE_PROXY = "SERVICE_PROXY";
public static final String SERVICE_KEY = "SERVICE_KEY";
public static final String SERVICE_PROXY_DENY = "SERVICE_PROXY_DENY";

/**
* The list of reserved service config keys for service/place creation
Expand All @@ -34,6 +35,7 @@ public class Configurations {
SERVICE_KEY,
SERVICE_NAME,
SERVICE_PROXY,
SERVICE_PROXY_DENY,
SERVICE_QUALITY,
SERVICE_TYPE));

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/emissary/directory/DirectoryPlace.java
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,13 @@ protected List<DirectoryEntry> nextKeys(final String dataID, final IBaseDataObje
return Collections.emptyList();
}

// remove denied entries
currentList.removeIf(de -> de.getLocalPlace() != null && de.getLocalPlace().isDenied(payload.currentForm()));

if (currentList.isEmpty()) {
logger.debug("nextKeys - no non-DENIED entries found here for {}", dataID);
return Collections.emptyList();
}
// The list we are building for return to the caller
final List<DirectoryEntry> keyList = new ArrayList<>();

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/emissary/place/IServiceProviderPlace.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,8 @@ public interface IServiceProviderPlace {
*/
MobileAgent getAgent() throws NamespaceException;

/**
* Returns whether form is denied
*/
boolean isDenied(String s);
}
17 changes: 16 additions & 1 deletion src/main/java/emissary/place/ServiceProviderPlace.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import static emissary.core.constants.Configurations.SERVICE_KEY;
import static emissary.core.constants.Configurations.SERVICE_NAME;
import static emissary.core.constants.Configurations.SERVICE_PROXY;
import static emissary.core.constants.Configurations.SERVICE_PROXY_DENY;
import static emissary.core.constants.Configurations.SERVICE_QUALITY;
import static emissary.core.constants.Configurations.SERVICE_TYPE;

Expand Down Expand Up @@ -86,6 +87,11 @@ public abstract class ServiceProviderPlace implements emissary.place.IServicePro
*/
protected List<String> keys = new ArrayList<>();

/**
* List of denied places in SERVICE_PROXY_DENY
*/
protected List<String> denyList = new ArrayList<>();

// Items that are going to be deprecated, but here now to
// make the transition easier, for compatibility
protected String myKey = null;
Expand Down Expand Up @@ -293,7 +299,7 @@ protected void setupPlace(String theDir, String placeLocation) throws IOExceptio
}

/**
* Get a local reference to the directpry.
* Get a local reference to the directory.
*
* @param theDir key for the directory to use, if null will look up default name
* @return true if it worked
Expand Down Expand Up @@ -438,6 +444,11 @@ protected void configureServicePlace(@Nullable String placeLocation) throws IOEx
DirectoryEntry de = new DirectoryEntry(sp, serviceName, serviceType, locationPart, serviceDescription, serviceCost, serviceQuality);
keys.add(de.getFullKey());
}
// pick up the denied proxies(save full 4-tuple keys!)
for (String sp : configG.findEntries(SERVICE_PROXY_DENY)) {
DirectoryEntry de = new DirectoryEntry(sp, serviceName, serviceType, locationPart, serviceDescription, serviceCost, serviceQuality);
denyList.add(de.getDataType());
}
} else {
// May be configured the new way, but warn if there is a mixture of
// null and non-null items using the old-fashioned way. Perhaps the
Expand Down Expand Up @@ -1125,6 +1136,10 @@ protected IBaseDataObject getTLD() {
return null;
}

public boolean isDenied(String s) {
return denyList.contains(s);
}

/**
* This method should return a list of all the parameters that a subclass Place will potentially modify (this includes
* adding, removing, or changing the value of).
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/emissary/core/MobileAgentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

import emissary.admin.PlaceStarter;
import emissary.directory.DirectoryEntry;
import emissary.directory.DirectoryPlace;
import emissary.directory.EmissaryNode;
import emissary.place.IServiceProviderPlace;
import emissary.test.core.junit5.UnitTest;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -76,6 +81,38 @@ void testAddParrallelTrackingInfo() {
assertTrue(agent.visitedPlaces.containsAll(Arrays.asList("FOO", "FOOD")), "FOO and FOOD should have both been added");
}

@Test
void testDenyList() throws Exception {
HDMobileAgent agent = new MobAg2();
fbruton marked this conversation as resolved.
Show resolved Hide resolved

// test accepted
byte[] configDeniedData = ("PLACE_NAME = \"DelayPlace\"\n" + "SERVICE_NAME = \"DELAY\"\n"
+ "SERVICE_TYPE = \"ANALYZE\"\n" + "SERVICE_COST = 99\n" + "SERVICE_QUALITY = 50\n"
+ "SERVICE_PROXY = \"*\"\n" + "SERVICE_PROXY_DENY = \"FINI\"\n").getBytes();
InputStream config = new ByteArrayInputStream(configDeniedData);
DirectoryPlace place = new PlaceTest("http://example.com:8001/DelayPlace", config);
IBaseDataObject d1 = DataObjectFactory.getInstance();
d1.setCurrentForm("THECF");
agent.getNextKey(place, d1);

// test denied
byte[] configDeniedData2 = ("PLACE_NAME = \"DelayPlace2\"\n" + "SERVICE_NAME = \"DELAY2\"\n"
+ "SERVICE_TYPE = \"ANALYZE\"\n" + "SERVICE_COST = 99\n" + "SERVICE_QUALITY = 50\n"
+ "SERVICE_PROXY = \"*\"\n" + "SERVICE_PROXY_DENY = \"FINI\"\n").getBytes();
InputStream config2 = new ByteArrayInputStream(configDeniedData2);
DirectoryPlace place2 = new PlaceTest("http://example.com:8002/DelayPlace", config2);
IBaseDataObject d2 = DataObjectFactory.getInstance();
d2.setCurrentForm("FINI");
agent.getNextKey(place2, d2);

// verify
assertEquals(1, agent.visitedPlaces.size(), "DELAY2 should not have been added");
assertTrue(agent.visitedPlaces.contains("DELAY"), "Only DELAY should have been added");

agent.killAgent();
place.shutDown();
}

static final class MobAg extends HDMobileAgent {
static final long serialVersionUID = 102211824991899593L;

Expand All @@ -98,4 +135,12 @@ protected DirectoryEntry nextKeyFromDirectory(final String dataID, final IServic
}
}
}

class PlaceTest extends DirectoryPlace {
public PlaceTest(final String placeLoc, InputStream config) throws IOException {
super(config, placeLoc, new EmissaryNode());
}
}
static final class MobAg2 extends HDMobileAgent {
fbruton marked this conversation as resolved.
Show resolved Hide resolved
}
}
30 changes: 30 additions & 0 deletions src/test/java/emissary/place/ServiceProviderPlaceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ class ServiceProviderPlaceTest extends UnitTest {
private static final byte[] configBadKeyData = ("TGT_HOST = \"localhost\"\n" + "TGT_PORT = \"8001\"\n"
+ "SERVICE_KEY = \"TP4.TNAME.http://@{TGT_HOST}:@{TGT_PORT}/TPlaceName$8050\"\n" + "SERVICE_DESCRIPTION = \"bogus\"\n").getBytes();

private static final byte[] configDeniedData = ("PLACE_NAME = \"PlaceTest\"\n" + "SERVICE_NAME = \"TEST_SERVICE_NAME\"\n"
+ "SERVICE_TYPE = \"ANALYZE\"\n" + "SERVICE_DESCRIPTION = \"test place with denied list\"\n" + "SERVICE_COST = 60\n"
+ "SERVICE_QUALITY = 90\n" + "SERVICE_PROXY = \"TEST_SERVICE_PROXY\"\n" + "SERVICE_PROXY = \"TEST_SERVICE_PROXY2\"\n"
+ "SERVICE_PROXY_DENY = \"TEST_SERVICE_PROXY\"\n" + "SERVICE_PROXY_DENY = \"TEST_SERVICE_PROXY3\"\n"
+ "SERVICE_PROXY_DENY != \"TEST_SERVICE_PROXY3\"\n").getBytes();

private static final byte[] configDeniedData2 = ("PLACE_NAME = \"PlaceTest\"\n" + "SERVICE_NAME = \"TEST_SERVICE_NAME\"\n"
+ "SERVICE_TYPE = \"ANALYZE\"\n" + "SERVICE_DESCRIPTION = \"test place with denied list\"\n" + "SERVICE_COST = 60\n"
+ "SERVICE_QUALITY = 90\n" + "SERVICE_PROXY = \"TEST_SERVICE_PROXY\"\n"
+ "SERVICE_PROXY_DENY = \"TEST_SERVICE_PROXY\"\n" + "SERVICE_PROXY_DENY != \"*\"\n").getBytes();

String CFGDIR = System.getProperty(ConfigUtil.CONFIG_DIR_PROPERTY);

@Override
Expand Down Expand Up @@ -531,6 +542,25 @@ void testBogusKeyRemoval() {
}
}

@Test
void testDeniedServiceProxy() {
try {
InputStream config = new ByteArrayInputStream(configDeniedData);
IServiceProviderPlace p = new PlaceTest(config);
assertEquals("PlaceTest", p.getPlaceName(), "Configured place name");
assertTrue(p.isDenied("TEST_SERVICE_PROXY"), "TEST_SERVICE_PROXY should be denied");
assertTrue(!p.isDenied("TEST_SERVICE_PROXY2"), "TEST_SERVICE_PROXY2 should be allowed");
assertTrue(!p.isDenied("TEST_SERVICE_PROXY3"), "TEST_SERVICE_PROXY3 should be allowed");

InputStream config2 = new ByteArrayInputStream(configDeniedData2);
IServiceProviderPlace p2 = new PlaceTest(config2);
assertTrue(!p2.isDenied("TEST_SERVICE_PROXY"), "TEST_SERVICE_PROXY should be allowed");
assertTrue(!p2.isDenied("TEST_SERVICE_PROXY2"), "TEST_SERVICE_PROXY2 should be allowed");
} catch (IOException iox) {
fail("Place should have configured with SERVICE_KEY", iox);
}
}

private static final class PlaceTest extends ServiceProviderPlace {

public PlaceTest() throws IOException {
Expand Down