Skip to content

Commit

Permalink
refactor(secrets): Make common createPath() code the default
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed May 23, 2024
1 parent ef416a8 commit 1482f21
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 64 deletions.
25 changes: 0 additions & 25 deletions secrets/file/src/main/kotlin/FileBasedSecretsProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,29 +116,4 @@ class FileBasedSecretsProvider(config: Config) : SecretsProvider {
secrets -= path
writeSecrets(secrets)
}

override fun createPath(
organizationId: Long?,
productId: Long?,
repositoryId: Long?,
secretName: String
): Path {
val secretType = when {
organizationId != null -> "organization"
productId != null -> "product"
repositoryId != null -> "repository"
else -> throw IllegalArgumentException(
"Either one of organizationId, productId or repositoryId should be specified to create a path."
)
}
return Path(
listOfNotNull(
secretType,
organizationId,
productId,
repositoryId,
secretName
).joinToString("_")
)
}
}
15 changes: 13 additions & 2 deletions secrets/spi/src/main/kotlin/SecretsProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,18 @@ interface SecretsProvider {

/**
* Generate a [Path] for the secret belonging to the given [organizationId], [productId] and [repositoryId], as well
* as the [secretName].
* as the [secretName]. The default implementation concatenates these properties with underscores.
*/
fun createPath(organizationId: Long?, productId: Long?, repositoryId: Long?, secretName: String): Path
fun createPath(organizationId: Long?, productId: Long?, repositoryId: Long?, secretName: String): Path {
val secretType = when {
organizationId != null -> "organization"
productId != null -> "product"
repositoryId != null -> "repository"
else -> throw IllegalArgumentException(
"Either one of organizationId, productId or repositoryId should be specified to create a path."
)
}

return Path(listOfNotNull(secretType, organizationId, productId, repositoryId, secretName).joinToString("_"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,31 +103,6 @@ class SecretsProviderFactoryForTesting : SecretsProviderFactory {
override fun removeSecret(path: Path) {
storage -= checkPath(path)
}

override fun createPath(
organizationId: Long?,
productId: Long?,
repositoryId: Long?,
secretName: String
): Path {
val secretType = when {
organizationId != null -> "organization"
productId != null -> "product"
repositoryId != null -> "repository"
else -> throw IllegalArgumentException(
"Either one of organizationId, productId or repositoryId should be specified to create a path."
)
}
return Path(
listOfNotNull(
secretType,
organizationId,
productId,
repositoryId,
secretName
).joinToString("_")
)
}
}.also { latestInstance = it }
}
}
12 changes: 0 additions & 12 deletions secrets/vault/src/main/kotlin/VaultSecretsProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,6 @@ class VaultSecretsProvider(
}
}

override fun createPath(organizationId: Long?, productId: Long?, repositoryId: Long?, secretName: String): Path {
val secretType = when {
organizationId != null -> "organization"
productId != null -> "product"
repositoryId != null -> "repository"
else -> throw IllegalArgumentException(
"Either one of organizationId, productId or repositoryId should be specified to create a path."
)
}
return Path(listOfNotNull(secretType, organizationId, productId, repositoryId, secretName).joinToString("_"))
}

/**
* Create an [HttpClient] with a configuration to communicate with the Vault service. The client is prepared to
* obtain a new client token if necessary.
Expand Down

0 comments on commit 1482f21

Please sign in to comment.