Skip to content

Commit 19bf3c1

Browse files
committed
refactor(secrets): Rename Secret to SecretValue
Rename the value class `Secret` that stores the value of a secret to `SecretValue` to avoid ambiguity with the `Secret` class that describes the properties of a secret. Signed-off-by: Martin Nonnenmacher <[email protected]>
1 parent c2ba258 commit 19bf3c1

File tree

16 files changed

+81
-79
lines changed

16 files changed

+81
-79
lines changed

components/secrets/backend/src/main/kotlin/SecretService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import org.eclipse.apoapsis.ortserver.model.util.ListQueryParameters
3131
import org.eclipse.apoapsis.ortserver.model.util.ListQueryResult
3232
import org.eclipse.apoapsis.ortserver.model.util.OptionalValue
3333
import org.eclipse.apoapsis.ortserver.secrets.Path
34-
import org.eclipse.apoapsis.ortserver.secrets.Secret as SecretValue
3534
import org.eclipse.apoapsis.ortserver.secrets.SecretStorage
35+
import org.eclipse.apoapsis.ortserver.secrets.SecretValue
3636

3737
import org.jetbrains.exposed.sql.Database
3838

secrets/azure-keyvault/src/main/kotlin/AzureKeyvaultProvider.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import org.eclipse.apoapsis.ortserver.model.OrganizationId
2626
import org.eclipse.apoapsis.ortserver.model.ProductId
2727
import org.eclipse.apoapsis.ortserver.model.RepositoryId
2828
import org.eclipse.apoapsis.ortserver.secrets.Path
29-
import org.eclipse.apoapsis.ortserver.secrets.Secret
29+
import org.eclipse.apoapsis.ortserver.secrets.SecretValue
3030
import org.eclipse.apoapsis.ortserver.secrets.SecretsProvider
3131

3232
// Regex for allowed object names in Azure Key Vault, see:
@@ -35,12 +35,12 @@ import org.eclipse.apoapsis.ortserver.secrets.SecretsProvider
3535
private val PATH_REGEX = Regex("^[0-9a-zA-Z\\-]{1,100}\$")
3636

3737
class AzureKeyvaultProvider(private val secretClient: SecretClient) : SecretsProvider {
38-
override fun readSecret(path: Path): Secret? =
38+
override fun readSecret(path: Path): SecretValue? =
3939
runCatching {
40-
secretClient.getSecret(path.path).value?.let { Secret(it) }
40+
secretClient.getSecret(path.path).value?.let { SecretValue(it) }
4141
}.getOrNull()
4242

43-
override fun writeSecret(path: Path, secret: Secret) {
43+
override fun writeSecret(path: Path, secret: SecretValue) {
4444
secretClient.setSecret(path.path, secret.value)
4545
}
4646

secrets/file/src/main/kotlin/FileBasedSecretsProvider.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import kotlin.io.encoding.ExperimentalEncodingApi
2929
import kotlinx.serialization.json.Json
3030

3131
import org.eclipse.apoapsis.ortserver.secrets.Path
32-
import org.eclipse.apoapsis.ortserver.secrets.Secret
32+
import org.eclipse.apoapsis.ortserver.secrets.SecretValue
3333
import org.eclipse.apoapsis.ortserver.secrets.SecretsProvider
3434
import org.eclipse.apoapsis.ortserver.secrets.file.model.FileBasedSecretsStorage
3535
import org.eclipse.apoapsis.ortserver.utils.config.getStringOrDefault
@@ -60,7 +60,7 @@ class FileBasedSecretsProvider(config: Config) : SecretsProvider {
6060
* Return a map representing all secrets stored in file-based secret storage.
6161
*/
6262
@OptIn(ExperimentalEncodingApi::class)
63-
private fun readSecrets(): MutableMap<Path, Secret> {
63+
private fun readSecrets(): MutableMap<Path, SecretValue> {
6464
val file = getOrCreateStorageFile()
6565

6666
val decodedSecrets = Base64.decode(file.readBytes())
@@ -69,7 +69,7 @@ class FileBasedSecretsProvider(config: Config) : SecretsProvider {
6969
return Json.decodeFromString(
7070
serializer,
7171
String(decodedSecrets)
72-
).secrets.map { (key, value) -> Path(key) to Secret(value) }.toMap().toMutableMap()
72+
).secrets.map { (key, value) -> Path(key) to SecretValue(value) }.toMap().toMutableMap()
7373
}
7474

7575
private fun getOrCreateStorageFile(): File {
@@ -90,7 +90,7 @@ class FileBasedSecretsProvider(config: Config) : SecretsProvider {
9090
* Return a map representing all secrets stored in file-based secret storage.
9191
*/
9292
@OptIn(ExperimentalEncodingApi::class)
93-
private fun writeSecrets(secrets: MutableMap<Path, Secret>) {
93+
private fun writeSecrets(secrets: MutableMap<Path, SecretValue>) {
9494
val serializer = FileBasedSecretsStorage.serializer()
9595
val secretsJson = Json.encodeToString(
9696
serializer,
@@ -103,12 +103,12 @@ class FileBasedSecretsProvider(config: Config) : SecretsProvider {
103103
}
104104

105105
@Synchronized
106-
override fun readSecret(path: Path): Secret? {
106+
override fun readSecret(path: Path): SecretValue? {
107107
return readSecrets()[path]
108108
}
109109

110110
@Synchronized
111-
override fun writeSecret(path: Path, secret: Secret) {
111+
override fun writeSecret(path: Path, secret: SecretValue) {
112112
val secrets = readSecrets()
113113
secrets[path] = secret
114114
writeSecrets(secrets)

secrets/file/src/test/kotlin/FileBasedSecretStorageTest.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class FileBasedSecretStorageTest : WordSpec() {
5555
"return the value of an existing secret" {
5656
val password = storage.readSecret(Path("password"))
5757

58-
password shouldBe Secret("securePassword123")
58+
password shouldBe SecretValue("securePassword123")
5959
}
6060

6161
"return null for a non-existing secret" {
@@ -68,7 +68,7 @@ class FileBasedSecretStorageTest : WordSpec() {
6868
"writeSecret" should {
6969
"create a new secret" {
7070
val newSecretPath = Path("brandNewSecret")
71-
val newSecretValue = Secret("You will never know...")
71+
val newSecretValue = SecretValue("You will never know...")
7272

7373
storage.writeSecret(newSecretPath, newSecretValue)
7474

@@ -77,8 +77,8 @@ class FileBasedSecretStorageTest : WordSpec() {
7777

7878
"update an existing secret" {
7979
val newSecretPath = Path("secretWithUpdates")
80-
val firstValue = Secret("You will never know...")
81-
val secondValue = Secret("Maybe time after time?")
80+
val firstValue = SecretValue("You will never know...")
81+
val secondValue = SecretValue("Maybe time after time?")
8282

8383
storage.writeSecret(newSecretPath, firstValue)
8484

@@ -92,7 +92,7 @@ class FileBasedSecretStorageTest : WordSpec() {
9292
"remove an existing secret" {
9393
val targetPath = Path("justWaste")
9494

95-
storage.writeSecret(targetPath, Secret("toBeDeleted"))
95+
storage.writeSecret(targetPath, SecretValue("toBeDeleted"))
9696

9797
storage.removeSecret(targetPath)
9898

@@ -102,9 +102,9 @@ class FileBasedSecretStorageTest : WordSpec() {
102102
"remove a secret with all its versions" {
103103
val targetPath = Path("evenMoreWaste")
104104

105-
storage.writeSecret(targetPath, Secret("toBeOverwritten"))
106-
storage.writeSecret(targetPath, Secret("toBeOverwrittenAgain"))
107-
storage.writeSecret(targetPath, Secret("toBeDeleted"))
105+
storage.writeSecret(targetPath, SecretValue("toBeOverwritten"))
106+
storage.writeSecret(targetPath, SecretValue("toBeOverwrittenAgain"))
107+
storage.writeSecret(targetPath, SecretValue("toBeDeleted"))
108108

109109
storage.removeSecret(targetPath)
110110

secrets/scaleway/src/main/kotlin/ScalewaySecretsProvider.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import org.eclipse.apoapsis.ortserver.model.OrganizationId
4848
import org.eclipse.apoapsis.ortserver.model.ProductId
4949
import org.eclipse.apoapsis.ortserver.model.RepositoryId
5050
import org.eclipse.apoapsis.ortserver.secrets.Path
51-
import org.eclipse.apoapsis.ortserver.secrets.Secret
51+
import org.eclipse.apoapsis.ortserver.secrets.SecretValue
5252
import org.eclipse.apoapsis.ortserver.secrets.SecretsProvider
5353
import org.eclipse.apoapsis.ortserver.utils.logging.runBlocking
5454

@@ -91,7 +91,7 @@ class ScalewaySecretsProvider(
9191
}
9292

9393
@OptIn(ExperimentalEncodingApi::class)
94-
override fun readSecret(path: Path): Secret? = runBlocking {
94+
override fun readSecret(path: Path): SecretValue? = runBlocking {
9595
// See https://www.scaleway.com/en/developers/api/secret-manager/#path-secret-versions-access-a-secrets-version-using-the-secrets-name-and-path.
9696
val response = client.get("secrets-by-path/versions/$LATEST_REVISION/access") {
9797
parameter("project_id", config.projectId)
@@ -117,14 +117,14 @@ class ScalewaySecretsProvider(
117117

118118
logger.debug("Read a secret at $path.")
119119

120-
Secret(String(Base64.decode(secretResponse.data)))
120+
SecretValue(String(Base64.decode(secretResponse.data)))
121121
}
122122

123123
else -> throw ClientRequestException(response, response.body())
124124
}
125125
}
126126

127-
override fun writeSecret(path: Path, secret: Secret) = runBlocking {
127+
override fun writeSecret(path: Path, secret: SecretValue) = runBlocking {
128128
val listResponse = listSecrets(path)
129129

130130
val secretId = if (listResponse.totalCount < 1) {

secrets/scaleway/src/test/kotlin/ScalewaySecretsProviderTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import io.kotest.matchers.should
2525
import io.kotest.matchers.shouldBe
2626

2727
import org.eclipse.apoapsis.ortserver.model.OrganizationId
28-
import org.eclipse.apoapsis.ortserver.secrets.Secret
28+
import org.eclipse.apoapsis.ortserver.secrets.SecretValue
2929

3030
class ScalewaySecretsProviderTest : WordSpec({
3131
// Some test cases in this test spec actually connect to the real production Scaleway API. These tests are only
@@ -37,7 +37,7 @@ class ScalewaySecretsProviderTest : WordSpec({
3737

3838
val provider = ScalewaySecretsProvider(config)
3939
val path = provider.createPath(OrganizationId(1), "This_is_a_29-chr._secret_name")
40-
val secret = Secret("Ernie & Bert live at Sesame Street!")
40+
val secret = SecretValue("Ernie & Bert live at Sesame Street!")
4141

4242
"createPath()" should {
4343
"create an absolute path from the path prefix and path name" {

secrets/spi/src/main/kotlin/SecretStorage.kt

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ import org.eclipse.apoapsis.ortserver.model.HierarchyId
2929
* A class providing convenient access to secrets based on a [SecretsProvider].
3030
*
3131
* This class takes care of the instantiation of a [SecretsProvider] based on the application configuration via the
32-
* [createStorage] function. This provider is then wrapped, and a richer API to deal with [Secret]s is implemented on
33-
* top of it.
32+
* [createStorage] function. This provider is then wrapped, and a richer API to deal with [SecretValue]s is implemented
33+
* on top of it.
3434
*
35-
* The extended functionality compared to [SecretsProvider] is mainly related to the handling of missing [Secret]s
36-
* and exception handling. There are functions that require a [Secret] to exist or throw an exception otherwise.
35+
* The extended functionality compared to [SecretsProvider] is mainly related to the handling of missing [SecretValue]s
36+
* and exception handling. There are functions that require a [SecretValue] to exist or throw an exception otherwise.
3737
* With regard to exception handling, in general all exceptions thrown by the underlying [SecretsProvider] are caught
3838
* and wrapped in a [SecretStorageException]; so, it should be sufficient to catch this exception type. Alternatively,
3939
* consumers can choose to use functions that return [Result] objects.
@@ -80,35 +80,36 @@ class SecretStorage(
8080
}
8181

8282
/**
83-
* Return the [Secret] at the given [path] or `null` if the path cannot be resolved.
83+
* Return the [SecretValue] at the given [path] or `null` if the path cannot be resolved.
8484
*/
85-
fun readSecret(path: Path): Secret? = wrapExceptions { provider.readSecret(path) }
85+
fun readSecret(path: Path): SecretValue? = wrapExceptions { provider.readSecret(path) }
8686

8787
/**
88-
* Return the [Secret] at the given [path] or fail with a [SecretStorageException] if the path cannot be resolved.
88+
* Return the [SecretValue] at the given [path] or fail with a [SecretStorageException] if the path cannot be
89+
* resolved.
8990
*/
90-
fun getSecret(path: Path): Secret =
91+
fun getSecret(path: Path): SecretValue =
9192
readSecret(path) ?: throw SecretStorageException("No secret found at path '$path'.")
9293

9394
/**
94-
* Return a [Result] with a nullable [Secret] found at the given [path]. This function works like [readSecret],
95+
* Return a [Result] with a nullable [SecretValue] found at the given [path]. This function works like [readSecret],
9596
* but wraps an occurring exception inside a [Result]. Exceptions from the underlying [SecretsProvider] are
9697
* wrapped in a [SecretStorageException].
9798
*/
98-
fun readSecretCatching(path: Path): Result<Secret?> = runCatching { readSecret(path) }
99+
fun readSecretCatching(path: Path): Result<SecretValue?> = runCatching { readSecret(path) }
99100

100101
/**
101-
* Return a [Result] with the [Secret] found at the given [path]. This function works like [getSecret], but
102+
* Return a [Result] with the [SecretValue] found at the given [path]. This function works like [getSecret], but
102103
* wraps an occurring exception inside a [Result]. Exceptions from the underlying [SecretsProvider] are wrapped
103104
* in a [SecretStorageException]. If the given [path] cannot be resolved, a failed [Result] is returned as well.
104105
*/
105-
fun getSecretCatching(path: Path): Result<Secret> = runCatching { getSecret(path) }
106+
fun getSecretCatching(path: Path): Result<SecretValue> = runCatching { getSecret(path) }
106107

107108
/**
108109
* Store the given [secret] under the given [path] in the underlying [SecretsProvider]. Throw a
109110
* [SecretStorageException] if this fails.
110111
*/
111-
fun writeSecret(path: Path, secret: Secret) {
112+
fun writeSecret(path: Path, secret: SecretValue) {
112113
wrapExceptions { provider.writeSecret(path, secret) }
113114
}
114115

@@ -117,18 +118,19 @@ class SecretStorage(
117118
* the outcome of the operation. Exceptions thrown by the [SecretsProvider] are wrapped in a
118119
* [SecretStorageException] and returned in the [Result].
119120
*/
120-
fun writeSecretCatching(path: Path, secret: Secret): Result<Unit> = runCatching { writeSecret(path, secret) }
121+
fun writeSecretCatching(path: Path, secret: SecretValue): Result<Unit> = runCatching { writeSecret(path, secret) }
121122

122123
/**
123-
* Remove the [Secret] under the given [path]. Throw a [SecretStorageException] if this fails.
124+
* Remove the [SecretValue] under the given [path]. Throw a [SecretStorageException] if this fails.
124125
*/
125126
fun removeSecret(path: Path) {
126127
wrapExceptions { provider.removeSecret(path) }
127128
}
128129

129130
/**
130-
* Remove the [Secret] under the given [path] and return a [Result] for the outcome of the operation. Exceptions
131-
* thrown by the [SecretsProvider] are wrapped in a [SecretStorageException] and returned in the [Result].
131+
* Remove the [SecretValue] under the given [path] and return a [Result] for the outcome of the operation.
132+
* Exceptions thrown by the [SecretsProvider] are wrapped in a [SecretStorageException] and returned in the
133+
* [Result].
132134
*/
133135
fun removeSecretCatching(path: Path): Result<Unit> = runCatching { removeSecret(path) }
134136

secrets/spi/src/main/kotlin/Secret.kt renamed to secrets/spi/src/main/kotlin/SecretValue.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
package org.eclipse.apoapsis.ortserver.secrets
2121

2222
/**
23-
* A class representing a secret.
23+
* A class representing the value of a secret.
2424
*
2525
* This is a typically text-based secret that was obtained from or can be written into a secret storage.
2626
*/
2727
@JvmInline
28-
value class Secret(val value: String)
28+
value class SecretValue(val value: String)

secrets/spi/src/main/kotlin/SecretsProvider.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,20 @@ import org.eclipse.apoapsis.ortserver.model.RepositoryId
3636
*/
3737
interface SecretsProvider {
3838
/**
39-
* Return the [Secret] associated with the given [Path] or `null` if no secret is associated with this path.
39+
* Return the [SecretValue] associated with the given [Path] or `null` if no secret is associated with this path.
4040
* A concrete implementation may throw a proprietary exception if there was a problem when accessing the
4141
* underlying secret storage.
4242
*/
43-
fun readSecret(path: Path): Secret?
43+
fun readSecret(path: Path): SecretValue?
4444

4545
/**
4646
* Write the given [secret] under the given [path] into the underlying secret storage. An implementation may throw
4747
* a proprietary exception if it encounters a problem.
4848
*/
49-
fun writeSecret(path: Path, secret: Secret)
49+
fun writeSecret(path: Path, secret: SecretValue)
5050

5151
/**
52-
* Remove the [Secret] associated with the given [path] from the underlying secret storage. An implementation
52+
* Remove the [SecretValue] associated with the given [path] from the underlying secret storage. An implementation
5353
* should throw an exception if the operation failed.
5454
*/
5555
fun removeSecret(path: Path)

secrets/spi/src/test/kotlin/SecretStorageTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class SecretStorageTest : WordSpec({
146146
"writeSecret" should {
147147
"write a secret successfully" {
148148
val newPath = Path("new-secret")
149-
val newSecret = Secret("BrandNewSecret")
149+
val newSecret = SecretValue("BrandNewSecret")
150150
val storage = createStorage()
151151

152152
storage.writeSecret(newPath, newSecret)
@@ -156,7 +156,7 @@ class SecretStorageTest : WordSpec({
156156

157157
"throw an exception if writing fails" {
158158
val exception = shouldThrow<SecretStorageException> {
159-
createStorage().writeSecret(ERROR_PATH, Secret("will-fail"))
159+
createStorage().writeSecret(ERROR_PATH, SecretValue("will-fail"))
160160
}
161161

162162
exception.cause should beInstanceOf<IllegalArgumentException>()
@@ -166,7 +166,7 @@ class SecretStorageTest : WordSpec({
166166
"writeSecretCatching" should {
167167
"return a success result if the operation is successful" {
168168
val newPath = Path("new-secret")
169-
val newSecret = Secret("BrandNewSecret")
169+
val newSecret = SecretValue("BrandNewSecret")
170170
val storage = createStorage()
171171

172172
val result = storage.writeSecretCatching(newPath, newSecret)
@@ -176,7 +176,7 @@ class SecretStorageTest : WordSpec({
176176
}
177177

178178
"return a failure result for a failing operation" {
179-
val result = createStorage().writeSecretCatching(ERROR_PATH, Secret("?"))
179+
val result = createStorage().writeSecretCatching(ERROR_PATH, SecretValue("?"))
180180

181181
result shouldBeFailure { exception ->
182182
exception should beInstanceOf<SecretStorageException>()

0 commit comments

Comments
 (0)