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

[WIP] non-root user for NodeJS template #83

Merged
merged 2 commits into from
Oct 26, 2017
Merged

[WIP] non-root user for NodeJS template #83

merged 2 commits into from
Oct 26, 2017

Conversation

austinfrey
Copy link
Contributor

@austinfrey austinfrey commented Sep 6, 2017

Initial draft of non-root user in the NodeJS template.

Description

update the template/node/Dockerfile to use the node user rather than root. function directory structure needed to changed from /root/ to /usr/local/app/ as well

Motivation and Context

Allows users to run function as non-root which may increase security

This relates to #81

How Has This Been Tested?

Manually built test function after changes, deployed and tested through the UI gateway

Linux 16+ on Docker CE 17.06

Will only impact the NodeJS template

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I've read the CONTRIBUTION guide
  • I have signed-off my commits with git commit -s
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@austinfrey
Copy link
Contributor Author

Initial draft to get feedback before moving on to other templates. No need to merge yet.

@@ -12,27 +12,31 @@ WORKDIR /root/
# Turn down the verbosity to default level.
ENV NPM_CONFIG_LOGLEVEL warn

RUN mkdir -p /usr/local/app/function
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 a regular home directory like /home/app/ ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sounds good

Copy link
Member

@alexellis alexellis Sep 12, 2017

Choose a reason for hiding this comment

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

This should be: /home/app/ as per the comment, could you update? I'd like to move forward and merge your change. Thank you for helping lead this @aafrey

WORKDIR /root/
WORKDIR /usr/local/app/

USER node
Copy link
Member

Choose a reason for hiding this comment

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

Great start! Maybe the username could be app and we could use that across the board - like a copy/paste segment for all the templates?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that makes the most sense. I'll work on it this evening

Copy link
Contributor

Choose a reason for hiding this comment

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

Just picking up on the user, and acknowledging that its largely academic, but why dont we use faas or openfaas? Setting this as a sort of principle might be helpful further down the road, when 3rd party templates start to emerge.

Copy link
Member

Choose a reason for hiding this comment

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

I'd just keep it generic like app for now. Always good to think forward though so thanks for your input.

@alexellis alexellis changed the title initial draft of non-root user in NodeJS template [WIP] non-root user for NodeJS template Sep 7, 2017
@alexellis
Copy link
Member

@aafrey - really appreciate your work on this and the "serverless inc" template. 💯

When can we get this fixed-up and merged? @rgee0 @rorpage any other feedback?

Copy link
Member

@alexellis alexellis left a comment

Choose a reason for hiding this comment

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

Changes needed.

@austinfrey
Copy link
Contributor Author

I'll allocate some time tomorrow for this if no one else has any feedback @alex @rgee0 @rorpage

@ericstoekl
Copy link
Contributor

When I try this change, I get the following error:

$ curl http://localhost:8080/function/nodejs-echo -d "stuff"
exit status 1
fs.js:641
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^

Error: EACCES: permission denied, open '/usr/local/app/index.js'
    at Error (native)
    at Object.fs.openSync (fs.js:641:18)
    at Object.fs.readFileSync (fs.js:509:33)
    at Object.Module._extensions..js (module.js:578:20)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)

I believe this can be fixed by the following line just before the USER node line:

RUN chown -R node .

@alexellis
Copy link
Member

@ems5311 thank you for testing.

@austinfrey
Copy link
Contributor Author

@ems5311 appreciate the feedback. I couldn't reproduce so I'm unsure what the issue is. I wouldn't think you'd need to chown since the node user is setup by default when building from that image. I made some changes as asked to use /home/app directory and the app user. I tested and it seems to be working for me, but it worked for me last time too, so would you mind testing again, and let me know if you see that error again? i thought docker was supposed to fix the "works on my machine" issue :)

@ericstoekl
Copy link
Contributor

@aafrey Thanks for the feedback, strangely I am still seeing the issue. I have uploaded the image that faas-cli build -f <yaml> builds for me for the nodejs-echo function, based on this latest dockerfile. https://hub.docker.com/r/ems5311/nodejs-echo-noroot/

I believe if you run faas-cli deploy --image ems5311/nodejs-echo-noroot --name ems5311-node-noroot, it will pull and run the image from the docker hub repo.

I have also made sure that I am building the faas-cli binary with this latest pull request, and the faas-cli version hash matches the latest commit. I am running Ubuntu 16.04 and I might have some unconventional docker settings on my system.

Thanks again,
Eric

@austinfrey
Copy link
Contributor Author

@ems5311 Hi Eric, the image you sent over doesn't work for me either, could you try building with the build.sh script in the template/node directory rather than the cli? just to test if there is a difference

@ericstoekl
Copy link
Contributor

@aafrey good call, I'll try this out tonight.

@ericstoekl
Copy link
Contributor

Yep, works now. Force rebuilding the image with --no-cache did the trick. Sorry for the false alarm, this changelist is good to go. 👏

@austinfrey
Copy link
Contributor Author

@ems5311 appreciate the testing! thanks!

@austinfrey
Copy link
Contributor Author

@alexellis anything else needed here?

@alexellis
Copy link
Member

Yes I posted a comment about using /home/app as the folder for the app.

@austinfrey
Copy link
Contributor Author

austinfrey commented Sep 12, 2017

@alexellis the most recent commit made that change

@alexellis
Copy link
Member

@austinfrey are we good to go with this?


WORKDIR /root/
RUN addgroup -S app && adduser -S -g app app
Copy link
Member

Choose a reason for hiding this comment

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

We can add the group at the top - meaning this gets cached and not affected by copying code in.

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 can make this change @alexellis then we should be all set. should I make the change, squash and resubmit? or can i squash and commit with this same PR?

@alexellis
Copy link
Member

Yes you can squash and re-push with the --force flag. I only raise a new PR if I screw things up really bad and have to push out of a different branch.

@alexellis
Copy link
Member

Ping me for merging.

@ghost
Copy link

ghost commented Sep 21, 2017

Thank you for your contribution. I've just checked and your commit doesn't appear to be signed-off.
That's something we need before your Pull Request can be merged. Please see our contributing guide.

@ghost ghost added no-dco and removed no-dco labels Sep 21, 2017
@austinfrey
Copy link
Contributor Author

@alexellis i had to merge the latest master with my branch to get up to date, i squashed everything down to one commit, but attributing all the previous changes i had to merge in as being part of this commit. is that normal?

@alexellis
Copy link
Member

Struggling to see the changes here. This contains 32 files and now has some merge conflicts. Maybe it would be easier to submit a new PR?

@austinfrey
Copy link
Contributor Author

Agreed, I'll resubmit this evening

…om the `home/app` directory.

Signed-off-by: Austin Frey <aafrey85@gmail.com>
@austinfrey
Copy link
Contributor Author

@alexellis started fresh, hopefully that's the last of it :)

@alexellis
Copy link
Member

Derek add label: status/testing

WORKDIR /root/

# Turn down the verbosity to default level.
ENV NPM_CONFIG_LOGLEVEL warn

RUN mkdir -p /home/app

# Wrapper/boot-strapper
Copy link
Member

Choose a reason for hiding this comment

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

Just wondering if "user app" can go higher?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure. I'll update and test

Signed-off-by: austinfrey <aafrey85@gmail.com>
@austinfrey
Copy link
Contributor Author

@alexellis moved adduser to line two. tested and had no issues

@alexellis alexellis merged commit cc9ce2d into openfaas:master Oct 26, 2017
@alexellis
Copy link
Member

Merged. Please can you retro-fit to the armhf template? @rgee0 perhaps you could help test?

@alexellis
Copy link
Member

faas-cli invoke notroot
Reading from STDIN - hit (Control + D) to stop.
test this
Server returned unexpected status code: 500 - exit status 1
fs.js:641
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^

Error: EACCES: permission denied, open '/home/app/index.js'
    at Error (native)
    at Object.fs.openSync (fs.js:641:18)
    at Object.fs.readFileSync (fs.js:509:33)
    at Object.Module._extensions..js (module.js:578:20)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)

@austinfrey this broke :-/

@austinfrey
Copy link
Contributor Author

austinfrey commented Oct 26, 2017

Interesting. I'll look at this tonight Looks like you already fixed

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

Successfully merging this pull request may close these issues.

4 participants