Skip to content

Commit

Permalink
set bind permissions on the executable in dockerfile (grafana#6817)
Browse files Browse the repository at this point in the history
* set bind capacity in docker file

* add test for nonroot

* newline
  • Loading branch information
captncraig committed Apr 3, 2024
1 parent 9646f5c commit 7393c83
Show file tree
Hide file tree
Showing 7 changed files with 285 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cmd/grafana-agent/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ LABEL org.opencontainers.image.source="https://github.com/grafana/agent"
# Install dependencies needed at runtime.
RUN <<EOF
apt-get update
apt-get install -qy libsystemd-dev tzdata ca-certificates
apt-get install -qy libsystemd-dev tzdata ca-certificates libcap2-bin
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
EOF

Expand All @@ -53,6 +53,7 @@ RUN groupadd --gid $UID $USERNAME
RUN useradd -m -u $UID -g $UID $USERNAME
RUN chown -R $USERNAME:$USERNAME /etc/agent
RUN chown -R $USERNAME:$USERNAME /bin/grafana-agent
RUN setcap 'cap_net_bind_service=+ep' /bin/grafana-agent

ENTRYPOINT ["/bin/grafana-agent"]
ENV AGENT_DEPLOY_MODE=docker
Expand Down
7 changes: 7 additions & 0 deletions operations/helm/charts/grafana-agent/ci/nonroot-values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
global:
podSecurityContext:
fsGroup: 473
agent:
securityContext:
runAsUser: 473
runAsGroup: 473
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
# Source: grafana-agent/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: grafana-agent
labels:
helm.sh/chart: grafana-agent
app.kubernetes.io/name: grafana-agent
app.kubernetes.io/instance: grafana-agent
app.kubernetes.io/version: "vX.Y.Z"
app.kubernetes.io/managed-by: Helm
data:
config.river: |-
logging {
level = "info"
format = "logfmt"
}
discovery.kubernetes "pods" {
role = "pod"
}
discovery.kubernetes "nodes" {
role = "node"
}
discovery.kubernetes "services" {
role = "service"
}
discovery.kubernetes "endpoints" {
role = "endpoints"
}
discovery.kubernetes "endpointslices" {
role = "endpointslice"
}
discovery.kubernetes "ingresses" {
role = "ingress"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
# Source: grafana-agent/templates/controllers/daemonset.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: grafana-agent
labels:
helm.sh/chart: grafana-agent
app.kubernetes.io/name: grafana-agent
app.kubernetes.io/instance: grafana-agent
app.kubernetes.io/version: "vX.Y.Z"
app.kubernetes.io/managed-by: Helm
spec:
minReadySeconds: 10
selector:
matchLabels:
app.kubernetes.io/name: grafana-agent
app.kubernetes.io/instance: grafana-agent
template:
metadata:
annotations:
kubectl.kubernetes.io/default-container: grafana-agent
labels:
app.kubernetes.io/name: grafana-agent
app.kubernetes.io/instance: grafana-agent
spec:
securityContext:
fsGroup: 473
serviceAccountName: grafana-agent
containers:
- name: grafana-agent
image: docker.io/grafana/agent:v0.40.3
imagePullPolicy: IfNotPresent
args:
- run
- /etc/agent/config.river
- --storage.path=/tmp/agent
- --server.http.listen-addr=0.0.0.0:80
- --server.http.ui-path-prefix=/
env:
- name: AGENT_MODE
value: flow
- name: AGENT_DEPLOY_MODE
value: "helm"
- name: HOSTNAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
ports:
- containerPort: 80
name: http-metrics
readinessProbe:
httpGet:
path: /-/ready
port: 80
scheme: HTTP
initialDelaySeconds: 10
timeoutSeconds: 1
securityContext:
runAsGroup: 473
runAsUser: 473
volumeMounts:
- name: config
mountPath: /etc/agent
- name: config-reloader
image: ghcr.io/jimmidyson/configmap-reload:v0.12.0
args:
- --volume-dir=/etc/agent
- --webhook-url=http://localhost:80/-/reload
volumeMounts:
- name: config
mountPath: /etc/agent
resources:
requests:
cpu: 1m
memory: 5Mi
dnsPolicy: ClusterFirst
volumes:
- name: config
configMap:
name: grafana-agent
117 changes: 117 additions & 0 deletions operations/helm/tests/nonroot/grafana-agent/templates/rbac.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
---
# Source: grafana-agent/templates/rbac.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: grafana-agent
labels:
helm.sh/chart: grafana-agent
app.kubernetes.io/name: grafana-agent
app.kubernetes.io/instance: grafana-agent
app.kubernetes.io/version: "vX.Y.Z"
app.kubernetes.io/managed-by: Helm
rules:
# Rules which allow discovery.kubernetes to function.
- apiGroups:
- ""
- "discovery.k8s.io"
- "networking.k8s.io"
resources:
- endpoints
- endpointslices
- ingresses
- nodes
- nodes/proxy
- nodes/metrics
- pods
- services
verbs:
- get
- list
- watch
# Rules which allow loki.source.kubernetes and loki.source.podlogs to work.
- apiGroups:
- ""
resources:
- pods
- pods/log
- namespaces
verbs:
- get
- list
- watch
- apiGroups:
- "monitoring.grafana.com"
resources:
- podlogs
verbs:
- get
- list
- watch
# Rules which allow mimir.rules.kubernetes to work.
- apiGroups: ["monitoring.coreos.com"]
resources:
- prometheusrules
verbs:
- get
- list
- watch
- nonResourceURLs:
- /metrics
verbs:
- get
# Rules for prometheus.kubernetes.*
- apiGroups: ["monitoring.coreos.com"]
resources:
- podmonitors
- servicemonitors
- probes
verbs:
- get
- list
- watch
# Rules which allow eventhandler to work.
- apiGroups:
- ""
resources:
- events
verbs:
- get
- list
- watch
# needed for remote.kubernetes.*
- apiGroups: [""]
resources:
- "configmaps"
- "secrets"
verbs:
- get
- list
- watch
# needed for otelcol.processor.k8sattributes
- apiGroups: ["apps"]
resources: ["replicasets"]
verbs: ["get", "list", "watch"]
- apiGroups: ["extensions"]
resources: ["replicasets"]
verbs: ["get", "list", "watch"]
---
# Source: grafana-agent/templates/rbac.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: grafana-agent
labels:
helm.sh/chart: grafana-agent
app.kubernetes.io/name: grafana-agent
app.kubernetes.io/instance: grafana-agent
app.kubernetes.io/version: "vX.Y.Z"
app.kubernetes.io/managed-by: Helm
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: grafana-agent
subjects:
- kind: ServiceAccount
name: grafana-agent
namespace: default
23 changes: 23 additions & 0 deletions operations/helm/tests/nonroot/grafana-agent/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
# Source: grafana-agent/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: grafana-agent
labels:
helm.sh/chart: grafana-agent
app.kubernetes.io/name: grafana-agent
app.kubernetes.io/instance: grafana-agent
app.kubernetes.io/version: "vX.Y.Z"
app.kubernetes.io/managed-by: Helm
spec:
type: ClusterIP
selector:
app.kubernetes.io/name: grafana-agent
app.kubernetes.io/instance: grafana-agent
internalTrafficPolicy: Cluster
ports:
- name: http-metrics
port: 80
targetPort: 80
protocol: "TCP"
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
# Source: grafana-agent/templates/serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: grafana-agent
namespace: default
labels:
helm.sh/chart: grafana-agent
app.kubernetes.io/name: grafana-agent
app.kubernetes.io/instance: grafana-agent
app.kubernetes.io/version: "vX.Y.Z"
app.kubernetes.io/managed-by: Helm

0 comments on commit 7393c83

Please sign in to comment.