Skip to content

Commit

Permalink
Will set permissions based on api.json rather than what functions wer…
Browse files Browse the repository at this point in the history
…e deployed (#75)
  • Loading branch information
southpolesteve authored Sep 2, 2016
1 parent eea5d7f commit 7ec37a5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
21 changes: 20 additions & 1 deletion src/deploy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,30 @@ export default function(opts){
.tap(() => done(pushApiTask))
.then((id) => {
return promoteAliases(funcs)
.map((alias) => setPermission(alias, id, env) )
.then(() => setPermissions(api, id, env) )
.tap(() => done(permissionsTask))
.return(id)
})
.then((id) => deployApi(id, env))
.tap(() => done(deployTask))
}
}

function setPermissions(api, id, env){
let promises = []
for (var path in api.paths){
for (var method in api.paths[path]){
if (api.paths[path][method]['x-amazon-apigateway-integration'].type === 'aws') {
const uri = api.paths[path][method]['x-amazon-apigateway-integration'].uri.split(':')
promises.push(setPermission({
env,
region: uri[8],
accountId: uri[9],
apiId: id,
name: uri[11]
}))
}
}
}
return Promise.all(promises)
}
16 changes: 6 additions & 10 deletions src/util/lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,22 @@ export function setAlias(func, name){
params.FunctionVersion = func.Version
return updateAlias(params)
})
.catch({ code: 'ResourceNotFoundException'}, ()=>{
.catch({ code: 'ResourceNotFoundException' }, ()=>{
params.FunctionVersion = func.Version
return createAlias(params)
})
}


export function setPermission(alias, apiId, env){
const arn = alias.AliasArn.split(':')
const region = arn[3]
const accountId = arn[4]
const functionName = arn[6]
export function setPermission({ name, region, env, apiId, accountId }){

let params = {
Action: 'lambda:InvokeFunction',
Qualifier: alias.Name,
FunctionName: functionName,
Qualifier: env,
FunctionName: name,
Principal: 'apigateway.amazonaws.com',
StatementId: `api-gateway-access-${apiId}-${env}`,
SourceArn: `arn:aws:execute-api:${region}:${accountId}:${apiId}/${env}/*`
StatementId: `api-gateway-${apiId}`,
SourceArn: `arn:aws:execute-api:${region}:${accountId}:${apiId}/*`
}

return addPermission(params)
Expand Down

0 comments on commit 7ec37a5

Please sign in to comment.