Skip to content

Commit

Permalink
DXE-3621 Merge pull request #200 from akamai/release/v3.4.5
Browse files Browse the repository at this point in the history
DXE-3621 Release/v3.4.5
  • Loading branch information
mgwoj committed Apr 3, 2024
2 parents 6ad3331 + d8378a0 commit 7017d36
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 16 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# RELEASE NOTES

## 3.4.5 (Apr 3, 2024)

#### BUG FIXES
* Fixed a bug where the `max_body` parameter was not utilized when generating the authentication header.
* Implemented support for the `max_body` parameter when the configuration is provided as function parameter.

#### IMPROVEMENTS:
* Updated various dependencies

## 3.4.4 (Nov 15, 2023)

#### IMPROVEMENTS:
Expand Down
9 changes: 5 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { AxiosError, AxiosResponse } from "axios";

declare class EdgeGrid {
constructor(clientTokenOrOptions: string | object,
clientSecret?: string,
accessToken?: string,
host?: string,
debug?: boolean);
clientSecret?: string,
accessToken?: string,
host?: string,
debug?: boolean,
max_body?: number);

request: object;
config: object;
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "akamai-edgegrid",
"version": "3.4.4",
"version": "3.4.5",
"description": "Authentication handler for the Akamai OPEN EdgeGrid Authentication scheme in Node.js",
"main": "index.js",
"scripts": {
Expand Down
12 changes: 7 additions & 5 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ const axios = require('axios'),
helpers = require('./helpers'),
logger = require('./logger');

const EdgeGrid = function (client_token, client_secret, access_token, host, debug) {
const EdgeGrid = function (client_token, client_secret, access_token, host, debug, max_body) {
// accepting an object containing a path to .edgerc and a config section
if (typeof arguments[0] === 'object') {
let edgercPath = arguments[0];
this._setConfigFromObj(edgercPath);
} else {
this._setConfigFromStrings(client_token, client_secret, access_token, host);
this._setConfigFromStrings(client_token, client_secret, access_token, host, max_body);
}
if (process.env.EG_VERBOSE || debug || (typeof arguments[0] === 'object' && arguments[0].debug)) {
axios.interceptors.request.use(request => {
Expand Down Expand Up @@ -75,7 +75,8 @@ EdgeGrid.prototype.auth = function (req) {
this.config.client_token,
this.config.client_secret,
this.config.access_token,
this.config.host
this.config.host,
this.config.max_body
);
if (req.headers['Accept'] === 'application/gzip' || req.headers['Accept'] === 'application/tar+gzip') {
this.request["responseType"] = 'arraybuffer';
Expand Down Expand Up @@ -123,7 +124,7 @@ EdgeGrid.prototype._handleRedirect = function (resp, callback) {
* @param {String} access_token The access token
* @param {String} host The host
*/
EdgeGrid.prototype._setConfigFromStrings = function (client_token, client_secret, access_token, host) {
EdgeGrid.prototype._setConfigFromStrings = function (client_token, client_secret, access_token, host, max_body) {
if (!validatedArgs([client_token, client_secret, access_token, host])) {
throw new Error('Insufficient Akamai credentials');
}
Expand All @@ -132,7 +133,8 @@ EdgeGrid.prototype._setConfigFromStrings = function (client_token, client_secret
client_token: client_token,
client_secret: client_secret,
access_token: access_token,
host: host.indexOf('https://') > -1 ? host : 'https://' + host
host: host.indexOf('https://') > -1 ? host : 'https://' + host,
max_body: helpers.getDefaultOrMaxBody(max_body)
};
};

Expand Down
5 changes: 2 additions & 3 deletions src/edgerc.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ function getSection(lines, sectionName) {
}

function validatedConfig(config) {

config.max_body = config.max_body || 131072
config.max_body = helpers.getDefaultOrMaxBody(config.max_body);

if (!(config.host && config.access_token &&
config.client_secret && config.client_token)) {
Expand Down Expand Up @@ -81,7 +80,7 @@ function buildObj(configs) {
if (index > -1 && !isComment) {
key = config.substr(0, index);
if (key.startsWith("max-body")) {
key = key.replace('-', '_')
key = key.replace('-', '_');
}
val = config.substring(index + 1);
// remove inline comments
Expand Down
7 changes: 7 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,11 @@ module.exports = {
}
return filePath;
},

getDefaultOrMaxBody:function(max_body){
if (max_body !== undefined && isNaN(Number(max_body))) {
throw new Error('max_body is not a valid number.');
}
return Number(max_body) || 131072;
}
};
104 changes: 103 additions & 1 deletion test/src/api_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ const assert = require('assert'),
path = require('path'),
Api = require('../../src/api');

const EdgeGrid = require("../../index");

describe('Api', function () {
beforeEach(function () {
this.api = new Api(
'clientToken',
'clientSecret',
'accessToken',
'base.com',
false,
false
);
});

Expand Down Expand Up @@ -566,5 +568,105 @@ describe('Api', function () {
});
});
});

describe('Builds the request using the properties of the local config Object (.edgerc file)', () => {
it('when max_body is provided in the config', () => {
const req = {
path: '/api/resource',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: {key: 'value'}
};

const edgercObject = {
path: path.resolve(__dirname, '../test_edgerc'),
section: 'custom:max_body'
};

const edgeGrid = new EdgeGrid(edgercObject);
// Call auth method
const response = edgeGrid.auth(req);
// Assertions
assert.strictEqual(response.config.max_body, 8192);
});

it('when max_body is provided in the config - NAN', () => {
const edgercObject = {
path: path.resolve(__dirname, '../test_edgerc'),
section: 'custom:max_body_NAN'
};
assert.throws(
function () {
return new EdgeGrid(edgercObject);
},
/max_body is not a valid number./
);
});

it('when max_body is not provided in the configuration, the default value is used', () => {
const req = {
path: '/api/resource',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: {key: 'value'}
};

const edgercObject = {
path: path.resolve(__dirname, '../test_edgerc'),
section: 'no-max-body'
};

const edgeGrid = new EdgeGrid(edgercObject, undefined, undefined, undefined, undefined, 1000);
// Call auth method
const response = edgeGrid.auth(req);
// Assertions
assert.strictEqual(response.config.max_body, 131072); // picks default max_body 131072
});
});

describe('Builds the request using the config as string parameters', () => {
it('when max_body is provided in the config', () => {
const req = {
path: '/api/resource',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: {key: 'value'}
};

const edgeGrid = new EdgeGrid('clientToken', 'clientSecret', 'accessToken', 'example.com', false, 8192);
// Call auth method
const response = edgeGrid.auth(req);
// Assertions
assert.strictEqual(response.config.max_body, 8192);
});


it('when max_body is not provided in the config', () => {
const req = {
path: '/api/resource',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: {key: 'value'}
};

const edgeGrid = new EdgeGrid('clientToken', 'clientSecret', 'accessToken', 'example.com');
// Call auth method
const response = edgeGrid.auth(req);
// Assertions
assert.strictEqual(response.config.max_body, 131072);
});
});
});
});
14 changes: 14 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,18 @@ describe('Signature Generation', function () {
assert.strictEqual(test_auth.headers.Authorization, expected_header);
});
});

describe('POST request when max_body is provided in .edgerc file', function () {
it('should return the expected string when the signing request is run.', function () {
const expected_header = "EG1-HMAC-SHA256 client_token=akab-client-token-xxx-xxxxxxxxxxxxxxxx;access_token=akab-access-token-xxx-xxxxxxxxxxxxxxxx;timestamp=20140321T19:34:21+0000;nonce=nonce-xx-xxxx-xxxx-xxxx-xxxxxxxxxxxx;signature=lUcTuRl/Iy5vmBp7uNvya9BoRaA9/oyHKC+pCDOlg1s=";
const data = "{\"name\":\"text24.devexp-cli-dns-test.net\",\"type\":\"SRV\",\"ttl\":300,\"zone\":\"devexp-cli-dns-test.net\",\"rdata\":[\"10 40 5061 small.example.com\",\"20 10 5060 tiny.example.com\"]}";
const request = {
"path": "testapi/v1/t3",
"method": "POST",
"body": data
};
test_auth = auth.generateAuth(request, client_token, client_secret, access_token, base_url, 169, nonce, timestamp);
assert.strictEqual(test_auth.headers.Authorization, expected_header);
});
});
});
7 changes: 7 additions & 0 deletions test/test_edgerc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ client_secret = sectionClientSecret
access_token = sectionAccessToken
max_body = 8192

[custom:max_body_NAN]
host = sectionexample.luna.akamaiapis.net
client_token = sectionClientToken
client_secret = sectionClientSecret
access_token = sectionAccessToken
max_body = 8A192

[no-max-body]
host = sectionexample.luna.akamaiapis.net
client_token = sectionClientToken
Expand Down

0 comments on commit 7017d36

Please sign in to comment.