fix addKnownHost for host with non-default port, i.e. different from 22, remove date from version.txt
This commit is contained in:
parent
dc4dd03f28
commit
482ec00bdd
4 changed files with 34 additions and 6 deletions
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
|
@ -1 +1 @@
|
|||
0.39.4-SNAPSHOT (2024-12-11T21:08:51)
|
||||
0.39.4-SNAPSHOT
|
|
@ -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")
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue