-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathVaultResource.cs
56 lines (50 loc) · 2.69 KB
/
VaultResource.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
namespace ShadowShop.AppHost.Resources;
public class VaultServerResource(string name) : ContainerResource(name), IResourceWithServiceDiscovery
{
internal const string PrimaryEndpointName = "http";
internal const int DefaultContainerPort = 8200;
internal const string DefaultTokenId = "dev-root";
private EndpointReference? _primaryEndpoint;
public EndpointReference PrimaryEndpoint => _primaryEndpoint ??= new(this, PrimaryEndpointName);
}
public static class VaultServerBuilderExtensions
{
public static IResourceBuilder<VaultServerResource> AddVaultDevServer(this IDistributedApplicationBuilder builder,
string name, string? rootTokenId = VaultServerResource.DefaultTokenId)
{
var address = $"0.0.0.0:{VaultServerResource.DefaultContainerPort}";
var apiAddress = $"http://{address}";
var args = new List<string> { "server", "-dev", "-dev-no-store-token" };
return builder.AddResource(new VaultServerResource(name))
.WithImage("hashicorp/vault")
.WithImageRegistry("docker.io")
.WithHttpEndpoint(
port: VaultServerResource.DefaultContainerPort,
targetPort: VaultServerResource.DefaultContainerPort,
name: VaultServerResource.PrimaryEndpointName
)
.WithArgs(args.ToArray())
.WithEnvironment("VAULT_LOG_LEVEL", "info")
.WithEnvironment("VAULT_DEV_ROOT_TOKEN_ID", rootTokenId ?? VaultServerResource.DefaultTokenId)
.WithEnvironment("VAULT_API_ADDR", apiAddress)
.WithEnvironment("VAULT_ADDR", apiAddress)
.WithEnvironment("VAULT_DEV_LISTEN_ADDRESS", address)
.WithHttpHealthCheck("/v1/sys/health")
.ExcludeFromManifest();
}
public static IResourceBuilder<TDestination> WithReference<TDestination>(
this IResourceBuilder<TDestination> builder, IResourceBuilder<VaultServerResource> source,
string? rootTokenId = null)
where TDestination : IResourceWithEnvironment, IResourceWithWaitSupport
{
builder.WithReference(source as IResourceBuilder<IResourceWithServiceDiscovery>);
return builder.WaitFor(source).WithEnvironment(ctx =>
{
ctx.EnvironmentVariables["VAULT_ADDR"] = source.Resource.GetEndpoint(VaultServerResource.PrimaryEndpointName);
ctx.EnvironmentVariables["VAULT_PORT"] =
source.Resource.GetEndpoint(VaultServerResource.PrimaryEndpointName).Property(EndpointProperty.Port);
ctx.EnvironmentVariables["VAULT_APP_MOUNT"] ="shadowshop";
ctx.EnvironmentVariables["VAULT_TOKEN"] = rootTokenId ?? VaultServerResource.DefaultTokenId;
});
}
}