-
Notifications
You must be signed in to change notification settings - Fork 825
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Custom policies IAM Policies for Lambda and Containers (#8068)
* Custom policy implementation * feat: add custom policies file to function and API container add custom policies file to function and API container, merge the custom policies to CFN template, validation for regex of resources and actions in the custom policies file * feat: changes for first PR * feat: Some changes according to the PR comments 1. Add Json Schema to validate the customers input 2. some minor changes related to format issue 3. error handle * feat: replace env to current env in the resource when checkout and add env, and push * feat: e2e test and replacing env * feat: Minor changes for env replacement * feat: remove changing env between env * feat: Add cloudform type for type safety, move validation to provider-cloudformation, validation * feat: remove some unused function and import, change regex for resource * feat: Some changes according to the PR comment * feat: changes according to PR comments * feat: remove unused import * feat: remove previous unused code * feat: Changes according to PR comments * feat: some changes according to PR comments * feat: work on PR comments * feat: rebase for conflict * feat: rebase for failure of hooksmanager test failed * feat: unit test * feat: fix fail test * feat: change default template of custom policies * feat: fix failed test * feat: PR comments * feat: pr comments * feat: fix failed test * feat: PR comments from ED Co-authored-by: Lu Han <lhnamz@amazon.com>
- Loading branch information
1 parent
3168885
commit 3e1ce0d
Showing
20 changed files
with
579 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/amplify-cli-core/src/__tests__/customPoliciesUtils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {createDefaultCustomPoliciesFile} from '../customPoliciesUtils' | ||
import { JSONUtilities } from '..'; | ||
import { pathManager, PathConstants } from '../state-manager'; | ||
import path from 'path'; | ||
|
||
describe('Custom policies util test', () => { | ||
|
||
jest.mock('../state-manager'); | ||
|
||
const testCategoryName = 'function'; | ||
const testResourceName = 'functionTest'; | ||
const expectedFilePath = path.join(__dirname, 'testFiles', 'custom-policies-test', testCategoryName, testResourceName, PathConstants.CustomPoliciesFilename); | ||
jest.spyOn(pathManager, 'getCustomPoliciesPath').mockReturnValue(expectedFilePath); | ||
|
||
beforeEach(jest.clearAllMocks); | ||
|
||
test('Write default custom policy file to the specified resource name', () => { | ||
|
||
createDefaultCustomPoliciesFile(testCategoryName, testResourceName); | ||
|
||
const data = JSONUtilities.readJson(expectedFilePath); | ||
|
||
expect(data).toMatchObject([ | ||
{ | ||
Action: [], | ||
Resource: [] | ||
} | ||
]); | ||
|
||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...e/src/__tests__/testFiles/custom-policies-test/function/functionTest/custom-policies.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[ | ||
{ | ||
"Action": [], | ||
"Resource": [] | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Fn, IAM } from 'cloudform-types'; | ||
import { JSONUtilities, pathManager } from '.'; | ||
|
||
export type CustomIAMPolicies = CustomIAMPolicy[]; | ||
|
||
export type CustomIAMPolicy = { | ||
Action: string[]; | ||
Effect: string; | ||
Resource: string[]; | ||
} | ||
|
||
export const CustomIAMPoliciesSchema = { | ||
type : 'array', | ||
minItems: 1, | ||
items: { | ||
type: 'object', | ||
properties: { | ||
Action: { type: 'array', items: { type: 'string' }, minItems: 1, nullable: false }, | ||
Resource: { type: 'array', items: { type: 'string' }, minItems: 1, nullable: false } | ||
}, | ||
optionalProperties: { | ||
Effect: { type: 'string', enum:['Allow', 'Deny'], default: 'Allow' }, | ||
}, | ||
required: ['Resource', 'Action'], | ||
additionalProperties: true | ||
}, | ||
additionalProperties: false | ||
} | ||
|
||
export const customExecutionPolicyForFunction = new IAM.Policy({ | ||
PolicyName: 'custom-lambda-execution-policy', | ||
Roles: [ | ||
Fn.Ref('LambdaExecutionRole') | ||
], | ||
PolicyDocument: { | ||
Version: '2012-10-17', | ||
Statement: [] | ||
} | ||
}).dependsOn(['LambdaExecutionRole']); | ||
|
||
export const customExecutionPolicyForContainer = new IAM.Policy({ | ||
PolicyDocument: { | ||
Statement: [ | ||
], | ||
Version: '2012-10-17' | ||
}, | ||
PolicyName: 'CustomExecutionPolicyForContainer', | ||
Roles: [ | ||
] | ||
}); | ||
|
||
export function createDefaultCustomPoliciesFile(categoryName: string, resourceName: string) { | ||
const customPoliciesPath = pathManager.getCustomPoliciesPath(categoryName, resourceName); | ||
const defaultCustomPolicies = [ | ||
{ | ||
Action: [], | ||
Resource: [] | ||
} | ||
] | ||
JSONUtilities.writeJson(customPoliciesPath, defaultCustomPolicies); | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const aws = require('aws-sdk'); | ||
|
||
exports.handler = async event => { | ||
const { secretName} = event; | ||
const { Parameter } = await new aws.SSM() | ||
.getParameter({ | ||
Name: secretName, | ||
WithDecryption: true, | ||
}) | ||
.promise(); | ||
return Parameter; | ||
}; |
Oops, something went wrong.