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

usersecrets are not accessible inside container for worker service project #36062

Closed
pratiksanglikar opened this issue Jan 17, 2020 · 15 comments
Closed

Comments

@pratiksanglikar
Copy link

Describe the bug

The user secrets for a worker service are not accessible inside docker container.

To Reproduce

Steps to reproduce the behavior:

  1. Create a new .NET Core 3.0/3.1 WorkerService project using Visual Studio. (Or just clone this repo and jump to step 5)
  2. Right click on the project, click "Add Docker Support" select "Linux".
  3. Edit the project file and add -
    a. Package reference to Microsoft.Extensions.Configuration.UserSecrets as following <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="3.1.1" />
    Because of this issue #2743
    b. A volume mapping of the user secrets directory inside the container by adding following line to the property group -
    <DockerfileRunArguments>-v "$(AppData)/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro"</DockerfileRunArguments>
  4. Modify the file Worker.cs to access the user secrets -
 public class Worker : BackgroundService
    {
        private readonly ILogger<Worker> _logger;
        private readonly IConfiguration configuration;

        public Worker(ILogger<Worker> logger, IConfiguration configuration)
        {
            _logger = logger;
            this.configuration = configuration;
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                _logger.LogInformation("The secret - {}", this.configuration["secretKey"]);
                await Task.Delay(1000, stoppingToken);
            }
        }
    }
  1. On powershell or command prompt, navigate to the project directory and add user secrets using the command -
    dotnet user-secrets set "secretKey" "secretValue"

  2. hit F5 with the Docker debug profile and monitor the output window. Notice that the value of the user secret is null.

Note: You can open the Containers window in the VS to check the file contents of the container.
If you check the path - /root/.microsoft/usersecrets you will notice the user-secrets directory with secrets.json inside it.

Expected behavior

The output should print the value of the user-secret.

Screenshots

Filesystem and output of the program

UserSecretIDAttribute in the assembly

Additional context

Same program with WorkerService debug profile works well.

@Lechus
Copy link

Lechus commented Mar 19, 2020

is this fixed?

@analogrelay analogrelay transferred this issue from dotnet/extensions May 7, 2020
@Dotnet-GitSync-Bot
Copy link
Collaborator

I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label May 7, 2020
@ericstj ericstj removed the untriaged New issue has not been triaged by the area owner label Jun 18, 2020
@ericstj ericstj added this to the 5.0.0 milestone Jun 18, 2020
@guibranco
Copy link

Any news ?

@Lesthad
Copy link

Lesthad commented Feb 18, 2021

I am running into the same issue for a console app running in docker. Any news on this?

@wzchua
Copy link
Contributor

wzchua commented Mar 20, 2021

There's no issue with the mount. The docker image default DOTNET_ENVIRONMENT value is * (it's actually unset). If you set it to Development in the launchSettings.json, it will work.

    "Docker": {
      "commandName": "Docker",
      "environmentVariables": {
        "DOTNET_ENVIRONMENT": "Development"
      }
    }

@pratiksanglikar
Copy link
Author

@wzchua,
Why worker service's behavior is different than that of ASP.NET application?

@wzchua
Copy link
Contributor

wzchua commented Mar 26, 2021

when you run the default aspnetcore project with docker. -e "ASPNETCORE_ENVIRONMENT=Development" is added to the commandline argument

@angusmillar
Copy link

Having the same issue here. I can see the problem as explained by @wzchua is that my DOTNET_ENVIRONMENT value is not being set. However, I have added the launchSettings.json as follows. It just does not pick it up. How else can I set that environment variable?

{
  "profiles": {
    "Iata.Verity.Service": {
      "commandName": "Project",
      "environmentVariables": {
        "DOTNET_ENVIRONMENT": "Development"
      },
      "dotnetRunMessages": "true"
    },
    "Docker": {
      "commandName": "Docker",
      "environmentVariables": {
        "DOTNET_ENVIRONMENT": "Development"
      }
    }
  }
}

image

@maryamariyan maryamariyan modified the milestones: 6.0.0, 7.0.0 Aug 6, 2021
@jcotton42
Copy link

jcotton42 commented Apr 23, 2022

@angusmillar Not that it's a proper fix, but telling Docker directly about it seems to work. For example I have this in my docker-compose.override.yml under the node for the relevant service.

environment:                      
  - DOTNET_ENVIRONMENT=Development
volumes:                                                               
  - "${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets/:ro"

I also needed the volume definition as it wasn't being mapped by default.

@maryamariyan
Copy link
Member

@angusmillar Not that it's a proper fix, but telling Docker directly about it seems to work. For example I have this in my docker-compose.override.yml under the node for the relevant service.

Closing since there seems to be a workaround

@jcotton42
Copy link

@angusmillar Not that it's a proper fix, but telling Docker directly about it seems to work. For example I have this in my docker-compose.override.yml under the node for the relevant service.

Closing since there seems to be a workaround

Closing this issue is not proper. The Docker tooling should do the setup for user secrets automatically. Please reopen this.

@maryamariyan
Copy link
Member

Closing this issue is not proper. The Docker tooling should do the setup for user secrets automatically. Please reopen this.

@MichaelSimons what are your thoughts here?

@maryamariyan maryamariyan reopened this May 10, 2022
@eerhardt
Copy link
Member

cc @mthalman as well.

@MichaelSimons
Copy link
Member

MichaelSimons commented May 10, 2022

This seems like a VS Docker Tools issue vs a runtime issue. I suggest opening an issue at https://github.com/microsoft/dockertools/issues.

@eerhardt
Copy link
Member

Closing as this is not a dotnet/runtime issue. If someone is blocked on the VS Docker Tools, please open an issue at https://github.com/microsoft/dockertools/issues.

@ghost ghost locked as resolved and limited conversation to collaborators Aug 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests