Control: CronJob containers --service-account-key-file argument should be set as appropriate
Description
This check ensures that the container in the CronJob has the --service-account-key-file argument set as appropriate.
Usage
Run the control in your terminal:
powerpipe control run kubernetes_compliance.control.cronjob_container_argument_service_account_key_file_appropriate
Snapshot and share results via Turbot Pipes:
powerpipe loginpowerpipe control run kubernetes_compliance.control.cronjob_container_argument_service_account_key_file_appropriate --share
SQL
This control uses a named query:
with container_list as ( select c ->> 'name' as container_name, trim('"' from split_part(co::text, '=', 2)) as value, j.name as cronjob from kubernetes_cronjob as j, jsonb_array_elements(job_template -> 'spec' -> 'template' -> 'spec' -> 'containers') as c, jsonb_array_elements_text(c -> 'command') as co where co like '%--service-account-key-file=%'), container_name_with_cronjob_name as ( select j.name as cronjob_name, j.uid as cronjob_uid, j.path as path, j.start_line as start_line, j.end_line as end_line, j.context_name as context_name, j.namespace as namespace, j.source_type as source_type, j.tags as tags, j._ctx as _ctx, c.* from kubernetes_cronjob as j, jsonb_array_elements(job_template -> 'spec' -> 'template' -> 'spec' -> 'containers') as c)select coalesce(j.cronjob_uid, concat(j.path, ':', j.start_line)) as resource, case when (j.value -> 'command') is null or not ((j.value -> 'command') @> '["kube-apiserver"]') then 'ok' when l.container_name is not null and (j.value -> 'command') @> '["kube-apiserver"]' and ((l.value) like '%.pem') then 'ok' else 'alarm' end as status, case when (j.value -> 'command') is null then j.value ->> 'name' || ' command not defined.' when not ((j.value -> 'command') @> '["kube-apiserver"]') then j.value ->> 'name' || ' kube-apiserver not defined.' when l.container_name is not null and (j.value -> 'command') @> '["kube-apiserver"]' and ((l.value) like '%.pem') then j.value ->> 'name' || ' service-account-key-file set appropriate.' else j.value ->> 'name' || ' service-account-key-file set inappropriate.' end as reason, j.cronjob_name as cronjob_name , coalesce(context_name, '') as context_name, namespace, source_type, coalesce(path || ':' || start_line || '-' || end_line, '') as pathfrom container_name_with_cronjob_name as j left join container_list as l on j.value ->> 'name' = l.container_name and j.cronjob_name = l.cronjob;