Skip to content

feat: validate JWT token and use projected token #5871

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

Merged
merged 18 commits into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from 17 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
1 change: 1 addition & 0 deletions internal/infrastructure/common/proxy_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func BuildProxyArgs(
if bootstrapConfigOptions != nil && bootstrapConfigOptions.IPFamily == nil {
bootstrapConfigOptions.IPFamily = getIPFamily(infra)
}

bootstrapConfigOptions.GatewayNamespaceMode = gatewayNamespaceMode
bootstrapConfigurations, err := bootstrap.GetRenderedBootstrapConfig(bootstrapConfigOptions)
if err != nil {
Expand Down
33 changes: 30 additions & 3 deletions internal/infrastructure/kubernetes/proxy/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func expectedProxyContainers(infra *ir.ProxyInfra,
Resources: *containerSpec.Resources,
SecurityContext: expectedEnvoySecurityContext(containerSpec),
Ports: ports,
VolumeMounts: expectedContainerVolumeMounts(containerSpec),
VolumeMounts: expectedContainerVolumeMounts(containerSpec, gatewayNamespaceMode),
TerminationMessagePolicy: corev1.TerminationMessageReadFile,
TerminationMessagePath: "/dev/termination-log",
StartupProbe: &corev1.Probe{
Expand Down Expand Up @@ -288,7 +288,7 @@ func expectedShutdownPreStopCommand(cfg *egv1a1.ShutdownConfig) []string {
}

// expectedContainerVolumeMounts returns expected proxy container volume mounts.
func expectedContainerVolumeMounts(containerSpec *egv1a1.KubernetesContainerSpec) []corev1.VolumeMount {
func expectedContainerVolumeMounts(containerSpec *egv1a1.KubernetesContainerSpec, gatewayNamespaceMode bool) []corev1.VolumeMount {
volumeMounts := []corev1.VolumeMount{
{
Name: "certs",
Expand All @@ -300,11 +300,19 @@ func expectedContainerVolumeMounts(containerSpec *egv1a1.KubernetesContainerSpec
MountPath: "/sds",
},
}
if gatewayNamespaceMode {
volumeMounts = append(volumeMounts, corev1.VolumeMount{
Name: "sa-token",
MountPath: "/var/run/secrets/token",
ReadOnly: true,
})
}

return resource.ExpectedContainerVolumeMounts(containerSpec, volumeMounts)
}

// expectedVolumes returns expected proxy deployment volumes.
func expectedVolumes(name string, gatewayNamespacedMode bool, pod *egv1a1.KubernetesPodSpec) []corev1.Volume {
func expectedVolumes(name string, gatewayNamespacedMode bool, pod *egv1a1.KubernetesPodSpec, dnsDomain string) []corev1.Volume {
var volumes []corev1.Volume
certsVolume := corev1.Volume{
Name: "certs",
Expand Down Expand Up @@ -335,6 +343,25 @@ func expectedVolumes(name string, gatewayNamespacedMode bool, pod *egv1a1.Kubern
},
},
}
saAudience := fmt.Sprintf("%s.%s.svc.%s", config.EnvoyGatewayServiceName, config.DefaultNamespace, dnsDomain)
saTokenProjectedVolume := corev1.Volume{
Name: "sa-token",
VolumeSource: corev1.VolumeSource{
Projected: &corev1.ProjectedVolumeSource{
Sources: []corev1.VolumeProjection{
{
ServiceAccountToken: &corev1.ServiceAccountTokenProjection{
Path: "sa-token",
Audience: saAudience,
ExpirationSeconds: ptr.To[int64](3600),
},
},
},
DefaultMode: ptr.To[int32](420),
},
},
}
volumes = append(volumes, saTokenProjectedVolume)
}

volumes = append(volumes, certsVolume)
Expand Down
10 changes: 2 additions & 8 deletions internal/infrastructure/kubernetes/proxy/resource_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,14 @@ func (r *ResourceRender) Deployment() (*appsv1.Deployment, error) {
Containers: containers,
InitContainers: deploymentConfig.InitContainers,
ServiceAccountName: r.Name(),
AutomountServiceAccountToken: expectedAutoMountServiceAccountToken(r.GatewayNamespaceMode),
TerminationGracePeriodSeconds: expectedTerminationGracePeriodSeconds(proxyConfig.Spec.Shutdown),
DNSPolicy: corev1.DNSClusterFirst,
RestartPolicy: corev1.RestartPolicyAlways,
SchedulerName: "default-scheduler",
SecurityContext: deploymentConfig.Pod.SecurityContext,
Affinity: deploymentConfig.Pod.Affinity,
Tolerations: deploymentConfig.Pod.Tolerations,
Volumes: expectedVolumes(r.infra.Name, r.GatewayNamespaceMode, deploymentConfig.Pod),
Volumes: expectedVolumes(r.infra.Name, r.GatewayNamespaceMode, deploymentConfig.Pod, r.DNSDomain),
ImagePullSecrets: deploymentConfig.Pod.ImagePullSecrets,
NodeSelector: deploymentConfig.Pod.NodeSelector,
TopologySpreadConstraints: deploymentConfig.Pod.TopologySpreadConstraints,
Expand Down Expand Up @@ -537,10 +536,6 @@ func expectedTerminationGracePeriodSeconds(cfg *egv1a1.ShutdownConfig) *int64 {
return ptr.To(int64(s))
}

func expectedAutoMountServiceAccountToken(gatewayNamespacedMode bool) *bool {
return ptr.To(gatewayNamespacedMode)
}

func (r *ResourceRender) getPodSpec(
containers, initContainers []corev1.Container,
pod *egv1a1.KubernetesPodSpec,
Expand All @@ -550,15 +545,14 @@ func (r *ResourceRender) getPodSpec(
Containers: containers,
InitContainers: initContainers,
ServiceAccountName: ExpectedResourceHashedName(r.infra.Name),
AutomountServiceAccountToken: expectedAutoMountServiceAccountToken(r.GatewayNamespaceMode),
TerminationGracePeriodSeconds: expectedTerminationGracePeriodSeconds(proxyConfig.Spec.Shutdown),
DNSPolicy: corev1.DNSClusterFirst,
RestartPolicy: corev1.RestartPolicyAlways,
SchedulerName: "default-scheduler",
SecurityContext: pod.SecurityContext,
Affinity: pod.Affinity,
Tolerations: pod.Tolerations,
Volumes: expectedVolumes(r.infra.Name, r.GatewayNamespaceMode, pod),
Volumes: expectedVolumes(r.infra.Name, r.GatewayNamespaceMode, pod, r.DNSDomain),
ImagePullSecrets: pod.ImagePullSecrets,
NodeSelector: pod.NodeSelector,
TopologySpreadConstraints: pod.TopologySpreadConstraints,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,6 @@ func TestServiceAccount(t *testing.T) {
ns = tc.infra.GetProxyInfra().Namespace
}
r := NewResourceRender(ns, cfg.DNSDomain, tc.infra.GetProxyInfra(), cfg.EnvoyGateway)

sa, err := r.ServiceAccount()
require.NoError(t, err)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ spec:
label1: value1-override
label2: value2
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: true
containers:
- args:
- --service-cluster default
Expand Down Expand Up @@ -243,7 +242,7 @@ spec:
- name: jwt-sa-bearer
generic_secret:
secret:
filename: "/var/run/secrets/kubernetes.io/serviceaccount/token"
filename: "/var/run/secrets/token/sa-token"
overload_manager:
refresh_interval: 0.25s
resource_monitors:
Expand Down Expand Up @@ -336,6 +335,9 @@ spec:
readOnly: true
- mountPath: /sds
name: sds
- mountPath: /var/run/secrets/token
name: sa-token
readOnly: true
- args:
- envoy
- shutdown-manager
Expand Down Expand Up @@ -414,6 +416,14 @@ spec:
serviceAccountName: envoy-default-37a8eec1
terminationGracePeriodSeconds: 360
volumes:
- name: sa-token
projected:
defaultMode: 420
sources:
- serviceAccountToken:
audience: envoy-gateway.envoy-gateway-system.svc.cluster.local
expirationSeconds: 3600
path: sa-token
- configMap:
defaultMode: 420
items:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ spec:
label1: value1-override
label2: value2
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ spec:
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
spec:
automountServiceAccountToken: false
containers:
- args:
- --service-cluster default
Expand Down
Loading
Loading