Skip to content

Commit e214d6d

Browse files
committed
refactor: rework logging and cancellation in services
1 parent 55b53c7 commit e214d6d

12 files changed

Lines changed: 310 additions & 290 deletions

internal/bootstrap/router_bootstrap.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ func (app *BootstrapApp) setupRouter() error {
2929
}
3030
}
3131

32-
contextMiddleware := middleware.NewContextMiddleware(middleware.ContextMiddlewareConfig{
33-
CookieDomain: app.runtime.CookieDomain,
34-
SessionCookieName: app.runtime.SessionCookieName,
35-
}, app.services.authService, app.services.oauthBrokerService)
32+
contextMiddleware := middleware.NewContextMiddleware(app.log, app.runtime, app.services.authService, app.services.oauthBrokerService)
3633

3734
err := contextMiddleware.Init()
3835

@@ -52,7 +49,7 @@ func (app *BootstrapApp) setupRouter() error {
5249

5350
engine.Use(uiMiddleware.Middleware())
5451

55-
zerologMiddleware := middleware.NewZerologMiddleware()
52+
zerologMiddleware := middleware.NewZerologMiddleware(app.log)
5653

5754
err = zerologMiddleware.Init()
5855

internal/bootstrap/service_bootstrap.go

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,11 @@ import (
44
"fmt"
55
"os"
66

7-
"github.com/tinyauthapp/tinyauth/internal/model"
87
"github.com/tinyauthapp/tinyauth/internal/service"
98
)
109

1110
func (app *BootstrapApp) setupServices() error {
12-
ldapService := service.NewLdapService(service.LdapServiceConfig{
13-
Address: app.config.LDAP.Address,
14-
BindDN: app.config.LDAP.BindDN,
15-
BindPassword: app.config.LDAP.BindPassword,
16-
BaseDN: app.config.LDAP.BaseDN,
17-
Insecure: app.config.LDAP.Insecure,
18-
SearchFilter: app.config.LDAP.SearchFilter,
19-
AuthCert: app.config.LDAP.AuthCert,
20-
AuthKey: app.config.LDAP.AuthKey,
21-
})
11+
ldapService := service.NewLdapService(app.log, app.config, app.ctx)
2212

2313
err := ldapService.Init()
2414

@@ -32,10 +22,12 @@ func (app *BootstrapApp) setupServices() error {
3222
useKubernetes := app.config.LabelProvider == "kubernetes" ||
3323
(app.config.LabelProvider == "auto" && os.Getenv("KUBERNETES_SERVICE_HOST") != "")
3424

25+
var labelProvider service.LabelProviderImpl
26+
3527
if useKubernetes {
3628
app.log.App.Debug().Msg("Using Kubernetes label provider")
3729

38-
kubernetesService := service.NewKubernetesService()
30+
kubernetesService := service.NewKubernetesService(app.log, app.ctx)
3931

4032
err = kubernetesService.Init()
4133

@@ -44,11 +36,11 @@ func (app *BootstrapApp) setupServices() error {
4436
}
4537

4638
app.services.kubernetesService = kubernetesService
47-
app.runtime.LabelProvider = model.LabelProviderKubernetes
39+
labelProvider = kubernetesService
4840
} else {
4941
app.log.App.Debug().Msg("Using Docker label provider")
5042

51-
dockerService := service.NewDockerService()
43+
dockerService := service.NewDockerService(app.log, app.ctx)
5244

5345
err = dockerService.Init()
5446

@@ -57,10 +49,10 @@ func (app *BootstrapApp) setupServices() error {
5749
}
5850

5951
app.services.dockerService = dockerService
60-
app.runtime.LabelProvider = model.LabelProviderDocker
52+
labelProvider = dockerService
6153
}
6254

63-
accessControlsService := service.NewAccessControlsService(app.runtime.LabelProvider, app.config.Apps)
55+
accessControlsService := service.NewAccessControlsService(app.log, labelProvider, app.config.Apps)
6456

6557
err = accessControlsService.Init()
6658

@@ -70,7 +62,7 @@ func (app *BootstrapApp) setupServices() error {
7062

7163
app.services.accessControlService = accessControlsService
7264

73-
oauthBrokerService := service.NewOAuthBrokerService(app.runtime.OAuthProviders)
65+
oauthBrokerService := service.NewOAuthBrokerService(app.log, app.runtime.OAuthProviders)
7466

7567
err = oauthBrokerService.Init()
7668

@@ -80,20 +72,7 @@ func (app *BootstrapApp) setupServices() error {
8072

8173
app.services.oauthBrokerService = oauthBrokerService
8274

83-
authService := service.NewAuthService(service.AuthServiceConfig{
84-
LocalUsers: &app.runtime.LocalUsers,
85-
OauthWhitelist: app.runtime.OAuthWhitelist,
86-
SessionExpiry: app.config.Auth.SessionExpiry,
87-
SessionMaxLifetime: app.config.Auth.SessionMaxLifetime,
88-
SecureCookie: app.config.Auth.SecureCookie,
89-
CookieDomain: app.runtime.CookieDomain,
90-
LoginTimeout: app.config.Auth.LoginTimeout,
91-
LoginMaxRetries: app.config.Auth.LoginMaxRetries,
92-
SessionCookieName: app.runtime.SessionCookieName,
93-
IP: app.config.Auth.IP,
94-
LDAPGroupsCacheTTL: app.config.LDAP.GroupCacheTTL,
95-
SubdomainsEnabled: app.config.Auth.SubdomainsEnabled,
96-
}, app.services.ldapService, app.queries, app.services.oauthBrokerService)
75+
authService := service.NewAuthService(app.log, app.config, app.runtime, app.ctx, app.services.ldapService, app.queries, app.services.oauthBrokerService)
9776

9877
err = authService.Init()
9978

@@ -103,13 +82,7 @@ func (app *BootstrapApp) setupServices() error {
10382

10483
app.services.authService = authService
10584

106-
oidcService := service.NewOIDCService(service.OIDCServiceConfig{
107-
Clients: app.config.OIDC.Clients,
108-
PrivateKeyPath: app.config.OIDC.PrivateKeyPath,
109-
PublicKeyPath: app.config.OIDC.PublicKeyPath,
110-
Issuer: app.config.AppURL,
111-
SessionExpiry: app.config.Auth.SessionExpiry,
112-
}, app.queries)
85+
oidcService := service.NewOIDCService(app.log, app.config, app.runtime, app.queries, app.ctx)
11386

11487
err = oidcService.Init()
11588

internal/controller/user_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ func (controller *UserController) totpHandler(c *gin.Context) {
375375
cookie, err := controller.auth.CreateSession(c, sessionCookie)
376376

377377
if err != nil {
378-
tlog.App.Error().Err(err).Msg("Failed to create session cookie")
378+
controller.log.App.Error().Err(err).Str("username", context.GetUsername()).Msg("Failed to create session cookie after successful TOTP verification")
379379
c.JSON(500, gin.H{
380380
"status": 500,
381381
"message": "Internal Server Error",

internal/model/runtime.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,10 @@ type RuntimeConfig struct {
1313
OAuthWhitelist []string
1414
ConfiguredProviders []Provider
1515
OIDCClients []OIDCClientConfig
16-
LabelProvider LabelProvider
1716
}
1817

1918
type Provider struct {
2019
Name string `json:"name"`
2120
ID string `json:"id"`
2221
OAuth bool `json:"oauth"`
2322
}
24-
25-
type LabelProvider int
26-
27-
const (
28-
LabelProviderDocker LabelProvider = iota
29-
LabelProviderKubernetes
30-
)

internal/service/access_controls_service.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,25 @@ import (
44
"strings"
55

66
"github.com/tinyauthapp/tinyauth/internal/model"
7-
"github.com/tinyauthapp/tinyauth/internal/utils/tlog"
7+
"github.com/tinyauthapp/tinyauth/internal/utils/logger"
88
)
99

1010
type LabelProviderImpl interface {
1111
GetLabels(appDomain string) (*model.App, error)
1212
}
1313

1414
type AccessControlsService struct {
15-
labelProvider LabelProvider
15+
log *logger.Logger
16+
labelProvider LabelProviderImpl
1617
static map[string]model.App
1718
}
1819

19-
func NewAccessControlsService(labelProvider LabelProvider, static map[string]model.App) *AccessControlsService {
20+
func NewAccessControlsService(
21+
log *logger.Logger,
22+
labelProvider LabelProviderImpl,
23+
static map[string]model.App) *AccessControlsService {
2024
return &AccessControlsService{
25+
log: log,
2126
labelProvider: labelProvider,
2227
static: static,
2328
}
@@ -31,13 +36,13 @@ func (acls *AccessControlsService) lookupStaticACLs(domain string) *model.App {
3136
var appAcls *model.App
3237
for app, config := range acls.static {
3338
if config.Config.Domain == domain {
34-
tlog.App.Debug().Str("name", app).Msg("Found matching container by domain")
39+
acls.log.App.Debug().Str("name", app).Msg("Found matching container by domain")
3540
appAcls = &config
3641
break // If we find a match by domain, we can stop searching
3742
}
3843

3944
if strings.SplitN(domain, ".", 2)[0] == app {
40-
tlog.App.Debug().Str("name", app).Msg("Found matching container by app name")
45+
acls.log.App.Debug().Str("name", app).Msg("Found matching container by app name")
4146
appAcls = &config
4247
break // If we find a match by app name, we can stop searching
4348
}
@@ -50,11 +55,11 @@ func (acls *AccessControlsService) GetAccessControls(domain string) (*model.App,
5055
app := acls.lookupStaticACLs(domain)
5156

5257
if app != nil {
53-
tlog.App.Debug().Msg("Using ACls from static configuration")
58+
acls.log.App.Debug().Msg("Using static ACLs for app")
5459
return app, nil
5560
}
5661

5762
// Fallback to label provider
58-
tlog.App.Debug().Msg("Falling back to label provider for ACLs")
63+
acls.log.App.Debug().Msg("Using label provider for app")
5964
return acls.labelProvider.GetLabels(domain)
6065
}

0 commit comments

Comments
 (0)