From af21f90acd9f90ab73d1b29209ad12816905830c Mon Sep 17 00:00:00 2001 From: Alex Rakoczy Date: Wed, 3 Apr 2019 13:36:23 -0400 Subject: WHY ARE WE LIKE THIS --- .gcloudignore | 19 ++++++ .gitignore | 89 ++++++++++++++++++++++++++++ .go-version | 1 + LICENSE | 7 +++ README.md | 1 + frogslack.go | 132 +++++++++++++++++++++++++++++++++++++++++ frogslack_test.go | 172 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 12 ++++ go.sum | 21 +++++++ 9 files changed, 454 insertions(+) create mode 100644 .gcloudignore create mode 100644 .gitignore create mode 100644 .go-version create mode 100644 LICENSE create mode 100644 README.md create mode 100644 frogslack.go create mode 100644 frogslack_test.go create mode 100644 go.mod create mode 100644 go.sum diff --git a/.gcloudignore b/.gcloudignore new file mode 100644 index 0000000..3391f61 --- /dev/null +++ b/.gcloudignore @@ -0,0 +1,19 @@ +# This file specifies files that are *not* uploaded to Google Cloud Platform +# using gcloud. It follows the same syntax as .gitignore, with the addition of +# "#!include" directives (which insert the entries of the given .gitignore-style +# file at that point). +# +# For more information, run: +# $ gcloud topic gcloudignore +# +.gcloudignore +# If you would like to upload your .git directory, .gitignore file or files +# from your .gitignore file, remove the corresponding line +# below: +.git +.gitignore + +node_modules +#!include:.gitignore + +.idea diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5da98af --- /dev/null +++ b/.gitignore @@ -0,0 +1,89 @@ +.idea +.env +.env.yaml + + +# Autogenerated gitignores below + +# Google App Engine generated folder +appengine-generated/ + +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/modules.xml +# .idea/*.iml +# .idea/modules + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser diff --git a/.go-version b/.go-version new file mode 100644 index 0000000..e6dbb7c --- /dev/null +++ b/.go-version @@ -0,0 +1 @@ +1.11.5 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f3fb454 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2019 Alexander J Rakoczy + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..06405a5 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# frogslack \ No newline at end of file diff --git a/frogslack.go b/frogslack.go new file mode 100644 index 0000000..0a3f1aa --- /dev/null +++ b/frogslack.go @@ -0,0 +1,132 @@ +package frogslack + +import ( + "context" + "encoding/json" + "errors" + "io/ioutil" + "net/http" + "os" + + "github.com/golang/glog" + "github.com/nlopes/slack" +) + +const ( + RESPONSE_TYPE_IN_CHANNEL = "in_channel" + RESPONSE_TYPE_EPHEMERAL = "ephemeral" +) + +var ( + apiUrl = "https://frog.tips/api/1/tips" + signingSecret = os.Getenv("SLACK_SIGNING_SECRET_SHH") +) + +type Attachment struct { + Text string `json:"text"` +} + +type Response struct { + ResponseType string `json:"response_type"` + Text string `json:"text"` + Attachments []Attachment `json:"attachments,omitempty"` +} + +type Request struct { +} + +type TipsResponse struct { + Tips []Tip `json:"tips"` +} + +type Tip struct { + Tip string `json:"tip"` + Number int `json:"number"` +} + +func getTip(ctx context.Context) (Tip, error) { + var tr TipsResponse + var tip Tip + req, err := http.NewRequest(http.MethodGet, apiUrl, nil) + if err != nil { + return tip, err + } + req.WithContext(ctx) + resp, err := http.DefaultClient.Do(req) + if err != nil { + return tip, err + } + d := json.NewDecoder(resp.Body) + if err := d.Decode(&tr); err != nil { + return tip, err + } + if len(tr.Tips) == 0 { + return tip, errors.New("NOT ENOUGH TIPS") + } + return tr.Tips[0], err +} + +func Croak(w http.ResponseWriter, r *http.Request) { + ctx, cancel := context.WithCancel(r.Context()) + defer cancel() + + body, err := ioutil.ReadAll(r.Body) + if err != nil { + glog.Errorf("I will not do this anymore %q", err) + writeError(w, "SORRY, NO TIPS RIGHT NOW.") + return + } + + if err = verify(r.Header, body); err != nil { + writeError(w, "SORRY, NO TIPS RIGHT NOW.") + return + } + + tip, err := getTip(ctx) + if err != nil { + glog.Errorf("getTip(%v) = _, %q", ctx, err) + writeResponse(w, &Response{ + ResponseType: RESPONSE_TYPE_EPHEMERAL, + Text: "SORRY, NO TIPS RIGHT NOW. COME BACK.", + }) + return + } + + writeResponse(w, &Response{ + ResponseType: RESPONSE_TYPE_IN_CHANNEL, + Text: tip.Tip, + }) +} + +func verify(h http.Header, body []byte) error { + sv, err := slack.NewSecretsVerifier(h, signingSecret) + if err != nil { + glog.Errorf("Ohmy, no secrets? %q", err) + return err + } + _, err = sv.Write(body) + if err != nil { + glog.Errorf("forget it %q", err) + return err + } + if err = sv.Ensure(); err != nil { + glog.Errorf("I can't do this anymore %q", err) + return err + } + return nil +} + +func writeError(w http.ResponseWriter, message string) { + writeResponse(w, &Response{ + ResponseType: RESPONSE_TYPE_EPHEMERAL, + Text: message, + }) +} + +func writeResponse(w http.ResponseWriter, resp *Response) { + w.Header().Set("Content-Type", "application/json") + e := json.NewEncoder(w) + if err := e.Encode(resp); err != nil { + glog.Errorf("e.Encode(%#v) = _, %q", resp, err) + } +} diff --git a/frogslack_test.go b/frogslack_test.go new file mode 100644 index 0000000..47f8551 --- /dev/null +++ b/frogslack_test.go @@ -0,0 +1,172 @@ +package frogslack + +import ( + "crypto/hmac" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "fmt" + "log" + "net/http" + "net/http/httptest" + "strconv" + "strings" + "testing" + "time" + + "github.com/google/go-cmp/cmp" +) + +const ( + firstTip = "IF YOU MEET A FROG ON THE ROAD, RMA IT FOR CREDIT TOWARDS ANOTHER FROG." + testResponse = `{"tips":[ + {"tip":"IF YOU MEET A FROG ON THE ROAD, RMA IT FOR CREDIT TOWARDS ANOTHER FROG.","number":1015}, + {"tip":"DO NOT GAZE DIRECTLY AT FROG WITHOUT CLASS 3 EYE PROTECTION AND COMPLETION OF CERTIFICATE 254L3: \"DISASSEMBLING FROG FOR MAINTENANCE\".","number":2320} + ]}` + signatureHeader = "X-Slack-Signature" + timestampHeader = "X-Slack-Request-Timestamp" + fakeSecret = "e6b19c573432dcc6b075501d51b51bb8" +) + +func signature(t *testing.T, secret, body string, timestamp int64) string { + t.Helper() + h := hmac.New(sha256.New, []byte(secret)) + val := fmt.Sprintf("v0:%s:%s", strconv.FormatInt(timestamp, 10), body) + if _, err := h.Write([]byte(val)); err != nil { + t.Fatalf("Error generating signature: %q", err) + } + return fmt.Sprintf("v0=%s", hex.EncodeToString(h.Sum(nil))) +} + +func TestCroak(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(getTipsSuccess)) + defer ts.Close() + apiUrl = ts.URL + signingSecret = fakeSecret + + cases := []struct { + body string + want *Response + }{ + { + body: "token=barf", + want: &Response{ResponseType: "in_channel", Text: firstTip}, + }, + } + for _, c := range cases { + now := time.Now() + req := httptest.NewRequest("GET", "/", strings.NewReader(c.body)) + req.Header.Set(timestampHeader, strconv.FormatInt(now.Unix(), 10)) + req.Header.Set(signatureHeader, signature(t, fakeSecret, c.body, now.Unix())) + w := httptest.NewRecorder() + + Croak(w, req) + + ct := w.Result().Header.Get("Content-Type") + if ct != "application/json" { + t.Errorf("w.Result().Header.Get(%q) = %q, wanted %q", "Content-Type", ct, "application/json") + continue + } + d := json.NewDecoder(w.Result().Body) + got := &Response{} + if err := d.Decode(got); err != nil { + t.Errorf("d.Decode(%v) = %q, wanted no error", got, err) + continue + } + if diff := cmp.Diff(got, c.want); diff != "" { + t.Errorf("Croak(%q) returned diff (got, want)\n %v", c.body, diff) + } + } +} + +func TestCroakDeadBackend(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(getTipsSuccess)) + ts.Close() + apiUrl = ts.URL + want := &Response{ResponseType: RESPONSE_TYPE_EPHEMERAL, Text: "SORRY, NO TIPS RIGHT NOW. COME BACK."} + + now := time.Now() + req := httptest.NewRequest("GET", "/", strings.NewReader("welp")) + req.Header.Set(timestampHeader, strconv.FormatInt(now.Unix(), 10)) + req.Header.Set(signatureHeader, signature(t, fakeSecret, "welp", now.Unix())) + w := httptest.NewRecorder() + + Croak(w, req) + + ct := w.Result().Header.Get("Content-Type") + if ct != "application/json" { + t.Errorf("w.Result().Header.Get(%q) = %q, wanted %q", "Content-Type", ct, "application/json") + } + d := json.NewDecoder(w.Result().Body) + got := &Response{} + if err := d.Decode(got); err != nil { + t.Errorf("d.Decode(%v) = %q, wanted no error", got, err) + } + if diff := cmp.Diff(got, want); diff != "" { + t.Errorf("Croak(%q) returned diff (got, want)\n %v", "welp", diff) + } +} + +func TestCroakNoResponse(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) + defer ts.Close() + apiUrl = ts.URL + want := &Response{ResponseType: RESPONSE_TYPE_EPHEMERAL, Text: "SORRY, NO TIPS RIGHT NOW. COME BACK."} + + now := time.Now() + req := httptest.NewRequest("GET", "/", strings.NewReader("welp")) + req.Header.Set(timestampHeader, strconv.FormatInt(now.Unix(), 10)) + req.Header.Set(signatureHeader, signature(t, fakeSecret, "welp", now.Unix())) + w := httptest.NewRecorder() + + Croak(w, req) + + ct := w.Result().Header.Get("Content-Type") + if ct != "application/json" { + t.Errorf("w.Result().Header.Get(%q) = %q, wanted %q", "Content-Type", ct, "application/json") + } + d := json.NewDecoder(w.Result().Body) + got := &Response{} + if err := d.Decode(got); err != nil { + t.Errorf("d.Decode(%v) = %q, wanted no error", got, err) + } + if diff := cmp.Diff(got, want); diff != "" { + t.Errorf("Croak(%q) returned diff (got, want)\n %v", "welp", diff) + } +} + +func TestCroakAPIError(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusInternalServerError) + })) + defer ts.Close() + apiUrl = ts.URL + want := &Response{ResponseType: RESPONSE_TYPE_EPHEMERAL, Text: "SORRY, NO TIPS RIGHT NOW. COME BACK."} + + now := time.Now() + req := httptest.NewRequest("GET", "/", strings.NewReader("welp")) + req.Header.Set(timestampHeader, strconv.FormatInt(now.Unix(), 10)) + req.Header.Set(signatureHeader, signature(t, fakeSecret, "welp", now.Unix())) + w := httptest.NewRecorder() + + Croak(w, req) + + ct := w.Result().Header.Get("Content-Type") + if ct != "application/json" { + t.Errorf("w.Result().Header.Get(%q) = %q, wanted %q", "Content-Type", ct, "application/json") + } + d := json.NewDecoder(w.Result().Body) + got := &Response{} + if err := d.Decode(got); err != nil { + t.Errorf("d.Decode(%v) = %q, wanted no error", got, err) + } + if diff := cmp.Diff(got, want); diff != "" { + t.Errorf("Croak(%q) returned diff (got, want)\n %v", "welp", diff) + } +} + +func getTipsSuccess(w http.ResponseWriter, r *http.Request) { + if _, err := fmt.Fprint(w, testResponse); err != nil { + log.Fatalf("error in fake handler: %q", err) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..584f617 --- /dev/null +++ b/go.mod @@ -0,0 +1,12 @@ +module github.com/toothrot/frogslack + +require ( + github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b + github.com/google/go-cmp v0.2.0 + github.com/gorilla/websocket v1.4.0 // indirect + github.com/lusis/go-slackbot v0.0.0-20180109053408-401027ccfef5 // indirect + github.com/lusis/slack-test v0.0.0-20180109053238-3c758769bfa6 // indirect + github.com/nlopes/slack v0.5.0 + github.com/pkg/errors v0.8.1 // indirect + github.com/stretchr/testify v1.3.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..80e3568 --- /dev/null +++ b/go.sum @@ -0,0 +1,21 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= +github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= +github.com/lusis/go-slackbot v0.0.0-20180109053408-401027ccfef5 h1:AsEBgzv3DhuYHI/GiQh2HxvTP71HCCE9E/tzGUzGdtU= +github.com/lusis/go-slackbot v0.0.0-20180109053408-401027ccfef5/go.mod h1:c2mYKRyMb1BPkO5St0c/ps62L4S0W2NAkaTXj9qEI+0= +github.com/lusis/slack-test v0.0.0-20180109053238-3c758769bfa6 h1:iOAVXzZyXtW408TMYejlUPo6BIn92HmOacWtIfNyYns= +github.com/lusis/slack-test v0.0.0-20180109053238-3c758769bfa6/go.mod h1:sFlOUpQL1YcjhFVXhg1CG8ZASEs/Mf1oVb6H75JL/zg= +github.com/nlopes/slack v0.5.0 h1:NbIae8Kd0NpqaEI3iUrsuS0KbcEDhzhc939jLW5fNm0= +github.com/nlopes/slack v0.5.0/go.mod h1:jVI4BBK3lSktibKahxBF74txcK2vyvkza1z/+rRnVAM= +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -- cgit v1.2.3-73-g0e29