Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the hashed name for the path to each ssh-secret. (eclipse#14151). #14243

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import static com.google.common.base.Strings.isNullOrEmpty;

import com.google.common.hash.Hashing;
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
import io.fabric8.kubernetes.api.model.ConfigMapVolumeSourceBuilder;
Expand All @@ -24,6 +25,7 @@
import io.fabric8.kubernetes.api.model.VolumeBuilder;
import io.fabric8.kubernetes.api.model.VolumeMount;
import io.fabric8.kubernetes.api.model.VolumeMountBuilder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -139,10 +141,11 @@ private void doProvisionSshKey(SshPair sshPair, KubernetesEnvironment k8sEnv, St
.values()
.forEach(
p ->
mountSshKeySecret(secret.getMetadata().getName(), validNameForSecret, p.getSpec()));
mountSshKeySecret(
secret.getMetadata().getName(), getSha256(sshPair.getName()), p.getSpec()));
}

private void mountSshKeySecret(String secretName, String sshKeyName, PodSpec podSpec) {
private void mountSshKeySecret(String secretName, String sshKeyNameHashed, PodSpec podSpec) {
podSpec
.getVolumes()
.add(
Expand All @@ -158,7 +161,7 @@ private void mountSshKeySecret(String secretName, String sshKeyName, PodSpec pod
.withName(secretName)
.withNewReadOnly(false)
.withReadOnly(false)
.withMountPath(SSH_BASE_CONFIG_PATH + sshKeyName)
.withMountPath(SSH_BASE_CONFIG_PATH + sshKeyNameHashed)
.build();
container.getVolumeMounts().add(volumeMount);
});
Expand Down Expand Up @@ -238,7 +241,7 @@ private String buildConfig(@NotNull String name) {
+ host
+ "\nIdentityFile "
+ SSH_BASE_CONFIG_PATH
+ getValidNameForSecret(name)
+ getSha256(name)
+ "/"
+ SSH_PRIVATE_KEY
+ "\n";
Expand All @@ -248,4 +251,9 @@ private String buildConfig(@NotNull String name) {
private String getValidNameForSecret(@NotNull String name) {
return name.replace(".", "-");
}

/** Returns a sha256-hashed string value. */
private String getSha256(@NotNull String value) {
return Hashing.sha256().hashString(value, StandardCharsets.UTF_8).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
import static org.testng.Assert.assertTrue;

import com.google.common.collect.ImmutableList;
import com.google.common.hash.Hashing;
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodSpec;
import io.fabric8.kubernetes.api.model.Secret;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Map;
import java.util.UUID;
Expand Down Expand Up @@ -124,12 +126,20 @@ public void addSshKeysConfigInPod() throws Exception {

String sshConfig = mapData.get("ssh_config");
assertTrue(sshConfig.contains("host " + keyName1));
assertTrue(sshConfig.contains("IdentityFile " + "/etc/ssh/" + keyName1 + "/ssh-privatekey"));
assertTrue(
sshConfig.contains("IdentityFile /etc/ssh/" + getSha256(keyName1) + "/ssh-privatekey"));

assertTrue(sshConfig.contains("host *"));
assertTrue(sshConfig.contains("IdentityFile " + "/etc/ssh/" + keyName2 + "/ssh-privatekey"));
assertTrue(
sshConfig.contains("IdentityFile /etc/ssh/" + getSha256(keyName2) + "/ssh-privatekey"));

assertTrue(sshConfig.contains("host github.com"));
assertTrue(sshConfig.contains("IdentityFile /etc/ssh/github-com/ssh-privatekey"));
assertTrue(sshConfig.contains("host " + keyName3));
assertTrue(
sshConfig.contains("IdentityFile /etc/ssh/" + getSha256(keyName3) + "/ssh-privatekey"));
}

/** Returns a sha256-hashed string value. */
private String getSha256(String value) {
return Hashing.sha256().hashString(value, StandardCharsets.UTF_8).toString();
}
}