Skip to content

Commit 8901e35

Browse files
committed
add minimal slack support
1 parent 5cc3d34 commit 8901e35

File tree

6 files changed

+108
-9
lines changed

6 files changed

+108
-9
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea/
22
bin
3+
.env

apiserver/config/config.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package config
22

33
type Config struct {
4-
DbConnURI string
4+
DbConnURI string
5+
SlackToken string
6+
BindAddress string
57
}
68

79
func DefaultConfig() Config {

apiserver/slack/slack.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package slack
2+
3+
import (
4+
"fmt"
5+
"github.com/nais/device/apiserver/database"
6+
log "github.com/sirupsen/logrus"
7+
"github.com/slack-go/slack"
8+
"strings"
9+
)
10+
11+
type slackbot struct {
12+
api *slack.Client
13+
database database.APIServerDB
14+
}
15+
16+
func New(token string, database database.APIServerDB) *slackbot {
17+
return &slackbot{
18+
api: slack.New(token),
19+
database: database,
20+
}
21+
}
22+
23+
func (s *slackbot) registrationSlackHandler() {
24+
log.SetLevel(log.DebugLevel)
25+
rtm := s.api.NewRTM()
26+
27+
go rtm.ManageConnection()
28+
29+
for message := range rtm.IncomingEvents {
30+
log.Debugf("received rtm msg: %v", message)
31+
switch ev := message.Data.(type) {
32+
case *slack.MessageEvent:
33+
msg := ev.Msg
34+
35+
if msg.SubType != "" {
36+
break
37+
}
38+
39+
log.Debugf("%v", msg.User)
40+
publicKey, serial, err := parseRegisterMessage(msg.Text)
41+
if err != nil {
42+
log.Errorf("parsing message: %v", err)
43+
}
44+
45+
email, err := s.getUserEmail(msg.User)
46+
if err != nil {
47+
log.Errorf("getting user email: %v", err)
48+
}
49+
50+
log.Infof("email: %v, publicKey: %v, serial: %v", email, publicKey, serial)
51+
52+
err = s.database.AddClient(msg.Username, publicKey, serial)
53+
if err != nil {
54+
log.Errorf("adding client to database: %v", err)
55+
rtm.SendMessage(rtm.NewOutgoingMessage("Something went wrong during registration :sweat_smile:, I've notified the nais device team for you.", msg.Channel))
56+
} else {
57+
rtm.SendMessage(rtm.NewOutgoingMessage("Successfully registered :partyparrot:", msg.Channel))
58+
}
59+
60+
61+
case *slack.InvalidAuthEvent:
62+
log.Fatalf("slack auth failed: %v", message)
63+
}
64+
}
65+
}
66+
67+
func parseRegisterMessage(text string) (string, string, error) {
68+
// "register publicKey serial"
69+
parts := strings.Split(text, " ")
70+
publicKey, serial := parts[1], parts[2]
71+
return publicKey, serial, nil
72+
}
73+
74+
func (s *slackbot) getUserEmail(userID string) (string, error) {
75+
if info, err := s.api.GetUserInfo(userID); err != nil {
76+
return "", fmt.Errorf("getting user info: %w", err)
77+
} else {
78+
return info.Profile.Email, nil
79+
}
80+
}
81+
82+
func (s *slackbot) Run() {
83+
go s.registrationSlackHandler()
84+
}

cmd/apiserver/main.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/nais/device/apiserver/api"
99
"github.com/nais/device/apiserver/config"
1010
"github.com/nais/device/apiserver/database"
11+
"github.com/nais/device/apiserver/slack"
1112
log "github.com/sirupsen/logrus"
1213
flag "github.com/spf13/pflag"
1314
)
@@ -19,6 +20,8 @@ var (
1920
func init() {
2021
log.SetFormatter(&log.JSONFormatter{})
2122
flag.StringVar(&cfg.DbConnURI, "db-connection-uri", os.Getenv("DB_CONNECTION_URI"), "database connection URI (DSN)")
23+
flag.StringVar(&cfg.SlackToken, "slack-token", os.Getenv("SLACK_TOKEN"), "Slack token")
24+
flag.StringVar(&cfg.BindAddress, "bind-address", "10.255.240.1:80", "Bind address")
2225

2326
flag.Parse()
2427
}
@@ -30,9 +33,12 @@ func main() {
3033
panic(fmt.Sprintf("instantiating database: %s", err))
3134
}
3235

36+
37+
slack:= slack.New(cfg.SlackToken, db)
38+
slack.Run()
39+
3340
router := api.New(api.Config{DB: db})
3441

35-
bindAddr := fmt.Sprintf("%s:%d", "10.255.240.1", 80)
36-
fmt.Println("running @", bindAddr)
37-
fmt.Println(http.ListenAndServe(bindAddr, router))
42+
fmt.Println("running @", cfg.BindAddress)
43+
fmt.Println(http.ListenAndServe(cfg.BindAddress, router))
3844
}

go.mod

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ require (
77
github.com/jackc/pgx/v4 v4.5.0
88
github.com/lib/pq v1.3.0 // indirect
99
github.com/sirupsen/logrus v1.4.2
10+
github.com/slack-go/slack v0.6.3
1011
github.com/spf13/pflag v1.0.5
1112
github.com/stretchr/testify v1.5.1
12-
golang.zx2c4.com/wireguard v0.0.20200320
13+
golang.org/x/crypto v0.0.0-20191002192127-34f69633bfdc // indirect
14+
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 // indirect
1315
)

go.sum

+8-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
99
github.com/go-chi/chi v4.0.4+incompatible h1:7fVnpr0gAXG15uDbtH+LwSeMztvIvlHrBNRkTzgphS0=
1010
github.com/go-chi/chi v4.0.4+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
1111
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
12+
github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
13+
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
1214
github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE=
1315
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
16+
github.com/gorilla/websocket v1.2.0 h1:VJtLvh6VQym50czpZzx07z/kw9EgAxI3x1ZB8taTMQQ=
17+
github.com/gorilla/websocket v1.2.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
1418
github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0=
1519
github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=
1620
github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
@@ -71,6 +75,8 @@ github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd
7175
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
7276
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
7377
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
78+
github.com/nlopes/slack v0.6.0/go.mod h1:JzQ9m3PMAqcpeCam7UaHSuBuupz7CmpjehYMayT6YOk=
79+
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
7480
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
7581
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
7682
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -84,6 +90,8 @@ github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9Nz
8490
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
8591
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
8692
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
93+
github.com/slack-go/slack v0.6.3 h1:qU037g8gQ71EuH6S9zYKnvYrEUj0fLFH4HFekFqBoRU=
94+
github.com/slack-go/slack v0.6.3/go.mod h1:HE4RwNe7YpOg/F0vqo5PwXH3Hki31TplTvKRW9dGGaw=
8795
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
8896
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
8997
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@@ -111,8 +119,6 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn
111119
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
112120
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
113121
golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
114-
golang.org/x/net v0.0.0-20191003171128-d98b1b443823 h1:Ypyv6BNJh07T1pUSrehkLemqPKXhus2MkfktJ91kRh4=
115-
golang.org/x/net v0.0.0-20191003171128-d98b1b443823/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
116122
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
117123
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
118124
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@@ -135,8 +141,6 @@ golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8T
135141
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
136142
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
137143
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
138-
golang.zx2c4.com/wireguard v0.0.20200320 h1:1vE6zVeO7fix9cJX1Z9ZQ+ikPIIx7vIyU0o0tLDD88g=
139-
golang.zx2c4.com/wireguard v0.0.20200320/go.mod h1:lDian4Sw4poJ04SgHh35nzMVwGSYlPumkdnHcucAQoY=
140144
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
141145
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
142146
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

0 commit comments

Comments
 (0)