Skip to content

Commit

Permalink
[env] exit if starting as root
Browse files Browse the repository at this point in the history
  • Loading branch information
jbudz committed Aug 1, 2018
1 parent 017e3bb commit f6e9090
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/setup_node_env/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
* under the License.
*/

require('./root');
require('./node_version_validator');
require('./babel_register');
25 changes: 25 additions & 0 deletions src/setup_node_env/root/force.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

module.exports = function (argv) {
var forceIndex = argv.indexOf('--force-root');
var force = forceIndex >= 0;
if (force) argv.splice(forceIndex, 1);
return force;
};
39 changes: 39 additions & 0 deletions src/setup_node_env/root/force.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

var forceRoot = require('./force');

describe('forceRoot', function () {

it('with flag', function () {
expect(forceRoot(['--force-root'])).toBeTruthy();
});

it('without flag', function () {
expect(forceRoot(['--foo'])).toBeFalsy();

});

test('remove argument', function () {
var args = ['--force-root', 'foo'];
forceRoot(args);
expect(args.includes('--force-root')).toBeFalsy();
});

});
26 changes: 26 additions & 0 deletions src/setup_node_env/root/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

var force = require('./force')(process.argv);
var isRoot = require('./is_root')(process.getuid());

if(isRoot && !force) {
console.error('Kibana should not be run as root. Use --force-root to continue.');
process.exit(1);
}
22 changes: 22 additions & 0 deletions src/setup_node_env/root/is_root.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

module.exports = function (uid) {
return uid === 0;
};
32 changes: 32 additions & 0 deletions src/setup_node_env/root/is_root.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

var isRoot = require('./is_root');


describe('isRoot', function () {

test('0 is root', function () {
expect(isRoot(0)).toBeTruthy();
});

test('not 0 is not root', function () {
expect(isRoot(5)).toBeFalsy();
});
});

0 comments on commit f6e9090

Please sign in to comment.