-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathPamTargetSystemRepositoryTests.cs
More file actions
231 lines (204 loc) · 9.69 KB
/
Copy pathPamTargetSystemRepositoryTests.cs
File metadata and controls
231 lines (204 loc) · 9.69 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using Bit.Core.Repositories;
using Bit.Core.SecretsManager.Entities;
using Bit.Core.SecretsManager.Repositories;
using Bit.Core.Utilities;
using Bit.Core.Vault.Entities;
using Bit.Core.Vault.Enums;
using Bit.Core.Vault.Repositories;
using Bit.Infrastructure.IntegrationTest.AdminConsole;
using Bit.Infrastructure.IntegrationTest.Comparers;
using Bit.Pam.Entities;
using Bit.Pam.Enums;
using Bit.Pam.Repositories;
using Xunit;
namespace Bit.Infrastructure.IntegrationTest.Pam.Repositories;
public class PamTargetSystemRepositoryTests
{
[DatabaseTheory, DatabaseData]
public async Task CreateAsync_ThenRead_RoundTripsFields(
IOrganizationRepository organizationRepository,
IPamTargetSystemRepository pamTargetSystemRepository)
{
var organization = await organizationRepository.CreateTestOrganizationAsync();
var now = DateTime.UtcNow;
var target = await pamTargetSystemRepository.CreateAsync(new PamTargetSystem
{
OrganizationId = organization.Id,
Name = "prod-mssql",
Method = PamTargetSystemMethod.Automatic,
Kind = PamTargetSystemKind.Mssql,
PasswordPolicy = """{"minLength":16,"maxLength":32,"includeUppercase":true,"includeDigits":true}""",
SupportsSessionTermination = true,
Status = PamTargetSystemStatus.Active,
CreationDate = now,
RevisionDate = now,
});
var persisted = await pamTargetSystemRepository.GetByIdAsync(target.Id);
Assert.NotNull(persisted);
Assert.Equal(organization.Id, persisted!.OrganizationId);
Assert.Equal("prod-mssql", persisted.Name);
Assert.Equal(PamTargetSystemMethod.Automatic, persisted.Method);
Assert.Equal(PamTargetSystemKind.Mssql, persisted.Kind);
Assert.Contains("minLength", persisted.PasswordPolicy);
Assert.True(persisted.SupportsSessionTermination);
Assert.Equal(PamTargetSystemStatus.Active, persisted.Status);
}
// Kind is a bare TINYINT with no check constraint: every connector kind persists on every provider without a
// schema change.
[DatabaseTheory, DatabaseData]
public async Task CreateAsync_ThenRead_RoundTripsEveryAutomaticKind(
IOrganizationRepository organizationRepository,
IPamTargetSystemRepository pamTargetSystemRepository)
{
var organization = await organizationRepository.CreateTestOrganizationAsync();
var now = DateTime.UtcNow;
foreach (var kind in Enum.GetValues<PamTargetSystemKind>())
{
var target = await pamTargetSystemRepository.CreateAsync(
BuildTarget(organization.Id, $"target-{kind}", now, kind));
var persisted = await pamTargetSystemRepository.GetByIdAsync(target.Id);
Assert.NotNull(persisted);
Assert.Equal(kind, persisted!.Kind);
}
}
// A manual target carries no connector; Kind/PasswordPolicy stay null through the round trip.
[DatabaseTheory, DatabaseData]
public async Task ReplaceAsync_UpdatesFields(
IOrganizationRepository organizationRepository,
IPamTargetSystemRepository pamTargetSystemRepository)
{
var organization = await organizationRepository.CreateTestOrganizationAsync();
var now = DateTime.UtcNow;
var target = await pamTargetSystemRepository.CreateAsync(new PamTargetSystem
{
OrganizationId = organization.Id,
Name = "manual-vault",
Method = PamTargetSystemMethod.Manual,
Status = PamTargetSystemStatus.Active,
CreationDate = now,
RevisionDate = now,
});
Assert.Null(target.Kind);
Assert.Null(target.PasswordPolicy);
target.Name = "manual-vault-renamed";
target.Status = PamTargetSystemStatus.Disabled;
target.RevisionDate = now.AddMinutes(5);
await pamTargetSystemRepository.ReplaceAsync(target);
var persisted = await pamTargetSystemRepository.GetByIdAsync(target.Id);
Assert.NotNull(persisted);
Assert.Equal("manual-vault-renamed", persisted!.Name);
Assert.Equal(PamTargetSystemStatus.Disabled, persisted.Status);
Assert.Equal(now.AddMinutes(5), persisted.RevisionDate, LaxDateTimeComparer.Default);
Assert.Null(persisted.Kind);
Assert.Null(persisted.PasswordPolicy);
}
[DatabaseTheory, DatabaseData]
public async Task GetManyByOrganizationIdAsync_ScopesToOrganization(
IOrganizationRepository organizationRepository,
IPamTargetSystemRepository pamTargetSystemRepository)
{
var organization = await organizationRepository.CreateTestOrganizationAsync();
var otherOrganization = await organizationRepository.CreateTestOrganizationAsync();
var now = DateTime.UtcNow;
var mine = await pamTargetSystemRepository.CreateAsync(BuildTarget(organization.Id, "mine", now));
await pamTargetSystemRepository.CreateAsync(BuildTarget(otherOrganization.Id, "not-mine", now));
var results = await pamTargetSystemRepository.GetManyByOrganizationIdAsync(organization.Id);
var row = Assert.Single(results);
Assert.Equal(mine.Id, row.Id);
}
[DatabaseTheory, DatabaseData]
public async Task DeleteWithAssignmentsAsync_CascadesAssignments_ButRefusesWhileAConfigNamesTheTarget(
IOrganizationRepository organizationRepository,
IPamTargetSystemRepository pamTargetSystemRepository,
IApiKeyRepository apiKeyRepository,
IPamDaemonRepository pamDaemonRepository,
ICipherRepository cipherRepository,
IPamRotationConfigRepository pamRotationConfigRepository)
{
var organization = await organizationRepository.CreateTestOrganizationAsync();
var now = DateTime.UtcNow;
var target = await pamTargetSystemRepository.CreateAsync(BuildTarget(organization.Id, "prod-entra", now));
var daemon = await CreateEnrolledDaemonAsync(apiKeyRepository, pamDaemonRepository, organization.Id);
await pamDaemonRepository.CreateAssignmentAsync(new PamDaemonTargetAssignment
{
Id = CombGuid.Generate(),
DaemonId = daemon.Id,
TargetSystemId = target.Id,
OrganizationId = organization.Id,
CreationDate = now,
});
var cipher = await cipherRepository.CreateAsync(new Cipher
{
OrganizationId = organization.Id,
Type = CipherType.Login,
Data = "{\"originalSecret\":true}",
});
var config = await pamRotationConfigRepository.CreateAsync(new PamRotationConfig
{
OrganizationId = organization.Id,
CipherId = cipher.Id,
TargetSystemId = target.Id,
AccountIdentity = "svc-account",
TerminateSessions = false,
RotateOnAccessEnd = false,
Enabled = true,
CreationDate = now,
RevisionDate = now,
});
// Delete is refused while a config names the target.
Assert.False(await pamTargetSystemRepository.DeleteWithAssignmentsAsync(target.Id));
Assert.NotNull(await pamTargetSystemRepository.GetByIdAsync(target.Id));
Assert.True(await pamDaemonRepository.AssignmentExistsAsync(daemon.Id, target.Id));
Assert.True(await pamRotationConfigRepository.DeleteWithJobsAsync(config.Id));
// The assignment (connector-to-target edge) cascades with the target rather than blocking on the NO ACTION FK.
Assert.True(await pamTargetSystemRepository.DeleteWithAssignmentsAsync(target.Id));
Assert.Null(await pamTargetSystemRepository.GetByIdAsync(target.Id));
Assert.False(await pamDaemonRepository.AssignmentExistsAsync(daemon.Id, target.Id));
// The access connector itself outlives the target it was assigned to.
Assert.NotNull(await pamDaemonRepository.GetByIdAsync(daemon.Id));
}
[DatabaseTheory, DatabaseData]
public async Task DeleteWithAssignmentsAsync_NoAssignments_DeletesTheTarget(
IOrganizationRepository organizationRepository,
IPamTargetSystemRepository pamTargetSystemRepository)
{
var organization = await organizationRepository.CreateTestOrganizationAsync();
var target = await pamTargetSystemRepository.CreateAsync(
BuildTarget(organization.Id, "unassigned", DateTime.UtcNow));
Assert.True(await pamTargetSystemRepository.DeleteWithAssignmentsAsync(target.Id));
Assert.Null(await pamTargetSystemRepository.GetByIdAsync(target.Id));
}
private static async Task<PamDaemon> CreateEnrolledDaemonAsync(
IApiKeyRepository apiKeyRepository, IPamDaemonRepository pamDaemonRepository, Guid organizationId)
{
var apiKey = await apiKeyRepository.CreateAsync(new ApiKey
{
ServiceAccountId = null,
Name = $"daemon-{Guid.NewGuid()}",
Scope = """["api.pam.rotation"]""",
EncryptedPayload = "encrypted-payload",
Key = "encrypted-key",
});
return await pamDaemonRepository.CreateAsync(new PamDaemon
{
OrganizationId = organizationId,
Name = $"daemon-{Guid.NewGuid()}",
ApiKeyId = apiKey.Id,
Status = PamAccessConnectorStatus.Enabled,
});
}
private static PamTargetSystem BuildTarget(
Guid organizationId,
string name,
DateTime now,
PamTargetSystemKind kind = PamTargetSystemKind.Entra) => new()
{
OrganizationId = organizationId,
Name = name,
Method = PamTargetSystemMethod.Automatic,
Kind = kind,
Status = PamTargetSystemStatus.Active,
CreationDate = now,
RevisionDate = now,
};
}