-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataStack.ts
66 lines (58 loc) · 1.82 KB
/
DataStack.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
import * as sst from '@serverless-stack/resources'
import * as cdk from '@aws-cdk/core'
import { Effect, PolicyStatement } from '@aws-cdk/aws-iam'
import { MultiStackProps } from '.'
export class DataStack extends sst.Stack {
public readonly table: sst.Table
constructor(scope: sst.App, id: string, props: MultiStackProps) {
super(scope, id, props)
// Create a simple table
this.table = new sst.Table(this, 'DynamoDBTableResource', {
fields: {
pk: sst.TableFieldType.STRING,
sk: sst.TableFieldType.STRING,
},
primaryIndex: {
partitionKey: 'pk',
sortKey: 'sk',
},
dynamodbTable: {
removalPolicy: cdk.RemovalPolicy.DESTROY,
},
})
const publicPolicy = new PolicyStatement({
sid: 'AllowPrecedingKeysToDynamoDBPublic',
effect: Effect.ALLOW,
actions: ['dynamodb:GetItem', 'dynamodb:Query'],
resources: [this.table.tableArn],
conditions: {
'ForAllValues:StringLike': {
'dynamodb:LeadingKeys': ['PUBLIC#*'],
},
},
})
/**
* Policy that enables a tenant to access their entire organizations data.
*
* Entires in dynamoDB needs to begin with
*
*/
const tenantPolicy = new PolicyStatement({
sid: 'AllowPrecedingKeysToDynamoDBOrganisation',
effect: Effect.ALLOW,
actions: ['dynamodb:GetItem', 'dynamodb:Query'],
resources: [this.table.tableArn],
conditions: {
'ForAllValues:StringLike': {
'dynamodb:LeadingKeys': ['${aws:PrincipalTag/org}#*'],
},
},
})
props.auth && props.auth.attachPermissionsForUnauthUsers([publicPolicy])
props.auth &&
props.auth.attachPermissionsForAuthUsers([publicPolicy, tenantPolicy])
this.addOutputs({
TableName: this.table.tableName,
})
}
}