serverless-logging-config
Advanced tools
Comparing version
47
index.js
@@ -0,1 +1,3 @@ | ||
const _ = require('lodash') | ||
class ServerlessLoggingConfig { | ||
@@ -104,30 +106,27 @@ constructor (serverless) { | ||
const updatedRoles = [] | ||
const updateRole = roleLogicalId => { | ||
if (!updatedRoles.includes(roleLogicalId)) { | ||
const role = template.Resources[roleLogicalId] | ||
if (!role) { | ||
this.log('Role not found:', roleLogicalId) | ||
return | ||
} | ||
const role = template.Resources[roleLogicalId] | ||
if (!role) { | ||
this.log('Role not found:', roleLogicalId) | ||
return | ||
} | ||
role.Properties.Policies.forEach(x => { | ||
x.PolicyDocument.Statement | ||
.filter(stm => stm.Effect === 'Allow') | ||
.forEach(stm => { | ||
stm.Action = this.arrayify(stm.Action) | ||
stm.Resource = this.arrayify(stm.Resource) | ||
const resource = { | ||
'Fn::Sub': `arn:\${AWS::Partition}:logs:\${AWS::Region}:\${AWS::AccountId}:log-group:${settings.logGroupName}:*` | ||
} | ||
if (stm.Resource.filter(res => res.startsWith('*')).length === 0) { | ||
if (stm.Action.filter(act => act.startsWith('logs:')).length > 0) { | ||
stm.Resource.push({ | ||
'Fn::Sub': `arn:\${AWS::Partition}:logs:\${AWS::Region}:\${AWS::AccountId}:log-group:${settings.logGroupName}:*` | ||
}) | ||
} | ||
role.Properties.Policies.forEach(x => { | ||
x.PolicyDocument.Statement | ||
.filter(stm => stm.Effect === 'Allow') | ||
.forEach(stm => { | ||
stm.Action = this.arrayify(stm.Action) | ||
stm.Resource = this.arrayify(stm.Resource) | ||
if (stm.Action.filter(act => act.startsWith('logs:')).length > 0) { | ||
if (!stm.Resource.find(r => _.isEqual(r, resource))) { | ||
stm.Resource.push(resource) | ||
} | ||
}) | ||
}) | ||
} | ||
updatedRoles.push(roleLogicalId) | ||
} | ||
}) | ||
}) | ||
} | ||
@@ -134,0 +133,0 @@ |
@@ -234,2 +234,6 @@ const ServerlessLoggingConfig = require('./index') | ||
}) | ||
const insertedPermissions = role.Properties.Policies[0].PolicyDocument.Statement[0].Resource | ||
.filter(x => x['Fn::Sub'] === `arn:\${AWS::Partition}:logs:\${AWS::Region}:\${AWS::AccountId}:log-group:${logGroupName}:*`) | ||
expect(insertedPermissions).toHaveLength(1) | ||
}) | ||
@@ -345,102 +349,1 @@ }) | ||
}) | ||
describe('Given a logGroupName is set and resource is *', () => { | ||
let serverlessMock | ||
let plugin | ||
const logGroupName = 'my-logs' | ||
beforeEach(() => { | ||
serverlessMock = { | ||
service: { | ||
custom: { | ||
'serverless-logging-config': { | ||
logGroupName | ||
} | ||
}, | ||
functions: { | ||
hello: { | ||
handler: 'hello.handler' | ||
}, | ||
world: { | ||
handler: 'world.handler' | ||
} | ||
}, | ||
provider: { | ||
compiledCloudFormationTemplate: { | ||
Resources: { | ||
HelloLambdaFunction: { | ||
Type: 'AWS::Lambda::Function', | ||
Properties: { | ||
Role: { 'Fn::GetAtt': ['IamRoleLambdaExecution'] } | ||
} | ||
}, | ||
WorldLambdaFunction: { | ||
Type: 'AWS::Lambda::Function', | ||
Properties: { | ||
Role: { 'Fn::GetAtt': ['IamRoleLambdaExecution'] } | ||
} | ||
}, | ||
IamRoleLambdaExecution: { | ||
Type: 'AWS::IAM::Role', | ||
Properties: { | ||
Policies: [{ | ||
PolicyDocument: { | ||
Statement: [{ | ||
Effect: 'Allow', | ||
Action: ['logs:CreateLogGroup', 'logs:CreateLogStream'], | ||
Resource: ['*'] | ||
}] | ||
} | ||
}] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
plugin = new ServerlessLoggingConfig(serverlessMock) | ||
}) | ||
test('init should load settings correctly', () => { | ||
expect(() => plugin.init()).not.toThrow() | ||
}) | ||
test('disableFunctionLogs should disable logs for all functions', () => { | ||
plugin.disableFunctionLogs() | ||
Object.values(serverlessMock.service.functions) | ||
.forEach(func => { | ||
expect(func.disableLogs).toBe(true) | ||
}) | ||
}) | ||
test('setLoggingConfig should set a LoggingConfig for all functions', () => { | ||
plugin.setLoggingConfig() | ||
Object.values(serverlessMock.service.provider.compiledCloudFormationTemplate.Resources) | ||
.filter(x => x.Type === 'AWS::Lambda::Function') | ||
.forEach(resource => { | ||
expect(resource.Properties.LoggingConfig).toEqual({ | ||
LogGroup: logGroupName, | ||
LogFormat: 'Text' | ||
}) | ||
expect(resource.DependsOn).toEqual([]) | ||
}) | ||
}) | ||
test('addIamPermissions should leave * as is', () => { | ||
plugin.addIamPermissions() | ||
const role = serverlessMock.service.provider.compiledCloudFormationTemplate.Resources.IamRoleLambdaExecution | ||
expect(role.Properties.Policies[0].PolicyDocument.Statement[0].Resource).toContainEqual('*') | ||
}) | ||
test('addIamPermissions should not permissions to the shared IAM role', () => { | ||
plugin.addIamPermissions() | ||
const role = serverlessMock.service.provider.compiledCloudFormationTemplate.Resources.IamRoleLambdaExecution | ||
expect(role.Properties.Policies[0].PolicyDocument.Statement[0].Resource).toContainEqual(expect.not.objectContaining({ | ||
// eslint-disable-next-line no-template-curly-in-string | ||
'Fn::Sub': `arn:\${AWS::Partition}:logs:\${AWS::Region}:\${AWS::AccountId}:log-group:${logGroupName}:*` | ||
})) | ||
}) | ||
}) |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Lets you configure custom log group, JSON logging, and other recent logging changes announce in Nov 2023.", | ||
@@ -10,0 +10,0 @@ "main": "index.js", |
710113
47.3%23
21.05%19078
47.35%