forked from drakkan/sftpgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelemetry_test.go
181 lines (152 loc) · 5.42 KB
/
telemetry_test.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (C) 2019 Nicola Murino
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package telemetry
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/stretchr/testify/require"
"github.com/drakkan/sftpgo/v2/internal/common"
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
)
const (
httpsCert = `-----BEGIN CERTIFICATE-----
MIICHTCCAaKgAwIBAgIUHnqw7QnB1Bj9oUsNpdb+ZkFPOxMwCgYIKoZIzj0EAwIw
RTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGElu
dGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yMDAyMDQwOTUzMDRaFw0zMDAyMDEw
OTUzMDRaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYD
VQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwdjAQBgcqhkjOPQIBBgUrgQQA
IgNiAARCjRMqJ85rzMC998X5z761nJ+xL3bkmGVqWvrJ51t5OxV0v25NsOgR82CA
NXUgvhVYs7vNFN+jxtb2aj6Xg+/2G/BNxkaFspIVCzgWkxiz7XE4lgUwX44FCXZM
3+JeUbKjUzBRMB0GA1UdDgQWBBRhLw+/o3+Z02MI/d4tmaMui9W16jAfBgNVHSME
GDAWgBRhLw+/o3+Z02MI/d4tmaMui9W16jAPBgNVHRMBAf8EBTADAQH/MAoGCCqG
SM49BAMCA2kAMGYCMQDqLt2lm8mE+tGgtjDmtFgdOcI72HSbRQ74D5rYTzgST1rY
/8wTi5xl8TiFUyLMUsICMQC5ViVxdXbhuG7gX6yEqSkMKZICHpO8hqFwOD/uaFVI
dV4vKmHUzwK/eIx+8Ay3neE=
-----END CERTIFICATE-----`
httpsKey = `-----BEGIN EC PARAMETERS-----
BgUrgQQAIg==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MIGkAgEBBDCfMNsN6miEE3rVyUPwElfiJSWaR5huPCzUenZOfJT04GAcQdWvEju3
UM2lmBLIXpGgBwYFK4EEACKhZANiAARCjRMqJ85rzMC998X5z761nJ+xL3bkmGVq
WvrJ51t5OxV0v25NsOgR82CANXUgvhVYs7vNFN+jxtb2aj6Xg+/2G/BNxkaFspIV
CzgWkxiz7XE4lgUwX44FCXZM3+JeUbI=
-----END EC PRIVATE KEY-----`
)
func TestInitialization(t *testing.T) {
configDir := filepath.Join(".", "..", "..")
providerConf := dataprovider.Config{
Driver: dataprovider.MemoryDataProviderName,
BackupsPath: "backups",
}
err := dataprovider.Initialize(providerConf, configDir, false)
require.NoError(t, err)
commonConfig := common.Configuration{}
err = common.Initialize(commonConfig, 0)
require.NoError(t, err)
c := Conf{
BindPort: 10000,
BindAddress: "invalid address",
EnableProfiler: false,
}
err = c.Initialize(configDir)
require.Error(t, err)
c.AuthUserFile = "missing"
err = c.Initialize(".")
require.Error(t, err)
err = ReloadCertificateMgr()
require.NoError(t, err)
c.AuthUserFile = ""
c.CertificateFile = "crt"
c.CertificateKeyFile = "key"
err = c.Initialize(".")
require.Error(t, err)
certPath := filepath.Join(os.TempDir(), "test.crt")
keyPath := filepath.Join(os.TempDir(), "test.key")
err = os.WriteFile(certPath, []byte(httpsCert), os.ModePerm)
require.NoError(t, err)
err = os.WriteFile(keyPath, []byte(httpsKey), os.ModePerm)
require.NoError(t, err)
c.CertificateFile = certPath
c.CertificateKeyFile = keyPath
err = c.Initialize(".")
require.Error(t, err)
err = ReloadCertificateMgr()
require.NoError(t, err)
err = os.Remove(certPath)
require.NoError(t, err)
err = os.Remove(keyPath)
require.NoError(t, err)
}
func TestShouldBind(t *testing.T) {
c := Conf{
BindPort: 10000,
EnableProfiler: false,
}
require.True(t, c.ShouldBind())
c.BindPort = 0
require.False(t, c.ShouldBind())
if runtime.GOOS != "windows" {
c.BindAddress = "/absolute/path"
require.True(t, c.ShouldBind())
}
}
func TestRouter(t *testing.T) {
authUserFile := filepath.Join(os.TempDir(), "http_users.txt")
authUserData := []byte("test1:$2y$05$bcHSED7aO1cfLto6ZdDBOOKzlwftslVhtpIkRhAtSa4GuLmk5mola\n")
err := os.WriteFile(authUserFile, authUserData, os.ModePerm)
require.NoError(t, err)
httpAuth, err = common.NewBasicAuthProvider(authUserFile)
require.NoError(t, err)
initializeRouter(true)
testServer := httptest.NewServer(router)
defer testServer.Close()
req, err := http.NewRequest(http.MethodGet, "/healthz", nil)
require.NoError(t, err)
rr := httptest.NewRecorder()
testServer.Config.Handler.ServeHTTP(rr, req)
require.Equal(t, http.StatusOK, rr.Code)
require.Equal(t, "ok", rr.Body.String())
req, err = http.NewRequest(http.MethodGet, "/metrics", nil)
require.NoError(t, err)
rr = httptest.NewRecorder()
testServer.Config.Handler.ServeHTTP(rr, req)
require.Equal(t, http.StatusUnauthorized, rr.Code)
req.SetBasicAuth("test1", "password1")
rr = httptest.NewRecorder()
testServer.Config.Handler.ServeHTTP(rr, req)
require.Equal(t, http.StatusOK, rr.Code)
req, err = http.NewRequest(http.MethodGet, pprofBasePath+"/pprof/", nil)
require.NoError(t, err)
rr = httptest.NewRecorder()
testServer.Config.Handler.ServeHTTP(rr, req)
require.Equal(t, http.StatusUnauthorized, rr.Code)
req.SetBasicAuth("test1", "password1")
rr = httptest.NewRecorder()
testServer.Config.Handler.ServeHTTP(rr, req)
require.Equal(t, http.StatusOK, rr.Code)
httpAuth, err = common.NewBasicAuthProvider("")
require.NoError(t, err)
req, err = http.NewRequest(http.MethodGet, "/metrics", nil)
require.NoError(t, err)
rr = httptest.NewRecorder()
testServer.Config.Handler.ServeHTTP(rr, req)
require.Equal(t, http.StatusOK, rr.Code)
err = os.Remove(authUserFile)
require.NoError(t, err)
}