Skip to content

Commit

Permalink
Github secrets support
Browse files Browse the repository at this point in the history
  • Loading branch information
ramzes642 committed Jul 3, 2021
1 parent 5f7e528 commit d57f5b2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ Edit config **/etc/mini-deployer.json** as you need
### Configuration sample:
```json5
{
"cert": "",
"key": "",
"cert": "/etc/ssl/site.crt",
"key": "/etc/ssl/site.key",
"commands": {
"micro": "cd /var/www/micro && git pull"
},
Expand All @@ -76,6 +76,7 @@ Edit config **/etc/mini-deployer.json** as you need
"log": "",
"disable_autoreload": false,
"gitlab_token": "",
"github_secret": "",
"timeout": 120
}
```
Expand All @@ -85,6 +86,7 @@ Edit config **/etc/mini-deployer.json** as you need
* log - path to logfile (if you leave it empty, as described in service file - logs will be in syslog)
* disable_autoreload - disable autoreload feature (use curl localhost:7654/reload to do it manually)
* gitlab_token - Instead of using whitelist ips you may bypass it using gitlab_token config flag equal to "Secret token" from gitlab webhook configuration
* github_secret - Or use GitHub secret
* timeout - how many seconds to wait until process kill (default 10 seconds)


Expand Down
3 changes: 2 additions & 1 deletion config.sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"::1/128"
],
"disable_autoreload": false,
"gitlab_token": ""
"gitlab_token": "",
"github_secret": ""
}
26 changes: 24 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package main

import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
Expand All @@ -27,6 +30,7 @@ type configFile struct {
Timeout int64 `json:"timeout"`
DisableAutoreload bool `json:"disable_autoreload"`
GitlabToken string `json:"gitlab_token"`
GithubSecret string `json:"github_secret"`
}

var Cfg configFile
Expand Down Expand Up @@ -116,7 +120,10 @@ func ReadConfig() error {
func RunHttp() {
for {
port := strings.Split(*listen, ":")[1]
fmt.Printf("Deployer started\n# curl http://localhost:%s/reload\t to manual reload\n\n", port)
fmt.Printf("Deployer started\n")
if Cfg.DisableAutoreload {
fmt.Printf("# curl http://localhost:%s/reload to manual reload\n", port)
}
mux := http.NewServeMux()

srv := &http.Server{Addr: *listen, Handler: logRequest(mux), ErrorLog: log.Default()}
Expand Down Expand Up @@ -155,7 +162,7 @@ func registerHandlers(srv *http.Server, mux *http.ServeMux) {
writer.WriteHeader(500)
wr.Write([]byte(fmt.Sprintf("run err: %s", err)))
}
done := make(chan error)
done := make(chan error, 1)
go func() { done <- c.Wait() }()

// Start a timer
Expand Down Expand Up @@ -209,10 +216,25 @@ func registerHandlers(srv *http.Server, mux *http.ServeMux) {
}
}

func checkGithubSig(secret string, header string, body []byte) bool {
s := hmac.New(sha256.New, []byte(secret))
s.Write(body)
hash := "sha256=" + hex.EncodeToString(s.Sum(nil))
return hash == header
}

func checkWhitelist(addr string, req *http.Request) bool {
if Cfg.GitlabToken != "" && req.Header.Get("X-Gitlab-Token") == Cfg.GitlabToken {
return true
}
if Cfg.GithubSecret != "" && req.Header.Get("X-Hub-Signature-256") != "" {
post, e := ioutil.ReadAll(req.Body)
if e != nil {
log.Printf("github post read error")
return false
}
return checkGithubSig(Cfg.GithubSecret, req.Header.Get("X-Hub-Signature-256"), post)
}

addrParts := strings.Split(addr, ":")
addr = strings.Join(addrParts[0:len(addrParts)-1], ":")
Expand Down
15 changes: 15 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import "testing"

var testBody = `payload`

func TestGithubSecret(t *testing.T) {

if checkGithubSig("123", "sha256=5908ccfcc78e69944fd954f569473d5cf65ad2a9dc52056fea7e814b133dbad2", []byte(testBody)) {
t.Logf("Github token check ok")
} else {
t.Fatalf("Github sig check failed")
}

}

0 comments on commit d57f5b2

Please sign in to comment.