-
Notifications
You must be signed in to change notification settings - Fork 2
/
bootstrapper_authkey.go
70 lines (61 loc) · 2.16 KB
/
bootstrapper_authkey.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
package soratun
import (
"fmt"
"os"
)
// AuthKeyBootstrapper defines bootstrap method with SORACOM API authentication. Needs Profile information.
type AuthKeyBootstrapper struct {
Profile *Profile
}
// Execute calls SORACOM API to create a new standalone virtual subscriber.
func (b *AuthKeyBootstrapper) Execute(config *Config) (*Config, error) {
client, err := NewDefaultSoracomClient(*b.Profile)
if err != nil {
return nil, err
}
if v := os.Getenv("SORACOM_VERBOSE"); v != "" {
client.SetVerbose(true)
}
if config == nil {
// if no config, bootstrap with API call
sim, err := client.CreateVirtualSim()
if err != nil {
return nil, err
}
privateKey, err := NewKey(sim.Profiles[sim.SimId].ArcClientPeerPrivateKey)
if err != nil {
return nil, fmt.Errorf("virtual SIM/subscriber %s was created but failed to create a configuration. "+
"due to unexpected ArcClientPeerPrivateKey received. "+
"Please open SORACOM User Console at https://console.soracom.io and check virtual SIM status. "+
"You have to create arc.json manually", sim.SimId)
}
publicKey, err := NewKey(sim.Profiles[sim.SimId].ArcClientPeerPublicKey)
if err != nil {
return nil, fmt.Errorf("virtual SIM/subscriber %s was created but failed to create a configuration. "+
"due to unexpected ArcClientPeerPublicKey received. "+
"Please open SORACOM User Console at https://console.soracom.io and check virtual SIM status. "+
"You have to create arc.json manually", sim.SimId)
}
config = &Config{
PrivateKey: privateKey,
PublicKey: publicKey,
SimId: sim.SimId,
LogLevel: LogLevelVerbose,
EnableMetrics: true,
Interface: DefaultInterfaceName(),
AdditionalAllowedIPs: nil,
Mtu: DefaultMTU,
PersistentKeepalive: DefaultPersistentKeepaliveInterval,
Profile: b.Profile,
ArcSession: &sim.ArcSession,
}
} else {
// or just update arcSession
arcSession, err := client.CreateArcSession(config.SimId, config.PublicKey.AsWgKey().String())
if err != nil {
return nil, err
}
config.ArcSession = arcSession
}
return config, nil
}