From 203a94fab276c69e09bcf4cbcd879d4745300ccb Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Fri, 4 Aug 2023 16:55:37 +0200 Subject: [PATCH] poc implementation of knownHosts refactoring --- doc/dev/ADRServicesImplementationStatic.md | 38 +++++++++++++++ doc/dev/architecture.md | 11 +++-- .../provs/desktop/domain/DesktopService.kt | 1 + .../provs/desktop/domain/KnownHost.kt | 48 +++++++++++++++++++ .../provs/desktop/domain/KnownHostConfig.kt | 31 ------------ .../provs/desktop/infrastructure/DevOps.kt | 2 + 6 files changed, 97 insertions(+), 34 deletions(-) create mode 100644 doc/dev/ADRServicesImplementationStatic.md create mode 100644 src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/KnownHost.kt delete mode 100644 src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/KnownHostConfig.kt diff --git a/doc/dev/ADRServicesImplementationStatic.md b/doc/dev/ADRServicesImplementationStatic.md new file mode 100644 index 0000000..4b22424 --- /dev/null +++ b/doc/dev/ADRServicesImplementationStatic.md @@ -0,0 +1,38 @@ +# ADR: We implement domain services static + +Domain services can be implemented either as object (and composed like done in spring / example1 ) or with extension +function and composed static (see example2). + +## example1 +```kotlin +class DesktopServie(val aptApi: AptApi, val prov: Prov) { + fun provisionIdeDesktop(onlyModules: List? = null) { + prov.task { + if (onlyModules == null) { + aptApi.aptInstall(OPEN_VPM) + } + } + } +} +``` + +## example2 +```kotlin +fun Prov.provisionIdeDesktop(onlyModules: List? = null) { + if (onlyModules == null) { + aptInstall(OPEN_VPM) + } +} +``` + +## Decission + +We use extension function and composed static. + +## Reason + +1. Similar to composed objects we can easily mock `aptInstall` in tests. Both solutions are equivalent. +2. Inheritance in case of composed objects we can solve by static composition. +3. Object composition we can solve by static composition. + +There is no reason left to change the current implementd pattern. diff --git a/doc/dev/architecture.md b/doc/dev/architecture.md index 1570abf..8f091a2 100644 --- a/doc/dev/architecture.md +++ b/doc/dev/architecture.md @@ -6,7 +6,7 @@ sequenceDiagram actor user participant app as Application participant ds as DesktopService - participant gtr as GitTrustFactory + participant gtr as KnownHost participant pa as CliArgumentsParser participant cr as DesktopConfigRepository participant ut as CliUtils @@ -20,8 +20,8 @@ sequenceDiagram app ->> su: ensureSudoWithoutPassword(cmd.target.remoteTarget()?.password) app ->> ds: provisionDesktopCommand(cmd, config) activate ds - ds ->> gtr: get("github", "gitlab") - gtr -->> ds: GitTrust + ds ->> gtr: values() + gtr -->> ds: List(KnownHost) deactivate ds deactivate app ``` @@ -62,6 +62,11 @@ classDiagram <> FIREFOX, VERIFY } + + class KnownHost { + hostName: String, + hostKeys: List + } } DesktopCliCommand "1" *-- "1" DesktopType: type diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopService.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopService.kt index 778e341..0e351bb 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopService.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopService.kt @@ -12,6 +12,7 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.keys.provisionKeys import org.domaindrivenarchitecture.provs.framework.ubuntu.user.base.currentUserCanSudoWithoutPassword import org.domaindrivenarchitecture.provs.framework.ubuntu.user.base.whoami + internal fun Prov.provisionDesktopCommand(cmd: DesktopCliCommand, conf: DesktopConfig) = task { provisionDesktop( cmd.type, diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/KnownHost.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/KnownHost.kt new file mode 100644 index 0000000..367ad21 --- /dev/null +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/KnownHost.kt @@ -0,0 +1,48 @@ +package org.domaindrivenarchitecture.provs.desktop.domain + +/** + * A HostKey should contain space-separated: keytype, key and (optionally) a comment + * + * See: https://man7.org/linux/man-pages/man8/sshd.8.html#SSH_KNOWN_HOSTS_FILE_FORMAT + */ +typealias HostKey = String + +open class KnownHost protected constructor(val hostName: String, val hostKeys: List) { + companion object { + val GITHUB = KnownHost( + "github.com", listOf( + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl", + "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEmKSENjQEezOmxkZMy7opKgwFB9nkt5YRrYMjNuG5N87uRgg6CLrbo5wAdT/y6v0mKV0U2w0WZ2YB/++Tpockg=", + "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCj7ndNxQowgcQnjshcLrqPEiiphnt+VTTvDP6mHBL9j1aNUkY4Ue1gvwnGLVlOhGeYrnZaMgRK6+PKCUXaDbC7qtbW8gIkhL7aGCsOr/C56SJMy/BCZfxd1nWzAOxSDPgVsmerOBYfNqltV9/hWCqBywINIR+5dIg6JTJ72pcEpEjcYgXkE2YEFXV1JHnsKgbLWNlhScqb2UmyRkQyytRLtL+38TGxkxCflmO+5Z8CSSNY7GidjMIZ7Q4zMjA2n1nGrlTDkzwDCsw+wqFPGQA179cnfGWOWRVruj16z6XyvxvjJwbz0wQZ75XK5tKSb7FNyeIEs4TT4jk+S4dhPeAUC5y+bDYirYgM4GC7uEnztnZyaVWQ7B381AK4Qdrwt51ZqExKbQpTUNn+EjqoTwvqNj4kqx5QUCI0ThS/YkOxJCXmPUWZbhjpCg56i+2aB6CmK2JGhn57K5mj0MNdBXA4/WnwH6XoPWJzK5Nyu2zB3nAZp+S5hpQs+p1vN1/wsjk=", + ) + ) + val GITLAB = KnownHost( + "gitlab.com", listOf( + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAfuCHKVTjquxvt6CM6tdG4SLp1Btn/nOeHHE5UOzRdf", + "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCsj2bNKTBSpIYDEGk9KxsGh3mySTRgMtXL583qmBpzeQ+jqCMRgBqB98u3z++J1sKlXHWfM9dyhSevkMwSbhoR8XIq/U0tCNyokEi/ueaBMCvbcTHhO7FcwzY92WK4Yt0aGROY5qX2UKSeOvuP4D6TPqKF1onrSzH9bx9XUf2lEdWT/ia1NEKjunUqu1xOB/StKDHMoX4/OKyIzuS0q/T1zOATthvasJFoPrAjkohTyaDUz2LN5JoH839hViyEG82yB+MjcFV5MU3N1l1QL3cVUCh93xSaua1N85qivl+siMkPGbO5xR/En4iEY6K2XPASUEMaieWVNTRCtJ4S8H+9", + "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBFSMqzJeV9rUzU4kWitGjeR4PWSa29SPqJ1fVkhtj3Hw9xjLVXVYrU9QlYWrOLXBpQ6KWjbjTDTdDkoohFzgbEY=", + ) + ) + + @JvmStatic + protected val values = listOf(KnownHost.GITHUB, KnownHost.GITLAB) + + fun values(): List { + return values + } + + @JvmStatic + fun valueOf(value: String): KnownHost = valueOf(value, KnownHost.values) + + @JvmStatic + protected fun valueOf(value: String, valueList: List): KnownHost { + for (type in valueList) { + if (value.uppercase().equals(type.hostName)) { + return type + } + } + throw RuntimeException("No KnownHost found for value: $value") + } + } +} + diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/KnownHostConfig.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/KnownHostConfig.kt deleted file mode 100644 index 07f8768..0000000 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/KnownHostConfig.kt +++ /dev/null @@ -1,31 +0,0 @@ -package org.domaindrivenarchitecture.provs.desktop.domain - -/** - * A HostKey should contain space-separated: keytype, key and (optionally) a comment - * - * See: https://man7.org/linux/man-pages/man8/sshd.8.html#SSH_KNOWN_HOSTS_FILE_FORMAT - */ -typealias HostKey = String - -data class KnownHost(val hostName: String, val hostKeys: List) - - -val defaultKnownHosts = listOf( - // github - https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/githubs-ssh-key-fingerprints - KnownHost( - "github.com", listOf( - "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl", - "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEmKSENjQEezOmxkZMy7opKgwFB9nkt5YRrYMjNuG5N87uRgg6CLrbo5wAdT/y6v0mKV0U2w0WZ2YB/++Tpockg=", - "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCj7ndNxQowgcQnjshcLrqPEiiphnt+VTTvDP6mHBL9j1aNUkY4Ue1gvwnGLVlOhGeYrnZaMgRK6+PKCUXaDbC7qtbW8gIkhL7aGCsOr/C56SJMy/BCZfxd1nWzAOxSDPgVsmerOBYfNqltV9/hWCqBywINIR+5dIg6JTJ72pcEpEjcYgXkE2YEFXV1JHnsKgbLWNlhScqb2UmyRkQyytRLtL+38TGxkxCflmO+5Z8CSSNY7GidjMIZ7Q4zMjA2n1nGrlTDkzwDCsw+wqFPGQA179cnfGWOWRVruj16z6XyvxvjJwbz0wQZ75XK5tKSb7FNyeIEs4TT4jk+S4dhPeAUC5y+bDYirYgM4GC7uEnztnZyaVWQ7B381AK4Qdrwt51ZqExKbQpTUNn+EjqoTwvqNj4kqx5QUCI0ThS/YkOxJCXmPUWZbhjpCg56i+2aB6CmK2JGhn57K5mj0MNdBXA4/WnwH6XoPWJzK5Nyu2zB3nAZp+S5hpQs+p1vN1/wsjk=", - ) - ), - // gitlab.com - https://docs.gitlab.com/ee/user/gitlab_com/ - KnownHost( - "gitlab.com", listOf( - "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAfuCHKVTjquxvt6CM6tdG4SLp1Btn/nOeHHE5UOzRdf", - "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCsj2bNKTBSpIYDEGk9KxsGh3mySTRgMtXL583qmBpzeQ+jqCMRgBqB98u3z++J1sKlXHWfM9dyhSevkMwSbhoR8XIq/U0tCNyokEi/ueaBMCvbcTHhO7FcwzY92WK4Yt0aGROY5qX2UKSeOvuP4D6TPqKF1onrSzH9bx9XUf2lEdWT/ia1NEKjunUqu1xOB/StKDHMoX4/OKyIzuS0q/T1zOATthvasJFoPrAjkohTyaDUz2LN5JoH839hViyEG82yB+MjcFV5MU3N1l1QL3cVUCh93xSaua1N85qivl+siMkPGbO5xR/En4iEY6K2XPASUEMaieWVNTRCtJ4S8H+9", - "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBFSMqzJeV9rUzU4kWitGjeR4PWSa29SPqJ1fVkhtj3Hw9xjLVXVYrU9QlYWrOLXBpQ6KWjbjTDTdDkoohFzgbEY=", - ) - ) -) - diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DevOps.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DevOps.kt index a75a6d9..178f201 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DevOps.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DevOps.kt @@ -15,7 +15,9 @@ fun Prov.installDevOps() = task { installTerraform() installKubectlAndTools() installYq() + // TODO: the can be removed installAwsCredentials() + // TODO: the can be removed installDevOpsFolder() }