mirror of
https://github.com/ansible/awx.git
synced 2026-05-12 11:57:37 -02:30
Prevent automountServiceAccountToken in containergroup pod sepc (#15586)
* Prevent job pod from mounting serviceaccount token * Add serializer validation for cg pod_spec_override Prevent automountServiceAccountToken to be set to true and provide an error message when automountServiceAccountToken is being set to true
This commit is contained in:
@@ -6,6 +6,7 @@ import copy
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
|
import yaml
|
||||||
from collections import Counter, OrderedDict
|
from collections import Counter, OrderedDict
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
@@ -5916,6 +5917,34 @@ class InstanceGroupSerializer(BaseSerializer):
|
|||||||
raise serializers.ValidationError(_('Only Kubernetes credentials can be associated with an Instance Group'))
|
raise serializers.ValidationError(_('Only Kubernetes credentials can be associated with an Instance Group'))
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
def validate_pod_spec_override(self, value):
|
||||||
|
if not value:
|
||||||
|
return value
|
||||||
|
|
||||||
|
# value should be empty for non-container groups
|
||||||
|
if self.instance and not self.instance.is_container_group:
|
||||||
|
raise serializers.ValidationError(_('pod_spec_override is only valid for container groups'))
|
||||||
|
|
||||||
|
pod_spec_override_json = None
|
||||||
|
# defect if the value is yaml or json if yaml convert to json
|
||||||
|
try:
|
||||||
|
# convert yaml to json
|
||||||
|
pod_spec_override_json = yaml.safe_load(value)
|
||||||
|
except yaml.YAMLError:
|
||||||
|
try:
|
||||||
|
pod_spec_override_json = json.loads(value)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
raise serializers.ValidationError(_('pod_spec_override must be valid yaml or json'))
|
||||||
|
|
||||||
|
# validate the
|
||||||
|
spec = pod_spec_override_json.get('spec', {})
|
||||||
|
automount_service_account_token = spec.get('automountServiceAccountToken', False)
|
||||||
|
|
||||||
|
if automount_service_account_token:
|
||||||
|
raise serializers.ValidationError(_('automountServiceAccountToken is not allowed for security reasons'))
|
||||||
|
|
||||||
|
return value
|
||||||
|
|
||||||
def validate(self, attrs):
|
def validate(self, attrs):
|
||||||
attrs = super(InstanceGroupSerializer, self).validate(attrs)
|
attrs = super(InstanceGroupSerializer, self).validate(attrs)
|
||||||
|
|
||||||
|
|||||||
@@ -174,6 +174,9 @@ class PodManager(object):
|
|||||||
)
|
)
|
||||||
pod_spec['spec']['containers'][0]['name'] = self.pod_name
|
pod_spec['spec']['containers'][0]['name'] = self.pod_name
|
||||||
|
|
||||||
|
# Prevent mounting of service account token in job pods in order to prevent job pods from accessing the k8s API via in cluster service account auth
|
||||||
|
pod_spec['spec']['automountServiceAccountToken'] = False
|
||||||
|
|
||||||
return pod_spec
|
return pod_spec
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user