🚚 Move files
This commit is contained in:
parent
f52cba81d5
commit
5483847304
@ -1,76 +0,0 @@
|
||||
package dev.dinauer;
|
||||
|
||||
import dev.dinauer.service.PodService;
|
||||
import dev.dinauer.utils.ClientProvider;
|
||||
import io.fabric8.kubernetes.api.model.Pod;
|
||||
import io.fabric8.kubernetes.client.KubernetesClient;
|
||||
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
|
||||
import io.quarkus.security.Authenticated;
|
||||
import io.smallrye.common.annotation.Blocking;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.*;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import org.jboss.resteasy.reactive.common.NotImplementedYet;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Path("/pods/{pod-id}/logs")
|
||||
@ApplicationScoped
|
||||
@Blocking
|
||||
@Authenticated
|
||||
public class LogResource
|
||||
{
|
||||
@Inject
|
||||
ClientProvider clientProvider;
|
||||
|
||||
@Inject
|
||||
PodService podService;
|
||||
|
||||
@Inject
|
||||
ProcessRunner processRunner;
|
||||
|
||||
@GET
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public List<KubernetesLog> getLogs(@PathParam("pod-id") String podId)
|
||||
{
|
||||
Optional<Pod> podOptional = podService.findPodById(podId);
|
||||
if(podOptional.isPresent())
|
||||
{
|
||||
Pod pod = podOptional.get();
|
||||
return getLogs(pod, null);
|
||||
}
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
public List<KubernetesLog> getLogs(Pod pod, LocalDateTime from)
|
||||
{
|
||||
String command = String.format("kubectl logs %s -n %s --timestamps --tail=1000", pod.getMetadata().getName(), pod.getMetadata().getNamespace());
|
||||
List<KubernetesLog> result = new ArrayList<>();
|
||||
List<String> logs = processRunner.runToLines(command);
|
||||
for (String log : logs)
|
||||
{
|
||||
int indexFirstSpace = log.indexOf(" ");
|
||||
if (indexFirstSpace != -1)
|
||||
{
|
||||
String timestampRaw = log.substring(0, indexFirstSpace);
|
||||
String message = log.substring(indexFirstSpace).trim();
|
||||
try
|
||||
{
|
||||
result.add(new KubernetesLog(LocalDateTime.parse(timestampRaw, DateTimeFormatter.ISO_DATE_TIME), message));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
package dev.dinauer;
|
||||
package dev.dinauer.inspect.log;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
package dev.dinauer;
|
||||
package dev.dinauer.inspect.log;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
@ -1,6 +1,4 @@
|
||||
package dev.dinauer;
|
||||
|
||||
import dev.dinauer.inspect.websocket.EventType;
|
||||
package dev.dinauer.inspect.websocket;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -2,32 +2,26 @@ package dev.dinauer.inspect.websocket;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import dev.dinauer.ResourceEvent;
|
||||
import dev.dinauer.ResourceType;
|
||||
import dev.dinauer.service.ResourceService;
|
||||
import dev.dinauer.utils.ClientProvider;
|
||||
import io.fabric8.kubernetes.client.Watch;
|
||||
import io.fabric8.kubernetes.client.Watcher;
|
||||
import io.fabric8.kubernetes.client.WatcherException;
|
||||
import io.quarkus.security.UnauthorizedException;
|
||||
import io.smallrye.jwt.auth.principal.JWTParser;
|
||||
import io.smallrye.jwt.auth.principal.ParseException;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.websocket.CloseReason;
|
||||
import jakarta.websocket.OnClose;
|
||||
import jakarta.websocket.OnOpen;
|
||||
import jakarta.websocket.Session;
|
||||
import jakarta.websocket.server.PathParam;
|
||||
import jakarta.websocket.server.ServerEndpoint;
|
||||
import jakarta.ws.rs.QueryParam;
|
||||
import jakarta.ws.rs.WebApplicationException;
|
||||
import org.eclipse.microprofile.context.ManagedExecutor;
|
||||
import org.eclipse.microprofile.jwt.JsonWebToken;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@ -9,6 +9,7 @@ import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.*;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import io.quarkus.elytron.security.common.BcryptUtil;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -44,9 +45,9 @@ public class LoginResource
|
||||
.sign();
|
||||
}
|
||||
LOG.info("Cannot access user. Forbidden");
|
||||
throw new ForbiddenException();
|
||||
throw new ForbiddenException(Response.status(403).type(MediaType.TEXT_PLAIN).entity("wrong_password").build());
|
||||
}
|
||||
LOG.info("User not found");
|
||||
throw new NotFoundException();
|
||||
throw new NotFoundException(Response.status(404).type(MediaType.TEXT_PLAIN).entity("user_not_found").build());
|
||||
}
|
||||
}
|
||||
|
||||
5
src/main/java/dev/dinauer/login/UserCreation.java
Normal file
5
src/main/java/dev/dinauer/login/UserCreation.java
Normal file
@ -0,0 +1,5 @@
|
||||
package dev.dinauer.login;
|
||||
|
||||
public record UserCreation(String username, String firstname, String lastname, String email, String role, String password)
|
||||
{
|
||||
}
|
||||
19
src/main/java/dev/dinauer/login/UserMapper.java
Normal file
19
src/main/java/dev/dinauer/login/UserMapper.java
Normal file
@ -0,0 +1,19 @@
|
||||
package dev.dinauer.login;
|
||||
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ApplicationScoped
|
||||
public class UserMapper
|
||||
{
|
||||
public User map(UserEntity user)
|
||||
{
|
||||
return new User(user.getUsername(), user.getEmail(), user.getRoles(), null);
|
||||
}
|
||||
|
||||
public List<User> map(List<UserEntity> users)
|
||||
{
|
||||
return users.stream().map(this::map).toList();
|
||||
}
|
||||
}
|
||||
@ -1,11 +1,9 @@
|
||||
package dev.dinauer;
|
||||
package dev.dinauer.login;
|
||||
|
||||
import dev.dinauer.login.User;
|
||||
import dev.dinauer.login.UserEntity;
|
||||
import dev.dinauer.login.UserRepo;
|
||||
import io.quarkus.elytron.security.common.BcryptUtil;
|
||||
import io.quarkus.security.Authenticated;
|
||||
import io.quarkus.security.identity.SecurityIdentity;
|
||||
import jakarta.annotation.security.RolesAllowed;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.transaction.Transactional;
|
||||
@ -13,6 +11,7 @@ import jakarta.ws.rs.*;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
@ -27,6 +26,9 @@ public class UserResource
|
||||
@Inject
|
||||
SecurityIdentity securityIdentity;
|
||||
|
||||
@Inject
|
||||
UserMapper userMapper;
|
||||
|
||||
@GET
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Path("/{username}")
|
||||
@ -35,24 +37,37 @@ public class UserResource
|
||||
Optional<UserEntity> userOptional = userRepo.findByIdOptional(id);
|
||||
if (userOptional.isPresent())
|
||||
{
|
||||
UserEntity user = userOptional.get();
|
||||
return new User(user.getUsername(), user.getEmail(), user.getRoles(), null);
|
||||
return userMapper.map(userOptional.get());
|
||||
}
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
@GET
|
||||
public List<User> getAll()
|
||||
{
|
||||
return userMapper.map(userRepo.listAll());
|
||||
}
|
||||
|
||||
@POST
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Transactional
|
||||
public void createUser(User user)
|
||||
@RolesAllowed({"ADMIN", "OWNER"})
|
||||
public void createUser(UserCreation user)
|
||||
{
|
||||
UserEntity userEntity = UserEntity.init();
|
||||
userEntity.setUsername(user.username());
|
||||
userEntity.setPassword(BcryptUtil.bcryptHash(user.password()));
|
||||
userEntity.setRoles(Set.of("user"));
|
||||
userEntity.setEmail(user.email());
|
||||
userRepo.persist(userEntity);
|
||||
if (List.of("ADMIN", "USER").contains(user.role()))
|
||||
{
|
||||
UserEntity userEntity = UserEntity.init();
|
||||
userEntity.setFirstname(user.firstname());
|
||||
userEntity.setLastname(user.lastname());
|
||||
userEntity.setUsername(user.username());
|
||||
userEntity.setPassword(BcryptUtil.bcryptHash(user.password()));
|
||||
userEntity.setRoles(Set.of(user.role()));
|
||||
userEntity.setEmail(user.email());
|
||||
userRepo.persist(userEntity);
|
||||
return;
|
||||
}
|
||||
throw new RuntimeException("Invalid role.");
|
||||
}
|
||||
|
||||
@PUT
|
||||
@ -60,13 +75,13 @@ public class UserResource
|
||||
@Produces
|
||||
@Consumes(MediaType.TEXT_PLAIN)
|
||||
@Transactional
|
||||
public void changePassword(@PathParam("username") String username, String password) throws IOException
|
||||
public void changePassword(@PathParam("username") String username, String password)
|
||||
{
|
||||
Optional<UserEntity> persistentUserOptional = userRepo.findOptionalByUsername(username);
|
||||
if(persistentUserOptional.isPresent() && password != null && !password.isBlank())
|
||||
{
|
||||
UserEntity persistentUser = persistentUserOptional.get();
|
||||
if(securityIdentity.getPrincipal().getName().equals(persistentUser.getUsername()))
|
||||
if(securityIdentity.getPrincipal().getName().equals(persistentUser.getId()))
|
||||
{
|
||||
persistentUser.setPassword(BcryptUtil.bcryptHash(password));
|
||||
userRepo.persist(persistentUser);
|
||||
@ -0,0 +1,55 @@
|
||||
package dev.dinauer.monitoring;
|
||||
|
||||
import dev.dinauer.monitoring.entity.MonitoringConfig;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "monitoring_collection")
|
||||
public class MonitoringCollection
|
||||
{
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
private String name;
|
||||
|
||||
@OneToMany(mappedBy = "monitoringCollection")
|
||||
private List<MonitoringConfig> configs;
|
||||
|
||||
public String getId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
public MonitoringCollection setId(String id)
|
||||
{
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
public MonitoringCollection setName(String name)
|
||||
{
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public List<MonitoringConfig> getConfigs()
|
||||
{
|
||||
return configs;
|
||||
}
|
||||
|
||||
public MonitoringCollection setConfigs(List<MonitoringConfig> configs)
|
||||
{
|
||||
this.configs = configs;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
package dev.dinauer.monitoring.entity;
|
||||
|
||||
import dev.dinauer.monitoring.MonitoringCollection;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@ -29,6 +30,10 @@ public class MonitoringConfig
|
||||
@OneToOne(mappedBy = "config")
|
||||
private VolumeConfig volumeConfig;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "monitoring_collection_id")
|
||||
private MonitoringCollection monitoringCollection;
|
||||
|
||||
public String getId()
|
||||
{
|
||||
return id;
|
||||
@ -116,4 +121,15 @@ public class MonitoringConfig
|
||||
this.volumeConfig = volumeConfig;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MonitoringCollection getMonitoringCollection()
|
||||
{
|
||||
return monitoringCollection;
|
||||
}
|
||||
|
||||
public MonitoringConfig setMonitoringCollection(MonitoringCollection monitoringCollection)
|
||||
{
|
||||
this.monitoringCollection = monitoringCollection;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user