@@ -10,6 +10,8 @@ import (
10
10
"os"
11
11
"os/exec"
12
12
"path/filepath"
13
+ "regexp"
14
+ "strings"
13
15
"time"
14
16
)
15
17
@@ -103,7 +105,12 @@ func main() {
103
105
log .Fatalf ("generate public key during enroll: %v" , err )
104
106
}
105
107
106
- fmt .Printf ("no enrollment token present. Send 'Nais Device' this message on slack: 'enroll %v'" , string (pubkey ))
108
+ serial , err := getDeviceSerial ()
109
+ if err != nil {
110
+ log .Fatalf ("getting device serial: %v" , err )
111
+ }
112
+
113
+ fmt .Printf ("no enrollment token present. Send 'Nais Device' this message on slack: 'enroll %v %v'" , serial , pubkey )
107
114
os .Exit (0 )
108
115
}
109
116
@@ -128,6 +135,24 @@ func main() {
128
135
}
129
136
}
130
137
138
+ // TODO(jhrv): extract this as a separate interface, with platform specific implmentations
139
+ func getDeviceSerial () (string , error ) {
140
+ cmd := exec .Command ("/usr/sbin/ioreg" , "-rd1" , "-c" , "IOPlatformExpertDevice" )
141
+ b , err := cmd .Output ()
142
+ if err != nil {
143
+ return "" , fmt .Errorf ("getting serial with ioreg: %w" , err )
144
+ }
145
+
146
+ re := regexp .MustCompile ("\" IOPlatformSerialNumber\" = \" ([^\" ]+)\" " )
147
+ matches := re .FindSubmatch (b )
148
+
149
+ if len (matches ) != 2 {
150
+ return "" , fmt .Errorf ("unable to extract serial from output: %v" , string (b ))
151
+ }
152
+
153
+ return string (matches [1 ]), nil
154
+ }
155
+
131
156
func setupControlPlane (enrollmentToken string ) error {
132
157
enrollmentConfig , err := ParseEnrollmentToken (enrollmentToken )
133
158
if err != nil {
@@ -212,24 +237,30 @@ Endpoint = %s
212
237
return []byte (fmt .Sprintf (template , privateKey , enrollmentConfig .PublicKey , enrollmentConfig .APIServerIP , enrollmentConfig .Endpoint ))
213
238
}
214
239
215
- func generatePublicKey (privateKeyPath string ) ([] byte , error ) {
240
+ func generatePublicKey (privateKeyPath string ) (string , error ) {
216
241
cmd := exec .Command (WGBinary , "pubkey" )
217
242
218
243
stdin , err := cmd .StdinPipe ()
219
244
if err != nil {
220
- return nil , fmt .Errorf ("creating stdin pipe on 'wg pubkey': %w" , err )
245
+ return "" , fmt .Errorf ("creating stdin pipe on 'wg pubkey': %w" , err )
221
246
}
222
247
223
248
b , err := ioutil .ReadFile (privateKeyPath )
224
249
if err != nil {
225
- return nil , fmt .Errorf ("reading private key: %w" , err )
250
+ return "" , fmt .Errorf ("reading private key: %w" , err )
226
251
}
227
252
228
253
if _ , err := stdin .Write (b ); err != nil {
229
- return nil , fmt .Errorf ("piping private key to 'wg genkey': %w" , err )
254
+ return "" , fmt .Errorf ("piping private key to 'wg genkey': %w" , err )
255
+ }
256
+
257
+ if err := stdin .Close (); err != nil {
258
+ return "" , fmt .Errorf ("closing stdin: %w" , err )
230
259
}
231
260
232
- return cmd .Output ()
261
+ b , err = cmd .Output ()
262
+ pubkey := strings .TrimSuffix (string (b ), "\n " )
263
+ return pubkey , err
233
264
}
234
265
235
266
func filesExist (files ... string ) error {
0 commit comments