Skip to content

Commit 4440ee7

Browse files
committed
function to get control plane peer, and return enrollment token to user
1 parent efd93f6 commit 4440ee7

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

apiserver/config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type Config struct {
88
PrivateKeyPath string
99
ControlPlaneWGConfigPath string
1010
SkipSetupInterface bool
11+
ControlPlaneEndpoint string
1112
}
1213

1314
func DefaultConfig() Config {

apiserver/database/database.go

+24
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,30 @@ INSERT
190190
return tx.Commit(ctx)
191191
}
192192

193+
func (d *APIServerDB) ReadControlPlanePeer(serial string) (*Peer, error) {
194+
ctx := context.Background()
195+
196+
query := `
197+
SELECT public_key, ip
198+
FROM client_peer
199+
JOIN client on id = client_id
200+
JOIN peer on id = peer_id
201+
WHERE c.serial = $1
202+
AND p.type = 'control'
203+
LIMIT 1;`
204+
205+
row := d.conn.QueryRow(ctx, query)
206+
207+
var peer Peer
208+
err := row.Scan(&peer.PublicKey, &peer.IP)
209+
210+
if err != nil {
211+
return nil, fmt.Errorf("scanning row: %s", err)
212+
}
213+
214+
return &peer, nil
215+
}
216+
193217
func ips(tx pgx.Tx, ctx context.Context) ([]string, error) {
194218
rows, err := tx.Query(ctx, "SELECT ip FROM peer;")
195219
if err != nil {

apiserver/slack/slack.go

+32-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package slack
22

33
import (
4+
"encoding/base64"
5+
"encoding/json"
46
"fmt"
57
"strings"
68

@@ -12,8 +14,16 @@ import (
1214
const Usage = `register publicKey serialNumber`
1315

1416
type slackbot struct {
15-
api *slack.Client
16-
database *database.APIServerDB
17+
api *slack.Client
18+
database *database.APIServerDB
19+
controlPlaneEndpoint string
20+
}
21+
22+
type EnrollmentConfig struct {
23+
ClientIP string `json:"clientIP"`
24+
PublicKey string `json:"publicKey"`
25+
Endpoint string `json:"endpoint"`
26+
APIServerIP string `json:"apiServerIP"`
1727
}
1828

1929
func New(token string, database *database.APIServerDB) *slackbot {
@@ -39,9 +49,27 @@ func (s *slackbot) handleRegister(msg slack.Msg) string {
3949
err = s.database.AddClient(email, publicKey, serial)
4050
if err != nil {
4151
log.Errorf("adding client to database: %v", err)
42-
return "Something went wrong during registration :sweat_smile:, I've notified the nais device team for you."
52+
return "Something went wrong during registration :sweat_smile:, I've notified the nais device team for you. (1)"
4353
} else {
44-
return "Successfully registered :partyparrot:"
54+
c, err := s.database.ReadControlPlanePeer(serial)
55+
if err != nil {
56+
return "Something went wrong during registration :sweat_smile:, I've notified the nais device team for you. (2)"
57+
}
58+
59+
ec := EnrollmentConfig{
60+
ClientIP: c.IP,
61+
PublicKey: c.PublicKey,
62+
Endpoint: s.controlPlaneEndpoint,
63+
APIServerIP: "10.255.240.1",
64+
}
65+
66+
b, err := json.Marshal(&ec)
67+
if err != nil {
68+
return "Something went wrong during registration :sweat_smile:, I've notified the nais device team for you. (3)"
69+
}
70+
71+
token := base64.StdEncoding.EncodeToString(b)
72+
return fmt.Sprintf("Successfully registered :partyparrot:, copy and paste this command on your command line: `sudo tee /usr/local/etc/nais-device/enrollment.token <<< '%s'`", token)
4573
}
4674
}
4775

cmd/apiserver/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func init() {
2727
flag.StringVar(&cfg.SlackToken, "slack-token", os.Getenv("SLACK_TOKEN"), "Slack token")
2828
flag.StringVar(&cfg.BindAddress, "bind-address", cfg.BindAddress, "Bind address")
2929
flag.StringVar(&cfg.ConfigDir, "config-dir", cfg.ConfigDir, "Path to configuration directory")
30+
flag.StringVar(&cfg.ControlPlaneEndpoint, "control-plane-endpoint", cfg.ControlPlaneEndpoint, "Control Plane public endpoint (ip:port)")
3031
flag.BoolVar(&cfg.SkipSetupInterface, "skip-setup-interface", cfg.SkipSetupInterface, "Skip setting up WireGuard control plane interface")
3132

3233
flag.Parse()

0 commit comments

Comments
 (0)