Skip to content

add: container securityContext not available in podSecurityContext - #24588

Closed
ChrisFraun wants to merge 3070 commits into
apache:mainfrom
ChrisFraun:feat/adding-container-security-context
Closed

add: container securityContext not available in podSecurityContext#24588
ChrisFraun wants to merge 3070 commits into
apache:mainfrom
ChrisFraun:feat/adding-container-security-context

Conversation

@ChrisFraun

@ChrisFraun ChrisFraun commented Jun 21, 2022

Copy link
Copy Markdown
Contributor

This PR is only a small change in the helm chart of Airflow.

What: Deployments can have security settings in their manifest on two levels: pod and container. However, there are some capabilities only configurable in one of the respective levels(https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.23/#securitycontext-v1-core). This PR sets a default configuration for container securityContext, which denies privilege escalation and drops all POSIX capabilities. These are and should be standard settings in the context of Kubernetes. It also adds the possibility of running Airflow in an Kubernetes environment without PSP (to be removed in v1.25 https://kubernetes.io/docs/concepts/security/pod-security-policy/), but with OpenPolicyAgent (a or possibly the PSP substitute) with the same capabilities as a restricted PSP instead.

Why: This missing configuration restricts Airflow from being used with the simple upstream helm chart without modifications/unnecessary maintenance. This especially applies to the restricted policy use in OPA. The specific setting in this PR is not inherited from podSecurityContext(pod level) in securityContext(container level).

Problem: There is already a securityContext in the values.yaml, however, this should also be be called podSecurityContext since it's on pod level, but it isn't. To not break backwards compatibility of Airflow, this PR hardcodes the respective capabilities on container level for statsd, scheduler and webserver.

The other possibility would be to introduce a containerSecurityContext in the values.yaml, which is a made up word since it is commonly called scurityContext.

Benefit in either case would be a more secure deployment.

In case of existing issue, reference it using one of the following:

closes: #27612

Could not find any related issue at first sight.


^ Add meaningful description above

Read the Pull Request Guidelines for more information.

Test was a simple helm lint . on chart level as well as a successful deployment.
WARNING: Kubernetes configuration file is group-readable. This is insecure. Location: /Users/christophfraundorfer/.kube/config
WARNING: Kubernetes configuration file is world-readable. This is insecure. Location: /Users/christophfraundorfer/.kube/config
==> Linting .

1 chart(s) linted, 0 chart(s) failed

@boring-cyborg boring-cyborg Bot added the area:helm-chart Airflow Helm Chart label Jun 21, 2022
@boring-cyborg

boring-cyborg Bot commented Jun 21, 2022

Copy link
Copy Markdown

Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contribution Guide (https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst)
Here are some useful points:

  • Pay attention to the quality of your code (flake8, mypy and type annotations). Our pre-commits will help you with that.
  • In case of a new feature add useful documentation (in docstrings or in docs/ directory). Adding a new operator? Check this short guide Consider adding an example DAG that shows how users should use it.
  • Consider using Breeze environment for testing locally, it’s a heavy docker but it ships with a working Airflow and a lot of integrations.
  • Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
  • Please follow ASF Code of Conduct for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
  • Be sure to read the Airflow Coding style.
    Apache Airflow is a community-driven project and together we are making it better 🚀.
    In case of doubts contact the developers at:
    Mailing List: dev@airflow.apache.org
    Slack: https://s.apache.org/airflow-slack

@potiuk

potiuk commented Jun 29, 2022

Copy link
Copy Markdown
Member

I'd say that better action would really be to have two separate contexts and maybe simply - rather than adding "containerSecurityContext", introduce values/securityContexts/pod and values/securityContexts/container as a bit more detailed structure in values, allowing more fine-grained security context configuration (while keeping deprecated default)

Something like:

Screenshot 2022-06-29 at 07 28 03

I think that would be much more versatile and rather easy to use configure (more detailed values could be merged into the defaults).

@ChrisFraun

Copy link
Copy Markdown
Contributor Author

Updated the change, can you check it?

@ChrisFraun

Copy link
Copy Markdown
Contributor Author

is the change how you imagined it @potiuk ?

@potiuk

potiuk commented Jul 11, 2022

Copy link
Copy Markdown
Member

Yep it looks good :). I am not sure if there is an easy way to "warn" if you use the deprecated configuration (I think this is the only change that I would like to see here) - and of course some tests would be useful. @jedcunningham @dstandish - WDYT?

@jedcunningham

Copy link
Copy Markdown
Member

Deprecation warnings happen in NOTES, e.g:

{{- if .Values.flower.extraNetworkPolicies }}
DEPRECATION WARNING:
`flower.extraNetworkPolicies` has been renamed to `flower.networkPolicy.peers`.
Please change your values as support for the old name will be dropped in a future release.
{{- end }}

This should be expanded to cover all of the components as well.

I'd also like to see the component specific override come from the components config section, e.g. scheduler.securityContexts.pod instead of securityContexts.pod.scheduler.

@ChrisFraun

ChrisFraun commented Jul 19, 2022

Copy link
Copy Markdown
Contributor Author

Hi, sorry for the late reply!
what was added:

  • added deprecation notes
  • Modified values.yaml (webserver, scheduler, statsd)
  • Modified deployments (webserver, scheduler, statsd)
  • Modified values.schema (webserver, scheduler, statsd)

Tested with helm lint . and helm template . --debug - both compiled.
Is this the way you imagine it? @potiuk @jedcunningham

@potiuk

potiuk commented Jul 21, 2022

Copy link
Copy Markdown
Member

still some static checks are failing,

@ChrisFraun

Copy link
Copy Markdown
Contributor Author

Hi @potiuk can you maybe point me into a direction on why the checks are not running through? - I don't see the problem so far.

@jedcunningham jedcunningham left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably worth adding some test coverage to make sure the old deprecated options still work.

Comment thread chart/templates/NOTES.txt
Comment thread chart/values.schema.json Outdated
Comment thread chart/values.schema.json Outdated
Comment thread chart/values.schema.json Outdated
Comment thread chart/values.schema.json Outdated
Comment thread chart/values.schema.json
Comment thread chart/values.yaml Outdated
Comment on lines +48 to +51
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the default, yeah? Not sure we need to expand it here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed it from here and put it in the helper:
{{/*
Set the default value for container securityContext
If no value is passed for securityContext.container or .securityContext.container, defaults to deny privileges escallation and dropping all POSIX capabilities.

+------------------------+      +-----------------+      +-------------------------+
| <node>.securityContext.container |  ->  | securityContext.container |  ->  | allowPrivilegesEscalation: false, capabilities.drop: [ALL]|
+------------------------+      +-----------------+      +-------------------------+

The template can be called like so:
include "airflowSecurityContextContainer" (list . .Values.webserver)

Where . is the global variables scope and .Values.webserver the local variables scope for the webserver template.
*/}}
{{- define "airflowSecurityContextContainer" -}}
{{- $ := index . 0 -}}
{{- with index . 1 }}
{{- if .securityContext.container -}}
{{ toYaml .securityContext.container | print }}
{{- else if $.Values.securityContext.container -}}
{{ toYaml $.Values.securityContext.container | print }}
{{- else -}}
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
{{- end -}}
{{- end -}}
{{- end -}}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, should have been more clear. Isn't that the default kubernetes sets if you provide nothing? If so, I'd rather we not set it at all.

@ChrisFraun ChrisFraun Aug 12, 2022

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you have to set them yourself, they are not automatically set.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay, coming back to this now.

I had this backwards - this removes rights. While I agree this is a good sane default, we can't make this change in a minor release without breaking backward compatibility. Given that, I think for now we should keep the default as nothing.

Comment thread tests/charts/test_scheduler.py Outdated
Comment thread tests/charts/test_scheduler.py Outdated
@ChrisFraun

Copy link
Copy Markdown
Contributor Author

Hi @jedcunningham thanks a lot for the input! I tried to stick to your comments, but feel free to add some more :)

Comment thread chart/values.yaml Outdated
Comment thread chart/values.yaml Outdated
Comment thread chart/templates/_helpers.yaml Outdated
Set the default value for securityContext
If no value is passed for securityContext or <node>.securityContext, defaults to global uid and gid.
Set the default value for pod securityContext
If no value is passed for securityContext.pod or <node>.securityContext.pod, defaults to global uid and gid.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want this order of precedence:

<node>.securityContexts.pod
<node>.securityContext
securityContexts.pod
securityContext (backcompat for deprecated config)
uid/gid

So I think this template still needs a little work, right? Also feel free to rename this airflowPodSecurityContext to make it more clear.

@ChrisFraun ChrisFraun Aug 12, 2022

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I tried to adapt to your suggestion and renamed also the template.
Btw airflowSecurityContextIds was not used anywhere so the renamed version airflowPodSecurityContextsIds can probably be deleted.

Hope I got it right this time.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I think airflowSecurityContextIds is used on workers-deployment to set the uid and gid during a chown. Not sure if has changed.

Comment thread chart/values.schema.json Outdated
@ChrisFraun

Copy link
Copy Markdown
Contributor Author

Hi, I am still getting this error during the images build: ERROR: denied: permission_denied: write_package
It does not seem to me that this has something to do with my changes.
Any advice?

@potiuk

potiuk commented Sep 7, 2022

Copy link
Copy Markdown
Member

Hi, I am still getting this error during the images build: ERROR: denied: permission_denied: write_package It does not seem to me that this has something to do with my changes. Any advice?

Rebase please. GitHub seemed to have a rough day.

Comment thread chart/templates/_helpers.yaml Outdated
Comment thread chart/templates/_helpers.yaml Outdated
Comment thread chart/templates/_helpers.yaml Outdated
Comment thread chart/templates/_helpers.yaml Outdated

Where `.` is the global variables scope and `.Values.webserver` the local variables scope for the webserver template.
*/}}
{{- define "airflowContainerSecurityContext" -}}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should call this containerSecurityContext instead, since it's used for more than just Airflow containers?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, this isn't even used? Let's drop it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added it to the charts

@potiuk

potiuk commented Sep 19, 2022

Copy link
Copy Markdown
Member

Conflict to solve :(

@ChrisFraun

ChrisFraun commented Sep 20, 2022

Copy link
Copy Markdown
Contributor Author

Tried to add the changes demanded in #25985

@jedcunningham jedcunningham left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect there are more of this scenario than just the 2 below:

Comment thread chart/templates/scheduler/scheduler-deployment.yaml Outdated
Comment thread chart/templates/workers/worker-deployment.yaml Outdated
@anja-istenic

Copy link
Copy Markdown
Contributor

Thanks @ChrisFraun for reconciling our 2 PRs into one (#25985 and this one). These changes look good to me 👍

Comment thread chart/values.schema.json
@eladkal eladkal added this to the Airflow Helm Chart 1.8.0 milestone Oct 24, 2022
@potiuk

potiuk commented Oct 25, 2022

Copy link
Copy Markdown
Member

Running the tests now (sorry @ChrisFraun for this all taking so long - just returned from Holidays and trying to catch-up - let's see how this one looks like for tests and I will give it another pass).

@potiuk

potiuk commented Oct 31, 2022

Copy link
Copy Markdown
Member

errors ?

@malthe
malthe self-requested a review November 3, 2022 14:46

@malthe malthe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tested this out and it works beautifully!

hussein-awala and others added 23 commits April 20, 2023 09:46
…apache#29913)

* Add a failing test to make it pass

* use partial_kwargs when they are provide and override only None values by dag default values

* update the test and check if the values are filled in the right order

* fix overriding retry_delay with default value when it is equal to 0

* add missing default value for inlets and outlets

* set partial_kwargs dict type to dict[str, Any] and remove type ignore comments

* create a dict for default values and use NotSet instead of None to support None as accepted value

* update partial typing by removing None type from some args and set NotSet for all args

* Tweak kwarg merging slightly

This should improve iteration a bit, I think.

* Fix unit tests

---------

Co-authored-by: Tzu-ping Chung <uranusjr@gmail.com>
This PR consistent mostly of code that was created in OpenLineage project. It 
consists of

- Provider wiring
- OpenLineageListener that uses Listener API to get notification about changes
  to TaskInstance and Dag states
- Extractor framework, which is used to extract lineage information from
  particular operators. It's ment to be replaced by direct implementation of
  lineage features in later phase and extracting them using DefaultExtractor.
  This PR does not include actual extractors, but code around using and registering them.
- OpenLineageAdapter that translates extracted information to OpenLineage events.
- Utils around specific Airflow OL facets and features

This is a base implementation that's not ment to be released yet, but to add
code modified to be consistent with Airflow standards, get early feedback and
provide canvas to add later features, docs, tests on.

Signed-off-by: Jakub Dardzinski <kuba0221@gmail.com>
…pache#29809)

* Adding configuration to control retry parameters for k8s api client

* Handling review comments

* Fixing code bug

* Fixing failing tests

* Temporary commit with UT wip

* Fixing unit test

* Fixing the strict checks

* Handling review comments from Hussein

* Revert "Handling review comments from Hussein"

This reverts commit fa3bc26.

* Fixing failing ut

* Reverting bad hack

* Updating logic in kube_client.py

Co-authored-by: Hussein Awala <hussein@awala.fr>

* Fixing unit tests

* Fixing unit tests

* Handling review comments from Ash

* Fix loading mock call args for python3.7

* Apply suggestions from code review

* fix static check

* add in 2.6.0

---------

Co-authored-by: Amogh <adesai@cloudera.com>
Co-authored-by: Hussein Awala <houssein.awala.96@gmail.com>
* fix(chart): webserver probes timeout and period

* Update default values in JSON schema to reflect values.yaml

* remove defautl templated values
…ype` (apache#30595)

* Add  while initilizing

* Add  while initilizing

* Add logic to pick either client_type or resource_type

* Add test case

* Assert expected path
* update run clear+mark, update task clear

* add mark as tasks and include list of affected tasks

* Add support for mapped tasks, add shared modal component

* Clean up styling, restore warning for past/future tg clear

---------

Co-authored-by: Hussein Awala <hussein@awala.fr>
Co-authored-by: Hussein Awala <houssein.awala.96@gmail.com>
…apache#29065)

* When clearing task instances try to get associated DAGs from database.

This fixes problems when recursively clearing task instances across multiple DAGs:
  * Task instances in downstream DAGs weren't having their `max_tries` property incremented, which could cause downstream external task sensors in reschedule mode to instantly time out (issue apache#29049).
  * Task instances in downstream DAGs could have some of their properties overridden by an unrelated task in the upstream DAG if they had the same task ID.

* Use session fixture for new `test_clear_task_instances_without_dag_param` test.

* Use session fixture for new `test_clear_task_instances_in_multiple_dags` test.

---------

Co-authored-by: eladkal <45845474+eladkal@users.noreply.github.com>

---------

Co-authored-by: Tzu-ping Chung <uranusjr@gmail.com>
Co-authored-by: Ephraim Anierobi <splendidzigy24@gmail.com>

---------

Co-authored-by: Josh Fell <48934154+josh-fell@users.noreply.github.com>
Co-authored-by: Mikaël Ducharme <mikaelducharme@gmail.com>
Co-authored-by: Mikaël Ducharme <mikaelducharme@gmail.com>
Co-authored-by: Mikaël Ducharme <mikaelducharme@gmail.com>
…hrisFraun/airflow into feat/adding-container-security-context
@ChrisFraun

ChrisFraun commented Apr 20, 2023

Copy link
Copy Markdown
Contributor Author

I messed up :(

I was trying to resolve the conflicts locally but did not realise that vscode is adding all the changes in the remote branch...
I am not sure what to do exactly from here, maybe it is even smarter to create a new PR and add the changes to the most current version?

@mikaeld

mikaeld commented Apr 20, 2023

Copy link
Copy Markdown
Contributor

You likely can revert and/or rewrite history to fix this since this is on your fork. If you can't fix the conflicts, we would need to link this PR in the new one to keep the context. @potiuk thoughts on this?

@potiuk

potiuk commented Apr 22, 2023

Copy link
Copy Markdown
Member

Create a new PR and make your changes there again. It happens, and has the opportunity to re-review your own code while applying.

Closing it for now.

@potiuk potiuk closed this Apr 22, 2023
@mikaeld

mikaeld commented Apr 25, 2023

Copy link
Copy Markdown
Contributor

@ChrisFraun did you plan on re-creating a PR? I don't mind doing it if you did not plan on re-creating it.

@ChrisFraun

Copy link
Copy Markdown
Contributor Author

Feel free to go ahead @mikaeld! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:helm-chart Airflow Helm Chart

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Chart] Container security context is not defined