-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from UKHomeOffice/feature/197-restrict-access
197-restrict-access: Adds auth middleware
- Loading branch information
Showing
15 changed files
with
241 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
eyJhbGciOiJIUzI1NiJ9.cGlwZWxpbmU6CgogIG15LWJ1aWxkOgogICAgcHJpdmlsZWdlZDogdHJ1ZQogICAgaW1hZ2U6IGRvY2tlcjoxLjExCiAgICBlbnZpcm9ubWVudDoKICAgICAgLSBET0NLRVJfSE9TVD10Y3A6Ly8xMjcuMC4wLjE6MjM3NQogICAgY29tbWFuZHM6CiAgICAgIC0gZG9ja2VyIGJ1aWxkIC10IHJlcG8tc2VjdXJpdHktc2Nhbm5lciAuCiAgICB3aGVuOgogICAgICBldmVudDogcHVzaAoKICBpbWFnZV90b19xdWF5OgogICAgaW1hZ2U6IGRvY2tlcjoxLjExCiAgICBlbnZpcm9ubWVudDoKICAgICAgLSBET0NLRVJfSE9TVD10Y3A6Ly8xMjcuMC4wLjE6MjM3NQogICAgY29tbWFuZHM6CiAgICAgIC0gZG9ja2VyIGxvZ2luIC11PSJ1a2hvbWVvZmZpY2VkaWdpdGFsK3JlcG9fc2VjdXJpdHlfc2Nhbm5lcl9ib3QiIC1wPSR7RE9DS0VSX1BBU1NXT1JEfSBxdWF5LmlvCiAgICAgIC0gZG9ja2VyIHRhZyByZXBvLXNlY3VyaXR5LXNjYW5uZXIgcXVheS5pby91a2hvbWVvZmZpY2VkaWdpdGFsL3JlcG8tc2VjdXJpdHktc2Nhbm5lcj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ogICAgd2hlbjoKICAgICAgZXZlbnQ6IFtkZXBsb3ltZW50LCBwdXNoXQoKc2VydmljZXM6CiAgZGluZDoKICAgIGltYWdlOiBkb2NrZXI6MS4xMS1kaW5kCiAgICBwcml2aWxlZ2VkOiB0cnVlCiAgICBjb21tYW5kOgogICAgICAtICItcyIKICAgICAgLSAib3ZlcmxheSIK.fGU1SaQhWfl_kQlZaCoU_mVOh0Bl-OU5q4zeHWle19U |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export GITHUB_WEBHOOKSECRET=blah |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
# repo-security-scanner | ||
|
||
[Documentation on GoDoc](https://godoc.org/github.com/UKHomeOffice/repo-security-scanner). | ||
|
||
## Installation | ||
|
||
```make install``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
apiVersion: v1 | ||
kind: Secret | ||
metadata: | ||
name: secrets | ||
name: repo-security-scanner | ||
type: Opaque | ||
data: | ||
GITHUB_PRIVATEKEY: {{.GITHUB_PRIVATEKEY}} | ||
GITHUB_WEBHOOKSECRET: {{.GITHUB_WEBHOOKSECRET}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"crypto/hmac" | ||
"crypto/sha1" | ||
"encoding/hex" | ||
"errors" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
// Adapter defines a middleware handler | ||
type Adapter func(http.Handler) http.Handler | ||
|
||
// Adapt chains a series of middleware handlers | ||
func Adapt(h http.Handler, adapters ...Adapter) http.Handler { | ||
for _, adapter := range adapters { | ||
h = adapter(h) | ||
} | ||
return h | ||
} | ||
|
||
// Middleware wraps a function and returns an http.Handler fn type | ||
type Middleware func(http.Handler) http.Handler | ||
|
||
// Authenticator is an interface for authorizing streams | ||
type Authenticator interface { | ||
CheckMAC([]byte, []byte) (bool, error) | ||
} | ||
|
||
// GithubAuthenticator authorizes a payload with a shared secret | ||
type GithubAuthenticator struct { | ||
secret []byte | ||
} | ||
|
||
// CheckMAC checks text matches a HMAC signature | ||
func (g GithubAuthenticator) CheckMAC(body, expectedMAC []byte) (bool, error) { | ||
if len(g.secret) < 1 { | ||
return false, errors.New("secret not set") | ||
} | ||
mac := hmac.New(sha1.New, g.secret) | ||
mac.Write(body) | ||
actualMAC := []byte(hex.EncodeToString(mac.Sum(nil))) | ||
return hmac.Equal(expectedMAC, append([]byte("sha1="), actualMAC...)), nil | ||
} | ||
|
||
// AuthMiddleware authenticates a request body | ||
func AuthMiddleware(ag Authenticator) Adapter { | ||
return func(next http.Handler) http.Handler { | ||
fn := func(w http.ResponseWriter, r *http.Request) { | ||
if r.Header.Get(headerGithubEvt) != "push" { | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte(msgIgnore)) | ||
return | ||
} | ||
buf, err := ioutil.ReadAll(r.Body) | ||
rdr1 := ioutil.NopCloser(bytes.NewBuffer(buf)) | ||
r.Body = rdr1 | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte(http.StatusText(http.StatusInternalServerError))) | ||
return | ||
} | ||
authorized, err := ag.CheckMAC( | ||
buf, | ||
[]byte(r.Header.Get(headerGithubMAC)), | ||
) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte(http.StatusText(http.StatusInternalServerError))) | ||
return | ||
} | ||
if authorized != true { | ||
w.WriteHeader(http.StatusUnauthorized) | ||
w.Write([]byte(http.StatusText(http.StatusUnauthorized))) | ||
return | ||
} | ||
next.ServeHTTP(w, r) | ||
} | ||
return http.HandlerFunc(fn) | ||
} | ||
} |
Oops, something went wrong.