|
| 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