Skip to content

Commit

Permalink
Set wildcard host name if key name start from 'default-' according to…
Browse files Browse the repository at this point in the history
… the #13494 (comment)

Signed-off-by: Vitalii Parfonov <vparfono@redhat.com>
  • Loading branch information
vparfonov committed Jul 18, 2019
1 parent 4050b06 commit d8886af
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,22 +210,28 @@ private void mountConfigFile(PodSpec podSpec, String sshConfigMapName) {
*
* <pre>
* host github.com
* HostName github.com
* IdentityFile /etc/ssh/github-com/ssh-privatekey
* </pre>
*
* @param host the host of version control service (e.g. github.com, gitlab.com and etc)
* or
*
* <pre>
* host *
* IdentityFile /etc/ssh/default-123456/ssh-privatekey
* </pre>
*
* @param name the of key given during generate for vcs service we will consider it as host of
* version control service (e.g. github.com, gitlab.com and etc) if name starts from
* "default-{anyString}" it will be replaced on wildcard "*" host name
* @return the ssh configuration which include host and identity file location
*/
private String buildConfig(@NotNull String host) {
private String buildConfig(@NotNull String name) {
String host = name.startsWith("default-") ? "*" : name;
return "host "
+ host
+ "\n"
+ "HostName "
+ host
+ "\nIdentityFile "
+ SSH_BASE_CONFIG_PATH
+ getValidNameForSecret(host)
+ getValidNameForSecret(name)
+ "/"
+ SSH_PRIVATE_KEY
+ "\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,22 @@ public void doNotDoAnythingIfNoSshKeys() throws Exception {

@Test
public void addSshKeysConfigInPod() throws Exception {
String keyName = UUID.randomUUID().toString();
String keyName1 = UUID.randomUUID().toString();
String keyName2 = "default-" + UUID.randomUUID().toString();
String keyName3 = "github.com";
when(sshManager.getPairs(someUser, "vcs"))
.thenReturn(
ImmutableList.of(new SshPairImpl(someUser, "vcs", keyName, "public", "private")));
ImmutableList.of(
new SshPairImpl(someUser, "vcs", keyName1, "public", "private"),
new SshPairImpl(someUser, "vcs", keyName2, "public", "private"),
new SshPairImpl(someUser, "vcs", keyName3, "public", "private")));

vcsSshKeysProvisioner.provision(k8sEnv, runtimeIdentity);

verify(podSpec, times(2)).getVolumes();
verify(podSpec, times(2)).getContainers();
verify(podSpec, times(4)).getVolumes();
verify(podSpec, times(4)).getContainers();

Secret secret = k8sEnv.getSecrets().get(keyName);
Secret secret = k8sEnv.getSecrets().get(keyName1);
assertNotNull(secret);
assertEquals(secret.getType(), "kubernetes.io/ssh-auth");

Expand All @@ -113,8 +118,13 @@ public void addSshKeysConfigInPod() throws Exception {
assertTrue(mapData.containsKey("ssh_config"));

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

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

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

0 comments on commit d8886af

Please sign in to comment.