-
Notifications
You must be signed in to change notification settings - Fork 0
Add logging adapter for storage #39
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
Open
wagnerdevocelot
wants to merge
3
commits into
main
Choose a base branch
from
codex/integrate-new-adapter-with-existing-flows
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,232 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "log" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/ory/fosite" | ||
| "github.com/ory/fosite/handler/oauth2" | ||
| "github.com/ory/fosite/handler/openid" | ||
| "github.com/ory/fosite/handler/pkce" | ||
| ) | ||
|
|
||
| // LoggingAdapter wraps another StorageInterface and logs all operations. | ||
| // fullStorage groups all interfaces required by fosite's Compose plus our | ||
| // generic StorageInterface. Any real backend (memory or DB) should implement | ||
| // all of them. | ||
| type fullStorage interface { | ||
| fosite.Storage | ||
| openid.OpenIDConnectRequestStorage | ||
| oauth2.CoreStorage | ||
| oauth2.TokenRevocationStorage | ||
| pkce.PKCERequestStorage | ||
| StorageInterface | ||
| } | ||
|
|
||
| // LoggingAdapter wraps another storage backend and records metrics while | ||
| // delegating all operations to it. | ||
| type LoggingAdapter struct { | ||
| fullStorage | ||
| mu sync.Mutex | ||
| metrics map[string]int | ||
| } | ||
|
|
||
| // NewLoggingAdapter creates a new adapter with the given backend. | ||
| func NewLoggingAdapter(backend fullStorage) *LoggingAdapter { | ||
| return &LoggingAdapter{fullStorage: backend, metrics: make(map[string]int)} | ||
| } | ||
|
|
||
| func (l *LoggingAdapter) inc(key string) { | ||
| l.mu.Lock() | ||
| l.metrics[key]++ | ||
| l.mu.Unlock() | ||
| } | ||
|
|
||
| // Metrics returns a copy of collected metrics. | ||
| func (l *LoggingAdapter) Metrics() map[string]int { | ||
| l.mu.Lock() | ||
| defer l.mu.Unlock() | ||
| copy := make(map[string]int, len(l.metrics)) | ||
| for k, v := range l.metrics { | ||
| copy[k] = v | ||
| } | ||
| return copy | ||
| } | ||
|
|
||
| func (l *LoggingAdapter) GetClient(ctx context.Context, id string) (fosite.Client, error) { | ||
| c, err := l.fullStorage.GetClient(ctx, id) | ||
| if err != nil { | ||
| log.Printf("logging adapter: GetClient %s failed: %v", id, err) | ||
| l.inc("GetClientError") | ||
| } else { | ||
| l.inc("GetClient") | ||
| } | ||
| return c, err | ||
| } | ||
|
|
||
| func (l *LoggingAdapter) CreateClient(ctx context.Context, client fosite.Client) error { | ||
| err := l.fullStorage.CreateClient(ctx, client) | ||
| if err != nil { | ||
| log.Printf("logging adapter: CreateClient %s failed: %v", client.GetID(), err) | ||
| l.inc("CreateClientError") | ||
| } else { | ||
| l.inc("CreateClient") | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| func (l *LoggingAdapter) UpdateClient(ctx context.Context, client fosite.Client) error { | ||
| err := l.fullStorage.UpdateClient(ctx, client) | ||
| if err != nil { | ||
| log.Printf("logging adapter: UpdateClient %s failed: %v", client.GetID(), err) | ||
| l.inc("UpdateClientError") | ||
| } else { | ||
| l.inc("UpdateClient") | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| func (l *LoggingAdapter) DeleteClient(ctx context.Context, id string) error { | ||
| err := l.fullStorage.DeleteClient(ctx, id) | ||
| if err != nil { | ||
| log.Printf("logging adapter: DeleteClient %s failed: %v", id, err) | ||
| l.inc("DeleteClientError") | ||
| } else { | ||
| l.inc("DeleteClient") | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| func (l *LoggingAdapter) CreateToken(ctx context.Context, tokenType, signature, clientID string, data interface{}) error { | ||
| err := l.fullStorage.CreateToken(ctx, tokenType, signature, clientID, data) | ||
| if err != nil { | ||
| log.Printf("logging adapter: CreateToken %s failed: %v", tokenType, err) | ||
wagnerdevocelot marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| l.inc("CreateTokenError") | ||
| } else { | ||
| l.inc("CreateToken") | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| func (l *LoggingAdapter) GetToken(ctx context.Context, tokenType, signature string) (interface{}, error) { | ||
| v, err := l.fullStorage.GetToken(ctx, tokenType, signature) | ||
| if err != nil { | ||
| log.Printf("logging adapter: GetToken %s failed: %v", tokenType, err) | ||
| l.inc("GetTokenError") | ||
| } else { | ||
| l.inc("GetToken") | ||
| } | ||
| return v, err | ||
| } | ||
|
|
||
| func (l *LoggingAdapter) DeleteToken(ctx context.Context, tokenType, signature string) error { | ||
| err := l.fullStorage.DeleteToken(ctx, tokenType, signature) | ||
| if err != nil { | ||
| log.Printf("logging adapter: DeleteToken %s failed: %v", tokenType, err) | ||
| l.inc("DeleteTokenError") | ||
| } else { | ||
| l.inc("DeleteToken") | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| func (l *LoggingAdapter) RevokeToken(ctx context.Context, tokenType, signature string) error { | ||
| err := l.fullStorage.RevokeToken(ctx, tokenType, signature) | ||
| if err != nil { | ||
| log.Printf("logging adapter: RevokeToken %s failed: %v", tokenType, err) | ||
| l.inc("RevokeTokenError") | ||
| } else { | ||
| l.inc("RevokeToken") | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| func (l *LoggingAdapter) CreateSession(ctx context.Context, sessionType, id string, data interface{}) error { | ||
| err := l.fullStorage.CreateSession(ctx, sessionType, id, data) | ||
| if err != nil { | ||
| log.Printf("logging adapter: CreateSession %s failed: %v", sessionType, err) | ||
| l.inc("CreateSessionError") | ||
| } else { | ||
| l.inc("CreateSession") | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| func (l *LoggingAdapter) GetSession(ctx context.Context, sessionType, id string) (interface{}, error) { | ||
| v, err := l.fullStorage.GetSession(ctx, sessionType, id) | ||
| if err != nil { | ||
| log.Printf("logging adapter: GetSession %s failed: %v", sessionType, err) | ||
| l.inc("GetSessionError") | ||
| } else { | ||
| l.inc("GetSession") | ||
| } | ||
| return v, err | ||
| } | ||
|
|
||
| func (l *LoggingAdapter) DeleteSession(ctx context.Context, sessionType, id string) error { | ||
| err := l.fullStorage.DeleteSession(ctx, sessionType, id) | ||
| if err != nil { | ||
| log.Printf("logging adapter: DeleteSession %s failed: %v", sessionType, err) | ||
| l.inc("DeleteSessionError") | ||
| } else { | ||
| l.inc("DeleteSession") | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| func (l *LoggingAdapter) ValidateJWT(ctx context.Context, jti string) error { | ||
| err := l.fullStorage.ValidateJWT(ctx, jti) | ||
| if err != nil { | ||
| log.Printf("logging adapter: ValidateJWT failed: %v", err) | ||
| l.inc("ValidateJWTError") | ||
| } else { | ||
| l.inc("ValidateJWT") | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| func (l *LoggingAdapter) MarkJWTAsUsed(ctx context.Context, jti string, exp time.Time) error { | ||
| err := l.fullStorage.MarkJWTAsUsed(ctx, jti, exp) | ||
| if err != nil { | ||
| log.Printf("logging adapter: MarkJWTAsUsed failed: %v", err) | ||
| l.inc("MarkJWTAsUsedError") | ||
| } else { | ||
| l.inc("MarkJWTAsUsed") | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| func (l *LoggingAdapter) GetPKCERequestSession(ctx context.Context, signature string, session fosite.Session) (fosite.Requester, error) { | ||
| v, err := l.fullStorage.GetPKCERequestSession(ctx, signature, session) | ||
| if err != nil { | ||
| log.Printf("logging adapter: GetPKCERequestSession failed: %v", err) | ||
| l.inc("GetPKCESessionError") | ||
| } else { | ||
| l.inc("GetPKCESession") | ||
| } | ||
| return v, err | ||
| } | ||
|
|
||
| func (l *LoggingAdapter) CreatePKCERequestSession(ctx context.Context, signature string, requester fosite.Requester) error { | ||
| err := l.fullStorage.CreatePKCERequestSession(ctx, signature, requester) | ||
| if err != nil { | ||
| log.Printf("logging adapter: CreatePKCERequestSession failed: %v", err) | ||
| l.inc("CreatePKCESessionError") | ||
| } else { | ||
| l.inc("CreatePKCESession") | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| func (l *LoggingAdapter) DeletePKCERequestSession(ctx context.Context, signature string) error { | ||
| err := l.fullStorage.DeletePKCERequestSession(ctx, signature) | ||
| if err != nil { | ||
| log.Printf("logging adapter: DeletePKCERequestSession failed: %v", err) | ||
| l.inc("DeletePKCESessionError") | ||
| } else { | ||
| l.inc("DeletePKCESession") | ||
| } | ||
| return err | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.