Skip to content

Commit

Permalink
Merge pull request #21 from MGTheTrain/refactor/logger-integration
Browse files Browse the repository at this point in the history
Refactor/logger integration
  • Loading branch information
MGTheTrain authored Dec 16, 2024
2 parents 7ac3a85 + a927a74 commit 001e343
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 56 deletions.
4 changes: 3 additions & 1 deletion cmd/crypto-vault-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
- [ECDSA Example](#ecdsa-example)
- [PKCS#11 Signing and Verifying](#pkcs11-signing-and-verifying)
- [PKCS#11 key management operations](#pkcs11-key-management-operations)
- [Running the e2e-test](#running-the-e2e-test)


## Summary

Expand Down Expand Up @@ -125,6 +127,6 @@ go run crypto_vault_cli.go delete-object --token-label my-token --object-label m
go run crypto_vault_cli.go delete-object --token-label my-token --object-label my-rsa-key --object-type privkey
```

### Running the e2e-test
## Running the e2e-test

In order to e2e-test the entire flow from encryption to decryption, key management, signing, and verifying signatures as outlined in previous [Getting Started](#getting-started) sections run `go test ./crypto_vault_cli_test.go`.
4 changes: 1 addition & 3 deletions cmd/crypto-vault-cli/internal/commands/aes_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ func NewAESCommandHandler() *AESCommandHandler {
FilePath: "",
}

factory := &logger.LoggerFactory{}

logger, err := factory.NewLogger(loggerSettings)
logger, err := logger.GetLogger(loggerSettings)
if err != nil {
log.Panicf("Error creating logger: %v", err)
return nil
Expand Down
4 changes: 1 addition & 3 deletions cmd/crypto-vault-cli/internal/commands/ec_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ func NewECCommandHandler() *ECCommandHandler {
FilePath: "",
}

factory := &logger.LoggerFactory{}

logger, err := factory.NewLogger(loggerSettings)
logger, err := logger.GetLogger(loggerSettings)
if err != nil {
log.Panicf("Error creating logger: %v", err)
return nil
Expand Down
4 changes: 1 addition & 3 deletions cmd/crypto-vault-cli/internal/commands/pkcs11_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ func NewPKCS11CommandsHandler() *PKCS11CommandsHandler {
FilePath: "",
}

factory := &logger.LoggerFactory{}

logger, err := factory.NewLogger(loggerSettings)
logger, err := logger.GetLogger(loggerSettings)
if err != nil {
log.Panicf("Error creating logger: %v", err)
return nil
Expand Down
4 changes: 1 addition & 3 deletions cmd/crypto-vault-cli/internal/commands/rsa_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ func NewRSACommandHandler() *RSACommandHandler {
FilePath: "",
}

factory := &logger.LoggerFactory{}

logger, err := factory.NewLogger(loggerSettings)
logger, err := logger.GetLogger(loggerSettings)
if err != nil {
log.Panicf("Error creating logger: %v", err)
return nil
Expand Down
31 changes: 27 additions & 4 deletions internal/infrastructure/logger/loggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package logger
import (
"crypto_vault_service/internal/infrastructure/settings"
"fmt"
"sync"

"github.com/natefinch/lumberjack"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -95,11 +96,33 @@ func (l *FileLogger) Panic(args ...interface{}) {
l.logger.Panic(args...)
}

// LoggerFactory is a factory that creates different types of loggers.
type LoggerFactory struct{}
var (
// Singleton logger instance, shared across the application
loggerInstance Logger
loggerOnce sync.Once // Guarantees that the logger is created only once
)

// GetLogger returns a singleton logger instance, shared across the application.
func GetLogger(settings *settings.LoggerSettings) (Logger, error) {
// Ensure that the logger is created only once
loggerOnce.Do(func() {
// Create the logger based on the config
logger, err := newLogger(settings)
if err == nil {
loggerInstance = logger
}
})

// If the loggerInstance is already created, just return it
if loggerInstance != nil {
return loggerInstance, nil
}

return nil, fmt.Errorf("failed to create logger")
}

// NewLogger creates a logger based on the given configuration.
func (f *LoggerFactory) NewLogger(config *settings.LoggerSettings) (Logger, error) {
// newLogger creates a logger based on the given configuration.
func newLogger(config *settings.LoggerSettings) (Logger, error) {
err := config.Validate()
if err != nil {
return nil, err
Expand Down
4 changes: 1 addition & 3 deletions test/helpers/test_db_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ func SetupTestDB(t *testing.T) *TestDBContext {
FilePath: "",
}

factory := &logger.LoggerFactory{}

logger, err := factory.NewLogger(loggerSettings)
logger, err := logger.GetLogger(loggerSettings)
if err != nil {
log.Fatalf("Error creating logger: %v", err)
}
Expand Down
8 changes: 2 additions & 6 deletions test/integration/app/services/key_services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ func TestCryptoKeyUploadService_Upload_Success(t *testing.T) {
FilePath: "",
}

factory := &logger.LoggerFactory{}

logger, err := factory.NewLogger(loggerSettings)
logger, err := logger.GetLogger(loggerSettings)
if err != nil {
log.Fatalf("Error creating logger: %v", err)
}
Expand Down Expand Up @@ -138,9 +136,7 @@ func TestCryptoKeyDownloadService_Download_Success(t *testing.T) {
FilePath: "",
}

factory := &logger.LoggerFactory{}

logger, err := factory.NewLogger(loggerSettings)
logger, err := logger.GetLogger(loggerSettings)
if err != nil {
log.Fatalf("Error creating logger: %v", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ func TestAzureBlobConnector_Upload(t *testing.T) {
FilePath: "",
}

factory := &logger.LoggerFactory{}

logger, err := factory.NewLogger(loggerSettings)
logger, err := logger.GetLogger(loggerSettings)
if err != nil {
log.Fatalf("Error creating logger: %v", err)
}
Expand Down Expand Up @@ -66,9 +64,7 @@ func TestAzureBlobConnector_Download(t *testing.T) {
FilePath: "",
}

factory := &logger.LoggerFactory{}

logger, err := factory.NewLogger(loggerSettings)
logger, err := logger.GetLogger(loggerSettings)
if err != nil {
log.Fatalf("Error creating logger: %v", err)
}
Expand Down Expand Up @@ -109,9 +105,7 @@ func TestAzureBlobConnector_Delete(t *testing.T) {
FilePath: "",
}

factory := &logger.LoggerFactory{}

logger, err := factory.NewLogger(loggerSettings)
logger, err := logger.GetLogger(loggerSettings)
if err != nil {
log.Fatalf("Error creating logger: %v", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ func TestAzureVaultConnector_Upload(t *testing.T) {
FilePath: "",
}

factory := &logger.LoggerFactory{}

logger, err := factory.NewLogger(loggerSettings)
logger, err := logger.GetLogger(loggerSettings)
if err != nil {
log.Fatalf("Error creating logger: %v", err)
}
Expand Down Expand Up @@ -66,9 +64,7 @@ func TestAzureVaultConnector_Download(t *testing.T) {
FilePath: "",
}

factory := &logger.LoggerFactory{}

logger, err := factory.NewLogger(loggerSettings)
logger, err := logger.GetLogger(loggerSettings)
if err != nil {
log.Fatalf("Error creating logger: %v", err)
}
Expand Down Expand Up @@ -109,9 +105,7 @@ func TestAzureVaultConnector_Delete(t *testing.T) {
FilePath: "",
}

factory := &logger.LoggerFactory{}

logger, err := factory.NewLogger(loggerSettings)
logger, err := logger.GetLogger(loggerSettings)
if err != nil {
log.Fatalf("Error creating logger: %v", err)
}
Expand Down
4 changes: 1 addition & 3 deletions test/integration/infrastructure/cryptography/pkcs11_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ func NewPKCS11Test(t *testing.T, slotId, modulePath, label, soPin, userPin, obje
FilePath: "",
}

factory := &logger.LoggerFactory{}

logger, err := factory.NewLogger(loggerSettings)
logger, err := logger.GetLogger(loggerSettings)
if err != nil {
log.Fatalf("Error creating logger: %v", err)
}
Expand Down
4 changes: 1 addition & 3 deletions test/unit/infrastructure/cryptography/aes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ func NewAESTests(t *testing.T) *AESTests {
FilePath: "",
}

factory := &logger.LoggerFactory{}

logger, err := factory.NewLogger(loggerSettings)
logger, err := logger.GetLogger(loggerSettings)
if err != nil {
log.Fatalf("Error creating logger: %v", err)
}
Expand Down
4 changes: 1 addition & 3 deletions test/unit/infrastructure/cryptography/ec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ func NewECTests(t *testing.T) *ECTests {
FilePath: "",
}

factory := &logger.LoggerFactory{}

logger, err := factory.NewLogger(loggerSettings)
logger, err := logger.GetLogger(loggerSettings)
if err != nil {
log.Fatalf("Error creating logger: %v", err)
}
Expand Down
4 changes: 1 addition & 3 deletions test/unit/infrastructure/cryptography/rsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ func NewRSATests(t *testing.T) *RSATests {
FilePath: "",
}

factory := &logger.LoggerFactory{}

logger, err := factory.NewLogger(loggerSettings)
logger, err := logger.GetLogger(loggerSettings)
if err != nil {
log.Fatalf("Error creating logger: %v", err)
}
Expand Down

0 comments on commit 001e343

Please sign in to comment.