Skip to content

Commit

Permalink
Revert "fix: enable IAM auth for custom types (#2961)"
Browse files Browse the repository at this point in the history
This reverts commit 8fc24f3.
  • Loading branch information
palpatim committed Oct 17, 2024
1 parent 8fc24f3 commit 415a919
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,22 +161,6 @@ describe('Deprecate Gen 1 patterns', () => {
);
});

test('does not allow implicit fields on @hasMany', () => {
const stack = verifySchema(/* GraphQL */ `
type Post @model {
author: Author @belongsTo
}
type Author @model {
posts: [Post] @hasMany
}
`);
Annotations.fromStack(stack).hasWarning(
'/Default/TestApi/GraphQLAPI',
'fields argument on @hasMany is deprecated. Modify Author.posts to use references instead. This functionality will be removed in the next major release.',
);
});

test('does not print warning when fields is not used on @hasMany', () => {
const stack = verifySchema(/* GraphQL */ `
type Post @model {
Expand Down
2 changes: 2 additions & 0 deletions packages/amplify-graphql-auth-transformer/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ export class AuthTransformer extends TransformerAuthBase implements TransformerA
// (undocumented)
addAutoGeneratedRelationalFields: (ctx: TransformerContextProvider, def: ObjectTypeDefinitionNode, allowedFields: Set<string>, fields: readonly string[]) => void;
// (undocumented)
addCustomOperationFieldsToAuthNonModelConfig: (ctx: TransformerTransformSchemaStepContextProvider) => void;
// (undocumented)
addFieldResolverForDynamicAuth: (ctx: TransformerContextProvider, def: ObjectTypeDefinitionNode, typeName: string, fieldName: string) => void;
// (undocumented)
addFieldsToObject: (ctx: TransformerTransformSchemaStepContextProvider, modelName: string, ownerFields: Array<string>) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ describe('Custom operations have @aws_iam directives when enableIamAuthorization
expect(out.schema).not.toMatch(/onUpdateFooCustom: String.*@aws_iam/);
});

test('Adds @aws_iam to non-model custom types when there is no model', () => {
// TODO: Enable this test once we fix https:/aws-amplify/amplify-category-api/issues/2929
test.skip('Adds @aws_iam to non-model custom types when there is no model', () => {
const strategy = makeStrategy(strategyType);
const schema = /* GraphQL */ `
type Foo {
Expand Down Expand Up @@ -402,7 +403,8 @@ describe('Custom operations have @aws_iam directives when enableIamAuthorization
expect(out.schema).toMatch(/type Foo.*@aws_iam/);
});

test('Adds @aws_iam to non-model custom types when there is a model', () => {
// TODO: Enable this test once we fix https:/aws-amplify/amplify-category-api/issues/2929
test.skip('Adds @aws_iam to non-model custom types when there is a model', () => {
const strategy = makeStrategy(strategyType);
const schema = /* GraphQL */ `
type Todo @model {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import {
getModelDataSourceNameForTypeName,
getSortKeyFieldNames,
getSubscriptionFilterInputName,
hasDirectiveWithName,
InvalidDirectiveError,
isBuiltInGraphqlNode,
isDynamoDbModel,
isModelType,
isObjectTypeDefinitionNode,
isSqlModel,
MappingTemplate,
TransformerAuthBase,
Expand All @@ -22,22 +20,22 @@ import {
DataSourceProvider,
MutationFieldType,
QueryFieldType,
TransformerAuthProvider,
TransformerBeforeStepContextProvider,
TransformerTransformSchemaStepContextProvider,
TransformerContextProvider,
TransformerResolverProvider,
TransformerSchemaVisitStepContextProvider,
TransformerTransformSchemaStepContextProvider,
TransformerAuthProvider,
TransformerBeforeStepContextProvider,
} from '@aws-amplify/graphql-transformer-interfaces';
import {
DirectiveNode,
FieldDefinitionNode,
ObjectTypeDefinitionNode,
InterfaceTypeDefinitionNode,
Kind,
TypeDefinitionNode,
ListValueNode,
ObjectTypeDefinitionNode,
StringValueNode,
TypeDefinitionNode,
} from 'graphql';
import { merge } from 'lodash';
import {
Expand Down Expand Up @@ -105,7 +103,6 @@ import {
isFieldRoleHavingAccessToBothSide,
isDynamicAuthOrCustomAuth,
isIdenticalAuthRole,
addDirectivesToObject,
} from './utils';
import {
defaultIdentityClaimWarning,
Expand Down Expand Up @@ -348,46 +345,30 @@ export class AuthTransformer extends TransformerAuthBase implements TransformerA
};

/**
* If needed, adds aws_iam auth directive to non-model types
* Adds custom Queries, Mutations, and Subscriptions to the authNonModelConfig map to ensure they are included when adding implicit
* aws_iam auth directives.
*/
private addIamAuthDirectiveToNonModelTypes = (ctx: TransformerTransformSchemaStepContextProvider): void => {
addCustomOperationFieldsToAuthNonModelConfig = (ctx: TransformerTransformSchemaStepContextProvider): void => {
if (!ctx.transformParameters.sandboxModeEnabled && !ctx.synthParameters.enableIamAccess) {
return;
}

const nonModelObjects = ctx.inputDocument.definitions
.filter(isObjectTypeDefinitionNode)
.filter((objectDef) => !isBuiltInGraphqlNode(objectDef))
.filter((objectDef) => !hasDirectiveWithName(objectDef, 'model'))
.filter((objectDef) => !hasDirectiveWithName(objectDef, 'aws_iam'));

nonModelObjects.forEach((object) => {
const typeName = object.name.value;
addDirectivesToObject(ctx, typeName, [makeDirective('aws_iam', [])]);
});
};

/**
* If needed, adds aws_iam auth directive to custom operations (Queries, Mutations, Subscriptions)
*/
private addIamAuthDirectiveToCustomOperationFields = (ctx: TransformerTransformSchemaStepContextProvider): void => {
if (!ctx.transformParameters.sandboxModeEnabled && !ctx.synthParameters.enableIamAccess) {
return;
}
const hasAwsIamDirective = (field: FieldDefinitionNode): boolean => {
return field.directives?.some((dir) => dir.name.value === 'aws_iam');
};

const builtInObjects = ctx.inputDocument.definitions.filter(isBuiltInGraphqlNode);
builtInObjects.forEach((object) => {
const allObjects = ctx.inputDocument.definitions.filter(isBuiltInGraphqlNode);
allObjects.forEach((object) => {
const typeName = object.name.value;
const fieldsWithoutIamDirective = object.fields.filter((field) => !hasDirectiveWithName(field, 'aws_iam'));
const fieldsWithoutIamDirective = object.fields.filter((field) => !hasAwsIamDirective(field));
fieldsWithoutIamDirective.forEach((field) => {
addDirectivesToField(ctx, typeName, field.name.value, [makeDirective('aws_iam', [])]);
});
});
};

transformSchema = (context: TransformerTransformSchemaStepContextProvider): void => {
this.addIamAuthDirectiveToNonModelTypes(context);
this.addIamAuthDirectiveToCustomOperationFields(context);
this.addCustomOperationFieldsToAuthNonModelConfig(context);

const searchableAggregateServiceDirectives = new Set<AuthProvider>();

Expand Down
14 changes: 1 addition & 13 deletions packages/amplify-graphql-auth-transformer/src/utils/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { ObjectTypeDefinitionNode, FieldDefinitionNode, DirectiveNode, NamedType
import {
blankObjectExtension,
extendFieldWithDirectives,
extendObjectWithDirectives,
extensionWithDirectives,
graphqlName,
isListType,
Expand Down Expand Up @@ -214,7 +213,7 @@ export const addDirectivesToField = (
typeName: string,
fieldName: string,
directives: Array<DirectiveNode>,
): void => {
) => {
const type = ctx.output.getType(typeName) as ObjectTypeDefinitionNode;
if (type) {
const field = type.fields?.find((f) => f.name.value === fieldName);
Expand All @@ -231,17 +230,6 @@ export const addDirectivesToField = (
}
};

export const addDirectivesToObject = (
ctx: TransformerTransformSchemaStepContextProvider,
typeName: string,
directives: Array<DirectiveNode>,
): void => {
const type = ctx.output.getType(typeName) as ObjectTypeDefinitionNode;
if (type) {
ctx.output.putType(extendObjectWithDirectives(type, directives));
}
};

/**
* addSubscriptionArguments
*/
Expand Down
3 changes: 0 additions & 3 deletions packages/amplify-graphql-transformer-core/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,6 @@ export interface GraphQLTransformOptions {
readonly userDefinedSlots?: Record<string, UserDefinedSlot[]>;
}

// @public (undocumented)
export const hasDirectiveWithName: (node: FieldDefinitionNode | InterfaceTypeDefinitionNode | ObjectTypeDefinitionNode, name: string) => boolean;

// @public (undocumented)
export type ImportAppSyncAPIInputs = {
apiName: string;
Expand Down
1 change: 0 additions & 1 deletion packages/amplify-graphql-transformer-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export {
getSubscriptionFilterInputName,
getTable,
getType,
hasDirectiveWithName,
isAmplifyDynamoDbModelDataSourceStrategy,
isBuiltInGraphqlNode,
isDefaultDynamoDbModelDataSourceStrategy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,3 @@ export const getField = (obj: ObjectTypeDefinitionNode, fieldName: string): Fiel

export const getType = (schema: DocumentNode, typeName: string): ObjectTypeDefinitionNode | undefined =>
schema.definitions.find((def) => isObjectTypeDefinitionNode(def) && def.name.value === typeName) as ObjectTypeDefinitionNode | undefined;

/**
* Returns true if the node has a directive named `name`
*/
export const hasDirectiveWithName = (
node: FieldDefinitionNode | InterfaceTypeDefinitionNode | ObjectTypeDefinitionNode,
name: string,
): boolean => {
return node.directives?.some((d) => d.name.value === name) ?? false;
};
3 changes: 0 additions & 3 deletions packages/graphql-transformer-common/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ export const directiveExists: (definition: ObjectTypeDefinitionNode, name: strin
// @public (undocumented)
export function extendFieldWithDirectives(field: FieldDefinitionNode, directives: DirectiveNode[]): FieldDefinitionNode;

// @public (undocumented)
export function extendObjectWithDirectives(object: ObjectTypeDefinitionNode, directives: DirectiveNode[]): ObjectTypeDefinitionNode;

// @public (undocumented)
export function extensionWithDirectives(object: ObjectTypeExtensionNode, directives: DirectiveNode[]): ObjectTypeExtensionNode;

Expand Down
150 changes: 0 additions & 150 deletions packages/graphql-transformer-common/src/__tests__/definition.test.ts

This file was deleted.

Loading

0 comments on commit 415a919

Please sign in to comment.