Skip to content

Commit

Permalink
Refactor SPIFFE from pkg/security to kit
Browse files Browse the repository at this point in the history
Updates the `pkg/security` package to move the SPIFFE implementation to
a new kit package. This new kit package is more modulated and fuller
test coverage. This package has been moved so that it can be both
imported by dapr & components-contrib, as well as making the package
more suitable for further development to support X.509 Component auth.

dapr/proposals#51

Also moves in test/utils from dapr to crypto/test for shared usage.

Part of dapr/proposals#51

Uses go mod fork of dapr/kit#92

Signed-off-by: joshvanl <[email protected]>
  • Loading branch information
JoshVanL committed Apr 23, 2024
1 parent d26894e commit 31fc97b
Show file tree
Hide file tree
Showing 30 changed files with 327 additions and 1,273 deletions.
4 changes: 2 additions & 2 deletions cmd/injector/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func Run() {
SentryAddress: cfg.SentryAddress,
ControlPlaneTrustDomain: cfg.ControlPlaneTrustDomain,
ControlPlaneNamespace: namespace,
TrustAnchorsFile: cfg.TrustAnchorsFile,
TrustAnchorsFile: &cfg.TrustAnchorsFile,
AppID: "dapr-injector",
MTLSEnabled: true,
Mode: modes.KubernetesMode,
Expand Down Expand Up @@ -165,7 +165,7 @@ func Run() {
return rerr
}

caBundle, rErr := sec.CurrentTrustAnchors()
caBundle, rErr := sec.CurrentTrustAnchors(ctx)
if rErr != nil {
return rErr
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/placement/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func Run() {
SentryAddress: opts.SentryAddress,
ControlPlaneTrustDomain: opts.TrustDomain,
ControlPlaneNamespace: security.CurrentNamespace(),
TrustAnchorsFile: opts.TrustAnchorsFile,
TrustAnchorsFile: &opts.TrustAnchorsFile,
AppID: "dapr-placement",
MTLSEnabled: opts.TLSEnabled,
Mode: modes.DaprMode(opts.Mode),
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ require (
github.com/sony/gobreaker v0.5.0
github.com/spf13/cast v1.6.0
github.com/spf13/pflag v1.0.5
github.com/spiffe/go-spiffe/v2 v2.1.6
github.com/spiffe/go-spiffe/v2 v2.1.7
github.com/stretchr/testify v1.8.4
github.com/valyala/fasthttp v1.51.0
go.mongodb.org/mongo-driver v1.12.1
Expand All @@ -68,7 +68,6 @@ require (
google.golang.org/genproto/googleapis/api v0.0.0-20231120223509-83a465c0220f
google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0
google.golang.org/grpc v1.60.1
google.golang.org/grpc/examples v0.0.0-20230224211313-3775f633ce20
google.golang.org/protobuf v1.33.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.28.4
Expand Down Expand Up @@ -416,6 +415,7 @@ require (
google.golang.org/api v0.149.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20231211222908-989df2bf70f3 // indirect
google.golang.org/grpc/examples v0.0.0-20230224211313-3775f633ce20 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/couchbase/gocb.v1 v1.6.7 // indirect
gopkg.in/couchbase/gocbcore.v7 v7.1.18 // indirect
Expand Down Expand Up @@ -472,7 +472,8 @@ replace github.com/microcosm-cc/bluemonday => github.com/microcosm-cc/bluemonday
// Don't commit with this uncommented!
//
// replace github.com/dapr/components-contrib => ../components-contrib
// replace github.com/dapr/kit => ../kit
replace github.com/dapr/kit => github.com/joshvanl/kit v0.0.0-20240403103913-969bdc7b9937

//
// Then, run `make modtidy-all` in this repository.
// This ensures that go.mod and go.sum are up-to-date for each go.mod file.
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ github.com/HdrHistogram/hdrhistogram-go v1.1.2/go.mod h1:yDgFjdqOqDEKOvasDdhWNXY
github.com/IBM/sarama v1.42.2 h1:VoY4hVIZ+WQJ8G9KNY/SQlWguBQXQ9uvFPOnrcu8hEw=
github.com/IBM/sarama v1.42.2/go.mod h1:FLPGUGwYqEs62hq2bVG6Io2+5n+pS6s/WOXVKWSLFtE=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/Microsoft/go-winio v0.6.0 h1:slsWYD/zyx7lCXoZVlvQrj0hPTM1HI4+v1sIda2yDvg=
github.com/Microsoft/go-winio v0.6.0/go.mod h1:cTAf44im0RAYeL23bpB+fzCyDH2MJiz2BO69KH/soAE=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/Netflix/go-env v0.0.0-20220526054621-78278af1949d h1:wvStE9wLpws31NiWUx+38wny1msZ/tm+eL5xmm4Y7So=
github.com/Netflix/go-env v0.0.0-20220526054621-78278af1949d/go.mod h1:9XMFaCeRyW7fC9XJOWQ+NdAv8VLG7ys7l3x4ozEGLUQ=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
Expand Down Expand Up @@ -437,8 +437,6 @@ github.com/danieljoos/wincred v1.1.2 h1:QLdCxFs1/Yl4zduvBdcHB8goaYk9RARS2SgLLRuA
github.com/danieljoos/wincred v1.1.2/go.mod h1:GijpziifJoIBfYh+S7BbkdUTU4LfM+QnGqR5Vl2tAx0=
github.com/dapr/components-contrib v1.13.0-rc.10 h1:d5xGjRAl2Gm23YsMfwa7Lwz5FFCb1ad4UqCmHTmtyV4=
github.com/dapr/components-contrib v1.13.0-rc.10/go.mod h1:CFCvUwS2ClqTVyzfqHEGiXM3gftWOjOI73pxECoY/Iw=
github.com/dapr/kit v0.13.1-0.20240402103809-0c7cfce53d9e h1:mLvqfGuppb6uhsijmwTlF5sZVtGvig+Ua5ESKF17SxA=
github.com/dapr/kit v0.13.1-0.20240402103809-0c7cfce53d9e/go.mod h1:dons8V2bF6MPR2yFdxtTa86PfaE7EJtKAOkZ9hOavBQ=
github.com/dave/jennifer v1.4.0/go.mod h1:fIb+770HOpJ2fmN9EPPKOqm1vMGhB+TwXKMZhrIygKg=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -579,8 +577,8 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/go-jose/go-jose/v3 v3.0.0 h1:s6rrhirfEP/CGIoc6p+PZAeogN2SxKav6Wp7+dyMWVo=
github.com/go-jose/go-jose/v3 v3.0.0/go.mod h1:RNkWWRld676jZEYoV3+XK8L2ZnNSvIsxFMht0mSX+u8=
github.com/go-jose/go-jose/v3 v3.0.1 h1:pWmKFVtt+Jl0vBZTIpz/eAKwsm6LkIxDVVbFHKkchhA=
github.com/go-jose/go-jose/v3 v3.0.1/go.mod h1:RNkWWRld676jZEYoV3+XK8L2ZnNSvIsxFMht0mSX+u8=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/kit v0.10.0 h1:dXFJfIHVvUcpSgDOV+Ne6t7jXri8Tfv2uOLHUZ2XNuo=
Expand Down Expand Up @@ -1014,6 +1012,8 @@ github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22
github.com/jonboulle/clockwork v0.2.2/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/joshvanl/kit v0.0.0-20240403103913-969bdc7b9937 h1:0Uw8Jhb/gNphz08pxL5ce+w2kAHE9/Pe6W1o8robFS8=
github.com/joshvanl/kit v0.0.0-20240403103913-969bdc7b9937/go.mod h1:LkPZyrSpa2xLBgYMwUhDbWZcZVt/WdL7FSPlN0PrSog=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
Expand Down Expand Up @@ -1522,8 +1522,8 @@ github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5q
github.com/spf13/viper v1.7.1/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
github.com/spf13/viper v1.15.0 h1:js3yy885G8xwJa6iOISGFwd+qlUo5AvyXb7CiihdtiU=
github.com/spf13/viper v1.15.0/go.mod h1:fFcTBJxvhhzSJiZy8n+PeW6t8l+KeT/uTARa0jHOQLA=
github.com/spiffe/go-spiffe/v2 v2.1.6 h1:4SdizuQieFyL9eNU+SPiCArH4kynzaKOOj0VvM8R7Xo=
github.com/spiffe/go-spiffe/v2 v2.1.6/go.mod h1:eVDqm9xFvyqao6C+eQensb9ZPkyNEeaUbqbBpOhBnNk=
github.com/spiffe/go-spiffe/v2 v2.1.7 h1:VUkM1yIyg/x8X7u1uXqSRVRCdMdfRIEdFBzpqoeASGk=
github.com/spiffe/go-spiffe/v2 v2.1.7/go.mod h1:QJDGdhXllxjxvd5B+2XnhhXB/+rC8gr+lNrtOryiWeE=
github.com/stealthrocket/wasi-go v0.8.1-0.20230912180546-8efbab50fb58 h1:mTC4gyv3lcJ1XpzZMAckqkvWUqeT5Bva4RAT1IoHAAA=
github.com/stealthrocket/wasi-go v0.8.1-0.20230912180546-8efbab50fb58/go.mod h1:ZAYCOqLJkc9P6fcq14TV4cf+gJ2fHthp9kCGxBViagE=
github.com/stealthrocket/wazergo v0.19.1 h1:BPrITETPgSFwiytwmToO0MbUC/+RGC39JScz1JmmG6c=
Expand Down
2 changes: 1 addition & 1 deletion pkg/actors/placement/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func testSecurity(t *testing.T) security.Handler {
ControlPlaneTrustDomain: "test.example.com",
ControlPlaneNamespace: "default",
MTLSEnabled: false,
OverrideCertRequestSource: func(context.Context, []byte) ([]*x509.Certificate, error) {
OverrideCertRequestFn: func(context.Context, []byte) ([]*x509.Certificate, error) {
return []*x509.Certificate{nil}, nil
},
})
Expand Down
3 changes: 2 additions & 1 deletion pkg/injector/service/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package service

import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
Expand Down Expand Up @@ -52,7 +53,7 @@ func TestHandleRequest(t *testing.T) {

require.NoError(t, err)
injector := i.(*injector)
injector.currentTrustAnchors = func() ([]byte, error) {
injector.currentTrustAnchors = func(context.Context) ([]byte, error) {
return nil, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/injector/service/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var AllowedServiceAccountInfos = []string{
}

type (
currentTrustAnchorsFn func() (ca []byte, err error)
currentTrustAnchorsFn func(context.Context) (ca []byte, err error)
)

// Injector is the interface for the Dapr runtime sidecar injection component.
Expand Down
2 changes: 1 addition & 1 deletion pkg/injector/service/pod_patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (i *injector) getPodPatchOperations(ctx context.Context, ar *admissionv1.Ad
sentryAddress := patcher.ServiceSentry.Address(i.config.Namespace, i.config.KubeClusterDomain)
operatorAddress := patcher.ServiceAPI.Address(i.config.Namespace, i.config.KubeClusterDomain)

trustAnchors, err := i.currentTrustAnchors()
trustAnchors, err := i.currentTrustAnchors(ctx)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/operator/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import (
subscriptionsapiV2alpha1 "github.com/dapr/dapr/pkg/apis/subscriptions/v2alpha1"
"github.com/dapr/dapr/pkg/client/clientset/versioned/scheme"
operatorv1pb "github.com/dapr/dapr/pkg/proto/operator/v1"
"github.com/dapr/dapr/tests/util"
"github.com/dapr/kit/crypto/test"
)

type mockComponentUpdateServer struct {
Expand Down Expand Up @@ -194,7 +194,7 @@ func TestProcessComponentSecrets(t *testing.T) {
func TestComponentUpdate(t *testing.T) {
appID := spiffeid.RequireFromString("spiffe://example.org/ns/ns1/app1")
serverID := spiffeid.RequireFromString("spiffe://example.org/ns/dapr-system/dapr-operator")
pki := util.GenPKI(t, util.PKIOptions{
pki := test.GenPKI(t, test.PKIOptions{
LeafID: serverID,
ClientID: appID,
})
Expand Down Expand Up @@ -319,7 +319,7 @@ func TestComponentUpdate(t *testing.T) {
func TestHTTPEndpointUpdate(t *testing.T) {
appID := spiffeid.RequireFromString("spiffe://example.org/ns/ns1/app1")
serverID := spiffeid.RequireFromString("spiffe://example.org/ns/dapr-system/dapr-operator")
pki := util.GenPKI(t, util.PKIOptions{
pki := test.GenPKI(t, test.PKIOptions{
LeafID: serverID,
ClientID: appID,
})
Expand Down Expand Up @@ -413,7 +413,7 @@ func TestHTTPEndpointUpdate(t *testing.T) {
func TestListsNamespaced(t *testing.T) {
appID := spiffeid.RequireFromString("spiffe://example.org/ns/namespace-a/app1")
serverID := spiffeid.RequireFromString("spiffe://example.org/ns/dapr-system/dapr-operator")
pki := util.GenPKI(t, util.PKIOptions{
pki := test.GenPKI(t, test.PKIOptions{
LeafID: serverID,
ClientID: appID,
})
Expand Down
6 changes: 3 additions & 3 deletions pkg/operator/api/authz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/dapr/dapr/tests/util"
"github.com/dapr/kit/crypto/test"
)

func Test_authzRequest(t *testing.T) {
appID := spiffeid.RequireFromString("spiffe://example.org/ns/ns1/app1")
serverID := spiffeid.RequireFromString("spiffe://example.org/ns/dapr-system/dapr-operator")
pki := util.GenPKI(t, util.PKIOptions{LeafID: serverID, ClientID: appID})
pki := test.GenPKI(t, test.PKIOptions{LeafID: serverID, ClientID: appID})

t.Run("no auth context should error", func(t *testing.T) {
err := new(apiServer).authzRequest(context.Background(), "ns1")
Expand All @@ -51,7 +51,7 @@ func Test_authzRequest(t *testing.T) {

t.Run("invalid SPIFFE path should error", func(t *testing.T) {
appID := spiffeid.RequireFromString("spiffe://example.org/foo/bar")
pki2 := util.GenPKI(t, util.PKIOptions{LeafID: serverID, ClientID: appID})
pki2 := test.GenPKI(t, test.PKIOptions{LeafID: serverID, ClientID: appID})
err := new(apiServer).authzRequest(pki2.ClientGRPCCtx(t), "ns1")
require.Error(t, err)
assert.Equal(t, codes.PermissionDenied, status.Code(err))
Expand Down
4 changes: 2 additions & 2 deletions pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func NewOperator(ctx context.Context, opts Options) (Operator, error) {
SentryAddress: config.SentryAddress,
ControlPlaneTrustDomain: config.ControlPlaneTrustDomain,
ControlPlaneNamespace: security.CurrentNamespace(),
TrustAnchorsFile: opts.TrustAnchorsFile,
TrustAnchorsFile: &opts.TrustAnchorsFile,
AppID: "dapr-operator",
// mTLS is always enabled for the operator.
MTLSEnabled: true,
Expand Down Expand Up @@ -314,7 +314,7 @@ func (o *operator) Run(ctx context.Context) error {
return rErr
}

caBundle, rErr := sec.CurrentTrustAnchors()
caBundle, rErr := sec.CurrentTrustAnchors(ctx)
if rErr != nil {
return rErr
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/runtime/processor/binding/send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func TestGetSubscribedBindingsGRPC(t *testing.T) {
ControlPlaneTrustDomain: "test.example.com",
ControlPlaneNamespace: "default",
MTLSEnabled: false,
OverrideCertRequestSource: func(context.Context, []byte) ([]*x509.Certificate, error) {
OverrideCertRequestFn: func(context.Context, []byte) ([]*x509.Certificate, error) {
return []*x509.Certificate{nil}, nil
},
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2134,7 +2134,7 @@ func testSecurity(t *testing.T) security.Handler {
ControlPlaneTrustDomain: "test.example.com",
ControlPlaneNamespace: "default",
MTLSEnabled: false,
OverrideCertRequestSource: func(context.Context, []byte) ([]*x509.Certificate, error) {
OverrideCertRequestFn: func(context.Context, []byte) ([]*x509.Certificate, error) {
return []*x509.Certificate{nil}, nil
},
})
Expand Down
10 changes: 5 additions & 5 deletions pkg/security/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
type Fake struct {
controlPlaneTrustDomainFn func() spiffeid.TrustDomain
controlPlaneNamespaceFn func() string
currentTrustAnchorsFn func() ([]byte, error)
currentTrustAnchorsFn func(context.Context) ([]byte, error)
watchTrustAnchorsFn func(context.Context, chan<- []byte)
mtls bool

Expand Down Expand Up @@ -75,7 +75,7 @@ func New() *Fake {
grpcServerOptionNoClientAuthFn: func() grpc.ServerOption {
return grpc.Creds(nil)
},
currentTrustAnchorsFn: func() ([]byte, error) {
currentTrustAnchorsFn: func(context.Context) ([]byte, error) {
return []byte{}, nil
},
watchTrustAnchorsFn: func(context.Context, chan<- []byte) {
Expand Down Expand Up @@ -173,7 +173,7 @@ func (f *Fake) GRPCServerOptionMTLS() grpc.ServerOption {
return f.grpcServerOptionMTLSFn()
}

func (f *Fake) WithCurrentTrustAnchorsFn(fn func() ([]byte, error)) *Fake {
func (f *Fake) WithCurrentTrustAnchorsFn(fn func(context.Context) ([]byte, error)) *Fake {
f.currentTrustAnchorsFn = fn
return f
}
Expand Down Expand Up @@ -202,8 +202,8 @@ func (f *Fake) GRPCServerOptionNoClientAuth() grpc.ServerOption {
return f.grpcServerOptionNoClientAuthFn()
}

func (f *Fake) CurrentTrustAnchors() ([]byte, error) {
return f.currentTrustAnchorsFn()
func (f *Fake) CurrentTrustAnchors(ctx context.Context) ([]byte, error) {
return f.currentTrustAnchorsFn(ctx)
}

func (f *Fake) WatchTrustAnchors(ctx context.Context, ch chan<- []byte) {
Expand Down
Loading

0 comments on commit 31fc97b

Please sign in to comment.