History

Deprecate auto detection of container user (#7930)

There is no reliable way to handle fallbacks for MinIO deployments, due to various command line options and multiple locations which require access inside container. Parsing command line options is tricky to figure out which is the backend disk etc, we did try to fix this in implementations of check-user.go but it wasn't complete and introduced more bugs. This PR simplifies the entire approach to rather than running Docker container as non-root by default always, it allows users to opt-in. Such that they are aware that that is what they are planning to do. In-fact there are other ways docker containers can be run as regular users, without modifying our internal behavior and adding more complexities.

Harshavardhana committed 7 years ago

Commit 55dd0171 parent 12353ca8 files · +21 −185Browse files

Unified · Split

8 files
  1. .gitignore +0 −1
  2. Dockerfile +1 −3
  3. Dockerfile.dev +2 −3
  4. Dockerfile.release +2 −5
  5. Makefile +0 −1
  6. dockerscripts/check-user.go +0 −135
  7. dockerscripts/docker-entrypoint.sh +9 −28
  8. docs/docker/README.md +7 −9
.gitignore+0−1
@@ -22,5 +22,4 @@ prime/ · 21 unmodified lines
2222 stage/
2323 .sia_temp/
2424 config.json
25−dockerscripts/check-user
2625 dockerscripts/healthcheck
Dockerfile+1−3
@@ -10,8 +10,7 @@ RUN \ · 9 unmodified lines
1010 apk add --no-cache git && \
1111 git clone https://github.com/minio/minio && cd minio && \
1212 go install -v -ldflags "$(go run buildscripts/gen-ldflags.go)" && \
13− cd dockerscripts; go build -tags kqueue -ldflags "-s -w" -o /usr/bin/healthcheck healthcheck.go && \
14− go build -tags kqueue -ldflags "-s -w" -o /usr/bin/check-user check-user.go
13+ cd dockerscripts; go build -tags kqueue -ldflags "-s -w" -o /usr/bin/healthcheck healthcheck.go
1514
1615 FROM alpine:3.9
1716
@@ -23,7 +22,6 @@ EXPOSE 9000 · 5 unmodified lines
2322
2423 COPY --from=0 /go/bin/minio /usr/bin/minio
2524 COPY --from=0 /usr/bin/healthcheck /usr/bin/healthcheck
26−COPY --from=0 /usr/bin/check-user /usr/bin/check-user
2725 COPY dockerscripts/docker-entrypoint.sh /usr/bin/
2826
2927 RUN \
Dockerfile.dev+2−3
@@ -2,7 +2,7 @@ FROM alpine:3.9 · 1 unmodified line
22
33 LABEL maintainer="MinIO Inc <dev@min.io>"
44
5−COPY dockerscripts/docker-entrypoint.sh dockerscripts/healthcheck dockerscripts/check-user /usr/bin/
5+COPY dockerscripts/docker-entrypoint.sh dockerscripts/healthcheck /usr/bin/
66 COPY minio /usr/bin/
77
88 ENV MINIO_UPDATE off
@@ -14,8 +14,7 @@ RUN \ · 5 unmodified lines
1414 echo 'hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4' >> /etc/nsswitch.conf && \
1515 chmod +x /usr/bin/minio && \
1616 chmod +x /usr/bin/docker-entrypoint.sh && \
17− chmod +x /usr/bin/healthcheck && \
18− chmod +x /usr/bin/check-user
17+ chmod +x /usr/bin/healthcheck
1918
2019 EXPOSE 9000
2120
Dockerfile.release+2−5
@@ -7,15 +7,13 @@ ENV GO111MODULE on · 6 unmodified lines
77 RUN \
88 apk add --no-cache git && \
99 git clone https://github.com/minio/minio && cd minio/dockerscripts && \
10− go build -tags kqueue -ldflags "-s -w" -o /usr/bin/healthcheck healthcheck.go && \
11− go build -tags kqueue -ldflags "-s -w" -o /usr/bin/check-user check-user.go
10+ go build -tags kqueue -ldflags "-s -w" -o /usr/bin/healthcheck healthcheck.go
1211
1312 FROM alpine:3.9
1413
1514 LABEL maintainer="MinIO Inc <dev@min.io>"
1615
1716 COPY --from=0 /usr/bin/healthcheck /usr/bin/healthcheck
18−COPY --from=0 /usr/bin/check-user /usr/bin/check-user
1917 COPY dockerscripts/docker-entrypoint.sh /usr/bin/
2018
2119 ENV MINIO_UPDATE off
@@ -28,8 +26,7 @@ RUN \ · 6 unmodified lines
2826 curl https://dl.min.io/server/minio/release/linux-amd64/minio > /usr/bin/minio && \
2927 chmod +x /usr/bin/minio && \
3028 chmod +x /usr/bin/docker-entrypoint.sh && \
31− chmod +x /usr/bin/healthcheck && \
32− chmod +x /usr/bin/check-user
29+ chmod +x /usr/bin/healthcheck
3330
3431 EXPOSE 9000
3532
Makefile+0−1
@@ -73,7 +73,6 @@ build: checks · 72 unmodified lines
7373 @echo "Building minio binary to './minio'"
7474 @GO111MODULE=on GOFLAGS="" CGO_ENABLED=0 go build -tags kqueue --ldflags $(BUILD_LDFLAGS) -o $(PWD)/minio 1>/dev/null
7575 @GO111MODULE=on GOFLAGS="" CGO_ENABLED=0 go build -tags kqueue --ldflags $(BUILD_LDFLAGS) -o $(PWD)/dockerscripts/healthcheck $(PWD)/dockerscripts/healthcheck.go 1>/dev/null
76− @GO111MODULE=on GOFLAGS="" CGO_ENABLED=0 go build -tags kqueue --ldflags $(BUILD_LDFLAGS) -o $(PWD)/dockerscripts/check-user $(PWD)/dockerscripts/check-user.go 1>/dev/null
7776
7877 docker: build
7978 @docker build -t $(TAG) . -f Dockerfile.dev
dockerscripts/check-user.go+0−135
@@ -1,135 +0,0 @@
1−// +build ignore
2−
3−/*
4− * MinIO Cloud Storage, (C) 2019 MinIO, Inc.
5− *
6− * Licensed under the Apache License, Version 2.0 (the "License");
7− * you may not use this file except in compliance with the License.
8− * You may obtain a copy of the License at
9− *
10− * http://www.apache.org/licenses/LICENSE-2.0
11− *
12− * Unless required by applicable law or agreed to in writing, software
13− * distributed under the License is distributed on an "AS IS" BASIS,
14− * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15− * See the License for the specific language governing permissions and
16− * limitations under the License.
17− */
18−
19−package main
20−
21−import (
22− "fmt"
23− "log"
24− "os"
25− "os/user"
26− "syscall"
27−
28− "github.com/minio/cli"
29− minio "github.com/minio/minio/cmd"
30−)
31−
32−var defaultUserGroup string
33−
34−func init() {
35− username := os.Getenv("MINIO_USERNAME")
36− groupname := os.Getenv("MINIO_GROUPNAME")
37− defaultUserGroup = username + ":" + groupname
38−}
39−
40−func getUserGroup(path string) (string, error) {
41− fi, err := os.Stat(minio.PathJoin(path, ".minio.sys"))
42− if err != nil {
43− // Fresh directory we should default to what was requested by user.
44− if os.IsNotExist(err) {
45− fi, err = os.Stat(path)
46− if err != nil {
47− return "", err
48− }
49− } else {
50− return "", err
51− }
52− }
53− stat, ok := fi.Sys().(*syscall.Stat_t)
54− if !ok {
55− // Unable to figure out uid/gid, default to defaultUserGroup
56− return defaultUserGroup, nil
57− }
58− u, err := user.LookupId(fmt.Sprintf("%d", stat.Uid))
59− if err != nil {
60− return fmt.Sprintf("%d:%d", stat.Uid, stat.Gid), nil
61− }
62− g, err := user.LookupGroupId(fmt.Sprintf("%d", stat.Gid))
63− if err != nil {
64− return fmt.Sprintf("%d:%d", stat.Uid, stat.Gid), nil
65− }
66− return fmt.Sprintf("%s:%s", u.Username, g.Name), nil
67−}
68−
69−func main() {
70− app := cli.NewApp()
71− app.Flags = append(minio.ServerFlags, minio.GlobalFlags...)
72− app.Action = func(ctx *cli.Context) {
73− // Fetch address option
74− serverAddr := ctx.GlobalString("address")
75− if serverAddr == "" || serverAddr == ":9000" {
76− serverAddr = ctx.String("address")
77− }
78− if ctx.Args().First() == "help" {
79− cli.ShowCommandHelpAndExit(ctx, "check-user", 1)
80− }
81− if ctx.Args().First() != "minio" {
82− cli.ShowCommandHelpAndExit(ctx, "check-user", 1)
83− }
84− args := cli.Args(ctx.Args().Tail())
85− if !args.Present() {
86− cli.ShowCommandHelpAndExit(ctx, "check-user", 1)
87− }
88− var ug string
89− var err error
90− switch args.First() {
91− case "gateway":
92− args = cli.Args(args.Tail())
93− if args.First() != "nas" {
94− fmt.Println(defaultUserGroup)
95− return
96− }
97− args = cli.Args(args.Tail())
98− if args.First() == "" {
99− fmt.Println("")
100− return
101− }
102− ug, err = getUserGroup(args.First())
103− if err != nil {
104− log.Fatalln(err)
105− }
106− case "server":
107− var setArgs [][]string
108− setArgs, err = minio.GetAllSets(args.Tail()...)
109− if err != nil {
110− log.Fatalln(err)
111− }
112− var endpoints minio.EndpointList
113− _, endpoints, _, err = minio.CreateEndpoints(serverAddr, setArgs...)
114− if err != nil {
115− log.Fatalln(err)
116− }
117− for _, endpoint := range endpoints {
118− if !endpoint.IsLocal {
119− continue
120− }
121− ug, err = getUserGroup(endpoint.Path)
122− if err != nil {
123− log.Fatalln(err)
124− }
125− break
126− }
127− default:
128− cli.ShowCommandHelpAndExit(ctx, "check-user", 1)
129− }
130− fmt.Println(ug)
131− }
132− if err := app.Run(os.Args); err != nil {
133− log.Fatalln(err)
134− }
135−}
dockerscripts/docker-entrypoint.sh+9−28
@@ -15,9 +15,6 @@ · 14 unmodified lines
1515 # limitations under the License.
1616 #
1717
18−export MINIO_USERNAME=${MINIO_USERNAME:-"minio"}
19−export MINIO_GROUPNAME=${MINIO_GROUPNAME:-"minio"}
20−
2118 # If command starts with an option, prepend minio.
2219 if [ "${1}" != "minio" ]; then
2320 if [ -n "${1}" ]; then
@@ -42,37 +39,21 @@ docker_secrets_env() { · 18 unmodified lines
4239 fi
4340 }
4441
45−## Create UID/GID based on available environment variables.
46−docker_set_uid_gid() {
47− addgroup -S "$MINIO_GROUPNAME" >/dev/null 2>&1 && \
48− adduser -S -G "$MINIO_GROUPNAME" "$MINIO_USERNAME" >/dev/null 2>&1
49−}
50−
51−# su-exec to requested user, if user cannot be requested
52−# existing user is used automatically.
42+# su-exec to requested user, if service cannot run exec will fail.
5343 docker_switch_user() {
54− owner=$(check-user "$@")
55− if [ "${owner}" != "${MINIO_USERNAME}:${MINIO_GROUPNAME}" ]; then
56− ## Print the message only if we are not using non-default username:groupname.
57− if [ "${MINIO_USERNAME}:${MINIO_GROUPNAME}" != "minio:minio" ]; then
58− echo "Requested username/group ${MINIO_USERNAME}:${MINIO_GROUPNAME} cannot be used"
59− echo "Found existing data with user ${owner}, we will continue and use ${owner} instead."
60− return
61− fi
62− fi
63− # check if su-exec is allowed, if yes proceed proceed.
64− if su-exec "${owner}" "/bin/ls" >/dev/null 2>&1; then
65− exec su-exec "${owner}" "$@"
44+ if [ -z "${MINIO_USERNAME}" ] || [ -z "${MINIO_GROUPNAME}" ]; then
45+ addgroup -S "$MINIO_GROUPNAME" >/dev/null 2>&1 && \
46+ adduser -S -G "$MINIO_GROUPNAME" "$MINIO_USERNAME" >/dev/null 2>&1
47+
48+ exec su-exec "${MINIO_USERNAME}:${MINIO_GROUPNAME}" "$@"
49+ else
50+ # fallback
51+ exec "$@"
6652 fi
67− # fallback
68− exec "$@"
6953 }
7054
7155 ## Set access env from secrets if necessary.
7256 docker_secrets_env
7357
74−## User Input UID and GID
75−docker_set_uid_gid
76−
7758 ## Switch to user if applicable.
7859 docker_switch_user "$@"
docs/docker/README.md+7−9
@@ -58,20 +58,19 @@ docker run -p 9000:9000 --name minio1 \ · 57 unmodified lines
5858 minio/minio server /data
5959 ```
6060
61−### Run MinIO Docker as non root user
62−MinIO server runs as non-root within the container by default. However, this is applicable only if you're deploying new MinIO instance (not upgrading from older releases). Deployments upgrading from older MinIO deployments, will continue to run as the user previously used if any.
61+### Run MinIO Docker as regular user
62+MinIO server doesn't run as a regular user by default in docker containers. To run MinIO container as regular user use environment variables `MINIO_USERNAME` and `MINIO_GROUPNAME`.
6363
64−By default `minio` is username and groupname. Use environment variables `MINIO_USERNAME` and `MINIO_GROUPNAME` to override these default values.
64+> NOTE: If you are upgrading from existing deployments, you need to make sure this user has write access to previous persistent volumes. MinIO will not migrate the content automatically.
6565
6666 #### GNU/Linux and macOS
6767 ```sh
6868 docker run -p 9000:9000 --name minio1 \
6969 -e "MINIO_ACCESS_KEY=AKIAIOSFODNN7EXAMPLE" \
7070 -e "MINIO_SECRET_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" \
71− -e "MINIO_USERNAME=custom" \
72− -e "MINIO_GROUPNAME=custom" \
71+ -e "MINIO_USERNAME=minio-user" \
72+ -e "MINIO_GROUPNAME=minio-user" \
7373 -v /mnt/data:/data \
74− -v /mnt/config:/root/.minio \
7574 minio/minio server /data
7675 ```
7776
@@ -80,10 +79,9 @@ docker run -p 9000:9000 --name minio1 \ · 2 unmodified lines
8079 docker run -p 9000:9000 --name minio1 \
8180 -e "MINIO_ACCESS_KEY=AKIAIOSFODNN7EXAMPLE" \
8281 -e "MINIO_SECRET_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" \
83− -e "MINIO_USERNAME=custom" \
84− -e "MINIO_GROUPNAME=custom" \
82+ -e "MINIO_USERNAME=minio-user" \
83+ -e "MINIO_GROUPNAME=minio-user" \
8584 -v D:\data:/data \
86− -v D:\minio\config:/root/.minio \
8785 minio/minio server /data
8886 ```
8987

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t