-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgencert.go
77 lines (63 loc) · 1.67 KB
/
gencert.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
// SPDX-FileCopyrightText: 2024 Tillitis AB <tillitis.se>
// SPDX-License-Identifier: BSD-2-Clause
package main
import (
"bytes"
"errors"
"fmt"
"log"
"os"
"os/exec"
"github.com/gliderlabs/ssh"
gossh "golang.org/x/crypto/ssh"
)
func run(cmdLine []string) (string, string, int) {
var outBuf bytes.Buffer
var errBuf bytes.Buffer
cmd := exec.Command(cmdLine[0], cmdLine[1:]...) // #nosec G204
cmd.Stdout = &outBuf
cmd.Stderr = &errBuf
err := cmd.Run()
stdout := string(outBuf.Bytes())
stderr := string(errBuf.Bytes())
code := 0
if err != nil {
var exitError *exec.ExitError
if !errors.As(err, &exitError) {
log.Printf("Failed to run %v: %s.\nSTDOUT:\n%s\nSTDERR:\n%s\n", cmdLine, err, stdout, stderr)
os.Exit(1)
}
code = exitError.ExitCode()
}
return stdout, stderr, code
}
func genCert(pubkey ssh.PublicKey, keyid string, user string) ([]byte, error) {
mKey := gossh.MarshalAuthorizedKey(pubkey)
log.Printf("Generating cert of pubkey %s\n", mKey)
f, err := os.CreateTemp("", "ssh-ca")
if err != nil {
return nil, fmt.Errorf("couldn't create temp file: %w", err)
}
defer os.Remove(f.Name())
_, err = f.Write(mKey)
if err != nil {
return nil, fmt.Errorf("couldn't write to temp file: %w", err)
}
cmdLine := []string{
"ssh-keygen",
"-Us", "./ca_key.pub",
"-I", keyid,
"-n", user,
f.Name(),
}
if stdout, stderr, code := run(cmdLine); code != 0 {
log.Printf("stdout: %v\nstderr: %v", stdout, stderr)
return nil, fmt.Errorf("couldn't run ssh-keygen")
}
defer os.Remove(f.Name() + "-cert.pub")
cert, err := os.ReadFile(f.Name() + "-cert.pub")
if err != nil {
return nil, fmt.Errorf("unable to read cert: %w", err)
}
return cert, nil
}