Skip to content

Commit

Permalink
Merge pull request #99 from akamai/dns_maint
Browse files Browse the repository at this point in the history
Dns maint
  • Loading branch information
edglynes authored Jun 22, 2020
2 parents 238d20b + 3fc87ad commit c581a3c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
33 changes: 17 additions & 16 deletions edgegrid/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,24 @@ import (
"unicode"

"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
)

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")
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
}

Expand All @@ -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")
}

Expand All @@ -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
}
1 change: 1 addition & 0 deletions edgegrid/signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c581a3c

Please sign in to comment.