refactoring and password generation
This commit is contained in:
parent
a8703ca940
commit
aac2a1ffc7
@ -13,9 +13,6 @@ import jakarta.persistence.Table;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Instances
|
||||
*/
|
||||
@ -42,11 +39,14 @@ public class Instance {
|
||||
public User owner;
|
||||
|
||||
public String containerId;
|
||||
public String password;
|
||||
|
||||
public Instance(String name, int port, User user, String containerId) {
|
||||
public Instance(String name, int port, User user, String containerId,
|
||||
String password) {
|
||||
this.name = name;
|
||||
this.port = port;
|
||||
this.owner = user;
|
||||
this.containerId = containerId;
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
package fr.la_banquise.backend.rest;
|
||||
|
||||
import fr.la_banquise.backend.services.DockerService;
|
||||
import io.quarkus.security.jpa.Roles;
|
||||
import jakarta.annotation.security.RolesAllowed;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.*;
|
||||
|
||||
@ -7,6 +7,7 @@ import io.quarkus.security.Authenticated;
|
||||
import io.quarkus.security.identity.SecurityIdentity;
|
||||
import jakarta.annotation.security.RolesAllowed;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.DELETE;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.POST;
|
||||
import jakarta.ws.rs.Path;
|
||||
@ -96,7 +97,7 @@ public class InstanceEndpoints {
|
||||
}
|
||||
|
||||
@GET
|
||||
@Authenticated
|
||||
@RolesAllowed("ROOT")
|
||||
@Path("/{id}/containers")
|
||||
public Response getStatusContainers(@PathParam("id") Long jiId) {
|
||||
return Response.ok(jiService.getStatusContainers(jiId)).build();
|
||||
@ -111,7 +112,7 @@ public class InstanceEndpoints {
|
||||
}
|
||||
|
||||
@GET
|
||||
@RolesAllowed("ROOT")
|
||||
@Authenticated
|
||||
@Path("/{id}/container")
|
||||
public Response getStatusMyContainer(@PathParam("id") Long jiId) {
|
||||
String name = identity.getPrincipal().getName();
|
||||
@ -119,12 +120,18 @@ public class InstanceEndpoints {
|
||||
return Response.ok(jiService.getStatusContainer(jiId, userId)).build();
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// Last but not least, be able do delete every container and instance
|
||||
///
|
||||
/// TODO
|
||||
|
||||
|
||||
@DELETE
|
||||
@RolesAllowed("ROOT")
|
||||
@Path("/{id}/container/{instanceId}")
|
||||
public Response
|
||||
delContainer(@PathParam("id") Long jiId,
|
||||
@PathParam("instanceId") Long instanceId) {
|
||||
jiService.deleteContainer(instanceId);
|
||||
// TODO: add filter by JI (but optionnal imo)
|
||||
return Response.ok().build();
|
||||
}
|
||||
}
|
||||
|
||||
@ -171,7 +171,8 @@ public class DockerService {
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
///
|
||||
/// First, we create our container and basic functions
|
||||
public String createContainer(String name, int port, String username, String password) {
|
||||
public String createContainer(String name, int port, String username,
|
||||
String password) {
|
||||
ExposedPort tcpSsh = ExposedPort.tcp(2222);
|
||||
|
||||
Ports portBindings = new Ports();
|
||||
@ -184,6 +185,9 @@ public class DockerService {
|
||||
labels.put("environment", "development");
|
||||
labels.put("version", "1.0");
|
||||
labels.put("created-by", "java-docker-api");
|
||||
if (username.equals("root")) {
|
||||
username = "root_user";
|
||||
}
|
||||
|
||||
dockerClient.buildImageCmd()
|
||||
.withDockerfile(new File("./dockerfile/Dockerfile"))
|
||||
@ -198,9 +202,10 @@ public class DockerService {
|
||||
.withLabels(labels)
|
||||
.withExposedPorts(tcpSsh)
|
||||
.withHostConfig(hostConfig)
|
||||
.withEnv("SUDO_ACCESS=false", "PASSWORD_ACCESS=true",
|
||||
"USER_NAME="+username, // TODO : User login
|
||||
"USER_PASSWORD="+password // TODO : Random passwd
|
||||
.withEnv(
|
||||
"SUDO_ACCESS=true", "PASSWORD_ACCESS=true",
|
||||
"USER_NAME=".concat(username), // TODO : User login
|
||||
"USER_PASSWORD=".concat(password) // TODO : Random passwd
|
||||
)
|
||||
.exec();
|
||||
|
||||
|
||||
@ -12,6 +12,7 @@ import jakarta.inject.Inject;
|
||||
import jakarta.transaction.Transactional;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@ -32,15 +33,34 @@ public class InstanceService {
|
||||
///
|
||||
/// First, we create our Instance and basic functions
|
||||
|
||||
public String randomAlphanumericString() {
|
||||
int leftLimit = 48; // numeral '0'
|
||||
int rightLimit = 122; // letter 'z'
|
||||
int targetStringLength = 20;
|
||||
Random random = new Random();
|
||||
|
||||
String generatedString =
|
||||
random.ints(leftLimit, rightLimit + 1)
|
||||
.filter(i -> (i <= 57 || i >= 65) && (i <= 90 || i >= 97))
|
||||
.limit(targetStringLength)
|
||||
.collect(StringBuilder::new, StringBuilder::appendCodePoint,
|
||||
StringBuilder::append)
|
||||
.toString();
|
||||
|
||||
return generatedString;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Instance createInstance(Long userId, Ji ji) {
|
||||
User user = userRepository.findById(userId);
|
||||
String name = user.name + "-" + ji.id;
|
||||
int port = getFreePort(1).iterator().next();
|
||||
Instance instance = new Instance(name, port, user, "Not created");
|
||||
Instance instance = new Instance(name, port, user, "Not created",
|
||||
randomAlphanumericString());
|
||||
instanceRepository.persist(instance);
|
||||
return instance;
|
||||
}
|
||||
|
||||
public List<Instance> getAllInstances() {
|
||||
return instanceRepository.findAll().list();
|
||||
}
|
||||
@ -63,7 +83,8 @@ public class InstanceService {
|
||||
Instance instance = instanceRepository.findById(instanceId);
|
||||
if (instance.containerId.equals("Not created"))
|
||||
instance.containerId = dockerService.createContainer(
|
||||
instance.name, instance.port, "test", "test");
|
||||
instance.name, instance.port, instance.owner.name,
|
||||
instance.password);
|
||||
}
|
||||
|
||||
public InspectContainerResponse.ContainerState getStatusContainer(Long id) {
|
||||
@ -91,7 +112,7 @@ public class InstanceService {
|
||||
|
||||
public boolean deleteContainer(Long id) {
|
||||
Instance instance = instanceRepository.findById(id);
|
||||
|
||||
instance.containerId = "Not created";
|
||||
return dockerService.remove(instance.name);
|
||||
}
|
||||
|
||||
|
||||
@ -165,6 +165,11 @@ public class JiService {
|
||||
|
||||
/// Last but not least, be able do delete every container and instance
|
||||
|
||||
@Transactional
|
||||
public void deleteContainer(Long instanceId) {
|
||||
instanceService.deleteContainer(instanceId);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteJi(Long jiId) {
|
||||
Ji ji = getJi(jiId);
|
||||
|
||||
@ -1 +1 @@
|
||||
Subproject commit ddcb0920c54cbbc3c896fb735f97a9bc5f7e28f5
|
||||
Subproject commit a24db1f53f2ebf96aa3ca679a3e2364feabb3527
|
||||
Loading…
x
Reference in New Issue
Block a user