Skip to content
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

Add audit tables to track IdSrv configuration data changes #61

Closed
cerginio opened this issue Aug 24, 2018 · 14 comments
Closed

Add audit tables to track IdSrv configuration data changes #61

cerginio opened this issue Aug 24, 2018 · 14 comments
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed priority: high
Milestone

Comments

@cerginio
Copy link

During administration clients, scopes, users, roles may be important to have control on it's history.

One of the cheap way to get changes history could be SQL server Track Data Changes feature.

  1. Add feature for SQL server
    https://docs.microsoft.com/en-us/sql/relational-databases/track-changes/enable-and-disable-change-data-capture-sql-server?view=sql-server-2017
  2. Add views to read tracking changes tables and build around them AuditDbContext.

Second options is to realize it on application level by using EF handlers and changetracker.
https://blog.tonysneed.com/2017/10/01/trackable-entities-for-ef-core/

@skoruba skoruba self-assigned this Aug 24, 2018
@ruant
Copy link

ruant commented Aug 25, 2018

I would like to see it not bound to MSSQL, that way people can choose what SQL technology they want to run themselves.

@skoruba
Copy link
Owner

skoruba commented Aug 25, 2018

I agree with @cerginio - audit log is very important. I'll check some details about the implementation that mentioned @ruant - it will be nice to use the implementation independent of the database engine.

@skoruba skoruba added the enhancement New feature or request label Aug 25, 2018
@skoruba skoruba added this to the Future milestone Aug 25, 2018
@skoruba
Copy link
Owner

skoruba commented Sep 26, 2018

@cerginio - Any suggestions about that?

@skoruba skoruba added the help wanted Extra attention is needed label Sep 26, 2018
@cerginio
Copy link
Author

cerginio commented Oct 2, 2018

I would chose application level logging based on EF handlers. Defenetely we must store in each row for each history table username from bearer token. I would use change tracker state and save row before change into history table. That tables should be defined in separate audit db context.
But how to achieve that with clean code is a big question, taking into account that we reuse ids4 entity model and do not have control on it.

@xmichaelx
Copy link
Contributor

xmichaelx commented Oct 5, 2018

I would suggest throwing event for every action we want to be part of the audit log and allow for registering listeners in the DI for those events.

public async Task<int> RemoveClientAsync(ClientDto client)
{
            var clientEntity = client.ToEntity();
            var result = await _clientRepository.RemoveClientAsync(clientEntity);
            this.auditLogger.Log(client, result);  // audit logger injected
            return result ;
}

Audit log sounds like a long list of events which we would ideally like to store in database designed for doing so like EventStore. In simplest scenario we could simply dump all events there without providing UI in AdminUI for reading them. Then for auditing we could use built-in EventStore.UI to query events either live or from past.

@skoruba skoruba added this to To do in 1.0.0-beta-5 Oct 23, 2018
@skoruba skoruba moved this from To do to In progress in 1.0.0-beta-5 Oct 23, 2018
@Robban1980
Copy link

I think an important part here is not also not bind this functionality to a specific storage type, like EventStore.
Raise an audit event and if subscribed to, the implementer can decide what they want to do with it. Be it storing it in MSSQL, PostgreSQL, EventStore etc.

@RobCodemonkey
Copy link

One of the compliance requirements for this kind of implementation would be immutability - I presume that would be handled on the implementer end rather than by the functionality handling audit events.

This might require guidelines for setup on the user side.

@skoruba
Copy link
Owner

skoruba commented Feb 9, 2019

Currently I am working on this stuff here:
https://github.com/skoruba/IdentityServer4.Admin/tree/feature/audit-logging
I have used the services for logging based on IdentityServer4.

@skoruba skoruba moved this from In progress to To do in 1.0.0-beta-5 Apr 4, 2019
@skoruba skoruba moved this from To do to Done in 1.0.0-beta-5 Apr 4, 2019
@skoruba skoruba moved this from Done to To do in 1.0.0-beta-5 Apr 4, 2019
@skoruba skoruba removed this from To do in 1.0.0-beta-5 Apr 4, 2019
@skoruba skoruba pinned this issue May 1, 2019
@pbros
Copy link
Contributor

pbros commented May 31, 2019

Will this audit logging extend to logging user logins?

It would be useful to have an audit log of successful (and failed) login history for each user. If presented to the user, he could notice suspicious activity

@cerginio
Copy link
Author

I played a bit with simple audit implementation in db agnostic way:
https://dejanstojanovic.net/aspnet/2018/november/tracking-data-changes-with-entity-framework-core/
https://www.meziantou.net/2017/08/14/entity-framework-core-history-audit-table

And it descover overhead in DAL implementation.
RemoveClientRelationsAsync(Client client) in
https://github.com/skoruba/IdentityServer4.Admin/blob/8ea5449965c8254d28c36c82ab45a6a77eafa663/src/Skoruba.IdentityServer4.Admin.BusinessLogic/Repositories/ClientRepository.cs

If you update single propery in Client - let's say Description from "A" to "B",
in audit logs you will see like all 9 navigation properties was removed and inserted!
and all client properties was modified. I tried to manually calculate diff and separate from the noise, but it killing my brain. it not worth of it.

@skoruba please consider how to implement an update in a more smart and lean way.
Above audit obfuscation such approach to update the whole client tree for single change kills performance.

I will try to suggest PR with audit this week.

@cerginio
Copy link
Author

#298
PR suggested

  1. Please take changes
  2. add migration for Audits table
  3. update db
  4. play with UI
  • choose random client
  • update description from "A" to "B"
  1. SELECT * FROM [dbo].[ClientConfigAudit]

ER: 13 changes in audit log
1 modify
6 delete
6 inserts

audit-1

@cerginio
Copy link
Author

expected:
1 modify

audit-2

@skoruba skoruba added this to In progress in 1.0.0-beta8 Oct 6, 2019
@skoruba
Copy link
Owner

skoruba commented Nov 6, 2019

Done in #391

@skoruba skoruba moved this from In progress to Done in 1.0.0-beta8 Nov 13, 2019
@skoruba
Copy link
Owner

skoruba commented Jan 24, 2020

I am closing this issue, it was released in master branch.

@skoruba skoruba closed this as completed Jan 24, 2020
@skoruba skoruba unpinned this issue Jan 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed priority: high
Projects
Future
  
Awaiting triage
1.0.0-beta8
  
Done
Development

No branches or pull requests

7 participants