Skip to content

Commit

Permalink
fix: Add paths to sqa middleware (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Feb 1, 2020
1 parent 10c45f2 commit 130c9c2
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 38 deletions.
38 changes: 29 additions & 9 deletions cmd/daemon/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package daemon

import (
"net/http"
"strings"
"sync"

"github.com/sirupsen/logrus"
Expand All @@ -17,10 +18,15 @@ import (
"github.com/ory/x/metricsx"

"github.com/ory/kratos/driver"
"github.com/ory/kratos/identity"
"github.com/ory/kratos/selfservice/errorx"
"github.com/ory/kratos/selfservice/flow/login"
"github.com/ory/kratos/selfservice/flow/logout"
"github.com/ory/kratos/selfservice/flow/profile"
"github.com/ory/kratos/selfservice/flow/registration"
"github.com/ory/kratos/selfservice/strategy/oidc"
"github.com/ory/kratos/selfservice/strategy/password"
"github.com/ory/kratos/session"
"github.com/ory/kratos/x"
)

Expand All @@ -32,8 +38,6 @@ func servePublic(d driver.Driver, wg *sync.WaitGroup, cmd *cobra.Command, args [
n := negroni.New()
r := d.Registry()

telemetry(cmd, n, d)

router := x.NewRouterPublic()
r.LoginHandler().RegisterPublicRoutes(router)
r.RegistrationHandler().RegisterPublicRoutes(router)
Expand All @@ -47,6 +51,8 @@ func servePublic(d driver.Driver, wg *sync.WaitGroup, cmd *cobra.Command, args [
r.HealthHandler().SetRoutes(router.Router, false)

n.Use(NewNegroniLoggerMiddleware(l.(*logrus.Logger), "public#"+c.SelfPublicURL().String()))
n.Use(sqa(cmd, d))

r.WithCSRFHandler(x.NewCSRFHandler(
router,
r.Writer(),
Expand Down Expand Up @@ -84,10 +90,10 @@ func serveAdmin(d driver.Driver, wg *sync.WaitGroup, cmd *cobra.Command, args []
r.ProfileManagementHandler().RegisterAdminRoutes(router)
r.IdentityHandler().RegisterAdminRoutes(router)
r.SessionHandler().RegisterAdminRoutes(router)
r.HealthHandler().SetRoutes(router.Router, false)
r.HealthHandler().SetRoutes(router.Router, true)

n.Use(NewNegroniLoggerMiddleware(l.(*logrus.Logger), "admin#"+c.SelfAdminURL().String()))
telemetry(cmd, n, d)
n.Use(sqa(cmd, d))

n.UseHandler(router)
server := graceful.WithDefaults(&http.Server{
Expand All @@ -102,13 +108,20 @@ func serveAdmin(d driver.Driver, wg *sync.WaitGroup, cmd *cobra.Command, args []
l.Println("Admin httpd was shutdown gracefully")
}

func telemetry(cmd *cobra.Command, n *negroni.Negroni, d driver.Driver) {
m := metricsx.New(
func sqa(cmd *cobra.Command, d driver.Driver) *metricsx.Service {
// Creates only one instance
return metricsx.New(
cmd,
d.Logger(),
&metricsx.Options{
Service: "ory-kratos",
ClusterID: metricsx.Hash(d.Configuration().DSN()),
Service: "ory-kratos",
ClusterID: metricsx.Hash(
strings.Join([]string{
d.Configuration().DSN(),
d.Configuration().SelfPublicURL().String(),
d.Configuration().SelfAdminURL().String(),
}, "|"),
),
IsDevelopment: flagx.MustGetBool(cmd, "dev"),
WriteKey: "qQlI6q8Q4WvkzTjKQSor4sHYOikHIvvi",
WhitelistedPaths: []string{
Expand All @@ -119,18 +132,25 @@ func telemetry(cmd *cobra.Command, n *negroni.Negroni, d driver.Driver) {
"/auth/methods/oidc/",
password.RegistrationPath,
password.LoginPath,
oidc.BasePath,
login.BrowserLoginPath,
login.BrowserLoginRequestsPath,
logout.BrowserLogoutPath,
registration.BrowserRegistrationPath,
registration.BrowserRegistrationRequestsPath,
session.SessionsWhoamiPath,
identity.IdentitiesPath,
profile.PublicProfileManagementPath,
profile.AdminBrowserProfileRequestPath,
profile.PublicProfileManagementPath,
profile.PublicProfileManagementUpdatePath,
errorx.ErrorsPath,
},
BuildVersion: d.Registry().BuildVersion(),
BuildHash: d.Registry().BuildHash(),
BuildTime: d.Registry().BuildDate(),
},
)
n.Use(m)
}

func ServeAll(d driver.Driver) func(cmd *cobra.Command, args []string) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ require (
github.com/ory/gojsonschema v1.2.0
github.com/ory/graceful v0.1.1
github.com/ory/herodot v0.6.3
github.com/ory/sdk/swagutil v0.0.0-20200131083057-53fe3c2ddf8a
github.com/ory/sdk/swagutil v0.0.0-20200131170418-ead0c2285f93
github.com/ory/viper v1.5.6
github.com/ory/x v0.0.93
github.com/pelletier/go-toml v1.6.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,8 @@ github.com/ory/sdk/swagutil v0.0.0-20200130190227-e1f6d26486c5 h1:sqkb7LZ5dg+zgD
github.com/ory/sdk/swagutil v0.0.0-20200130190227-e1f6d26486c5/go.mod h1:Ufg1eAyz+Zt3+oweSZVThG13ewewWCKwBmoNmK8Z0co=
github.com/ory/sdk/swagutil v0.0.0-20200131083057-53fe3c2ddf8a h1:bXsrzEDh1BACRHydHOhvhfxJ8b4W/cPMMbTQlwvWRyE=
github.com/ory/sdk/swagutil v0.0.0-20200131083057-53fe3c2ddf8a/go.mod h1:Ufg1eAyz+Zt3+oweSZVThG13ewewWCKwBmoNmK8Z0co=
github.com/ory/sdk/swagutil v0.0.0-20200131170418-ead0c2285f93 h1:+QLNv/tFJbwIpJ7fYGx07G0+IxO8FhhyaVGVkX6RAFM=
github.com/ory/sdk/swagutil v0.0.0-20200131170418-ead0c2285f93/go.mod h1:Ufg1eAyz+Zt3+oweSZVThG13ewewWCKwBmoNmK8Z0co=
github.com/ory/viper v1.5.6 h1:w4ceGgWwWLzAFYQ7bHaDZmwNsAto2JPVdyQjQnn7VWI=
github.com/ory/viper v1.5.6/go.mod h1:TYmpFpKLxjQwvT4f0QPpkOn4sDXU1kDgAwJpgLYiQ28=
github.com/ory/x v0.0.84 h1:foL2GSeL9yXBsEU14WcdX95H4Z0TmyodAJwdWkzvtHI=
Expand Down
12 changes: 7 additions & 5 deletions identity/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/ory/kratos/x"
)

const IdentitiesPath = "/identities"

type (
handlerDependencies interface {
PoolProvider
Expand All @@ -42,12 +44,12 @@ func NewHandler(
}

func (h *Handler) RegisterAdminRoutes(admin *x.RouterAdmin) {
admin.GET("/identities", h.list)
admin.GET("/identities/:id", h.get)
admin.DELETE("/identities/:id", h.delete)
admin.GET(IdentitiesPath, h.list)
admin.GET(IdentitiesPath+"/:id", h.get)
admin.DELETE(IdentitiesPath+"/:id", h.delete)

admin.POST("/identities", h.create)
admin.PUT("/identities/:id", h.update)
admin.POST(IdentitiesPath, h.create)
admin.PUT(IdentitiesPath+"/:id", h.update)
}

// A single identity.
Expand Down
22 changes: 0 additions & 22 deletions internal/httpclient/models/login_request_method.go

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

22 changes: 22 additions & 0 deletions internal/httpclient/models/registration_request_method.go

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

4 changes: 3 additions & 1 deletion selfservice/errorx/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/ory/kratos/x"
)

const ErrorsPath = "/errors"

type (
handlerDependencies interface {
x.WriterProvider
Expand All @@ -28,7 +30,7 @@ func NewHandler(
}

func (h *Handler) RegisterPublicRoutes(public *x.RouterPublic) {
public.GET("/errors", h.get)
public.GET(ErrorsPath, h.get)
}

func (h *Handler) get(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Expand Down

0 comments on commit 130c9c2

Please sign in to comment.