Skip to content

Commit b8c8fc0

Browse files
authored
Add EksPublicAccessCidrs parameter and enable EKS control plane logging (#117)
* add EKS public_access_cidrs * enable api, audit, and authenticator EKS control plane logging * update changelog * allow bastion access to Kubernetes API endpoint * use eks.LaunchTemplateSpecification to enforce HttpTokens-based metadata * update changelog * set HttpPutResponseHopLimit=3
1 parent d6382ec commit b8c8fc0

File tree

3 files changed

+74
-6
lines changed

3 files changed

+74
-6
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ Change Log
1010
* Add ``EksClusterName`` parameter to control name of EKS cluster. If upgrading, set this to STACK_NAME-cluster to match existing name.
1111
* Drop support for RDS PostgreSQL 9.x
1212
* Upgrade to troposphere v4.2.0
13-
13+
* Add ``EksPublicAccessCidrs`` parameter to optionally restrict access to your public Kubernetes API endpoint using CIDR blocks. If defined, both public and private endpoint access enabled as detailed in `API server endpoint access options <https://docs.aws.amazon.com/eks/latest/userguide/cluster-endpoint.html#modify-endpoint-access>`_.
14+
* Enable ``api``, ``audit``, and ``authenticator`` log types for `EKS control plane logging <https://docs.aws.amazon.com/eks/latest/userguide/control-plane-logs.html>`_.
15+
* Allow bastion access to Kubernetes API endpoint
16+
* Add ``eks.LaunchTemplateSpecification`` to enforce `HttpTokens-based metadata <https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html>`_
1417

1518
`2.1.2`_ (2022-03-10)
1619
---------------------

stack/bastion.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,18 @@
238238
if USE_EKS:
239239
from .eks import cluster
240240
backend_server_id = GetAtt(cluster, "ClusterSecurityGroupId")
241+
# Allow bastion access to Kubernetes API endpoint
242+
container_security_group_k8s_ingress = ec2.SecurityGroupIngress(
243+
'ContainerSecurityGroupKubernetesBastionIngress',
244+
template=template,
245+
GroupId=backend_server_id,
246+
IpProtocol='tcp',
247+
FromPort=443,
248+
ToPort=443,
249+
SourceSecurityGroupId=Ref(bastion_security_group),
250+
Condition=bastion_type_set,
251+
Description="Kubernetes API endpoint",
252+
)
241253
else:
242254
from .security_groups import container_security_group
243255
backend_server_id = Ref(container_security_group)

stack/eks.py

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
iam
1515
)
1616

17-
from .common import cmk_arn
17+
from .common import cmk_arn, use_aes256_encryption, use_cmk_arn
1818
from .containers import (
1919
container_instance_role,
2020
container_instance_type,
@@ -69,7 +69,7 @@
6969
AllowedValues=["true", "false"],
7070
Default="false",
7171
),
72-
group="Global",
72+
group="Elastic Kubernetes Service (EKS)",
7373
label="Enable EKS EncryptionConfig",
7474
))
7575
use_eks_encryption_config_cond = "EnableEksEncryptionConfigCond"
@@ -78,6 +78,20 @@
7878
Not(Equals(Ref(cmk_arn), ""))
7979
))
8080

81+
# https://docs.aws.amazon.com/eks/latest/userguide/cluster-endpoint.html#modify-endpoint-access
82+
public_access_cidrs = Ref(template.add_parameter(
83+
Parameter(
84+
"EksPublicAccessCidrs",
85+
Description="The CIDR blocks that are allowed access to your cluster's public Kubernetes API server endpoint.", # noqa
86+
Type="CommaDelimitedList",
87+
Default="",
88+
),
89+
group="Elastic Kubernetes Service (EKS)",
90+
label="Kubernetes API public access CIDRs",
91+
))
92+
restrict_eks_api_access_cond = "RestrictEksApiAccessCond"
93+
template.add_condition(restrict_eks_api_access_cond, Not(Equals(Join("", public_access_cidrs), "")))
94+
8195
# Unlike most other resources in the stack, we specify the cluster name
8296
# via a stack parameter so it's easy to find and so it cannot be accidentally
8397
# recreated (for example if the ResourcesVpcConfig is changed).
@@ -87,14 +101,23 @@
87101
Description="The unique name to give to your cluster.", # noqa
88102
Type="String",
89103
),
90-
group="Global",
104+
group="Elastic Kubernetes Service (EKS)",
91105
label="Cluster name",
92106
))
93107

94108
cluster = eks.Cluster(
95109
"EksCluster",
96110
template=template,
97111
Name=cluster_name,
112+
Logging=eks.Logging(
113+
ClusterLogging=eks.ClusterLogging(
114+
EnabledTypes=[
115+
eks.LoggingTypeConfig(Type="api"),
116+
eks.LoggingTypeConfig(Type="audit"),
117+
eks.LoggingTypeConfig(Type="authenticator"),
118+
]
119+
)
120+
),
98121
ResourcesVpcConfig=eks.ResourcesVpcConfig(
99122
SubnetIds=[
100123
# For load balancers
@@ -105,6 +128,9 @@
105128
Ref(private_subnet_b),
106129
],
107130
SecurityGroupIds=[Ref(eks_security_group)],
131+
EndpointPrivateAccess=If(restrict_eks_api_access_cond, True, False),
132+
EndpointPublicAccess=True,
133+
PublicAccessCidrs=If(restrict_eks_api_access_cond, public_access_cidrs, NoValue),
108134
),
109135
EncryptionConfig=If(
110136
use_eks_encryption_config_cond,
@@ -114,6 +140,32 @@
114140
RoleArn=GetAtt(eks_service_role, "Arn"),
115141
)
116142

143+
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-launchtemplate.html
144+
nodegroup_launch_template = ec2.LaunchTemplate(
145+
"NodegroupLaunchTemplate",
146+
template=template,
147+
LaunchTemplateData=ec2.LaunchTemplateData(
148+
BlockDeviceMappings=[
149+
ec2.LaunchTemplateBlockDeviceMapping(
150+
DeviceName="/dev/xvda",
151+
Ebs=ec2.EBSBlockDevice(
152+
DeleteOnTermination=True,
153+
Encrypted=use_aes256_encryption,
154+
KmsKeyId=If(use_cmk_arn, Ref(cmk_arn), Ref("AWS::NoValue")),
155+
VolumeType="gp2",
156+
VolumeSize=container_volume_size,
157+
),
158+
),
159+
],
160+
InstanceType=container_instance_type,
161+
MetadataOptions=ec2.MetadataOptions(
162+
HttpTokens="required",
163+
# Why 3? See note: https://github.com/adamchainz/ec2-metadata#instance-metadata-service-version-2
164+
HttpPutResponseHopLimit=3,
165+
),
166+
)
167+
)
168+
117169
eks.Nodegroup(
118170
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-eks-nodegroup.html
119171
"Nodegroup",
@@ -125,9 +177,10 @@
125177
ClusterName=Ref(cluster),
126178
# The NodeRole must be specified as an ARN.
127179
NodeRole=GetAtt(container_instance_role, "Arn"),
180+
LaunchTemplate=eks.LaunchTemplateSpecification(
181+
Id=Ref(nodegroup_launch_template),
182+
),
128183
# The rest are optional.
129-
DiskSize=container_volume_size,
130-
InstanceTypes=[container_instance_type],
131184
ScalingConfig=eks.ScalingConfig(
132185
DesiredSize=desired_container_instances,
133186
MaxSize=max_container_instances,

0 commit comments

Comments
 (0)