Skip to content

Commit

Permalink
Changes from code review
Browse files Browse the repository at this point in the history
* Use a different namespace for the `--verify` integration test
* Rate limit the number of requests to verify endpoint based on remote IP address
  • Loading branch information
wjam committed Nov 3, 2018
1 parent b931cbc commit adfe747
Show file tree
Hide file tree
Showing 14 changed files with 863 additions and 3 deletions.
26 changes: 24 additions & 2 deletions cmd/controller/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"time"

flag "github.com/spf13/pflag"
"github.com/throttled/throttled"
"github.com/throttled/throttled/store/memstore"
certUtil "k8s.io/client-go/util/cert"
)

Expand All @@ -24,14 +26,16 @@ type certProvider func() ([]*x509.Certificate, error)
type secretChecker func([]byte) (bool, error)

func httpserver(cp certProvider, sc secretChecker) {
httpRateLimiter := rateLimter()

mux := http.NewServeMux()

mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
io.WriteString(w, "ok\n")
})

mux.HandleFunc("/v1/verify", func(w http.ResponseWriter, r *http.Request) {
mux.Handle("/v1/verify", httpRateLimiter.RateLimit(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
content, err := ioutil.ReadAll(r.Body)

if err != nil {
Expand All @@ -54,7 +58,7 @@ func httpserver(cp certProvider, sc secretChecker) {
w.WriteHeader(http.StatusConflict)
}

})
})))

mux.HandleFunc("/v1/cert.pem", func(w http.ResponseWriter, r *http.Request) {
certs, err := cp()
Expand Down Expand Up @@ -84,3 +88,21 @@ func httpserver(cp certProvider, sc secretChecker) {
err := server.ListenAndServe()
log.Printf("HTTP server exiting: %v", err)
}

func rateLimter() throttled.HTTPRateLimiter {
store, err := memstore.New(65536)
if err != nil {
log.Fatal(err)
}

quota := throttled.RateQuota{MaxRate: throttled.PerSec(2), MaxBurst: 2}
rateLimiter, err := throttled.NewGCRARateLimiter(store, quota)
if err != nil {
log.Fatal(err)
}
return throttled.HTTPRateLimiter{
RateLimiter: rateLimiter,
VaryBy: &throttled.VaryBy{Path: true, Headers: []string{"X-Forwarded-For"}},
}

}
2 changes: 1 addition & 1 deletion integration/kubeseal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ var _ = Describe("kubeseal --version", func() {
var _ = Describe("kubeseal --verify", func() {
var c corev1.CoreV1Interface
const secretName = "testSecret"
const testNs = "testns"
const testNs = "testverifyns"
var input io.Reader
var output *bytes.Buffer
var ss *ssv1alpha1.SealedSecret
Expand Down
34 changes: 34 additions & 0 deletions vendor/github.com/throttled/throttled/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions vendor/github.com/throttled/throttled/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions vendor/github.com/throttled/throttled/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 76 additions & 0 deletions vendor/github.com/throttled/throttled/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions vendor/github.com/throttled/throttled/deprecated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/github.com/throttled/throttled/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit adfe747

Please sign in to comment.