Skip to content

Commit 7dee01e

Browse files
authored
add auth check, adjust trasnaction for sqlite, apply configs (#2325)
* add auth check, adjust trasnaction for sqlite, apply configs * update defaults * adjust env var
1 parent 24e72dd commit 7dee01e

File tree

5 files changed

+45
-43
lines changed

5 files changed

+45
-43
lines changed

taco/cmd/statesman/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func main() {
3838

3939
// Load configuration from environment variables into our struct.
4040
var queryCfg query.Config
41-
err := envconfig.Process("taco", &queryCfg) // The prefix "TACO" will be used for all vars.
41+
err := envconfig.Process("opentaco", &queryCfg) // The prefix "TACO" will be used for all vars.
4242
if err != nil {
4343
log.Fatalf("Failed to process configuration: %v", err)
4444
}

taco/internal/api/org_handler.go

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"log/slog"
66
"net/http"
77
"context"
8-
"fmt"
98

109
"github.com/diggerhq/digger/opentaco/internal/domain"
1110
"github.com/diggerhq/digger/opentaco/internal/rbac"
@@ -96,40 +95,17 @@ func (h *OrgHandler) CreateOrganization(c echo.Context) error {
9695
)
9796

9897
// ========================================
99-
// Use transaction to create org + init RBAC atomically
98+
// Create org first, then init RBAC (SQLite-friendly)
10099
// ========================================
101100
var org *domain.Organization
102101

102+
// Create organization in transaction
103103
err := h.orgRepo.WithTransaction(ctx, func(ctx context.Context, txRepo domain.OrganizationRepository) error {
104-
// Create organization within transaction
105104
createdOrg, err := txRepo.Create(ctx, req.OrgID, req.Name, userIDStr)
106105
if err != nil {
107106
return err
108107
}
109108
org = createdOrg
110-
111-
// Initialize RBAC within the same transaction
112-
if h.rbacManager != nil {
113-
slog.Info("Initializing RBAC for new organization",
114-
"orgID", req.OrgID,
115-
"adminUser", userIDStr,
116-
)
117-
118-
if err := h.rbacManager.InitializeRBAC(ctx, userIDStr, emailStr); err != nil {
119-
// IMPORTANT: Returning error here will rollback the entire transaction
120-
slog.Error("Failed to initialize RBAC, rolling back org creation",
121-
"orgID", req.OrgID,
122-
"error", err,
123-
)
124-
return fmt.Errorf("failed to initialize RBAC: %w", err)
125-
}
126-
127-
slog.Info("RBAC initialized successfully",
128-
"orgID", req.OrgID,
129-
"adminUser", userIDStr,
130-
)
131-
}
132-
133109
return nil
134110
})
135111

@@ -146,8 +122,7 @@ func (h *OrgHandler) CreateOrganization(c echo.Context) error {
146122
})
147123
}
148124

149-
// Any other error (including RBAC init failure) returns 500
150-
slog.Error("Failed to create organization with RBAC",
125+
slog.Error("Failed to create organization",
151126
"orgID", req.OrgID,
152127
"error", err,
153128
)
@@ -157,7 +132,31 @@ func (h *OrgHandler) CreateOrganization(c echo.Context) error {
157132
})
158133
}
159134

160-
// Success - both org and RBAC were created
135+
// Initialize RBAC after org creation (outside transaction for SQLite compatibility)
136+
if h.rbacManager != nil {
137+
slog.Info("Initializing RBAC for new organization",
138+
"orgID", req.OrgID,
139+
"adminUser", userIDStr,
140+
)
141+
142+
if err := h.rbacManager.InitializeRBAC(ctx, userIDStr, emailStr); err != nil {
143+
// Org was created but RBAC failed - log warning but don't fail the request
144+
// User can retry RBAC initialization or assign roles manually
145+
slog.Warn("Organization created but RBAC initialization failed",
146+
"orgID", req.OrgID,
147+
"error", err,
148+
"recommendation", "RBAC can be initialized later via /rbac/init endpoint",
149+
)
150+
// Continue with success response - org was created
151+
} else {
152+
slog.Info("RBAC initialized successfully",
153+
"orgID", req.OrgID,
154+
"adminUser", userIDStr,
155+
)
156+
}
157+
}
158+
159+
// Success - org created (and RBAC initialized if available)
161160
return c.JSON(http.StatusCreated, CreateOrgResponse{
162161
OrgID: org.OrgID,
163162
Name: org.Name,

taco/internal/query/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ type SQLiteConfig struct {
1616
Path string `envconfig:"DB_PATH" default:"./data/taco.db"`// if we call it PATH at the struct level, it will pick up the terminal path
1717
Cache string `envconfig:"CACHE" default:"shared"`
1818
BusyTimeout time.Duration `envconfig:"BUSY_TIMEOUT" default:"5s"`
19-
MaxOpenConns int `envconfig:"MAX_OPEN_CONNS" default:"1"`
20-
MaxIdleConns int `envconfig:"MAX_IDLE_CONNS" default:"1"`
19+
MaxOpenConns int `envconfig:"MAX_OPEN_CONNS" default:"25"`
20+
MaxIdleConns int `envconfig:"MAX_IDLE_CONNS" default:"10"`
2121
PragmaJournalMode string `envconfig:"PRAGMA_JOURNAL_MODE" default:"WAL"`
2222
PragmaForeignKeys string `envconfig:"PRAGMA_FOREIGN_KEYS" default:"ON"`
2323
PragmaBusyTimeout string `envconfig:"PRAGMA_BUSY_TIMEOUT" default:"5000"`

taco/internal/query/sqlite/store.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ func NewSQLiteQueryStore(cfg query.SQLiteConfig) (query.Store, error) {
3838
return nil, fmt.Errorf("apply busy_timeout: %w", err)
3939
}
4040

41+
// Configure connection pool settings
42+
sqlDB, err := db.DB()
43+
if err != nil {
44+
return nil, fmt.Errorf("get underlying sql.DB: %w", err)
45+
}
46+
sqlDB.SetMaxOpenConns(cfg.MaxOpenConns)
47+
sqlDB.SetMaxIdleConns(cfg.MaxIdleConns)
48+
4149
// Create the common SQLStore with our configured DB object, breaking the cycle.
4250
return common.NewSQLStore(db)
4351
}

taco/internal/rbac/handler.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,12 @@ func (h *Handler) DeleteRole(c echo.Context) error {
335335
// Helper functions
336336

337337
func (h *Handler) getPrincipalFromToken(c echo.Context) (Principal, error) {
338+
// First check if principal is already in context (webhook auth sets this)
339+
if principal, ok := PrincipalFromContext(c.Request().Context()); ok {
340+
return principal, nil
341+
}
342+
343+
// Fall back to JWT token verification for public API routes
338344
authz := c.Request().Header.Get("Authorization")
339345
if !strings.HasPrefix(authz, "Bearer ") {
340346
return Principal{}, echo.NewHTTPError(http.StatusUnauthorized, "missing bearer token")
@@ -345,19 +351,8 @@ func (h *Handler) getPrincipalFromToken(c echo.Context) (Principal, error) {
345351
return Principal{}, echo.NewHTTPError(http.StatusInternalServerError, "auth not configured")
346352
}
347353

348-
// Debug: check signer state
349-
fmt.Printf("[RBAC DEBUG] Signer nil? %t\n", h.signer == nil)
350-
fmt.Printf("[RBAC DEBUG] Signer addr: %p\n", h.signer)
351-
352354
claims, err := h.signer.VerifyAccess(token)
353355
if err != nil {
354-
// Debug: log the verification failure
355-
fmt.Printf("[RBAC DEBUG] Token verification failed: %v\n", err)
356-
tokenPreview := token
357-
if len(token) > 50 {
358-
tokenPreview = token[:50] + "..."
359-
}
360-
fmt.Printf("[RBAC DEBUG] Token preview: %s\n", tokenPreview)
361356
return Principal{}, echo.NewHTTPError(http.StatusUnauthorized, "invalid token")
362357
}
363358

0 commit comments

Comments
 (0)