|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2024 Rafael Fernandez |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package com.eff3ct.teckel.serializer |
| 26 | + |
| 27 | +import io.circe.Json |
| 28 | +import io.circe.yaml.parser |
| 29 | + |
| 30 | +object SecretResolver { |
| 31 | + |
| 32 | + private val secretPattern = """\{\{secrets\.([^}]+)\}\}""".r |
| 33 | + |
| 34 | + case class SecretKey(alias: String, scope: String, key: String) |
| 35 | + |
| 36 | + def resolve(content: String, provider: SecretsProvider = SecretsProvider.default): String = { |
| 37 | + // First, try to parse the YAML to extract the secrets.keys section |
| 38 | + val secretKeys: Map[String, SecretKey] = parser |
| 39 | + .parse(content) |
| 40 | + .toOption |
| 41 | + .flatMap(_.hcursor.downField("secrets").downField("keys").as[Map[String, Json]].toOption) |
| 42 | + .map(_.flatMap { case (alias, json) => |
| 43 | + for { |
| 44 | + scope <- json.hcursor.downField("scope").as[String].toOption |
| 45 | + key <- json.hcursor.downField("key").as[String].toOption |
| 46 | + } yield alias -> SecretKey(alias, scope, key) |
| 47 | + }) |
| 48 | + .getOrElse(Map.empty) |
| 49 | + |
| 50 | + // Replace all {{secrets.alias}} placeholders |
| 51 | + secretPattern.replaceAllIn( |
| 52 | + content, |
| 53 | + { m => |
| 54 | + val alias = m.group(1) |
| 55 | + val value = secretKeys.get(alias) match { |
| 56 | + case Some(sk) => provider.get(sk.scope, sk.key) |
| 57 | + case None => provider.get("", alias) |
| 58 | + } |
| 59 | + value match { |
| 60 | + case Some(v) => java.util.regex.Matcher.quoteReplacement(v) |
| 61 | + case None => |
| 62 | + throw new IllegalArgumentException( |
| 63 | + s"Secret '{{secrets.$alias}}' could not be resolved. Set environment variable SECRETS__${alias.toUpperCase |
| 64 | + .replace("-", "_")} or configure a secrets provider." |
| 65 | + ) |
| 66 | + } |
| 67 | + } |
| 68 | + ) |
| 69 | + } |
| 70 | +} |
0 commit comments