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

Feat: Environment variables in templating #849

Closed
5 tasks
Meldiron opened this issue Nov 1, 2022 · 3 comments · Fixed by #1314
Closed
5 tasks

Feat: Environment variables in templating #849

Meldiron opened this issue Nov 1, 2022 · 3 comments · Fixed by #1314
Assignees
Labels
feature New feature request

Comments

@Meldiron
Copy link
Contributor

Meldiron commented Nov 1, 2022

I find Mockoon CLI useful for testing purposes in CI/CD environments. In my use case, I am spinning up multiple Mockoon instances running in a Docker container. Even tho I want them to source from the same config file, I would prefer having the option to identify instances.

As a solution to this, I would like to add a new set of helpers that would allow you to read environment variables. Implementation might look something like this:

{ "server": "{{env 'Variable-Name' 'default value'}}" }

To keep the solution secure, this would only allow reading variables that are prefixed with MOCKOON_. There should be an option (CLI parameter?) to change this prefix. Setting prefix to empty string and allowing reading any variable should NOT be allowed.

Source of security concerns and solutions is Vite.

TODO:

  • Discuss implementation with maintainers
  • Add System Helpers
  • Implement environment variable system helper
  • Write tests (if applicable)
  • Update docs

If possible, I would like to contribute to this issue.

@Meldiron Meldiron changed the title Environment variables in templating Feat: Environment variables in templating Nov 1, 2022
@255kb 255kb added the feature New feature request label Nov 1, 2022
@Meldiron Meldiron mentioned this issue Nov 5, 2022
10 tasks
@supr3m
Copy link

supr3m commented Feb 16, 2024

It would be great, even then the values ​​could be updated at runtime without restarting the service.

export MOCKOON_FOO_VAR=hello-world
fooVar = process.env.MOCKOON_FOO_VAR;
console.log('Show ENVVAR: ', fooVar); // Print "Show ENVVAR: hello-world"

Even better, dotenv can be used to isolate and bundle envvar's into a single .env file to be used only by the application.

MOCKOON_PRIVATE_KEY=-----BEGIN RSA PRIVATE KEY-----\nKh9NV...\n-----END RSA PRIVATE KEY-----\n
MOCKOON_PRIVATE_KEY_MULTILINE="-----BEGIN RSA PRIVATE KEY-----
...
Kh9NV...
...
-----END RSA PRIVATE KEY-----"
MOCKOON_ENV=development
MOCKOON_URL_HEALTHCHECK=http://localhost/api/v1/healthcheck
MOCKOON_HELLO=hello-world
import * as dotenv from 'dotenv';
dotenv.config();

process.env.MOCKOON_PRIVATE_KEY;
process.env.MOCKOON_PRIVATE_KEY_MULTILINE;
process.env.MOCKOON_ENV;
process.env.MOCKOON_URL_HEALTHCHECK;
process.env.MOCKOON_HELLO;

Reference:

@255kb 255kb self-assigned this Feb 20, 2024
@255kb
Copy link
Member

255kb commented Feb 20, 2024

@supr3m Thank you for the feedback. There is currently no plan to support dotenv files as it requires way more work to make it work in the desktop/CLI/serverless apps.
It will first support traditional env vars, the one usually set up in the OS settings, or using a command line export, set, etc.

255kb added a commit that referenced this issue Feb 21, 2024
Add tests in commons-server and CLI
Add serverless option
Add desktop setting
Closes #849
255kb added a commit that referenced this issue Feb 21, 2024
Add tests in commons-server and CLI
Add serverless option
Add desktop setting
Closes #849
255kb added a commit that referenced this issue Feb 21, 2024
Add tests in commons-server and CLI
Add serverless option
Add desktop setting
Closes #849
255kb added a commit that referenced this issue Feb 21, 2024
Add tests in commons-server and CLI
Add serverless option
Add desktop setting
Closes #849
255kb added a commit that referenced this issue Feb 21, 2024
Add tests in commons-server and CLI
Add serverless option
Add desktop setting
Closes #849
255kb added a commit that referenced this issue Feb 21, 2024
* Merge #858 PR and fix conflicts

Co-authored-by: Matej Bačo <matejbaco2000@gmail.com>

* Finalize env vars feature

Add tests in commons-server and CLI
Add serverless option
Add desktop setting
Closes #849

---------

Co-authored-by: Matej Bačo <matejbaco2000@gmail.com>
255kb added a commit to mockoon/mockoon.com that referenced this issue Feb 21, 2024
See mockoon/mockoon#849
Add new docs variables pages
Update docs images
255kb added a commit to mockoon/mockoon.com that referenced this issue Feb 27, 2024
See mockoon/mockoon#849
Add new docs variables pages
Update docs images
255kb added a commit to mockoon/mockoon.com that referenced this issue Feb 28, 2024
See mockoon/mockoon#849
Add new docs variables pages
Update docs images
255kb added a commit to mockoon/mockoon.com that referenced this issue Feb 28, 2024
See mockoon/mockoon#849
Add new docs variables pages
Update docs images
255kb added a commit to mockoon/mockoon.com that referenced this issue Feb 29, 2024
Add environment variables documentation see mockoon/mockoon#849
Add new docs variables pages
Update docs images
255kb added a commit to mockoon/mockoon.com that referenced this issue Mar 4, 2024
Add environment variables documentation see mockoon/mockoon#849
Add new docs variables pages
Update docs images
255kb added a commit to mockoon/mockoon.com that referenced this issue Mar 4, 2024
Add environment variables documentation see mockoon/mockoon#849
Add new docs variables pages
Update docs images
@255kb
Copy link
Member

255kb commented Mar 4, 2024

📦 This is now released in v7.0.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature request
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants