Skip to content

Commit 55d0167

Browse files
committed
connect the web resource in a simple way with the policy engine
1 parent 533d2c8 commit 55d0167

File tree

8 files changed

+110
-0
lines changed

8 files changed

+110
-0
lines changed

cli/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
<artifactId>capua</artifactId>
88
<version>0.0.1</version>
99
</parent>
10+
1011
<groupId>com.purbon.kafka.policy</groupId>
1112
<artifactId>cli</artifactId>
1213
<version>0.0.1</version>
1314
<name>cli</name>
1415
<url>http://maven.apache.org</url>
16+
1517
<properties>
1618
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1719
</properties>

web/pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<properties>
1818
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1919
<spring.boot.version>2.7.4</spring.boot.version>
20+
<start-class>com.purbon.kafka.policy.webapi.WebapiApplication</start-class>
2021
</properties>
2122

2223
<dependencyManagement>
@@ -36,6 +37,15 @@
3637
<groupId>org.springframework.boot</groupId>
3738
<artifactId>spring-boot-starter-web</artifactId>
3839
</dependency>
40+
<dependency>
41+
<groupId>org.springframework</groupId>
42+
<artifactId>spring-webmvc</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>com.purbon.kafka.policy</groupId>
46+
<artifactId>core</artifactId>
47+
<version>0.0.1</version>
48+
</dependency>
3949
<dependency>
4050
<groupId>org.springframework.boot</groupId>
4151
<artifactId>spring-boot-starter-test</artifactId>
@@ -54,7 +64,19 @@
5464
<plugin>
5565
<groupId>org.springframework.boot</groupId>
5666
<artifactId>spring-boot-maven-plugin</artifactId>
67+
<version>${spring.boot.version}</version>
68+
<configuration>
69+
<mainClass>com.purbon.kafka.policy.webapi.WebapiApplication</mainClass>
70+
</configuration>
71+
<executions>
72+
<execution>
73+
<goals>
74+
<goal>repackage</goal>
75+
</goals>
76+
</execution>
77+
</executions>
5778
</plugin>
79+
5880
</plugins>
5981
</build>
6082

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.purbon.kafka.policy.webapi;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStream;
5+
import java.io.InputStreamReader;
6+
import java.util.stream.Collectors;
7+
8+
public class Utils {
9+
10+
public static String getResourceFileAsString(String fileName) {
11+
InputStream is = getResourceFileAsInputStream(fileName);
12+
if (is != null) {
13+
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
14+
return (String)reader.lines().collect(Collectors.joining(System.lineSeparator()));
15+
} else {
16+
throw new RuntimeException("resource not found");
17+
}
18+
}
19+
20+
public static InputStream getResourceFileAsInputStream(String fileName) {
21+
ClassLoader classLoader = Utils.class.getClassLoader();
22+
return classLoader.getResourceAsStream(fileName);
23+
}
24+
}

web/src/main/java/com/purbon/kafka/policy/webapi/WebapiApplication.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
56

67
@SpringBootApplication
78
public class WebapiApplication {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.purbon.kafka.policy.webapi.controller;
2+
3+
import com.purbon.kafka.policy.VerifyResult;
4+
import com.purbon.kafka.policy.webapi.service.KafkaPolicyService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.PostMapping;
7+
import org.springframework.web.bind.annotation.RequestBody;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
@RestController
12+
@RequestMapping("/policy")
13+
public class RestPolicyController {
14+
15+
@Autowired
16+
KafkaPolicyService service;
17+
18+
@PostMapping(value = "/validate", consumes = "application/json")
19+
public VerifyResult validate(@RequestBody String content) {
20+
VerifyResult vr = service.validate(content);
21+
return vr;
22+
}
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.purbon.kafka.policy.webapi.service;
2+
3+
import com.purbon.kafka.policy.KafkaPolicyEngine;
4+
import com.purbon.kafka.policy.VerifyResult;
5+
import com.purbon.kafka.policy.webapi.Utils;
6+
import org.springframework.stereotype.Service;
7+
8+
@Service
9+
public class KafkaPolicyService {
10+
11+
private KafkaPolicyEngine engine;
12+
13+
public KafkaPolicyService() {
14+
engine = KafkaPolicyEngine.getInstance();
15+
engine.init(Utils.getResourceFileAsString("kafka-policy.rego"));
16+
}
17+
18+
public VerifyResult validate(String content) {
19+
return engine.validate(content);
20+
}
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package kafka
2+
3+
allow {
4+
topic.partitions == 3
5+
topic.replicationFactor == 1
6+
startswith(topic.name, "foo")
7+
}
8+
9+
deny {
10+
startswith(topic.name, "foo")
11+
}

web/validate.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
curl --header "Content-Type: application/json" \
4+
--request POST \
5+
--data '{ "topics": [{ "name": "foo", "partitions": 3, "replicationFactor": 1, "config": {} }]}' \
6+
http://localhost:8080/policy/validate

0 commit comments

Comments
 (0)