summaryrefslogtreecommitdiffstats
path: root/server/src/Services/VaultService.cs
blob: 388f8d47b5c7e0a3a9bbb3304c3b044b958c721e (plain) (blame)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
namespace IOL.GreatOffice.Api.Services;

public class VaultService
{
	private readonly HttpClient _client;

	public VaultService(HttpClient client, IConfiguration configuration) {
		var token = configuration.GetValue<string>("VAULT_TOKEN");
		var vaultUrl = configuration.GetValue<string>("VAULT_URL");
		if (token.IsNullOrWhiteSpace()) throw new ApplicationException("VAULT_TOKEN is empty");
		if (vaultUrl.IsNullOrWhiteSpace()) throw new ApplicationException("VAULT_URL is empty");
		client.DefaultRequestHeaders.Add("X-Vault-Token", token);
		client.BaseAddress = new Uri(vaultUrl);
		_client = client;
	}

	public async Task<GetSecretResponse<T>> GetSecretAsync<T>(string path) {
		return await _client.GetFromJsonAsync<GetSecretResponse<T>>("/v1/kv/data/" + path);
	}

	public async Task<RenewTokenResponse> RenewTokenAsync<T>(string token) {
		var response = await _client.PostAsJsonAsync("v1/auth/token/renew",
													 new {
															 Token = token
													 });
		if (response.IsSuccessStatusCode) {
			return await response.Content.ReadFromJsonAsync<RenewTokenResponse>();
		}

		return default;
	}

	public class RenewTokenResponse
	{
		public Guid RequestId { get; set; }
		public string LeaseId { get; set; }
		public bool Renewable { get; set; }
		public long LeaseDuration { get; set; }
		public object Data { get; set; }
		public object WrapInfo { get; set; }
		public List<string> Warnings { get; set; }
		public Auth Auth { get; set; }
	}

	public class Auth
	{
		public string ClientToken { get; set; }
		public string Accessor { get; set; }
		public List<string> Policies { get; set; }
		public List<string> TokenPolicies { get; set; }
		public object Metadata { get; set; }
		public long LeaseDuration { get; set; }
		public bool Renewable { get; set; }
		public string EntityId { get; set; }
		public string TokenType { get; set; }
		public bool Orphan { get; set; }
		public object MfaRequirement { get; set; }
		public long NumUses { get; set; }
	}

	public class GetSecretResponse<T>
	{
		public VaultSecret<T> Data { get; set; }
	}

	public class VaultSecret<T>
	{
		public T Data { get; set; }
		public VaultSecretMetadata Metadata { get; set; }
	}

	public class VaultSecretMetadata
	{
		public DateTimeOffset CreatedTime { get; set; }
		public object CustomMetadata { get; set; }
		public string DeletionTime { get; set; }
		public bool Destroyed { get; set; }
		public long Version { get; set; }
	}
}