diff --git a/CHANGELOG.md b/CHANGELOG.md index 32ecca2..aa2b0ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,10 +2,16 @@ All notable changes to this project will be documented in this file. +## [0.0.1-RC5] + +- Introduced a PermissionValidator and introduced suppliers for versionGenerator, SchemaUpdater and PermissionValidator, to facilitate clients implement them via Dependency Injection +- Removed the unnecessary `LeiaMessages` data-model and converged to a Map Structure +- A few linting fixes, along the way + ## [0.0.1-RC4] - Periodic Refresh - Added support to enable/disable periodicRefresh. Default value is true -- Event Multiplexing - Added support for event transformations. Can multiplex one event into multiple events. +- Event Multiplexing - Added support for event transformations. Can multiplex one event into multiple events. ## [0.0.1-RC3] diff --git a/README.md b/README.md index 6559081..8a4b1c3 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ Leia is a governance and metadata framework aimed at meeting compliance requirem ### Using the schema registry -#### Build your own dropwizard schema server by using the `LeiaElastic` bundle. +#### Build your own dropwizard schema server by using the `LeiaElastic` bundle. ``` new LeiaElasticBundle() { @@ -71,9 +71,10 @@ Leia is a governance and metadata framework aimed at meeting compliance requirem } ``` - + - **SchemaUpdater** is an RBAC governing class, please extend this SchemaUpdater to implement your own UserProfile -- **CacheConfig** - If the schema will be always resolved from the dataStore (Elasticsearch) or from the in-memory cache with a refreshInterval to refresh the data +- **CacheConfig** - If the schema will be always resolved from the dataStore (Elasticsearch) or from the in-memory cache + with a refreshInterval to refresh the data - **VersionIdGenerator** - Your own version id generator, to generate a unique versionId for every document - **ElasticConfig** - Elasticsearch configuration to bring up the schema server @@ -126,7 +127,10 @@ A sample schema looks like the following ``` - **AttributeInfo** : There are various type of attributes you can define, please refer to the `SchemaAttribute` class. -- **TransformationTargets** - Helps in event multiplexing, in the above example, when provided with the namespace, `testNamespace` and schemaName, `testSchema` with version `V1234`, during message production the `LeiaMessageProduceClient`, will multiplex the testSchema to both the versions, the transformationTargets ought to be jsonPathRules. +- **TransformationTargets** - Helps in event multiplexing, in the above example, when provided with the + namespace, `testNamespace` and schemaName, `testSchema` with version `V1234`, during message production + the `LeiaMessageProduceClient`, will multiplex the testSchema to both the versions, the transformationTargets ought to + be jsonPathRules. #### Using the LeiaClientBundle diff --git a/leia-bom/pom.xml b/leia-bom/pom.xml index 04b3022..48073d7 100644 --- a/leia-bom/pom.xml +++ b/leia-bom/pom.xml @@ -21,7 +21,7 @@ com.grookage.leia leia - 0.0.1-RC4 + 0.0.1-RC5 leia-bom diff --git a/leia-client-dropwizard/pom.xml b/leia-client-dropwizard/pom.xml index 7f1f27c..1c893bf 100644 --- a/leia-client-dropwizard/pom.xml +++ b/leia-client-dropwizard/pom.xml @@ -22,7 +22,7 @@ com.grookage.leia leia-parent - 0.0.1-RC4 + 0.0.1-RC5 ../leia-parent diff --git a/leia-client/pom.xml b/leia-client/pom.xml index 517b8d5..6b590d0 100644 --- a/leia-client/pom.xml +++ b/leia-client/pom.xml @@ -22,7 +22,7 @@ com.grookage.leia leia-parent - 0.0.1-RC4 + 0.0.1-RC5 ../leia-parent diff --git a/leia-client/src/main/java/com/grookage/leia/client/LeiaMessageProduceClient.java b/leia-client/src/main/java/com/grookage/leia/client/LeiaMessageProduceClient.java index fed18e0..589fce7 100644 --- a/leia-client/src/main/java/com/grookage/leia/client/LeiaMessageProduceClient.java +++ b/leia-client/src/main/java/com/grookage/leia/client/LeiaMessageProduceClient.java @@ -17,7 +17,6 @@ package com.grookage.leia.client; import com.grookage.leia.models.mux.LeiaMessage; -import com.grookage.leia.models.mux.LeiaMessages; import com.grookage.leia.models.schema.SchemaKey; import com.grookage.leia.models.schema.transformer.TransformationTarget; import com.jayway.jsonpath.DocumentContext; @@ -33,6 +32,7 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.Optional; +import java.util.function.UnaryOperator; @EqualsAndHashCode(callSuper = true) @@ -80,18 +80,17 @@ private Optional getJsonPath(SchemaKey schemaKey, String attributeName Optional.ofNullable(compiledPaths.get(schemaKey).get(attributeName)) : Optional.empty(); } - @SneakyThrows - public LeiaMessages getMessages(SchemaKey schemaKey, byte[] sourceMessage) { + private Map getMessages(SchemaKey schemaKey, byte[] sourceMessage) { + final var messages = new HashMap(); + messages.put(schemaKey, LeiaMessage.builder() + .schemaKey(schemaKey) + .message(sourceMessage) + .build() + ); final var sourceSchemaDetails = super.getSchemaDetails() .stream().filter(each -> each.getSchemaKey().equals(schemaKey)) .findFirst().orElse(null); - final var messages = new LeiaMessages(); - messages.add( - LeiaMessage.builder() - .schemaKey(schemaKey) - .message(sourceMessage) - .build() - ); + final var transformationTargets = null == sourceSchemaDetails ? null : sourceSchemaDetails.getTransformationTargets(); if (null == transformationTargets) { @@ -99,10 +98,17 @@ public LeiaMessages getMessages(SchemaKey schemaKey, byte[] sourceMessage) { } final var documentContext = JsonPath.parse(new String(sourceMessage)); transformationTargets.forEach(transformationTarget -> - createMessage(documentContext, transformationTarget).ifPresent(messages::add)); + createMessage(documentContext, transformationTarget).ifPresent(message -> + messages.put(message.getSchemaKey(), message))); return messages; } + public void processMessages(final SchemaKey schemaKey, + final byte[] sourceMessage, + final UnaryOperator> messageHandler) { + messageHandler.apply(getMessages(schemaKey, sourceMessage)); + } + @Override public void start() { super.getSchemaDetails().forEach(schemaDetails -> { diff --git a/leia-client/src/test/java/com/grookage/leia/client/LeiaMessageProduceClientTest.java b/leia-client/src/test/java/com/grookage/leia/client/LeiaMessageProduceClientTest.java index 7650cda..4236d3c 100644 --- a/leia-client/src/test/java/com/grookage/leia/client/LeiaMessageProduceClientTest.java +++ b/leia-client/src/test/java/com/grookage/leia/client/LeiaMessageProduceClientTest.java @@ -70,8 +70,10 @@ void testLeiaMessageProduceClient() { .schemaUnits(List.of(TestSchemaUnit.builder() .registeredName("testRegisteredName").build())) .build(); - final var messages = schemaClient.getMessages(sourceSchema, mapper.writeValueAsBytes(testSchema)); - Assertions.assertFalse(messages.getMessages().isEmpty()); - Assertions.assertEquals(2, messages.getMessages().size()); + schemaClient.processMessages(sourceSchema, mapper.writeValueAsBytes(testSchema), messages -> { + Assertions.assertFalse(messages.isEmpty()); + Assertions.assertEquals(2, messages.size()); + return messages; + }); } } diff --git a/leia-core/pom.xml b/leia-core/pom.xml index 50c1e7c..3aea71b 100644 --- a/leia-core/pom.xml +++ b/leia-core/pom.xml @@ -21,7 +21,7 @@ com.grookage.leia leia-parent - 0.0.1-RC4 + 0.0.1-RC5 ../leia-parent diff --git a/leia-core/src/main/java/com/grookage/leia/core/ingestion/hub/SchemaProcessorHub.java b/leia-core/src/main/java/com/grookage/leia/core/ingestion/hub/SchemaProcessorHub.java index 9d08a40..3232803 100644 --- a/leia-core/src/main/java/com/grookage/leia/core/ingestion/hub/SchemaProcessorHub.java +++ b/leia-core/src/main/java/com/grookage/leia/core/ingestion/hub/SchemaProcessorHub.java @@ -28,13 +28,14 @@ import java.util.Arrays; import java.util.Map; import java.util.Optional; +import java.util.function.Supplier; @Slf4j public class SchemaProcessorHub { private final Map processors = Maps.newHashMap(); private SchemaRepository schemaRepository; - private VersionIDGenerator versionIDGenerator; + private Supplier versionSupplier; private SchemaProcessorHub() { @@ -49,14 +50,14 @@ public SchemaProcessorHub withSchemaRepository(SchemaRepository schemaRepository return this; } - public SchemaProcessorHub withVersionIDGenerator(VersionIDGenerator versionIDGenerator) { - this.versionIDGenerator = versionIDGenerator; + public SchemaProcessorHub wtihVersionSupplier(Supplier versionSupplier) { + this.versionSupplier = versionSupplier; return this; } public SchemaProcessorHub build() { Preconditions.checkNotNull(schemaRepository, "Schema Repository can't be null"); - Preconditions.checkNotNull(versionIDGenerator, "Version ID Generator can't be null"); + Preconditions.checkNotNull(versionSupplier, "Version ID Generator can't be null"); Arrays.stream(SchemaEvent.values()).forEach(this::buildProcessor); return this; } @@ -67,7 +68,7 @@ public void buildProcessor(final SchemaEvent event) { public SchemaProcessor schemaCreate() { return CreateSchemaProcessor.builder() .schemaRepository(schemaRepository) - .versionIDGenerator(versionIDGenerator) + .versionSupplier(versionSupplier) .build(); } @@ -75,7 +76,7 @@ public SchemaProcessor schemaCreate() { public SchemaProcessor schemaUpdate() { return UpdateSchemaProcessor.builder() .schemaRepository(schemaRepository) - .versionIDGenerator(versionIDGenerator) + .versionSupplier(versionSupplier) .build(); } @@ -83,7 +84,7 @@ public SchemaProcessor schemaUpdate() { public SchemaProcessor schemaApprove() { return ApproveSchemaProcessor.builder() .schemaRepository(schemaRepository) - .versionIDGenerator(versionIDGenerator) + .versionSupplier(versionSupplier) .build(); } @@ -91,7 +92,7 @@ public SchemaProcessor schemaApprove() { public SchemaProcessor schemaReject() { return RejectSchemaProcessor.builder() .schemaRepository(schemaRepository) - .versionIDGenerator(versionIDGenerator) + .versionSupplier(versionSupplier) .build(); } })); diff --git a/leia-core/src/main/java/com/grookage/leia/core/ingestion/processors/CreateSchemaProcessor.java b/leia-core/src/main/java/com/grookage/leia/core/ingestion/processors/CreateSchemaProcessor.java index ea19eba..f31bcae 100644 --- a/leia-core/src/main/java/com/grookage/leia/core/ingestion/processors/CreateSchemaProcessor.java +++ b/leia-core/src/main/java/com/grookage/leia/core/ingestion/processors/CreateSchemaProcessor.java @@ -59,7 +59,7 @@ public void process(SchemaContext context) { } final var userName = ContextUtils.getUser(context); final var email = ContextUtils.getEmail(context); - final var schemaDetails = SchemaUtils.toSchemaDetails(createSchemaRequest, userName, email, getVersionIDGenerator()); + final var schemaDetails = SchemaUtils.toSchemaDetails(createSchemaRequest, userName, email, getVersionSupplier()); getSchemaRepository().create(schemaDetails); context.addContext(SchemaDetails.class.getSimpleName(), schemaDetails); } diff --git a/leia-core/src/main/java/com/grookage/leia/core/ingestion/processors/SchemaProcessor.java b/leia-core/src/main/java/com/grookage/leia/core/ingestion/processors/SchemaProcessor.java index fe68426..3b058cb 100644 --- a/leia-core/src/main/java/com/grookage/leia/core/ingestion/processors/SchemaProcessor.java +++ b/leia-core/src/main/java/com/grookage/leia/core/ingestion/processors/SchemaProcessor.java @@ -24,13 +24,15 @@ import lombok.Data; import lombok.experimental.SuperBuilder; +import java.util.function.Supplier; + @SuperBuilder @Data @AllArgsConstructor public abstract class SchemaProcessor { private final SchemaRepository schemaRepository; - private final VersionIDGenerator versionIDGenerator; + private final Supplier versionSupplier; public abstract SchemaEvent name(); diff --git a/leia-core/src/main/java/com/grookage/leia/core/ingestion/utils/ContextUtils.java b/leia-core/src/main/java/com/grookage/leia/core/ingestion/utils/ContextUtils.java index a341045..9d95a7f 100644 --- a/leia-core/src/main/java/com/grookage/leia/core/ingestion/utils/ContextUtils.java +++ b/leia-core/src/main/java/com/grookage/leia/core/ingestion/utils/ContextUtils.java @@ -48,5 +48,4 @@ public static String getEmail(final SchemaContext schemaContext) { return schemaContext.getValue(EMAIL) .orElseThrow((Supplier) () -> LeiaException.error(LeiaErrorCode.VALUE_NOT_FOUND)); } - } diff --git a/leia-core/src/main/java/com/grookage/leia/core/ingestion/utils/SchemaUtils.java b/leia-core/src/main/java/com/grookage/leia/core/ingestion/utils/SchemaUtils.java index d960877..f9219b4 100644 --- a/leia-core/src/main/java/com/grookage/leia/core/ingestion/utils/SchemaUtils.java +++ b/leia-core/src/main/java/com/grookage/leia/core/ingestion/utils/SchemaUtils.java @@ -24,18 +24,20 @@ import com.grookage.leia.models.schema.ingestion.CreateSchemaRequest; import lombok.experimental.UtilityClass; +import java.util.function.Supplier; + @UtilityClass public class SchemaUtils { public SchemaDetails toSchemaDetails(final CreateSchemaRequest createSchemaRequest, final String userName, final String email, - final VersionIDGenerator versionIDGenerator) { + final Supplier versionSupplier) { return SchemaDetails.builder() .schemaKey(SchemaKey.builder() .namespace(createSchemaRequest.getNamespace()) .schemaName(createSchemaRequest.getSchemaName()) - .version(versionIDGenerator.generateVersionId("V")) + .version(versionSupplier.get().generateVersionId("V")) .build() ) .schemaState(SchemaState.CREATED) diff --git a/leia-core/src/test/java/com/grookage/leia/core/ingestion/hub/SchemaProcessorHubTest.java b/leia-core/src/test/java/com/grookage/leia/core/ingestion/hub/SchemaProcessorHubTest.java index c8979fb..ba53917 100644 --- a/leia-core/src/test/java/com/grookage/leia/core/ingestion/hub/SchemaProcessorHubTest.java +++ b/leia-core/src/test/java/com/grookage/leia/core/ingestion/hub/SchemaProcessorHubTest.java @@ -36,11 +36,11 @@ public String generateVersionId(String prefix) { }; final var hub = SchemaProcessorHub.of() .withSchemaRepository(schemaRepository) - .withVersionIDGenerator(generator) + .wtihVersionSupplier(() -> generator) .build(); Assertions.assertNotNull(hub.getProcessor(SchemaEvent.CREATE_SCHEMA).orElse(null)); Assertions.assertThrows(NullPointerException.class, () -> SchemaProcessorHub.of() - .withVersionIDGenerator(generator) + .wtihVersionSupplier(() -> generator) .build()); Assertions.assertThrows(NullPointerException.class, () -> SchemaProcessorHub.of() .withSchemaRepository(schemaRepository) diff --git a/leia-core/src/test/java/com/grookage/leia/core/ingestion/processors/ApproveSchemaProcessorTest.java b/leia-core/src/test/java/com/grookage/leia/core/ingestion/processors/ApproveSchemaProcessorTest.java index 4514cec..064f20b 100644 --- a/leia-core/src/test/java/com/grookage/leia/core/ingestion/processors/ApproveSchemaProcessorTest.java +++ b/leia-core/src/test/java/com/grookage/leia/core/ingestion/processors/ApproveSchemaProcessorTest.java @@ -34,7 +34,7 @@ class ApproveSchemaProcessorTest extends SchemaProcessorTest { SchemaProcessor getSchemaProcessor() { return ApproveSchemaProcessor.builder() .schemaRepository(getSchemaRepository()) - .versionIDGenerator(getGenerator()) + .versionSupplier(getGenerator()) .build(); } diff --git a/leia-core/src/test/java/com/grookage/leia/core/ingestion/processors/CreateSchemaProcessorTest.java b/leia-core/src/test/java/com/grookage/leia/core/ingestion/processors/CreateSchemaProcessorTest.java index d75eb03..bd35fa8 100644 --- a/leia-core/src/test/java/com/grookage/leia/core/ingestion/processors/CreateSchemaProcessorTest.java +++ b/leia-core/src/test/java/com/grookage/leia/core/ingestion/processors/CreateSchemaProcessorTest.java @@ -33,7 +33,7 @@ class CreateSchemaProcessorTest extends SchemaProcessorTest { @Override SchemaProcessor getSchemaProcessor() { return CreateSchemaProcessor.builder() - .versionIDGenerator(getGenerator()) + .versionSupplier(getGenerator()) .schemaRepository(getSchemaRepository()) .build(); } diff --git a/leia-core/src/test/java/com/grookage/leia/core/ingestion/processors/RejectSchemaProcessorTest.java b/leia-core/src/test/java/com/grookage/leia/core/ingestion/processors/RejectSchemaProcessorTest.java index 7530ab2..40589db 100644 --- a/leia-core/src/test/java/com/grookage/leia/core/ingestion/processors/RejectSchemaProcessorTest.java +++ b/leia-core/src/test/java/com/grookage/leia/core/ingestion/processors/RejectSchemaProcessorTest.java @@ -34,7 +34,7 @@ public class RejectSchemaProcessorTest extends SchemaProcessorTest { SchemaProcessor getSchemaProcessor() { return RejectSchemaProcessor.builder() .schemaRepository(getSchemaRepository()) - .versionIDGenerator(getGenerator()) + .versionSupplier(getGenerator()) .build(); } diff --git a/leia-core/src/test/java/com/grookage/leia/core/ingestion/processors/SchemaProcessorTest.java b/leia-core/src/test/java/com/grookage/leia/core/ingestion/processors/SchemaProcessorTest.java index 44d1636..302ea2e 100644 --- a/leia-core/src/test/java/com/grookage/leia/core/ingestion/processors/SchemaProcessorTest.java +++ b/leia-core/src/test/java/com/grookage/leia/core/ingestion/processors/SchemaProcessorTest.java @@ -22,15 +22,12 @@ import org.junit.jupiter.api.BeforeEach; import org.mockito.Mockito; +import java.util.function.Supplier; + public abstract class SchemaProcessorTest { @Getter - private static final VersionIDGenerator generator = new VersionIDGenerator() { - @Override - public String generateVersionId(String prefix) { - return "V1234"; - } - }; + private static final Supplier generator = () -> prefix -> "V1234"; @Getter private static SchemaRepository schemaRepository; private static SchemaProcessor schemaProcessor; diff --git a/leia-core/src/test/java/com/grookage/leia/core/ingestion/processors/UpdateSchemaProcessorTest.java b/leia-core/src/test/java/com/grookage/leia/core/ingestion/processors/UpdateSchemaProcessorTest.java index 36941bc..86ac846 100644 --- a/leia-core/src/test/java/com/grookage/leia/core/ingestion/processors/UpdateSchemaProcessorTest.java +++ b/leia-core/src/test/java/com/grookage/leia/core/ingestion/processors/UpdateSchemaProcessorTest.java @@ -70,7 +70,7 @@ void testUpdateSchemasNoDetails() { @Override SchemaProcessor getSchemaProcessor() { return UpdateSchemaProcessor.builder() - .versionIDGenerator(getGenerator()) + .versionSupplier(getGenerator()) .schemaRepository(getSchemaRepository()) .build(); } diff --git a/leia-core/src/test/java/com/grookage/leia/core/ingestion/utils/ContextUtilsTest.java b/leia-core/src/test/java/com/grookage/leia/core/ingestion/utils/ContextUtilsTest.java index 80625ad..fe2fb00 100644 --- a/leia-core/src/test/java/com/grookage/leia/core/ingestion/utils/ContextUtilsTest.java +++ b/leia-core/src/test/java/com/grookage/leia/core/ingestion/utils/ContextUtilsTest.java @@ -41,6 +41,11 @@ public String name() { public String email() { return "email"; } + + @Override + public String namespace() { + return "namespace"; + } }); Assertions.assertNotNull(ContextUtils.getEmail(schemaContext)); Assertions.assertNotNull(ContextUtils.getUser(schemaContext)); diff --git a/leia-core/src/test/java/com/grookage/leia/core/ingestion/utils/SchemaUtilsTest.java b/leia-core/src/test/java/com/grookage/leia/core/ingestion/utils/SchemaUtilsTest.java index 6a655a7..80ffbb9 100644 --- a/leia-core/src/test/java/com/grookage/leia/core/ingestion/utils/SchemaUtilsTest.java +++ b/leia-core/src/test/java/com/grookage/leia/core/ingestion/utils/SchemaUtilsTest.java @@ -24,6 +24,8 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.util.function.Supplier; + class SchemaUtilsTest { @Test @@ -40,7 +42,7 @@ public String generateVersionId(String prefix) { } }; final var schemaDetails = SchemaUtils.toSchemaDetails(createSchemaRequest, "testUser", - "testEmail", generator + "testEmail", () -> generator ); Assertions.assertNotNull(schemaDetails); final var schemaKey = schemaDetails.getSchemaKey(); diff --git a/leia-core/src/test/java/com/grookage/leia/core/stubs/StubbedSchemaUpdater.java b/leia-core/src/test/java/com/grookage/leia/core/stubs/StubbedSchemaUpdater.java index 938c638..31475ae 100644 --- a/leia-core/src/test/java/com/grookage/leia/core/stubs/StubbedSchemaUpdater.java +++ b/leia-core/src/test/java/com/grookage/leia/core/stubs/StubbedSchemaUpdater.java @@ -34,4 +34,10 @@ public String name() { public String email() { return "name@grookage.com"; } + + @Override + public String namespace() { + return "grookage"; + } + } diff --git a/leia-dropwizard-es/pom.xml b/leia-dropwizard-es/pom.xml index 96d3053..5cb8fcb 100644 --- a/leia-dropwizard-es/pom.xml +++ b/leia-dropwizard-es/pom.xml @@ -22,7 +22,7 @@ com.grookage.leia leia-parent - 0.0.1-RC4 + 0.0.1-RC5 ../leia-parent diff --git a/leia-dropwizard-es/src/main/java/com/grookage/leia/es/dropwizard/LeiaElasticBundle.java b/leia-dropwizard-es/src/main/java/com/grookage/leia/es/dropwizard/LeiaElasticBundle.java index 4fd5bed..7091943 100644 --- a/leia-dropwizard-es/src/main/java/com/grookage/leia/es/dropwizard/LeiaElasticBundle.java +++ b/leia-dropwizard-es/src/main/java/com/grookage/leia/es/dropwizard/LeiaElasticBundle.java @@ -17,15 +17,12 @@ package com.grookage.leia.es.dropwizard; import co.elastic.clients.elasticsearch.ElasticsearchClient; -import com.grookage.leia.core.ingestion.VersionIDGenerator; import com.grookage.leia.dropwizard.bundle.LeiaBundle; import com.grookage.leia.dropwizard.bundle.health.LeiaHealthCheck; -import com.grookage.leia.dropwizard.bundle.resolvers.SchemaUpdaterResolver; import com.grookage.leia.elastic.config.ElasticConfig; import com.grookage.leia.elastic.repository.ElasticRepository; import com.grookage.leia.models.user.SchemaUpdater; import com.grookage.leia.repository.SchemaRepository; -import com.grookage.leia.repository.config.CacheConfig; import io.dropwizard.Configuration; import io.dropwizard.setup.Environment; import lombok.Getter; diff --git a/leia-dropwizard/pom.xml b/leia-dropwizard/pom.xml index 263dc9e..d5f0326 100644 --- a/leia-dropwizard/pom.xml +++ b/leia-dropwizard/pom.xml @@ -21,7 +21,7 @@ com.grookage.leia leia-parent - 0.0.1-RC4 + 0.0.1-RC5 ../leia-parent diff --git a/leia-dropwizard/src/main/java/com/grookage/leia/dropwizard/bundle/LeiaBundle.java b/leia-dropwizard/src/main/java/com/grookage/leia/dropwizard/bundle/LeiaBundle.java index 058096e..9f092e9 100644 --- a/leia-dropwizard/src/main/java/com/grookage/leia/dropwizard/bundle/LeiaBundle.java +++ b/leia-dropwizard/src/main/java/com/grookage/leia/dropwizard/bundle/LeiaBundle.java @@ -25,6 +25,7 @@ import com.grookage.leia.dropwizard.bundle.lifecycle.Lifecycle; import com.grookage.leia.dropwizard.bundle.mapper.LeiaExceptionMapper; import com.grookage.leia.dropwizard.bundle.mapper.LeiaRefresherMapper; +import com.grookage.leia.dropwizard.bundle.permissions.PermissionValidator; import com.grookage.leia.dropwizard.bundle.resolvers.SchemaUpdaterResolver; import com.grookage.leia.dropwizard.bundle.resources.IngestionResource; import com.grookage.leia.dropwizard.bundle.resources.SchemaResource; @@ -40,6 +41,7 @@ import lombok.NoArgsConstructor; import java.util.List; +import java.util.function.Supplier; @NoArgsConstructor @Getter @@ -49,13 +51,16 @@ public abstract class LeiaBundle userResolver(T configuration); + protected abstract Supplier> userResolver(T configuration); protected abstract CacheConfig getCacheConfig(T configuration); protected abstract SchemaRepository getSchemaRepository(T configuration); - protected abstract VersionIDGenerator getVersionIDGenerator(); + protected abstract Supplier getVersionSupplier(); + + + protected abstract Supplier> getPermissionResolver(T configuration); protected List withHealthChecks(T configuration) { return List.of(); @@ -69,10 +74,13 @@ protected List withLifecycleManagers(T configuration) { public void run(T configuration, Environment environment) { final var userResolver = userResolver(configuration); Preconditions.checkNotNull(userResolver, "User Resolver can't be null"); + final var permissionResolver = getPermissionResolver(configuration); + Preconditions.checkNotNull(permissionResolver, "Permission Resolver can't be null"); + this.schemaRepository = getSchemaRepository(configuration); final var schemaProcessorHub = SchemaProcessorHub.of() .withSchemaRepository(schemaRepository) - .withVersionIDGenerator(getVersionIDGenerator()) + .wtihVersionSupplier(getVersionSupplier()) .build(); this.schemaIngestor = new SchemaIngestor() .withProcessorHub(schemaProcessorHub) @@ -93,7 +101,7 @@ public void stop() { })); withHealthChecks(configuration) .forEach(leiaHealthCheck -> environment.healthChecks().register(leiaHealthCheck.getName(), leiaHealthCheck)); - environment.jersey().register(new IngestionResource<>(schemaIngestor, userResolver)); + environment.jersey().register(new IngestionResource<>(schemaIngestor, userResolver, permissionResolver)); environment.jersey().register(new SchemaResource(schemaRetriever)); environment.jersey().register(new LeiaExceptionMapper()); environment.jersey().register(new LeiaRefresherMapper()); diff --git a/leia-dropwizard/src/main/java/com/grookage/leia/dropwizard/bundle/permissions/PermissionValidator.java b/leia-dropwizard/src/main/java/com/grookage/leia/dropwizard/bundle/permissions/PermissionValidator.java new file mode 100644 index 0000000..d97f33b --- /dev/null +++ b/leia-dropwizard/src/main/java/com/grookage/leia/dropwizard/bundle/permissions/PermissionValidator.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2024. Koushik R . + * + * 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 com.grookage.leia.dropwizard.bundle.permissions; + +import com.grookage.leia.models.schema.SchemaKey; +import com.grookage.leia.models.schema.ingestion.CreateSchemaRequest; +import com.grookage.leia.models.schema.ingestion.UpdateSchemaRequest; +import com.grookage.leia.models.user.SchemaUpdater; + +import javax.ws.rs.core.HttpHeaders; + +public interface PermissionValidator { + + void validateSchemaCreation(final HttpHeaders headers, + final U schemaUpdater, + final CreateSchemaRequest schemaRequest); + + void validationSchemaModification(final HttpHeaders headers, + final U schemaUpdater, + final UpdateSchemaRequest schemaRequest); + + void validateSchemaApproval(final HttpHeaders headers, + final U schemaUpdater, + final SchemaKey schemaKey); + + void validateSchemaRejection(final HttpHeaders headers, + final U schemaUpdater, + final SchemaKey schemaKey); + +} diff --git a/leia-models/src/main/java/com/grookage/leia/models/mux/LeiaMessages.java b/leia-dropwizard/src/main/java/com/grookage/leia/dropwizard/bundle/permissions/SchemaPermission.java similarity index 59% rename from leia-models/src/main/java/com/grookage/leia/models/mux/LeiaMessages.java rename to leia-dropwizard/src/main/java/com/grookage/leia/dropwizard/bundle/permissions/SchemaPermission.java index 6bdf253..f2aaa0b 100644 --- a/leia-models/src/main/java/com/grookage/leia/models/mux/LeiaMessages.java +++ b/leia-dropwizard/src/main/java/com/grookage/leia/dropwizard/bundle/permissions/SchemaPermission.java @@ -14,25 +14,11 @@ * limitations under the License. */ -package com.grookage.leia.models.mux; +package com.grookage.leia.dropwizard.bundle.permissions; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; +public enum SchemaPermission { -import java.util.ArrayList; -import java.util.List; + S_WRITE, -@Data -@Builder -@NoArgsConstructor -@AllArgsConstructor -public class LeiaMessages { - - private List messages = new ArrayList<>(); - - public void add(LeiaMessage leiaMessage) { - messages.add(leiaMessage); - } + S_ADMIN } diff --git a/leia-dropwizard/src/main/java/com/grookage/leia/dropwizard/bundle/resources/HealthCheckResource.java b/leia-dropwizard/src/main/java/com/grookage/leia/dropwizard/bundle/resources/HealthCheckResource.java new file mode 100644 index 0000000..61a41a0 --- /dev/null +++ b/leia-dropwizard/src/main/java/com/grookage/leia/dropwizard/bundle/resources/HealthCheckResource.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2024. Koushik R . + * + * 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 com.grookage.leia.dropwizard.bundle.resources; + +import com.codahale.metrics.annotation.Timed; +import lombok.Getter; +import lombok.Setter; +import lombok.extern.slf4j.Slf4j; + +import javax.inject.Singleton; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +@Singleton +@Getter +@Setter +@Path("/") +@Slf4j +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class HealthCheckResource { + + @Path("healthcheck") + @GET + @Timed + public Response checkHealth() { + return Response.ok().build(); + } +} diff --git a/leia-dropwizard/src/main/java/com/grookage/leia/dropwizard/bundle/resources/IngestionResource.java b/leia-dropwizard/src/main/java/com/grookage/leia/dropwizard/bundle/resources/IngestionResource.java index 6767471..3726bd4 100644 --- a/leia-dropwizard/src/main/java/com/grookage/leia/dropwizard/bundle/resources/IngestionResource.java +++ b/leia-dropwizard/src/main/java/com/grookage/leia/dropwizard/bundle/resources/IngestionResource.java @@ -19,6 +19,7 @@ import com.codahale.metrics.annotation.ExceptionMetered; import com.codahale.metrics.annotation.Timed; import com.grookage.leia.core.ingestion.SchemaIngestor; +import com.grookage.leia.dropwizard.bundle.permissions.PermissionValidator; import com.grookage.leia.dropwizard.bundle.resolvers.SchemaUpdaterResolver; import com.grookage.leia.models.GenericResponse; import com.grookage.leia.models.schema.SchemaDetails; @@ -31,12 +32,14 @@ import lombok.Setter; import lombok.extern.slf4j.Slf4j; +import javax.annotation.security.PermitAll; import javax.inject.Singleton; import javax.validation.Valid; import javax.ws.rs.*; import javax.ws.rs.core.Context; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; +import java.util.function.Supplier; @Singleton @Getter @@ -46,10 +49,12 @@ @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @AllArgsConstructor +@PermitAll public class IngestionResource { private final SchemaIngestor schemaIngestor; - private final SchemaUpdaterResolver updaterResolver; + private final Supplier> updaterResolver; + private final Supplier> permissionValidatorSupplier; @PUT @Timed @@ -57,7 +62,8 @@ public class IngestionResource { @Path("/add") public GenericResponse addSchema(@Context HttpHeaders headers, @Valid final CreateSchemaRequest schemaRequest) { - final var updater = updaterResolver.resolve(headers); + final var updater = updaterResolver.get().resolve(headers); + permissionValidatorSupplier.get().validateSchemaCreation(headers, updater, schemaRequest); return GenericResponse.builder() .success(true) .data(schemaIngestor.add(updater, schemaRequest)) @@ -70,7 +76,8 @@ public GenericResponse addSchema(@Context HttpHeaders headers, @Path("/update") public GenericResponse updateSchema(@Context HttpHeaders headers, @Valid final UpdateSchemaRequest schemaRequest) { - final var updater = updaterResolver.resolve(headers); + final var updater = updaterResolver.get().resolve(headers); + permissionValidatorSupplier.get().validationSchemaModification(headers, updater, schemaRequest); return GenericResponse.builder() .success(true) .data(schemaIngestor.update(updater, schemaRequest)) @@ -83,7 +90,8 @@ public GenericResponse updateSchema(@Context HttpHeaders headers, @Path("/approve") public GenericResponse approveSchema(@Context HttpHeaders headers, @Valid final SchemaKey schemaKey) { - final var updater = updaterResolver.resolve(headers); + final var updater = updaterResolver.get().resolve(headers); + permissionValidatorSupplier.get().validateSchemaApproval(headers, updater, schemaKey); return GenericResponse.builder() .success(true) .data(schemaIngestor.approve(updater, schemaKey)) @@ -96,7 +104,8 @@ public GenericResponse approveSchema(@Context HttpHeaders headers @Path("/reject") public GenericResponse rejectSchema(@Context HttpHeaders headers, @Valid final SchemaKey schemaKey) { - final var updater = updaterResolver.resolve(headers); + final var updater = updaterResolver.get().resolve(headers); + permissionValidatorSupplier.get().validateSchemaRejection(headers, updater, schemaKey); return GenericResponse.builder() .success(true) .data(schemaIngestor.reject(updater, schemaKey)) diff --git a/leia-dropwizard/src/main/java/com/grookage/leia/dropwizard/bundle/resources/SchemaResource.java b/leia-dropwizard/src/main/java/com/grookage/leia/dropwizard/bundle/resources/SchemaResource.java index cde52ce..19ef220 100644 --- a/leia-dropwizard/src/main/java/com/grookage/leia/dropwizard/bundle/resources/SchemaResource.java +++ b/leia-dropwizard/src/main/java/com/grookage/leia/dropwizard/bundle/resources/SchemaResource.java @@ -28,6 +28,7 @@ import lombok.Setter; import lombok.extern.slf4j.Slf4j; +import javax.annotation.security.PermitAll; import javax.inject.Singleton; import javax.validation.Valid; import javax.ws.rs.Consumes; @@ -45,6 +46,7 @@ @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @AllArgsConstructor +@PermitAll public class SchemaResource { private final SchemaRetriever schemaRetriever; diff --git a/leia-elastic/pom.xml b/leia-elastic/pom.xml index 9811602..82ea827 100644 --- a/leia-elastic/pom.xml +++ b/leia-elastic/pom.xml @@ -22,7 +22,7 @@ com.grookage.leia leia-parent - 0.0.1-RC4 + 0.0.1-RC5 ../leia-parent diff --git a/leia-models/pom.xml b/leia-models/pom.xml index 4463e96..1c4c180 100644 --- a/leia-models/pom.xml +++ b/leia-models/pom.xml @@ -21,7 +21,7 @@ com.grookage.leia leia-parent - 0.0.1-RC4 + 0.0.1-RC5 ../leia-parent diff --git a/leia-models/src/main/java/com/grookage/leia/models/user/SchemaUpdater.java b/leia-models/src/main/java/com/grookage/leia/models/user/SchemaUpdater.java index 29c92a6..1eb12c8 100644 --- a/leia-models/src/main/java/com/grookage/leia/models/user/SchemaUpdater.java +++ b/leia-models/src/main/java/com/grookage/leia/models/user/SchemaUpdater.java @@ -23,4 +23,6 @@ public interface SchemaUpdater { String email(); + String namespace(); + } diff --git a/leia-parent/pom.xml b/leia-parent/pom.xml index ff4ee50..8e015a4 100644 --- a/leia-parent/pom.xml +++ b/leia-parent/pom.xml @@ -21,7 +21,7 @@ com.grookage.leia leia-bom - 0.0.1-RC4 + 0.0.1-RC5 ../leia-bom diff --git a/leia-refresher/pom.xml b/leia-refresher/pom.xml index b71f977..09cb8b6 100644 --- a/leia-refresher/pom.xml +++ b/leia-refresher/pom.xml @@ -21,7 +21,7 @@ com.grookage.leia leia-parent - 0.0.1-RC4 + 0.0.1-RC5 ../leia-parent diff --git a/leia-refresher/src/main/java/com/grookage/leia/provider/TimeBasedDataProvider.java b/leia-refresher/src/main/java/com/grookage/leia/provider/TimeBasedDataProvider.java index b4ff447..caec008 100644 --- a/leia-refresher/src/main/java/com/grookage/leia/provider/TimeBasedDataProvider.java +++ b/leia-refresher/src/main/java/com/grookage/leia/provider/TimeBasedDataProvider.java @@ -52,7 +52,11 @@ public TimeBasedDataProvider(Supplier dataSupplier, T initialDefaultValue, in this(dataSupplier, initialDefaultValue, delay, timeUnit, (t1, t2) -> true, periodicRefresh); } - public TimeBasedDataProvider(Supplier dataSupplier, T initialDefaultValue, int delay, TimeUnit timeUnit, BiFunction shouldUpdate, + public TimeBasedDataProvider(Supplier dataSupplier, + T initialDefaultValue, + int delay, + TimeUnit timeUnit, + BiFunction shouldUpdate, boolean periodicRefresh) { this.dataSupplier = dataSupplier; this.initialDefaultValue = initialDefaultValue; diff --git a/leia-repository/pom.xml b/leia-repository/pom.xml index e8eaa71..5cc4c84 100644 --- a/leia-repository/pom.xml +++ b/leia-repository/pom.xml @@ -22,7 +22,7 @@ com.grookage.leia leia-parent - 0.0.1-RC4 + 0.0.1-RC5 ../leia-parent diff --git a/leia-schema-validator/pom.xml b/leia-schema-validator/pom.xml index 139f864..5d98210 100644 --- a/leia-schema-validator/pom.xml +++ b/leia-schema-validator/pom.xml @@ -22,7 +22,7 @@ com.grookage.leia leia-parent - 0.0.1-RC4 + 0.0.1-RC5 ../leia-parent diff --git a/leia-schema-validator/src/main/java/com/grookage/leia/validator/StaticSchemaValidator.java b/leia-schema-validator/src/main/java/com/grookage/leia/validator/StaticSchemaValidator.java index 4ccd67a..fe2dce8 100644 --- a/leia-schema-validator/src/main/java/com/grookage/leia/validator/StaticSchemaValidator.java +++ b/leia-schema-validator/src/main/java/com/grookage/leia/validator/StaticSchemaValidator.java @@ -91,10 +91,9 @@ public void stop() { @Override public boolean valid(SchemaKey schemaKey) { - return validationRegistry.computeIfAbsent(schemaKey, key -> { - final var klass = getKlass(key).orElse(null); - return null == klass ? Boolean.FALSE : validate(key, klass); - }); + return validationRegistry.computeIfAbsent(schemaKey, + key -> getKlass(key).map(aClass -> validate(key, aClass)) + .orElse(Boolean.FALSE)); } @Override diff --git a/pom.xml b/pom.xml index 3806d86..ff12a7e 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ 4.0.0 com.grookage.leia leia - 0.0.1-RC4 + 0.0.1-RC5 pom Leia