Skip to content

Commit

Permalink
Merge pull request #62 from godaddy/FixUpLogging
Browse files Browse the repository at this point in the history
Overhaul logging
  • Loading branch information
jgowdy authored Oct 12, 2023
2 parents 98b60d5 + 3928ff3 commit c39f225
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 62 deletions.
28 changes: 14 additions & 14 deletions internal/asherah/asherah.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/aws/aws-sdk-go/aws"
awssession "github.com/aws/aws-sdk-go/aws/session"
"github.com/godaddy/asherah-cobhan/internal/log"
log "github.com/godaddy/asherah-cobhan/internal/log"
"github.com/godaddy/asherah/go/appencryption"
"github.com/godaddy/asherah/go/appencryption/pkg/crypto/aead"
"github.com/godaddy/asherah/go/appencryption/pkg/kms"
Expand All @@ -24,7 +24,7 @@ var ErrAsherahFailedInitialization = errors.New("asherah failed initialization")

func Setup(options *Options) error {
if atomic.LoadInt32(&globalInitialized) == 1 {
output.StderrDebugLog("Failed to initialize asherah: already initialized")
log.ErrorLog("Failed to initialize asherah: already initialized")
return ErrAsherahAlreadyInitialized
}

Expand Down Expand Up @@ -60,7 +60,7 @@ func Setup(options *Options) error {
)

if globalSessionFactory == nil {
output.StderrDebugLog("Failed to create session factory")
log.ErrorLog("Failed to create session factory")
return ErrAsherahFailedInitialization
}

Expand All @@ -77,13 +77,13 @@ func Shutdown() {

func Encrypt(partitionId string, data []byte) (*appencryption.DataRowRecord, error) {
if globalInitialized == 0 {
output.StderrDebugLog("Failed to encrypt data: asherah is not initialized")
log.ErrorLog("Failed to encrypt data: asherah is not initialized")
return nil, ErrAsherahNotInitialized
}

session, err := globalSessionFactory.GetSession(partitionId)
if err != nil {
output.StderrDebugLogf("Failed to get session for partition %v: %v", partitionId, err.Error())
log.ErrorLogf("Failed to get session for partition %v: %v", partitionId, err.Error())
return nil, err
}
defer session.Close()
Expand All @@ -99,7 +99,7 @@ func Decrypt(partitionId string, drr *appencryption.DataRowRecord) ([]byte, erro

session, err := globalSessionFactory.GetSession(partitionId)
if err != nil {
output.StderrDebugLogf("Failed to get session for partition %v: %v", partitionId, err.Error())
log.ErrorLogf("Failed to get session for partition %v: %v", partitionId, err.Error())
return nil, err
}
defer session.Close()
Expand All @@ -114,15 +114,15 @@ func NewMetastore(opts *Options) appencryption.Metastore {
// TODO: support other databases
db, err := newMysql(opts.ConnectionString)
if err != nil {
output.StderrDebugLogf("PANIC: Failed to connect to database: %v", err.Error())
log.ErrorLogf("PANIC: Failed to connect to database: %v", err.Error())
panic(err)
}

// set optional replica read consistency
if len(opts.ReplicaReadConsistency) > 0 {
err := setRdbmsReplicaReadConsistencyValue(opts.ReplicaReadConsistency)
if err != nil {
output.StderrDebugLogf("PANIC: Failed to set replica read consistency: %v", err.Error())
log.ErrorLogf("PANIC: Failed to set replica read consistency: %v", err.Error())
panic(err)
}
}
Expand Down Expand Up @@ -150,21 +150,21 @@ func NewMetastore(opts *Options) appencryption.Metastore {
// We don't warn if the user specifically asks for test-debug-memory
return persistence.NewMemoryMetastore()
case "memory":
output.StderrDebugLog("*** WARNING WARNING WARNING USING MEMORY METASTORE - THIS IS FOR TEST/DEBUG ONLY ***")
log.ErrorLog("*** WARNING WARNING WARNING USING MEMORY METASTORE - THIS IS FOR TEST/DEBUG ONLY ***")
return persistence.NewMemoryMetastore()
default:
output.StderrDebugLogf("PANIC: Unknown metastore type: %v", opts.Metastore)
log.ErrorLogf("PANIC: Unknown metastore type: %v", opts.Metastore)
panic("Unknown metastore type")
}
}

func NewKMS(opts *Options, crypto appencryption.AEAD) appencryption.KeyManagementService {
if opts.KMS == "static" {
output.StderrDebugLog("*** WARNING WARNING WARNING USING STATIC MASTER KEY - THIS IS FOR TEST/DEBUG ONLY ***")
log.ErrorLog("*** WARNING WARNING WARNING USING STATIC MASTER KEY - THIS IS FOR TEST/DEBUG ONLY ***")

m, err := kms.NewStatic("thisIsAStaticMasterKeyForTesting", aead.NewAES256GCM())
if err != nil {
output.StderrDebugLogf("PANIC: Failed to create static master key: %v", err.Error())
log.ErrorLogf("PANIC: Failed to create static master key: %v", err.Error())
panic(err)
}

Expand All @@ -173,7 +173,7 @@ func NewKMS(opts *Options, crypto appencryption.AEAD) appencryption.KeyManagemen
// We don't warn if the user specifically asks for test-debug-static
m, err := kms.NewStatic("thisIsAStaticMasterKeyForTesting", crypto)
if err != nil {
output.StderrDebugLogf("PANIC: Failed to create static master key: %v", err.Error())
log.ErrorLogf("PANIC: Failed to create static master key: %v", err.Error())
panic(err)
}

Expand All @@ -182,7 +182,7 @@ func NewKMS(opts *Options, crypto appencryption.AEAD) appencryption.KeyManagemen

m, err := kms.NewAWS(crypto, opts.PreferredRegion, opts.RegionMap)
if err != nil {
output.StderrDebugLogf("PANIC: Failed to create AWS KMS: %v", err.Error())
log.ErrorLogf("PANIC: Failed to create AWS KMS: %v", err.Error())
panic(err)
}

Expand Down
26 changes: 14 additions & 12 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,32 @@ import (
"os"
)

var VerboseLog func(interface{}) = nil
var VerboseLogf func(format string, args ...interface{}) = nil
var DebugLog func(interface{}) = nil
var DebugLogf func(format string, args ...interface{}) = nil
var ErrorLog func(interface{}) = stderrDebugLog
var ErrorLogf func(format string, args ...interface{}) = stderrDebugLogf

func EnableVerboseLog(flag bool) {
if flag {
VerboseLog = StderrDebugLog
VerboseLogf = StderrDebugLogf
VerboseLog("asherah-cobhan: Enabled debug log")
DebugLog = stderrDebugLog
DebugLogf = stderrDebugLogf
DebugLog("asherah-cobhan: Enabled debug log")
} else {
VerboseLog = NullDebugLog
VerboseLogf = NullDebugLogf
DebugLog = nullDebugLog
DebugLogf = nullDebugLogf
}
}

func StderrDebugLog(output interface{}) {
func stderrDebugLog(output interface{}) {
fmt.Fprintf(os.Stderr, "asherah-cobhan: %#v\n", output)
}

func StderrDebugLogf(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, "asherah-cobhan:"+format+"\n", args...)
func stderrDebugLogf(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, "asherah-cobhan: "+format+"\n", args...)
}

func NullDebugLog(output interface{}) {
func nullDebugLog(output interface{}) {
}

func NullDebugLogf(format string, args ...interface{}) {
func nullDebugLogf(format string, args ...interface{}) {
}
Loading

0 comments on commit c39f225

Please sign in to comment.