Skip to content

Commit

Permalink
Merge pull request #460 from dadi/patch/accept-full-config-for-remote
Browse files Browse the repository at this point in the history
Accept full config for domains
  • Loading branch information
jimlambie committed Nov 15, 2018
2 parents 44aeaa6 + f57c2f9 commit 792014e
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 78 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ domains/
images/
public/
test/
workspace/_exif/
workspace/_tmp/
log/
cache/
11 changes: 6 additions & 5 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,10 @@ const schema = {
allowDomainOverride: true
},
path: {
doc: 'The remote host to request images from, for example http://media.example.com',
doc: 'The path to the assets directory',
format: String,
default: './public'
default: './public',
allowDomainOverride: true
}
},
s3: {
Expand Down Expand Up @@ -422,14 +423,14 @@ const schema = {
clientId: {
doc: 'Client ID used to access protected endpoints',
format: String,
default: '1235488',
default: null,
env: 'AUTH_TOKEN_ID',
allowDomainOverride: true
},
secret: {
doc: 'Client secret used to access protected endpoints',
format: String,
default: 'asd544see68e52',
default: null,
env: 'AUTH_TOKEN_SECRET',
allowDomainOverride: true
},
Expand All @@ -444,7 +445,7 @@ const schema = {
doc: 'Private key for signing JSON Web Tokens',
format: String,
env: 'AUTH_KEY',
default: 'YOU-MUST-CHANGE-ME-NOW!',
default: null,
allowDomainOverride: true
}
},
Expand Down
3 changes: 2 additions & 1 deletion config/config.test.json.sample
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
},
"auth": {
"clientId": "test",
"secret": "test"
"secret": "test",
"privateKey": "test"
},
"cloudfront": {
"accessKey": "",
Expand Down
14 changes: 14 additions & 0 deletions dadi/lib/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ module.exports = function (router) {
let clientId = req.body.clientId
let secret = req.body.secret

// Fail if the auth.clientId or auth.secret haven't been set.
if (!clientId || !secret) {
return fail('NoAccess', res)
}

// Fail if the auth.privateKey hasn't been set.
if (!config.get('auth.privateKey')) {
return fail('NoPrivateKey', res)
}

// Fail if the auth.clientId and auth.secret don't match the configured values.
if (
clientId !== config.get('auth.clientId', req.__domain) ||
secret !== config.get('auth.secret', req.__domain)
Expand Down Expand Up @@ -104,6 +115,9 @@ module.exports = function (router) {
case 'InvalidToken':
res.setHeader('WWW-Authenticate', 'Bearer, error="invalid_token", error_description="Invalid or expired access token"')
break
case 'NoPrivateKey':
res.setHeader('WWW-Authenticate', 'Bearer, error="no_private_key", error_description="No private key configured in auth.privateKey"')
break
default:
res.setHeader('WWW-Authenticate', 'Bearer realm="/token"')
}
Expand Down
60 changes: 12 additions & 48 deletions dadi/lib/controller/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,8 @@ module.exports.post = (req, res) => {

domains.forEach(item => {
if (!DomainManager.getDomain(item.domain)) {
// Prepare the domain configuration.
let configContent = {
images: {
directory: {
enabled: false
},
remote: {
enabled: true,
path: item.data.remote.path
}
},
assets: {
directory: {
enabled: false
},
remote: {
enabled: true,
path: item.data.remote.path
}
}
}

// Add the domain configuration.
DomainManager.addDomain(item.domain, configContent)
DomainManager.addDomain(item.domain, item.data)
}
})

Expand All @@ -55,11 +33,19 @@ module.exports.post = (req, res) => {
* Accept PUT requests for modifying domains in the internal domain configuration.
*/
module.exports.put = (req, res) => {
// Don't accept an empty body
if (!req.body || !req.body.data) {
return help.sendBackJSON(400, {
success: false,
errors: ['Bad Request']
}, res)
}

let domain = req.params.domain
let payload = req.body
let configSchema = req.body.data

// Don't accept an empty param.
if (!domain || Object.keys(payload).length === 0) {
if (!domain || Object.keys(configSchema).length === 0) {
return help.sendBackJSON(400, {
success: false,
errors: ['Bad Request']
Expand All @@ -74,30 +60,8 @@ module.exports.put = (req, res) => {
}, res)
}

// Prepare the domain configuration.
let configContent = {
images: {
directory: {
enabled: false
},
remote: {
enabled: true,
path: payload.remote.path
}
},
assets: {
directory: {
enabled: false
},
remote: {
enabled: true,
path: payload.remote.path
}
}
}

// Update the domain configuration.
DomainManager.addDomain(domain, configContent)
DomainManager.addDomain(domain, configSchema)

return help.sendBackJSON(200, {
success: true,
Expand Down
60 changes: 55 additions & 5 deletions test/acceptance/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ describe('Authentication', function () {
})
})

beforeEach(done => {
config.set('auth.clientId', 'test')
config.set('auth.secret', 'test')
config.set('auth.privateKey', 'test')
done()
})

after(done => {
app.stop(done)
})
Expand Down Expand Up @@ -55,6 +62,41 @@ describe('Authentication', function () {
.expect(401, done)
})

it('should not issue token if credentials are the null defaults', done => {
config.set('auth.clientId', null)
config.set('auth.secret', null)

request(cdnUrl)
.post(tokenRoute)
.send({
clientId: 'test123',
secret: 'badSecret',
code: ' '
})
.end((err, res) => {
res.statusCode.should.eql(401)
res.headers['www-authenticate'].should.eql('Bearer realm="/token"')
done()
})
})

it('should not issue token if privateKey for token signing is not set', done => {
config.set('auth.privateKey', null)

request(cdnUrl)
.post(tokenRoute)
.send({
clientId: 'test123',
secret: 'badSecret',
code: ' '
})
.end((err, res) => {
res.statusCode.should.eql(401)
res.headers['www-authenticate'].should.eql('Bearer, error="no_private_key", error_description="No private key configured in auth.privateKey"')
done()
})
})

it('should allow `/api/flush` request containing token', done => {
help.getBearerToken((err, token) => {
request(cdnUrl)
Expand Down Expand Up @@ -113,19 +155,27 @@ describe('Authentication', function () {
config.set('multiDomain.directory', 'domains')

config.loadDomainConfigs()

config.set('auth.clientId', 'testxyz', 'testdomain.com')
config.set('auth.secret', 'testabc', 'testdomain.com')
config.set('auth.privateKey', 'test123', 'testdomain.com')
})

after(() => {
config.set('multiDomain.enabled', configBackup.multiDomain.enabled)
config.set('multiDomain.directory', configBackup.multiDomain.directory)

config.set('auth.clientId', 'test', 'testdomain.com')
config.set('auth.secret', 'test', 'testdomain.com')
config.set('auth.privateKey', 'test', 'testdomain.com')
})

it('should encode the domain in the JWT', done => {
request(cdnUrl)
.post(tokenRoute)
.send({
clientId: 'test',
secret: 'test'
clientId: 'testxyz',
secret: 'testabc'
})
.set('host', 'testdomain.com:80')
.expect('content-type', 'application/json')
Expand All @@ -139,7 +189,7 @@ describe('Authentication', function () {

jwt.verify(
res.body.accessToken,
config.get('auth.privateKey'),
config.get('auth.privateKey', 'testdomain.com'),
(err, decoded) => {
if (err) return done(err)

Expand All @@ -155,8 +205,8 @@ describe('Authentication', function () {
request(cdnUrl)
.post(tokenRoute)
.send({
clientId: 'test',
secret: 'test'
clientId: 'testxyz',
secret: 'testabc'
})
.set('host', 'testdomain.com:80')
.expect('content-type', 'application/json')
Expand Down
4 changes: 4 additions & 0 deletions test/acceptance/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ describe('Cache', function () {
before(() => {
config.set('multiDomain.enabled', true)
config.loadDomainConfigs()

config.set('auth.clientId', 'test')
config.set('auth.secret', 'test')
config.set('auth.privateKey', 'test')
})

after(() => {
Expand Down
Loading

0 comments on commit 792014e

Please sign in to comment.