Skip to content

Commit e03fe51

Browse files
authored
Merge pull request #26 from rschick/add-schemas
Define schemas to fix serverless warnings
2 parents 8f5d879 + a4de10e commit e03fe51

File tree

4 files changed

+92
-117
lines changed

4 files changed

+92
-117
lines changed

Diff for: add-permissions.js

+87-22
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,100 @@
11
'use strict';
22

3-
const semver = require('semver');
3+
const STRING_OR_STRING_ARRAY = {
4+
anyOf: [
5+
{
6+
type: 'array',
7+
items: {
8+
type: 'string'
9+
}
10+
},
11+
{
12+
type: 'string'
13+
}
14+
]
15+
};
16+
17+
const ROLE_SCHEMA = {
18+
type: 'object',
19+
properties: {
20+
name: { type: 'string' },
21+
principals: STRING_OR_STRING_ARRAY,
22+
allowTagSession: { type: 'boolean' },
23+
maxSessionDuration: {
24+
type: 'integer',
25+
minimum: 3600,
26+
maximum: 43200,
27+
},
28+
},
29+
required: ['name', 'principals'],
30+
additionalProperties: false,
31+
};
32+
33+
const ACCESS_SCHEMA = {
34+
type: 'object',
35+
properties: {
36+
groups: {
37+
type: 'object',
38+
patternProperties: {
39+
'.+': {
40+
type: 'object',
41+
properties: {
42+
role: {
43+
anyOf: [
44+
{
45+
type: 'array',
46+
items: ROLE_SCHEMA
47+
},
48+
ROLE_SCHEMA,
49+
]
50+
},
51+
policy: {
52+
type: 'object',
53+
properties: {
54+
principals: STRING_OR_STRING_ARRAY,
55+
},
56+
required: ['principals']
57+
},
58+
},
59+
minProperties: 1,
60+
additionalProperties: false,
61+
}
62+
},
63+
minProperties: 1,
64+
},
65+
},
66+
required: ['groups'],
67+
additionalProperties: false,
68+
};
469

570
module.exports = class AwsAddLambdaAccountPermissions {
671
constructor(serverless, options) {
7-
if (!semver.satisfies(serverless.version, '>= 1.12')) {
8-
throw new Error('serverless-plugin-lambda-account-access requires serverless 1.12 or higher!');
9-
}
1072
this.serverless = serverless;
1173
this.options = options;
1274
this.provider = this.serverless.getProvider('aws');
1375
this.hooks = {
1476
'package:createDeploymentArtifacts': () => this.beforeDeploy(),
1577
};
78+
79+
if (serverless.configSchemaHandler) {
80+
if (serverless.configSchemaHandler.defineFunctionProperties) {
81+
serverless.configSchemaHandler.defineFunctionProperties('aws', {
82+
properties: {
83+
allowAccess: STRING_OR_STRING_ARRAY,
84+
},
85+
});
86+
}
87+
88+
if (serverless.configSchemaHandler.defineProvider) {
89+
serverless.configSchemaHandler.defineProvider('aws', {
90+
provider: {
91+
properties: {
92+
access: ACCESS_SCHEMA,
93+
},
94+
}
95+
});
96+
}
97+
}
1698
}
1799

18100
addPermissions(accessConfig) {
@@ -27,12 +109,7 @@ module.exports = class AwsAddLambdaAccountPermissions {
27109

28110
if (functions.length !== 0) {
29111
if (policy) {
30-
const { principals } = policy;
31-
if (!principals) {
32-
throw new Error(`Group "${groupName}" does not have policy principals configured`);
33-
}
34-
35-
[].concat(principals).forEach(principal => {
112+
[].concat(policy.principals).forEach(principal => {
36113
const {
37114
principal: normalizedPrincipal,
38115
principalName
@@ -67,14 +144,6 @@ module.exports = class AwsAddLambdaAccountPermissions {
67144

68145
if (role) {
69146
[].concat(role).forEach(({ allowTagSession = false, maxSessionDuration = 3600, name, principals }) => {
70-
if (!name) {
71-
throw new Error(`Group "${groupName}" does not have role name configured`);
72-
}
73-
74-
if (!principals) {
75-
throw new Error(`Role "${name}" in the "${groupName}" group does not have principals configured`);
76-
}
77-
78147
const resourceName = `LambdaAccessRole${this.normalizeName(name)}`;
79148
if (resources.Resources[resourceName]) {
80149
throw new Error(`Roles must have unique names [${name}]`);
@@ -137,10 +206,6 @@ module.exports = class AwsAddLambdaAccountPermissions {
137206
}
138207

139208
const { groups } = access;
140-
if (!groups) {
141-
throw new Error('Access configuration must have groups defined');
142-
}
143-
144209
const accessConfig = this.compileAccessConfig(groups, functions);
145210

146211
this.addPermissions(accessConfig);

Diff for: package-lock.json

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-plugin-lambda-account-access",
3-
"version": "4.1.0",
3+
"version": "4.2.0",
44
"engines": {
55
"node": ">=8.10"
66
},
@@ -30,7 +30,5 @@
3030
"nyc": "^15.1.0",
3131
"sinon": "^7.5.0"
3232
},
33-
"dependencies": {
34-
"semver": "^6.3.0"
35-
}
33+
"dependencies": {}
3634
}

Diff for: test/add-permissions-tests.js

-89
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ function createTestInstance(options) {
3232
describe('serverless-plugin-lambda-account-access', function() {
3333

3434
describe('#constructor', function() {
35-
it('should throw on older version', function() {
36-
expect(() => createTestInstance({ version: '1.11.0' }))
37-
.to.throw('serverless-plugin-lambda-account-access requires serverless 1.12 or higher!');
38-
});
39-
4035
it('should create hooks', function() {
4136
const instance = createTestInstance();
4237
expect(instance)
@@ -102,20 +97,6 @@ describe('serverless-plugin-lambda-account-access', function() {
10297
.that.is.undefined;
10398
});
10499

105-
it('should throw when access config does not have groups', function() {
106-
const instance = createTestInstance({
107-
provider: {
108-
access: {}
109-
},
110-
functions: {
111-
function1: {}
112-
}
113-
});
114-
115-
expect(() => instance.beforeDeploy())
116-
.to.throw('Access configuration must have groups defined');
117-
});
118-
119100
it('should throw when function references access group that does not exist', function() {
120101
const instance = createTestInstance({
121102
provider: {
@@ -207,28 +188,6 @@ describe('serverless-plugin-lambda-account-access', function() {
207188
});
208189

209190
describe('policy', function() {
210-
it('should throw when policy principals are not configured', function() {
211-
const instance = createTestInstance({
212-
provider: {
213-
access: {
214-
groups: {
215-
api: {
216-
policy: {}
217-
}
218-
}
219-
}
220-
},
221-
functions: {
222-
function1: {
223-
allowAccess: 'api'
224-
}
225-
}
226-
});
227-
228-
expect(() => instance.beforeDeploy())
229-
.to.throw('Group "api" does not have policy principals configured');
230-
});
231-
232191
it('should support single principal', function() {
233192
const instance = createTestInstance({
234193
provider: {
@@ -660,54 +619,6 @@ describe('serverless-plugin-lambda-account-access', function() {
660619
});
661620

662621
describe('role', function() {
663-
it('should throw when role does not have name', function() {
664-
const instance = createTestInstance({
665-
provider: {
666-
access: {
667-
groups: {
668-
api: {
669-
role: [{
670-
principals: 111111111111
671-
}]
672-
}
673-
}
674-
}
675-
},
676-
functions: {
677-
function1: {
678-
allowAccess: 'api'
679-
}
680-
}
681-
});
682-
683-
expect(() => instance.beforeDeploy())
684-
.to.throw('Group "api" does not have role name configured');
685-
});
686-
687-
it('should throw when role does not have principals', function() {
688-
const instance = createTestInstance({
689-
provider: {
690-
access: {
691-
groups: {
692-
api: {
693-
role: [{
694-
name: 'foo'
695-
}]
696-
}
697-
}
698-
}
699-
},
700-
functions: {
701-
function1: {
702-
allowAccess: 'api'
703-
}
704-
}
705-
});
706-
707-
expect(() => instance.beforeDeploy())
708-
.to.throw('Role "foo" in the "api" group does not have principals configured');
709-
});
710-
711622
it('should throw when role names are not unique', function() {
712623
const instance = createTestInstance({
713624
provider: {

0 commit comments

Comments
 (0)