-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
enable.go
144 lines (122 loc) · 4.49 KB
/
enable.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
Copyright 2023 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package kubernetes
import (
"context"
"os"
"path/filepath"
"testing"
"time"
"github.com/spiffe/go-spiffe/v2/spiffeid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials/insecure"
"github.com/dapr/dapr/pkg/security"
"github.com/dapr/dapr/tests/integration/framework"
procdaprd "github.com/dapr/dapr/tests/integration/framework/process/daprd"
"github.com/dapr/dapr/tests/integration/framework/process/exec"
"github.com/dapr/dapr/tests/integration/framework/process/grpc/operator"
"github.com/dapr/dapr/tests/integration/framework/process/placement"
"github.com/dapr/dapr/tests/integration/framework/process/sentry"
"github.com/dapr/dapr/tests/integration/suite"
)
func init() {
suite.Register(new(enable))
}
// enable tests Kubernetes daprd with mTLS enabled.
type enable struct {
daprd *procdaprd.Daprd
sentry *sentry.Sentry
placement *placement.Placement
operator *operator.Operator
trustAnchors []byte
}
func (e *enable) Setup(t *testing.T) []framework.Option {
e.sentry = sentry.New(t)
bundle := e.sentry.CABundle()
e.trustAnchors = bundle.TrustAnchors
// Control plane services always serves with mTLS in kubernetes mode.
taFile := filepath.Join(t.TempDir(), "ca.pem")
require.NoError(t, os.WriteFile(taFile, bundle.TrustAnchors, 0o600))
e.placement = placement.New(t,
placement.WithEnableTLS(true),
placement.WithTrustAnchorsFile(taFile),
placement.WithSentryAddress(e.sentry.Address()),
)
e.operator = operator.New(t, operator.WithSentry(e.sentry))
e.daprd = procdaprd.New(t,
procdaprd.WithAppID("my-app"),
procdaprd.WithMode("kubernetes"),
procdaprd.WithExecOptions(exec.WithEnvVars(t, "DAPR_TRUST_ANCHORS", string(bundle.TrustAnchors))),
procdaprd.WithSentryAddress(e.sentry.Address()),
procdaprd.WithControlPlaneAddress(e.operator.Address(t)),
procdaprd.WithDisableK8sSecretStore(true),
procdaprd.WithPlacementAddresses(e.placement.Address()),
// Enable mTLS
procdaprd.WithEnableMTLS(true),
)
return []framework.Option{
framework.WithProcesses(e.sentry, e.placement, e.operator, e.daprd),
}
}
func (e *enable) Run(t *testing.T, ctx context.Context) {
e.sentry.WaitUntilRunning(t, ctx)
e.placement.WaitUntilRunning(t, ctx)
e.daprd.WaitUntilRunning(t, ctx)
t.Run("trying plain text connection to Dapr API should fail", func(t *testing.T) {
gctx, gcancel := context.WithTimeout(ctx, time.Second)
t.Cleanup(gcancel)
_, err := grpc.DialContext(gctx, e.daprd.InternalGRPCAddress(),
grpc.WithReturnConnectionError(),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
require.ErrorContains(t, err, "error reading server preface")
})
t.Run("trying mTLS connection to Dapr API should succeed", func(t *testing.T) {
sctx, cancel := context.WithCancel(ctx)
secProv, err := security.New(sctx, security.Options{
SentryAddress: e.sentry.Address(),
ControlPlaneTrustDomain: "localhost",
ControlPlaneNamespace: "default",
TrustAnchors: e.trustAnchors,
AppID: "another-app",
MTLSEnabled: true,
})
require.NoError(t, err)
secProvErr := make(chan error)
go func() {
secProvErr <- secProv.Run(sctx)
}()
t.Cleanup(func() {
cancel()
select {
case <-time.After(5 * time.Second):
t.Fatal("timed out waiting for security provider to stop")
case err = <-secProvErr:
require.NoError(t, err)
}
})
sec, err := secProv.Handler(sctx)
require.NoError(t, err)
myAppID, err := spiffeid.FromSegments(spiffeid.RequireTrustDomainFromString("public"), "ns", "default", "my-app")
require.NoError(t, err)
conn, err := grpc.DialContext(ctx, e.daprd.InternalGRPCAddress(), sec.GRPCDialOptionMTLS(myAppID),
grpc.WithReturnConnectionError())
require.NoError(t, err)
conn.Connect()
assert.Equal(t, connectivity.Ready, conn.GetState())
require.NoError(t, conn.Close())
})
}