Skip to content

Commit 9a7d44f

Browse files
parrobearthurbarr
authored andcommitted
Add container suplimentary groups support
1 parent f73347a commit 9a7d44f

File tree

3 files changed

+108
-20
lines changed

3 files changed

+108
-20
lines changed

cmd/runmqserver/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ func doMain() error {
6464
logTermination(err)
6565
return err
6666
}
67+
68+
err = verifyCurrentUser()
69+
if err != nil {
70+
logTermination(err)
71+
return err
72+
}
73+
6774
err = createVolume("/mnt/mqm")
6875
if err != nil {
6976
logTermination(err)

cmd/runmqserver/mqconfig.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package main
1818
import (
1919
"fmt"
2020
"io/ioutil"
21-
"os/user"
2221
"runtime"
2322
"strings"
2423

@@ -52,25 +51,6 @@ func logBaseImage() error {
5251
return nil
5352
}
5453

55-
func logUser() {
56-
u, err := user.Current()
57-
if err == nil {
58-
g, err := u.GroupIds()
59-
if err != nil {
60-
log.Printf("Running as user ID %v (%v) with primary group %v", u.Uid, u.Name, u.Gid)
61-
} else {
62-
// Look for the primary group in the list of group IDs
63-
for i, v := range g {
64-
if v == u.Gid {
65-
// Remove the element from the slice
66-
g = append(g[:i], g[i+1:]...)
67-
}
68-
}
69-
log.Printf("Running as user ID %v (%v) with primary group %v, and supplemental groups %v", u.Uid, u.Name, u.Gid, strings.Join(g, ","))
70-
}
71-
}
72-
}
73-
7454
// logCapabilities logs the Linux capabilities (e.g. setuid, setgid). See https://docs.docker.com/engine/reference/run/#runtime-privilege-and-linux-capabilities
7555
func logCapabilities() error {
7656
caps, err := container.Capabilities()

cmd/runmqserver/user.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
© Copyright IBM Corporation 2018
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package main
17+
18+
import (
19+
"fmt"
20+
"os/user"
21+
"strings"
22+
23+
"github.com/ibm-messaging/mq-container/internal/command"
24+
)
25+
26+
const groupName string = "suplgroup"
27+
28+
func verifyCurrentUser() error {
29+
log.Debug("Verifying current user information")
30+
curUser, err := user.Current()
31+
if err != nil {
32+
return err
33+
}
34+
log.Debugf("Detected current user as: %v+", curUser)
35+
if curUser.Username == "mqm" {
36+
// Not supported yet
37+
return fmt.Errorf("Container is running as mqm user which is not supported. Please run this container as root")
38+
} else if curUser.Username == "root" {
39+
// We're running as root so need to check for suplimentary groups.
40+
// We can't use the golang User.GroupIDs as it doesn't seem to detect container supplimentary groups..
41+
groups, err := getCurrentGroups()
42+
for _, e := range groups {
43+
_, _, testGroup := command.Run("getent", "group", e)
44+
if testGroup != nil {
45+
log.Printf("Group %s does not exist on the system... Adding to system and MQM user", e)
46+
_, _, err = command.Run("groupadd", "-g", e, groupName)
47+
if err != nil {
48+
log.Errorf("Failed to create group %s as %s", e, groupName)
49+
return err
50+
}
51+
_, _, err = command.Run("usermod", "-aG", groupName, "mqm")
52+
if err != nil {
53+
log.Errorf("Failed to add group %s(%s) to the mqm user.", groupName, e)
54+
return err
55+
}
56+
}
57+
}
58+
} else {
59+
// We're running as an unknown user...
60+
return fmt.Errorf("Container is running as %s user which is not supported. Please run this container as root", curUser.Username)
61+
}
62+
63+
return nil
64+
}
65+
66+
func logUser() {
67+
u, err := user.Current()
68+
if err == nil {
69+
g, err := getCurrentGroups()
70+
if err != nil && len(g) == 0 {
71+
log.Printf("Running as user ID %v (%v) with primary group %v", u.Uid, u.Name, u.Gid)
72+
} else {
73+
// Look for the primary group in the list of group IDs
74+
for i, v := range g {
75+
if v == u.Gid {
76+
// Remove the element from the slice
77+
g = append(g[:i], g[i+1:]...)
78+
}
79+
}
80+
log.Printf("Running as user ID %v (%v) with primary group %v, and supplemental groups %v", u.Uid, u.Name, u.Gid, strings.Join(g, ","))
81+
}
82+
}
83+
}
84+
85+
func getCurrentGroups() ([]string, error) {
86+
var nilArray []string
87+
out, _, err := command.Run("id", "--groups")
88+
if err != nil {
89+
log.Debug("Unable to get current user groups")
90+
return nilArray, err
91+
}
92+
93+
out = strings.TrimSpace(out)
94+
if out == "" {
95+
// we don't have any groups?
96+
return nilArray, fmt.Errorf("Unable to determine groups for current user")
97+
}
98+
99+
groups := strings.Split(out, " ")
100+
return groups, nil
101+
}

0 commit comments

Comments
 (0)