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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(appsync): add authenticationConfig to HttpDataSource, fixes #9934 #9971

Closed
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1467be2
feat(appsync): add authenticationConfig to HttpDataSource, fixes #9934
haruharuharuby Aug 25, 2020
eab93cf
Update packages/@aws-cdk/aws-appsync/lib/data-source.ts
haruharuharuby Aug 26, 2020
a594a35
Update packages/@aws-cdk/aws-appsync/lib/data-source.ts
haruharuharuby Aug 26, 2020
dbbe4bf
feat(appsync): add example of http data source to the readme
haruharuharuby Aug 26, 2020
4500ce8
feat(appsync): AuthorizationConfig along with Cfn definition
haruharuharuby Aug 26, 2020
ad51794
Merge branch 'master' into feature/authorization_within_httpdatasource
haruharuharuby Aug 26, 2020
b24c6f3
Merge branch 'master' into feature/authorization_within_httpdatasource
haruharuharuby Aug 27, 2020
442828b
Update packages/@aws-cdk/aws-appsync/README.md
haruharuharuby Aug 28, 2020
8c68f3c
Update packages/@aws-cdk/aws-appsync/README.md
haruharuharuby Aug 28, 2020
beb0269
Update packages/@aws-cdk/aws-appsync/README.md
haruharuharuby Aug 28, 2020
b289be5
feat(appsync): Update readme (position of the Import header)
haruharuharuby Aug 28, 2020
f8879e6
Merge branch 'master' into feature/authorization_within_httpdatasource
haruharuharuby Aug 28, 2020
26fe550
Merge branch 'feature/authorization_within_httpdatasource' of https:/…
haruharuharuby Aug 28, 2020
e6250b2
feat(appsync) remove nested construction of awsIamConfig in HttpDataS…
haruharuharuby Aug 28, 2020
5b574c6
Merge branch 'master' into feature/authorization_within_httpdatasource
haruharuharuby Aug 28, 2020
97ab65a
Merge branch 'master' into feature/authorization_within_httpdatasource
haruharuharuby Aug 28, 2020
08b42d9
feat(appsync) fix options bug addHttpDataSource, addLambdaDataSource
haruharuharuby Aug 28, 2020
48ee5a9
Merge branch 'master' into feature/authorization_within_httpdatasource
haruharuharuby Aug 29, 2020
9be1c7c
Update packages/@aws-cdk/aws-appsync/lib/data-source.ts
haruharuharuby Aug 29, 2020
f1a1145
feat(appsync) fix it is not necessary to specify authorizationType in…
haruharuharuby Aug 29, 2020
5e7d3af
feat(appsync) fix lint error
haruharuharuby Aug 30, 2020
3268ff4
feat(appsync) fix lint error
haruharuharuby Aug 30, 2020
e33d77c
feat(appsync) fix lint error
haruharuharuby Aug 30, 2020
392bf12
feat(appsync) fix lint unit test
haruharuharuby Aug 30, 2020
7f608ba
Merge branch 'master' into feature/authorization_within_httpdatasource
haruharuharuby Sep 3, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 81 additions & 3 deletions packages/@aws-cdk/aws-appsync/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ APIs that use GraphQL.

### Example

#### DynamoDB

Example of a GraphQL API with `AWS_IAM` authorization resolving into a DynamoDb
backend data source.
backend data source.

GraphQL schema file `schema.graphql`:

Expand Down Expand Up @@ -82,6 +84,83 @@ demoDS.createResolver({
});
```

#### HTTP Endpoints
GraphQL schema file `schema.graphql`:

```gql
type job {
id: String!
version: String!
}

input DemoInput {
version: String!
}

type Mutation {
callStepFunction(input: DemoInput!): job
}
```

GraphQL request mapping template `request.vtl`:

```
{
"version": "2018-05-29",
"method": "POST",
"resourcePath": "/",
"params": {
"headers": {
"content-type": "application/x-amz-json-1.0",
"x-amz-target":"AWSStepFunctions.StartExecution"
},
"body": {
"stateMachineArn": "<your step functions arn>",
"input": "{ \"id\": \"$context.arguments.id\" }"
}
}
}
```

GraphQL request mapping template `response.vtl`:

```
{
"id": "${context.result.id}"
}
```

CDK stack file `app-stack.ts`:

```ts
import * as appsync from '@aws-cdk/aws-appsync';

const api = new appsync.GraphQLApi(scope, 'id', {
name: 'api',
schema: appsync.Schema.fromFile(join(__dirname, 'schema.graphql))
});

const httpDs = api.addHttpDataSource(
'ds',
'https://states.amazonaws.com',
{
name: 'httpDsWithStepF',
description: 'from appsync to StepFunctions Workflow',
authorizationConfig: {
signingRegion: 'us-east-1',
signingServiceName: 'states'
}
}
);

httpDs.createResolver({
typeName: 'Mutation',
fieldName: 'callStepFunction',
requestMappingTemplate: MappingTemplate.fromFile('request.vtl'),
responseMappingTemplate: MappingTemplate.fromFile('response.vtl')
});
```

### Schema

Every GraphQL Api needs a schema to define the Api. CDK offers `appsync.Schema`
Expand Down Expand Up @@ -128,8 +207,7 @@ const api = appsync.GraphQLApi(stack, 'api', {
});
```

### Imports

## Imports
Any GraphQL Api that has been created outside the stack can be imported from
another stack into your CDK app. Utilizing the `fromXxx` function, you have
the ability to add data sources and resolvers through a `IGraphQLApi` interface.
Expand Down
32 changes: 30 additions & 2 deletions packages/@aws-cdk/aws-appsync/lib/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,21 @@ export class DynamoDbDataSource extends BackedDataSource {
}
}

/**
* The authorization config in case the HTTP endpoint requires authorization
*/
export interface AwsIamConfig {
/**
* The signing region for AWS IAM authorization
*/
readonly signingRegion: string;

/**
* The signing service name for AWS IAM authorization
*/
readonly signingServiceName: string;
}

/**
* Properties for an AppSync http datasource
*/
Expand All @@ -211,18 +226,31 @@ export interface HttpDataSourceProps extends BaseDataSourceProps {
* The http endpoint
*/
readonly endpoint: string;

/**
* The authorization config in case the HTTP endpoint requires authorization
*
* @default - none
*
*/
readonly authorizationConfig?: AwsIamConfig;
}

/**
* An AppSync datasource backed by a http endpoint
*/
export class HttpDataSource extends BaseDataSource {
constructor(scope: Construct, id: string, props: HttpDataSourceProps) {
const authorizationConfig = props.authorizationConfig ? {
authorizationType: 'AWS_IAM',
awsIamConfig: props.authorizationConfig,
} : undefined;
super(scope, id, props, {
type: 'HTTP',
httpConfig: {
endpoint: props.endpoint,
authorizationConfig,
},
type: 'HTTP',
});
}
}
Expand Down Expand Up @@ -250,4 +278,4 @@ export class LambdaDataSource extends BackedDataSource {
});
props.lambdaFunction.grantInvoke(this);
}
}
}
19 changes: 16 additions & 3 deletions packages/@aws-cdk/aws-appsync/lib/graphqlapi-base.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ITable } from '@aws-cdk/aws-dynamodb';
import { IFunction } from '@aws-cdk/aws-lambda';
import { CfnResource, IResource, Resource } from '@aws-cdk/core';
import { DynamoDbDataSource, HttpDataSource, LambdaDataSource, NoneDataSource } from './data-source';
import { DynamoDbDataSource, HttpDataSource, LambdaDataSource, NoneDataSource, AwsIamConfig } from './data-source';

/**
* Optional configuration for data sources
Expand All @@ -22,6 +22,18 @@ export interface DataSourceOptions {
readonly description?: string;
}

/**
* Optional configuration for Http data sources
*/
export interface HttpDataSourceOptions extends DataSourceOptions {
/**
* The authorization config in case the HTTP endpoint requires authorization
*
* @default - none
*/
readonly authorizationConfig?: AwsIamConfig;
}

/**
* Interface for GraphQL
*/
Expand Down Expand Up @@ -67,7 +79,7 @@ export interface IGraphqlApi extends IResource {
* @param endpoint The http endpoint
* @param options The optional configuration for this data source
*/
addHttpDataSource(id: string, endpoint: string, options?: DataSourceOptions): HttpDataSource;
addHttpDataSource(id: string, endpoint: string, options?: HttpDataSourceOptions): HttpDataSource;

/**
* add a new Lambda data source to this API
Expand Down Expand Up @@ -140,12 +152,13 @@ export abstract class GraphqlApiBase extends Resource implements IGraphqlApi {
* @param endpoint The http endpoint
* @param options The optional configuration for this data source
*/
public addHttpDataSource(id: string, endpoint: string, options?: DataSourceOptions): HttpDataSource {
public addHttpDataSource(id: string, endpoint: string, options?: HttpDataSourceOptions): HttpDataSource {
return new HttpDataSource(this, id, {
api: this,
endpoint,
name: options?.name,
description: options?.description,
authorizationConfig: options?.authorizationConfig,
});
}

Expand Down
29 changes: 29 additions & 0 deletions packages/@aws-cdk/aws-appsync/test/appsync-http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,35 @@ describe('Http Data Source configuration', () => {
});
});

test('appsync configures name, authorizationConfig correctly', () => {
// WHEN
api.addHttpDataSource('ds', endpoint, {
name: 'custom',
description: 'custom description',
authorizationConfig: {
signingRegion: 'us-east-1',
signingServiceName: 'states',
},
});

// THEN
expect(stack).toHaveResourceLike('AWS::AppSync::DataSource', {
Type: 'HTTP',
Name: 'custom',
Description: 'custom description',
HttpConfig: {
Endpoint: endpoint,
AuthorizationConfig: {
AuthorizationType: 'AWS_IAM',
AwsIamConfig: {
SigningRegion: 'us-east-1',
SigningServiceName: 'states',
},
},
},
});
});

test('appsync errors when creating multiple http data sources with no configuration', () => {
// THEN
expect(() => {
Expand Down