Skip to content

Commit 3cae950

Browse files
committed
get serial from device
1 parent d18af2a commit 3cae950

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

cmd/client-agent/main.go

+37-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"os"
1111
"os/exec"
1212
"path/filepath"
13+
"regexp"
14+
"strings"
1315
"time"
1416
)
1517

@@ -103,7 +105,12 @@ func main() {
103105
log.Fatalf("generate public key during enroll: %v", err)
104106
}
105107

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)
107114
os.Exit(0)
108115
}
109116

@@ -128,6 +135,24 @@ func main() {
128135
}
129136
}
130137

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+
131156
func setupControlPlane(enrollmentToken string) error {
132157
enrollmentConfig, err := ParseEnrollmentToken(enrollmentToken)
133158
if err != nil {
@@ -212,24 +237,30 @@ Endpoint = %s
212237
return []byte(fmt.Sprintf(template, privateKey, enrollmentConfig.PublicKey, enrollmentConfig.APIServerIP, enrollmentConfig.Endpoint))
213238
}
214239

215-
func generatePublicKey(privateKeyPath string) ([]byte, error) {
240+
func generatePublicKey(privateKeyPath string) (string, error) {
216241
cmd := exec.Command(WGBinary, "pubkey")
217242

218243
stdin, err := cmd.StdinPipe()
219244
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)
221246
}
222247

223248
b, err := ioutil.ReadFile(privateKeyPath)
224249
if err != nil {
225-
return nil, fmt.Errorf("reading private key: %w", err)
250+
return "", fmt.Errorf("reading private key: %w", err)
226251
}
227252

228253
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)
230259
}
231260

232-
return cmd.Output()
261+
b, err = cmd.Output()
262+
pubkey := strings.TrimSuffix(string(b), "\n")
263+
return pubkey, err
233264
}
234265

235266
func filesExist(files ...string) error {

0 commit comments

Comments
 (0)