|
| 1 | +// SPDX-FileCopyrightText: 2017-2024 City of Espoo |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 | + |
| 5 | +package fi.espoo.evaka.sficlient.rest |
| 6 | + |
| 7 | +import fi.espoo.evaka.Sensitive |
| 8 | +import fi.espoo.evaka.SfiEnv |
| 9 | +import mu.KotlinLogging |
| 10 | +import software.amazon.awssdk.services.ssm.SsmClient |
| 11 | +import software.amazon.awssdk.services.ssm.model.GetParameterRequest |
| 12 | +import software.amazon.awssdk.services.ssm.model.LabelParameterVersionRequest |
| 13 | +import software.amazon.awssdk.services.ssm.model.ParameterNotFoundException |
| 14 | +import software.amazon.awssdk.services.ssm.model.ParameterType |
| 15 | +import software.amazon.awssdk.services.ssm.model.ParameterVersionNotFoundException |
| 16 | +import software.amazon.awssdk.services.ssm.model.PutParameterRequest |
| 17 | +import software.amazon.awssdk.services.ssm.model.SsmException |
| 18 | + |
| 19 | +/** Backing store for a single password with support for multiple versions/labels */ |
| 20 | +interface PasswordStore { |
| 21 | + /** Gets the password from the store which has the given label */ |
| 22 | + fun getPassword(label: Label): VersionedPassword? |
| 23 | + |
| 24 | + /** |
| 25 | + * Saves a new password to the store, returning its internal version that can be used for |
| 26 | + * applying labels |
| 27 | + */ |
| 28 | + fun putPassword(password: Sensitive<String>): Version |
| 29 | + |
| 30 | + /** Moves the given label to a saved password that has the given version */ |
| 31 | + fun moveLabel(version: Version, label: Label) |
| 32 | + |
| 33 | + data class VersionedPassword(val password: Sensitive<String>, val version: Version) |
| 34 | + |
| 35 | + /** An internal version number for a specific password value */ |
| 36 | + @JvmInline value class Version(val value: Long) |
| 37 | + |
| 38 | + /** Unique, transferable label for a password */ |
| 39 | + enum class Label { |
| 40 | + PENDING, |
| 41 | + CURRENT, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +class AwsSsmPasswordStore(private val client: SsmClient, env: SfiEnv) : PasswordStore { |
| 46 | + private val ssmName = |
| 47 | + env.restPasswordSsmName ?: error("SFI REST password SSM name is not configured") |
| 48 | + private val logger = KotlinLogging.logger {} |
| 49 | + |
| 50 | + override fun getPassword(label: PasswordStore.Label): PasswordStore.VersionedPassword? { |
| 51 | + logger.info { "Fetching a password with label $label" } |
| 52 | + return try { |
| 53 | + client |
| 54 | + .getParameter( |
| 55 | + GetParameterRequest.builder() |
| 56 | + .name("$ssmName:$label") |
| 57 | + .withDecryption(true) |
| 58 | + .build() |
| 59 | + ) |
| 60 | + .let { response -> |
| 61 | + PasswordStore.VersionedPassword( |
| 62 | + Sensitive(response.parameter().value()), |
| 63 | + PasswordStore.Version(response.parameter().version()), |
| 64 | + ) |
| 65 | + } |
| 66 | + } catch (e: SsmException) { |
| 67 | + if (e is ParameterNotFoundException || e is ParameterVersionNotFoundException) null |
| 68 | + else throw e |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + override fun putPassword(password: Sensitive<String>): PasswordStore.Version = |
| 73 | + client |
| 74 | + .putParameter( |
| 75 | + PutParameterRequest.builder() |
| 76 | + .name(ssmName) |
| 77 | + .type(ParameterType.SECURE_STRING) |
| 78 | + .overwrite(true) |
| 79 | + .value(password.value) |
| 80 | + .build() |
| 81 | + ) |
| 82 | + .let { response -> |
| 83 | + logger.info { "Saved a new password with version ${response.version()}" } |
| 84 | + PasswordStore.Version(response.version()) |
| 85 | + } |
| 86 | + |
| 87 | + override fun moveLabel(version: PasswordStore.Version, label: PasswordStore.Label) { |
| 88 | + logger.info { "Applying label $label to ${version.value}" } |
| 89 | + client.labelParameterVersion( |
| 90 | + LabelParameterVersionRequest.builder() |
| 91 | + .name(ssmName) |
| 92 | + .labels(label.toString()) |
| 93 | + .parameterVersion(version.value) |
| 94 | + .build() |
| 95 | + ) |
| 96 | + } |
| 97 | +} |
0 commit comments