Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SCALRCORE-32580 Agent > Restrict access to instance metadata #64

Merged
merged 3 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions charts/agent-k8s/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [UNRELEASED]

### Added

- Added `restrictMetadataService` option. When set to true, applies pod network policy that blocks outbound access to instance metadata service.

## [v0.5.19]

### Updated
Expand Down
20 changes: 20 additions & 0 deletions charts/agent-k8s/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,25 @@ The EFS storage will be mounted in all worker containers at the `agent.data_home
for Runs will inherit the EFS configuration. The controller will continue to use an ephemeral directory
as its data home.

## Restrict Access to VM Metadata Service

The chart includes an optional feature to restrict the pods from accessing the VM metadata service at 169.254.169.254, which is common for both AWS and GCP environments.

To enable it, use the `restrictMetadataService` option:

```console
$ helm upgrade ... \
--set restrictMetadataService=true
```

With this option enabled, a Kubernetes NetworkPolicy is applied to the agent pods that denies egress traffic to 169.254.169.254/32, blocking access to the VM metadata service. All other outbound traffic is allowed.

### Limitations

Ensure that your cluster is using a CNI plugin that supports egress NetworkPolicies. Example: Calico, Cilium, or native GKE NetworkPolicy provider for supported versions.

If your cluster doesn't currently support egress NetworkPolicies, you may need to recreate it with the appropriate settings.

## Maintainers

| Name | Email | Url |
Expand Down Expand Up @@ -155,6 +174,7 @@ as its data home.
| resources.limits.memory | string | `"1024Mi"` | |
| resources.requests.cpu | string | `"250m"` | |
| resources.requests.memory | string | `"256Mi"` | |
| restrictMetadataService | bool | `false` | Apply NetworkPolicy to an agent pod that denies access to VM metadata service address (169.254.169.254) |
| securityContext | object | `{"runAsGroup":0,"runAsUser":0}` | The Agent Pods security context. |
| serviceAccount.annotations | object | `{}` | Annotations to add to the service account |
| serviceAccount.create | bool | `true` | Specifies whether a service account should be created |
Expand Down
19 changes: 19 additions & 0 deletions charts/agent-k8s/README.md.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,25 @@ The EFS storage will be mounted in all worker containers at the `agent.data_home
for Runs will inherit the EFS configuration. The controller will continue to use an ephemeral directory
as its data home.

## Restrict Access to VM Metadata Service

The chart includes an optional feature to restrict the pods from accessing the VM metadata service at 169.254.169.254, that is common for both AWS and GCP environments.

To enable it, use the `restrictMetadataService` option:

```console
$ helm upgrade ... \
--set restrictMetadataService=true
```

With this option enabled, a Kubernetes NetworkPolicy is applied to the agent pods that denies egress traffic to 169.254.169.254/32, blocking access to the VM metadata service. All other outbound traffic is allowed.

### Limitations

Ensure that your cluster is using a CNI plugin that supports egress NetworkPolicies. Example: Calico, Cilium, or native GKE NetworkPolicy provider for supported versions.

If your cluster doesn't currently support egress NetworkPolicies, you may need to recreate it with the appropriate settings.

{{ template "chart.maintainersSection" . }}

{{ template "chart.requirementsSection" . }}
Expand Down
21 changes: 21 additions & 0 deletions charts/agent-k8s/templates/networkpolicy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{- if .Values.restrictMetadataService }}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: agent-k8s-network-policy
namespace: {{ .Release.Namespace | quote }}
spec:
podSelector:
matchLabels:
{{- include "agent-k8s.selectorLabels" . | nindent 6 }}
policyTypes:
- Egress
egress:
- to:
- ipBlock:
# Allow all egress traffic by default
cidr: 0.0.0.0/0
except:
# Deny access to IMDS
- 169.254.169.254/32
{{- end }}
3 changes: 3 additions & 0 deletions charts/agent-k8s/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ securityContext:
# -- The Agent Pods annotations.
podAnnotations: {}

# -- Apply NetworkPolicy to an agent pod that denies access to VM metadata service address (169.254.169.254)
restrictMetadataService: false

# -- Provides the amount of grace time prior to the agent-k8s container being forcibly terminated when marked for deletion or restarted.
terminationGracePeriodSeconds: 3660

Expand Down