Commit 7617ee44 authored by ReemyHasan's avatar ReemyHasan

Some cleaning

parent c5b76f65
...@@ -8,12 +8,12 @@ ...@@ -8,12 +8,12 @@
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" /> <sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" /> <outputRelativeToContentRoot value="true" />
<module name="SnmpReciever" /> <module name="SnmpReciever" />
<module name="TrapStream" />
<module name="Web-Application" /> <module name="Web-Application" />
<module name="api-gateway" /> <module name="api-gateway" />
<module name="user-service" /> <module name="user-service" />
<module name="identity-service" /> <module name="identity-service" />
<module name="service-registry" /> <module name="service-registry" />
<module name="Processing" />
<module name="kafkaConsumerSNMP" /> <module name="kafkaConsumerSNMP" />
</profile> </profile>
</annotationProcessing> </annotationProcessing>
...@@ -22,10 +22,11 @@ ...@@ -22,10 +22,11 @@
<module name="api-gateway" target="17" /> <module name="api-gateway" target="17" />
<module name="identity-service" target="17" /> <module name="identity-service" target="17" />
<module name="kafkaConsumerSNMP" target="17" /> <module name="kafkaConsumerSNMP" target="17" />
<module name="Processing" target="17" />
<module name="service-discovery" target="1.5" /> <module name="service-discovery" target="1.5" />
<module name="service-registry" target="17" /> <module name="service-registry" target="17" />
<module name="SnmpReciever" 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="user-service" target="17" />
<module name="Web-Application" target="17" /> <module name="Web-Application" target="17" />
</bytecodeTargetLevel> </bytecodeTargetLevel>
...@@ -33,8 +34,9 @@ ...@@ -33,8 +34,9 @@
<component name="JavacSettings"> <component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE"> <option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="Api-gateway" options="" /> <module name="Api-gateway" options="" />
<module name="Processing" options="-parameters" />
<module name="SnmpReciever" 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="Web-Application" options="-parameters" />
<module name="api-gateway" options="-parameters" /> <module name="api-gateway" options="-parameters" />
<module name="identity-service" options="-parameters" /> <module name="identity-service" options="-parameters" />
......
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="Encoding"> <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/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/SNMP-trap-generator/src/main/resources" 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/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/SnmpReciever/src/main/resources" 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/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/Web-Application/src/main/resources" 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" /> <file url="file://$PROJECT_DIR$/api-gateway/src/main/java" charset="UTF-8" />
......
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
<option value="$PROJECT_DIR$/api-gateway/pom.xml" /> <option value="$PROJECT_DIR$/api-gateway/pom.xml" />
<option value="$PROJECT_DIR$/identity-service/pom.xml" /> <option value="$PROJECT_DIR$/identity-service/pom.xml" />
<option value="$PROJECT_DIR$/user-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> </list>
</option> </option>
</component> </component>
......
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
<project version="4"> <project version="4">
<component name="ProjectModuleManager"> <component name="ProjectModuleManager">
<modules> <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$/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$/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$/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" /> <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; ...@@ -2,7 +2,6 @@ package com.example.Processing;
import com.example.Processing.entities.EnrichedTrap; import com.example.Processing.entities.EnrichedTrap;
import com.example.Processing.entities.ProcessedTrap; import com.example.Processing.entities.ProcessedTrap;
import com.example.Processing.entities.SeverityLevel;
import com.example.Processing.services.ElasticService; import com.example.Processing.services.ElasticService;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.PostConstruct; import jakarta.annotation.PostConstruct;
...@@ -12,16 +11,15 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; ...@@ -12,16 +11,15 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.kafka.annotation.KafkaListener; import org.springframework.kafka.annotation.KafkaListener;
import java.util.List;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
@SpringBootApplication @SpringBootApplication
@EnableDiscoveryClient @EnableDiscoveryClient
public class ProcessingApplication { public class TrapStreamApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(ProcessingApplication.class, args); SpringApplication.run(TrapStreamApplication.class, args);
} }
public static int num = 0; public static int num = 0;
......
...@@ -7,6 +7,7 @@ import org.springframework.cloud.gateway.filter.GatewayFilter; ...@@ -7,6 +7,7 @@ import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
@Component @Component
public class AuthenticationFilter extends AbstractGatewayFilterFactory<AuthenticationFilter.Config> { public class AuthenticationFilter extends AbstractGatewayFilterFactory<AuthenticationFilter.Config> {
...@@ -14,8 +15,6 @@ public class AuthenticationFilter extends AbstractGatewayFilterFactory<Authentic ...@@ -14,8 +15,6 @@ public class AuthenticationFilter extends AbstractGatewayFilterFactory<Authentic
@Autowired @Autowired
private RouteValidator validator; private RouteValidator validator;
// @Autowired
// private RestTemplate template;
@Autowired @Autowired
private JwtUtil jwtUtil; private JwtUtil jwtUtil;
...@@ -27,23 +26,31 @@ public class AuthenticationFilter extends AbstractGatewayFilterFactory<Authentic ...@@ -27,23 +26,31 @@ public class AuthenticationFilter extends AbstractGatewayFilterFactory<Authentic
public GatewayFilter apply(Config config) { public GatewayFilter apply(Config config) {
return ((exchange, chain) -> { return ((exchange, chain) -> {
if (validator.isSecured.test(exchange.getRequest())) { if (validator.isSecured.test(exchange.getRequest())) {
//header contains token or not if (exchange.getRequest().getHeaders().containsKey(HttpHeaders.AUTHORIZATION)) {
if (!exchange.getRequest().getHeaders().containsKey(HttpHeaders.AUTHORIZATION)) { String authHeader = exchange.getRequest().getHeaders().get(HttpHeaders.AUTHORIZATION).get(0);
throw new RuntimeException("missing authorization header"); if (authHeader != null && authHeader.startsWith("Bearer ")) {
} authHeader = authHeader.substring(7);
}
String authHeader = exchange.getRequest().getHeaders().get(HttpHeaders.AUTHORIZATION).get(0); try {
if (authHeader != null && authHeader.startsWith("Bearer ")) { jwtUtil.validateToken(authHeader);
authHeader = authHeader.substring(7); System.out.println("Accessed....");
} } catch (Exception e) {
try { System.out.println("invalid access...!");
// //REST call to AUTH service return Mono.error(new RuntimeException("unauthorized access to application"));
// template.getForObject("http://IDENTITY-SERVICE//validate?token" + authHeader, String.class); }
jwtUtil.validateToken(authHeader); } else {
// If AUTHORIZATION header is missing, check the token query parameter
} catch (Exception e) { String tokenQueryParam = exchange.getRequest().getQueryParams().getFirst("token");
System.out.println("invalid access...!"); if (tokenQueryParam == null || tokenQueryParam.isEmpty()) {
throw new RuntimeException("un authorized access to application"); 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); return chain.filter(exchange);
......
...@@ -33,7 +33,7 @@ public class JwtService { ...@@ -33,7 +33,7 @@ public class JwtService {
.setClaims(claims) .setClaims(claims)
.setSubject(userName) .setSubject(userName)
.setIssuedAt(new Date(System.currentTimeMillis())) .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(); .signWith(getSignKey(), SignatureAlgorithm.HS256).compact();
} }
......
...@@ -27,22 +27,19 @@ public class KafkaConsumerSnmpApplication { ...@@ -27,22 +27,19 @@ public class KafkaConsumerSnmpApplication {
@KafkaListener(topics = "TRAP") @KafkaListener(topics = "TRAP")
public void handleKafkaMessage(String pduJson) { public void handleKafkaMessage(String pduJson) {
String json = pduJson;// Replace with your JSON string String json = pduJson;
num++; num++;
try { try {
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
TrapData t = objectMapper.readValue(json,TrapData.class); TrapData t = objectMapper.readValue(json,TrapData.class);
//Some Processing: Add this later //Some Processing: Add this later
EnrichedTrap t2 = new EnrichedTrap(t); EnrichedTrap t2 = new EnrichedTrap(t);
//System.out.println("Not sent yet");
sender.send(t2); sender.send(t2);
//System.out.println("Sent now!");
rethinkDBService.saveKafkaMessageToRethink(t2); rethinkDBService.saveKafkaMessageToRethink(t2);
System.out.println(num); System.out.println(num);
} catch (Exception e) { } catch (Exception e) {
System.out.println(e); System.out.println(e);
e.printStackTrace(); 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; ...@@ -9,6 +9,8 @@ import org.snmp4j.smi.VariableBinding;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Random;
@Setter @Setter
@Getter @Getter
public class EnrichedTrap { public class EnrichedTrap {
...@@ -39,10 +41,18 @@ public class EnrichedTrap { ...@@ -39,10 +41,18 @@ public class EnrichedTrap {
this.agentAddress = pdu.getAgentAddress().toString(); this.agentAddress = pdu.getAgentAddress().toString();
this.variableBindings = pdu.getVariableBindings(); this.variableBindings = pdu.getVariableBindings();
this.date = new Date().getTime(); 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; this.severity = SeverityLevel.WARNING;
} }
else if (this.variableBindings.size() >= 3){ else if (randomChoice == 2){
this.severity = SeverityLevel.ERROR; this.severity = SeverityLevel.ERROR;
} }
else this.severity = SeverityLevel.INFO; else this.severity = SeverityLevel.INFO;
......
...@@ -40,15 +40,11 @@ public class RethinkDBConnectionFactory { ...@@ -40,15 +40,11 @@ public class RethinkDBConnectionFactory {
log.info("RethinkDB connected successfully"); log.info("RethinkDB connected successfully");
List<String> dbList = r.dbList().run(connection); List<String> dbList = r.dbList().run(connection);
if (!dbList.contains(dbName)) { if (!dbList.contains(dbName)) {
// System.out.println("Creating DATABASE Heeeeeeeeeeeeeeeeeeeeeeeeeere");
r.dbCreate(dbName).run(connection); r.dbCreate(dbName).run(connection);
} }
List<String> tables = r.db(dbName).tableList().run(connection); List<String> tables = r.db(dbName).tableList().run(connection);
if (!tables.contains(dbTableName)) { if (!tables.contains(dbTableName)) {
// System.out.println("Creating Table Heeeeeeeeeeeeeeeeeeeeeeeeeere");
r.db(dbName).tableCreate(dbTableName).run(connection); 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) { } catch (Exception e) {
log.error("Error connecting to RethinkDB", e); log.error("Error connecting to RethinkDB", e);
......
...@@ -31,25 +31,11 @@ public class RethinkDBService { ...@@ -31,25 +31,11 @@ public class RethinkDBService {
String jsonString = objectMapper.writeValueAsString(rethinkTrap); String jsonString = objectMapper.writeValueAsString(rethinkTrap);
JsonNode jsonNode = objectMapper.readTree(jsonString); JsonNode jsonNode = objectMapper.readTree(jsonString);
Map<String,EnrichedTrap> document = objectMapper.convertValue(jsonNode, Map.class); 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()); r.db(connectionFactory.getDbName()).table(connectionFactory.getDbTableName()).insert(document).run(connectionFactory.getConnection());
} catch (Exception e) { } catch (Exception e) {
System.out.println("error " + 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() { public List<Map<String, EnrichedTrap> > getData() {
try { try {
Cursor<Map<String, EnrichedTrap>> cursor = r.db(connectionFactory.getDbName()).table(connectionFactory.getDbTableName()).run(connectionFactory.getConnection()); Cursor<Map<String, EnrichedTrap>> cursor = r.db(connectionFactory.getDbName()).table(connectionFactory.getDbTableName()).run(connectionFactory.getConnection());
...@@ -63,17 +49,5 @@ public class RethinkDBService { ...@@ -63,17 +49,5 @@ public class RethinkDBService {
return null; 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