chore: move the go code into a subfolder

This commit is contained in:
Florian Forster
2025-08-05 15:20:32 -07:00
parent 4ad22ba456
commit cd2921de26
2978 changed files with 373 additions and 300 deletions

View File

@@ -0,0 +1,174 @@
package cache
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/hashicorp/golang-lru/v2"
"github.com/zitadel/logging"
"github.com/zitadel/zitadel/internal/api/authz"
"github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/zerrors"
)
type AuthRequestCache struct {
client *database.DB
idCache *lru.Cache[string, *domain.AuthRequest]
codeCache *lru.Cache[string, *domain.AuthRequest]
}
func Start(dbClient *database.DB, amountOfCachedAuthRequests uint16) *AuthRequestCache {
cache := &AuthRequestCache{
client: dbClient,
}
idCache, err := lru.New[string, *domain.AuthRequest](int(amountOfCachedAuthRequests))
logging.OnError(err).Info("auth request cache disabled")
if err == nil {
cache.idCache = idCache
}
codeCache, err := lru.New[string, *domain.AuthRequest](int(amountOfCachedAuthRequests))
logging.OnError(err).Info("auth request cache disabled")
if err == nil {
cache.codeCache = codeCache
}
return cache
}
func (c *AuthRequestCache) Health(ctx context.Context) error {
return c.client.PingContext(ctx)
}
func (c *AuthRequestCache) GetAuthRequestByID(ctx context.Context, id string) (*domain.AuthRequest, error) {
if authRequest, ok := c.getCachedByID(ctx, id); ok {
return authRequest, nil
}
request, err := c.getAuthRequest(ctx, "id", id, authz.GetInstance(ctx).InstanceID())
if err != nil {
return nil, err
}
c.CacheAuthRequest(ctx, request)
return request, nil
}
func (c *AuthRequestCache) GetAuthRequestByCode(ctx context.Context, code string) (*domain.AuthRequest, error) {
if authRequest, ok := c.getCachedByCode(ctx, code); ok {
return authRequest, nil
}
request, err := c.getAuthRequest(ctx, "code", code, authz.GetInstance(ctx).InstanceID())
if err != nil {
return nil, err
}
c.CacheAuthRequest(ctx, request)
return request, nil
}
func (c *AuthRequestCache) SaveAuthRequest(ctx context.Context, request *domain.AuthRequest) error {
return c.saveAuthRequest(ctx, request, "INSERT INTO auth.auth_requests (id, request, instance_id, creation_date, change_date, request_type) VALUES($1, $2, $3, $4, $4, $5)", request.CreationDate, request.Request.Type())
}
func (c *AuthRequestCache) UpdateAuthRequest(ctx context.Context, request *domain.AuthRequest) error {
if request.ChangeDate.IsZero() {
request.ChangeDate = time.Now()
}
return c.saveAuthRequest(ctx, request, "UPDATE auth.auth_requests SET request = $2, instance_id = $3, change_date = $4, code = $5 WHERE id = $1", request.ChangeDate, request.Code)
}
func (c *AuthRequestCache) DeleteAuthRequest(ctx context.Context, id string) error {
_, err := c.client.Exec("DELETE FROM auth.auth_requests WHERE instance_id = $1 and id = $2", authz.GetInstance(ctx).InstanceID(), id)
if err != nil {
return zerrors.ThrowInternal(err, "CACHE-dsHw3", "unable to delete auth request")
}
c.deleteFromCache(ctx, id)
return nil
}
func (c *AuthRequestCache) getAuthRequest(ctx context.Context, key, value, instanceID string) (*domain.AuthRequest, error) {
var b []byte
var requestType domain.AuthRequestType
query := fmt.Sprintf("SELECT request, request_type FROM auth.auth_requests WHERE instance_id = $1 and %s = $2", key)
err := c.client.QueryRowContext(
ctx,
func(row *sql.Row) error {
return row.Scan(&b, &requestType)
},
query, instanceID, value)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, zerrors.ThrowNotFound(err, "CACHE-d24aD", "Errors.AuthRequest.NotFound")
}
return nil, zerrors.ThrowInternal(err, "CACHE-as3kj", "Errors.Internal")
}
request, err := domain.NewAuthRequestFromType(requestType)
if err == nil {
err = json.Unmarshal(b, request)
}
if err != nil {
return nil, zerrors.ThrowInternal(err, "CACHE-2wshg", "Errors.Internal")
}
return request, nil
}
func (c *AuthRequestCache) saveAuthRequest(ctx context.Context, request *domain.AuthRequest, query string, date time.Time, param interface{}) error {
b, err := json.Marshal(request)
if err != nil {
return zerrors.ThrowInternal(err, "CACHE-os0GH", "Errors.Internal")
}
_, err = c.client.Exec(query, request.ID, b, request.InstanceID, date, param)
if err != nil {
return zerrors.ThrowInternal(err, "CACHE-su3GK", "Errors.Internal")
}
c.CacheAuthRequest(ctx, request)
return nil
}
func (c *AuthRequestCache) getCachedByID(ctx context.Context, id string) (*domain.AuthRequest, bool) {
if c.idCache == nil {
return nil, false
}
authRequest, ok := c.idCache.Get(cacheKey(ctx, id))
logging.WithFields("hit", ok, "type", "id").Info("get from auth request cache")
return authRequest, ok
}
func (c *AuthRequestCache) getCachedByCode(ctx context.Context, code string) (*domain.AuthRequest, bool) {
if c.codeCache == nil {
return nil, false
}
authRequest, ok := c.codeCache.Get(cacheKey(ctx, code))
logging.WithFields("hit", ok, "type", "code").Info("get from auth request cache")
return authRequest, ok
}
func (c *AuthRequestCache) CacheAuthRequest(ctx context.Context, request *domain.AuthRequest) {
if c.idCache == nil {
return
}
c.idCache.Add(cacheKey(ctx, request.ID), request)
if request.Code != "" {
c.codeCache.Add(cacheKey(ctx, request.Code), request)
}
}
func cacheKey(ctx context.Context, value string) string {
return fmt.Sprintf("%s-%s", authz.GetInstance(ctx).InstanceID(), value)
}
func (c *AuthRequestCache) deleteFromCache(ctx context.Context, id string) {
if c.idCache == nil {
return
}
idKey := cacheKey(ctx, id)
request, ok := c.idCache.Get(idKey)
if !ok {
return
}
c.idCache.Remove(idKey)
c.codeCache.Remove(cacheKey(ctx, request.Code))
}

View File

@@ -0,0 +1,3 @@
package repository
//go:generate mockgen -package mock -destination ./mock/repository.mock.go github.com/zitadel/zitadel/internal/auth_request/repository AuthRequestCache

View File

@@ -0,0 +1,139 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: github.com/zitadel/zitadel/internal/auth_request/repository (interfaces: AuthRequestCache)
//
// Generated by this command:
//
// mockgen -package mock -destination ./mock/repository.mock.go github.com/zitadel/zitadel/internal/auth_request/repository AuthRequestCache
//
// Package mock is a generated GoMock package.
package mock
import (
context "context"
reflect "reflect"
domain "github.com/zitadel/zitadel/internal/domain"
gomock "go.uber.org/mock/gomock"
)
// MockAuthRequestCache is a mock of AuthRequestCache interface.
type MockAuthRequestCache struct {
ctrl *gomock.Controller
recorder *MockAuthRequestCacheMockRecorder
}
// MockAuthRequestCacheMockRecorder is the mock recorder for MockAuthRequestCache.
type MockAuthRequestCacheMockRecorder struct {
mock *MockAuthRequestCache
}
// NewMockAuthRequestCache creates a new mock instance.
func NewMockAuthRequestCache(ctrl *gomock.Controller) *MockAuthRequestCache {
mock := &MockAuthRequestCache{ctrl: ctrl}
mock.recorder = &MockAuthRequestCacheMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockAuthRequestCache) EXPECT() *MockAuthRequestCacheMockRecorder {
return m.recorder
}
// CacheAuthRequest mocks base method.
func (m *MockAuthRequestCache) CacheAuthRequest(arg0 context.Context, arg1 *domain.AuthRequest) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "CacheAuthRequest", arg0, arg1)
}
// CacheAuthRequest indicates an expected call of CacheAuthRequest.
func (mr *MockAuthRequestCacheMockRecorder) CacheAuthRequest(arg0, arg1 any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CacheAuthRequest", reflect.TypeOf((*MockAuthRequestCache)(nil).CacheAuthRequest), arg0, arg1)
}
// DeleteAuthRequest mocks base method.
func (m *MockAuthRequestCache) DeleteAuthRequest(arg0 context.Context, arg1 string) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "DeleteAuthRequest", arg0, arg1)
ret0, _ := ret[0].(error)
return ret0
}
// DeleteAuthRequest indicates an expected call of DeleteAuthRequest.
func (mr *MockAuthRequestCacheMockRecorder) DeleteAuthRequest(arg0, arg1 any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteAuthRequest", reflect.TypeOf((*MockAuthRequestCache)(nil).DeleteAuthRequest), arg0, arg1)
}
// GetAuthRequestByCode mocks base method.
func (m *MockAuthRequestCache) GetAuthRequestByCode(arg0 context.Context, arg1 string) (*domain.AuthRequest, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetAuthRequestByCode", arg0, arg1)
ret0, _ := ret[0].(*domain.AuthRequest)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GetAuthRequestByCode indicates an expected call of GetAuthRequestByCode.
func (mr *MockAuthRequestCacheMockRecorder) GetAuthRequestByCode(arg0, arg1 any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAuthRequestByCode", reflect.TypeOf((*MockAuthRequestCache)(nil).GetAuthRequestByCode), arg0, arg1)
}
// GetAuthRequestByID mocks base method.
func (m *MockAuthRequestCache) GetAuthRequestByID(arg0 context.Context, arg1 string) (*domain.AuthRequest, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetAuthRequestByID", arg0, arg1)
ret0, _ := ret[0].(*domain.AuthRequest)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GetAuthRequestByID indicates an expected call of GetAuthRequestByID.
func (mr *MockAuthRequestCacheMockRecorder) GetAuthRequestByID(arg0, arg1 any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAuthRequestByID", reflect.TypeOf((*MockAuthRequestCache)(nil).GetAuthRequestByID), arg0, arg1)
}
// Health mocks base method.
func (m *MockAuthRequestCache) Health(arg0 context.Context) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Health", arg0)
ret0, _ := ret[0].(error)
return ret0
}
// Health indicates an expected call of Health.
func (mr *MockAuthRequestCacheMockRecorder) Health(arg0 any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Health", reflect.TypeOf((*MockAuthRequestCache)(nil).Health), arg0)
}
// SaveAuthRequest mocks base method.
func (m *MockAuthRequestCache) SaveAuthRequest(arg0 context.Context, arg1 *domain.AuthRequest) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "SaveAuthRequest", arg0, arg1)
ret0, _ := ret[0].(error)
return ret0
}
// SaveAuthRequest indicates an expected call of SaveAuthRequest.
func (mr *MockAuthRequestCacheMockRecorder) SaveAuthRequest(arg0, arg1 any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SaveAuthRequest", reflect.TypeOf((*MockAuthRequestCache)(nil).SaveAuthRequest), arg0, arg1)
}
// UpdateAuthRequest mocks base method.
func (m *MockAuthRequestCache) UpdateAuthRequest(arg0 context.Context, arg1 *domain.AuthRequest) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "UpdateAuthRequest", arg0, arg1)
ret0, _ := ret[0].(error)
return ret0
}
// UpdateAuthRequest indicates an expected call of UpdateAuthRequest.
func (mr *MockAuthRequestCacheMockRecorder) UpdateAuthRequest(arg0, arg1 any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateAuthRequest", reflect.TypeOf((*MockAuthRequestCache)(nil).UpdateAuthRequest), arg0, arg1)
}

View File

@@ -0,0 +1,18 @@
package repository
import (
"context"
"github.com/zitadel/zitadel/internal/domain"
)
type AuthRequestCache interface {
Health(ctx context.Context) error
GetAuthRequestByID(ctx context.Context, id string) (*domain.AuthRequest, error)
GetAuthRequestByCode(ctx context.Context, code string) (*domain.AuthRequest, error)
SaveAuthRequest(ctx context.Context, request *domain.AuthRequest) error
CacheAuthRequest(ctx context.Context, request *domain.AuthRequest)
UpdateAuthRequest(ctx context.Context, request *domain.AuthRequest) error
DeleteAuthRequest(ctx context.Context, id string) error
}