/
bootstrap_sim.go
95 lines (88 loc) · 4.09 KB
/
bootstrap_sim.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
package cmd
import (
"fmt"
"log"
"os"
"github.com/soracom/soratun"
"github.com/spf13/cobra"
)
var (
provisioningAPIEndpointURL string
requestParameters string
keysAPIEndpointURL string
signatureAlgorithm string
uiccInterfaceType string
portName string
baudRate uint
dataBits uint
stopBits uint
parityMode uint
disableKeyCache bool
clearKeyCache bool
kryptonCliPath string
dumpConfig bool
)
func bootstrapSimCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "sim",
Short: "Create virtual SIM which is associated with current SIM with SORACOM Krypton SIM authentication",
Long: "This command will create a new virtual SIM which is associated with current physical SIM, then create configuration for soratun. You need working \"krypton-cli\". See https://github.com/soracom/krypton-client-go for how to install.",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
err := bootstrap(&soratun.SimBootstrapper{
KryptonCliPath: kryptonCliPath,
Arguments: buildKryptonCliArguments(),
})
if err != nil {
log.Fatalf("failed to bootstrap: %v", err)
}
},
}
cmd.Flags().StringVar(&provisioningAPIEndpointURL, "provisioning-api-endpoint-url", "", "Use the specified URL as a Provisioning API endpoint.")
cmd.Flags().StringVar(&requestParameters, "params", "", "Pass additional JSON parameters to the service request")
cmd.Flags().StringVar(&keysAPIEndpointURL, "keys-api-endpoint-url", "", "Use the specified URL as a Keys API endpoint")
cmd.Flags().StringVar(&signatureAlgorithm, "signature-algorithm", "SHA-256", "Algorithm for generating signature.")
cmd.Flags().StringVar(&uiccInterfaceType, "interface", "autoDetect", "UICC Interface to use. Valid values are iso7816, comm, mmcli or autoDetect")
cmd.Flags().StringVar(&portName, "port-name", "", "Port name of communication device (e.g. COM1 or /dev/tty1)")
cmd.Flags().UintVar(&baudRate, "baud-rate", 57600, "Baud rate for communication device")
cmd.Flags().UintVar(&dataBits, "data-bits", 8, "Data bits for communication device")
cmd.Flags().UintVar(&stopBits, "stop-bits", 1, "Stop bits for communication device")
cmd.Flags().UintVar(&parityMode, "parity-mode", 0, "Parity mode for communication device. 0: None (default), 1: Odd, 2: Even")
cmd.Flags().BoolVar(&disableKeyCache, "disable-key-cache", false, "Do not store authentication result to the key cache")
cmd.Flags().BoolVar(&clearKeyCache, "clear-key-cache", false, "Remove all items in the key cache")
cmd.Flags().StringVar(&kryptonCliPath, "krypton-cli-path", "/usr/local/bin/krypton-cli", "Path to krypton-cli")
cmd.Flags().BoolVar(&dumpConfig, "dump-config", false, "dump configuration to stdout, ignoring --config setting")
return cmd
}
func buildKryptonCliArguments() []string {
var args []string
args = append(args, []string{"-operation", "bootstrapArc"}...)
args = append(args, []string{"-signature-algorithm", signatureAlgorithm}...)
args = append(args, []string{"-interface", uiccInterfaceType}...)
args = append(args, []string{"-baud-rate", fmt.Sprint(baudRate)}...)
args = append(args, []string{"-data-bits", fmt.Sprint(dataBits)}...)
args = append(args, []string{"-stop-bits", fmt.Sprint(stopBits)}...)
args = append(args, []string{"-parity-mode", fmt.Sprint(parityMode)}...)
if provisioningAPIEndpointURL != "" {
args = append(args, []string{"-provisioning-api-endpoint-url", provisioningAPIEndpointURL}...)
}
if requestParameters != "" {
args = append(args, []string{"-params", requestParameters}...)
}
if keysAPIEndpointURL != "" {
args = append(args, []string{"-keys-api-endpoint-url", keysAPIEndpointURL}...)
}
if portName != "" {
args = append(args, []string{"-port-name", portName}...)
}
if disableKeyCache {
args = append(args, []string{"-disable-key-cache"}...)
}
if clearKeyCache {
args = append(args, []string{"-clear-key-cache"}...)
}
if v := os.Getenv("SORACOM_VERBOSE"); v != "" {
args = append(args, []string{"-debug"}...)
}
return args
}