diff --git a/edgegrid/signer.go b/edgegrid/signer.go index feb8db57..e256a65a 100644 --- a/edgegrid/signer.go +++ b/edgegrid/signer.go @@ -16,7 +16,7 @@ import ( "unicode" "github.com/google/uuid" - log "github.com/sirupsen/logrus" + "github.com/sirupsen/logrus" ) const defaultSection = "DEFAULT" @@ -24,15 +24,16 @@ const defaultSection = "DEFAULT" // AddRequestHeader sets the Authorization header to use Akamai Open API func AddRequestHeader(config Config, req *http.Request) *http.Request { - if EdgegridLog != nil && config.Debug { - log.SetOutput(LogFile) // If app is using logging, outut to same location - } - - if config.Debug { - log.SetLevel(log.DebugLevel) + if EdgegridLog == nil { + SetupLogging() + if config.Debug { + EdgegridLog.SetLevel(logrus.DebugLevel) + } } timestamp := makeEdgeTimeStamp() + EdgegridLog.Debugf("Timestamp: '%s'", timestamp) nonce := createNonce() + EdgegridLog.Debugf("Nonce: '%s'", nonce) if req.Header.Get("Content-Type") == "" { req.Header.Set("Content-Type", "application/json") @@ -77,7 +78,7 @@ func makeEdgeTimeStamp() string { func createNonce() string { uuid, err := uuid.NewRandom() if err != nil { - log.Errorf(errorMap[ErrUUIDGenerateFailed], err) + EdgegridLog.Errorf(errorMap[ErrUUIDGenerateFailed], err) return "" } return uuid.String() @@ -163,19 +164,19 @@ func createContentHash(config Config, req *http.Request) string { preparedBody = string(bodyBytes) } - log.Debugf("Body is %s", preparedBody) + EdgegridLog.Debugf("Body is %s", preparedBody) if req.Method == "POST" && len(preparedBody) > 0 { - log.Debugf("Signing content: %s", preparedBody) + EdgegridLog.Debugf("Signing content: %s", preparedBody) if len(preparedBody) > config.MaxBody { - log.Debugf("Data length %d is larger than maximum %d", + EdgegridLog.Debugf("Data length %d is larger than maximum %d", len(preparedBody), config.MaxBody) preparedBody = preparedBody[0:config.MaxBody] - log.Debugf("Data truncated to %d for computing the hash", len(preparedBody)) + EdgegridLog.Debugf("Data truncated to %d for computing the hash", len(preparedBody)) } contentHash = createHash(preparedBody) } - log.Debugf("Content hash is '%s'", contentHash) + EdgegridLog.Debugf("Content hash is '%s'", contentHash) return contentHash } @@ -193,7 +194,7 @@ func signingData(config Config, req *http.Request, authHeader string) string { createContentHash(config, req), authHeader, } - log.Debugf("Data to sign %s", strings.Join(dataSign, "\t")) + EdgegridLog.Debugf("Data to sign %s", strings.Join(dataSign, "\t")) return strings.Join(dataSign, "\t") } @@ -212,10 +213,10 @@ func createAuthHeader(config Config, req *http.Request, timestamp string, nonce timestamp, nonce, ) - log.Debugf("Unsigned authorization header: '%s'", authHeader) + EdgegridLog.Debugf("Unsigned authorization header: '%s'", authHeader) signedAuthHeader := fmt.Sprintf("%ssignature=%s", authHeader, signingRequest(config, req, authHeader, timestamp)) - log.Debugf("Signed authorization header: '%s'", signedAuthHeader) + EdgegridLog.Debugf("Signed authorization header: '%s'", signedAuthHeader) return signedAuthHeader } diff --git a/edgegrid/signer_test.go b/edgegrid/signer_test.go index ad7542ae..5cb3ffe5 100644 --- a/edgegrid/signer_test.go +++ b/edgegrid/signer_test.go @@ -88,6 +88,7 @@ func TestCreateAuthHeader(t *testing.T) { req.Header.Set(k, v) } } + SetupLogging() actual := createAuthHeader(config, req, timestamp, nonce) if assert.Equal(t, edge.ExpectedAuthorization, actual, fmt.Sprintf("Fail: %s", edge.Name)) { t.Logf("Pass: %s\n", edge.Name)