Skip to content

Commit

Permalink
Add an in-memory policy service (#170)
Browse files Browse the repository at this point in the history
* #167 Removing S3 policy service.

* #167 Updating test for outside path delete.

* #167 Removing unused class.

* #169 Adding in memory policy service.
  • Loading branch information
jzonthemtn authored Nov 29, 2024
1 parent 8fc8dee commit 479614f
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import ai.philterd.phileas.services.anonymization.cache.AnonymizationCacheServiceFactory;
import ai.philterd.phileas.services.disambiguation.VectorBasedSpanDisambiguationService;
import ai.philterd.phileas.services.metrics.NoOpMetricsService;
import ai.philterd.phileas.services.policies.InMemoryPolicyService;
import ai.philterd.phileas.services.policies.LocalPolicyService;
import ai.philterd.phileas.services.policies.utils.PolicyUtils;
import ai.philterd.phileas.services.postfilters.IgnoredPatternsFilter;
Expand Down Expand Up @@ -361,6 +362,16 @@ public BinaryDocumentFilterResponse filter(final List<String> policyNames, final

}

private PolicyService buildPolicyService(final PhileasConfiguration phileasConfiguration) {

if(StringUtils.equalsIgnoreCase(phileasConfiguration.policyService(), "memory")) {
return new InMemoryPolicyService();
} else {
return new LocalPolicyService(phileasConfiguration);
}

}

/**
* Get the bounding boxes from the policy for a given mime type.
* @param policy The policy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ public String policiesDirectory() {
return getProperty("filter.policies.directory", "./policies/");
}

public String policyService() {
return getProperty("filter.policies.service", "local");
}

private String getProperty(final String property, final String defaultValue) {

final String environmentVariableValue = getEnvironmentVariable(property);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2024 Philterd, LLC @ https://www.philterd.ai
*
* Licensed 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 ai.philterd.phileas.services.policies;

import ai.philterd.phileas.model.exceptions.api.BadRequestException;
import ai.philterd.phileas.model.services.AbstractPolicyService;
import ai.philterd.phileas.model.services.PolicyService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InMemoryPolicyService extends AbstractPolicyService implements PolicyService {

private static final Logger LOGGER = LogManager.getLogger(InMemoryPolicyService.class);

private final Map<String, String> policies;

public InMemoryPolicyService() {
this.policies = new HashMap<>();
}

@Override
public List<String> get() throws IOException {

return policies.keySet().stream().toList();

}

@Override
public String get(String policyName) throws IOException {

return policies.get(policyName);

}

@Override
public Map<String, String> getAll() throws IOException {

return policies;

}

@Override
public void save(String policyJson) {

try {

final JSONObject object = new JSONObject(policyJson);
final String policyName = object.getString("name");

policies.put(policyName, policyJson);

} catch (JSONException ex) {

LOGGER.error("The provided policy is not valid.", ex);
throw new BadRequestException("The provided policy is not valid.");

}

}

@Override
public void delete(String policyName) {

policies.remove(policyName);

}

}


This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Copyright 2024 Philterd, LLC @ https://www.philterd.ai
*
* Licensed 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 ai.philterd.test.phileas.services.policies;

import ai.philterd.phileas.model.policy.Identifiers;
import ai.philterd.phileas.model.policy.Policy;
import ai.philterd.phileas.model.policy.filters.Age;
import ai.philterd.phileas.model.policy.filters.CreditCard;
import ai.philterd.phileas.model.policy.filters.strategies.rules.AgeFilterStrategy;
import ai.philterd.phileas.model.policy.filters.strategies.rules.CreditCardFilterStrategy;
import ai.philterd.phileas.model.services.PolicyService;
import ai.philterd.phileas.services.policies.InMemoryPolicyService;
import ai.philterd.phileas.services.policies.LocalPolicyService;
import com.google.gson.Gson;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class InMemoryPolicyServiceTest {

private final Gson gson = new Gson();

@Test
public void list() throws IOException {

final PolicyService policyService = new InMemoryPolicyService();

policyService.save(gson.toJson(getPolicy("name1")));
policyService.save(gson.toJson(getPolicy("name2")));

final List<String> names = policyService.get();

Assertions.assertEquals(2, names.size());
Assertions.assertTrue(names.contains("name1"));
Assertions.assertTrue(names.contains("name2"));

}

@Test
public void getAll() throws IOException {

final PolicyService policyService = new InMemoryPolicyService();

policyService.save(gson.toJson(getPolicy("name1")));
policyService.save(gson.toJson(getPolicy("name2")));

final Map<String, String> all = policyService.getAll();

Assertions.assertEquals(2, all.size());
Assertions.assertTrue(all.containsKey("name1"));
Assertions.assertTrue(all.containsKey("name2"));

}

@Test
public void save() throws IOException {

final String name = "default";

final String policy = gson.toJson(getPolicy(name));

final PolicyService policyService = new InMemoryPolicyService();

policyService.save(policy);

final String saved = policyService.get("default");

Assertions.assertNotNull(saved);
Assertions.assertEquals(policy, saved);

}

@Test
public void get() throws IOException {

final String name = "default";

final String policy = gson.toJson(getPolicy(name));

final PolicyService policyService = new InMemoryPolicyService();

policyService.save(policy);

final String policyJson = policyService.get(name);

Assertions.assertEquals(policy, policyJson);

}

@Test
public void delete() throws IOException {

final String name = "default";
final String policy = gson.toJson(getPolicy(name));

final PolicyService policyService = new InMemoryPolicyService();

policyService.save(policy);

policyService.delete(name);

Assertions.assertFalse(policyService.getAll().containsKey(name));

}

private Policy getPolicy(String name) {

AgeFilterStrategy ageFilterStrategy = new AgeFilterStrategy();

Age age = new Age();
age.setAgeFilterStrategies(List.of(ageFilterStrategy));

CreditCardFilterStrategy creditCardFilterStrategy = new CreditCardFilterStrategy();

CreditCard creditCard = new CreditCard();
creditCard.setCreditCardFilterStrategies(List.of(creditCardFilterStrategy));

Identifiers identifiers = new Identifiers();

identifiers.setAge(age);

Policy policy = new Policy();
policy.setName(name);
policy.setIdentifiers(identifiers);

return policy;

}

}
Loading

0 comments on commit 479614f

Please sign in to comment.