-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathSampleService.cs
More file actions
167 lines (141 loc) · 6.44 KB
/
SampleService.cs
File metadata and controls
167 lines (141 loc) · 6.44 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (c) One Identity LLC. All rights reserved.
namespace SampleA2aService;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Security;
using System.Text.Json;
using OneIdentity.SafeguardDotNet;
using OneIdentity.SafeguardDotNet.A2A;
using OneIdentity.SafeguardDotNet.Event;
using Serilog;
internal class SampleService
{
private readonly string _safeguardAddress;
private readonly string _safeguardClientCertificateThumbprint;
private readonly int _safeguardApiVersion;
private readonly bool _safeguardIgnoreSsl;
private ISafeguardConnection _connection;
private ISafeguardA2AContext _a2AContext;
private readonly List<ISafeguardEventListener> _listeners = new List<ISafeguardEventListener>();
private sealed class MonitoredPassword
{
public SecureString ApiKey { get; set; }
public string AssetName { get; set; }
public string AccountName { get; set; }
public override string ToString() => $"{AssetName}/{AccountName}";
}
private readonly List<MonitoredPassword> _monitoredPasswords = new List<MonitoredPassword>();
public SampleService()
{
_safeguardAddress =
ConfigUtils.ReadRequiredSettingFromAppConfig("SafeguardAddress", "Safeguard appliance network address");
_safeguardClientCertificateThumbprint =
ConfigUtils.ReadRequiredSettingFromAppConfig("SafeguardClientCertificateThumbprint",
"Safeguard client certificate thumbprint").ToUpper(CultureInfo.InvariantCulture);
_safeguardApiVersion =
int.Parse(ConfigUtils.ReadRequiredSettingFromAppConfig("SafeguardApiVersion", "Safeguard API version"), CultureInfo.InvariantCulture);
_safeguardIgnoreSsl = bool.Parse(ConfigurationManager.AppSettings["SafeguardIgnoreSsl"]);
}
private void GetApiKeysFromA2ARegistrations()
{
// optionally you can have Safeguard look up all A2A registrations for a given certificate user thumbprint
// currently this requires auditor permission, but we will enhance A2A to include read ability without it
try
{
var a2AJson = _connection.InvokeMethod(Service.Core, Method.Get, "A2ARegistrations", parameters: new Dictionary<string, string>
{
{ "filter", $"CertificateUserThumbprint ieq '{_safeguardClientCertificateThumbprint}'" },
});
using var a2ADoc = JsonDocument.Parse(a2AJson);
foreach (var a2A in a2ADoc.RootElement.EnumerateArray())
{
var id = a2A.GetProperty("Id").ToString();
var credsJson = _connection.InvokeMethod(Service.Core, Method.Get, $"A2ARegistrations/{id}/RetrievableAccounts");
using var credsDoc = JsonDocument.Parse(credsJson);
foreach (var cred in credsDoc.RootElement.EnumerateArray())
{
_monitoredPasswords.Add(new MonitoredPassword
{
ApiKey = (cred.TryGetProperty("ApiKey", out var akEl) ? akEl.GetString() : string.Empty).ToSecureString(),
AssetName = cred.TryGetProperty("SystemName", out var snEl) ? snEl.GetString() : null,
AccountName = cred.TryGetProperty("AccountName", out var anEl) ? anEl.GetString() : null,
});
}
}
}
catch (Exception ex)
{
throw new InvalidOperationException("Unable to get API keys using certificate user, did you grant auditor permissions?", ex);
}
}
private void PasswordChangeHandler(string eventName, string eventBody)
{
var eventInfo = JsonSerializer.Deserialize<MonitoredPassword>(eventBody);
Log.Information("Password changed for {MonitoredPassword}", eventInfo);
// NOTE: eventInfo won't have the API key field filled out because that isn't in the eventBody Json
// You can look up in the list of _monitoredPasswords to find the API key
try
{
var apiKey = _monitoredPasswords.Single(mp => mp.AssetName == eventInfo.AssetName && mp.AccountName == eventInfo.AccountName).ApiKey;
using var password = _a2AContext.RetrievePassword(apiKey);
// TODO: Add useful code here to do something with the fetched password
// Also, note that the password you get back is a SecureString. In order to turn it back into a regular string
// you can use the provided convenience function:
// password.ToInsecureString()
}
#pragma warning disable CA1031 // Intentional top-level catch-all for error logging
catch (Exception ex)
#pragma warning restore CA1031
{
Log.Information(ex, "Password not in monitored list for handled event {MonitoredPassword}", eventInfo);
}
}
private void StartListener(MonitoredPassword monitored)
{
Log.Information("Startling listener for {MonitoredPassword}", monitored);
var listener = _a2AContext.GetPersistentA2AEventListener(monitored.ApiKey, PasswordChangeHandler);
listener.Start();
_listeners.Add(listener);
}
public void Start()
{
ConfigUtils.CheckForDebugHook();
// connect to Safeguard
_connection = Safeguard.Connect(
_safeguardAddress,
_safeguardClientCertificateThumbprint,
_safeguardApiVersion,
_safeguardIgnoreSsl);
_a2AContext = Safeguard.A2A.GetContext(
_safeguardAddress,
_safeguardClientCertificateThumbprint,
_safeguardApiVersion,
_safeguardIgnoreSsl);
// figure out what API keys to monitor
GetApiKeysFromA2ARegistrations();
if (_monitoredPasswords.Count == 0)
{
throw new InvalidOperationException("No API keys found in A2A registrations. Nothing to do.");
}
Log.Information("Found {MonitoredPasswordCount} API keys to monitor for password changes", _monitoredPasswords.Count);
// start the listeners
foreach (var monitored in _monitoredPasswords)
{
StartListener(monitored);
}
}
public void Stop()
{
// shut everything down
foreach (var listener in _listeners)
{
listener?.Stop();
listener?.Dispose();
}
_connection?.Dispose();
_a2AContext?.Dispose();
}
}