Skip to content

Commit 415700e

Browse files
committed
+ Added fargate profile mapping to hard-coded IGNORED_CM_IDENTITIES
+ Fixed incorrect dictionary access (failed some tests) - Removed IGNORED_CM_IDENTITIES env variable from the auth-operator spec
1 parent 17eb758 commit 415700e

File tree

9 files changed

+696
-621
lines changed

9 files changed

+696
-621
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ repos:
1717
- id: pretty-format-json
1818
- id: trailing-whitespace
1919
- repo: https://github.com/psf/black
20-
rev: 21.5b2
20+
rev: "23.1.0"
2121
hooks:
2222
- id: black
2323
- repo: https://github.com/pre-commit/mirrors-mypy

.vscode/launch.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Python: Current File",
6+
"type": "python",
7+
"request": "launch",
8+
"program": "${file}",
9+
"console": "integratedTerminal",
10+
"justMyCode": true,
11+
"env": {
12+
"_PYTEST_RAISE": "1"
13+
}
14+
}
15+
]
16+
}

.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"python.testing.pytestArgs": [
3+
"tests"
4+
],
5+
"python.testing.unittestEnabled": false,
6+
"python.testing.pytestEnabled": true
7+
}

conftest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import sys
2+
import pytest
3+
4+
def is_debugging():
5+
if 'debugpy' in sys.modules:
6+
return True
7+
return False
8+
9+
10+
# enable_stop_on_exceptions if the debugger is running during a test
11+
if is_debugging():
12+
@pytest.hookimpl(tryfirst=True)
13+
def pytest_exception_interact(call):
14+
raise call.excinfo.value
15+
16+
@pytest.hookimpl(tryfirst=True)
17+
def pytest_internalerror(excinfo):
18+
raise excinfo.value

kubernetes/auth-operator.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ spec:
2929
image: dcodetech/aws_auth_eks_crd:1.1.8
3030
imagePullPolicy: IfNotPresent
3131
name: operator
32-
env:
33-
- name: IGNORED_CM_IDENTITIES
34-
value: "system:node:{{SessionName}},system:node:{{EC2PrivateDNSName}}"
3532
ports:
3633
- containerPort: 8080
3734
protocol: TCP

poetry.lock

Lines changed: 631 additions & 607 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ pytest-cov = "^2.12.1"
2828
PyYAML = "^5.4.1"
2929
vulture = "^2.3"
3030

31+
[tool.poetry.group.dev.dependencies]
32+
autopep8 = "^2.0.2"
33+
3134
[tool.pylint]
3235
[tool.pylint.message_control]
3336
disable = [

src/kubernetes_operator/iam_mapping.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
# Allow some mappings in the aws-auth ConfigMap to exist without being defined
3535
# in a IamIdentityMapping object.
3636
IGNORED_CM_IDENTITIES = [
37-
# EKS worker nodes
38-
# "system:node:{{EC2PrivateDNSName}}",
37+
# Fargate profile mapping
38+
"system:node:{{SessionName}}",
3939
]
4040

4141

@@ -49,7 +49,7 @@ async def update_mapping(old, new, diff, **_) -> None:
4949
await delete_mapping(old["spec"])
5050
await create_mapping(new["spec"], diff)
5151

52-
# @kopf.on.update(GROUP, VERSION, PLURAL)
52+
5353
@kopf.on.create(GROUP, VERSION, PLURAL)
5454
async def create_mapping(spec: dict, diff: list, **_) -> None:
5555
"""Create/update an identity mapping in the aws-auth configmap with the corresponding IamIdentityMapping.
@@ -223,11 +223,11 @@ def ensure_identity(identity: dict, identity_list: list) -> list:
223223

224224
for i, existing_identity in enumerate(identity_list):
225225
# Handle existing identity
226-
if "rolearn" in existing_identity and existing_identity["rolearn"] == identity["rolearn"]:
226+
if "rolearn" in existing_identity and existing_identity.get("rolearn") == identity.get("rolearn"):
227227
identity_list[i] = identity
228228
return identity_list
229-
230-
if "userarn" in existing_identity and existing_identity["userarn"] == identity["userarn"]:
229+
230+
if "userarn" in existing_identity and existing_identity.get("userarn") == identity.get("userarn"):
231231
identity_list[i] = identity
232232
return identity_list
233233

@@ -245,11 +245,11 @@ def delete_identity(identity: dict, identity_list: list) -> list:
245245
"""
246246

247247
for i, existing_user in enumerate(identity_list):
248-
if "rolearn" in existing_user and existing_user["rolearn"] == identity["rolearn"]:
248+
if "rolearn" in existing_user and existing_user.get("rolearn") == identity.get("rolearn"):
249249
del identity_list[i]
250250
return identity_list
251251

252-
if "userarn" in existing_user and existing_user["userarn"] == identity["userarn"]:
252+
if "userarn" in existing_user and existing_user.get("userarn") == identity.get("userarn"):
253253
del identity_list[i]
254254
return identity_list
255255

tests/kubernetes_operator/test_iam_mapping.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@
3232
"userarn": "arn:aws:iam::000000000000:role/dev-infra-us-east-000000000000000000000000000",
3333
"username": "system:node:{{EC2PrivateDNSName}}",
3434
}
35+
SPEC_FARGATE_PROFILE_ROLE_TO_IGNORE = {
36+
"groups": ["system:bootstrappers", "system:nodes"],
37+
"rolearn": "arn:aws:iam::000000000000:role/dev-infra-us-east-000000000000000000000000000",
38+
"username": "system:node:{{SessionName}}",
39+
}
3540

41+
EMPTY_DATA = {"mapRoles": yaml.safe_dump([]), "mapUsers": yaml.safe_dump([])}
3642
DATA = {"mapRoles": yaml.safe_dump([SPEC_CSEC_ADMIN]), "mapUsers": yaml.safe_dump([SPEC_USER_JOHNDOE])}
3743
DATA_MISSING_MAPUSERS = {"mapRoles": yaml.safe_dump([SPEC_CSEC_ADMIN])}
3844
DATA_MISSING_MAPROLES = {"mapUsers": yaml.safe_dump([SPEC_USER_JOHNDOE])}
@@ -185,9 +191,12 @@ def test_check_synchronization_no_diff(api_client, custom_objects_api):
185191
custom_objects_api.list_cluster_custom_object.assert_called_with(GROUP, VERSION, PLURAL)
186192

187193

194+
@patch.dict(
195+
environ, {"IGNORED_CM_IDENTITIES": f"{SPEC_USER_SYSTEM_NODE_TO_IGNORE.get('username')}"}
196+
)
188197
def test_check_synchronization_no_diff_with_ignored_identity(api_client, custom_objects_api):
189198
data = {
190-
"mapRoles": yaml.safe_dump([SPEC_CSEC_ADMIN, SPEC_USER_SYSTEM_NODE_TO_IGNORE]),
199+
"mapRoles": yaml.safe_dump([SPEC_CSEC_ADMIN, SPEC_USER_SYSTEM_NODE_TO_IGNORE, SPEC_FARGATE_PROFILE_ROLE_TO_IGNORE]),
191200
"mapUsers": yaml.safe_dump([SPEC_USER_JOHNDOE]),
192201
}
193202
configmap_with_ignore_mapping = client.V1ConfigMap(
@@ -201,7 +210,8 @@ def test_check_synchronization_no_diff_with_ignored_identity(api_client, custom_
201210

202211

203212
@patch.dict(
204-
environ, {"IGNORED_CM_IDENTITIES": f"{SPEC_USER_MARK.get('username')},{SPEC_CSEC_MAINTENANCE.get('username')}"}
213+
environ, {
214+
"IGNORED_CM_IDENTITIES": f"{SPEC_USER_MARK.get('username')},{SPEC_CSEC_MAINTENANCE.get('username')},{SPEC_USER_SYSTEM_NODE_TO_IGNORE.get('username')}"}
205215
)
206216
def test_check_synchronization_no_diff_with_ignored_identity_env(api_client, custom_objects_api):
207217
data = {

0 commit comments

Comments
 (0)