Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions cmd/gitops-repo-broker/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/weaveworks/weave-gitops-enterprise/pkg/utilities/healthcheck"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)
Expand Down Expand Up @@ -118,21 +119,27 @@ func NewServer(ctx context.Context, c client.Client, entitlementSecretKey client

r := mux.NewRouter()

r.HandleFunc("/gitops/api/agent.yaml", agent.NewGetHandler(
db, params.AgentTemplateNatsURL, params.AgentTemplateAlertmanagerURL)).Methods("GET")
r.HandleFunc("/gitops/api/clusters", api.ListClusters(db, json.MarshalIndent)).Methods("GET")
r.HandleFunc("/gitops/api/clusters/{id:[0-9]+}", api.FindCluster(db, json.MarshalIndent)).Methods("GET")
r.HandleFunc("/gitops/api/clusters", api.RegisterCluster(db, validator.New(), json.Unmarshal, json.MarshalIndent, utils.Generate)).Methods("POST")
r.HandleFunc("/gitops/api/clusters/{id:[0-9]+}", api.UpdateCluster(db, validator.New(), json.Unmarshal, json.MarshalIndent)).Methods("PUT")
r.HandleFunc("/gitops/api/clusters/{id:[0-9]+}", api.UnregisterCluster(db)).Methods("DELETE")
r.HandleFunc("/gitops/api/alerts", api.ListAlerts(db, json.MarshalIndent)).Methods("GET")
entitled := r.PathPrefix("/gitops/api").Subrouter()
// entitlementMiddleware adds the entitlement in the request context so
// it needs to be added before checkEntitlementMiddleware which reads from
// the request context.
entitled.Use(entitlementMiddleware(ctx, log, c, entitlementSecretKey))
entitled.Use(checkEntitlementMiddleware(log))

entitled.HandleFunc("/agent.yaml", agent.NewGetHandler(db, params.AgentTemplateNatsURL, params.AgentTemplateAlertmanagerURL)).Methods("GET")
entitled.HandleFunc("/clusters", api.ListClusters(db, json.MarshalIndent)).Methods("GET")
entitled.HandleFunc("/clusters/{id:[0-9]+}", api.FindCluster(db, json.MarshalIndent)).Methods("GET")
entitled.HandleFunc("/clusters", api.RegisterCluster(db, validator.New(), json.Unmarshal, json.MarshalIndent, utils.Generate)).Methods("POST")
entitled.HandleFunc("/clusters/{id:[0-9]+}", api.UpdateCluster(db, validator.New(), json.Unmarshal, json.MarshalIndent)).Methods("PUT")
entitled.HandleFunc("/clusters/{id:[0-9]+}", api.UnregisterCluster(db)).Methods("DELETE")
entitled.HandleFunc("/alerts", api.ListAlerts(db, json.MarshalIndent)).Methods("GET")

r.HandleFunc("/gitops/started", healthcheck.Started(started))
r.HandleFunc("/gitops/healthz", healthcheck.Healthz(started))
r.HandleFunc("/gitops/redirect", healthcheck.Redirect)

srv := &http.Server{
Handler: entitlement.EntitlementHandler(ctx, log, c, entitlementSecretKey, entitlement.CheckEntitlementHandler(log, r)),
Handler: r,
Addr: fmt.Sprintf("0.0.0.0:%s", params.Port),
// Good practice: enforce timeouts for servers you create!
WriteTimeout: params.HttpWriteTimeout,
Expand All @@ -141,3 +148,15 @@ func NewServer(ctx context.Context, c client.Client, entitlementSecretKey client

return srv, nil
}

func entitlementMiddleware(ctx context.Context, log logr.Logger, c client.Client, key types.NamespacedName) mux.MiddlewareFunc {
return func(next http.Handler) http.Handler {
return entitlement.EntitlementHandler(ctx, log, c, key, next)
}
}

func checkEntitlementMiddleware(log logr.Logger) mux.MiddlewareFunc {
return func(next http.Handler) http.Handler {
return entitlement.CheckEntitlementHandler(log, next)
}
}
21 changes: 20 additions & 1 deletion cmd/gitops-repo-broker/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package server_test

import (
"context"
"io/ioutil"
"net/http"
"os"
"testing"
"time"

"github.com/go-logr/logr"
"github.com/weaveworks/weave-gitops-enterprise/cmd/gitops-repo-broker/server"
dbutils "github.com/weaveworks/weave-gitops-enterprise/common/database/utils"
"github.com/weaveworks/weave-gitops-enterprise/test/utils"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -37,8 +40,24 @@ func TestEntitlementMiddleware(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()

file, err := ioutil.TempFile("", "db")
if err != nil {
t.Fatalf("expected no errors but got: %v", err)
}
defer os.Remove(file.Name())

db, err := dbutils.Open(file.Name(), "sqlite", "", "", "")
if err != nil {
t.Fatalf("expected no errors but got: %v", err)
}
err = dbutils.MigrateTables(db)
if err != nil {
t.Fatalf("expected no errors but got: %v", err)
}

s, err := server.NewServer(ctx, tt.client, client.ObjectKey{Name: "name", Namespace: "namespace"}, logr.Discard(), server.ParamSet{
DbType: "sqlite",
DbURI: file.Name(),
Port: "8001",
})
if err != nil {
Expand All @@ -51,7 +70,7 @@ func TestEntitlementMiddleware(t *testing.T) {
}()

time.Sleep(100 * time.Millisecond)
res, err := http.Get("http://localhost:8001/gitops/healthz")
res, err := http.Get("http://localhost:8001/gitops/api/clusters")
if err != nil {
t.Fatalf("expected no errors but got: %v", err)
}
Expand Down