简体   繁体   English

ECS Fargate 上的 EFS 安装 - 非 root 用户的读/写权限被拒绝

[英]EFS mount on ECS Fargate - Read/write permissions denied for non root user

I have an ECS Fargate container running a nodejs application with non-root permissions and is also mounted to EFS on /.user_data inside the container.我有一个 ECS Fargate 容器,它运行一个具有非 root 权限的 nodejs 应用程序,并且还挂载到容器内 /.user_data 上的 EFS。

I followed this AWS tutorial.我遵循了这个AWS 教程。 My setup is almost similar.我的设置几乎相似。

Here is the Docker file:这是 Docker 文件:

FROM node:12-buster-slim

RUN apt-get update && \ 
    apt-get install -y build-essential \
    wget \
    python3 \
    make \
    gcc \ 
    libc6-dev  \
    git

# delete old user
RUN userdel -r node 

# Run as a non-root user
RUN addgroup "new_user_group" && \
useradd "new_user" --gid "new_user_group" \
--home-dir "/home/new_user"

RUN git clone https://github.com/test-app.git /home/new_user/app

RUN chown -R new_user:new_user_group /home/new_user

RUN mkdir -p /home/new_user/.user_data 
RUN chown -R new_user:new_user_group /home/new_user/.user_data
RUN chmod -R 755 /home/new_user/

WORKDIR /home/new_user/app

RUN npm install

RUN npm run build

EXPOSE 1880

USER new_user

CMD [ "npm", "start" ]

When the Node app tries to write inside /.user_data I am getting read-write permission denied error.当 Node 应用程序尝试写入 /.user_data 时,我收到读写权限被拒绝的错误。

If I run the container as root the app is able to read/write data.如果我以 root 身份运行容器,则该应用程序能够读取/写入数据。

I tried adding an access point to EFS with UID and permissions but that didn't help as well.我尝试使用 UID 和权限向 EFS 添加访问点,但这也没有帮助。

Please note: The Dockerfile works fine on my local machine.请注意:Dockerfile 在我的本地机器上运行良好。

Update更新

Read this blog post - Developers guide to using Amazon EFS with Amazon ECS and AWS Fargate – Part 2 > POSIX permissions阅读此博文 - 将 Amazon EFS 与 Amazon ECS 和 AWS Fargate 结合使用的开发人员指南 – 第 2 部分 > POSIX 权限

Might be related to the IAM Policy that was assigned to the ECS Task's IAM Role.可能与分配给 ECS 任务的 IAM 角色的 IAM 策略有关。

"...if the AWS policies do not allow the ClientRootAccess action, your user is going to be squashed to a pre-defined UID:GID that is 65534:65534. From this point on, standard POSIX permissions apply: what this user can do is determined by the POSIX file system permissions. For example, a folder owned by any UID:GID other than 65534:65534 that has 666 (rw for owner and rw for everyone) will allow this reserved user to create a file. However, a folder owned by any UID:GID other than 65534:65534 that has 644 (rw for owner and r for everyone) will NOT allow this squashed user to create a file." “...如果 AWS 策略不允许 ClientRootAccess 操作,您的用户将被压缩到预定义的 UID:GID,即 65534:65534。从此时起,标准 POSIX 权限适用:此用户可以做什么do 由 POSIX 文件系统权限决定。例如,任何 UID:GID 拥有的文件夹,除 65534:65534 之外,具有 666(所有者为 rw,所有人为 rw)将允许此保留用户创建文件。但是,由除 65534:65534 以外的任何 UID:GID 拥有的具有 644(所有者为 rw,所有人为 r)的文件夹将不允许此压缩用户创建文件。”


Make sure that your root-dir permissions are set to 777 .确保您的根目录权限设置为777 This way any UID can read/write this dir.这样任何 UID 都可以读/写这个目录。

To be less permissive, set the root-dir to 755 , which is set by default, see the docs .为了不那么宽松,将 root-dir 设置为默认设置的755请参阅文档 This provides read-write-execute to the root user, read-execute to group and read-execute to all other users.这为 root 用户提供read-write-execute read-execute为分组提供read-execute ,为所有其他用户提供读执行。

A user (UID) can't access (read) a sub-directory if there's no read access to its parents (directories).如果没有对其父目录(目录)的读取权限,则用户 (UID) 无法访问(读取)子目录。

You can test it easily with Docker , here's a quick example您可以使用Docker轻松测试它,这是一个简单的示例

Create a Dockerfile -创建一个Dockerfile -

FROM ubuntu:20.04

# Fetch values from ARGs that were declared at the top of this file
ARG APP_NAME
ARG APP_ARTIFACT_DIR
ARG APP_HOME_DIR="/app"
ARG APP_USER_NAME="appuser"
ARG APP_GROUP_ID="appgroup"

# Define workdir
ENV HOME="${APP_HOME_DIR}"
WORKDIR "${HOME}"

RUN apt-get update -y && apt-get install tree

# Define env vars
ENV PATH="${HOME}/.local/bin:${PATH}"

# Run as a non-root user
RUN addgroup "${APP_GROUP_ID}" && \
    useradd "${APP_USER_NAME}" --gid "${APP_GROUP_ID}" --home-dir "${HOME}" && \
    chown -R ${APP_USER_NAME} .

RUN mkdir -p rootdir && \
    mkdir -p rootdir/subdir && \
    touch rootdir/root.file rootdir/subdir/sub.file  && \
    chown -R root:root rootdir && \
    chmod 600 rootdir rootdir/root.file && \
    chmod -R 775 rootdir/subdir

You should play with chmod 600 and chmod -R 775 , try different permissions sets such as 777 and 644 , and see if it makes sense.您应该尝试使用chmod 600chmod -R 775 ,尝试不同的权限集,例如777644 ,看看它是否有意义。

Build an image, run a container, and test the permissions -构建镜像、运行容器并测试权限 -

docker build boyfromnorth .
docker run --rm -it boyfromnorth bash

root@e0f043d9884c:~$ su appuser
$ ls -la
total 12
drwxr-xr-x 1 appuser root 4096 Jan 30 12:23 .
drwxr-xr-x 1 root    root 4096 Jan 30 12:33 ..
drw------- 3 root    root 4096 Jan 30 12:23 rootdir

$ ls rootdir
ls: cannot open directory 'rootdir': Permission denied

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM