Repository navigation
Expand file tree
/
Copy pathQuick.Core.Configuration.Secrets.pas
More file actions
132 lines (109 loc) · 3.15 KB
/
Copy pathQuick.Core.Configuration.Secrets.pas
File metadata and controls
132 lines (109 loc) · 3.15 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
{ ***************************************************************************
Copyright (c) 2016-2026 Kike Perez
Unit : Quick.Core.Configuration.Secrets
Description : Secrets management service
Author : Kike Perez
Version : 1.0
Created : 13/04/2026
Modified : 08/05/2026
*************************************************************************** }
unit Quick.Core.Configuration.Secrets;
{$i QuickCore.inc}
interface
uses
System.SysUtils,
System.IOUtils,
System.JSON,
System.Generics.Collections,
Quick.Options,
Quick.Core.DependencyInjection;
type
TSecretsOptions = class(TOptions)
private
fSecretFile : string;
public
constructor Create; override;
published
property SecretFile : string read fSecretFile write fSecretFile;
end;
ISecretsManager = interface
['{A2A0B2E7-0A9B-46A2-9B39-8D1ACFD14A33}']
function GetSecret(const aKey : string; const aDefault : string = '') : string;
end;
TSecretsManager = class(TInterfacedObject, ISecretsManager)
private
fOptions : TSecretsOptions;
fSecrets : TDictionary<string, string>;
procedure LoadSecrets;
public
constructor Create(aOptions : IOptions<TSecretsOptions>);
destructor Destroy; override;
function GetSecret(const aKey : string; const aDefault : string = '') : string;
end;
TSecretsServiceExtension = class(TServiceCollectionExtension)
public
class function AddSecrets(const aSecretFile : string = 'appsecrets.json') : TServiceCollection;
end;
implementation
{ TSecretsOptions }
constructor TSecretsOptions.Create;
begin
inherited;
Name := 'Secrets';
HideOptions := True;
fSecretFile := 'appsecrets.json';
end;
{ TSecretsManager }
constructor TSecretsManager.Create(aOptions : IOptions<TSecretsOptions>);
begin
inherited Create;
fOptions := aOptions.Value;
fSecrets := TDictionary<string, string>.Create;
LoadSecrets;
end;
destructor TSecretsManager.Destroy;
begin
fSecrets.Free;
inherited;
end;
function TSecretsManager.GetSecret(const aKey, aDefault : string) : string;
begin
if not fSecrets.TryGetValue(aKey, Result) then
Result := aDefault;
end;
procedure TSecretsManager.LoadSecrets;
var
fileName : string;
json : TJSONObject;
pair : TJSONPair;
begin
fileName := fOptions.SecretFile;
if fileName.IsEmpty then
Exit;
if TPath.IsRelativePath(fileName) then
fileName := TPath.Combine(GetCurrentDir, fileName);
if not FileExists(fileName) then
Exit;
json := TJSONObject.ParseJSONValue(TFile.ReadAllText(fileName, TEncoding.UTF8)) as TJSONObject;
try
if json = nil then
Exit;
for pair in json do
fSecrets.AddOrSetValue(pair.JsonString.Value, pair.JsonValue.Value);
finally
json.Free;
end;
end;
{ TSecretsServiceExtension }
class function TSecretsServiceExtension.AddSecrets(const aSecretFile : string) : TServiceCollection;
var
opts : TSecretsOptions;
begin
Result := ServiceCollection;
opts := TSecretsOptions.Create;
opts.SecretFile := aSecretFile;
Result.Configure<TSecretsOptions>(opts);
if not Result.IsRegistered<ISecretsManager, TSecretsManager> then
Result.AddSingleton<ISecretsManager, TSecretsManager>;
end;
end.