From 482ec00bdd6299613d5eeca8766185f8117fd27c Mon Sep 17 00:00:00 2001 From: ansgarz Date: Wed, 18 Dec 2024 21:37:10 +0100 Subject: [PATCH] fix addKnownHost for host with non-default port, i.e. different from 22, remove date from version.txt --- build.gradle | 2 +- .../provs/framework/ubuntu/keys/base/Ssh.kt | 14 ++++++++---- src/main/resources/version.txt | 2 +- .../framework/ubuntu/keys/base/SshKtTest.kt | 22 +++++++++++++++++++ 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/build.gradle b/build.gradle index 9dc2a3d..5d3707e 100644 --- a/build.gradle +++ b/build.gradle @@ -240,7 +240,7 @@ publishing { tasks.register('createVersion') { dependsOn processResources doLast { - def version = project.version.toString() + " (" + Instant.now().toString().split("\\.")[0] + ")" + def version = project.version.toString() def fileName = "src/main/resources/version.txt" def file = new File(fileName) file.write(version) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/keys/base/Ssh.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/keys/base/Ssh.kt index 044e68d..f4dff7c 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/keys/base/Ssh.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/keys/base/Ssh.kt @@ -3,6 +3,7 @@ package org.domaindrivenarchitecture.provs.framework.ubuntu.keys.base import org.domaindrivenarchitecture.provs.desktop.domain.KnownHost import org.domaindrivenarchitecture.provs.framework.core.Prov import org.domaindrivenarchitecture.provs.framework.core.ProvResult +import org.domaindrivenarchitecture.provs.framework.core.local import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.* import org.domaindrivenarchitecture.provs.framework.ubuntu.keys.SshKeyPair import java.io.File @@ -22,11 +23,11 @@ fun Prov.configureSshKeys(sshKeys: SshKeyPair) = task { * Checks if the specified host (domain name or IP) and (optional) port is contained in the known_hosts file */ fun Prov.isKnownHost(hostOrIp: String, port: Int? = null): Boolean { - val hostWithPotentialPort = port?.let { hostInKnownHostsFileFormat(hostOrIp, port) } ?: hostOrIp + val hostWithPotentialPort = port?.let { formatHostForKnownHostsFile(hostOrIp, port) } ?: hostOrIp return cmdNoEval("ssh-keygen -F $hostWithPotentialPort").out?.isNotEmpty() ?: false } -fun hostInKnownHostsFileFormat(hostOrIp: String, port: Int? = null): String { +fun formatHostForKnownHostsFile(hostOrIp: String, port: Int? = null): String { return port?.let { "[$hostOrIp]:$port" } ?: hostOrIp } @@ -45,11 +46,11 @@ fun Prov.addKnownHost(knownHost: KnownHost, verifyKeys: Boolean = false) = task with(knownHost) { for (key in hostKeys) { if (!verifyKeys) { - addTextToFile("\n$hostName $key\n", File(knownHostsFile)) + addTextToFile("\n${formatHostForKnownHostsFile(hostName, port)} $key\n", File(knownHostsFile)) } else { val validKeys = findSshKeys(hostName, port) if (validKeys?.contains(key) == true) { - val formattedHost = hostInKnownHostsFileFormat(hostName, port) + val formattedHost = formatHostForKnownHostsFile(hostName, port) addTextToFile("\n$formattedHost $key\n", File(knownHostsFile)) } else { addResultToEval( @@ -77,3 +78,8 @@ fun Prov.findSshKeys(host: String, port: Int? = null, keytype: String? = null): val output = cmd("ssh-keyscan $portOption $keytypeOption $host 2>/dev/null").out?.trim() return output?.split("\n")?.filter { x -> "" != x }?.map { x -> x.substringAfter(" ") } } + +fun main() { + val k = local().findSshKeys("repo.prod.meissa.de", 2222) + println(k) +} \ No newline at end of file diff --git a/src/main/resources/version.txt b/src/main/resources/version.txt index 3b180a3..6b989b3 100644 --- a/src/main/resources/version.txt +++ b/src/main/resources/version.txt @@ -1 +1 @@ -0.39.4-SNAPSHOT (2024-12-11T21:08:51) \ No newline at end of file +0.39.4-SNAPSHOT \ No newline at end of file diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/keys/base/SshKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/keys/base/SshKtTest.kt index 8ad7367..12713c9 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/keys/base/SshKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/keys/base/SshKtTest.kt @@ -99,4 +99,26 @@ internal class SshKtTest { assertFalse(res3.success) assertFalse(prov.fileContainsText(KNOWN_HOSTS_FILE, invalidKey)) } + + @ContainerTest + fun addKnownHost_with_port_without_verifications() { + // given + val prov = defaultTestContainer() + prov.task { + aptInstall("ssh") + deleteFile(KNOWN_HOSTS_FILE) + } + + // when + val res1 = prov.addKnownHost(KnownHost("myserver.org", 2222, listOf("mytype mykey")), verifyKeys = false) + // check idem-potence + val res2 = prov.addKnownHost(KnownHost("myserver.org", 2222, listOf("mytype mykey")), verifyKeys = false) + + // then + assertTrue(res1.success) + assertTrue(res2.success) + val expectedContent = "[myserver.org]:2222 mytype mykey" + val actualContent = prov.fileContent(KNOWN_HOSTS_FILE) + assertTrue(actualContent?.contains(expectedContent) == true, "$expectedContent\nis not contained in:\n$actualContent") + } } \ No newline at end of file