Skip to content
This repository was archived by the owner on Feb 2, 2026. It is now read-only.
This repository was archived by the owner on Feb 2, 2026. It is now read-only.

Add audit tables to track IdSrv configuration data changes #61

Closed

Description

@cerginio

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/

Activity

  1. self-assigned this
    on Aug 24, 2018
  2. ruant commented on Aug 25, 2018

    @ruant

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

  3. skoruba commented on Aug 25, 2018

    @skoruba
    Owner

    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.

  4. added
    enhancement
    on Aug 25, 2018
  5. added this to the Future milestone on Aug 25, 2018
  6. skoruba commented on Sep 26, 2018

    @skoruba
    Owner

    @cerginio - Any suggestions about that?

  7. added
    help wanted
    on Sep 26, 2018
  8. cerginio commented on Oct 2, 2018

    @cerginio
    Author

    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.

  9. xmichaelx commented on Oct 5, 2018

    @xmichaelx
    Contributor

    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.

  10. Robban1980 commented on Nov 7, 2018

    @Robban1980

    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.

  11. RobCodemonkey commented on Nov 16, 2018

    @RobCodemonkey

    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.

  12. skoruba commented on Feb 9, 2019

    @skoruba
    Owner

    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.

  13. pinned this issue on May 1, 2019
  14. pbros commented on May 31, 2019

    @pbros
    Contributor

    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

  15. cerginio commented on Jun 17, 2019

    @cerginio
    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.

  16. cerginio commented on Jun 18, 2019

    @cerginio
    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

  17. cerginio commented on Jun 18, 2019

    @cerginio
    Author

    expected:
    1 modify

    audit-2

  18. skoruba commented on Nov 6, 2019

    @skoruba
    Owner

    Done in #391

  19. skoruba commented on Jan 24, 2020

    @skoruba
    Owner

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

  20. unpinned this issue on Jan 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementhelp wantedpriority: high

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions