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

Check for npmrc in home directory for linux root users #4047

Merged
merged 8 commits into from
Jul 31, 2017

Conversation

kaylie-alexa
Copy link
Member

@kaylie-alexa kaylie-alexa commented Jul 29, 2017

Summary
Fix for #3920.

In this commit, userHome for linux users running as root was changed to /usr/local/share, mainly to allow for other users to run globally added bins. However this introduced a bug where npmrc wasn't being looked in the root directory after checking /user/local/share. So this change pushes another location to check in case userHome is different from native home directory returned by os.homedir()

Test plan
I've added a test to generally test the getPossibleConfigLocations method, but I haven't been able to properly mock out running it as a root user on linux. Suggestions welcome!

@kaylie-alexa kaylie-alexa changed the title Check for npmrc Check for npmrc in home directory for linux root users Jul 29, 2017
@BYK BYK self-assigned this Jul 29, 2017
Copy link
Member

@BYK BYK left a comment

Choose a reason for hiding this comment

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

LGTM. Just one question about Linux whitelist vs. Windows blacklist

process.platform === 'linux' && ROOT_USER ? path.resolve('/usr/local/share') : require('os').homedir();
export const home = require('os').homedir();

const userHomeDir = process.platform === 'linux' && ROOT_USER ? path.resolve('/usr/local/share') : home;
Copy link
Member

Choose a reason for hiding this comment

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

Why limit to linux instead of blacklisting Windows only?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, I'm not sure I fully understand the question. Would you mind explaining a bit? Windows is already filtered out in the ROOT_USER here

if (process.platform !== 'win32' && process.getuid) {

Copy link
Member

Choose a reason for hiding this comment

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

What about other operating systems like BSD? I wonder if we should use this root logic for those, too.

Copy link
Member Author

Choose a reason for hiding this comment

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

It seems safe to add 'freebsd' to the list, are there any other platforms we should add from here? https://nodejs.org/api/process.html#process_process_platform

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah actually @BYK I see what you're saying. Yeah I don't think the platform check is even necessary given that line of code in root-user.js

@@ -14,7 +14,7 @@ import {addSuffix} from '../util/misc';
import {getPosixPath, resolveWithHome} from '../util/path';

const normalizeUrl = require('normalize-url');
const userHome = require('../util/user-home-dir').default;
const {default: userHome, home} = require('../util/user-home-dir');
Copy link
Member

Choose a reason for hiding this comment

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

While you're here, could you please change this to an import statement? I've been meaning to revisit one of my old pull requests where I updated all require statements to use import, but just haven't had time.

Copy link
Member Author

Choose a reason for hiding this comment

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

sure thing!

Copy link
Member

@BYK BYK left a comment

Choose a reason for hiding this comment

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

Pushing back for the test describe comment. I'll push a fix myself and merge it for expediency.

Thanks a lot for the fix!

const ini = require('ini');
import normalizeUrl from 'normalize-url';
import {default as userHome, home} from '../util/user-home-dir';
import path from 'path';
Copy link
Member

Choose a reason for hiding this comment

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

Nice! I think the convention is to list stdlib imports at the top, right?

const mockReporter = jest.fn();
const mockLog = [];
const mockReporter = {
lang: (...args) => mockLog.push(...args),
Copy link
Member

Choose a reason for hiding this comment

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

I think we can also use the BufferReporter for tests?

@@ -292,3 +297,16 @@ describe('getScope functional test', () => {
});
});
});

describe('getPossibleConfigLocations', () => {
describe('searches recursively to home directory', async () => {
Copy link
Member

Choose a reason for hiding this comment

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

This should be a test or it call not describe.

// When home directory for global install is different from where $HOME/npmrc is stored,
// E.g. /usr/local/share vs /root on linux machines, check the additional location
if (home !== userHome) {
possibles.push([true, path.join(home, localfile)]);
Copy link
Member

Choose a reason for hiding this comment

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

I think we should change this data structure to be a normal object instead of an array sometime in the future: {isHomeDir: true, path: path.join(home, localfile)}

@BYK BYK merged commit 0ef3bf1 into yarnpkg:master Jul 31, 2017
@kaylie-alexa
Copy link
Member Author

kaylie-alexa commented Jul 31, 2017

@BYK thanks for being awesome and making those changes!

@felixfbecker
Copy link

Any ETA for a release containing this fix?

@Daniel15
Copy link
Member

Daniel15 commented Aug 26, 2017

@felixfbecker You can use a nightly build if you like. https://nightly.yarnpkg.com/

The Yarn team are working on 1.0 at the moment.

@felixfbecker
Copy link

Tried to, but unfortunately I need to add it to an Alpine Docker image and running the install script just results in

Step 8/16 : RUN curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --nightly
 ---> Running in dc6404f17920
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  6746    0  6746    0     0  33679      0 --:--:-- --:--:-- --:--:-- 54845
Installing Yarn!
> Downloading tarball...

[1/2]: https://nightly.yarnpkg.com/latest.tar.gz --> /tmp/yarn.tar.gz.XXXXLPmEmH
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100  821k  100  821k    0     0  1764k      0 --:--:-- --:--:-- --:--:-- 1764k

[2/2]: https://nightly.yarnpkg.com/latest.tar.gz.asc --> /tmp/yarn.tar.gz.XXXXLPmEmH.asc
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   900  100   900    0     0   7945      0 --:--:-- --:--:-- --:--:--  7945
> WARNING: GPG is not installed, integrity can not be verified!
> Extracting to ~/.yarn...
tar: unrecognized option: strip
BusyBox v1.24.2 (2016-03-29 19:24:15 GMT) multi-call binary.

Usage: tar -[cxtZzJjahmvO] [-X FILE] [-T FILE] [-f TARFILE] [-C DIR] [FILE]...

Create, extract, or list files from a tar file

Operation:
        c       Create
        x       Extract
        t       List
        f       Name of TARFILE ('-' for stdin/out)
        C       Change to DIR before operation
        v       Verbose
        Z       (De)compress using compress
        z       (De)compress using gzip
        J       (De)compress using xz
        j       (De)compress using bzip2
        a       (De)compress using lzma
        O       Extract to stdout
        h       Follow symlinks
        m       Don't restore mtime
        exclude File to exclude
        X       File with names to exclude
        T       File with names to include
The command '/bin/sh -c curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --nightly' returned a non-zero code: 1

Really don't want to change our whole image just for a hotfix. I guess the alternative is switching to npm 5

@BYK
Copy link
Member

BYK commented Aug 29, 2017

@felixfbecker that's unfortunate :( I'll file an issue to see why the script is not working on Alpine. Guessing some mismatch between GNU tar and others.

In the meantime, you can use the single-file bundle from https://nightly.yarnpkg.com/yarn-1.0.0-20170825.2124.js (just curl this wherever and use it directly run it.

@Daniel15
Copy link
Member

In the meantime, you can use the single-file bundle from nightly.yarnpkg.com/yarn-1.0.0-20170825.2124.js

https://nightly.yarnpkg.com/latest.js will also give you the latest version.

You could also manually grab the tarball from https://nightly.yarnpkg.com/latest.tar.gz, check the GPG signature, and extract it. That's what the install script does :)

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

Successfully merging this pull request may close these issues.

4 participants