-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[PM-40336] Access Rules: domain, persistence & schema #7981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Hinton
wants to merge
9
commits into
main
Choose a base branch
from
pam/access-rule
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+13,872
β20
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ed7863e
Add the Pam.Domain library with the AccessRule domain and audit-eventβ¦
Hinton 7d1e12c
Add Dapper and EF Core AccessRule repositories
Hinton 265c3b9
Add AccessRule schema: SSDT tables/procs, Collection association, andβ¦
Hinton 268b892
Add AccessRule repository integration tests
Hinton 15635ab
Trim PAM audit-event contract to the rule-administration events
Hinton f0767eb
Harden AccessRule repository delete and detail read
Hinton 94b283c
Remove the PAM audit-event surface
Hinton 31229c2
Assign PAM code to team-pam-dev via a single **/Pam* codeowner glob
Hinton 0d9002b
Trim stale audit-event reference from AccessRule delete comments
Hinton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/Infrastructure.Dapper/Pam/Repositories/AccessRuleRepository.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| ο»Ώusing System.Data; | ||
| using Bit.Core.Settings; | ||
| using Bit.Infrastructure.Dapper.Repositories; | ||
| using Bit.Pam.Entities; | ||
| using Bit.Pam.Models; | ||
| using Bit.Pam.Repositories; | ||
| using Dapper; | ||
| using Microsoft.Data.SqlClient; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace Bit.Infrastructure.Dapper.Pam.Repositories; | ||
|
|
||
| public class AccessRuleRepository : Repository<AccessRule, Guid>, IAccessRuleRepository | ||
| { | ||
| public AccessRuleRepository(GlobalSettings globalSettings) | ||
| : this(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString) | ||
| { } | ||
|
|
||
| public AccessRuleRepository(string connectionString, string readOnlyConnectionString) | ||
| : base(connectionString, readOnlyConnectionString) | ||
| { } | ||
|
|
||
| public async Task<ICollection<AccessRule>> GetManyByOrganizationIdAsync(Guid organizationId) | ||
| { | ||
| using var connection = new SqlConnection(ConnectionString); | ||
| var results = await connection.QueryAsync<AccessRule>( | ||
| $"[{Schema}].[AccessRule_ReadByOrganizationId]", | ||
| new { OrganizationId = organizationId }, | ||
| commandType: CommandType.StoredProcedure); | ||
|
|
||
| return results.ToList(); | ||
| } | ||
|
|
||
| public async Task<AccessRuleDetails?> GetDetailsByIdAsync(Guid id) | ||
| { | ||
| using var connection = new SqlConnection(ConnectionString); | ||
| using var results = await connection.QueryMultipleAsync( | ||
| $"[{Schema}].[AccessRule_ReadDetailsById]", | ||
| new { Id = id }, | ||
| commandType: CommandType.StoredProcedure); | ||
|
|
||
| var rule = await results.ReadFirstOrDefaultAsync<AccessRuleDetails>(); | ||
| if (rule is null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| rule.CollectionIds = (await results.ReadAsync<Guid>()).ToList(); | ||
| return rule; | ||
| } | ||
|
|
||
| public async Task<ICollection<AccessRuleDetails>> GetManyDetailsByOrganizationIdAsync(Guid organizationId) | ||
| { | ||
| using var connection = new SqlConnection(ConnectionString); | ||
| using var results = await connection.QueryMultipleAsync( | ||
| $"[{Schema}].[AccessRule_ReadDetailsByOrganizationId]", | ||
| new { OrganizationId = organizationId }, | ||
| commandType: CommandType.StoredProcedure); | ||
|
|
||
| var rules = (await results.ReadAsync<AccessRuleDetails>()).ToList(); | ||
| var collectionIdsByRule = (await results.ReadAsync<CollectionAccessRuleMapping>()) | ||
| .GroupBy(m => m.AccessRuleId) | ||
| .ToDictionary(g => g.Key, g => g.Select(m => m.CollectionId).ToList()); | ||
|
|
||
| foreach (var rule in rules) | ||
| { | ||
| if (collectionIdsByRule.TryGetValue(rule.Id, out var collectionIds)) | ||
| { | ||
| rule.CollectionIds = collectionIds; | ||
| } | ||
| } | ||
|
|
||
| return rules; | ||
| } | ||
|
|
||
| public async Task SetCollectionAssociationsAsync(Guid organizationId, Guid accessRuleId, | ||
| IEnumerable<Guid> collectionIdsToAssign, IEnumerable<Guid> collectionIdsToClear) | ||
| { | ||
| using var connection = new SqlConnection(ConnectionString); | ||
| await connection.ExecuteAsync( | ||
| $"[{Schema}].[Collection_SetAccessRuleAssociations]", | ||
| new | ||
| { | ||
| AccessRuleId = accessRuleId, | ||
| OrganizationId = organizationId, | ||
| ToAssign = collectionIdsToAssign.ToGuidIdArrayTVP(), | ||
| ToClear = collectionIdsToClear.ToGuidIdArrayTVP(), | ||
| }, | ||
| commandType: CommandType.StoredProcedure); | ||
| } | ||
|
|
||
| private sealed class CollectionAccessRuleMapping | ||
| { | ||
| public Guid AccessRuleId { get; set; } | ||
| public Guid CollectionId { get; set; } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/Infrastructure.EntityFramework/Pam/Models/AccessRule.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| ο»Ώ// FIXME: Update this file to be null safe and then delete the line below | ||
| #nullable disable | ||
|
|
||
| using AutoMapper; | ||
| using Bit.Infrastructure.EntityFramework.AdminConsole.Models; | ||
|
|
||
| namespace Bit.Infrastructure.EntityFramework.Pam.Models; | ||
|
|
||
| public class AccessRule : Bit.Pam.Entities.AccessRule | ||
|
Hinton marked this conversation as resolved.
Dismissed
|
||
| { | ||
| public virtual Organization Organization { get; set; } | ||
| } | ||
|
|
||
| public class AccessRuleMapperProfile : Profile | ||
| { | ||
| public AccessRuleMapperProfile() | ||
| { | ||
| CreateMap<Bit.Pam.Entities.AccessRule, AccessRule>().ReverseMap(); | ||
| CreateMap<AccessRule, Bit.Pam.Models.AccessRuleDetails>(); | ||
| } | ||
| } | ||
141 changes: 141 additions & 0 deletions
141
src/Infrastructure.EntityFramework/Pam/Repositories/AccessRuleRepository.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| ο»Ώusing AutoMapper; | ||
| using Bit.Infrastructure.EntityFramework.Repositories; | ||
| using Bit.Pam.Models; | ||
| using Bit.Pam.Repositories; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using CoreEntity = Bit.Pam.Entities.AccessRule; | ||
| using EfModel = Bit.Infrastructure.EntityFramework.Pam.Models.AccessRule; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace Bit.Infrastructure.EntityFramework.Pam.Repositories; | ||
|
|
||
| public class AccessRuleRepository : Repository<CoreEntity, EfModel, Guid>, IAccessRuleRepository | ||
| { | ||
| public AccessRuleRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper) | ||
| : base(serviceScopeFactory, mapper, context => context.AccessRules) | ||
| { } | ||
|
|
||
| public async Task<ICollection<CoreEntity>> GetManyByOrganizationIdAsync(Guid organizationId) | ||
| { | ||
| using var scope = ServiceScopeFactory.CreateScope(); | ||
| var dbContext = GetDatabaseContext(scope); | ||
| var rules = await dbContext.AccessRules | ||
| .Where(p => p.OrganizationId == organizationId) | ||
| .AsNoTracking() | ||
| .ToListAsync(); | ||
| return Mapper.Map<List<CoreEntity>>(rules); | ||
| } | ||
|
|
||
| public async Task<AccessRuleDetails?> GetDetailsByIdAsync(Guid id) | ||
| { | ||
| using var scope = ServiceScopeFactory.CreateScope(); | ||
| var dbContext = GetDatabaseContext(scope); | ||
| var rule = await dbContext.AccessRules | ||
| .Where(p => p.Id == id) | ||
| .AsNoTracking() | ||
| .FirstOrDefaultAsync(); | ||
| if (rule is null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| var details = Mapper.Map<AccessRuleDetails>(rule); | ||
| details.CollectionIds = await dbContext.Collections | ||
| .Where(c => c.AccessRuleId == id) | ||
| .Select(c => c.Id) | ||
| .ToListAsync(); | ||
| return details; | ||
| } | ||
|
|
||
| public async Task<ICollection<AccessRuleDetails>> GetManyDetailsByOrganizationIdAsync(Guid organizationId) | ||
| { | ||
| using var scope = ServiceScopeFactory.CreateScope(); | ||
| var dbContext = GetDatabaseContext(scope); | ||
| var rules = await dbContext.AccessRules | ||
| .Where(p => p.OrganizationId == organizationId) | ||
| .AsNoTracking() | ||
| .ToListAsync(); | ||
|
|
||
| var collectionIdsByRule = (await dbContext.Collections | ||
| .Where(c => c.OrganizationId == organizationId && c.AccessRuleId != null) | ||
| .Select(c => new { AccessRuleId = c.AccessRuleId!.Value, CollectionId = c.Id }) | ||
| .ToListAsync()) | ||
| .GroupBy(r => r.AccessRuleId) | ||
| .ToDictionary(g => g.Key, g => g.Select(r => r.CollectionId).ToList()); | ||
|
|
||
| return rules | ||
| .Select(rule => | ||
| { | ||
| var details = Mapper.Map<AccessRuleDetails>(rule); | ||
| if (collectionIdsByRule.TryGetValue(rule.Id, out var collectionIds)) | ||
| { | ||
| details.CollectionIds = collectionIds; | ||
| } | ||
| return details; | ||
| }) | ||
| .ToList(); | ||
| } | ||
|
|
||
| public override async Task DeleteAsync(CoreEntity accessRule) | ||
| { | ||
| using var scope = ServiceScopeFactory.CreateScope(); | ||
| var dbContext = GetDatabaseContext(scope); | ||
|
|
||
| await using var transaction = await dbContext.Database.BeginTransactionAsync(); | ||
|
|
||
| // Clear the collection links before deleting the rule: the FK Collection.AccessRuleId -> AccessRule is | ||
| // ON DELETE NO ACTION, so the delete fails while any collection still points at it. | ||
| await dbContext.Collections | ||
| .Where(c => c.AccessRuleId == accessRule.Id) | ||
| .ExecuteUpdateAsync(s => s | ||
| .SetProperty(c => c.AccessRuleId, (Guid?)null) | ||
| .SetProperty(c => c.RevisionDate, DateTime.UtcNow)); | ||
|
|
||
| await dbContext.AccessRules | ||
| .Where(r => r.Id == accessRule.Id) | ||
| .ExecuteDeleteAsync(); | ||
|
|
||
| await dbContext.UserBumpAccountRevisionDateByOrganizationIdAsync(accessRule.OrganizationId); | ||
| await dbContext.SaveChangesAsync(); | ||
| await transaction.CommitAsync(); | ||
| } | ||
|
|
||
| public async Task SetCollectionAssociationsAsync(Guid organizationId, Guid accessRuleId, | ||
| IEnumerable<Guid> collectionIdsToAssign, IEnumerable<Guid> collectionIdsToClear) | ||
| { | ||
| var assignIds = collectionIdsToAssign.ToList(); | ||
| var clearIds = collectionIdsToClear.ToList(); | ||
|
|
||
| using var scope = ServiceScopeFactory.CreateScope(); | ||
| var dbContext = GetDatabaseContext(scope); | ||
| var now = DateTime.UtcNow; | ||
|
|
||
| await using var transaction = await dbContext.Database.BeginTransactionAsync(); | ||
|
|
||
| if (clearIds.Count > 0) | ||
| { | ||
| await dbContext.Collections | ||
| .Where(c => c.OrganizationId == organizationId | ||
| && c.AccessRuleId == accessRuleId | ||
| && clearIds.Contains(c.Id)) | ||
| .ExecuteUpdateAsync(s => s | ||
| .SetProperty(c => c.AccessRuleId, (Guid?)null) | ||
| .SetProperty(c => c.RevisionDate, now)); | ||
| } | ||
|
|
||
| if (assignIds.Count > 0) | ||
| { | ||
| await dbContext.Collections | ||
| .Where(c => c.OrganizationId == organizationId && assignIds.Contains(c.Id)) | ||
| .ExecuteUpdateAsync(s => s | ||
| .SetProperty(c => c.AccessRuleId, accessRuleId) | ||
| .SetProperty(c => c.RevisionDate, now)); | ||
| } | ||
|
|
||
| await dbContext.UserBumpAccountRevisionDateByOrganizationIdAsync(organizationId); | ||
| await dbContext.SaveChangesAsync(); | ||
| await transaction.CommitAsync(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.