Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix appsync permission assignment from functions #5342

Merged
merged 2 commits into from Jan 14, 2021

Conversation

r0zar
Copy link
Contributor

@r0zar r0zar commented Sep 16, 2020

fix #4306

Issue #, if available:
4306

Description of changes:
The current implementation of appsync permissions is using an incorrect understanding of IAM policies for AppSync. This PR updates the IAM Policy generation process for lambdas from amplify function create/update walkthroughs.

This PR is ready for merging. 馃殌

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@jamesonwilliams
Copy link
Contributor

PHAL @aws-amplify/amplify-cli (@kaustavghosh06 @renebrandel @UnleashedMind @yuth @attilah)

@codecov
Copy link

codecov bot commented Sep 16, 2020

Codecov Report

Merging #5342 (e8b97a3) into master (473bea5) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff            @@
##           master    #5342    +/-   ##
========================================
  Coverage   57.05%   57.05%            
========================================
  Files         468      468            
  Lines       21489    21490     +1     
  Branches     4273     4058   -215     
========================================
+ Hits        12261    12262     +1     
- Misses       8349     8369    +20     
+ Partials      879      859    -20     
Impacted Files Coverage 螖
...amplify-cli-core/src/feature-flags/featureFlags.ts 83.10% <100.00%> (+0.07%) 猬嗭笍
packages/amplify-util-mock/src/api/api.ts 0.00% <0.00%> (酶)
packages/graphql-mapping-template/src/print.ts 35.29% <0.00%> (酶)
packages/amplify-util-mock/src/storage/storage.ts 0.00% <0.00%> (酶)
...ges/amplify-util-mock/src/CFNParser/stack/index.ts 0.00% <0.00%> (酶)
...es/amplify-util-mock/src/api/resolver-overrides.ts 0.00% <0.00%> (酶)
...es/graphql-transformer-core/src/stripDirectives.ts 35.29% <0.00%> (酶)
.../amplify-cli-core/src/state-manager/pathManager.ts 67.85% <0.00%> (酶)
.../amplify-util-mock/src/utils/lambda/loadMinimal.ts 0.00% <0.00%> (酶)
...graphql-transformer-core/src/TransformerContext.ts 25.09% <0.00%> (酶)
... and 10 more

Continue to review full report at Codecov.

Legend - Click here to learn more
螖 = absolute <relative> (impact), 酶 = not affected, ? = missing data
Powered by Codecov. Last update 206ab32...e8b97a3. Read the comment docs.

@renebrandel renebrandel added this to Review in Bug bash via automation Sep 16, 2020
@renebrandel renebrandel moved this from Review to To do in Bug bash Sep 16, 2020
@renebrandel
Copy link
Contributor

hi @r0zar ! I'll try to get the team to review it on Friday. Thanks for starting this!

@r0zar
Copy link
Contributor Author

r0zar commented Oct 2, 2020

@renebrandel 馃憢 Completed my final updates. This PR is ready for merging now AFAIK

@renebrandel
Copy link
Contributor

@attilah will test this PR and see if we need to introduce a feature flag. At this point it looks good.

@attilah attilah self-assigned this Oct 2, 2020
@attilah attilah self-requested a review October 2, 2020 18:19
@attilah attilah moved this from To do to In progress in Bug bash Oct 2, 2020
@attilah
Copy link
Contributor

attilah commented Oct 23, 2020

@r0zar Thanks for the PR, I went through it and I see the reason for the changes, but because of existing deployments we have to preserve the existing functionality so it needs to be put behind a feature flag that can be enabled by default for new projects and not for existing projects.

I have one concern about the generated IAM policy and that is that I can't see that the policy generation code includes the schema types. If I remember correctly you have to have access to the types as well, since if I have a policy for UpdatePost that does not implicitly grant me access to the Post type. This brings to the other thing I am missing is an E2E test which creates a function and an AppSync API and invokes a GraphQL operation.

Since this change has to go behind a feature flag we don't have to deal with the legacy permissions especially we don't have to map it.

If you need further help on the above suggested changes, ping me and I we can setup a meeting to get it through the finish line.

@r0zar
Copy link
Contributor Author

r0zar commented Oct 23, 2020

Thanks for the review.

Yeah let's find some time next week so I get aligned on this and put together a todo list.

@attilah
Copy link
Contributor

attilah commented Oct 24, 2020

@r0zar sounds good, please test out the types/* policy I miss before that if you can.

@attilah
Copy link
Contributor

attilah commented Oct 30, 2020

Synced up offline with @r0zar will add the E2E tests and the Feature flag to preserve the existing functionality and will update the PR.

@attilah attilah moved this from In progress to Review in Bug bash Nov 2, 2020
@attilah
Copy link
Contributor

attilah commented Nov 17, 2020

@r0zar how you see your time working on this, do you have an ETA (no rush) ?

@r0zar
Copy link
Contributor Author

r0zar commented Nov 17, 2020

I finally had some time open up this week and was going to make a start on it. Hopefully I can knock it out in a few sessions.

@attilah
Copy link
Contributor

attilah commented Nov 18, 2020

@r0zar let me know if I can be any help.

@r0zar
Copy link
Contributor Author

r0zar commented Nov 19, 2020

Ran a rebase to keep my changes.

@r0zar r0zar force-pushed the bugfix/appsync-function-permissions branch from 01337da to 8810503 Compare November 19, 2020 18:34
@r0zar
Copy link
Contributor Author

r0zar commented Nov 19, 2020

Ok, fixed the terrible rebase. 馃槄

@lgtm-com
Copy link

lgtm-com bot commented Nov 19, 2020

This pull request introduces 3 alerts when merging 1c4309c into a7b7a8c - view on LGTM.com

new alerts:

  • 3 for Expression has no effect

@r0zar
Copy link
Contributor Author

r0zar commented Nov 20, 2020

馃帀

Copy link
Contributor

@attilah attilah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a few comments on the recent changes, but overall almost there!

import path from 'path';
import * as TransformPackage from 'graphql-transformer-core';
import _ from 'lodash';
import { topLevelCommentPrefix, topLevelCommentSuffix, envVarPrintoutPrefix, CRUDOperation } from '../../../constants';
import _, { get, capitalize, concat, isEmpty } from 'lodash';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use import _ from 'lodash'; and don't deconstruct its functions, it's more readable that way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

鉁旓笍

@@ -60,7 +60,7 @@ export async function createWalkthrough(
// list out the advanced settings before asking whether to configure them
context.print.info('');
context.print.success('Available advanced settings:');
advancedSettingsList.forEach(setting => context.print.info('- '.concat(setting)));
advancedSettingsList.forEach(setting => context.print.info(setting));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why removed the '-' prefix? We rarely change prompts and DX without API reviews. Sync up on this with @renebrandel .

Copy link
Contributor Author

@r0zar r0zar Dec 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The - served as a pseudo-bullet-point in the unordered list of choices. I replaced that with the icons in the PR. It results in a more visually comprehensible list of choices that can be understood better internationally.

import _ from 'lodash';

const featureFlagPR5342 = () => FeatureFlags.getBoolean('useAppSyncPermissionsInLambdaPolicies')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just name it normally as a variable, no need for GH reference, also FeatureFlags has a ., I'd suggest to use appSync.generateGraphQLPermissionsForCRUD since this feature flag is not for graphqlTransformer. We can debate the name but this is what it does IMHO, suggest something similar if you have a better in mind.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how do you feel about the category being iamPolicies? This distinction would be helpful to make clear it's related to how the policies are generated and managed.

const permissions = _.get(permissionMap, [category, resourceName], []);
// If AppSync and legacy CRUD permissions BUT they opted into the feature flag, update policy.
if (serviceType == 'AppSync' && hasCRUDPermissions(permissions) && featureFlagPR5342()) {
return updatePermissionsForBackwardsCompatibility(permissionMap, category, resourceName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the FF branch of the if doing anything that is backward compatible? It should not, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote it so that enabling a feature flag with incorrect permissions would autocorrect their policies. I now see that the way feature flags are being used in amplify is that by turning them on early, the user may deals with the consequences. I see this results in code with less 'artifacts' so I'll update to fit this design.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the FF check to line 102 of execPermissionsWalkthrough.ts. Still exists as an artifact in the code, but there is no more autocorrecting of permissions. Is this the intention?

@@ -504,6 +504,12 @@ export class FeatureFlags {
defaultValueForExistingProjects: false,
defaultValueForNewProjects: false,
},
{
name: 'useAppSyncPermissionsInLambdaPolicies',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As suggested above it should go into a new appSync group.

@lgtm-com
Copy link

lgtm-com bot commented Dec 22, 2020

This pull request introduces 1 alert when merging c3b406d into 206ab32 - view on LGTM.com

new alerts:

  • 1 for Unused variable, import, function or class

@r0zar r0zar force-pushed the bugfix/appsync-function-permissions branch from c3b406d to 24aa3b0 Compare December 22, 2020 19:01
@r0zar
Copy link
Contributor Author

r0zar commented Dec 22, 2020

I squashed and force pushed the commit with only the relevant changes to keep the scope a little tighter.

@r0zar r0zar marked this pull request as draft December 23, 2020 00:28
@@ -25,32 +26,31 @@ export const askExecRolePermissionsQuestions = async (
category?: string,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This value seems to be coming in as undefined for most scenarios. Is it still used?

@r0zar r0zar force-pushed the bugfix/appsync-function-permissions branch from 4f8ce5a to e8b97a3 Compare December 23, 2020 01:23
@r0zar r0zar marked this pull request as ready for review December 23, 2020 01:26
@r0zar r0zar requested review from attilah and removed request for a team December 23, 2020 01:26
Copy link
Contributor

@attilah attilah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for hanging in there ;-)

@attilah attilah merged commit b2e2dd0 into aws-amplify:master Jan 14, 2021
Bug bash automation moved this from Review to Done Jan 14, 2021
ammarkarachi pushed a commit to ammarkarachi/amplify-cli that referenced this pull request Jan 19, 2021
ammarkarachi pushed a commit to ammarkarachi/amplify-cli that referenced this pull request Jan 20, 2021
ammarkarachi added a commit that referenced this pull request Jan 20, 2021
@github-actions
Copy link

This pull request has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
Bug bash
  
Done
Development

Successfully merging this pull request may close these issues.

Grant lambdas access to other project resources for GraphQL API is either broken or very misleading
5 participants