From 2a30cad76a1745670b43474441ad1b7c050df4c8 Mon Sep 17 00:00:00 2001 From: ansgarz Date: Mon, 21 Feb 2022 22:58:31 +0100 Subject: [PATCH] refactor gopass, change gopass config to latest format, omit configureGopass if config already in place --- .../provs/desktop/infrastructure/Gopass.kt | 65 +++++++++---------- .../extensions/workplace/base/GopassKtTest.kt | 5 +- 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Gopass.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Gopass.kt index c917cb0..9d75ca0 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Gopass.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Gopass.kt @@ -2,10 +2,7 @@ package org.domaindrivenarchitecture.provs.desktop.infrastructure import org.domaindrivenarchitecture.provs.framework.core.Prov import org.domaindrivenarchitecture.provs.framework.core.ProvResult -import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createDir -import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createDirs -import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createFile -import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.userHome +import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.* import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.isPackageInstalled import org.domaindrivenarchitecture.provs.framework.ubuntu.web.base.downloadFromURL @@ -44,16 +41,22 @@ fun Prov.installGopass( } -fun Prov.configureGopass(gopassRootFolder: String? = null) = def { - if ((gopassRootFolder != null) && (gopassRootFolder.trim().length > 0) && ("~" == gopassRootFolder.trim().substring(0, 1))) { - return@def ProvResult(false, err = "Gopass cannot be initialized with folders starting with ~") - } +fun Prov.configureGopass(gopassRootFolder: String? = null) = task { + val configFile = ".config/gopass/config.yml" val defaultRootFolder = userHome() + ".password-store" val rootFolder = gopassRootFolder ?: defaultRootFolder + + if (fileExists(configFile)) { + return@task ProvResult(true, out = "Gopass already configured in file $configFile") + } + + if ((gopassRootFolder != null) && (!gopassRootFolder.startsWith("/"))) { + return@task ProvResult(false, err = "Gopass cannot be initialized with a relative path or path starting with ~") + } // use default createDir(rootFolder) createDirs(".config/gopass") - createFile("~/.config/gopass/config.yml", gopassConfig(rootFolder)) + createFile(configFile, gopassConfig(rootFolder)) // auto-completion configureBashForUser() @@ -61,35 +64,31 @@ fun Prov.configureGopass(gopassRootFolder: String? = null) = def { } -fun Prov.gopassMountStore(storeName: String, path: String, indexOfRecepientKey: Int = 0) = def { - cmd("printf \"$indexOfRecepientKey\\n\" | gopass mounts add $storeName $path") +fun Prov.gopassMountStore(storeName: String, path: String) = def { + cmd("gopass mounts add $storeName $path") +} + + +@Suppress("unused") +fun Prov.gopassInitStore(storeName: String, indexOfRecepientKey: Int = 0) = def { + cmd("printf \"$indexOfRecepientKey\\n\" | gopass init --store=$storeName") } internal fun gopassConfig(gopassRoot: String): String { return """ -root: - askformore: false - autoclip: true - autoprint: false - autoimport: true - autosync: false - check_recipient_hash: false - cliptimeout: 45 - concurrency: 1 - editrecipients: false - exportkeys: true - nocolor: false - noconfirm: true - nopager: false - notifications: true - path: gpgcli-gitcli-fs+file://$gopassRoot - recipient_hash: - .gpg-id: 3078303637343130344341383141343930350aa69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26 - safecontent: false - usesymbols: false -mounts: {} - """.trim() + "\n" + autoclip: true + autoimport: true + cliptimeout: 45 + exportkeys: false + nocolor: false + nopager: false + notifications: true + parsing: true + path: $gopassRoot + safecontent: false + mounts: {} + """.trimIndent() + "\n" } diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/extensions/workplace/base/GopassKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/extensions/workplace/base/GopassKtTest.kt index 9816565..a47a67f 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/extensions/workplace/base/GopassKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/extensions/workplace/base/GopassKtTest.kt @@ -25,7 +25,10 @@ internal class GopassKtTest { @Test fun test_configureGopass_fails_with_path_starting_with_tilde() { // when - val res = defaultTestContainer().configureGopass("~/somedir") + val res = defaultTestContainer().task { + deleteFile(".config/gopass/config.yml") + configureGopass("~/somedir") + } // then assertFalse(res.success)