Commit 7617ee44 authored by ReemyHasan's avatar ReemyHasan

Some cleaning

parent c5b76f65
......@@ -8,12 +8,12 @@
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="SnmpReciever" />
<module name="TrapStream" />
<module name="Web-Application" />
<module name="api-gateway" />
<module name="user-service" />
<module name="identity-service" />
<module name="service-registry" />
<module name="Processing" />
<module name="kafkaConsumerSNMP" />
</profile>
</annotationProcessing>
......@@ -22,10 +22,11 @@
<module name="api-gateway" target="17" />
<module name="identity-service" target="17" />
<module name="kafkaConsumerSNMP" target="17" />
<module name="Processing" target="17" />
<module name="service-discovery" target="1.5" />
<module name="service-registry" target="17" />
<module name="SnmpReciever" target="17" />
<module name="Trap Stream" target="17" />
<module name="TrapStream" target="17" />
<module name="user-service" target="17" />
<module name="Web-Application" target="17" />
</bytecodeTargetLevel>
......@@ -33,8 +34,9 @@
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="Api-gateway" options="" />
<module name="Processing" options="-parameters" />
<module name="SnmpReciever" options="-parameters" />
<module name="Trap Stream" options="-parameters" />
<module name="TrapStream" options="-parameters" />
<module name="Web-Application" options="-parameters" />
<module name="api-gateway" options="-parameters" />
<module name="identity-service" options="-parameters" />
......
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/Processing/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/Processing/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/SNMP-trap-generator/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/SNMP-trap-generator/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/SnmpReciever/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/SnmpReciever/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/TrapStream/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/TrapStream/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/Web-Application/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/Web-Application/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/api-gateway/src/main/java" charset="UTF-8" />
......
......@@ -16,6 +16,8 @@
<option value="$PROJECT_DIR$/api-gateway/pom.xml" />
<option value="$PROJECT_DIR$/identity-service/pom.xml" />
<option value="$PROJECT_DIR$/user-service/pom.xml" />
<option value="$PROJECT_DIR$/Trap Stream/pom.xml" />
<option value="$PROJECT_DIR$/TrapStream/pom.xml" />
</list>
</option>
</component>
......
......@@ -2,8 +2,8 @@
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Processing/Processing.iml" filepath="$PROJECT_DIR$/Processing/Processing.iml" />
<module fileurl="file://$PROJECT_DIR$/SnmpReciever/SnmpReciever.iml" filepath="$PROJECT_DIR$/SnmpReciever/SnmpReciever.iml" />
<module fileurl="file://$PROJECT_DIR$/TrapStream/TrapStream.iml" filepath="$PROJECT_DIR$/TrapStream/TrapStream.iml" />
<module fileurl="file://$PROJECT_DIR$/Web-Application/Web-Application.iml" filepath="$PROJECT_DIR$/Web-Application/Web-Application.iml" />
<module fileurl="file://$PROJECT_DIR$/api-gateway/api-gateway.iml" filepath="$PROJECT_DIR$/api-gateway/api-gateway.iml" />
<module fileurl="file://$PROJECT_DIR$/identity-service/identity-service.iml" filepath="$PROJECT_DIR$/identity-service/identity-service.iml" />
......
......@@ -2,7 +2,6 @@ package com.example.Processing;
import com.example.Processing.entities.EnrichedTrap;
import com.example.Processing.entities.ProcessedTrap;
import com.example.Processing.entities.SeverityLevel;
import com.example.Processing.services.ElasticService;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.PostConstruct;
......@@ -12,16 +11,15 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.kafka.annotation.KafkaListener;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
@SpringBootApplication
@EnableDiscoveryClient
public class ProcessingApplication {
public class TrapStreamApplication {
public static void main(String[] args) {
SpringApplication.run(ProcessingApplication.class, args);
SpringApplication.run(TrapStreamApplication.class, args);
}
public static int num = 0;
......
......@@ -7,6 +7,7 @@ import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
@Component
public class AuthenticationFilter extends AbstractGatewayFilterFactory<AuthenticationFilter.Config> {
......@@ -14,8 +15,6 @@ public class AuthenticationFilter extends AbstractGatewayFilterFactory<Authentic
@Autowired
private RouteValidator validator;
// @Autowired
// private RestTemplate template;
@Autowired
private JwtUtil jwtUtil;
......@@ -27,23 +26,31 @@ public class AuthenticationFilter extends AbstractGatewayFilterFactory<Authentic
public GatewayFilter apply(Config config) {
return ((exchange, chain) -> {
if (validator.isSecured.test(exchange.getRequest())) {
//header contains token or not
if (!exchange.getRequest().getHeaders().containsKey(HttpHeaders.AUTHORIZATION)) {
throw new RuntimeException("missing authorization header");
}
String authHeader = exchange.getRequest().getHeaders().get(HttpHeaders.AUTHORIZATION).get(0);
if (authHeader != null && authHeader.startsWith("Bearer ")) {
authHeader = authHeader.substring(7);
}
try {
// //REST call to AUTH service
// template.getForObject("http://IDENTITY-SERVICE//validate?token" + authHeader, String.class);
jwtUtil.validateToken(authHeader);
} catch (Exception e) {
System.out.println("invalid access...!");
throw new RuntimeException("un authorized access to application");
if (exchange.getRequest().getHeaders().containsKey(HttpHeaders.AUTHORIZATION)) {
String authHeader = exchange.getRequest().getHeaders().get(HttpHeaders.AUTHORIZATION).get(0);
if (authHeader != null && authHeader.startsWith("Bearer ")) {
authHeader = authHeader.substring(7);
}
try {
jwtUtil.validateToken(authHeader);
System.out.println("Accessed....");
} catch (Exception e) {
System.out.println("invalid access...!");
return Mono.error(new RuntimeException("unauthorized access to application"));
}
} else {
// If AUTHORIZATION header is missing, check the token query parameter
String tokenQueryParam = exchange.getRequest().getQueryParams().getFirst("token");
if (tokenQueryParam == null || tokenQueryParam.isEmpty()) {
return Mono.error(new RuntimeException("missing authorization header and token query parameter"));
}
try {
jwtUtil.validateToken(tokenQueryParam);
System.out.println("Accessed....");
} catch (Exception e) {
System.out.println("invalid access...!");
return Mono.error(new RuntimeException("unauthorized access to application"));
}
}
}
return chain.filter(exchange);
......
......@@ -33,7 +33,7 @@ public class JwtService {
.setClaims(claims)
.setSubject(userName)
.setIssuedAt(new Date(System.currentTimeMillis()))
.setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 30))
.setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60))
.signWith(getSignKey(), SignatureAlgorithm.HS256).compact();
}
......
......@@ -27,22 +27,19 @@ public class KafkaConsumerSnmpApplication {
@KafkaListener(topics = "TRAP")
public void handleKafkaMessage(String pduJson) {
String json = pduJson;// Replace with your JSON string
String json = pduJson;
num++;
try {
ObjectMapper objectMapper = new ObjectMapper();
TrapData t = objectMapper.readValue(json,TrapData.class);
//Some Processing: Add this later
EnrichedTrap t2 = new EnrichedTrap(t);
//System.out.println("Not sent yet");
sender.send(t2);
//System.out.println("Sent now!");
rethinkDBService.saveKafkaMessageToRethink(t2);
System.out.println(num);
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
//System.out.println(num);
}
}
//package com.example.kafkaConsumerSNMP.configuration;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.fasterxml.jackson.databind.ObjectWriter;
//import org.springframework.stereotype.Component;
//import org.springframework.web.socket.CloseStatus;
//import org.springframework.web.socket.TextMessage;
//import org.springframework.web.socket.WebSocketSession;
//import org.springframework.web.socket.handler.TextWebSocketHandler;
//
//import java.io.IOException;
//import java.util.List;
//import java.util.Map;
//import java.util.concurrent.CopyOnWriteArrayList;
//
//@Component
//public class SocketTextHandler extends TextWebSocketHandler {
// private List<WebSocketSession> sessions = new CopyOnWriteArrayList<>();
//
// @Override
// public void afterConnectionEstablished(WebSocketSession session) throws Exception {
// System.out.println("New connection from " + session.getRemoteAddress());
// sessions.add(session);
// }
//
// @Override
// protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
// System.out.println("Received message from " + session.getRemoteAddress() + ": " + message.getPayload());
// // Handle incoming message
// }
//
// @Override
// public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
// System.out.println("Connection closed to " + session.getRemoteAddress() + " with status " + status);
// sessions.remove(session);
// }
//
// public void broadcast(List<Map<String, Object>> message) throws IOException {
// System.out.println("Hey Broadcasting");
// System.out.println(message);
//
// ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
// String json = ow.writeValueAsString(message);
// System.out.println(json);
// TextMessage s = new TextMessage(json);
// System.out.println(s);
// for (WebSocketSession session : sessions) {
// if (session.isOpen()) {
// session.sendMessage(s);
// }
// }
// }
//}
//package com.example.kafkaConsumerSNMP.configuration;
//
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.web.socket.config.annotation.EnableWebSocket;
//import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
//import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
//
//@Configuration
//@EnableWebSocket
//public class WebSocketConfig implements WebSocketConfigurer {
//
// @Autowired
// private SocketTextHandler socket_server;
// @Override
// public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
// System.out.println("HEYYYYYYYYYYYYYYYYYY");
// registry.addHandler(socket_server, "/my-websocket").setAllowedOrigins("*");
// }
//}
......@@ -9,6 +9,8 @@ import org.snmp4j.smi.VariableBinding;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
@Setter
@Getter
public class EnrichedTrap {
......@@ -39,10 +41,18 @@ public class EnrichedTrap {
this.agentAddress = pdu.getAgentAddress().toString();
this.variableBindings = pdu.getVariableBindings();
this.date = new Date().getTime();
if (this.variableBindings.size() > 5){
int[] choices = {1, 2, 3};
Random random = new Random();
int randomIndex = random.nextInt(choices.length);
int randomChoice = choices[randomIndex];
if (randomChoice == 1){
this.severity = SeverityLevel.WARNING;
}
else if (this.variableBindings.size() >= 3){
else if (randomChoice == 2){
this.severity = SeverityLevel.ERROR;
}
else this.severity = SeverityLevel.INFO;
......
......@@ -40,15 +40,11 @@ public class RethinkDBConnectionFactory {
log.info("RethinkDB connected successfully");
List<String> dbList = r.dbList().run(connection);
if (!dbList.contains(dbName)) {
// System.out.println("Creating DATABASE Heeeeeeeeeeeeeeeeeeeeeeeeeere");
r.dbCreate(dbName).run(connection);
}
List<String> tables = r.db(dbName).tableList().run(connection);
if (!tables.contains(dbTableName)) {
// System.out.println("Creating Table Heeeeeeeeeeeeeeeeeeeeeeeeeere");
r.db(dbName).tableCreate(dbTableName).run(connection);
//r.db(dbName).table(dbTableName).indexCreate("timestamp").run(connection);
//r.db("my_database").table("my_table").indexCreate("trap").run(connection);
}
} catch (Exception e) {
log.error("Error connecting to RethinkDB", e);
......
......@@ -31,25 +31,11 @@ public class RethinkDBService {
String jsonString = objectMapper.writeValueAsString(rethinkTrap);
JsonNode jsonNode = objectMapper.readTree(jsonString);
Map<String,EnrichedTrap> document = objectMapper.convertValue(jsonNode, Map.class);
// System.out.println(jsonString);
// System.out.println(jsonNode);
// System.out.println(document);
r.db(connectionFactory.getDbName()).table(connectionFactory.getDbTableName()).insert(document).run(connectionFactory.getConnection());
} catch (Exception e) {
System.out.println("error " + e);
}
}
/*public List<Map<String,EnrichedTrap> > getById(String id){
}*/
/*public void saveData(String database, String table, Map<String, Object> data) {
try {
r.db(database).table(table).insert(data).run(connectionFactory.getConnection());
log.info("Data saved successfully in RethinkDB");
} catch (Exception e) {
log.error("Error saving data in RethinkDB", e);
}
}*/
public List<Map<String, EnrichedTrap> > getData() {
try {
Cursor<Map<String, EnrichedTrap>> cursor = r.db(connectionFactory.getDbName()).table(connectionFactory.getDbTableName()).run(connectionFactory.getConnection());
......@@ -63,17 +49,5 @@ public class RethinkDBService {
return null;
}
}
//Connection conn = connectionFactory.getConnection();
//Cursor<Map<String, Object> > changeCursor = r.db(database).table(table).changes().run(conn);
/*for (Object change : changeCursor) {
result.add((Map<String, Object>) change);
}
try {
socket_server.broadcast(result);
} catch (IOException e) {
throw new RuntimeException(e);
}*/
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment