-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathslack-app.ts
165 lines (143 loc) · 4.43 KB
/
slack-app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import * as fs from 'fs';
import { CustomResource } from 'aws-cdk-lib';
import * as secretsmanager from 'aws-cdk-lib/aws-secretsmanager';
import { Construct, IConstruct } from 'constructs';
import { SlackAppManifest, SlackAppManifestProps } from './manifest';
import { SlackAppProvider } from './provider';
/**
* Properties for a SlackApp
*/
export interface SlackAppProps {
/**
* The definition of the app manifest
*
* @see https://api.slack.com/reference/manifests
*/
readonly manifest: SlackAppManifestDefinition;
/**
* An AWS Secrets Manager secret containing the app configuration token
*
* Must use the following JSON format:
*
* ```
* {
* "refreshToken": "<token>"
* }
* ```
*/
readonly configurationTokenSecret: secretsmanager.ISecret;
/**
* The AWS Secrets Manager secret where to store the app credentials
*
* @default - a new secret is created
*/
readonly credentialsSecret?: secretsmanager.ISecret;
}
/**
* A Slack app manifest definition
*/
export abstract class SlackAppManifestDefinition {
/**
* Create a Slack app manifest from a JSON app manifest encoded as a string
*/
public static fromString(manifest: string): SlackAppManifestDefinition {
return new StringManifest(manifest);
}
/**
* Creates a Slack app manifest from a file containg a JSON app manifest
*/
public static fromFile(file: string): SlackAppManifestDefinition {
return new FileManifest(file);
}
/**
* Creates a Slack app manifest by specifying properties
*/
public static fromManifest(props: SlackAppManifestProps): SlackAppManifestDefinition {
return new SlackAppManifest(props);
}
/**
* Renders the JSON app manifest encoded as a string
*/
public abstract render(construct: IConstruct): string;
}
class StringManifest extends SlackAppManifestDefinition {
constructor(private readonly manifest: string) {
super();
}
public render(_construct: IConstruct): string {
return this.manifest;
}
}
class FileManifest extends SlackAppManifestDefinition {
constructor(private readonly file: string) {
super();
}
public render(_construct: IConstruct): string {
return fs.readFileSync(this.file, 'utf8');
}
}
/**
* A Slack application deployed with a manifest
*
* @see https://api.slack.com/reference/manifests
*/
export class SlackApp extends Construct {
/**
* The ID of the application
*/
public readonly appId: string;
/**
* An AWS Secrets Manager secret containing the credentials of the application.
*
* ```
* {
* "appId": "...",
* "clientId": "...",
* "clientSecret": "...",
* "verificationToken": "...",
* "signingSecret": "..."
* }
* ```
*/
public readonly credentials: secretsmanager.ISecret;
/**
* A dynamic reference to the client ID of the app
*/
public readonly clientId: string;
/**
* A dynamic reference to the client secret of the app
*/
public readonly clientSecret: string;
/**
* A dynamic reference to the verification token of the app
*/
public readonly verificationToken: string;
/**
* A dynamic reference to the signing secret of the app
*/
public readonly signingSecret: string;
constructor(scope: Construct, id: string, props: SlackAppProps) {
super(scope, id);
const provider = SlackAppProvider.getOrCreate(this);
props.configurationTokenSecret.grantRead(provider.handler);
props.configurationTokenSecret.grantWrite(provider.handler);
this.credentials = props.credentialsSecret ?? new secretsmanager.Secret(this, 'Credentials', {
description: `Credentials for Slack App ${this.node.id}`,
});
this.credentials.grantWrite(provider.handler);
const resource = new CustomResource(this, 'Resource', {
serviceToken: provider.serviceToken,
resourceType: 'Custom::SlackApp',
properties: {
manifest: props.manifest.render(this),
configurationTokenSecretArn: props.configurationTokenSecret.secretArn,
credentialsSecretArn: this.credentials.secretArn,
},
});
this.appId = resource.getAttString('appId');
this.clientId = this.credentials.secretValueFromJson('clientId').toString();
this.clientSecret = this.credentials.secretValueFromJson('clientSecret').toString();
this.verificationToken = this.credentials.secretValueFromJson('verificationToken').toString();
this.signingSecret = this.credentials.secretValueFromJson('signingSecret').toString();
}
}