-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbootstrapper_sim_linux.go
77 lines (66 loc) · 2.28 KB
/
bootstrapper_sim_linux.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
package soratun
import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
)
// SimBootstrapper defines bootstrap method with SORACOM Krypton SIM authentication. Needs krypton-cli installed.
type SimBootstrapper struct {
KryptonCliPath string
Arguments []string
}
// Execute calls SORACOM Krypton CLI to create a new virtual subscriber which is associated with current physical SIM.
func (b *SimBootstrapper) Execute(config *Config) (*Config, error) {
if _, err := os.Stat(b.KryptonCliPath); os.IsNotExist(err) {
return nil, err
}
if v := os.Getenv("SORACOM_VERBOSE"); v != "" {
fmt.Fprintf(os.Stderr, "Running %s %s\n", b.KryptonCliPath, b.Arguments)
}
// if no config, create a blank, then replace keys and ArcSession with new
if config == nil {
config = &Config{
PrivateKey: Key{},
PublicKey: Key{},
SimId: "",
LogLevel: LogLevelVerbose,
EnableMetrics: true,
Interface: DefaultInterfaceName(),
AdditionalAllowedIPs: nil,
Mtu: DefaultMTU,
PersistentKeepalive: DefaultPersistentKeepaliveInterval,
Profile: nil,
ArcSession: nil,
}
}
cmd := exec.Command(b.KryptonCliPath, b.Arguments...)
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
log.Fatalf("Error while running %s: %s\n%s", b.KryptonCliPath, err, &stderr)
}
var arcSession ArcSession
err = json.Unmarshal(stdout.Bytes(), &arcSession)
if err != nil {
return nil, fmt.Errorf("error while reading response from krypton-cli: %s\nkrypton-cli output:\n-----\n%s", err, stdout.String())
}
// Since soratun.ArcSession marshaler omits ArcClientPeerPrivateKey, we can simply and safely marshal response from
// Krypton CLI, and print it to stdout.
t, err := json.Marshal(&arcSession)
if err != nil {
return nil, fmt.Errorf("error while marshaling response from krypton-cli: %s", err)
}
if v := os.Getenv("SORACOM_VERBOSE"); v != "" {
fmt.Fprintf(os.Stderr, "Got response from %s: %s\n", b.KryptonCliPath, t)
}
config.PrivateKey = arcSession.ArcClientPeerPrivateKey
config.PublicKey = (Key)(arcSession.ArcClientPeerPrivateKey.AsWgKey().PublicKey())
config.ArcSession = &arcSession
return config, nil
}