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

fail if service name != service account name #281

Merged
merged 1 commit into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions connect-inject/container_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ func (h *Handler) containerInit(pod *corev1.Pod, k8sNamespace string) (corev1.Co
panic("No service found. This should be impossible since we default it.")
}

// When ACLs are enabled, the ACL token returned from `consul login` is only
// valid for a service with the same name as the ServiceAccountName.
if data.AuthMethod != "" && data.ServiceName != pod.Spec.ServiceAccountName {
return corev1.Container{}, fmt.Errorf("serviceAccountName %q does not match service name %q", pod.Spec.ServiceAccountName, data.ServiceName)
}

// If a port is specified, then we determine the value of that port
// and register that port for the host service.
if raw, ok := pod.Annotations[annotationPort]; ok && raw != "" {
Expand Down
51 changes: 51 additions & 0 deletions connect-inject/container_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ func TestHandlerContainerInit_namespacesEnabled(t *testing.T) {
},
},
},
ServiceAccountName: "web",
},
}
}
Expand Down Expand Up @@ -1323,6 +1324,7 @@ func TestHandlerContainerInit_authMethod(t *testing.T) {
},
},
},
ServiceAccountName: "foo",
},
}
container, err := h.containerInit(pod, k8sNamespace)
Expand Down Expand Up @@ -1373,6 +1375,7 @@ func TestHandlerContainerInit_authMethodAndCentralConfig(t *testing.T) {
},
},
},
ServiceAccountName: "foo",
},
}
container, err := h.containerInit(pod, k8sNamespace)
Expand Down Expand Up @@ -1514,3 +1517,51 @@ func TestHandlerContainerInit_Resources(t *testing.T) {
},
}, container.Resources)
}

func TestHandlerContainerInit_MismatchedServiceNameServiceAccountNameWithACLsEnabled(t *testing.T) {
require := require.New(t)
h := Handler{
AuthMethod: "auth-method",
}
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
annotationService: "foo",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "serviceName",
},
},
ServiceAccountName: "notServiceName",
},
}

_, err := h.containerInit(pod, k8sNamespace)
require.EqualError(err, `serviceAccountName "notServiceName" does not match service name "foo"`)
}
lkysow marked this conversation as resolved.
Show resolved Hide resolved

func TestHandlerContainerInit_MismatchedServiceNameServiceAccountNameWithACLsDisabled(t *testing.T) {
require := require.New(t)
h := Handler{}
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
annotationService: "foo",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "serviceName",
},
},
ServiceAccountName: "notServiceName",
},
}

_, err := h.containerInit(pod, k8sNamespace)
require.NoError(err)
}