🚀 Socket Launch Week 🚀 Day 5: Introducing Socket Fix.Learn More →

serverless-logging-config

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

serverless-logging-config - npm Package Compare versions

Comparing version

to
1.0.2

@@ -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",