| 1 | // Copyright 2019 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package mainscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/ |
| 16 | |
| 17 | import ( |
| 18 | "bufioscip-go gomod github.com/golang/go/src go1.13 bufio/" |
| 19 | "bytesscip-go gomod github.com/golang/go/src go1.13 bytes/" |
| 20 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.13 `encoding/json`/" |
| 21 | "fmtscip-go gomod github.com/golang/go/src go1.13 fmt/" |
| 22 | "io/ioutilscip-go gomod github.com/golang/go/src go1.13 `io/ioutil`/" |
| 23 | "osscip-go gomod github.com/golang/go/src go1.13 os/" |
| 24 | "os/execscip-go gomod github.com/golang/go/src go1.13 `os/exec`/" |
| 25 | "pathscip-go gomod github.com/golang/go/src go1.13 path/" |
| 26 | "sortscip-go gomod github.com/golang/go/src go1.13 sort/" |
| 27 | "strconvscip-go gomod github.com/golang/go/src go1.13 strconv/" |
| 28 | "stringsscip-go gomod github.com/golang/go/src go1.13 strings/" |
| 29 | "timescip-go gomod github.com/golang/go/src go1.13 time/" |
| 30 | |
| 31 | "github.com/GoogleCloudPlatform/guest-logging-go/loggerscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/" |
| 32 | ) |
| 33 | |
| 34 | var ( |
| 35 | // sshKeys is a cache of what we have added to each managed users' authorized |
| 36 | // keys file. Avoids necessity of re-reading all files on every change. |
| 37 | sshKeysscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/sshKeys. map[string][]string |
| 38 | googleUsersFilescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/googleUsersFile. = "/var/lib/google/google_users" |
| 39 | ) |
| 40 | |
| 41 | // compareStringSlice returns true if two string slices are equal, false |
| 42 | // otherwise. Does not modify the slices. |
| 43 | func compareStringSlicescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/compareStringSlice().(first, second []string) bool { |
| 44 | if len(first) != len(second) { |
| 45 | return false |
| 46 | } |
| 47 | for _, list := range [][]string{first, second} { |
| 48 | sortfunc := func(i, j int) bool { return list[i] < list[j] } |
| 49 | list = append([]string{}, list...) |
| 50 | sortscip-go gomod github.com/golang/go/src go1.13 sort/.Slicescip-go gomod github.com/golang/go/src go1.13 sort/Slice().(list, sortfunc) |
| 51 | } |
| 52 | for idx := range first { |
| 53 | if first[idx] != second[idx] { |
| 54 | return false |
| 55 | } |
| 56 | } |
| 57 | return true |
| 58 | } |
| 59 | |
| 60 | type accountsMgrscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/accountsMgr# struct{} |
| 61 | |
| 62 | func (a *accountsMgrscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/accountsMgr#) diffscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/accountsMgr#diff().() bool { |
| 63 | // If any keys have changed. |
| 64 | if !compareStringSlicescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/compareStringSlice().(newMetadatascip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/newMetadata..Instancescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/metadata#Instance..Attributesscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/instance#Attributes..SSHKeysscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/attributes#SSHKeys., oldMetadatascip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/oldMetadata..Instancescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/metadata#Instance..Attributesscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/instance#Attributes..SSHKeysscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/attributes#SSHKeys.) { |
| 65 | return true |
| 66 | } |
| 67 | if !compareStringSlicescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/compareStringSlice().(newMetadatascip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/newMetadata..Projectscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/metadata#Project..Attributesscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/project#Attributes..SSHKeysscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/attributes#SSHKeys., oldMetadatascip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/oldMetadata..Projectscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/metadata#Project..Attributesscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/project#Attributes..SSHKeysscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/attributes#SSHKeys.) { |
| 68 | return true |
| 69 | } |
| 70 | if newMetadatascip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/newMetadata..Instancescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/metadata#Instance..Attributesscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/instance#Attributes..BlockProjectKeysscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/attributes#BlockProjectKeys. != oldMetadatascip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/oldMetadata..Instancescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/metadata#Instance..Attributesscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/instance#Attributes..BlockProjectKeysscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/attributes#BlockProjectKeys. { |
| 71 | return true |
| 72 | } |
| 73 | |
| 74 | // If any on-disk keys have expired. |
| 75 | for _, keys := range sshKeysscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/sshKeys. { |
| 76 | if len(keys) != len(removeExpiredKeysscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/removeExpiredKeys().(keys)) { |
| 77 | return true |
| 78 | } |
| 79 | } |
| 80 | // If we've just disabled OS Login. |
| 81 | oldOslogin, _, _ := getOSLoginEnabledscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/getOSLoginEnabled().(oldMetadatascip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/oldMetadata.) |
| 82 | newOslogin, _, _ := getOSLoginEnabledscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/getOSLoginEnabled().(newMetadatascip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/newMetadata.) |
| 83 | if oldOslogin && !newOslogin { |
| 84 | return true |
| 85 | } |
| 86 | |
| 87 | return false |
| 88 | } |
| 89 | |
| 90 | func (a *accountsMgrscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/accountsMgr#) timeoutscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/accountsMgr#timeout().() bool { |
| 91 | return false |
| 92 | } |
| 93 | |
| 94 | func (a *accountsMgrscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/accountsMgr#) disabledscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/accountsMgr#disabled().(os string) bool { |
| 95 | oslogin, _, _ := getOSLoginEnabledscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/getOSLoginEnabled().(newMetadatascip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/newMetadata.) |
| 96 | return false || |
| 97 | os == "windows" || oslogin || |
| 98 | !configscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/config..Sectionscip-go gomod github.com/go-ini/ini v1.51.1 `github.com/go-ini/ini`/File#Section().("Daemons").Keyscip-go gomod github.com/go-ini/ini v1.51.1 `github.com/go-ini/ini`/Section#Key().("accounts_daemon").MustBoolscip-go gomod github.com/go-ini/ini v1.51.1 `github.com/go-ini/ini`/Key#MustBool().(true) |
| 99 | } |
| 100 | |
| 101 | func (a *accountsMgrscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/accountsMgr#) setscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/accountsMgr#set().() error { |
| 102 | if sshKeysscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/sshKeys. == nil { |
| 103 | loggerscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/.Debugfscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/Debugf().("initialize sshKeys map") |
| 104 | sshKeysscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/sshKeys. = make(map[string][]string) |
| 105 | } |
| 106 | |
| 107 | loggerscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/.Debugfscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/Debugf().("create sudoers file if needed") |
| 108 | if err := createSudoersFilescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/createSudoersFile().(); err != nil { |
| 109 | loggerscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/.Errorfscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/Errorf().("Error creating google-sudoers file: %v.", err) |
| 110 | } |
| 111 | loggerscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/.Debugfscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/Debugf().("create sudoers group if needed") |
| 112 | if err := createSudoersGroupscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/createSudoersGroup().(); err != nil { |
| 113 | loggerscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/.Errorfscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/Errorf().("Error creating google-sudoers group: %v.", err) |
| 114 | } |
| 115 | |
| 116 | mdkeys := newMetadatascip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/newMetadata..Instancescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/metadata#Instance..Attributesscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/instance#Attributes..SSHKeysscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/attributes#SSHKeys. |
| 117 | if !newMetadatascip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/newMetadata..Instancescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/metadata#Instance..Attributesscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/instance#Attributes..BlockProjectKeysscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/attributes#BlockProjectKeys. { |
| 118 | mdkeys = append(mdkeys, newMetadatascip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/newMetadata..Projectscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/metadata#Project..Attributesscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/project#Attributes..SSHKeysscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/attributes#SSHKeys....) |
| 119 | } |
| 120 | |
| 121 | mdKeyMap := make(map[string][]string) |
| 122 | for _, key := range removeExpiredKeysscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/removeExpiredKeys().(mdkeys) { |
| 123 | idx := stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Indexscip-go gomod github.com/golang/go/src go1.13 strings/Index().(key, ":") |
| 124 | if idx == -1 { |
| 125 | loggerscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/.Debugfscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/Debugf().("invalid ssh key entry: %q", key) |
| 126 | continue |
| 127 | } |
| 128 | user := key[:idx] |
| 129 | if user == "" { |
| 130 | loggerscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/.Debugfscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/Debugf().("invalid ssh key entry: %q", key) |
| 131 | continue |
| 132 | } |
| 133 | userKeys := mdKeyMap[user] |
| 134 | userKeys = append(userKeys, key[idx+1:]) |
| 135 | mdKeyMap[user] = userKeys |
| 136 | } |
| 137 | |
| 138 | loggerscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/.Debugfscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/Debugf().("read google users file") |
| 139 | gUsers, err := readGoogleUsersFilescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/readGoogleUsersFile().() |
| 140 | if err != nil { |
| 141 | // TODO: is this OK to continue past? |
| 142 | loggerscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/.Errorfscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/Errorf().("Couldn't read google_users file: %v.", err) |
| 143 | } |
| 144 | |
| 145 | // Update SSH keys, creating Google users as needed. |
| 146 | for user, userKeys := range mdKeyMap { |
| 147 | if _, err := getPasswdscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/getPasswd().(user); err != nil { |
| 148 | loggerscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/.Infofscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/Infof().("Creating user %s.", user) |
| 149 | if err := createGoogleUserscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/createGoogleUser().(user); err != nil { |
| 150 | loggerscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/.Errorfscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/Errorf().("Error creating user: %s.", err) |
| 151 | continue |
| 152 | } |
| 153 | gUsers[user] = "" |
| 154 | } |
| 155 | if _, ok := gUsers[user]; !ok { |
| 156 | loggerscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/.Infofscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/Infof().("Adding existing user %s to google-sudoers group.", user) |
| 157 | if err := addUserToGroupscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/addUserToGroup().(user, "google-sudoers"); err != nil { |
| 158 | loggerscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/.Errorfscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/Errorf().("%v.", err) |
| 159 | } |
| 160 | } |
| 161 | if !compareStringSlicescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/compareStringSlice().(userKeys, sshKeysscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/sshKeys.[user]) { |
| 162 | loggerscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/.Infofscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/Infof().("Updating keys for user %s.", user) |
| 163 | if err := updateAuthorizedKeysFilescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/updateAuthorizedKeysFile().(user, userKeys); err != nil { |
| 164 | loggerscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/.Errorfscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/Errorf().("Error updating SSH keys for %s: %v.", user, err) |
| 165 | continue |
| 166 | } |
| 167 | sshKeysscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/sshKeys.[user] = userKeys |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // Remove Google users not found in metadata. |
| 172 | for user := range gUsers { |
| 173 | if _, ok := mdKeyMap[user]; !ok && user != "" { |
| 174 | loggerscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/.Infofscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/Infof().("Removing user %s.", user) |
| 175 | err = removeGoogleUserscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/removeGoogleUser().(user) |
| 176 | if err != nil { |
| 177 | loggerscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/.Errorfscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/Errorf().("Error removing user: %v.", err) |
| 178 | } |
| 179 | delete(sshKeysscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/sshKeys., user) |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // Update the google_users file if we've added or removed any users. |
| 184 | loggerscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/.Debugfscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/Debugf().("write google_users file") |
| 185 | if err := writeGoogleUsersFilescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/writeGoogleUsersFile().(); err != nil { |
| 186 | loggerscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/.Errorfscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/Errorf().("Error writing google_users file: %v.", err) |
| 187 | } |
| 188 | |
| 189 | // Start SSHD if not started. We do this in agent instead of adding a |
| 190 | // Wants= directive, and here instead of instance setup, so that this |
| 191 | // can be disabled by the instance configs file. |
| 192 | for _, svc := range []string{"ssh", "sshd"} { |
| 193 | // Ignore output, it's just a best effort. |
| 194 | systemctlStartscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/systemctlStart().(svc) |
| 195 | } |
| 196 | |
| 197 | return nil |
| 198 | } |
| 199 | |
| 200 | // passwdEntry is a user.User with omitted passwd fields restored. |
| 201 | type passwdEntryscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/passwdEntry# struct { |
| 202 | Usernamescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/passwdEntry#Username. string |
| 203 | Passwdscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/passwdEntry#Passwd. string |
| 204 | UIDscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/passwdEntry#UID. int |
| 205 | GIDscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/passwdEntry#GID. int |
| 206 | Namescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/passwdEntry#Name. string |
| 207 | HomeDirscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/passwdEntry#HomeDir. string |
| 208 | Shellscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/passwdEntry#Shell. string |
| 209 | } |
| 210 | |
| 211 | // getPasswd returns a passwdEntry from the local passwd database. Code adapted from os/user |
| 212 | func getPasswdscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/getPasswd().(user string) (*passwdEntryscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/passwdEntry#, error) { |
| 213 | prefix := []byte(user + ":") |
| 214 | colon := []byte{':'} |
| 215 | |
| 216 | parse := func(line []byte) (*passwdEntryscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/passwdEntry#, error) { |
| 217 | if !bytesscip-go gomod github.com/golang/go/src go1.13 bytes/.HasPrefixscip-go gomod github.com/golang/go/src go1.13 bytes/HasPrefix().(line, prefix) || bytesscip-go gomod github.com/golang/go/src go1.13 bytes/.Countscip-go gomod github.com/golang/go/src go1.13 bytes/Count().(line, colon) < 6 { |
| 218 | return nil, nil |
| 219 | } |
| 220 | // kevin:x:1005:1006::/home/kevin:/usr/bin/zsh |
| 221 | parts := stringsscip-go gomod github.com/golang/go/src go1.13 strings/.SplitNscip-go gomod github.com/golang/go/src go1.13 strings/SplitN().(string(line), ":", 7) |
| 222 | if len(parts) < 7 { |
| 223 | return nil, fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("invalid passwd entry for %s", user) |
| 224 | } |
| 225 | uid, err := strconvscip-go gomod github.com/golang/go/src go1.13 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.13 strconv/Atoi().(parts[2]) |
| 226 | if err != nil { |
| 227 | return nil, fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("invalid passwd entry for %s", user) |
| 228 | } |
| 229 | gid, err := strconvscip-go gomod github.com/golang/go/src go1.13 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.13 strconv/Atoi().(parts[3]) |
| 230 | if err != nil { |
| 231 | return nil, fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("invalid passwd entry for %s", user) |
| 232 | } |
| 233 | u := &passwdEntryscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/passwdEntry#{ |
| 234 | Usernamescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/passwdEntry#Username.: parts[0], |
| 235 | Passwdscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/passwdEntry#Passwd.: parts[1], |
| 236 | UIDscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/passwdEntry#UID.: uid, |
| 237 | GIDscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/passwdEntry#GID.: gid, |
| 238 | Namescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/passwdEntry#Name.: parts[4], |
| 239 | HomeDirscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/passwdEntry#HomeDir.: parts[5], |
| 240 | Shellscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/passwdEntry#Shell.: parts[6], |
| 241 | } |
| 242 | return u, nil |
| 243 | } |
| 244 | |
| 245 | passwd, err := osscip-go gomod github.com/golang/go/src go1.13 os/.Openscip-go gomod github.com/golang/go/src go1.13 os/Open().("/etc/passwd") |
| 246 | if err != nil { |
| 247 | return nil, err |
| 248 | } |
| 249 | bs := bufioscip-go gomod github.com/golang/go/src go1.13 bufio/.NewScannerscip-go gomod github.com/golang/go/src go1.13 bufio/NewScanner().(passwd) |
| 250 | for bs.Scanscip-go gomod github.com/golang/go/src go1.13 bufio/Scanner#Scan().() { |
| 251 | line := bs.Bytesscip-go gomod github.com/golang/go/src go1.13 bufio/Scanner#Bytes().() |
| 252 | // There's no spec for /etc/passwd or /etc/group, but we try to follow |
| 253 | // the same rules as the glibc parser, which allows comments and blank |
| 254 | // space at the beginning of a line. |
| 255 | line = bytesscip-go gomod github.com/golang/go/src go1.13 bytes/.TrimSpacescip-go gomod github.com/golang/go/src go1.13 bytes/TrimSpace().(line) |
| 256 | if len(line) == 0 || line[0] == '#' { |
| 257 | continue |
| 258 | } |
| 259 | v, err := parse(line) |
| 260 | if v != nil || err != nil { |
| 261 | return v, err |
| 262 | } |
| 263 | } |
| 264 | return nil, fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("User not found") |
| 265 | } |
| 266 | |
| 267 | func writeGoogleUsersFilescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/writeGoogleUsersFile().() error { |
| 268 | dir := pathscip-go gomod github.com/golang/go/src go1.13 path/.Dirscip-go gomod github.com/golang/go/src go1.13 path/Dir().(googleUsersFilescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/googleUsersFile.) |
| 269 | if _, err := osscip-go gomod github.com/golang/go/src go1.13 os/.Statscip-go gomod github.com/golang/go/src go1.13 os/Stat().(dir); err != nil { |
| 270 | if err = osscip-go gomod github.com/golang/go/src go1.13 os/.Mkdirscip-go gomod github.com/golang/go/src go1.13 os/Mkdir().(dir, 0755); err != nil { |
| 271 | return err |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | gfile, err := osscip-go gomod github.com/golang/go/src go1.13 os/.OpenFilescip-go gomod github.com/golang/go/src go1.13 os/OpenFile().(googleUsersFilescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/googleUsersFile., osscip-go gomod github.com/golang/go/src go1.13 os/.O_WRONLYscip-go gomod github.com/golang/go/src go1.13 os/O_WRONLY.|osscip-go gomod github.com/golang/go/src go1.13 os/.O_CREATEscip-go gomod github.com/golang/go/src go1.13 os/O_CREATE.|osscip-go gomod github.com/golang/go/src go1.13 os/.O_TRUNCscip-go gomod github.com/golang/go/src go1.13 os/O_TRUNC., 0600) |
| 276 | if err == nil { |
| 277 | defer gfile.Closescip-go gomod github.com/golang/go/src go1.13 os/File#Close().() |
| 278 | for user := range sshKeysscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/sshKeys. { |
| 279 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Fprintf().(gfile, "%s\n", user) |
| 280 | } |
| 281 | } |
| 282 | return err |
| 283 | } |
| 284 | |
| 285 | func readGoogleUsersFilescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/readGoogleUsersFile().() (map[string]string, error) { |
| 286 | res := make(map[string]string) |
| 287 | gUsers, err := ioutilscip-go gomod github.com/golang/go/src go1.13 `io/ioutil`/.ReadFilescip-go gomod github.com/golang/go/src go1.13 `io/ioutil`/ReadFile().(googleUsersFilescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/googleUsersFile.) |
| 288 | if err != nil && !osscip-go gomod github.com/golang/go/src go1.13 os/.IsNotExistscip-go gomod github.com/golang/go/src go1.13 os/IsNotExist().(err) { |
| 289 | return nil, err |
| 290 | } |
| 291 | for _, user := range stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Splitscip-go gomod github.com/golang/go/src go1.13 strings/Split().(string(gUsers), "\n") { |
| 292 | if user != "" { |
| 293 | res[user] = "" |
| 294 | } |
| 295 | } |
| 296 | return res, nil |
| 297 | } |
| 298 | |
| 299 | type linuxKeyscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/linuxKey# windowsKeyscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/windowsKey# |
| 300 | |
| 301 | // expired returns true if the key's expireOn field is in the past, false otherwise. |
| 302 | func (k linuxKeyscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/linuxKey#) expiredscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/linuxKey#expired().() bool { |
| 303 | t, err := timescip-go gomod github.com/golang/go/src go1.13 time/.Parsescip-go gomod github.com/golang/go/src go1.13 time/Parse().("2006-01-02T15:04:05-0700", k.ExpireOnscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/windowsKey#ExpireOn.) |
| 304 | if err != nil { |
| 305 | if !containsStringscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/containsString().(k.ExpireOnscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/windowsKey#ExpireOn., badExpirescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/badExpire.) { |
| 306 | loggerscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/.Errorfscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/Errorf().("Error parsing time: %v.", err) |
| 307 | badExpirescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/badExpire. = append(badExpirescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/badExpire., k.ExpireOnscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/windowsKey#ExpireOn.) |
| 308 | } |
| 309 | return true |
| 310 | } |
| 311 | return t.Beforescip-go gomod github.com/golang/go/src go1.13 time/Time#Before().(timescip-go gomod github.com/golang/go/src go1.13 time/.Nowscip-go gomod github.com/golang/go/src go1.13 time/Now().()) |
| 312 | } |
| 313 | |
| 314 | // removeExpiredKeys returns the provided list of keys with expired keys removed. |
| 315 | // valid formats are: |
| 316 | // ssh-rsa [KEY_VALUE] [USERNAME] |
| 317 | // ssh-rsa [KEY_VALUE] |
| 318 | // ssh-rsa [KEY_VALUE] google-ssh {"userName":"[USERNAME]","expireOn":"[EXPIRE_TIME]"} |
| 319 | // |
| 320 | // see: https://cloud.google.com/compute/docs/instances/adding-removing-ssh-keys#sshkeyformat |
| 321 | func removeExpiredKeysscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/removeExpiredKeys().(keys []string) []string { |
| 322 | var res []string |
| 323 | for i := 0; i < len(keys); i++ { |
| 324 | key := stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Trimscip-go gomod github.com/golang/go/src go1.13 strings/Trim().(keys[i], " ") |
| 325 | if key == "" { |
| 326 | continue |
| 327 | } |
| 328 | fields := stringsscip-go gomod github.com/golang/go/src go1.13 strings/.SplitNscip-go gomod github.com/golang/go/src go1.13 strings/SplitN().(key, " ", 4) |
| 329 | if len(fields) < 3 || fields[2] != "google-ssh" { |
| 330 | // non-expiring key, add it. |
| 331 | res = append(res, key) |
| 332 | continue |
| 333 | } |
| 334 | if len(fields) < 4 { |
| 335 | // expiring key without expiration format. |
| 336 | continue |
| 337 | } |
| 338 | lkey := linuxKeyscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/linuxKey#{} |
| 339 | if err := jsonscip-go gomod github.com/golang/go/src go1.13 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.13 `encoding/json`/Unmarshal().([]byte(fields[3]), &lkey); err != nil { |
| 340 | // invalid expiration format. |
| 341 | continue |
| 342 | } |
| 343 | if !lkey.expiredscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/linuxKey#expired().() { |
| 344 | res = append(res, key) |
| 345 | } |
| 346 | } |
| 347 | return res |
| 348 | } |
| 349 | |
| 350 | // Replaces {user} or {group} in command string. Supports legacy python-era |
| 351 | // user command overrides. |
| 352 | func createUserGroupCmdscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/createUserGroupCmd().(cmd, user, group string) *execscip-go gomod github.com/golang/go/src go1.13 `os/exec`/.Cmdscip-go gomod github.com/golang/go/src go1.13 `os/exec`/Cmd# { |
| 353 | cmd = stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Replacescip-go gomod github.com/golang/go/src go1.13 strings/Replace().(cmd, "{user}", user, 1) |
| 354 | cmd = stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Replacescip-go gomod github.com/golang/go/src go1.13 strings/Replace().(cmd, "{group}", group, 1) |
| 355 | cmds := stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Fieldsscip-go gomod github.com/golang/go/src go1.13 strings/Fields().(cmd) |
| 356 | |
| 357 | // We don't use runCmd here because we might need the exit codes. |
| 358 | return execscip-go gomod github.com/golang/go/src go1.13 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.13 `os/exec`/Command().(cmds[0], cmds[1:]...) |
| 359 | } |
| 360 | |
| 361 | // createGoogleUser creates a Google managed user account if needed and adds it |
| 362 | // to the configured groups. |
| 363 | func createGoogleUserscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/createGoogleUser().(user string) error { |
| 364 | var uid string |
| 365 | if configscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/config..Sectionscip-go gomod github.com/go-ini/ini v1.51.1 `github.com/go-ini/ini`/File#Section().("Accounts").Keyscip-go gomod github.com/go-ini/ini v1.51.1 `github.com/go-ini/ini`/Section#Key().("reuse_homedir").MustBoolscip-go gomod github.com/go-ini/ini v1.51.1 `github.com/go-ini/ini`/Key#MustBool().(false) { |
| 366 | uid = getUIDscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/getUID().(fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Sprintf().("/home/%s", user)) |
| 367 | } |
| 368 | |
| 369 | if err := createUserscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/createUser().(user, uid); err != nil { |
| 370 | return err |
| 371 | } |
| 372 | groups := configscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/config..Sectionscip-go gomod github.com/go-ini/ini v1.51.1 `github.com/go-ini/ini`/File#Section().("Accounts").Keyscip-go gomod github.com/go-ini/ini v1.51.1 `github.com/go-ini/ini`/Section#Key().("groups").MustStringscip-go gomod github.com/go-ini/ini v1.51.1 `github.com/go-ini/ini`/Key#MustString().("adm,dip,docker,lxd,plugdev,video") |
| 373 | for _, group := range stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Splitscip-go gomod github.com/golang/go/src go1.13 strings/Split().(groups, ",") { |
| 374 | addUserToGroupscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/addUserToGroup().(user, group) |
| 375 | } |
| 376 | return addUserToGroupscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/addUserToGroup().(user, "google-sudoers") |
| 377 | } |
| 378 | |
| 379 | // removeGoogleUser removes Google managed users. If deprovision_remove is true, the |
| 380 | // user and its home directory are removed. Otherwise, SSH keys and sudoer |
| 381 | // permissions are removed but the user remains on the system. Group membership |
| 382 | // is not changed. |
| 383 | func removeGoogleUserscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/removeGoogleUser().(user string) error { |
| 384 | if configscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/config..Sectionscip-go gomod github.com/go-ini/ini v1.51.1 `github.com/go-ini/ini`/File#Section().("Accounts").Keyscip-go gomod github.com/go-ini/ini v1.51.1 `github.com/go-ini/ini`/Section#Key().("deprovision_remove").MustBoolscip-go gomod github.com/go-ini/ini v1.51.1 `github.com/go-ini/ini`/Key#MustBool().(false) { |
| 385 | userdel := configscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/config..Sectionscip-go gomod github.com/go-ini/ini v1.51.1 `github.com/go-ini/ini`/File#Section().("Accounts").Keyscip-go gomod github.com/go-ini/ini v1.51.1 `github.com/go-ini/ini`/Section#Key().("userdel_cmd").MustStringscip-go gomod github.com/go-ini/ini v1.51.1 `github.com/go-ini/ini`/Key#MustString().("userdel -r {user}") |
| 386 | return runCmdscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/runCmd().(createUserGroupCmdscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/createUserGroupCmd().(userdel, user, "")) |
| 387 | } |
| 388 | if err := updateAuthorizedKeysFilescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/updateAuthorizedKeysFile().(user, []string{}); err != nil { |
| 389 | return err |
| 390 | } |
| 391 | gpasswddel := configscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/config..Sectionscip-go gomod github.com/go-ini/ini v1.51.1 `github.com/go-ini/ini`/File#Section().("Accounts").Keyscip-go gomod github.com/go-ini/ini v1.51.1 `github.com/go-ini/ini`/Section#Key().("gpasswd_remove_cmd").MustStringscip-go gomod github.com/go-ini/ini v1.51.1 `github.com/go-ini/ini`/Key#MustString().("gpasswd -d {user} {group}") |
| 392 | return runCmdscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/runCmd().(createUserGroupCmdscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/createUserGroupCmd().(gpasswddel, user, "google-sudoers")) |
| 393 | } |
| 394 | |
| 395 | // createSudoersFile creates the google_sudoers configuration file if it does |
| 396 | // not exist and specifies the group 'google-sudoers' should have all |
| 397 | // permissions. |
| 398 | func createSudoersFilescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/createSudoersFile().() error { |
| 399 | sudoFile, err := osscip-go gomod github.com/golang/go/src go1.13 os/.OpenFilescip-go gomod github.com/golang/go/src go1.13 os/OpenFile().("/etc/sudoers.d/google_sudoers", osscip-go gomod github.com/golang/go/src go1.13 os/.O_WRONLYscip-go gomod github.com/golang/go/src go1.13 os/O_WRONLY.|osscip-go gomod github.com/golang/go/src go1.13 os/.O_CREATEscip-go gomod github.com/golang/go/src go1.13 os/O_CREATE.|osscip-go gomod github.com/golang/go/src go1.13 os/.O_EXCLscip-go gomod github.com/golang/go/src go1.13 os/O_EXCL., 0440) |
| 400 | if err != nil { |
| 401 | if osscip-go gomod github.com/golang/go/src go1.13 os/.IsExistscip-go gomod github.com/golang/go/src go1.13 os/IsExist().(err) { |
| 402 | return nil |
| 403 | } |
| 404 | return err |
| 405 | } |
| 406 | defer sudoFile.Closescip-go gomod github.com/golang/go/src go1.13 os/File#Close().() |
| 407 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Fprintf().(sudoFile, "%%google-sudoers ALL=(ALL:ALL) NOPASSWD:ALL\n") |
| 408 | return nil |
| 409 | } |
| 410 | |
| 411 | // createSudoersGroup creates the google-sudoers group if it does not exist. |
| 412 | func createSudoersGroupscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/createSudoersGroup().() error { |
| 413 | groupadd := configscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/config..Sectionscip-go gomod github.com/go-ini/ini v1.51.1 `github.com/go-ini/ini`/File#Section().("Accounts").Keyscip-go gomod github.com/go-ini/ini v1.51.1 `github.com/go-ini/ini`/Section#Key().("groupadd_cmd").MustStringscip-go gomod github.com/go-ini/ini v1.51.1 `github.com/go-ini/ini`/Key#MustString().("groupadd {group}") |
| 414 | ret := runCmdOutputscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/runCmdOutput().(createUserGroupCmdscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/createUserGroupCmd().(groupadd, "", "google-sudoers")) |
| 415 | if ret.ExitCodescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/execResult#ExitCode().() == 9 { |
| 416 | // 9 means group already exists. |
| 417 | return nil |
| 418 | } |
| 419 | if ret.ExitCodescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/execResult#ExitCode().() != 0 { |
| 420 | return error(ret) |
| 421 | } |
| 422 | loggerscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/.Infofscip-go gomod github.com/GoogleCloudPlatform/guest-logging-go d798144cee48 `github.com/GoogleCloudPlatform/guest-logging-go/logger`/Infof().("Created google sudoers file") |
| 423 | return nil |
| 424 | } |
| 425 | |
| 426 | // updateAuthorizedKeysFile adds provided keys to the user's SSH |
| 427 | // AuthorizedKeys file. The file and containing directory are created if it |
| 428 | // does not exist. Uses a temporary file to avoid partial updates in case of |
| 429 | // errors. If no keys are provided, the authorized keys file is removed. |
| 430 | func updateAuthorizedKeysFilescip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/updateAuthorizedKeysFile().(user string, keys []string) error { |
| 431 | gcomment := "# Added by Google" |
| 432 | |
| 433 | passwd, err := getPasswdscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/getPasswd().(user) |
| 434 | if err != nil { |
| 435 | return err |
| 436 | } |
| 437 | |
| 438 | if passwd.HomeDirscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/passwdEntry#HomeDir. == "" { |
| 439 | return fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("user %s has no homedir set", user) |
| 440 | } |
| 441 | if passwd.Shellscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/passwdEntry#Shell. == "/sbin/nologin" { |
| 442 | return nil |
| 443 | } |
| 444 | |
| 445 | sshpath := pathscip-go gomod github.com/golang/go/src go1.13 path/.Joinscip-go gomod github.com/golang/go/src go1.13 path/Join().(passwd.HomeDirscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/passwdEntry#HomeDir., ".ssh") |
| 446 | if _, err := osscip-go gomod github.com/golang/go/src go1.13 os/.Statscip-go gomod github.com/golang/go/src go1.13 os/Stat().(sshpath); err != nil { |
| 447 | if osscip-go gomod github.com/golang/go/src go1.13 os/.IsNotExistscip-go gomod github.com/golang/go/src go1.13 os/IsNotExist().(err) { |
| 448 | if err = osscip-go gomod github.com/golang/go/src go1.13 os/.Mkdirscip-go gomod github.com/golang/go/src go1.13 os/Mkdir().(sshpath, 0700); err != nil { |
| 449 | return err |
| 450 | } |
| 451 | if err = osscip-go gomod github.com/golang/go/src go1.13 os/.Chownscip-go gomod github.com/golang/go/src go1.13 os/Chown().(sshpath, passwd.UIDscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/passwdEntry#UID., passwd.GIDscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/passwdEntry#GID.); err != nil { |
| 452 | return err |
| 453 | } |
| 454 | } else { |
| 455 | return err |
| 456 | } |
| 457 | } |
| 458 | akpath := pathscip-go gomod github.com/golang/go/src go1.13 path/.Joinscip-go gomod github.com/golang/go/src go1.13 path/Join().(sshpath, "authorized_keys") |
| 459 | // Remove empty file. |
| 460 | if len(keys) == 0 { |
| 461 | osscip-go gomod github.com/golang/go/src go1.13 os/.Removescip-go gomod github.com/golang/go/src go1.13 os/Remove().(akpath) |
| 462 | return nil |
| 463 | } |
| 464 | |
| 465 | tempPath := akpath + ".google" |
| 466 | akcontents, err := ioutilscip-go gomod github.com/golang/go/src go1.13 `io/ioutil`/.ReadFilescip-go gomod github.com/golang/go/src go1.13 `io/ioutil`/ReadFile().(akpath) |
| 467 | if err != nil && !osscip-go gomod github.com/golang/go/src go1.13 os/.IsNotExistscip-go gomod github.com/golang/go/src go1.13 os/IsNotExist().(err) { |
| 468 | return err |
| 469 | } |
| 470 | |
| 471 | var isgoogle bool |
| 472 | var userKeys []string |
| 473 | for _, key := range stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Splitscip-go gomod github.com/golang/go/src go1.13 strings/Split().(string(akcontents), "\n") { |
| 474 | if key == "" { |
| 475 | continue |
| 476 | } |
| 477 | if isgoogle { |
| 478 | isgoogle = false |
| 479 | continue |
| 480 | } |
| 481 | if key == gcomment { |
| 482 | isgoogle = true |
| 483 | continue |
| 484 | } |
| 485 | userKeys = append(userKeys, key) |
| 486 | } |
| 487 | |
| 488 | newfile, err := osscip-go gomod github.com/golang/go/src go1.13 os/.OpenFilescip-go gomod github.com/golang/go/src go1.13 os/OpenFile().(tempPath, osscip-go gomod github.com/golang/go/src go1.13 os/.O_WRONLYscip-go gomod github.com/golang/go/src go1.13 os/O_WRONLY.|osscip-go gomod github.com/golang/go/src go1.13 os/.O_CREATEscip-go gomod github.com/golang/go/src go1.13 os/O_CREATE., 0600) |
| 489 | if err != nil { |
| 490 | return err |
| 491 | } |
| 492 | defer newfile.Closescip-go gomod github.com/golang/go/src go1.13 os/File#Close().() |
| 493 | |
| 494 | for _, key := range userKeys { |
| 495 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Fprintf().(newfile, "%s\n", key) |
| 496 | } |
| 497 | for _, key := range keys { |
| 498 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Fprintf().(newfile, "%s\n%s\n", gcomment, key) |
| 499 | } |
| 500 | err = osscip-go gomod github.com/golang/go/src go1.13 os/.Chownscip-go gomod github.com/golang/go/src go1.13 os/Chown().(tempPath, passwd.UIDscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/passwdEntry#UID., passwd.GIDscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/passwdEntry#GID.) |
| 501 | if err != nil { |
| 502 | // Existence of temp file will block further updates for this user. |
| 503 | // Don't catch remove error, nothing we can do. Return the |
| 504 | // chown error which caused the issue. |
| 505 | osscip-go gomod github.com/golang/go/src go1.13 os/.Removescip-go gomod github.com/golang/go/src go1.13 os/Remove().(tempPath) |
| 506 | return fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("error setting ownership of new keys file: %v", err) |
| 507 | } |
| 508 | runCmdscip-go gomod github.com/GoogleCloudPlatform/guest-agent . `github.com/GoogleCloudPlatform/guest-agent/google_guest_agent`/runCmd().(execscip-go gomod github.com/golang/go/src go1.13 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.13 `os/exec`/Command().("restorecon", tempPath)) |
| 509 | return osscip-go gomod github.com/golang/go/src go1.13 os/.Renamescip-go gomod github.com/golang/go/src go1.13 os/Rename().(tempPath, akpath) |
| 510 | } |