@@ -73,7 +73,7 @@ func (a *accountsMgr) diff() bool {
73
73
74
74
// If any on-disk keys have expired.
75
75
for _ , keys := range sshKeys {
76
- if len (keys ) != len (removeExpiredKeys (keys )) {
76
+ if len (keys ) != len (getUserKeys (keys )) {
77
77
return true
78
78
}
79
79
}
@@ -118,22 +118,7 @@ func (a *accountsMgr) set() error {
118
118
mdkeys = append (mdkeys , newMetadata .Project .Attributes .SSHKeys ... )
119
119
}
120
120
121
- mdKeyMap := make (map [string ][]string )
122
- for _ , key := range removeExpiredKeys (mdkeys ) {
123
- idx := strings .Index (key , ":" )
124
- if idx == - 1 {
125
- logger .Debugf ("invalid ssh key entry: %q" , key )
126
- continue
127
- }
128
- user := key [:idx ]
129
- if user == "" {
130
- 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
- }
121
+ mdKeyMap := getUserKeys (mdkeys )
137
122
138
123
logger .Debugf ("read google users file" )
139
124
gUsers , err := readGoogleUsersFile ()
@@ -197,6 +182,55 @@ func (a *accountsMgr) set() error {
197
182
return nil
198
183
}
199
184
185
+ // getUserKeys returns the keys which are not expired and non-expiring key.
186
+ // valid formats are:
187
+ // user:ssh-rsa [KEY_VALUE] [USERNAME]
188
+ // user:ssh-rsa [KEY_VALUE]
189
+ // user:ssh-rsa [KEY_VALUE] google-ssh {"userName":"[USERNAME]","expireOn":"[EXPIRE_TIME]"}
190
+ func getUserKeys (mdkeys []string ) map [string ][]string {
191
+ mdKeyMap := make (map [string ][]string )
192
+ for i := 0 ; i < len (mdkeys ); i ++ {
193
+ key := strings .Trim (mdkeys [i ], " " )
194
+ if key == "" {
195
+ logger .Debugf ("invalid ssh key entry: %q" , key )
196
+ continue
197
+ }
198
+ idx := strings .Index (key , ":" )
199
+ if idx == - 1 {
200
+ logger .Debugf ("invalid ssh key entry: %q" , key )
201
+ continue
202
+ }
203
+ user := key [:idx ]
204
+ if user == "" {
205
+ logger .Debugf ("invalid ssh key entry: %q" , key )
206
+ continue
207
+ }
208
+ fields := strings .SplitN (key , " " , 4 )
209
+ if len (fields ) == 3 && fields [2 ] == "google-ssh" {
210
+ logger .Debugf ("invalid ssh key entry: %q" , key )
211
+ // expiring key without expiration format.
212
+ continue
213
+ }
214
+ if len (fields ) > 3 {
215
+ lkey := linuxKey {}
216
+ if err := json .Unmarshal ([]byte (fields [3 ]), & lkey ); err != nil {
217
+ // invalid expiration format.
218
+ logger .Debugf ("invalid ssh key entry: %q" , key )
219
+ continue
220
+ }
221
+ if lkey .expired () {
222
+ logger .Debugf ("expired ssh key entry: %q" , key )
223
+ continue
224
+ }
225
+ }
226
+ // key which is not expired or non-expiring key, add it.
227
+ userKeys := mdKeyMap [user ]
228
+ userKeys = append (userKeys , key [idx + 1 :])
229
+ mdKeyMap [user ] = userKeys
230
+ }
231
+ return mdKeyMap
232
+ }
233
+
200
234
// passwdEntry is a user.User with omitted passwd fields restored.
201
235
type passwdEntry struct {
202
236
Username string
@@ -311,42 +345,6 @@ func (k linuxKey) expired() bool {
311
345
return t .Before (time .Now ())
312
346
}
313
347
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 removeExpiredKeys (keys []string ) []string {
322
- var res []string
323
- for i := 0 ; i < len (keys ); i ++ {
324
- key := strings .Trim (keys [i ], " " )
325
- if key == "" {
326
- continue
327
- }
328
- fields := 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 := linuxKey {}
339
- if err := json .Unmarshal ([]byte (fields [3 ]), & lkey ); err != nil {
340
- // invalid expiration format.
341
- continue
342
- }
343
- if ! lkey .expired () {
344
- res = append (res , key )
345
- }
346
- }
347
- return res
348
- }
349
-
350
348
// Replaces {user} or {group} in command string. Supports legacy python-era
351
349
// user command overrides.
352
350
func createUserGroupCmd (cmd , user , group string ) * exec.Cmd {
0 commit comments