Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add missing tracing in manager_cookie.go #3615

Merged
merged 1 commit into from
Nov 10, 2023
Merged
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
42 changes: 28 additions & 14 deletions continuity/manager_cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ import (
"github.com/pkg/errors"

"github.com/ory/herodot"
"github.com/ory/x/otelx"
"github.com/ory/x/sqlcon"

"github.com/ory/kratos/session"
"github.com/ory/kratos/x"
)

var _ Manager = new(ManagerCookie)
var ErrNotResumable = *herodot.ErrBadRequest.WithError("no resumable session found").WithReasonf("The browser does not contain the necessary cookie to resume the session. This is a security violation and was blocked. Please clear your browser's cookies and cache and try again!")
var (
_ Manager = new(ManagerCookie)
ErrNotResumable = *herodot.ErrBadRequest.WithError("no resumable session found").WithReasonf("The browser does not contain the necessary cookie to resume the session. This is a security violation and was blocked. Please clear your browser's cookies and cache and try again!")
)

const CookieName = "ory_kratos_continuity"

Expand All @@ -29,6 +32,7 @@ type (
PersistenceProvider
x.CookieProvider
session.ManagementProvider
x.TracingProvider
}
ManagerCookie struct {
d managerCookieDependencies
Expand All @@ -39,7 +43,9 @@ func NewManagerCookie(d managerCookieDependencies) *ManagerCookie {
return &ManagerCookie{d: d}
}

func (m *ManagerCookie) Pause(ctx context.Context, w http.ResponseWriter, r *http.Request, name string, opts ...ManagerOption) error {
func (m *ManagerCookie) Pause(ctx context.Context, w http.ResponseWriter, r *http.Request, name string, opts ...ManagerOption) (err error) {
ctx, span := m.d.Tracer(ctx).Tracer().Start(ctx, "continuity.ManagerCookie.Pause")
defer otelx.End(span, &err)
if len(name) == 0 {
return errors.Errorf("continuity container name must be set")
}
Expand All @@ -63,8 +69,11 @@ func (m *ManagerCookie) Pause(ctx context.Context, w http.ResponseWriter, r *htt
return nil
}

func (m *ManagerCookie) Continue(ctx context.Context, w http.ResponseWriter, r *http.Request, name string, opts ...ManagerOption) (*Container, error) {
container, err := m.container(ctx, w, r, name)
func (m *ManagerCookie) Continue(ctx context.Context, w http.ResponseWriter, r *http.Request, name string, opts ...ManagerOption) (container *Container, err error) {
ctx, span := m.d.Tracer(ctx).Tracer().Start(ctx, "continuity.ManagerCookie.Continue")
defer otelx.End(span, &err)

container, err = m.container(ctx, w, r, name)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -95,21 +104,24 @@ func (m *ManagerCookie) Continue(ctx context.Context, w http.ResponseWriter, r *
return container, nil
}

func (m *ManagerCookie) sid(ctx context.Context, w http.ResponseWriter, r *http.Request, name string) (uuid.UUID, error) {
var sid uuid.UUID
if s, err := x.SessionGetString(r, m.d.ContinuityCookieManager(ctx), CookieName, name); err != nil {
func (m *ManagerCookie) sessionID(ctx context.Context, w http.ResponseWriter, r *http.Request, name string) (uuid.UUID, error) {
s, err := x.SessionGetString(r, m.d.ContinuityCookieManager(ctx), CookieName, name)
if err != nil {
_ = x.SessionUnsetKey(w, r, m.d.ContinuityCookieManager(ctx), CookieName, name)
return sid, errors.WithStack(ErrNotResumable.WithDebugf("%+v", err))
} else if sid = x.ParseUUID(s); sid == uuid.Nil {
return uuid.Nil, errors.WithStack(ErrNotResumable.WithDebugf("%+v", err))
}

sid, err := uuid.FromString(s)
if err != nil {
_ = x.SessionUnsetKey(w, r, m.d.ContinuityCookieManager(ctx), CookieName, name)
return sid, errors.WithStack(ErrNotResumable.WithDebug("session id is not a valid uuid"))
return uuid.Nil, errors.WithStack(ErrNotResumable.WithDebug("session id is not a valid uuid"))
}

return sid, nil
}

func (m *ManagerCookie) container(ctx context.Context, w http.ResponseWriter, r *http.Request, name string) (*Container, error) {
sid, err := m.sid(ctx, w, r, name)
sid, err := m.sessionID(ctx, w, r, name)
if err != nil {
return nil, err
}
Expand All @@ -129,8 +141,10 @@ func (m *ManagerCookie) container(ctx context.Context, w http.ResponseWriter, r
return container, err
}

func (m ManagerCookie) Abort(ctx context.Context, w http.ResponseWriter, r *http.Request, name string) error {
sid, err := m.sid(ctx, w, r, name)
func (m ManagerCookie) Abort(ctx context.Context, w http.ResponseWriter, r *http.Request, name string) (err error) {
ctx, span := m.d.Tracer(ctx).Tracer().Start(ctx, "continuity.ManagerCookie.Abort")
defer otelx.End(span, &err)
sid, err := m.sessionID(ctx, w, r, name)
if errors.Is(err, &ErrNotResumable) {
// We do not care about an error here
return nil
Expand Down