@kensio/yulin 1.20.15 → 1.20.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -0
- package/dist/sdk/module/sim-sdk-module-client-interceptor.d.ts +5 -0
- package/dist/sdk/module/sim-sdk-module-client-interceptor.js +14 -0
- package/dist/sdk/module/sim-sdk-static-client-factory.d.ts +26 -0
- package/dist/sdk/module/sim-sdk-static-client-factory.js +59 -0
- package/dist/sdk/send-patch.d.ts +8 -0
- package/dist/sdk/send-patch.js +11 -0
- package/dist/service/aws/caller/sim-aws-caller-resolver.d.ts +28 -6
- package/dist/service/aws/caller/sim-aws-caller-resolver.js +18 -11
- package/dist/service/aws/caller/sim-aws-caller.d.ts +9 -0
- package/dist/service/aws/caller/sim-aws-resolved-caller.d.ts +19 -0
- package/dist/service/aws/caller/sim-aws-resolved-caller.js +26 -0
- package/dist/service/aws/factory/sim-aws-account-region-service-builder.d.ts +6 -0
- package/dist/service/aws/factory/sim-aws-account-region-service-builder.js +3 -0
- package/dist/service/aws/factory/sim-aws-account-service-cache.d.ts +6 -0
- package/dist/service/aws/factory/sim-aws-account-service-cache.js +5 -0
- package/dist/service/aws/factory/sim-aws-service-factory.d.ts +5 -0
- package/dist/service/aws/factory/sim-aws-service-factory.js +6 -0
- package/dist/service/aws/sim-aws-properties.d.ts +15 -0
- package/dist/service/aws/sim-aws.js +2 -1
- package/dist/service/iam/authorize/context/sim-iam-auth-z-caller-context-builder.d.ts +30 -7
- package/dist/service/iam/authorize/context/sim-iam-auth-z-caller-context-builder.js +20 -11
- package/dist/service/iam/authorize/context/sim-iam-auth-z-context-builder.d.ts +2 -1
- package/dist/service/iam/authorize/context/sim-iam-auth-z-context-builder.js +5 -1
- package/dist/service/iam/authorize/match/sim-iam-policy-principal-matcher.d.ts +10 -0
- package/dist/service/iam/authorize/match/sim-iam-policy-principal-matcher.js +15 -3
- package/dist/service/iam/authorize/sim-iam-account-auth-z.d.ts +11 -4
- package/dist/service/iam/authorize/sim-iam-account-auth-z.js +5 -3
- package/dist/service/iam/authorize/sim-iam-authorizer.d.ts +2 -1
- package/dist/service/iam/authorize/sim-iam-authorizer.js +1 -0
- package/dist/service/iam/role/sim-iam-role-with-policy.factory.d.ts +9 -0
- package/dist/service/iam/role/sim-iam-role-with-policy.factory.js +4 -2
- package/dist/service/iam/sim-iam-account-parts.d.ts +8 -0
- package/dist/service/iam/sim-iam-account-parts.js +1 -0
- package/dist/service/sts/auth-z/assume-role-auth-z-coordinator.d.ts +9 -2
- package/dist/service/sts/auth-z/assume-role-auth-z-coordinator.js +2 -2
- package/dist/service/sts/auth-z/assume-role-source-account-auth-z.d.ts +10 -4
- package/dist/service/sts/auth-z/assume-role-source-account-auth-z.js +8 -7
- package/dist/service/sts/auth-z/assume-role-target-auth-z.d.ts +2 -2
- package/dist/service/sts/auth-z/assume-role-trust-policy-authorizer.d.ts +6 -2
- package/dist/service/sts/auth-z/assume-role-trust-policy-authorizer.js +14 -11
- package/dist/service/sts/command/assume-role/assume-role.handler.d.ts +5 -1
- package/dist/service/sts/command/assume-role/assume-role.handler.js +5 -2
- package/dist/service/sts/command/get-caller-identity/get-caller-identity.handler.d.ts +5 -1
- package/dist/service/sts/command/get-caller-identity/get-caller-identity.handler.js +4 -1
- package/dist/service/sts/service-role/sim-service-role.js +5 -1
- package/dist/service/sts/sim-sts.d.ts +9 -1
- package/dist/service/sts/sim-sts.js +4 -0
- package/docs/README.md +54 -0
- package/docs/ai-skill/README.md +69 -0
- package/docs/cli/README.md +284 -0
- package/docs/factories/README.md +140 -0
- package/docs/lint/README.md +176 -0
- package/docs/non-aws-dependencies/README.md +261 -0
- package/docs/sdk/README.md +272 -0
- package/docs/serve/README.md +1126 -0
- package/docs/services/acm/README.md +823 -0
- package/docs/services/apigateway/README.md +1919 -0
- package/docs/services/apigatewayv2/README.md +2678 -0
- package/docs/services/athena/README.md +1044 -0
- package/docs/services/bedrock/README.md +350 -0
- package/docs/services/cloudformation/README.md +3440 -0
- package/docs/services/cloudfront/README.md +2874 -0
- package/docs/services/cloudwatch/README.md +420 -0
- package/docs/services/cognito/README.md +4923 -0
- package/docs/services/dynamodb/README.md +3784 -0
- package/docs/services/ecr/README.md +302 -0
- package/docs/services/ecs/README.md +2010 -0
- package/docs/services/elbv2/README.md +1897 -0
- package/docs/services/eventbridge/README.md +1110 -0
- package/docs/services/firehose/README.md +805 -0
- package/docs/services/glue/README.md +501 -0
- package/docs/services/iam/README.md +1283 -0
- package/docs/services/kinesis/README.md +454 -0
- package/docs/services/kms/README.md +659 -0
- package/docs/services/lambda/README.md +3645 -0
- package/docs/services/logs/README.md +868 -0
- package/docs/services/organizations/README.md +574 -0
- package/docs/services/personalize/README.md +835 -0
- package/docs/services/rekognition/README.md +1070 -0
- package/docs/services/route53/README.md +1783 -0
- package/docs/services/s3/README.md +3024 -0
- package/docs/services/scheduler/README.md +806 -0
- package/docs/services/secretsmanager/README.md +667 -0
- package/docs/services/ses/README.md +1011 -0
- package/docs/services/sns/README.md +1904 -0
- package/docs/services/sqs/README.md +1185 -0
- package/docs/services/ssm/README.md +1072 -0
- package/docs/services/stepfunctions/README.md +1332 -0
- package/docs/services/sts/README.md +245 -0
- package/docs/services/wafv2/README.md +1378 -0
- package/docs/terraform/README.md +248 -0
- package/docs/time/README.md +284 -0
- package/llms.txt +59 -0
- package/package.json +7 -4
|
@@ -0,0 +1,659 @@
|
|
|
1
|
+
# Simulated KMS
|
|
2
|
+
|
|
3
|
+
Yulin includes a simulated AWS Key Management Service (KMS) for tests and local development.
|
|
4
|
+
|
|
5
|
+
Encryption is real. Each simulated key holds AES-256 key material and the operations run through
|
|
6
|
+
Node.js's own `crypto`. A ciphertext can only be read with its key, and a decryption with the wrong
|
|
7
|
+
encryption context fails.
|
|
8
|
+
|
|
9
|
+
KMS-specific types are imported from the `@kensio/yulin/kms` subpath.
|
|
10
|
+
|
|
11
|
+
## Encrypting and decrypting
|
|
12
|
+
|
|
13
|
+
Create a key and use it. `Decrypt` needs no `KeyId` for a symmetric key, because the ciphertext
|
|
14
|
+
already names the key that produced it.
|
|
15
|
+
|
|
16
|
+
```typescript sim-kms-encrypt-decrypt
|
|
17
|
+
/**
|
|
18
|
+
* Encrypting and decrypting with a simulated KMS key.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import {
|
|
22
|
+
CreateKeyCommand,
|
|
23
|
+
DecryptCommand,
|
|
24
|
+
EncryptCommand,
|
|
25
|
+
} from "@aws-sdk/client-kms";
|
|
26
|
+
|
|
27
|
+
import { SimAws } from "@kensio/yulin";
|
|
28
|
+
|
|
29
|
+
const simAws = new SimAws();
|
|
30
|
+
const kms = simAws.kms();
|
|
31
|
+
|
|
32
|
+
const created = await kms.createKey(
|
|
33
|
+
new CreateKeyCommand({ Description: "Application key" }),
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
const encrypted = await kms.encrypt(
|
|
37
|
+
new EncryptCommand({
|
|
38
|
+
KeyId: created.KeyMetadata?.Arn,
|
|
39
|
+
Plaintext: Buffer.from("hunter2", "utf8"),
|
|
40
|
+
}),
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const decrypted = await kms.decrypt(
|
|
44
|
+
new DecryptCommand({ CiphertextBlob: encrypted.CiphertextBlob }),
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
console.log(Buffer.from(decrypted.Plaintext ?? []).toString("utf8")); // "hunter2"
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
The ciphertext blob is opaque. Only the `SimAws` instance that produced it can read it. Real AWS and
|
|
51
|
+
any second `SimAws` instance both reject it.
|
|
52
|
+
|
|
53
|
+
## Encryption context
|
|
54
|
+
|
|
55
|
+
An encryption context is non-secret key/value data bound to a ciphertext. Decryption with a
|
|
56
|
+
different context fails. That ties a ciphertext to the thing it belongs to.
|
|
57
|
+
|
|
58
|
+
```typescript sim-kms-encryption-context
|
|
59
|
+
/**
|
|
60
|
+
* Binding an encryption context to a simulated KMS ciphertext.
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
import {
|
|
64
|
+
CreateKeyCommand,
|
|
65
|
+
DecryptCommand,
|
|
66
|
+
EncryptCommand,
|
|
67
|
+
} from "@aws-sdk/client-kms";
|
|
68
|
+
|
|
69
|
+
import { SimAws } from "@kensio/yulin";
|
|
70
|
+
import { SimKmsInvalidCiphertextException } from "@kensio/yulin/kms";
|
|
71
|
+
|
|
72
|
+
const simAws = new SimAws();
|
|
73
|
+
const kms = simAws.kms();
|
|
74
|
+
|
|
75
|
+
const created = await kms.createKey(new CreateKeyCommand({}));
|
|
76
|
+
|
|
77
|
+
const encrypted = await kms.encrypt(
|
|
78
|
+
new EncryptCommand({
|
|
79
|
+
KeyId: created.KeyMetadata?.Arn,
|
|
80
|
+
Plaintext: Buffer.from("hunter2", "utf8"),
|
|
81
|
+
EncryptionContext: { tenant: "acme" },
|
|
82
|
+
}),
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
try {
|
|
86
|
+
await kms.decrypt(
|
|
87
|
+
new DecryptCommand({
|
|
88
|
+
CiphertextBlob: encrypted.CiphertextBlob,
|
|
89
|
+
EncryptionContext: { tenant: "other" },
|
|
90
|
+
}),
|
|
91
|
+
);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
// The wrong context fails the cipher's own authentication, exactly as it
|
|
94
|
+
// does on real KMS.
|
|
95
|
+
console.log(error instanceof SimKmsInvalidCiphertextException); // true
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
The context is an unordered map. The same pairs written in a different order still decrypt.
|
|
100
|
+
|
|
101
|
+
## Envelope encryption
|
|
102
|
+
|
|
103
|
+
`Encrypt` takes at most 4096 bytes. That limit is what makes envelope encryption necessary.
|
|
104
|
+
`GenerateDataKey` returns a data key twice, once in the clear to encrypt your data with, and once
|
|
105
|
+
encrypted under the KMS key to store alongside it.
|
|
106
|
+
|
|
107
|
+
```typescript sim-kms-generate-data-key
|
|
108
|
+
/**
|
|
109
|
+
* Envelope encryption with a simulated KMS data key.
|
|
110
|
+
*/
|
|
111
|
+
|
|
112
|
+
import {
|
|
113
|
+
CreateKeyCommand,
|
|
114
|
+
DecryptCommand,
|
|
115
|
+
GenerateDataKeyCommand,
|
|
116
|
+
} from "@aws-sdk/client-kms";
|
|
117
|
+
|
|
118
|
+
import { SimAws } from "@kensio/yulin";
|
|
119
|
+
|
|
120
|
+
const simAws = new SimAws();
|
|
121
|
+
const kms = simAws.kms();
|
|
122
|
+
|
|
123
|
+
const created = await kms.createKey(new CreateKeyCommand({}));
|
|
124
|
+
|
|
125
|
+
const dataKey = await kms.generateDataKey(
|
|
126
|
+
new GenerateDataKeyCommand({
|
|
127
|
+
KeyId: created.KeyMetadata?.Arn,
|
|
128
|
+
KeySpec: "AES_256",
|
|
129
|
+
}),
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
console.log(dataKey.Plaintext?.length); // 32
|
|
133
|
+
|
|
134
|
+
// Store dataKey.CiphertextBlob with the data; discard the plaintext copy.
|
|
135
|
+
const recovered = await kms.decrypt(
|
|
136
|
+
new DecryptCommand({ CiphertextBlob: dataKey.CiphertextBlob }),
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
console.log(recovered.Plaintext?.length); // 32
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Signing and verifying
|
|
143
|
+
|
|
144
|
+
A key created with `KeyUsage: SIGN_VERIFY` holds a real key pair, and the signatures are real
|
|
145
|
+
signatures. A key spec is required, because the default `SYMMETRIC_DEFAULT` spec cannot sign.
|
|
146
|
+
|
|
147
|
+
```typescript sim-kms-sign-verify
|
|
148
|
+
/**
|
|
149
|
+
* Signing and verifying with a simulated asymmetric KMS key.
|
|
150
|
+
*/
|
|
151
|
+
|
|
152
|
+
import {
|
|
153
|
+
CreateKeyCommand,
|
|
154
|
+
SignCommand,
|
|
155
|
+
VerifyCommand,
|
|
156
|
+
} from "@aws-sdk/client-kms";
|
|
157
|
+
|
|
158
|
+
import { SimAws } from "@kensio/yulin";
|
|
159
|
+
|
|
160
|
+
const simAws = new SimAws();
|
|
161
|
+
const kms = simAws.kms();
|
|
162
|
+
|
|
163
|
+
const created = await kms.createKey(
|
|
164
|
+
new CreateKeyCommand({
|
|
165
|
+
KeySpec: "ECC_NIST_P256",
|
|
166
|
+
KeyUsage: "SIGN_VERIFY",
|
|
167
|
+
}),
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
const message = Buffer.from("order-1234", "utf8");
|
|
171
|
+
|
|
172
|
+
const signed = await kms.sign(
|
|
173
|
+
new SignCommand({
|
|
174
|
+
KeyId: created.KeyMetadata?.Arn,
|
|
175
|
+
Message: message,
|
|
176
|
+
SigningAlgorithm: "ECDSA_SHA_256",
|
|
177
|
+
}),
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
const verified = await kms.verify(
|
|
181
|
+
new VerifyCommand({
|
|
182
|
+
KeyId: created.KeyMetadata?.Arn,
|
|
183
|
+
Message: message,
|
|
184
|
+
Signature: signed.Signature,
|
|
185
|
+
SigningAlgorithm: "ECDSA_SHA_256",
|
|
186
|
+
}),
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
console.log(verified.SignatureValid); // true
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
`Verify` raises `KMSInvalidSignatureException` for a signature that fails to check out. Real KMS
|
|
193
|
+
does the same, in place of answering `SignatureValid: false`.
|
|
194
|
+
|
|
195
|
+
The key specs simulated are `ECC_NIST_P256`, `ECC_NIST_P384`, `ECC_NIST_P521`, `ECC_SECG_P256K1`,
|
|
196
|
+
`RSA_2048`, `RSA_3072` and `RSA_4096`. Each ECC spec offers the one ECDSA algorithm paired with its
|
|
197
|
+
curve. Each RSA spec offers all six RSASSA algorithms. A signing algorithm the key spec leaves out
|
|
198
|
+
is refused. So is `Sign` against an encryption key, and `Encrypt` against a signing key.
|
|
199
|
+
|
|
200
|
+
`DescribeKey` reports the key's own spec, usage and algorithms. Code that branches on them branches
|
|
201
|
+
the same way it would on AWS.
|
|
202
|
+
|
|
203
|
+
### Verifying outside KMS
|
|
204
|
+
|
|
205
|
+
`GetPublicKey` returns the public key as DER `SubjectPublicKeyInfo`, the encoding real KMS returns.
|
|
206
|
+
A verifier that never sees the simulator accepts a signature made in it.
|
|
207
|
+
|
|
208
|
+
```typescript sim-kms-public-key
|
|
209
|
+
/**
|
|
210
|
+
* Verifying a simulated KMS signature outside KMS, with its public key.
|
|
211
|
+
*/
|
|
212
|
+
|
|
213
|
+
import { createPublicKey, verify } from "node:crypto";
|
|
214
|
+
|
|
215
|
+
import {
|
|
216
|
+
CreateKeyCommand,
|
|
217
|
+
GetPublicKeyCommand,
|
|
218
|
+
SignCommand,
|
|
219
|
+
} from "@aws-sdk/client-kms";
|
|
220
|
+
|
|
221
|
+
import { SimAws } from "@kensio/yulin";
|
|
222
|
+
|
|
223
|
+
const simAws = new SimAws();
|
|
224
|
+
const kms = simAws.kms();
|
|
225
|
+
|
|
226
|
+
const created = await kms.createKey(
|
|
227
|
+
new CreateKeyCommand({
|
|
228
|
+
KeySpec: "ECC_NIST_P256",
|
|
229
|
+
KeyUsage: "SIGN_VERIFY",
|
|
230
|
+
}),
|
|
231
|
+
);
|
|
232
|
+
|
|
233
|
+
const message = Buffer.from("order-1234", "utf8");
|
|
234
|
+
|
|
235
|
+
const signed = await kms.sign(
|
|
236
|
+
new SignCommand({
|
|
237
|
+
KeyId: created.KeyMetadata?.Arn,
|
|
238
|
+
Message: message,
|
|
239
|
+
SigningAlgorithm: "ECDSA_SHA_256",
|
|
240
|
+
}),
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
const fetched = await kms.getPublicKey(
|
|
244
|
+
new GetPublicKeyCommand({ KeyId: created.KeyMetadata?.Arn }),
|
|
245
|
+
);
|
|
246
|
+
|
|
247
|
+
const publicKey = createPublicKey({
|
|
248
|
+
key: Buffer.from(fetched.PublicKey ?? new Uint8Array()),
|
|
249
|
+
format: "der",
|
|
250
|
+
type: "spki",
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
console.log(
|
|
254
|
+
verify("sha256", message, publicKey, signed.Signature ?? new Uint8Array()),
|
|
255
|
+
); // true
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
ECDSA signatures are DER encoded, and RSASSA-PSS signatures use a salt the length of the digest,
|
|
259
|
+
both matching what KMS produces. `GetPublicKey` against a symmetric key is
|
|
260
|
+
`UnsupportedOperationException`, since there is no public key to hand out.
|
|
261
|
+
|
|
262
|
+
## Key policies and IAM
|
|
263
|
+
|
|
264
|
+
Every KMS key has a policy, and it cannot be removed. An IAM policy granting `kms:Decrypt` only
|
|
265
|
+
takes effect where the key's own policy admits the caller. How it admits them decides what else is
|
|
266
|
+
needed:
|
|
267
|
+
|
|
268
|
+
- A statement naming the caller grants access outright. A role with no permissions of its own can
|
|
269
|
+
still use the key.
|
|
270
|
+
- A statement naming the account root only delegates to that account's IAM. The caller still needs
|
|
271
|
+
an identity policy allowing the action. The default key policy is of this kind.
|
|
272
|
+
|
|
273
|
+
```typescript sim-kms-key-policy
|
|
274
|
+
/**
|
|
275
|
+
* A simulated KMS key policy deciding who can use the key.
|
|
276
|
+
*/
|
|
277
|
+
|
|
278
|
+
import { CreateRoleCommand } from "@aws-sdk/client-iam";
|
|
279
|
+
import { CreateKeyCommand, EncryptCommand } from "@aws-sdk/client-kms";
|
|
280
|
+
|
|
281
|
+
import { SimAws } from "@kensio/yulin";
|
|
282
|
+
import { SimIamAccessDenied } from "@kensio/yulin/iam";
|
|
283
|
+
|
|
284
|
+
const simAws = new SimAws();
|
|
285
|
+
const accountId = simAws.defaultAccountId;
|
|
286
|
+
|
|
287
|
+
const role = await simAws.iam().createRole(
|
|
288
|
+
new CreateRoleCommand({
|
|
289
|
+
RoleName: "Encrypter",
|
|
290
|
+
AssumeRolePolicyDocument: JSON.stringify({
|
|
291
|
+
Version: "2012-10-17",
|
|
292
|
+
Statement: {
|
|
293
|
+
Effect: "Allow",
|
|
294
|
+
Principal: { AWS: `arn:aws:iam::${accountId}:root` },
|
|
295
|
+
Action: "sts:AssumeRole",
|
|
296
|
+
},
|
|
297
|
+
}),
|
|
298
|
+
}),
|
|
299
|
+
);
|
|
300
|
+
|
|
301
|
+
// A key policy naming the Role directly, with no delegation to the Account.
|
|
302
|
+
const created = await simAws.kms().createKey(
|
|
303
|
+
new CreateKeyCommand({
|
|
304
|
+
Policy: JSON.stringify({
|
|
305
|
+
Version: "2012-10-17",
|
|
306
|
+
Statement: [
|
|
307
|
+
{
|
|
308
|
+
Effect: "Allow",
|
|
309
|
+
Principal: { AWS: role.Role.Arn },
|
|
310
|
+
Action: "kms:Encrypt",
|
|
311
|
+
Resource: "*",
|
|
312
|
+
},
|
|
313
|
+
],
|
|
314
|
+
}),
|
|
315
|
+
}),
|
|
316
|
+
);
|
|
317
|
+
|
|
318
|
+
// The Role can encrypt, with no identity policy of its own.
|
|
319
|
+
await simAws.kms().encrypt(
|
|
320
|
+
new EncryptCommand({
|
|
321
|
+
KeyId: created.KeyMetadata?.Arn,
|
|
322
|
+
Plaintext: Buffer.from("hunter2", "utf8"),
|
|
323
|
+
}),
|
|
324
|
+
{ caller: { kind: "arn", arn: role.Role.Arn } },
|
|
325
|
+
);
|
|
326
|
+
|
|
327
|
+
// The Account root cannot, because this key policy does not admit it.
|
|
328
|
+
try {
|
|
329
|
+
await simAws.kms().encrypt(
|
|
330
|
+
new EncryptCommand({
|
|
331
|
+
KeyId: created.KeyMetadata?.Arn,
|
|
332
|
+
Plaintext: Buffer.from("hunter2", "utf8"),
|
|
333
|
+
}),
|
|
334
|
+
);
|
|
335
|
+
} catch (error) {
|
|
336
|
+
console.log(error instanceof SimIamAccessDenied); // true
|
|
337
|
+
}
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
Replacing a key policy with `PutKeyPolicyCommand` can lock an account out of its own key, as it can
|
|
341
|
+
on real KMS.
|
|
342
|
+
|
|
343
|
+
## AWS managed keys and `kms:ViaService`
|
|
344
|
+
|
|
345
|
+
An alias beginning `alias/aws/` names an AWS managed key, and the key is created the first time
|
|
346
|
+
something references it. Such a key gets the policy real AWS gives it, which differs from the
|
|
347
|
+
customer default:
|
|
348
|
+
|
|
349
|
+
- Use of the key is allowed to any principal in the owning account, but only when `kms:ViaService`
|
|
350
|
+
names the service that owns the key, such as `ssm.us-east-1.amazonaws.com` for `aws/ssm`.
|
|
351
|
+
- The account root is allowed to read the key's metadata. That is the whole of what this policy
|
|
352
|
+
delegates to IAM.
|
|
353
|
+
|
|
354
|
+
That is why a role holding only `ssm:GetParameter` reads a decrypted `SecureString` under `aws/ssm`,
|
|
355
|
+
and why a role holding `kms:Decrypt` on that key still cannot use it by calling KMS itself.
|
|
356
|
+
|
|
357
|
+
`kms:ViaService` is set by the service making the call on the caller's behalf. Sim SSM does this for
|
|
358
|
+
`SecureString` parameters. Code calling simulated KMS directly sets it with the `viaService` request
|
|
359
|
+
option, naming the service on its own (`ssm`), since the region is the key's.
|
|
360
|
+
|
|
361
|
+
```typescript sim-kms-aws-managed-key
|
|
362
|
+
/**
|
|
363
|
+
* An AWS managed key, usable only through the service that owns it.
|
|
364
|
+
*/
|
|
365
|
+
|
|
366
|
+
import { EncryptCommand, GetKeyPolicyCommand } from "@aws-sdk/client-kms";
|
|
367
|
+
|
|
368
|
+
import { SimAws } from "@kensio/yulin";
|
|
369
|
+
import { SimIamAccessDenied } from "@kensio/yulin/iam";
|
|
370
|
+
|
|
371
|
+
const simAws = new SimAws();
|
|
372
|
+
|
|
373
|
+
// A request reaching the key through Systems Manager, as Parameter Store makes
|
|
374
|
+
// it. No KMS permission is involved.
|
|
375
|
+
const encrypted = await simAws.kms().encrypt(
|
|
376
|
+
new EncryptCommand({
|
|
377
|
+
KeyId: "alias/aws/ssm",
|
|
378
|
+
Plaintext: Buffer.from("hunter2", "utf8"),
|
|
379
|
+
}),
|
|
380
|
+
{ viaService: "ssm" },
|
|
381
|
+
);
|
|
382
|
+
|
|
383
|
+
console.log(encrypted.KeyId); // the ARN of the aws/ssm key
|
|
384
|
+
|
|
385
|
+
// The same request made directly is denied, whatever IAM allows.
|
|
386
|
+
try {
|
|
387
|
+
await simAws.kms().encrypt(
|
|
388
|
+
new EncryptCommand({
|
|
389
|
+
KeyId: "alias/aws/ssm",
|
|
390
|
+
Plaintext: Buffer.from("hunter2", "utf8"),
|
|
391
|
+
}),
|
|
392
|
+
);
|
|
393
|
+
} catch (error) {
|
|
394
|
+
console.log(error instanceof SimIamAccessDenied); // true
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// Reading the key's metadata is allowed, because that much is delegated.
|
|
398
|
+
const policy = await simAws
|
|
399
|
+
.kms()
|
|
400
|
+
.getKeyPolicy(new GetKeyPolicyCommand({ KeyId: "alias/aws/ssm" }));
|
|
401
|
+
|
|
402
|
+
console.log(policy.Policy); // the via-service-scoped policy
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
A key policy of your own can use `kms:ViaService` too, with the ordinary condition operators, and it
|
|
406
|
+
matches for requests carrying the option. A request that names no service has no value for the key,
|
|
407
|
+
and a condition on it stays unmatched.
|
|
408
|
+
|
|
409
|
+
## Naming a key
|
|
410
|
+
|
|
411
|
+
Every operation takes its target as a `KeyId`, in any of the four forms real KMS accepts. Those are
|
|
412
|
+
a key ID, a key ARN, an alias name such as `alias/app-key`, and an alias ARN.
|
|
413
|
+
|
|
414
|
+
A key ARN or alias ARN naming another account or region resolves to no key at all. Its identifier is
|
|
415
|
+
never read out and looked up locally. A foreign ARN cannot reach a key that happens to share an
|
|
416
|
+
identifier.
|
|
417
|
+
|
|
418
|
+
Aliases beginning `alias/aws/` are reserved for AWS managed keys. `CreateAlias` refuses to create
|
|
419
|
+
one, but referencing one brings the key into existence, the way an AWS managed key appears on real
|
|
420
|
+
AWS when a service first needs it.
|
|
421
|
+
|
|
422
|
+
```typescript sim-kms-aliases
|
|
423
|
+
/**
|
|
424
|
+
* Naming a simulated KMS key by alias.
|
|
425
|
+
*/
|
|
426
|
+
|
|
427
|
+
import {
|
|
428
|
+
CreateAliasCommand,
|
|
429
|
+
CreateKeyCommand,
|
|
430
|
+
DescribeKeyCommand,
|
|
431
|
+
EncryptCommand,
|
|
432
|
+
} from "@aws-sdk/client-kms";
|
|
433
|
+
|
|
434
|
+
import { SimAws } from "@kensio/yulin";
|
|
435
|
+
|
|
436
|
+
const simAws = new SimAws();
|
|
437
|
+
const kms = simAws.kms();
|
|
438
|
+
|
|
439
|
+
const created = await kms.createKey(new CreateKeyCommand({}));
|
|
440
|
+
await kms.createAlias(
|
|
441
|
+
new CreateAliasCommand({
|
|
442
|
+
AliasName: "alias/app-key",
|
|
443
|
+
TargetKeyId: created.KeyMetadata?.KeyId,
|
|
444
|
+
}),
|
|
445
|
+
);
|
|
446
|
+
|
|
447
|
+
// The alias reaches the same key as its ID or ARN would.
|
|
448
|
+
await kms.encrypt(
|
|
449
|
+
new EncryptCommand({
|
|
450
|
+
KeyId: "alias/app-key",
|
|
451
|
+
Plaintext: Buffer.from("hunter2", "utf8"),
|
|
452
|
+
}),
|
|
453
|
+
);
|
|
454
|
+
|
|
455
|
+
// An AWS managed key appears the first time its alias is referenced.
|
|
456
|
+
const managed = await kms.describeKey(
|
|
457
|
+
new DescribeKeyCommand({ KeyId: "alias/aws/s3" }),
|
|
458
|
+
);
|
|
459
|
+
|
|
460
|
+
console.log(managed.KeyMetadata?.KeyManager); // "AWS"
|
|
461
|
+
```
|
|
462
|
+
|
|
463
|
+
## Key state and deletion
|
|
464
|
+
|
|
465
|
+
A key can be disabled and re-enabled later. A disabled key stays present, and refuses to be used.
|
|
466
|
+
|
|
467
|
+
Deletion is never immediate. `ScheduleKeyDeletion` sets a recovery window of 7 to 30 days, defaulting
|
|
468
|
+
to 30. During that window the key refuses to be used but can still be recovered with
|
|
469
|
+
`CancelKeyDeletion`. Cancelling leaves the key disabled. Re-enabling it is a separate step.
|
|
470
|
+
|
|
471
|
+
A disabled key fails cryptographic operations with `DisabledException`. A key pending deletion fails
|
|
472
|
+
with `KMSInvalidStateException`. The two are distinct. One means the key can be enabled again, the
|
|
473
|
+
other that it is on its way out.
|
|
474
|
+
|
|
475
|
+
## Scoping
|
|
476
|
+
|
|
477
|
+
KMS keys belong to an account and a region, as they do on real AWS. A key created in one region
|
|
478
|
+
cannot be found or used from another, and a ciphertext produced under it cannot be decrypted
|
|
479
|
+
elsewhere.
|
|
480
|
+
|
|
481
|
+
```typescript sim-kms-scoping
|
|
482
|
+
/**
|
|
483
|
+
* Simulated KMS keys are scoped to an account and region.
|
|
484
|
+
*/
|
|
485
|
+
|
|
486
|
+
import { CreateKeyCommand, DescribeKeyCommand } from "@aws-sdk/client-kms";
|
|
487
|
+
|
|
488
|
+
import { SimAws } from "@kensio/yulin";
|
|
489
|
+
import { SimKmsNotFoundException } from "@kensio/yulin/kms";
|
|
490
|
+
|
|
491
|
+
const simAws = new SimAws();
|
|
492
|
+
|
|
493
|
+
const created = await simAws
|
|
494
|
+
.account("222222222222")
|
|
495
|
+
.region("eu-west-2")
|
|
496
|
+
.kms()
|
|
497
|
+
.createKey(new CreateKeyCommand({}));
|
|
498
|
+
|
|
499
|
+
try {
|
|
500
|
+
await simAws
|
|
501
|
+
.account("222222222222")
|
|
502
|
+
.region("us-east-1")
|
|
503
|
+
.kms()
|
|
504
|
+
.describeKey(new DescribeKeyCommand({ KeyId: created.KeyMetadata?.Arn }));
|
|
505
|
+
} catch (error) {
|
|
506
|
+
console.log(error instanceof SimKmsNotFoundException); // true
|
|
507
|
+
}
|
|
508
|
+
```
|
|
509
|
+
|
|
510
|
+
## Deploying a key from CloudFormation
|
|
511
|
+
|
|
512
|
+
Simulated CloudFormation creates a key from an `AWS::KMS::Key` resource and points an
|
|
513
|
+
`AWS::KMS::Alias` at it, in the stack's account and region. The key comes out of the same `CreateKey`
|
|
514
|
+
path an SDK caller uses, with real key material. A `KeyPolicy` the template declares becomes the key
|
|
515
|
+
policy. Omitting `KeyPolicy` gets the default root-delegation policy, as `CreateKey` with no
|
|
516
|
+
`Policy` does.
|
|
517
|
+
|
|
518
|
+
`Ref` on the key gives its key ID, as on real AWS, and `Fn::GetAtt` gives `Arn` or `KeyId`. `Ref` on
|
|
519
|
+
the alias gives the alias name, such as `alias/app-key`, and an alias name is itself usable as a
|
|
520
|
+
`KeyId`. So a property wanting a key ID takes the `Ref`, while an IAM policy resource wanting the
|
|
521
|
+
key needs `Fn::GetAtt … Arn`.
|
|
522
|
+
|
|
523
|
+
```typescript sim-kms-cloudformation-key
|
|
524
|
+
/**
|
|
525
|
+
* Deploying a KMS key and alias from a CloudFormation template, then
|
|
526
|
+
* encrypting through the alias the template created.
|
|
527
|
+
*/
|
|
528
|
+
|
|
529
|
+
import { DecryptCommand, EncryptCommand } from "@aws-sdk/client-kms";
|
|
530
|
+
|
|
531
|
+
import { SimAws } from "@kensio/yulin";
|
|
532
|
+
|
|
533
|
+
const simAws = new SimAws();
|
|
534
|
+
|
|
535
|
+
const stack = await simAws.cloudFormation().deployTemplate({
|
|
536
|
+
stackName: "app-stack",
|
|
537
|
+
template: {
|
|
538
|
+
Resources: {
|
|
539
|
+
AppKey: {
|
|
540
|
+
Type: "AWS::KMS::Key",
|
|
541
|
+
Properties: { Description: "Application data key" },
|
|
542
|
+
},
|
|
543
|
+
AppKeyAlias: {
|
|
544
|
+
Type: "AWS::KMS::Alias",
|
|
545
|
+
Properties: {
|
|
546
|
+
AliasName: "alias/app-key",
|
|
547
|
+
TargetKeyId: { Ref: "AppKey" },
|
|
548
|
+
},
|
|
549
|
+
},
|
|
550
|
+
},
|
|
551
|
+
Outputs: {
|
|
552
|
+
KeyArn: { Value: { "Fn::GetAtt": ["AppKey", "Arn"] } },
|
|
553
|
+
},
|
|
554
|
+
},
|
|
555
|
+
});
|
|
556
|
+
await stack.waitForDeployComplete();
|
|
557
|
+
|
|
558
|
+
const kms = simAws.kms();
|
|
559
|
+
|
|
560
|
+
const encrypted = await kms.encrypt(
|
|
561
|
+
new EncryptCommand({
|
|
562
|
+
KeyId: "alias/app-key",
|
|
563
|
+
Plaintext: Buffer.from("hunter2", "utf8"),
|
|
564
|
+
}),
|
|
565
|
+
);
|
|
566
|
+
|
|
567
|
+
const decrypted = await kms.decrypt(
|
|
568
|
+
new DecryptCommand({ CiphertextBlob: encrypted.CiphertextBlob }),
|
|
569
|
+
);
|
|
570
|
+
|
|
571
|
+
console.log(Buffer.from(decrypted.Plaintext ?? []).toString("utf8")); // "hunter2"
|
|
572
|
+
console.log(stack.output("KeyArn")); // "arn:aws:kms:...:key/..."
|
|
573
|
+
```
|
|
574
|
+
|
|
575
|
+
A property asking for behaviour Yulin leaves out still deploys. The key is created without it, and
|
|
576
|
+
the property is recorded in
|
|
577
|
+
[`stack.ignoredProperties`](https://yulinsim.dev/services/cloudformation/#properties-a-resource-was-created-without).
|
|
578
|
+
The template deploys, and the record says what the key leaves out. `EnableKeyRotation`,
|
|
579
|
+
`RotationPeriodInDays`, `MultiRegion` and `Tags` are all recorded that way. The key encrypts and
|
|
580
|
+
decrypts, its material stays as it was, it exists in one region, and its tags go unread.
|
|
581
|
+
|
|
582
|
+
A `KeySpec` or `KeyUsage` pair simulated KMS has no model for, or an `Origin` other than `AWS_KMS`,
|
|
583
|
+
is refused by `CreateKey` in the same terms it refuses an SDK caller, since there is no key to
|
|
584
|
+
create at all. `Enabled: false` is supported, and deploys a key that is disabled from the moment it
|
|
585
|
+
exists.
|
|
586
|
+
|
|
587
|
+
## Inside a simulated Lambda handler
|
|
588
|
+
|
|
589
|
+
Function code requiring `@aws-sdk/client-kms` is routed into the same simulated AWS environment, with
|
|
590
|
+
the function's execution role as the caller. A handler that decrypts a value therefore has to be
|
|
591
|
+
allowed to, by both the key policy and the role's identity policy, the same as on real AWS. See
|
|
592
|
+
[simulated Lambda](https://yulinsim.dev/services/lambda/) for how function code and execution roles work.
|
|
593
|
+
|
|
594
|
+
## Available functionality
|
|
595
|
+
|
|
596
|
+
Sim KMS currently supports:
|
|
597
|
+
|
|
598
|
+
- `CreateKeyCommand`, for symmetric encryption keys and asymmetric signing keys
|
|
599
|
+
- `DescribeKeyCommand` and `ListKeysCommand`
|
|
600
|
+
- `GetKeyPolicyCommand` and `PutKeyPolicyCommand`
|
|
601
|
+
- `EncryptCommand` and `DecryptCommand`, including encryption context
|
|
602
|
+
- `GenerateDataKeyCommand`, by `KeySpec` or `NumberOfBytes`
|
|
603
|
+
- `SignCommand`, `VerifyCommand` and `GetPublicKeyCommand`, for asymmetric signing keys
|
|
604
|
+
- `CreateAliasCommand` and `ListAliasesCommand`, including AWS managed key aliases
|
|
605
|
+
- `EnableKeyCommand`, `DisableKeyCommand`, `ScheduleKeyDeletionCommand`, `CancelKeyDeletionCommand`
|
|
606
|
+
- Key policy evaluation by simulated IAM
|
|
607
|
+
- Key ARNs, key IDs, alias names and alias ARNs as interchangeable ways to name a key
|
|
608
|
+
- The `AWS::KMS::Key` and `AWS::KMS::Alias` CloudFormation resources
|
|
609
|
+
- Calls made from inside a simulated Lambda handler, authorized as the function's execution role
|
|
610
|
+
|
|
611
|
+
## Limitations
|
|
612
|
+
|
|
613
|
+
Current documented limitations:
|
|
614
|
+
|
|
615
|
+
- Only encryption with a symmetric key and signing with an asymmetric key are simulated. Left out
|
|
616
|
+
are asymmetric encryption (`RSAES_OAEP_SHA_1` and `RSAES_OAEP_SHA_256`), HMAC keys and
|
|
617
|
+
`GENERATE_VERIFY_MAC`, key agreement and `DeriveSharedSecret`, the `SM2` key spec, and
|
|
618
|
+
`ReEncrypt`. An RSA key asked for with `KeyUsage: ENCRYPT_DECRYPT` is refused, which real KMS
|
|
619
|
+
allows.
|
|
620
|
+
- `Sign` and `Verify` take a `MessageType` of `RAW` only. A `DIGEST` message is refused. Node cannot
|
|
621
|
+
sign a digest that has already been computed, because `crypto.sign` with no algorithm hashes what
|
|
622
|
+
it is given. The resulting signature would differ from the one real KMS makes, and would fail to
|
|
623
|
+
verify against the message anywhere outside the simulator.
|
|
624
|
+
- Generating an RSA key pair is real key generation. An `RSA_4096` key costs a second or so of test
|
|
625
|
+
time. The ECC specs are effectively free.
|
|
626
|
+
- Imported key material and custom key stores are left out. `Origin` other than `AWS_KMS` is
|
|
627
|
+
refused.
|
|
628
|
+
- Grants (`CreateGrant` and friends) are left out.
|
|
629
|
+
- Key material stays as it was created. An `AWS::KMS::Key` declaring `EnableKeyRotation` or
|
|
630
|
+
`RotationPeriodInDays` is created without it, and the property is recorded in
|
|
631
|
+
`stack.ignoredProperties`.
|
|
632
|
+
- A Multi-Region key comes out as an ordinary key in one region. An `AWS::KMS::Key` declaring
|
|
633
|
+
`MultiRegion: true` deploys that way, and the property is recorded.
|
|
634
|
+
- `AWS::KMS::Key` accepts but ignores `PendingWindowInDays` and `BypassPolicyLockoutSafetyCheck`.
|
|
635
|
+
Both are inert here. A stack teardown schedules the key for deletion with the default window, and
|
|
636
|
+
simulated KMS applies no policy lockout safety check to bypass.
|
|
637
|
+
- An `AWS::KMS::Alias` retargeted by a stack update is deleted and created again pointing at the new
|
|
638
|
+
key, because a stack update replaces a changed resource and simulated KMS has no `UpdateAlias`.
|
|
639
|
+
- A template declaring `AWS::KMS::Grant` or `AWS::KMS::ReplicaKey` is refused.
|
|
640
|
+
- A key pending deletion stays in that state indefinitely. Advancing the simulated clock past the
|
|
641
|
+
recovery window leaves it there, and the key ID stays taken.
|
|
642
|
+
- `UpdateAlias` is absent. An alias is retargeted by deleting it and creating it again.
|
|
643
|
+
- Tags, `ListResourceTags` and the `aws:ResourceTag` condition key are left out. An
|
|
644
|
+
`AWS::KMS::Key` declaring `Tags` deploys with the tags dropped and the property recorded. A policy
|
|
645
|
+
condition written around one fails to match here, where on AWS it would match.
|
|
646
|
+
- `kms:ViaService` and `kms:CallerAccount` are the KMS-specific condition keys derived. A policy
|
|
647
|
+
relying on `kms:EncryptionContext:*` or any other one fails to match. Ordinary condition operators
|
|
648
|
+
on values sim IAM does supply work as usual.
|
|
649
|
+
- The service an AWS managed key belongs to is taken from its alias. `alias/aws/ebs` is scoped to
|
|
650
|
+
`ebs.<region>.amazonaws.com`. Real AWS scopes that one to EC2. The two agree for every service
|
|
651
|
+
Yulin simulates.
|
|
652
|
+
- Key material lives in process memory for the lifetime of the `SimAws` instance. Anything sharing
|
|
653
|
+
the process can reach it.
|
|
654
|
+
- Sim SSM encrypts `SecureString` parameters with simulated keys, checking `kms:Encrypt` and
|
|
655
|
+
`kms:Decrypt`, and sim Secrets Manager encrypts every secret version, checking
|
|
656
|
+
`kms:GenerateDataKey` and `kms:Decrypt`. No other simulated service uses simulated keys. Sim S3,
|
|
657
|
+
sim DynamoDB and sim Lambda environment variables leave them alone, and skip the `kms:Decrypt`
|
|
658
|
+
check.
|
|
659
|
+
- KMS is not served as an HTTP API by `serveSimAws`.
|