microsoft / kernel-memory

RAG architecture: index and query any data using LLM and natural language, track sources, show citations, asynchronous memory patterns.
https://microsoft.github.io/kernel-memory
MIT License
1.57k stars 300 forks source link

[Bug] Docker image fails to start when ASPNETCORE_ENVIRONMENT set to Production #851

Closed alexmg closed 3 hours ago

alexmg commented 6 hours ago

Context / Scenario

Starting from tag 0.80.241017.2 the service cannot be started in a Docker container when the ASPNETCORE_ENVIRONMENT environment variable is to anything other than Development.

What happened?

The exception below is thrown as soon as the service starts.

Unhandled exception. System.ApplicationException: Sensitive data logging can be enabled only in a development environment. Check ASPNETCORE_ENVIRONMENT env var.
   at Microsoft.KernelMemory.Diagnostics.SensitiveDataLogger.set_Enabled(Boolean value) in /src/service/Abstractions/Diagnostics/SensitiveDataLogger.cs:line 25
   at Microsoft.KernelMemory.Service.Program.Main(String[] args) in /src/service/Service/Program.cs:line 52

This is a result of a recent change to disable sensitive logging as the very first line of code during startup.

SensitiveDataLogger.Enabled = false;

The SensitiveDataLogger will throw an exception when the property setter is called unless the environment Development. This occurs regardless of the value the property is being set to. Because the container image runs using the Production environment (as it should) the exception is thrown, and the service fails to start.

Importance

I cannot use Kernel Memory

Platform, Language, Versions

Docker image: kernelmemory/service:0.80.241017.2

Relevant log output

No response

dluc commented 5 hours ago

This should fix it https://github.com/microsoft/kernel-memory/pull/852

alexmg commented 5 hours ago

Thanks @dluc. I started a PR and have some unit tests if you're interested in adding those.

public sealed class SensitiveDataLoggerTests : IDisposable
{
    private const string EnvironmentVariableName = "ASPNETCORE_ENVIRONMENT";

    [Fact]
    [Trait("Category", "UnitTest")]
    public void ItCanBeEnabledInDevelopmentEnvironment()
    {
        Environment.SetEnvironmentVariable(EnvironmentVariableName, "Development");
        SensitiveDataLogger.Enabled = true;
        Assert.True(SensitiveDataLogger.Enabled);
    }

    [Theory]
    [InlineData("Staging")]
    [InlineData("Production")]
    [Trait("Category", "UnitTest")]
    public void ItCannotBeEnabledInNonDevelopmentEnvironments(string environment)
    {
        Environment.SetEnvironmentVariable(EnvironmentVariableName, environment);
        Assert.Throws<ApplicationException>(() => SensitiveDataLogger.Enabled = true);
    }

    [Theory]
    [InlineData("Development")]
    [InlineData("Staging")]
    [InlineData("Production")]
    [Trait("Category", "UnitTest")]
    public void ItCanBeDisabledForAllEnvironments(string environment)
    {
        Environment.SetEnvironmentVariable(EnvironmentVariableName, environment);
        SensitiveDataLogger.Enabled = false;
        Assert.False(SensitiveDataLogger.Enabled);
    }

    public void Dispose() => Environment.SetEnvironmentVariable(EnvironmentVariableName, null);
}
dluc commented 5 hours ago

Unit tests welcome :-) Release with the fix in progress

alexmg commented 5 hours ago

I will open a PR with the additional unit tests.

dluc commented 4 hours ago

Should be fixed in 0.90.241021.1, could you confirm?

Thanks!!

alexmg commented 3 hours ago

I can confirm this is now fixed. 👍

Thank you for the quick turnaround on the fix @dluc!