serilog-contrib / Serilog.Enrichers.Sensitive

A Serilog LogEvent enricher that masks sensitive data
MIT License
111 stars 23 forks source link

Transform C# Configuration as Json #20

Closed SViradiya-MarutiTech closed 1 year ago

SViradiya-MarutiTech commented 1 year ago

We are not doing hard code C# hard code configuration in Startup/Program.cs, we have stored JSON Configuration of Serilog in AWS SSM, So to apply this Enrichers, how can I transform or configure below code in JSON?

  var logger = new LoggerConfiguration()
                .Enrich.WithSensitiveDataMasking(o =>
                {
                    o.Mode = MaskingMode.Globally;
                    o.ExcludeProperties.Add("email");
                    o.MaskValue = "^^";
                })
                .WriteTo.Console()
                .CreateLogger();

I able to Set Enricher in Serilog JSON Configuration like below. I am using other Enrichers as JSON configuration only.

"Serilog": {
    "Enrich": {
        "WithSensitiveDataMasking": {
            "Name": "WithSensitiveDataMasking"
        }
    }
}

but not able to transform other proprties.

SViradiya-MarutiTech commented 1 year ago

I tried below, but no luck

{
  "Serilog": {
    "Enrich": {
      "WithSensitiveDataMasking": {
        "Name": "WithSensitiveDataMasking",
        "Args": {
          "SensitiveDataEnricherOptions": {
            "Mode": "Globally",
            "ExcludeProperties": [
              "email"
            ],
            "MaskValue": "^^"
          }
        }
      }
    }
  }
}
sandermvanvliet commented 1 year ago

Because configuration of the enricher uses an options lambda it's not possible to use the currently supported configuration for the enricher via JSON as you can't "serialize" something like: options => options.MaskValue = "CUSTOM"; to JSON.

sandermvanvliet commented 1 year ago

Turned out to be not super hard: https://github.com/serilog-contrib/Serilog.Enrichers.Sensitive#json-configuration

Will be in package release 1.7.0 which should appear on NuGet shortly.

SViradiya-MarutiTech commented 1 year ago

@sandermvanvliet Thanks a lot..

SViradiya-MarutiTech commented 1 year ago

Can we add Custom masking operators, as in latest release, you have provided Custom masking on Email. but I am thinking to add in MakingOperators list, So is it possible?


{
  "Serilog": {
    "Using": [
      "Serilog.Enrichers.Sensitive"
    ],
    "Enrich": [
      {
        "Name": "WithSensitiveDataMasking",
        "Args": {
          "options": {
            "MaskValue": "CUSTOM_MASK_FROM_JSON",
            "ExcludeProperties": [
              "email"
            ],
            "MaskingOperators":["My.Assembly.Name.CustomMaskingOperator"],

            "Mode": "Globally"
          }
        }
      }
    ]
  }
}