Skip to content

Commit

Permalink
feat: added admin-credential.json file (backend)
Browse files Browse the repository at this point in the history
  • Loading branch information
yp969803 committed Jan 5, 2025
1 parent d1205f5 commit 4f5091e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/producer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,24 @@
</properties>
<dependencies>


<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>

</dependency>




<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>


<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.5</version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.fordevio.producer.payloads.response.CurrentUserResponse;
import com.fordevio.producer.payloads.response.MessageResponse;
import com.fordevio.producer.services.database.UserHandler;
import com.fordevio.producer.services.fileIO.FileHandlerSvc;

import jakarta.validation.Valid;

Expand All @@ -32,6 +33,9 @@ public class UserController {
@Autowired
private UserHandler userHandler;

@Autowired
private FileHandlerSvc fileHandler;

@GetMapping("/me")
public ResponseEntity<?> getLoggedInUser(@AuthenticationPrincipal UserDetails userDetails){
try{
Expand Down Expand Up @@ -117,6 +121,10 @@ public ResponseEntity<?> updateUser(@PathVariable Long id, @Valid @RequestBody A
userToUpdate.setPassword(user.getPassword());
userToUpdate.setPermissions(permissions);
userHandler.saveUser(userToUpdate);

if(adminUser.getId().equals(id)){
fileHandler.updateCredentialFile(userToUpdate);
}
return ResponseEntity.ok(userToUpdate);
}catch(Exception e){
return ResponseEntity.internalServerError().body(new MessageResponse(e.getMessage()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.stereotype.Component;

import com.fordevio.producer.models.Project;
import com.fordevio.producer.models.User;
import com.fordevio.producer.services.database.ProjectHandler;
import com.fordevio.producer.services.database.UserHandler;
import com.fordevio.producer.services.fileIO.FileHandlerSvc;
Expand Down Expand Up @@ -45,6 +46,8 @@ public class StartRunner {
public void init(){
try{
createAdminIfNot();
User admin = userHandler.getAdminUser();
fileHandler.updateCredentialFile(admin);
createProjectStatusMap();
createMainThread();
}catch(Exception e){
Expand All @@ -60,6 +63,7 @@ void createAdminIfNot() throws Exception {
fileHandler.createDirIfNot("scripts");
fileHandler.createDirIfNot("logs");


} catch (Exception e) {
log.error("Error while creating admin", e);
throw new RuntimeException("Error while creating admin", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fordevio.producer.models.User;

import lombok.extern.slf4j.Slf4j;

@Service
Expand Down Expand Up @@ -127,4 +133,24 @@ public void executeShellScript(String scriptFilePath, String logFilePath) throws
log.error(scriptFilePath + " failed with exit code " + exitCode);
}
}

@Override
public void updateCredentialFile(User admin) throws Exception {
String path = "/var/autocd/admin-credential.json";
File credentialFile = new File(path);

ObjectMapper objectMapper = new ObjectMapper();
if (credentialFile.exists()) {
Map<String, Object> jsonData = objectMapper.readValue(credentialFile, new TypeReference<Map<String, Object>>() {});
jsonData.put("username", admin.getUsername());
jsonData.put("password", admin.getPassword());
objectMapper.writeValue(credentialFile, jsonData);
log.info("Credentials file updated");
}
Map<String, Object> defaultData = new HashMap<>();
defaultData.put("username", admin.getUsername());
defaultData.put("password", admin.getPassword());
objectMapper.writeValue(credentialFile, defaultData);
log.info("Credentials file created");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.IOException;

import com.fordevio.producer.models.User;

public interface FileHandlerSvc {

public void createDirIfNot(String dirPath) throws Exception;
Expand All @@ -16,4 +18,5 @@ public interface FileHandlerSvc {
public String getProjectLogs(String name) throws Exception;
public String getProjectLogPath(String name) throws Exception;
public void executeShellScript(String scriptFilePath, String logFilePath) throws IOException, InterruptedException;
public void updateCredentialFile(User user) throws Exception;
}

0 comments on commit 4f5091e

Please sign in to comment.