Skip to content

feat: use SNYK_TMP_PATH env var for temp dir path #267

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

Merged
merged 4 commits into from
May 30, 2025

Conversation

thomasschafer
Copy link
Contributor

@thomasschafer thomasschafer commented May 21, 2025

This plugin requires use of a directory for temporarily storing assets, which by default is the temp dir specified by the OS (e.g. /tmp on macOS). We have seen issues with users running the CLI in environments where they do not have root access and so cannot create directories at this default OS temp dir. This PR overcomes this by making use of the SNYK_TMP_PATH env var: if present then the directory specified by the env var will be used as the temp dir.

To test, build the CLI pointing to this branch of the plugin, and then open up a Python project and run that locally built CLI against it:

SNYK_TMP_PATH=<some/directory> <path/to/CLI/repo>/bin/snyk test

If you provide a directory that exists and you have access to, there will be no errors. If you e.g. use SNYK_TMP_PATH=/usr/bin (which requires sudo to write to), the command will fail:

Failed to write temporary files:
Error: EPERM: operation not permitted, mkdir '/usr/bin/tmp-15640-js1sj0BYRdv3'
Try running again with SNYK_TMP_PATH=<some directory>, where <some directory> is a valid directory that you have permissions to write to.

@CLAassistant
Copy link

CLAassistant commented May 21, 2025

CLA assistant check
All committers have signed the CLA.

@thomasschafer thomasschafer force-pushed the feat/read-env-var-to-allow-overriding-of-tmp-dir branch 6 times, most recently from 2a852e0 to 8a8b01b Compare May 21, 2025 16:37
@thomasschafer thomasschafer force-pushed the feat/read-env-var-to-allow-overriding-of-tmp-dir branch from 8a8b01b to 46cabae Compare May 22, 2025 08:21
@thomasschafer thomasschafer marked this pull request as ready for review May 22, 2025 11:06
@thomasschafer thomasschafer requested a review from a team as a code owner May 22, 2025 11:06
const tempDirObj = tmp.dirSync({
unsafeCleanup: true,
});
const tmp_path = process.env.SNYK_TMP_PATH;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps this should be an official CLI parameter? --tmp-path

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a look at implementing this - out of interest @danlucian, was there a reason for us to use an env var in your PR here rather than the plugin accepting a CLI param?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gitphill I've just made the change - it will also require this change in the CLI

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use the snyk test -- --<plugin specific params here method to pass in the tmp dir param to the plugin, without involving the CLI team in a new param.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, @thomasschafer, the CLI provides a centrally defined set of local filesystem paths that should be used internally and by extensions to cache and temporarily store data. The paths are part of the user docs as well and can be configured by the user.

The tmp dir is a directory that is used during the CLI execution to store files in the filesystem. The tmp dir will be cleaned up at the end of the CLI run and its relative to the cache folder.

The tmp dir is based on the following pattern:
<OS_CACHE_PATH>/snyk/snyk-cli/<CLI_VERSION>/tmp/pid<PROCESS_ID>

The easiest propagation mechanism was through an environment variable, that helped us avoid changing the signature of the functions to possibly accomodate a new argument/variable.

Hope this helps!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @danlucian - so by the sound of it, there's no reason we can't go with the recommendation from @orsagie to use a plugin-specific CLI arg? Is it just a matter of ease of implementation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per @PeterSchafer's recommendation I've reverted the change to use CLI flags back to using the env var instead

@thomasschafer thomasschafer changed the title feat: use SNYK_TEMP_PATH env var for temp dir path feat: use --tmp-dir flag for temp dir path May 23, 2025
@thomasschafer thomasschafer force-pushed the feat/read-env-var-to-allow-overriding-of-tmp-dir branch from 0ec0b1a to f4dadc0 Compare May 23, 2025 09:48
@thomasschafer thomasschafer force-pushed the feat/read-env-var-to-allow-overriding-of-tmp-dir branch from f4dadc0 to aa090ee Compare May 27, 2025 10:58
@thomasschafer thomasschafer changed the title feat: use --tmp-dir flag for temp dir path feat: use SNYK_TMP_PATH env var for temp dir path May 27, 2025
@thomasschafer thomasschafer force-pushed the feat/read-env-var-to-allow-overriding-of-tmp-dir branch from 48f7e1a to 587c374 Compare May 28, 2025 10:42
Copy link
Member

@JamesPatrickGill JamesPatrickGill left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good ...once I read the filenames properly lol

@JamesPatrickGill
Copy link
Member

Also double checked arbitrary string from env not being put somewhere it shouldn't - even though this all happens on the client machine so risk was low anyway

@thomasschafer thomasschafer merged commit 10467ad into main May 30, 2025
25 checks passed
@thomasschafer thomasschafer deleted the feat/read-env-var-to-allow-overriding-of-tmp-dir branch May 30, 2025 08:58
@snyksec
Copy link

snyksec commented May 30, 2025

🎉 This PR is included in version 2.7.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants