|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { getRegionFromIdentifier } from '@aws-crypto/kms-keyring' |
| 5 | +import { assertValidNotAliasArn } from './kms_config_helpers' |
| 6 | + |
| 7 | +// a general interface that outlines common operations any of the 4 AWS KMS |
| 8 | +// configurations should perform |
| 9 | +export interface KmsConfig { |
| 10 | + /** |
| 11 | + * this method tells the user if the config is SRK/MRK compatibility |
| 12 | + * @returns a flag answering the method's purpose |
| 13 | + */ |
| 14 | + isKmsKeyArn(): boolean |
| 15 | + |
| 16 | + /** |
| 17 | + * this method tells the user if the config is MrDiscovery |
| 18 | + * @returns a flag answering the method's purpose |
| 19 | + */ |
| 20 | + isMrDiscovery(): boolean |
| 21 | + |
| 22 | + /** |
| 23 | + * this method tells the user if the config is Discovery |
| 24 | + * @returns a flag answering the method's purpose |
| 25 | + */ |
| 26 | + isDiscovery(): boolean |
| 27 | + |
| 28 | + /** |
| 29 | + * this method tells the user if the config is compatible with an arn |
| 30 | + * @param otherArn |
| 31 | + * @returns a flag answering the method's purpose |
| 32 | + */ |
| 33 | + isCompatibleWithArn(otherArn: string): boolean |
| 34 | +} |
| 35 | + |
| 36 | +// an interface to outline the common operations any of the 3 region-based AWS KMS |
| 37 | +// configurations should perform |
| 38 | +export interface RegionalKmsConfig extends KmsConfig { |
| 39 | + /** |
| 40 | + * this method tells the user the config's region |
| 41 | + * @returns the region |
| 42 | + */ |
| 43 | + getRegion(): string |
| 44 | +} |
| 45 | + |
| 46 | +// an abstract class defining common behavior for operations that SRK and MRK compatibility |
| 47 | +// configs should perform |
| 48 | +export abstract class KmsKeyArnConfig implements RegionalKmsConfig { |
| 49 | + private _arn: string |
| 50 | + |
| 51 | + //= aws-encryption-sdk-specification/framework/branch-key-store.md#aws-kms-configuration |
| 52 | + //# `KMS Key ARN` and `KMS MRKey ARN` MUST take an additional argument |
| 53 | + //# that is a KMS ARN. |
| 54 | + constructor(arn: string) { |
| 55 | + //= aws-encryption-sdk-specification/framework/branch-key-store.md#aws-kms-configuration |
| 56 | + //# This ARN MUST NOT be an Alias. |
| 57 | + //# This ARN MUST be a valid |
| 58 | + //# [AWS KMS Key ARN](./aws-kms/aws-kms-key-arn.md#a-valid-aws-kms-arn). |
| 59 | + assertValidNotAliasArn(arn) |
| 60 | + this._arn = arn |
| 61 | + } |
| 62 | + |
| 63 | + getRegion(): string { |
| 64 | + return getRegionFromIdentifier(this._arn) |
| 65 | + } |
| 66 | + |
| 67 | + isKmsKeyArn(): boolean { |
| 68 | + return true |
| 69 | + } |
| 70 | + |
| 71 | + isMrDiscovery(): boolean { |
| 72 | + return false |
| 73 | + } |
| 74 | + |
| 75 | + isDiscovery(): boolean { |
| 76 | + return false |
| 77 | + } |
| 78 | + |
| 79 | + getArn(): string { |
| 80 | + return this._arn |
| 81 | + } |
| 82 | + |
| 83 | + abstract isCompatibleWithArn(otherArn: string): boolean |
| 84 | +} |
| 85 | + |
| 86 | +// a concrete class specifiying behavior for the SRK compatibility config |
| 87 | +export class SrkCompatibilityKmsConfig extends KmsKeyArnConfig { |
| 88 | + constructor(arn: string) { |
| 89 | + super(arn) |
| 90 | + } |
| 91 | + |
| 92 | + //= aws-encryption-sdk-specification/framework/branch-key-store.md#aws-key-arn-compatibility |
| 93 | + //# For two ARNs to be compatible: |
| 94 | + |
| 95 | + //# If the [AWS KMS Configuration](#aws-kms-configuration) designates single region ARN compatibility, |
| 96 | + //# then two ARNs are compatible if they are exactly equal. |
| 97 | + isCompatibleWithArn(otherArn: string): boolean { |
| 98 | + return this.getArn() === otherArn |
| 99 | + } |
| 100 | +} |
0 commit comments