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

Injector: Change daprd projected token audience to sentry SPIFFE ID #7041

Merged
merged 5 commits into from
Jan 19, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 2 additions & 6 deletions cmd/injector/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,10 @@ func Run() {
log.Fatalf("Failed to get authentication uids from services accounts: %s", err)
}

namespace, err := security.CurrentNamespaceOrError()
if err != nil {
log.Fatalf("Failed to get current namespace: %s", err)
}

secProvider, err := security.New(ctx, security.Options{
SentryAddress: cfg.SentryAddress,
ControlPlaneTrustDomain: cfg.ControlPlaneTrustDomain,
ControlPlaneNamespace: namespace,
ControlPlaneNamespace: security.CurrentNamespace(),
TrustAnchorsFile: cfg.TrustAnchorsFile,
AppID: "dapr-injector",
MTLSEnabled: true,
Expand Down Expand Up @@ -134,6 +129,7 @@ func Run() {
})
return inj.Run(ctx,
sec.TLSServerConfigNoClientAuth(),
sentryID,
requester.RequestCertificateFromSentry,
sec.CurrentTrustAnchors,
)
Expand Down
1 change: 1 addition & 0 deletions pkg/injector/patcher/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type SidecarConfig struct {
ControlPlaneTrustDomain string
ActorsService string
RemindersService string
SentrySPIFFEID string
SidecarHTTPPort int32 `default:"3500"`
SidecarAPIGRPCPort int32 `default:"50001"`
SidecarInternalGRPCPort int32 `default:"50002"`
Expand Down
7 changes: 7 additions & 0 deletions pkg/injector/patcher/sidecar_patcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ func TestPatching(t *testing.T) {
c.Identity = "pod:identity"
c.CertChain = "certchain"
c.CertKey = "certkey"
c.SentrySPIFFEID = "spiffe://foo.bar/ns/example/dapr-sentry"

if tc.sidecarConfigModifierFn != nil {
tc.sidecarConfigModifierFn(c)
Expand Down Expand Up @@ -317,6 +318,9 @@ func TestPatching(t *testing.T) {
tokenVolume := pod.Spec.Volumes[0]
assert.Equal(t, "dapr-identity-token", tokenVolume.Name)
assert.NotNil(t, tokenVolume.Projected)
require.Len(t, tokenVolume.Projected.Sources, 1)
require.NotNil(t, tokenVolume.Projected.Sources[0].ServiceAccountToken)
assert.Equal(t, "spiffe://foo.bar/ns/example/dapr-sentry", tokenVolume.Projected.Sources[0].ServiceAccountToken.Audience)

// Assertions on added labels
assert.Equal(t, "true", pod.Labels[injectorConsts.SidecarInjectedLabel])
Expand Down Expand Up @@ -351,6 +355,9 @@ func TestPatching(t *testing.T) {
tokenVolume := pod.Spec.Volumes[1]
assert.Equal(t, "dapr-identity-token", tokenVolume.Name)
assert.NotNil(t, tokenVolume.Projected)
require.Len(t, tokenVolume.Projected.Sources, 1)
require.NotNil(t, tokenVolume.Projected.Sources[0].ServiceAccountToken)
assert.Equal(t, "spiffe://foo.bar/ns/example/dapr-sentry", tokenVolume.Projected.Sources[0].ServiceAccountToken.Audience)

// Check the presence of the volume mount in the app container
appContainer := pod.Spec.Containers[0]
Expand Down
3 changes: 1 addition & 2 deletions pkg/injector/patcher/sidecar_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
corev1 "k8s.io/api/core/v1"

injectorConsts "github.com/dapr/dapr/pkg/injector/consts"
securityConsts "github.com/dapr/dapr/pkg/security/consts"
"github.com/dapr/kit/ptr"
)

Expand Down Expand Up @@ -78,7 +77,7 @@ func (c *SidecarConfig) getTokenVolume() corev1.Volume {
DefaultMode: ptr.Of(int32(420)),
Sources: []corev1.VolumeProjection{{
ServiceAccountToken: &corev1.ServiceAccountTokenProjection{
Audience: securityConsts.ServiceAccountTokenAudience,
Audience: c.SentrySPIFFEID,
ExpirationSeconds: ptr.Of(int64(7200)),
Path: "token",
},
Expand Down
7 changes: 5 additions & 2 deletions pkg/injector/service/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"strings"
"time"

"github.com/spiffe/go-spiffe/v2/spiffeid"
admissionv1 "k8s.io/api/admission/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -62,7 +63,7 @@

// Injector is the interface for the Dapr runtime sidecar injection component.
type Injector interface {
Run(context.Context, *tls.Config, signDaprdCertificateFn, currentTrustAnchorsFn) error
Run(context.Context, *tls.Config, spiffeid.ID, signDaprdCertificateFn, currentTrustAnchorsFn) error
Ready(context.Context) error
}

Expand All @@ -87,6 +88,7 @@
controlPlaneNamespace string
controlPlaneTrustDomain string
currentTrustAnchors currentTrustAnchorsFn
sentrySPIFFEID spiffeid.ID
signDaprdCertificate signDaprdCertificateFn

namespaceNameMatcher *namespacednamematcher.EqualPrefixNameNamespaceMatcher
Expand Down Expand Up @@ -213,7 +215,7 @@
return allowedUids, nil
}

func (i *injector) Run(ctx context.Context, tlsConfig *tls.Config, signDaprdFn signDaprdCertificateFn, currentTrustAnchors currentTrustAnchorsFn) error {
func (i *injector) Run(ctx context.Context, tlsConfig *tls.Config, sentryID spiffeid.ID, signDaprdFn signDaprdCertificateFn, currentTrustAnchors currentTrustAnchorsFn) error {
select {
case <-i.ready:
return errors.New("injector already running")
Expand All @@ -225,6 +227,7 @@

i.currentTrustAnchors = currentTrustAnchors
i.signDaprdCertificate = signDaprdFn
i.sentrySPIFFEID = sentryID

Check warning on line 230 in pkg/injector/service/injector.go

View check run for this annotation

Codecov / codecov/patch

pkg/injector/service/injector.go#L230

Added line #L230 was not covered by tests
i.server.TLSConfig = tlsConfig

errCh := make(chan error, 1)
Expand Down
1 change: 1 addition & 0 deletions pkg/injector/service/pod_patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (i *injector) getPodPatchOperations(ctx context.Context, ar *admissionv1.Ad
sidecar.SidecarDropALLCapabilities = i.config.GetDropCapabilities()
sidecar.ControlPlaneNamespace = i.controlPlaneNamespace
sidecar.ControlPlaneTrustDomain = i.controlPlaneTrustDomain
sidecar.SentrySPIFFEID = i.sentrySPIFFEID.String()
sidecar.CurrentTrustAnchors = trustAnchors
sidecar.CertChain = string(daprdCert)
sidecar.CertKey = string(daprdPrivateKey)
Expand Down
3 changes: 0 additions & 3 deletions pkg/security/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ const (
// TrustBundleK8sSecretName is the name of the kubernetes secret that holds the trust bundle.
TrustBundleK8sSecretName = "dapr-trust-bundle" /* #nosec */

// ServiceAccountTokenAudience is the audience for the service account token.
ServiceAccountTokenAudience = "dapr.io/sentry" /* #nosec */

// TrustAnchorsEnvVar is the environment variable name for the trust anchors in the sidecar.
TrustAnchorsEnvVar = "DAPR_TRUST_ANCHORS"
// CertChainEnvVar is the environment variable name for the cert chain in the sidecar.
Expand Down