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

fix(appmesh): Move Client Policy from Virtual Service to backend structure #12943

Merged
merged 12 commits into from
Mar 12, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export class AppMeshExtension extends ServiceExtension {
// Next update the app mesh config so that the local Envoy
// proxy on this service knows how to route traffic to
// nodes from the other service.
this.virtualNode.addBackend(otherAppMesh.virtualService);
this.virtualNode.addBackend(appmesh.Backend.virtualService(otherAppMesh.virtualService));
}

private routeSpec(weightedTargets: appmesh.WeightedTarget[], serviceName: string): appmesh.RouteSpec {
Expand Down
24 changes: 14 additions & 10 deletions packages/@aws-cdk/aws-appmesh/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,11 @@ const node = new VirtualNode(this, 'node', {
idle: cdk.Duration.seconds(5),
},
})],
backendsDefaultClientPolicy: appmesh.ClientPolicy.fileTrust({
certificateChain: '/keys/local_cert_chain.pem',
}),
backendDefaults: {
clientPolicy: appmesh.ClientPolicy.fileTrust({
certificateChain: '/keys/local_cert_chain.pem',
}),
},
accessLog: appmesh.AccessLog.fromFilePath('/dev/stdout'),
});

Expand Down Expand Up @@ -230,14 +232,14 @@ const virtualService = new appmesh.VirtualService(stack, 'service-1', {
}),
});

node.addBackend(virtualService);
node.addBackend(appmesh.Backend.virtualService(virtualService));
```

The `listeners` property can be left blank and added later with the `node.addListener()` method. The `healthcheck` and `timeout` properties are optional but if specifying a listener, the `port` must be added.

The `backends` property can be added with `node.addBackend()`. We define a virtual service and add it to the virtual node to allow egress traffic to other node.

The `backendsDefaultClientPolicy` property are added to the node while creating the virtual node. These are virtual node's service backends client policy defaults.
The `backendDefaults` property are added to the node while creating the virtual node. These are virtual node's default settings for all backends.

## Adding TLS to a listener

Expand Down Expand Up @@ -369,10 +371,12 @@ const gateway = new appmesh.VirtualGateway(stack, 'gateway', {
interval: cdk.Duration.seconds(10),
},
})],
backendsDefaultClientPolicy: appmesh.ClientPolicy.acmTrust({
certificateAuthorities: [acmpca.CertificateAuthority.fromCertificateAuthorityArn(stack, 'certificate', certificateAuthorityArn)],
ports: [8080, 8081],
}),
backendDefaults: {
clientPolicy: appmesh.ClientPolicy.acmTrust({
certificateAuthorities: [acmpca.CertificateAuthority.fromCertificateAuthorityArn(stack, 'certificate', certificateAuthorityArn)],
ports: [8080, 8081],
}),
},
accessLog: appmesh.AccessLog.fromFilePath('/dev/stdout'),
virtualGatewayName: 'virtualGateway',
});
Expand All @@ -396,7 +400,7 @@ const gateway = mesh.addVirtualGateway('gateway', {
The listeners field can be omitted which will default to an HTTP Listener on port 8080.
A gateway route can be added using the `gateway.addGatewayRoute()` method.

The `backendsDefaultClientPolicy` property are added to the node while creating the virtual gateway. These are virtual gateway's service backends client policy defaults.
The `backendDefaults` property is added to the node while creating the virtual gateway. These are virtual gateway's default settings for all backends.

## Adding a Gateway Route

Expand Down
79 changes: 79 additions & 0 deletions packages/@aws-cdk/aws-appmesh/lib/shared-interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as cdk from '@aws-cdk/core';
import { CfnVirtualGateway, CfnVirtualNode } from './appmesh.generated';
import { ClientPolicy } from './client-policy';
import { IVirtualService } from './virtual-service';

// keep this import separate from other imports to reduce chance for merge conflicts with v2-main
// eslint-disable-next-line no-duplicate-imports, import/order
Expand Down Expand Up @@ -194,3 +196,80 @@ class FileAccessLog extends AccessLog {
}
}

/**
* Represents the properties needed to define backend defaults
*/
export interface BackendDefaults {
/**
* Client policy for backend defaults
*
* @default none
*/
readonly clientPolicy?: ClientPolicy;
}

/**
* Represents the properties needed to define a Virtual Service backend
*/
export interface VirtualServiceBackendOptions {

/**
* Client policy for the backend
*
* @default none
*/
readonly clientPolicy?: ClientPolicy;
}

/**
* Properties for a backend
*/
export interface BackendConfig {
/**
* Config for a Virtual Service backend
*/
readonly virtualServiceBackend: CfnVirtualNode.BackendProperty;
}


/**
* Contains static factory methods to create backends
*/
export abstract class Backend {
/**
* Construct a Virtual Service backend
*/
public static virtualService(virtualService: IVirtualService, props: VirtualServiceBackendOptions = {}): Backend {
return new VirtualServiceBackend(virtualService, props.clientPolicy);
}

/**
* Return backend config
*/
public abstract bind(_scope: Construct): BackendConfig;
}

/**
* Represents the properties needed to define a Virtual Service backend
*/
class VirtualServiceBackend extends Backend {

constructor (private readonly virtualService: IVirtualService,
private readonly clientPolicy: ClientPolicy | undefined) {
skinny85 marked this conversation as resolved.
Show resolved Hide resolved
super();
}

/**
* Return config for a Virtual Service backend
*/
public bind(_scope: Construct): BackendConfig {
return {
virtualServiceBackend: {
virtualService: {
virtualServiceName: this.virtualService.virtualServiceName,
clientPolicy: this.clientPolicy?.bind(_scope).clientPolicy,
},
},
};
}
}
11 changes: 7 additions & 4 deletions packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as cdk from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnVirtualGateway } from './appmesh.generated';
import { ClientPolicy } from './client-policy';
import { GatewayRoute, GatewayRouteBaseProps } from './gateway-route';
import { IMesh, Mesh } from './mesh';
import { AccessLog } from './shared-interfaces';
import { AccessLog, BackendDefaults } from './shared-interfaces';
import { VirtualGatewayListener, VirtualGatewayListenerConfig } from './virtual-gateway-listener';

/**
Expand Down Expand Up @@ -66,7 +65,7 @@ export interface VirtualGatewayBaseProps {
*
* @default - No Config
*/
readonly backendsDefaultClientPolicy?: ClientPolicy;
readonly backendDefaults?: BackendDefaults;
}

/**
Expand Down Expand Up @@ -180,7 +179,11 @@ export class VirtualGateway extends VirtualGatewayBase {
meshName: this.mesh.meshName,
spec: {
listeners: this.listeners.map(listener => listener.listener),
backendDefaults: props.backendsDefaultClientPolicy?.bind(this),
backendDefaults: props.backendDefaults !== undefined
? {
clientPolicy: props.backendDefaults?.clientPolicy?.bind(this).clientPolicy,
}
: undefined,
logging: accessLogging !== undefined ? {
accessLog: accessLogging.virtualGatewayAccessLog,
} : undefined,
Expand Down
23 changes: 10 additions & 13 deletions packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import * as cdk from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnVirtualNode } from './appmesh.generated';
import { ClientPolicy } from './client-policy';
import { IMesh, Mesh } from './mesh';
import { ServiceDiscovery } from './service-discovery';
import { AccessLog } from './shared-interfaces';
import { AccessLog, BackendDefaults, Backend } from './shared-interfaces';
import { VirtualNodeListener, VirtualNodeListenerConfig } from './virtual-node-listener';
import { IVirtualService } from './virtual-service';

/**
* Interface which all VirtualNode based classes must implement
Expand Down Expand Up @@ -61,7 +59,7 @@ export interface VirtualNodeBaseProps {
*
* @default - No backends
*/
readonly backends?: IVirtualService[];
readonly backends?: Backend[];

/**
* Initial listener for the virtual node
Expand All @@ -82,7 +80,7 @@ export interface VirtualNodeBaseProps {
*
* @default - No Config
*/
readonly backendsDefaultClientPolicy?: ClientPolicy;
readonly backendDefaults?: BackendDefaults;
}

/**
Expand Down Expand Up @@ -185,7 +183,11 @@ export class VirtualNode extends VirtualNodeBase {
spec: {
backends: cdk.Lazy.anyValue({ produce: () => this.backends }, { omitEmptyArray: true }),
listeners: cdk.Lazy.anyValue({ produce: () => this.listeners.map(listener => listener.listener) }, { omitEmptyArray: true }),
backendDefaults: props.backendsDefaultClientPolicy?.bind(this),
backendDefaults: props.backendDefaults !== undefined
? {
clientPolicy: props.backendDefaults?.clientPolicy?.bind(this).clientPolicy,
}
: undefined,
serviceDiscovery: {
dns: serviceDiscovery?.dns,
awsCloudMap: serviceDiscovery?.cloudmap,
Expand Down Expand Up @@ -214,13 +216,8 @@ export class VirtualNode extends VirtualNodeBase {
/**
* Add a Virtual Services that this node is expected to send outbound traffic to
*/
public addBackend(virtualService: IVirtualService) {
this.backends.push({
virtualService: {
virtualServiceName: virtualService.virtualServiceName,
clientPolicy: virtualService.clientPolicy?.bind(this).clientPolicy,
},
});
public addBackend(backend: Backend) {
this.backends.push(backend.bind(this).virtualServiceBackend);
}
}

Expand Down
24 changes: 0 additions & 24 deletions packages/@aws-cdk/aws-appmesh/lib/virtual-service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as cdk from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnVirtualService } from './appmesh.generated';
import { ClientPolicy } from './client-policy';
import { IMesh, Mesh } from './mesh';
import { IVirtualNode } from './virtual-node';
import { IVirtualRouter } from './virtual-router';
Expand All @@ -28,11 +27,6 @@ export interface IVirtualService extends cdk.IResource {
* The Mesh which the VirtualService belongs to
*/
readonly mesh: IMesh;

/**
* Client policy for this Virtual Service
*/
readonly clientPolicy?: ClientPolicy;
}

/**
Expand All @@ -50,13 +44,6 @@ export interface VirtualServiceProps {
*/
readonly virtualServiceName?: string;

/**
* Client policy for this Virtual Service
*
* @default - none
*/
readonly clientPolicy?: ClientPolicy;

/**
* The VirtualNode or VirtualRouter which the VirtualService uses as its provider
*/
Expand Down Expand Up @@ -90,7 +77,6 @@ export class VirtualService extends cdk.Resource implements IVirtualService {
return new class extends cdk.Resource implements IVirtualService {
readonly virtualServiceName = attrs.virtualServiceName;
readonly mesh = attrs.mesh;
readonly clientPolicy = attrs.clientPolicy;
readonly virtualServiceArn = cdk.Stack.of(this).formatArn({
service: 'appmesh',
resource: `mesh/${attrs.mesh.meshName}/virtualService`,
Expand All @@ -114,14 +100,11 @@ export class VirtualService extends cdk.Resource implements IVirtualService {
*/
public readonly mesh: IMesh;

public readonly clientPolicy?: ClientPolicy;

constructor(scope: Construct, id: string, props: VirtualServiceProps) {
super(scope, id, {
physicalName: props.virtualServiceName || cdk.Lazy.string({ produce: () => cdk.Names.uniqueId(this) }),
});

this.clientPolicy = props.clientPolicy;
const providerConfig = props.virtualServiceProvider.bind(this);
this.mesh = providerConfig.mesh;

Expand Down Expand Up @@ -160,13 +143,6 @@ export interface VirtualServiceAttributes {
* The Mesh which the VirtualService belongs to
*/
readonly mesh: IMesh;

/**
* Client policy for this Virtual Service
*
* @default - none
*/
readonly clientPolicy?: ClientPolicy;
}

/**
Expand Down
35 changes: 19 additions & 16 deletions packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,15 @@ const node = mesh.addVirtualNode('node', {
path: '/check-path',
},
})],
backends: [
virtualService,
],
backends: [appmesh.Backend.virtualService(virtualService)],
});

node.addBackend(new appmesh.VirtualService(stack, 'service-2', {
virtualServiceName: 'service2.domain.local',
virtualServiceProvider: appmesh.VirtualServiceProvider.none(mesh),
}),
);
node.addBackend(appmesh.Backend.virtualService(
new appmesh.VirtualService(stack, 'service-2', {
virtualServiceName: 'service2.domain.local',
virtualServiceProvider: appmesh.VirtualServiceProvider.none(mesh),
}),
));

router.addRoute('route-1', {
routeSpec: appmesh.RouteSpec.http({
Expand Down Expand Up @@ -78,15 +77,17 @@ const node2 = mesh.addVirtualNode('node2', {
unhealthyThreshold: 2,
},
})],
backendsDefaultClientPolicy: appmesh.ClientPolicy.fileTrust({
certificateChain: 'path/to/cert',
}),
backends: [
backendDefaults: {
clientPolicy: appmesh.ClientPolicy.fileTrust({
certificateChain: 'path/to/cert',
}),
},
backends: [appmesh.Backend.virtualService(
new appmesh.VirtualService(stack, 'service-3', {
virtualServiceName: 'service3.domain.local',
virtualServiceProvider: appmesh.VirtualServiceProvider.none(mesh),
}),
],
)],
});

const node3 = mesh.addVirtualNode('node3', {
Expand All @@ -102,9 +103,11 @@ const node3 = mesh.addVirtualNode('node3', {
unhealthyThreshold: 2,
},
})],
backendsDefaultClientPolicy: appmesh.ClientPolicy.fileTrust({
certificateChain: 'path-to-certificate',
}),
backendDefaults: {
clientPolicy: appmesh.ClientPolicy.fileTrust({
certificateChain: 'path-to-certificate',
}),
},
accessLog: appmesh.AccessLog.fromFilePath('/dev/stdout'),
});

Expand Down
2 changes: 0 additions & 2 deletions packages/@aws-cdk/aws-appmesh/test/test.health-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ export = {
// THEN
test.doesNotThrow(() => toThrow(min));
test.doesNotThrow(() => toThrow(max));
// falsy, falls back to portMapping.port
// test.throws(() => toThrow(min - 1), /below the minimum threshold/);
test.throws(() => toThrow(max + 1), /above the maximum threshold/);

test.done();
Expand Down
Loading