-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store secrets in separate config and retrieve via API
- Loading branch information
1 parent
bea1239
commit 19151c7
Showing
15 changed files
with
166 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Options; | ||
using MusicCatalogue.Entities.Config; | ||
using MusicCatalogue.Logic.Config; | ||
|
||
namespace MusicCatalogue.Api.Controllers | ||
{ | ||
[Authorize] | ||
[ApiController] | ||
[ApiConventionType(typeof(DefaultApiConventions))] | ||
[Route("[controller]")] | ||
public class SecretsController : Controller | ||
{ | ||
private readonly MusicApplicationSettings _settings; | ||
|
||
public SecretsController(IOptions<MusicApplicationSettings> settings) | ||
{ | ||
_settings = settings.Value; | ||
SecretResolver.ResolveAllSecrets(_settings); | ||
} | ||
|
||
/// <summary> | ||
/// Return a secret from the configuration file | ||
/// </summary> | ||
/// <param name="name"></param> | ||
/// <returns></returns> | ||
[HttpGet] | ||
[Route("{name}")] | ||
public ActionResult<string?> GetSecret(string name) | ||
{ | ||
var secret = _settings.Secrets.FirstOrDefault(x => x.Name == name); | ||
|
||
if (secret == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
if (string.IsNullOrEmpty(secret.Value)) | ||
{ | ||
return NoContent(); | ||
} | ||
|
||
return secret.Value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace MusicCatalogue.Entities.Config | ||
{ | ||
[ExcludeFromCodeCoverage] | ||
public class Secret | ||
{ | ||
public string Name { get; set; } = ""; | ||
public string Value { get; set; } = ""; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
namespace MusicCatalogue.Logic.Config | ||
{ | ||
public abstract class ResolverBase | ||
{ | ||
/// <summary> | ||
/// Resolve a value given the value from the configuration file | ||
/// </summary> | ||
/// <param name="configValue"></param> | ||
/// <returns></returns> | ||
public static string ResolveValue(string configValue) | ||
{ | ||
string resolvedValue; | ||
|
||
// If the value from the configuration file is a valid file path, the actual value | ||
// is stored separately in the file indicated. This separation allows secrets not to | ||
// be published as part of the API or UI Docker container images but read from volume | ||
// mounts | ||
if (File.Exists(configValue)) | ||
{ | ||
resolvedValue = File.ReadAllText(configValue); | ||
} | ||
else | ||
{ | ||
// Not a path to a file, so just return the configuration value | ||
resolvedValue = configValue; | ||
} | ||
|
||
return resolvedValue; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using MusicCatalogue.Entities.Config; | ||
|
||
namespace MusicCatalogue.Logic.Config | ||
{ | ||
public class SecretResolver : ResolverBase | ||
{ | ||
/// <summary> | ||
/// Resolve all the API key definitions in the supplied application settings | ||
/// </summary> | ||
/// <param name="settings"></param> | ||
public static void ResolveAllSecrets(MusicApplicationSettings settings) | ||
{ | ||
// Iterate over the secret definitions | ||
foreach (var secret in settings.Secrets) | ||
{ | ||
// Resolve the value for the current secret | ||
secret.Value = ResolveValue(secret.Value); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
my-separate-maps-key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"ApplicationSettings": { | ||
"Secrets": [ | ||
{ | ||
"Name": "Maps API Key", | ||
"Value": "secret.txt" | ||
} | ||
] | ||
} | ||
} |