Skip to content

Commit b35bd33

Browse files
committed
Improve logging of provider
1 parent be26356 commit b35bd33

File tree

1 file changed

+22
-31
lines changed

1 file changed

+22
-31
lines changed

src/Backend/src/Vault.Configuration/VaulConfigurationProvider.cs

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ public override void Load()
3636
// with az login.
3737
data ??= LoadConfigurationFromAuthoring(applicationName, partName, environment);
3838

39-
var filepath = GetTokenFromFile(_source.Part.Name, _source.Part.PartName, environment);
39+
var filepath =
40+
CreateLocalConfigurationPath(_source.Part.Name, _source.Part.PartName, environment);
41+
4042
var protector = CreateProtector();
4143

4244
// If we were able to load the configuration we save it to the local file system as a cache.
@@ -58,8 +60,10 @@ public override void Load()
5860
{
5961
// If the file does not exist we return null to indicate that we could not load the
6062
// configuration from the local file system.
61-
if (File.Exists(filepath))
63+
if (!File.Exists(filepath))
6264
{
65+
Console.WriteLine($"Could not load configuration from file: File {filepath} not found");
66+
6367
return null;
6468
}
6569

@@ -86,8 +90,13 @@ private static void UpdateLocalConfiguration(
8690
{
8791
if (File.Exists(filepath))
8892
{
93+
Console.WriteLine($"Updating configuration in file {filepath}");
8994
File.Delete(filepath);
9095
}
96+
else
97+
{
98+
Console.WriteLine($"Creating configuration in file {filepath}");
99+
}
91100

92101
File.WriteAllText(filepath, protector.Protect(JsonSerializer.Serialize(data)));
93102
}
@@ -100,13 +109,15 @@ private static void UpdateLocalConfiguration(
100109
// to load the configuration from the authoring we need a valid authoring url
101110
if (_provider.ResolveAuthoringUrl() is not { } authoringUrl)
102111
{
112+
Console.WriteLine("Could not load configuration with az login: No authoring url found");
103113
return null;
104114
}
105115

106116
// to load the configuration we also need a valid token from az login otherwise we
107117
// can't load the configuration.
108118
if (GetAzureCliToken() is not { } token)
109119
{
120+
Console.WriteLine("Could not load configuration with az login: No token found");
110121
return null;
111122
}
112123

@@ -159,19 +170,24 @@ private static void UpdateLocalConfiguration(
159170
// load the configuration from the vault
160171
if (_provider.ResolveVaultToken() is not { } vaultToken)
161172
{
173+
Console.WriteLine(
174+
"Could not load configuration with vault token: No vault token found");
162175
return null;
163176
}
164177

165178
// we need a decryption key to decrypt the configuration from the vault otherwise we dont
166179
// even try to load the configuration from the vault
167180
if (_provider.ResolveDecryptionKey() is not { } decryptionKey)
168181
{
182+
Console.WriteLine(
183+
"Could not load configuration with vault token: No decryption key found");
169184
return null;
170185
}
171186

172187
// we need a vault url to load the configuration from the vault
173188
if (_provider.ResolveVaultUrl() is not { } vaultUrl)
174189
{
190+
Console.WriteLine("Could not load configuration with vault token: No vault url found");
175191
return null;
176192
}
177193

@@ -196,34 +212,6 @@ private static void UpdateLocalConfiguration(
196212

197213
return null;
198214
}
199-
200-
static CypherAndIv? FetchVaultConfig(
201-
string applicationName,
202-
string partName,
203-
string environment,
204-
string vaultToken)
205-
{
206-
try
207-
{
208-
var client = VaultClientFactory.CreateClient(vaultToken);
209-
var ct = CancellationToken.None;
210-
var response = client
211-
.GetAsync(applicationName, partName, environment, vaultToken, ct)
212-
.GetAwaiter()
213-
.GetResult();
214-
215-
if (response is null)
216-
{
217-
return null;
218-
}
219-
220-
return response.Deserialize<CypherAndIv>();
221-
}
222-
catch
223-
{
224-
return null;
225-
}
226-
}
227215
}
228216

229217
/// <summary>
@@ -271,7 +259,10 @@ private static void UpdateLocalConfiguration(
271259
.GetRequiredService<IDataProtectionProvider>()
272260
.CreateProtector("Confix_Token");
273261

274-
private static string GetTokenFromFile(string name, string partName, string environmentName)
262+
private static string CreateLocalConfigurationPath(
263+
string name,
264+
string partName,
265+
string environmentName)
275266
=> Path.Combine(Directory.GetCurrentDirectory(),
276267
$"{name}_{partName}_{environmentName}_confix.vaultconfig.json");
277268
}

0 commit comments

Comments
 (0)