Skip to content

Commit

Permalink
Merge pull request #61 from godaddy/UpdateLogs
Browse files Browse the repository at this point in the history
Clean up logging naming
  • Loading branch information
jgowdy committed Oct 12, 2023
2 parents eb08852 + cb2acd1 commit 98b60d5
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 84 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@

.idea
asherah-cobhan
output/
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/output"
"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.StderrDebugOutput("Failed to initialize asherah: already initialized")
output.StderrDebugLog("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.StderrDebugOutput("Failed to create session factory")
output.StderrDebugLog("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.StderrDebugOutput("Failed to encrypt data: asherah is not initialized")
output.StderrDebugLog("Failed to encrypt data: asherah is not initialized")
return nil, ErrAsherahNotInitialized
}

session, err := globalSessionFactory.GetSession(partitionId)
if err != nil {
output.StderrDebugOutputf("Failed to get session for partition %v: %v", partitionId, err.Error())
output.StderrDebugLogf("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.StderrDebugOutputf("Failed to get session for partition %v: %v", partitionId, err.Error())
output.StderrDebugLogf("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.StderrDebugOutputf("PANIC: Failed to connect to database: %v", err.Error())
output.StderrDebugLogf("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.StderrDebugOutputf("PANIC: Failed to set replica read consistency: %v", err.Error())
output.StderrDebugLogf("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.StderrDebugOutput("*** WARNING WARNING WARNING USING MEMORY METASTORE - THIS IS FOR TEST/DEBUG ONLY ***")
output.StderrDebugLog("*** WARNING WARNING WARNING USING MEMORY METASTORE - THIS IS FOR TEST/DEBUG ONLY ***")
return persistence.NewMemoryMetastore()
default:
output.StderrDebugOutputf("PANIC: Unknown metastore type: %v", opts.Metastore)
output.StderrDebugLogf("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.StderrDebugOutput("*** WARNING WARNING WARNING USING STATIC MASTER KEY - THIS IS FOR TEST/DEBUG ONLY ***")
output.StderrDebugLog("*** WARNING WARNING WARNING USING STATIC MASTER KEY - THIS IS FOR TEST/DEBUG ONLY ***")

m, err := kms.NewStatic("thisIsAStaticMasterKeyForTesting", aead.NewAES256GCM())
if err != nil {
output.StderrDebugOutputf("PANIC: Failed to create static master key: %v", err.Error())
output.StderrDebugLogf("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.StderrDebugOutputf("PANIC: Failed to create static master key: %v", err.Error())
output.StderrDebugLogf("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.StderrDebugOutputf("PANIC: Failed to create AWS KMS: %v", err.Error())
output.StderrDebugLogf("PANIC: Failed to create AWS KMS: %v", err.Error())
panic(err)
}

Expand Down
34 changes: 34 additions & 0 deletions internal/log/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package output

import (
"fmt"
"os"
)

var VerboseLog func(interface{}) = nil
var VerboseLogf func(format string, args ...interface{}) = nil

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

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 NullDebugLog(output interface{}) {
}

func NullDebugLogf(format string, args ...interface{}) {
}
34 changes: 0 additions & 34 deletions internal/output/output.go

This file was deleted.

0 comments on commit 98b60d5

Please sign in to comment.