|
| 1 | +package wisp.resources |
| 2 | + |
| 3 | +import okio.Buffer |
| 4 | +import okio.BufferedSource |
| 5 | +import okio.source |
| 6 | +import java.io.IOException |
| 7 | + |
| 8 | +/** |
| 9 | + * Read-only resources that are fetched from the 1password-cli (`op`) tool. |
| 10 | + * |
| 11 | + * To use, please: |
| 12 | + * 1. ensure the 1password-cli is installed with `hermit install op` or `brew install 1password-cli`. |
| 13 | + * 2. make sure to enable the cli integration in the 1password app: https://developer.1password.com/docs/cli/app-integration/#step-1-turn-on-the-app-integration. |
| 14 | + * 3. check that the secret being accessed is available. You can get the secret-reference by following: https://developer.1password.com/docs/cli/secret-references/#step-1-get-secret-references |
| 15 | + * |
| 16 | + * This uses the scheme `1password:`. Secret-references from 1password can therefore be used after |
| 17 | + * copy-pasting and replacing "op" like so: "1password://secretRef/goes/here". To use a specific |
| 18 | + * account, add the accountId like so "1password:accountId@//secretRef/goes/here". |
| 19 | + */ |
| 20 | +object OnePasswordResourceLoaderBackend : ResourceLoader.Backend() { |
| 21 | + const val SCHEME = "1password:" |
| 22 | + |
| 23 | + override fun checkPath(path: String) { |
| 24 | + OnePasswordResourcePath.fromPath(path) |
| 25 | + } |
| 26 | + |
| 27 | + override fun list(path: String): List<String> { |
| 28 | + require(path.isNotEmpty()) |
| 29 | + |
| 30 | + return listOf(OnePasswordResourcePath.fromPath(path).toString()) |
| 31 | + } |
| 32 | + |
| 33 | + override fun exists(path: String): Boolean { |
| 34 | + return try { |
| 35 | + fetch(path, "type").size > 0 |
| 36 | + } catch (e: Exception) { |
| 37 | + false |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + override fun open(path: String): BufferedSource? { |
| 42 | + return fetch(path) |
| 43 | + } |
| 44 | + |
| 45 | + private fun fetch(path: String, attribute: String? = null): Buffer { |
| 46 | + val resource = OnePasswordResourcePath.fromPath(path) |
| 47 | + val command = listOf("op", "read") + resource.asCliArgs(attribute) |
| 48 | + try { |
| 49 | + val process = ProcessBuilder().command(command).start() |
| 50 | + |
| 51 | + val exitCode = process.waitFor() |
| 52 | + if (exitCode == 0) { |
| 53 | + return Buffer().apply { writeAll(process.inputStream.source()) } |
| 54 | + } |
| 55 | + |
| 56 | + throw NoSuchElementException( |
| 57 | + "1Password secret $resource could not be found! Please make sure it is available via: `${command.joinToString(" ")}`" |
| 58 | + ) |
| 59 | + } catch (e: IOException) { |
| 60 | + throw UnsupportedOperationException( |
| 61 | + "Error calling the 1password-cli. Please ensure it is installed with `hermit install op` or `brew install 1password-cli`", |
| 62 | + e, |
| 63 | + ) |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Represents a 1password secret-reference, with an optional extra account identifier to differentiate |
| 70 | + * if there are multiple 1password accounts. The secret-reference schema is documented at |
| 71 | + * https://developer.1password.com/docs/cli/secret-reference-syntax/. The only change for this use |
| 72 | + * case is the "op:" prefix is not present as the ResourceLoader implementation strips the schema. |
| 73 | + */ |
| 74 | +class OnePasswordResourcePath private constructor(val account: String?, val secretReference: String) { |
| 75 | + override fun toString(): String { |
| 76 | + return account?.let { "$it@$secretReference" } ?: secretReference |
| 77 | + } |
| 78 | + |
| 79 | + @JvmOverloads |
| 80 | + fun asCliArgs(attribute: String? = null): List<String> { |
| 81 | + // For checking a specific attribute of the secret rather than the value (default) |
| 82 | + val attributeField = attribute?.let { "?attribute=$it" } ?: "" |
| 83 | + // Add `op:` prefix for the 1password-cli |
| 84 | + val secretRef = "op:$secretReference$attributeField" |
| 85 | + // Include account args, if specified |
| 86 | + if (account != null) { |
| 87 | + return listOf("--no-newline", "--account", account, secretRef) |
| 88 | + } |
| 89 | + return listOf("--no-newline", secretRef) |
| 90 | + } |
| 91 | + |
| 92 | + companion object { |
| 93 | + /** Expects a path in the form "accountId@//secretRef/goes/here" or "//secretRef/goes/here" */ |
| 94 | + fun fromPath(path: String): OnePasswordResourcePath { |
| 95 | + if (path.contains("@")) { |
| 96 | + val (account, secretReference) = path.split("@", limit = 2) |
| 97 | + require(secretReference.startsWith("//")) { "1Password secret reference must start with //" } |
| 98 | + return OnePasswordResourcePath(account, secretReference) |
| 99 | + } |
| 100 | + |
| 101 | + require(path.startsWith("//")) { "1Password secret reference must start with //" } |
| 102 | + return OnePasswordResourcePath(null, path) |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments