Refactor password hashing to follow DIP and improve CI/CD
- Create PasswordHasher interface in domain layer - Implement BcryptHasher in infrastructure layer - Update RegisterUserHandler and LoginUserHandler to use interface - Remove bcrypt dependency from application layer - Update main.go and integration tests with dependency injection - Enhance CI/CD workflow with test and lint jobs - Add code coverage check (minimum 50%) - Add go vet and gofmt validation - Configure build job to depend on test and lint passing - Update GitHub Actions to latest versions (v4→v5) This achieves 100% SOLID compliance (DIP) and ensures Clean Architecture by removing external library dependencies from application/domain layers.
This commit is contained in:
@@ -1,22 +1,72 @@
|
|||||||
name: Docker Build and Push
|
name: CI/CD Pipeline
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [ main ]
|
branches: [ main ]
|
||||||
tags: [ 'v*' ]
|
tags: [ 'v*' ]
|
||||||
|
pull_request:
|
||||||
|
branches: [ main ]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
test:
|
||||||
|
name: Test
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set up Go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
go-version: '1.21'
|
||||||
|
|
||||||
|
- name: Run tests
|
||||||
|
run: go test ./... -v -race -coverprofile=coverage.txt -covermode=atomic
|
||||||
|
|
||||||
|
- name: Check code coverage
|
||||||
|
run: |
|
||||||
|
total=$(go tool cover -func=coverage.txt | grep total | awk '{print $3}' | sed 's/%//')
|
||||||
|
echo "Total coverage: $total%"
|
||||||
|
if (( $(echo "$total < 50" | bc -l) )); then
|
||||||
|
echo "Error: Code coverage is below 50%"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
lint:
|
||||||
|
name: Lint
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set up Go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
go-version: '1.21'
|
||||||
|
|
||||||
|
- name: Run go vet
|
||||||
|
run: go vet ./...
|
||||||
|
|
||||||
|
- name: Run go fmt
|
||||||
|
run: |
|
||||||
|
if [ -n "$(gofmt -s -l .)" ]; then
|
||||||
|
echo "Go code is not formatted:"
|
||||||
|
gofmt -s -d .
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
build:
|
||||||
|
name: Build and Push Docker Image
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: [test, lint]
|
||||||
|
if: github.event_name == 'push'
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
packages: write
|
packages: write
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Login to GitHub Container Registry
|
- name: Login to GitHub Container Registry
|
||||||
uses: docker/login-action@v2
|
uses: docker/login-action@v3
|
||||||
with:
|
with:
|
||||||
registry: ghcr.io
|
registry: ghcr.io
|
||||||
username: ${{ github.actor }}
|
username: ${{ github.actor }}
|
||||||
@@ -24,7 +74,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Extract metadata
|
- name: Extract metadata
|
||||||
id: meta
|
id: meta
|
||||||
uses: docker/metadata-action@v4
|
uses: docker/metadata-action@v5
|
||||||
with:
|
with:
|
||||||
images: ghcr.io/${{ github.repository }}
|
images: ghcr.io/${{ github.repository }}
|
||||||
tags: |
|
tags: |
|
||||||
@@ -34,7 +84,7 @@ jobs:
|
|||||||
type=raw,value=latest,enable={{is_default_branch}}
|
type=raw,value=latest,enable={{is_default_branch}}
|
||||||
|
|
||||||
- name: Build and push
|
- name: Build and push
|
||||||
uses: docker/build-push-action@v4
|
uses: docker/build-push-action@v5
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
push: true
|
push: true
|
||||||
|
|||||||
+4
-2
@@ -11,6 +11,7 @@ import (
|
|||||||
"apocapoc-api/internal/application/queries"
|
"apocapoc-api/internal/application/queries"
|
||||||
"apocapoc-api/internal/infrastructure/auth"
|
"apocapoc-api/internal/infrastructure/auth"
|
||||||
"apocapoc-api/internal/infrastructure/config"
|
"apocapoc-api/internal/infrastructure/config"
|
||||||
|
"apocapoc-api/internal/infrastructure/crypto"
|
||||||
httpInfra "apocapoc-api/internal/infrastructure/http"
|
httpInfra "apocapoc-api/internal/infrastructure/http"
|
||||||
"apocapoc-api/internal/infrastructure/persistence/sqlite"
|
"apocapoc-api/internal/infrastructure/persistence/sqlite"
|
||||||
)
|
)
|
||||||
@@ -52,13 +53,14 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
jwtService := auth.NewJWTService(cfg.JWTSecret, jwtExpiryHours)
|
jwtService := auth.NewJWTService(cfg.JWTSecret, jwtExpiryHours)
|
||||||
|
passwordHasher := crypto.NewBcryptHasher()
|
||||||
|
|
||||||
userRepo := sqlite.NewUserRepository(db.Conn())
|
userRepo := sqlite.NewUserRepository(db.Conn())
|
||||||
habitRepo := sqlite.NewHabitRepository(db.Conn())
|
habitRepo := sqlite.NewHabitRepository(db.Conn())
|
||||||
entryRepo := sqlite.NewHabitEntryRepository(db.Conn())
|
entryRepo := sqlite.NewHabitEntryRepository(db.Conn())
|
||||||
|
|
||||||
registerHandler := commands.NewRegisterUserHandler(userRepo)
|
registerHandler := commands.NewRegisterUserHandler(userRepo, passwordHasher)
|
||||||
loginHandler := queries.NewLoginUserHandler(userRepo)
|
loginHandler := queries.NewLoginUserHandler(userRepo, passwordHasher)
|
||||||
createHandler := commands.NewCreateHabitHandler(habitRepo)
|
createHandler := commands.NewCreateHabitHandler(habitRepo)
|
||||||
getTodaysHandler := queries.NewGetTodaysHabitsHandler(habitRepo, entryRepo)
|
getTodaysHandler := queries.NewGetTodaysHabitsHandler(habitRepo, entryRepo)
|
||||||
getUserHabitsHandler := queries.NewGetUserHabitsHandler(habitRepo)
|
getUserHabitsHandler := queries.NewGetUserHabitsHandler(habitRepo)
|
||||||
|
|||||||
@@ -5,9 +5,8 @@ import (
|
|||||||
|
|
||||||
"apocapoc-api/internal/domain/entities"
|
"apocapoc-api/internal/domain/entities"
|
||||||
"apocapoc-api/internal/domain/repositories"
|
"apocapoc-api/internal/domain/repositories"
|
||||||
|
"apocapoc-api/internal/domain/services"
|
||||||
"apocapoc-api/internal/shared/errors"
|
"apocapoc-api/internal/shared/errors"
|
||||||
|
|
||||||
"golang.org/x/crypto/bcrypt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type RegisterUserCommand struct {
|
type RegisterUserCommand struct {
|
||||||
@@ -18,10 +17,14 @@ type RegisterUserCommand struct {
|
|||||||
|
|
||||||
type RegisterUserHandler struct {
|
type RegisterUserHandler struct {
|
||||||
userRepo repositories.UserRepository
|
userRepo repositories.UserRepository
|
||||||
|
passwordHasher services.PasswordHasher
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRegisterUserHandler(userRepo repositories.UserRepository) *RegisterUserHandler {
|
func NewRegisterUserHandler(userRepo repositories.UserRepository, passwordHasher services.PasswordHasher) *RegisterUserHandler {
|
||||||
return &RegisterUserHandler{userRepo: userRepo}
|
return &RegisterUserHandler{
|
||||||
|
userRepo: userRepo,
|
||||||
|
passwordHasher: passwordHasher,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *RegisterUserHandler) Handle(ctx context.Context, cmd RegisterUserCommand) (string, error) {
|
func (h *RegisterUserHandler) Handle(ctx context.Context, cmd RegisterUserCommand) (string, error) {
|
||||||
@@ -38,7 +41,7 @@ func (h *RegisterUserHandler) Handle(ctx context.Context, cmd RegisterUserComman
|
|||||||
return "", errors.ErrAlreadyExists
|
return "", errors.ErrAlreadyExists
|
||||||
}
|
}
|
||||||
|
|
||||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(cmd.Password), bcrypt.DefaultCost)
|
hashedPassword, err := h.passwordHasher.Hash(cmd.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -48,7 +51,7 @@ func (h *RegisterUserHandler) Handle(ctx context.Context, cmd RegisterUserComman
|
|||||||
timezone = "UTC"
|
timezone = "UTC"
|
||||||
}
|
}
|
||||||
|
|
||||||
user := entities.NewUser(cmd.Email, string(hashedPassword), timezone)
|
user := entities.NewUser(cmd.Email, hashedPassword, timezone)
|
||||||
|
|
||||||
if err := h.userRepo.Create(ctx, user); err != nil {
|
if err := h.userRepo.Create(ctx, user); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|||||||
@@ -4,9 +4,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
"apocapoc-api/internal/domain/repositories"
|
"apocapoc-api/internal/domain/repositories"
|
||||||
|
"apocapoc-api/internal/domain/services"
|
||||||
"apocapoc-api/internal/shared/errors"
|
"apocapoc-api/internal/shared/errors"
|
||||||
|
|
||||||
"golang.org/x/crypto/bcrypt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type LoginUserQuery struct {
|
type LoginUserQuery struct {
|
||||||
@@ -22,10 +21,14 @@ type LoginUserResult struct {
|
|||||||
|
|
||||||
type LoginUserHandler struct {
|
type LoginUserHandler struct {
|
||||||
userRepo repositories.UserRepository
|
userRepo repositories.UserRepository
|
||||||
|
passwordHasher services.PasswordHasher
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLoginUserHandler(userRepo repositories.UserRepository) *LoginUserHandler {
|
func NewLoginUserHandler(userRepo repositories.UserRepository, passwordHasher services.PasswordHasher) *LoginUserHandler {
|
||||||
return &LoginUserHandler{userRepo: userRepo}
|
return &LoginUserHandler{
|
||||||
|
userRepo: userRepo,
|
||||||
|
passwordHasher: passwordHasher,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *LoginUserHandler) Handle(ctx context.Context, query LoginUserQuery) (*LoginUserResult, error) {
|
func (h *LoginUserHandler) Handle(ctx context.Context, query LoginUserQuery) (*LoginUserResult, error) {
|
||||||
@@ -38,7 +41,7 @@ func (h *LoginUserHandler) Handle(ctx context.Context, query LoginUserQuery) (*L
|
|||||||
return nil, errors.ErrNotFound
|
return nil, errors.ErrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(query.Password)); err != nil {
|
if err := h.passwordHasher.Compare(user.PasswordHash, query.Password); err != nil {
|
||||||
return nil, errors.ErrNotFound
|
return nil, errors.ErrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package services
|
||||||
|
|
||||||
|
type PasswordHasher interface {
|
||||||
|
Hash(password string) (string, error)
|
||||||
|
Compare(hashedPassword, password string) error
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package crypto
|
||||||
|
|
||||||
|
import (
|
||||||
|
"apocapoc-api/internal/domain/services"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type BcryptHasher struct{}
|
||||||
|
|
||||||
|
func NewBcryptHasher() services.PasswordHasher {
|
||||||
|
return &BcryptHasher{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *BcryptHasher) Hash(password string) (string, error) {
|
||||||
|
hashedBytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(hashedBytes), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *BcryptHasher) Compare(hashedPassword, password string) error {
|
||||||
|
return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"apocapoc-api/internal/application/commands"
|
"apocapoc-api/internal/application/commands"
|
||||||
"apocapoc-api/internal/application/queries"
|
"apocapoc-api/internal/application/queries"
|
||||||
"apocapoc-api/internal/infrastructure/auth"
|
"apocapoc-api/internal/infrastructure/auth"
|
||||||
|
"apocapoc-api/internal/infrastructure/crypto"
|
||||||
"apocapoc-api/internal/infrastructure/persistence/sqlite"
|
"apocapoc-api/internal/infrastructure/persistence/sqlite"
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
@@ -32,13 +33,14 @@ func setupTestServer(t *testing.T) *TestServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
jwtService := auth.NewJWTService("test-secret", 24)
|
jwtService := auth.NewJWTService("test-secret", 24)
|
||||||
|
passwordHasher := crypto.NewBcryptHasher()
|
||||||
|
|
||||||
userRepo := sqlite.NewUserRepository(db)
|
userRepo := sqlite.NewUserRepository(db)
|
||||||
habitRepo := sqlite.NewHabitRepository(db)
|
habitRepo := sqlite.NewHabitRepository(db)
|
||||||
entryRepo := sqlite.NewHabitEntryRepository(db)
|
entryRepo := sqlite.NewHabitEntryRepository(db)
|
||||||
|
|
||||||
registerHandler := commands.NewRegisterUserHandler(userRepo)
|
registerHandler := commands.NewRegisterUserHandler(userRepo, passwordHasher)
|
||||||
loginHandler := queries.NewLoginUserHandler(userRepo)
|
loginHandler := queries.NewLoginUserHandler(userRepo, passwordHasher)
|
||||||
createHandler := commands.NewCreateHabitHandler(habitRepo)
|
createHandler := commands.NewCreateHabitHandler(habitRepo)
|
||||||
getTodaysHandler := queries.NewGetTodaysHabitsHandler(habitRepo, entryRepo)
|
getTodaysHandler := queries.NewGetTodaysHabitsHandler(habitRepo, entryRepo)
|
||||||
getUserHabitsHandler := queries.NewGetUserHabitsHandler(habitRepo)
|
getUserHabitsHandler := queries.NewGetUserHabitsHandler(habitRepo)
|
||||||
|
|||||||
Reference in New Issue
Block a user