/
expiry.go
110 lines (89 loc) · 3.12 KB
/
expiry.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
/*
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 metrics
import (
"bytes"
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"fmt"
"io"
"net/http"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/dapr/dapr/pkg/sentry/server/ca"
"github.com/dapr/dapr/tests/integration/framework"
procsentry "github.com/dapr/dapr/tests/integration/framework/process/sentry"
"github.com/dapr/dapr/tests/integration/framework/util"
"github.com/dapr/dapr/tests/integration/suite"
)
func init() {
suite.Register(new(expiry))
}
// expiry tests the certificate expiry metric.
type expiry struct {
notGiven *procsentry.Sentry
given *procsentry.Sentry
}
func (e *expiry) Setup(t *testing.T) []framework.Option {
rootKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
require.NoError(t, err)
onemonth := time.Hour * 24 * 30
bundle, err := ca.GenerateBundle(rootKey, "integration.test.dapr.io", time.Second*5, &onemonth)
require.NoError(t, err)
e.notGiven = procsentry.New(t, procsentry.WithWriteTrustBundle(false))
e.given = procsentry.New(t, procsentry.WithCABundle(bundle))
return []framework.Option{
framework.WithProcesses(e.notGiven, e.given),
}
}
func (e *expiry) Run(t *testing.T, ctx context.Context) {
e.notGiven.WaitUntilRunning(t, ctx)
e.given.WaitUntilRunning(t, ctx)
client := util.HTTPClient(t)
testExpiry := func(proc *procsentry.Sentry, expTime time.Time) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("http://localhost:%d/metrics", proc.MetricsPort()), nil)
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
respBody, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.NoError(t, resp.Body.Close())
for _, line := range bytes.Split(respBody, []byte("\n")) {
if len(line) == 0 || line[0] == '#' {
continue
}
split := bytes.Split(line, []byte(" "))
if len(split) != 2 {
continue
}
if string(split[0]) != "dapr_sentry_issuercert_expiry_timestamp" {
continue
}
timestamp, err := strconv.ParseFloat(string(split[1]), 64)
require.NoError(t, err)
tsTime := time.Unix(int64(timestamp), 0)
assert.InDelta(t, expTime.Unix(), tsTime.Unix(), 20, "expected expiry time to be within 20 seconds of expected")
return
}
assert.Fail(t, "metric not found")
}
// Expect the expiry to be 1 year from now for sentry self generated certs.
testExpiry(e.notGiven, time.Now().Add(time.Hour*24*365))
// Expect the expiry to be 1 month from now for sentry certs given by the user.
testExpiry(e.given, time.Now().Add(time.Hour*24*30))
}