-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathmain.go
110 lines (96 loc) · 2.79 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2022 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Utilities for Google Guest Agent and Google Authorized Keys
package utils
import (
"encoding/json"
"errors"
"strings"
"time"
"github.com/tarm/serial"
)
//ContainsString checks for the presence of a string in a slice.
func ContainsString(s string, ss []string) bool {
for _, a := range ss {
if a == s {
return true
}
}
return false
}
type sshKeyData struct {
ExpireOn string
UserName string
}
//CheckExpired takes a time string and determines if it represents a time in the past.
func CheckExpired(expireOn string) (bool, error) {
t, err := time.Parse(time.RFC3339, expireOn)
if err != nil {
t2, err2 := time.Parse("2006-01-02T15:04:05-0700", expireOn)
if err2 != nil {
return true, err //Return RFC3339 error
}
t = t2
}
return t.Before(time.Now()), nil
}
//GetUserKey takes a string and determines if it is a valid SSH key and returns
//the user and key if valid, nil otherwise.
func GetUserKey(rawKey string) (string, string, error) {
key := strings.Trim(rawKey, " ")
if key == "" {
return "", "", errors.New("Invalid ssh key entry - empty key")
}
idx := strings.Index(key, ":")
if idx == -1 {
return "", "", errors.New("Invalid ssh key entry - unrecognized format")
}
user := key[:idx]
if user == "" {
return "", "", errors.New("Invalid ssh key entry - user missing")
}
fields := strings.SplitN(key, " ", 4)
if len(fields) == 3 && fields[2] == "google-ssh" {
// expiring key without expiration format.
return "", "", errors.New("Invalid ssh key entry - expiration missing")
}
if len(fields) > 3 {
lkey := sshKeyData{}
if err := json.Unmarshal([]byte(fields[3]), &lkey); err != nil {
// invalid expiration format.
return "", "", err
}
expired, err := CheckExpired(lkey.ExpireOn)
if err != nil {
return "", "", err
}
if expired {
return "", "", errors.New("Invalid ssh key entry - expired key")
}
}
return user, key[idx+1:], nil
}
//SerialPort is a type for writing to a named serial port.
type SerialPort struct {
Port string
}
func (s *SerialPort) Write(b []byte) (int, error) {
c := &serial.Config{Name: s.Port, Baud: 115200}
p, err := serial.OpenPort(c)
if err != nil {
return 0, err
}
defer p.Close()
return p.Write(b)
}