Skip to content

The pk secrets env command for meeting Development Environment Usecase #31

@CMCDragonkai

Description

@CMCDragonkai

Specification

  1. The pk secrets env command should operate similar to the unix env command
  2. We can use pk secrets env to do double duty: inject environment variables into a new subcommand, or allow sourcing of environment variables into an existing subshell
  3. The task for the development environment usecase is specified in https://github.com/MatrixAI/Polykey-Desktop/issues/77
  4. The pk secrets env someprogram arg1 arg2 should do a proper process replacement when possible

Additional context

Copied excerpt:

Now when we want to start development on the project, we have 2 options:

  1. Open up a shell with Polykey sourcing the environment variables
  2. Use Polykey to inject environment variables into a sub-shell

Either result is the same, because you'll be running a shell with environment variables set so that your test runs of your software can inherit this environment configuration. Let's demonstrate both.

In the first case, we will reuse the source command and ask pk to constuct the equivalent of the .env file but entirely in-memory and only for this specific usage. We must also use process substitution <(...) so that the pk command output can be redirected into a temporary file descriptor that is read by the source command.

# one secret
source <(pk secrets env my-software-project:AWS_ACCESS_KEY_ID)

# multiple secrets
source <(pk secrets env my-software-project:AWS_ACCESS_KEY_ID my-software-project:GOOGLE_MAPS_API_KEY)

# globbing style (you can use globstar as well, this will only export immediate files)
source <(pk secrets env my-software-project:*)

# use the -e flag to export all variables
source <(pk secrets env -e my-software-project:*)
# use the -- to separate so you can export just one out of many
source <(pk secrets env -- my-software-project:AWS_ACCESS_KEY_ID -e my-software-project:GOOGLE_MAPS_API_KEY)
source <(pk secrets env -e -- my-software-project:AWS_ACCESS_KEY_ID my-software-project:GOOGLE_MAPS_API_KEY)

Now your shell has the relevant environment variables set by Polykey, and they will exist for as long as this current shell is alive.

In the second case, we ill use pk secrets env command to run a subshell, it can run any subprogram, but in the context of a development environment, you usually want a shell.

# one secret
pk secrets env my-software-project:AWS_ACCESS_KEY_ID bash

# multiple secrets
pk secrets env my-software-project:AWS_ACCESS_KEY_ID my-software-project:GOOGLE_MAPS_API_KEY bash

# globbing style
pk secrets env my-software-project:* bash

# the -e flag has no effect when using it this way
# this is because the subprogram determines whether to export variable or not
# it usually exports the variable
pk secrets env -e my-software-project:* bash

Now you have a subshell that has the environment variables configured, it will also automatically export it to child programs. When you have finished your development, you can just exit the shell, and the environment variables are gone!

Tasks

  1. Ensure that pk secrets env can be used to inject environment variables and run a subprocess
  2. Ensure that pk secrets env can be used to source environment variables and output something that can be used as a file to be sourced by a shell
  3. Deal with the fact that nodejs doesn't have an exec that can replace the current node process: Ability to replace current Node process with another nodejs/node#21664
    • You can deal with this the way babel deals with it

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions