Skip to content

Commit

Permalink
Added: Coder Param (APP_ID and APP_SECRET)
Browse files Browse the repository at this point in the history
  • Loading branch information
djuarezgf committed Feb 6, 2025
1 parent a21f465 commit a0f215c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 20 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.0.1 - 2025-02-05]
## [0.0.1 - 2025-02-06]
### Added
- First version of the project
- Spring Application
Expand Down Expand Up @@ -206,3 +206,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- EncryptionConverter to encrypt results url
- Coder Configuration
- App Register Service
- Coder Param (APP_ID and APP_SECRET)
5 changes: 3 additions & 2 deletions src/main/java/de/samply/app/ProjectManagerConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,6 @@ public class ProjectManagerConst {
public final static String CODER_API_PATH = "/api/v2";
public final static String CODER_SESSION_TOKEN_HEADER = "Coder-Session-Token";

public final static String CODER_SAMPLY_BEAM_APP_ID_PARAM_KEY = "Samply.Beam: App ID (short)";
public final static String CODER_SAMPLY_BEAM_APP_SECRET_PARAM_KEY = "Samply.Beam: App Secret";
public final static String CODER_DELETE_TRANSITION = "delete";

// Environment Variables
Expand Down Expand Up @@ -410,6 +408,7 @@ public class ProjectManagerConst {
public final static String CODER_DELETE_PATH = "CODER_DELETE_PATH";
public final static String CODER_SESSION_TOKEN = "CODER_SESSION_TOKEN";
public final static String CODER_CRON_EXPRESSION = "CODER_CRON_EXPRESSION";
public final static String CODER_WORKSPACE_NAME_MAX_LENGTH = "CODER_WORKSPACE_NAME_MAX_LENGTH";

public final static String CODER_BEAM_ID_SUFFIX = "CODER_BEAM_ID_SUFFIX";
public final static String CODER_TEST_FILE_BEAM_ID = "CODER_TEST_FILE_BEAM_ID";
Expand Down Expand Up @@ -518,6 +517,8 @@ public class ProjectManagerConst {
public final static String CODER_BEAM_ID_SUFFIX_SV = HEAD_SV + CODER_BEAM_ID_SUFFIX + BOTTOM_SV;
public final static String CODER_TEST_FILE_BEAM_ID_SV = HEAD_SV + CODER_TEST_FILE_BEAM_ID + BOTTOM_SV;
public final static String CODER_CRON_EXPRESSION_SV = HEAD_SV + CODER_CRON_EXPRESSION + BOTTOM_SV;
public final static String CODER_WORKSPACE_NAME_MAX_LENGTH_SV = HEAD_SV + CODER_WORKSPACE_NAME_MAX_LENGTH + ":32" + BOTTOM_SV;


public final static String ENABLE_CODER_SV = HEAD_SV + ENABLE_CODER + ":true" + BOTTOM_SV;

Expand Down
24 changes: 24 additions & 0 deletions src/main/java/de/samply/coder/CoderParam.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.samply.coder;

import de.samply.coder.request.CreateRequestParameter;
import de.samply.db.model.ProjectCoder;
import jakarta.validation.constraints.NotNull;
import org.springframework.util.StringUtils;

public enum CoderParam {
APP_ID,
APP_SECRET;

public static void replaceParameters(@NotNull CreateRequestParameter parameter, @NotNull ProjectCoder projectCoder) {
replaceParameter(parameter, APP_ID, projectCoder.getAppId());
replaceParameter(parameter, APP_SECRET, projectCoder.getAppSecret());
}

private static void replaceParameter(CreateRequestParameter parameter, CoderParam coderParam, String value) {
if (StringUtils.hasText(parameter.getValue()) && StringUtils.hasText(value)) {
parameter.setValue(parameter.getValue().replace("${" + coderParam.name() + "}", value));
}
}


}
55 changes: 38 additions & 17 deletions src/main/java/de/samply/coder/CoderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
import reactor.core.publisher.Mono;

import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

@Slf4j
Expand All @@ -40,13 +37,14 @@ public class CoderService {
private final NotificationService notificationService;
private final SessionUser sessionUser;
private final CoderConfiguration coderConfiguration;
private final WebClient webClient;

private final String coderCreatePath;
private final String coderDeletePath;
private final String coderSessionToken;
private final String researchEnvironmentUrl;

private final WebClient webClient;
private final int coderWorkspaceMaxLength;


public CoderService(
Expand All @@ -55,20 +53,22 @@ public CoderService(
ProjectBridgeheadUserRepository projectBridgeheadUserRepository,
SessionUser sessionUser,
CoderConfiguration coderConfiguration,
WebClientFactory webClientFactory,
@Value(ProjectManagerConst.ENABLE_CODER_SV) boolean coderEnabled,
@Value(ProjectManagerConst.CODER_BASE_URL_SV) String coderBaseUrl,
@Value(ProjectManagerConst.CODER_ORGANISATION_ID_SV) String coderOrganizationId,
@Value(ProjectManagerConst.CODER_CREATE_PATH_SV) String coderCreatePath,
@Value(ProjectManagerConst.CODER_DELETE_PATH_SV) String coderDeletePath,
@Value(ProjectManagerConst.CODER_SESSION_TOKEN_SV) String coderSessionToken,
WebClientFactory webClientFactory) {
@Value(ProjectManagerConst.CODER_WORKSPACE_NAME_MAX_LENGTH_SV) int coderWorkspaceMaxLength) {
this.coderEnabled = coderEnabled;
this.projectCoderRepository = projectCoderRepository;
this.notificationService = notificationService;
this.projectBridgeheadUserRepository = projectBridgeheadUserRepository;
this.sessionUser = sessionUser;
this.coderConfiguration = coderConfiguration;
this.coderSessionToken = coderSessionToken;
this.coderWorkspaceMaxLength = coderWorkspaceMaxLength;
Map<String, String> pathVariables = Map.of(ProjectManagerConst.CODER_ORGANISATION_ID, coderOrganizationId);
this.coderCreatePath = replaceVariablesInPath(coderCreatePath, pathVariables);
this.coderDeletePath = replaceVariablesInPath(coderDeletePath, pathVariables);
Expand Down Expand Up @@ -200,23 +200,44 @@ private Mono<Response> deleteWorkspace(ProjectCoder projectCoder) {
private CreateRequestBody generateCreateRequestBody(ProjectCoder projectCoder) {
CreateRequestBody createRequestBody = coderConfiguration.cloneCreateRequestBody(projectCoder.getProjectBridgeheadUser().getProjectBridgehead().getProject().getType());
createRequestBody.setName(projectCoder.getAppId());
addRichParameterValues(createRequestBody, projectCoder);
replaceParameterValues(createRequestBody, projectCoder);
return createRequestBody;
}

private void addRichParameterValues(CreateRequestBody createRequestBody, ProjectCoder projectCoder) {
List<CreateRequestParameter> createRequestParameters = List.of(
new CreateRequestParameter(ProjectManagerConst.CODER_SAMPLY_BEAM_APP_ID_PARAM_KEY, projectCoder.getAppId()),
new CreateRequestParameter(ProjectManagerConst.CODER_SAMPLY_BEAM_APP_SECRET_PARAM_KEY, projectCoder.getAppSecret())
);
createRequestBody.setRichParameterValues(createRequestParameters.toArray(CreateRequestParameter[]::new));
private void replaceParameterValues(CreateRequestBody createRequestBody, ProjectCoder projectCoder) {
Arrays.stream(createRequestBody.getRichParameterValues()).forEach(parameter -> CoderParam.replaceParameters(parameter, projectCoder));
}

public String fetchCoderAppId(@NotNull ProjectBridgeheadUser projectBridgeheadUser) {
return projectBridgeheadUser.getEmail().substring(0, projectBridgeheadUser.getEmail().indexOf("@"))
.replaceAll("[^a-zA-Z0-9]", "") +
projectBridgeheadUser.getProjectBridgehead().getProject().getCode() +
projectBridgeheadUser.getProjectBridgehead().getProject().getState().toString().toLowerCase();
String email = projectBridgeheadUser.getEmail().substring(0, projectBridgeheadUser.getEmail().indexOf("@")).replaceAll("[^a-zA-Z0-9]", "");
String projectCode = projectBridgeheadUser.getProjectBridgehead().getProject().getCode();
String state = projectBridgeheadUser.getProjectBridgehead().getProject().getState().toString().toLowerCase().substring(0,3); // Only three first characters

String coderAppId = email + projectCode + state;
// Check if the coderAppId exceeds the maximum allowed length
if (coderAppId.length() > coderWorkspaceMaxLength) {
// Calculate the surplus length that needs to be trimmed
int surplus = coderAppId.length() - coderWorkspaceMaxLength;
// Reduce the projectCode length if necessary, prioritizing keeping at least half of it
int maxProjectCodeLength = (projectCode.length() / 2 > surplus)
? projectCode.length() / 2
: projectCode.length() - surplus;
// Trim the projectCode to the calculated length
projectCode = projectCode.substring(0, maxProjectCodeLength);
// Rebuild the coderAppId with the reduced projectCode
coderAppId = email + projectCode + state;
// If the coderAppId is still too long, reduce the email length
if (coderAppId.length() > coderWorkspaceMaxLength) {
// Recalculate the surplus length after trimming projectCode
surplus = coderAppId.length() - coderWorkspaceMaxLength;
// Reduce the email length from the right, ensuring at least 1 character remains
int newEmailLength = Math.max(1, email.length() - surplus);
email = email.substring(0, newEmailLength);
// Final rebuild of the coderAppId
coderAppId = email + projectCode + state;
}
}
return coderAppId;
}

private String generateAppSecret() {
Expand Down

0 comments on commit a0f215c

Please sign in to comment.