From cb2acd13d4705a4fa1dd1a643137f6da81319eff Mon Sep 17 00:00:00 2001 From: Jeremiah Gowdy Date: Thu, 12 Oct 2023 08:42:29 -0700 Subject: [PATCH] Clean up logging naming --- .gitignore | 1 + internal/asherah/asherah.go | 28 +++++++-------- internal/log/log.go | 34 ++++++++++++++++++ internal/output/output.go | 34 ------------------ libasherah.go | 72 ++++++++++++++++++------------------- 5 files changed, 85 insertions(+), 84 deletions(-) create mode 100644 internal/log/log.go delete mode 100644 internal/output/output.go diff --git a/.gitignore b/.gitignore index c581a6d..1506bdb 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ .idea asherah-cobhan +output/ diff --git a/internal/asherah/asherah.go b/internal/asherah/asherah.go index b1b4d49..a38e0ff 100644 --- a/internal/asherah/asherah.go +++ b/internal/asherah/asherah.go @@ -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" @@ -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 } @@ -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 } @@ -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() @@ -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() @@ -114,7 +114,7 @@ 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) } @@ -122,7 +122,7 @@ func NewMetastore(opts *Options) appencryption.Metastore { 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) } } @@ -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) } @@ -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) } @@ -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) } diff --git a/internal/log/log.go b/internal/log/log.go new file mode 100644 index 0000000..5a06460 --- /dev/null +++ b/internal/log/log.go @@ -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{}) { +} diff --git a/internal/output/output.go b/internal/output/output.go deleted file mode 100644 index dd8a09a..0000000 --- a/internal/output/output.go +++ /dev/null @@ -1,34 +0,0 @@ -package output - -import ( - "fmt" - "os" -) - -var VerboseOutput func(interface{}) = nil -var VerboseOutputf func(format string, args ...interface{}) = nil - -func EnableVerboseOutput(flag bool) { - if flag { - VerboseOutput = StderrDebugOutput - VerboseOutputf = StderrDebugOutputf - VerboseOutput("asherah-cobhan: Enabled debug output") - } else { - VerboseOutput = NullDebugOutput - VerboseOutputf = NullDebugOutputf - } -} - -func StderrDebugOutput(output interface{}) { - fmt.Fprintf(os.Stderr, "asherah-cobhan: %#v\n", output) -} - -func StderrDebugOutputf(format string, args ...interface{}) { - fmt.Fprintf(os.Stderr, "asherah-cobhan:"+format+"\n", args...) -} - -func NullDebugOutput(output interface{}) { -} - -func NullDebugOutputf(format string, args ...interface{}) { -} diff --git a/libasherah.go b/libasherah.go index 71283e1..f02956f 100644 --- a/libasherah.go +++ b/libasherah.go @@ -14,7 +14,7 @@ import ( "unsafe" "github.com/godaddy/asherah-cobhan/internal/asherah" - "github.com/godaddy/asherah-cobhan/internal/output" + "github.com/godaddy/asherah-cobhan/internal/log" "github.com/godaddy/asherah/go/appencryption" ) @@ -25,7 +25,7 @@ func main() { //export Shutdown func Shutdown() { - output.VerboseOutput("Asherah shutdown") + output.VerboseLog("Asherah shutdown") asherah.Shutdown() } @@ -46,7 +46,7 @@ func SetEnv(envJson unsafe.Pointer) int32 { result := cobhan.BufferToJsonStruct(envJson, &env) if result != cobhan.ERR_NONE { - output.StderrDebugOutputf("Failed to deserialize environment JSON string %v", cobhan.CobhanErrorToString(result)) + output.StderrDebugLogf("Failed to deserialize environment JSON string %v", cobhan.CobhanErrorToString(result)) return result } @@ -63,35 +63,35 @@ func SetupJson(configJson unsafe.Pointer) int32 { options := &asherah.Options{} result := cobhan.BufferToJsonStruct(configJson, options) if result != cobhan.ERR_NONE { - output.StderrDebugOutputf("Failed to deserialize configuration string %v", cobhan.CobhanErrorToString(result)) + output.StderrDebugLogf("Failed to deserialize configuration string %v", cobhan.CobhanErrorToString(result)) configString, stringResult := cobhan.BufferToString(configJson) if stringResult != cobhan.ERR_NONE { - output.StderrDebugOutputf("Could not convert configJson to string: %v", cobhan.CobhanErrorToString(result)) + output.StderrDebugLogf("Could not convert configJson to string: %v", cobhan.CobhanErrorToString(result)) return result } - output.StderrDebugOutputf("Could not deserialize: %v", configString) + output.StderrDebugLogf("Could not deserialize: %v", configString) return result } - output.EnableVerboseOutput(options.Verbose) + output.EnableVerboseLog(options.Verbose) - output.VerboseOutput("Successfully deserialized config JSON") + output.VerboseLog("Successfully deserialized config JSON") EstimatedIntermediateKeyOverhead = len(options.ProductID) + len(options.ServiceName) err := asherah.Setup(options) if err == asherah.ErrAsherahAlreadyInitialized { - output.StderrDebugOutput("Setup failed: asherah is already initialized") - output.StderrDebugOutputf("Setup: asherah.Setup returned %v", err) + output.StderrDebugLog("Setup failed: asherah is already initialized") + output.StderrDebugLogf("Setup: asherah.Setup returned %v", err) return ERR_ALREADY_INITIALIZED } if err != nil { - output.StderrDebugOutput("Setup failed due to bad config?") - output.StderrDebugOutputf("Setup: asherah.Setup returned %v", err) + output.StderrDebugLog("Setup failed due to bad config?") + output.StderrDebugLogf("Setup: asherah.Setup returned %v", err) return ERR_BAD_CONFIG } - output.VerboseOutput("Successfully configured asherah") + output.VerboseLog("Successfully configured asherah") return cobhan.ERR_NONE } @@ -112,19 +112,19 @@ func Decrypt(partitionIdPtr unsafe.Pointer, encryptedDataPtr unsafe.Pointer, enc created int64, parentKeyIdPtr unsafe.Pointer, parentKeyCreated int64, outputDecryptedDataPtr unsafe.Pointer) int32 { encryptedData, result := cobhan.BufferToBytes(encryptedDataPtr) if result != cobhan.ERR_NONE { - output.StderrDebugOutputf("Decrypt failed: Failed to convert encryptedDataPtr cobhan buffer to bytes %v", cobhan.CobhanErrorToString(result)) + output.StderrDebugLogf("Decrypt failed: Failed to convert encryptedDataPtr cobhan buffer to bytes %v", cobhan.CobhanErrorToString(result)) return result } encryptedKey, result := cobhan.BufferToBytes(encryptedKeyPtr) if result != cobhan.ERR_NONE { - output.StderrDebugOutputf("Decrypt failed: Failed to convert encryptedKeyPtr cobhan buffer to bytes %v", cobhan.CobhanErrorToString(result)) + output.StderrDebugLogf("Decrypt failed: Failed to convert encryptedKeyPtr cobhan buffer to bytes %v", cobhan.CobhanErrorToString(result)) return result } parentKeyId, result := cobhan.BufferToString(parentKeyIdPtr) if result != cobhan.ERR_NONE { - output.StderrDebugOutputf("Decrypt failed: Failed to convert parentKeyIdPtr cobhan buffer to string %v", cobhan.CobhanErrorToString(result)) + output.StderrDebugLogf("Decrypt failed: Failed to convert parentKeyIdPtr cobhan buffer to string %v", cobhan.CobhanErrorToString(result)) return result } @@ -142,8 +142,8 @@ func Decrypt(partitionIdPtr unsafe.Pointer, encryptedDataPtr unsafe.Pointer, enc data, result, err := decryptData(partitionIdPtr, &drr) if result != cobhan.ERR_NONE { - output.StderrDebugOutputf("Failed to decrypt data %v", cobhan.CobhanErrorToString(result)) - output.StderrDebugOutputf("Decrypt: decryptData returned %v", err) + output.StderrDebugLogf("Failed to decrypt data %v", cobhan.CobhanErrorToString(result)) + output.StderrDebugLogf("Decrypt: decryptData returned %v", err) return result } @@ -157,39 +157,39 @@ func Encrypt(partitionIdPtr unsafe.Pointer, dataPtr unsafe.Pointer, outputEncryp drr, result, err := encryptData(partitionIdPtr, dataPtr) if result != cobhan.ERR_NONE { - output.StderrDebugOutputf("Failed to encrypt data %v", cobhan.CobhanErrorToString(result)) - output.StderrDebugOutputf("Encrypt failed: encryptData returned %v", err) + output.StderrDebugLogf("Failed to encrypt data %v", cobhan.CobhanErrorToString(result)) + output.StderrDebugLogf("Encrypt failed: encryptData returned %v", err) return result } result = cobhan.BytesToBuffer(drr.Data, outputEncryptedDataPtr) if result != cobhan.ERR_NONE { - output.StderrDebugOutputf("Encrypted data length: %v", len(drr.Data)) - output.StderrDebugOutputf("Encrypt failed: BytesToBuffer returned %v for outputEncryptedDataPtr", cobhan.CobhanErrorToString(result)) + output.StderrDebugLogf("Encrypted data length: %v", len(drr.Data)) + output.StderrDebugLogf("Encrypt failed: BytesToBuffer returned %v for outputEncryptedDataPtr", cobhan.CobhanErrorToString(result)) return result } result = cobhan.BytesToBuffer(drr.Key.EncryptedKey, outputEncryptedKeyPtr) if result != cobhan.ERR_NONE { - output.StderrDebugOutputf("Encrypt failed: BytesToBuffer returned %v for outputEncryptedKeyPtr", cobhan.CobhanErrorToString(result)) + output.StderrDebugLogf("Encrypt failed: BytesToBuffer returned %v for outputEncryptedKeyPtr", cobhan.CobhanErrorToString(result)) return result } result = cobhan.Int64ToBuffer(drr.Key.Created, outputCreatedPtr) if result != cobhan.ERR_NONE { - output.StderrDebugOutputf("Encrypt failed: Int64ToBuffer returned %v for outputCreatedPtr", cobhan.CobhanErrorToString(result)) + output.StderrDebugLogf("Encrypt failed: Int64ToBuffer returned %v for outputCreatedPtr", cobhan.CobhanErrorToString(result)) return result } result = cobhan.StringToBuffer(drr.Key.ParentKeyMeta.ID, outputParentKeyIdPtr) if result != cobhan.ERR_NONE { - output.StderrDebugOutputf("Encrypt failed: BytesToBuffer returned %v for outputParentKeyIdPtr", cobhan.CobhanErrorToString(result)) + output.StderrDebugLogf("Encrypt failed: BytesToBuffer returned %v for outputParentKeyIdPtr", cobhan.CobhanErrorToString(result)) return result } result = cobhan.Int64ToBuffer(drr.Key.ParentKeyMeta.Created, outputParentKeyCreatedPtr) if result != cobhan.ERR_NONE { - output.StderrDebugOutputf("Encrypt failed: BytesToBuffer returned %v for outputParentKeyCreatedPtr", cobhan.CobhanErrorToString(result)) + output.StderrDebugLogf("Encrypt failed: BytesToBuffer returned %v for outputParentKeyCreatedPtr", cobhan.CobhanErrorToString(result)) return result } @@ -200,8 +200,8 @@ func Encrypt(partitionIdPtr unsafe.Pointer, dataPtr unsafe.Pointer, outputEncryp func EncryptToJson(partitionIdPtr unsafe.Pointer, dataPtr unsafe.Pointer, jsonPtr unsafe.Pointer) int32 { drr, result, err := encryptData(partitionIdPtr, dataPtr) if result != cobhan.ERR_NONE { - output.StderrDebugOutputf("Failed to encrypt data %v", cobhan.CobhanErrorToString(result)) - output.StderrDebugOutputf("EncryptToJson failed: encryptData returned %v", err) + output.StderrDebugLogf("Failed to encrypt data %v", cobhan.CobhanErrorToString(result)) + output.StderrDebugLogf("EncryptToJson failed: encryptData returned %v", err) return result } @@ -210,11 +210,11 @@ func EncryptToJson(partitionIdPtr unsafe.Pointer, dataPtr unsafe.Pointer, jsonPt if result == cobhan.ERR_BUFFER_TOO_SMALL { outputBytes, err := json.Marshal(drr) if err == nil { - output.StderrDebugOutputf("EncryptToJson failed: JsonToBuffer: Output buffer needed %v bytes", len(outputBytes)) + output.StderrDebugLogf("EncryptToJson failed: JsonToBuffer: Output buffer needed %v bytes", len(outputBytes)) return result } } - output.StderrDebugOutputf("EncryptToJson failed: JsonToBuffer returned %v for jsonPtr", cobhan.CobhanErrorToString(result)) + output.StderrDebugLogf("EncryptToJson failed: JsonToBuffer returned %v for jsonPtr", cobhan.CobhanErrorToString(result)) return result } @@ -226,24 +226,24 @@ func DecryptFromJson(partitionIdPtr unsafe.Pointer, jsonPtr unsafe.Pointer, data var drr appencryption.DataRowRecord result := cobhan.BufferToJsonStruct(jsonPtr, &drr) if result != cobhan.ERR_NONE { - output.StderrDebugOutputf("DecryptFromJson failed: Failed to convert cobhan buffer to JSON structs %v", cobhan.CobhanErrorToString(result)) + output.StderrDebugLogf("DecryptFromJson failed: Failed to convert cobhan buffer to JSON structs %v", cobhan.CobhanErrorToString(result)) return result } data, result, err := decryptData(partitionIdPtr, &drr) if result != cobhan.ERR_NONE { - output.StderrDebugOutputf("Failed to decrypt data %v", cobhan.CobhanErrorToString(result)) - output.StderrDebugOutputf("DecryptFromJson failed: decryptData returned %v", err) + output.StderrDebugLogf("Failed to decrypt data %v", cobhan.CobhanErrorToString(result)) + output.StderrDebugLogf("DecryptFromJson failed: decryptData returned %v", err) return result } result = cobhan.BytesToBuffer(data, dataPtr) if result != cobhan.ERR_NONE { if result == cobhan.ERR_BUFFER_TOO_SMALL { - output.StderrDebugOutputf("DecryptFromJson: BytesToBuffer: Output buffer needed %v bytes", len(data)) + output.StderrDebugLogf("DecryptFromJson: BytesToBuffer: Output buffer needed %v bytes", len(data)) return result } - output.StderrDebugOutputf("DecryptFromJson failed: BytesToBuffer returned %v for dataPtr", cobhan.CobhanErrorToString(result)) + output.StderrDebugLogf("DecryptFromJson failed: BytesToBuffer returned %v for dataPtr", cobhan.CobhanErrorToString(result)) return result } @@ -278,7 +278,7 @@ func decryptData(partitionIdPtr unsafe.Pointer, drr *appencryption.DataRowRecord partitionId, result := cobhan.BufferToString(partitionIdPtr) if result != cobhan.ERR_NONE { errorMessage := fmt.Sprintf("decryptData failed: Failed to convert cobhan buffer to string %v", cobhan.CobhanErrorToString(result)) - output.StderrDebugOutputf(errorMessage) + output.StderrDebugLogf(errorMessage) return nil, result, errors.New(errorMessage) }