Skip to content

Commit f3ad811

Browse files
fix: [CDS-42134]: added gcpFileKey creds for shell script and native … (#36714)
* fix: [CDS-42134]: added gcpFileKey creds for shell script and native helm steps in NG * fix: [CDS-42134]: added gcpFileKey creds for native helm steps in CG * fix: [CDS-42134]: remove unused imports
1 parent b483fd4 commit f3ad811

23 files changed

Lines changed: 157 additions & 44 deletions

File tree

260-delegate/src/test/java/software/wings/delegatetasks/helm/HelmCommandTaskTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import static io.harness.rule.OwnerRule.ABOSII;
1212

1313
import static org.assertj.core.api.Assertions.assertThat;
14-
import static org.mockito.ArgumentMatchers.nullable;
1514
import static org.mockito.Matchers.any;
1615
import static org.mockito.Mockito.doReturn;
1716
import static org.mockito.Mockito.doThrow;
@@ -28,6 +27,7 @@
2827
import io.harness.delegate.beans.TaskData;
2928
import io.harness.delegate.task.helm.HelmCommandResponse;
3029
import io.harness.k8s.K8sGlobalConfigService;
30+
import io.harness.k8s.model.KubernetesConfig;
3131
import io.harness.logging.CommandExecutionStatus;
3232
import io.harness.logging.LogCallback;
3333
import io.harness.logging.NoopExecutionCallback;
@@ -45,7 +45,6 @@
4545
import software.wings.helpers.ext.helm.request.HelmRollbackCommandRequest;
4646
import software.wings.helpers.ext.helm.response.HelmInstallCommandResponse;
4747
import software.wings.helpers.ext.helm.response.HelmReleaseHistoryCommandResponse;
48-
import software.wings.service.impl.ContainerServiceParams;
4948

5049
import java.io.IOException;
5150
import org.apache.commons.lang3.NotImplementedException;
@@ -78,6 +77,8 @@ public void setup() {
7877
doReturn(ensureHelmInstalledResponse).when(helmDeployService).ensureHelmInstalled(any(HelmCommandRequest.class));
7978

8079
when(k8sGlobalConfigService.getOcPath()).thenReturn("/tmp");
80+
when(containerDeploymentDelegateHelper.getKubernetesConfig(any())).thenReturn(KubernetesConfig.builder().build());
81+
when(containerDeploymentDelegateHelper.createKubeConfig(any())).thenReturn(".kube/config");
8182
}
8283

8384
@Test
@@ -87,9 +88,6 @@ public void testInitPrerequisite() {
8788
String kubeConfigLocation = ".kube/config";
8889

8990
doReturn(mock(LogCallback.class)).when(dummyCommandRequest).getExecutionLogCallback();
90-
doReturn(kubeConfigLocation)
91-
.when(containerDeploymentDelegateHelper)
92-
.createAndGetKubeConfigLocation(nullable(ContainerServiceParams.class));
9391
helmCommandTask.run(dummyCommandRequest);
9492

9593
verify(helmDeployService, times(1)).ensureHelmInstalled(dummyCommandRequest);

400-rest/src/main/java/software/wings/sm/states/HelmDeployState.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ protected int getPreviousReleaseVersion(ExecutionContext context, Application ap
533533
int prevVersion = 0;
534534
HelmReleaseHistoryCommandRequest helmReleaseHistoryCommandRequest =
535535
HelmReleaseHistoryCommandRequest.builder()
536+
.activityId(obtainActivityId(context))
536537
.releaseName(releaseName)
537538
.containerServiceParams(containerServiceParams)
538539
.gitConfig(gitConfig)

930-delegate-tasks/src/main/java/io/harness/delegate/task/helm/HelmCommandDataMapperNG.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public HelmCommandData getHelmCmdDataNG(@NonNull HelmCommandRequestNG helmComman
3636
.namespace(helmCommandRequestNG.getNamespace())
3737
.valueMap(helmChartManifestDelegateConfig.getHelmCommandFlag().getValueMap())
3838
.isHelmCmdFlagsNull(helmChartManifestDelegateConfig.getHelmCommandFlag() == null)
39+
.gcpKeyPath(helmCommandRequestNG.getGcpKeyPath())
3940
.build();
4041

4142
if (helmCommandRequestNG instanceof HelmInstallCommandRequestNG) {

930-delegate-tasks/src/main/java/io/harness/delegate/task/helm/HelmCommandTaskNG.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import static io.harness.data.structure.UUIDGenerator.generateUuid;
1212
import static io.harness.filesystem.FileIo.createDirectoryIfDoesNotExist;
1313
import static io.harness.filesystem.FileIo.waitForDirectoryToBeAccessibleOutOfProcess;
14+
import static io.harness.filesystem.FileIo.writeUtf8StringToFile;
1415
import static io.harness.logging.CommandExecutionStatus.FAILURE;
1516

1617
import static java.lang.String.format;
@@ -19,6 +20,8 @@
1920
import io.harness.delegate.beans.DelegateResponseData;
2021
import io.harness.delegate.beans.DelegateTaskPackage;
2122
import io.harness.delegate.beans.DelegateTaskResponse;
23+
import io.harness.delegate.beans.connector.gcpconnector.GcpConnectorDTO;
24+
import io.harness.delegate.beans.connector.gcpconnector.GcpManualDetailsDTO;
2225
import io.harness.delegate.beans.logstreaming.CommandUnitProgress;
2326
import io.harness.delegate.beans.logstreaming.CommandUnitsProgress;
2427
import io.harness.delegate.beans.logstreaming.ILogStreamingTaskClient;
@@ -29,16 +32,20 @@
2932
import io.harness.delegate.task.ManifestDelegateConfigHelper;
3033
import io.harness.delegate.task.TaskParameters;
3134
import io.harness.delegate.task.k8s.ContainerDeploymentDelegateBaseHelper;
35+
import io.harness.delegate.task.k8s.GcpK8sInfraDelegateConfig;
3236
import io.harness.exception.ExceptionUtils;
3337
import io.harness.exception.ExplanationException;
3438
import io.harness.exception.HintException;
3539
import io.harness.exception.sanitizer.ExceptionMessageSanitizer;
40+
import io.harness.k8s.K8sConstants;
3641
import io.harness.k8s.K8sGlobalConfigService;
3742
import io.harness.logging.CommandExecutionStatus;
3843
import io.harness.logging.LogCallback;
3944
import io.harness.logging.LogLevel;
4045

4146
import com.google.inject.Inject;
47+
import java.io.IOException;
48+
import java.nio.file.Path;
4249
import java.nio.file.Paths;
4350
import java.util.LinkedHashMap;
4451
import java.util.Map;
@@ -163,7 +170,7 @@ public void decryptRequestDTOs(HelmCommandRequestNG commandRequestNG) {
163170
containerDeploymentDelegateBaseHelper.decryptK8sInfraDelegateConfig(commandRequestNG.getK8sInfraDelegateConfig());
164171
}
165172

166-
private void init(HelmCommandRequestNG commandRequestNG, LogCallback logCallback) {
173+
private void init(HelmCommandRequestNG commandRequestNG, LogCallback logCallback) throws IOException {
167174
commandRequestNG.setLogCallback(logCallback);
168175
logCallback.saveExecutionLog("Creating KubeConfig", LogLevel.INFO, CommandExecutionStatus.RUNNING);
169176
String configLocation = containerDeploymentDelegateBaseHelper.createKubeConfig(
@@ -172,6 +179,21 @@ private void init(HelmCommandRequestNG commandRequestNG, LogCallback logCallback
172179
logCallback.saveExecutionLog(
173180
"Setting KubeConfig\nKUBECONFIG_PATH=" + configLocation, LogLevel.INFO, CommandExecutionStatus.RUNNING);
174181

182+
if (commandRequestNG.getK8sInfraDelegateConfig() instanceof GcpK8sInfraDelegateConfig) {
183+
GcpK8sInfraDelegateConfig gcpK8sInfraDelegateConfig =
184+
(GcpK8sInfraDelegateConfig) commandRequestNG.getK8sInfraDelegateConfig();
185+
GcpConnectorDTO gcpConnectorDTO = gcpK8sInfraDelegateConfig.getGcpConnectorDTO();
186+
if (gcpConnectorDTO.getCredential().getConfig() instanceof GcpManualDetailsDTO) {
187+
GcpManualDetailsDTO gcpManualDetailsDTO = (GcpManualDetailsDTO) gcpConnectorDTO.getCredential().getConfig();
188+
char[] gcpFileKeyContent = gcpManualDetailsDTO.getSecretKeyRef().getDecryptedValue();
189+
Path gcpKeyFilePath = Paths.get(commandRequestNG.getWorkingDir(), K8sConstants.GCP_JSON_KEY_FILE_NAME);
190+
writeUtf8StringToFile(gcpKeyFilePath.toString(), String.valueOf(gcpFileKeyContent));
191+
commandRequestNG.setGcpKeyPath(gcpKeyFilePath.toAbsolutePath().toString());
192+
logCallback.saveExecutionLog("Created and Setting gcpFileKeyPath at: " + gcpKeyFilePath.toAbsolutePath(),
193+
LogLevel.INFO, CommandExecutionStatus.RUNNING);
194+
}
195+
}
196+
175197
ensureHelmInstalled(commandRequestNG);
176198
logCallback.saveExecutionLog("\nDone.", LogLevel.INFO, CommandExecutionStatus.SUCCESS);
177199
}

930-delegate-tasks/src/main/java/io/harness/delegate/task/helm/HelmDeployServiceImplNG.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ public HelmCommandResponseNG deploy(HelmInstallCommandRequestNG commandRequest)
169169
int prevVersion = -1;
170170
boolean isInstallUpgrade = false;
171171
List<KubernetesResource> resources = Collections.emptyList();
172+
String initWorkingDir = commandRequest.getWorkingDir();
172173
try {
173174
HelmInstallCmdResponseNG commandResponse;
174175
logCallback.saveExecutionLog(
@@ -279,7 +280,7 @@ public HelmCommandResponseNG deploy(HelmInstallCommandRequestNG commandRequest)
279280
logCallback.saveExecutionLog("Deployment failed.");
280281
deleteAndPurgeHelmRelease(commandRequest, logCallback);
281282
}
282-
cleanUpWorkingDirectory(Paths.get(commandRequest.getWorkingDir()).getParent().toString());
283+
cleanUpWorkingDirectory(initWorkingDir);
283284
}
284285
}
285286

@@ -394,6 +395,12 @@ private List<ContainerInfo> getKubectlContainerInfos(HelmCommandRequestNG comman
394395
if (ocPath.isPresent()) {
395396
commandRequest.setOcPath(ocPath.get());
396397
}
398+
if (isNotEmpty(commandRequest.getGcpKeyPath())) {
399+
Path oldPath = Paths.get(commandRequest.getGcpKeyPath());
400+
Path newPath = Paths.get(commandRequest.getWorkingDir(), K8sConstants.GCP_JSON_KEY_FILE_NAME);
401+
Files.move(oldPath, newPath);
402+
commandRequest.setGcpKeyPath(newPath.toAbsolutePath().toString());
403+
}
397404
success = success
398405
&& doStatusCheckAllResourcesForHelm(client, entry.getValue(), commandRequest.getOcPath(),
399406
commandRequest.getWorkingDir(), namespace, commandRequest.getKubeConfigLocation(), logCallback);

930-delegate-tasks/src/main/java/io/harness/delegate/task/k8s/K8sTaskHelperBase.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,10 +1062,18 @@ public String getRolloutHistoryCommandForDeploymentConfig(
10621062
public ProcessResult executeCommandUsingUtils(K8sDelegateTaskParams k8sDelegateTaskParams,
10631063
LogOutputStream statusInfoStream, LogOutputStream statusErrorStream, String command,
10641064
Map<String, String> environment) throws Exception {
1065+
addGcpCredentialsToEnvironmentIfExist(k8sDelegateTaskParams.getWorkingDirectory(), environment);
10651066
return executeCommandUsingUtils(
10661067
k8sDelegateTaskParams.getWorkingDirectory(), statusInfoStream, statusErrorStream, command, environment);
10671068
}
10681069

1070+
private void addGcpCredentialsToEnvironmentIfExist(String directory, Map<String, String> environment) {
1071+
Path googleApplicationCredentialsPath = Paths.get(directory).resolve(K8sConstants.GCP_JSON_KEY_FILE_NAME);
1072+
if (Files.exists(googleApplicationCredentialsPath)) {
1073+
environment.put("GOOGLE_APPLICATION_CREDENTIALS", googleApplicationCredentialsPath.toAbsolutePath().toString());
1074+
}
1075+
}
1076+
10691077
public String getRolloutStatusCommandForDeploymentConfig(
10701078
String ocPath, String kubeConfigPath, KubernetesResourceId resourceId) {
10711079
String namespace = "";

930-delegate-tasks/src/main/java/io/harness/delegate/task/shell/ShellScriptTaskNG.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@
1111
import io.harness.delegate.beans.DelegateResponseData;
1212
import io.harness.delegate.beans.DelegateTaskPackage;
1313
import io.harness.delegate.beans.DelegateTaskResponse;
14+
import io.harness.delegate.beans.connector.gcpconnector.GcpConnectorDTO;
15+
import io.harness.delegate.beans.connector.gcpconnector.GcpManualDetailsDTO;
1416
import io.harness.delegate.beans.logstreaming.CommandUnitsProgress;
1517
import io.harness.delegate.beans.logstreaming.ILogStreamingTaskClient;
1618
import io.harness.delegate.beans.logstreaming.UnitProgressDataMapper;
1719
import io.harness.delegate.task.AbstractDelegateRunnableTask;
1820
import io.harness.delegate.task.TaskParameters;
1921
import io.harness.delegate.task.k8s.ContainerDeploymentDelegateBaseHelper;
22+
import io.harness.delegate.task.k8s.GcpK8sInfraDelegateConfig;
2023
import io.harness.k8s.K8sConstants;
2124
import io.harness.k8s.KubernetesContainerService;
2225
import io.harness.logging.CommandExecutionStatus;
26+
import io.harness.security.encryption.EncryptedDataDetail;
2327
import io.harness.security.encryption.SecretDecryptionService;
2428
import io.harness.shell.ExecuteCommandResponse;
2529
import io.harness.shell.ScriptProcessExecutor;
@@ -29,6 +33,7 @@
2933
import io.harness.shell.SshSessionManager;
3034

3135
import com.google.inject.Inject;
36+
import java.util.List;
3237
import java.util.function.BooleanSupplier;
3338
import java.util.function.Consumer;
3439
import lombok.extern.slf4j.Slf4j;
@@ -136,6 +141,19 @@ private ShellExecutorConfig getShellExecutorConfig(ShellScriptTaskParametersNG t
136141
? containerDeploymentDelegateBaseHelper.getKubeconfigFileContent(taskParameters.getK8sInfraDelegateConfig())
137142
: "";
138143

144+
char[] serviceAccountKeyFileContent = null;
145+
if (taskParameters.getK8sInfraDelegateConfig() instanceof GcpK8sInfraDelegateConfig) {
146+
GcpK8sInfraDelegateConfig gcpK8sInfraDelegateConfig =
147+
(GcpK8sInfraDelegateConfig) taskParameters.getK8sInfraDelegateConfig();
148+
GcpConnectorDTO gcpConnectorDTO = gcpK8sInfraDelegateConfig.getGcpConnectorDTO();
149+
if (gcpConnectorDTO.getCredential().getConfig() instanceof GcpManualDetailsDTO) {
150+
GcpManualDetailsDTO gcpManualDetailsDTO = (GcpManualDetailsDTO) gcpConnectorDTO.getCredential().getConfig();
151+
List<EncryptedDataDetail> encryptedDataDetails = gcpK8sInfraDelegateConfig.getEncryptionDataDetails();
152+
secretDecryptionService.decrypt(gcpManualDetailsDTO, encryptedDataDetails);
153+
serviceAccountKeyFileContent = gcpManualDetailsDTO.getSecretKeyRef().getDecryptedValue();
154+
}
155+
}
156+
139157
return ShellExecutorConfig.builder()
140158
.accountId(taskParameters.getAccountId())
141159
.executionId(taskParameters.getExecutionId())
@@ -144,6 +162,7 @@ private ShellExecutorConfig getShellExecutorConfig(ShellScriptTaskParametersNG t
144162
.environment(taskParameters.getEnvironmentVariables())
145163
.kubeConfigContent(kubeConfigFileContent)
146164
.scriptType(taskParameters.getScriptType())
165+
.gcpKeyFileContent(serviceAccountKeyFileContent)
147166
.build();
148167
}
149168
}

930-delegate-tasks/src/main/java/software/wings/delegatetasks/helm/HelmCommandTask.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
package software.wings.delegatetasks.helm;
99

1010
import static io.harness.annotations.dev.HarnessTeam.CDP;
11+
import static io.harness.filesystem.FileIo.createDirectoryIfDoesNotExist;
12+
import static io.harness.filesystem.FileIo.waitForDirectoryToBeAccessibleOutOfProcess;
13+
import static io.harness.filesystem.FileIo.writeUtf8StringToFile;
1114

1215
import static java.lang.String.format;
16+
import static org.apache.commons.lang3.StringUtils.replace;
1317

1418
import io.harness.annotations.dev.HarnessModule;
1519
import io.harness.annotations.dev.OwnedBy;
@@ -22,7 +26,9 @@
2226
import io.harness.delegate.task.helm.HelmCommandResponse;
2327
import io.harness.exception.HarnessException;
2428
import io.harness.exception.sanitizer.ExceptionMessageSanitizer;
29+
import io.harness.k8s.K8sConstants;
2530
import io.harness.k8s.K8sGlobalConfigService;
31+
import io.harness.k8s.model.KubernetesConfig;
2632
import io.harness.logging.CommandExecutionStatus;
2733
import io.harness.logging.LogCallback;
2834
import io.harness.logging.LogLevel;
@@ -41,6 +47,8 @@
4147
import software.wings.helpers.ext.helm.request.HelmRollbackCommandRequest;
4248

4349
import com.google.inject.Inject;
50+
import java.nio.file.Path;
51+
import java.nio.file.Paths;
4452
import java.util.function.BooleanSupplier;
4553
import java.util.function.Consumer;
4654
import lombok.extern.slf4j.Slf4j;
@@ -58,7 +66,9 @@ public class HelmCommandTask extends AbstractDelegateRunnableTask {
5866
@Inject private ContainerDeploymentDelegateHelper containerDeploymentDelegateHelper;
5967
@Inject private HelmCommandHelper helmCommandHelper;
6068
@Inject private K8sGlobalConfigService k8sGlobalConfigService;
61-
69+
protected static final String WORKING_DIR = "./repository/helm/source/${"
70+
+ "ACTIVITY_ID"
71+
+ "}";
6272
public HelmCommandTask(DelegateTaskPackage delegateTaskPackage, ILogStreamingTaskClient logStreamingTaskClient,
6373
Consumer<DelegateTaskResponse> consumer, BooleanSupplier preExecute) {
6474
super(delegateTaskPackage, logStreamingTaskClient, consumer, preExecute);
@@ -130,12 +140,34 @@ public HelmCommandExecutionResponse run(TaskParameters parameters) {
130140
private void init(HelmCommandRequest helmCommandRequest, LogCallback executionLogCallback) throws Exception {
131141
helmCommandRequest.setExecutionLogCallback(executionLogCallback);
132142
executionLogCallback.saveExecutionLog("Creating KubeConfig", LogLevel.INFO, CommandExecutionStatus.RUNNING);
133-
String configLocation = containerDeploymentDelegateHelper.createAndGetKubeConfigLocation(
134-
helmCommandRequest.getContainerServiceParams());
143+
KubernetesConfig kubernetesConfig =
144+
containerDeploymentDelegateHelper.getKubernetesConfig(helmCommandRequest.getContainerServiceParams());
145+
String configLocation = containerDeploymentDelegateHelper.createKubeConfig(kubernetesConfig);
135146
helmCommandRequest.setKubeConfigLocation(configLocation);
136147
executionLogCallback.saveExecutionLog(
137148
"Setting KubeConfig\nKUBECONFIG_PATH=" + configLocation, LogLevel.INFO, CommandExecutionStatus.RUNNING);
138149

150+
String workingDir = Paths
151+
.get(replace(WORKING_DIR,
152+
"${"
153+
+ "ACTIVITY_ID"
154+
+ "}",
155+
helmCommandRequest.getActivityId()))
156+
.normalize()
157+
.toAbsolutePath()
158+
.toString();
159+
160+
createDirectoryIfDoesNotExist(workingDir);
161+
waitForDirectoryToBeAccessibleOutOfProcess(workingDir, 10);
162+
163+
if (kubernetesConfig.getGcpAccountKeyFileContent().isPresent()) {
164+
Path gcpKeyFilePath = Paths.get(workingDir, K8sConstants.GCP_JSON_KEY_FILE_NAME);
165+
writeUtf8StringToFile(gcpKeyFilePath.toString(), kubernetesConfig.getGcpAccountKeyFileContent().get());
166+
helmCommandRequest.setGcpKeyPath(gcpKeyFilePath.toAbsolutePath().toString());
167+
executionLogCallback.saveExecutionLog("Created and Setting gcpFileKeyPath at: " + gcpKeyFilePath.toAbsolutePath(),
168+
LogLevel.INFO, CommandExecutionStatus.RUNNING);
169+
}
170+
139171
ensureHelmInstalled(helmCommandRequest);
140172
executionLogCallback.saveExecutionLog("\nDone.", LogLevel.INFO, CommandExecutionStatus.SUCCESS);
141173
}

930-delegate-tasks/src/main/java/software/wings/helpers/ext/container/ContainerDeploymentDelegateHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void persistKubernetesConfig(K8sClusterConfig k8sClusterConfig, String wo
7878
kubernetesContainerService.persistKubernetesConfig(getKubernetesConfig(k8sClusterConfig, false), workingDir);
7979
}
8080

81-
private String createKubeConfig(KubernetesConfig kubernetesConfig) {
81+
public String createKubeConfig(KubernetesConfig kubernetesConfig) {
8282
try {
8383
String configFileContent = kubernetesContainerService.getConfigFileContent(kubernetesConfig);
8484
String md5Hash = DigestUtils.md5Hex(configFileContent);

930-delegate-tasks/src/main/java/software/wings/helpers/ext/helm/HelmCommandDataMapper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public HelmCommandData getHelmCommandData(@NonNull HelmCommandRequest helmComman
3333
.workingDir(helmCommandRequest.getWorkingDir())
3434
.commandFlags(helmCommandRequest.getCommandFlags())
3535
.repoName(helmCommandRequest.getRepoName())
36+
.gcpKeyPath(helmCommandRequest.getGcpKeyPath())
3637
.build();
3738

3839
if (helmCommandRequest.getChartSpecification() != null) {

0 commit comments

Comments
 (0)