Skip to content
v2.277.0

Mixin Class ​

Abstract base class for mixins that provides default implementations.

Example ​

ts
class MyEncryptionAtRest extends Mixin {
  constructor(props: MyEncryptionAtRestProps = {}) {
    super();
    // Validate Mixin props at construction time
    if (props.bucketKey && props.algorithm === 'aws:kms:dsse') {
      throw new Error("Cannot use S3 Bucket Key and DSSE together");
    }
  }

  supports(construct: any): construct is s3.CfnBucket {
    return s3.CfnBucket.isCfnBucket(construct);
  }

  applyTo(target: s3.CfnBucket): s3.CfnBucket {
    // Validate pre-conditions on the target, throw if error is unrecoverable
    if (!target.bucketEncryption) {
      throw new Error("Bucket encryption not configured");
    }

    // Validate properties are met after app execution
    target.node.addValidation({
      validate: () => isKmsEncrypted(target)
        ? ['This bucket must use aws:kms encryption.']
        : []
    });

    target.bucketEncryption = {
      serverSideEncryptionConfiguration: [{
        bucketKeyEnabled: true,
        serverSideEncryptionByDefault: {
          sseAlgorithm: "aws:kms"
        }
      }]
    };
    return target;
  }
}
python
class MyEncryptionAtRest(Mixin):
    def __init__(self, *, bucketKey=None, algorithm=None):
        super().__init__()
        # Validate Mixin props at construction time
        if bucket_key && algorithm == "aws:kms:dsse": throw new Error("Cannot use S3 Bucket Key and DSSE together");

    def supports(self, construct):
        return s3.CfnBucket.is_cfn_bucket(construct)

    def apply_to(self, target):
        # Validate pre-conditions on the target, throw if error is unrecoverable
        if not target.bucket_encryption: throw new Error("Bucket encryption not configured");

        # Validate properties are met after app execution
        target.node.add_validation({
            "validate": () => isKmsEncrypted(target)
                    ? ['This bucket must use aws:kms encryption.']
                    : []
        })

        target.bucket_encryption = s3.CfnBucket.BucketEncryptionProperty(
            server_side_encryption_configuration=[s3.CfnBucket.ServerSideEncryptionRuleProperty(
                bucket_key_enabled=True,
                server_side_encryption_by_default=s3.CfnBucket.ServerSideEncryptionByDefaultProperty(
                    sse_algorithm="aws:kms"
                )
            )]
        )
        return target
java
public class MyEncryptionAtRest extends Mixin {
    public MyEncryptionAtRest() {
        this(new MyEncryptionAtRestProps());
    }

    public MyEncryptionAtRest(MyEncryptionAtRestProps props) {
        super();
        // Validate Mixin props at construction time
        if (props.getBucketKey() && props.getAlgorithm() == "aws:kms:dsse") {throw new Error("Cannot use S3 Bucket Key and DSSE together");
        }
    }

    public boolean supports(Object construct) {
        return CfnBucket.isCfnBucket(construct);
    }

    public CfnBucket applyTo(CfnBucket target) {
        // Validate pre-conditions on the target, throw if error is unrecoverable
        if (!target.getBucketEncryption()) {throw new Error("Bucket encryption not configured");
        }

        // Validate properties are met after app execution
        target.node.addValidation(Map.of(
                "validate", () => isKmsEncrypted(target)
                        ? ['This bucket must use aws:kms encryption.']
                        : []));

        target.getBucketEncryption() = BucketEncryptionProperty.builder()
                .serverSideEncryptionConfiguration(List.of(ServerSideEncryptionRuleProperty.builder()
                        .bucketKeyEnabled(true)
                        .serverSideEncryptionByDefault(ServerSideEncryptionByDefaultProperty.builder()
                                .sseAlgorithm("aws:kms")
                                .build())
                        .build()))
                .build();
        return target;
    }
}
csharp
// Install: dotnet add package Amazon.CDK.Lib
class MyEncryptionAtRest : Mixin
{
    public MyEncryptionAtRest(MyEncryptionAtRestProps props=new MyEncryptionAtRestProps { }) : base()
    {
        // Validate Mixin props at construction time
        if (props.BucketKey && props.Algorithm == "aws:kms:dsse")
        {throw new Error("Cannot use S3 Bucket Key and DSSE together");
        }
    }

    public boolean Supports(object construct)
    {
        return CfnBucket.IsCfnBucket(construct);
    }

    public CfnBucket ApplyTo(CfnBucket target)
    {
        // Validate pre-conditions on the target, throw if error is unrecoverable
        if (!target.BucketEncryption)
        {throw new Error("Bucket encryption not configured");
        }

        // Validate properties are met after app execution
        target.Node.AddValidation(new Dictionary<string, validate> {
            { "validate", () => isKmsEncrypted(target)
                    ? ['This bucket must use aws:kms encryption.']
                    : [] }
        });

        target.BucketEncryption = new BucketEncryptionProperty {
            ServerSideEncryptionConfiguration = new [] { new ServerSideEncryptionRuleProperty {
                BucketKeyEnabled = true,
                ServerSideEncryptionByDefault = new ServerSideEncryptionByDefaultProperty {
                    SseAlgorithm = "aws:kms"
                }
            } }
        };
        return target;
    }
}
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
type myEncryptionAtRest struct {
	Mixin
}

func newMyEncryptionAtRest(props myEncryptionAtRestProps) *myEncryptionAtRest {
	if props == nil {
		props = &myEncryptionAtRestProps{
		}
	}
	this := &myEncryptionAtRest{}
	newMixin_Override(this, )
	// Validate Mixin props at construction time
	if *props.bucketKey && *props.algorithm == "aws:kms:dsse" {throw new Error("Cannot use S3 Bucket Key and DSSE together");
	}
	return this
}

func (this *myEncryptionAtRest) supports(construct interface{}) *bool {
	return s3.CfnBucket_IsCfnBucket(*construct)
}

func (this *myEncryptionAtRest) applyTo(target CfnBucket) CfnBucket {
	// Validate pre-conditions on the target, throw if error is unrecoverable
	if !*target.bucketEncryption {throw new Error("Bucket encryption not configured");
	}

	// Validate properties are met after app execution
	*target.Node.AddValidation(map[string]validate{
		"validate": () => isKmsEncrypted(target)
		        ? ['This bucket must use aws:kms encryption.']
		        : [],
	})

	*target.bucketEncryption = &BucketEncryptionProperty{
		ServerSideEncryptionConfiguration: []interface{}{
			&ServerSideEncryptionRuleProperty{
				BucketKeyEnabled: jsii.Boolean(true),
				ServerSideEncryptionByDefault: &ServerSideEncryptionByDefaultProperty{
					SseAlgorithm: jsii.String("aws:kms"),
				},
			},
		},
	}
	return *target
}

Initializer ​

ts
new Mixin()
python
Mixin()
java
new Mixin()
csharp
new Mixin()
go
NewMixin()

Methods ​

applyTo(construct: IConstruct): voiddef apply_to(self, construct: IConstruct) -> Nonepublic void applyTo(IConstruct construct)public void ApplyTo(IConstruct construct)func ApplyTo(Construct IConstruct) #
Applies the mixin functionality to the target construct.
isMixin(x: any): boolean@staticmethod def is_mixin(x: Any) -> boolpublic static Boolean isMixin(Object x)public static bool IsMixin(object x)func IsMixin(X interface{}) *bool static #
Checks if `x` is a Mixin.
1 parameter · returns

Parameters

  • xxxxX — Any object.

Returns: true if x is an object created from a class which extends Mixin.

supports(_construct: IConstruct): booleandef supports(self, _construct: IConstruct) -> boolpublic Boolean supports(IConstruct _construct)public bool Supports(IConstruct _construct)func Supports(_construct IConstruct) *bool #
Determines whether this mixin can be applied to the given construct.