dataretention_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. // Copyright (C) 2019 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package common
  15. import (
  16. "errors"
  17. "fmt"
  18. "os/exec"
  19. "runtime"
  20. "testing"
  21. "time"
  22. "github.com/sftpgo/sdk"
  23. "github.com/stretchr/testify/assert"
  24. "github.com/stretchr/testify/require"
  25. "github.com/drakkan/sftpgo/v2/internal/dataprovider"
  26. "github.com/drakkan/sftpgo/v2/internal/smtp"
  27. )
  28. func TestRetentionValidation(t *testing.T) {
  29. check := RetentionCheck{}
  30. check.Folders = []dataprovider.FolderRetention{
  31. {
  32. Path: "/",
  33. Retention: -1,
  34. },
  35. }
  36. err := check.Validate()
  37. require.Error(t, err)
  38. assert.Contains(t, err.Error(), "invalid folder retention")
  39. check.Folders = []dataprovider.FolderRetention{
  40. {
  41. Path: "/ab/..",
  42. Retention: 0,
  43. },
  44. }
  45. err = check.Validate()
  46. require.Error(t, err)
  47. assert.Contains(t, err.Error(), "nothing to delete")
  48. assert.Equal(t, "/", check.Folders[0].Path)
  49. check.Folders = append(check.Folders, dataprovider.FolderRetention{
  50. Path: "/../..",
  51. Retention: 24,
  52. })
  53. err = check.Validate()
  54. require.Error(t, err)
  55. assert.Contains(t, err.Error(), `duplicated folder path "/"`)
  56. check.Folders = []dataprovider.FolderRetention{
  57. {
  58. Path: "/dir1",
  59. Retention: 48,
  60. },
  61. {
  62. Path: "/dir2",
  63. Retention: 96,
  64. },
  65. }
  66. err = check.Validate()
  67. assert.NoError(t, err)
  68. assert.Len(t, check.Notifications, 0)
  69. assert.Empty(t, check.Email)
  70. check.Notifications = []RetentionCheckNotification{RetentionCheckNotificationEmail}
  71. err = check.Validate()
  72. require.Error(t, err)
  73. assert.Contains(t, err.Error(), "you must configure an SMTP server")
  74. smtpCfg := smtp.Config{
  75. Host: "mail.example.com",
  76. Port: 25,
  77. From: "[email protected]",
  78. TemplatesPath: "templates",
  79. }
  80. err = smtpCfg.Initialize(configDir, true)
  81. require.NoError(t, err)
  82. err = check.Validate()
  83. require.Error(t, err)
  84. assert.Contains(t, err.Error(), "you must add a valid email address")
  85. check.Email = "[email protected]"
  86. err = check.Validate()
  87. assert.NoError(t, err)
  88. smtpCfg = smtp.Config{}
  89. err = smtpCfg.Initialize(configDir, true)
  90. require.NoError(t, err)
  91. check.Notifications = []RetentionCheckNotification{RetentionCheckNotificationHook}
  92. err = check.Validate()
  93. require.Error(t, err)
  94. assert.Contains(t, err.Error(), "data_retention_hook")
  95. check.Notifications = []string{"not valid"}
  96. err = check.Validate()
  97. require.Error(t, err)
  98. assert.Contains(t, err.Error(), "invalid notification")
  99. }
  100. func TestRetentionEmailNotifications(t *testing.T) {
  101. smtpCfg := smtp.Config{
  102. Host: "127.0.0.1",
  103. Port: 2525,
  104. From: "[email protected]",
  105. TemplatesPath: "templates",
  106. }
  107. err := smtpCfg.Initialize(configDir, true)
  108. require.NoError(t, err)
  109. user := dataprovider.User{
  110. BaseUser: sdk.BaseUser{
  111. Username: "user1",
  112. },
  113. }
  114. user.Permissions = make(map[string][]string)
  115. user.Permissions["/"] = []string{dataprovider.PermAny}
  116. check := RetentionCheck{
  117. Notifications: []RetentionCheckNotification{RetentionCheckNotificationEmail},
  118. Email: "[email protected]",
  119. results: []folderRetentionCheckResult{
  120. {
  121. Path: "/",
  122. Retention: 24,
  123. DeletedFiles: 10,
  124. DeletedSize: 32657,
  125. Elapsed: 10 * time.Second,
  126. },
  127. },
  128. }
  129. conn := NewBaseConnection("", "", "", "", user)
  130. conn.SetProtocol(ProtocolDataRetention)
  131. conn.ID = fmt.Sprintf("data_retention_%v", user.Username)
  132. check.conn = conn
  133. check.sendNotifications(1*time.Second, nil)
  134. err = check.sendEmailNotification(nil)
  135. assert.NoError(t, err)
  136. err = check.sendEmailNotification(errors.New("test error"))
  137. assert.NoError(t, err)
  138. check.results = nil
  139. err = check.sendEmailNotification(nil)
  140. if assert.Error(t, err) {
  141. assert.Contains(t, err.Error(), "no data retention report available")
  142. }
  143. smtpCfg.Port = 2626
  144. err = smtpCfg.Initialize(configDir, true)
  145. require.NoError(t, err)
  146. err = check.sendEmailNotification(nil)
  147. assert.Error(t, err)
  148. check.results = []folderRetentionCheckResult{
  149. {
  150. Path: "/",
  151. Retention: 24,
  152. DeletedFiles: 20,
  153. DeletedSize: 456789,
  154. Elapsed: 12 * time.Second,
  155. },
  156. }
  157. smtpCfg = smtp.Config{}
  158. err = smtpCfg.Initialize(configDir, true)
  159. require.NoError(t, err)
  160. err = check.sendEmailNotification(nil)
  161. assert.Error(t, err)
  162. }
  163. func TestRetentionHookNotifications(t *testing.T) {
  164. dataRetentionHook := Config.DataRetentionHook
  165. Config.DataRetentionHook = fmt.Sprintf("http://%v", httpAddr)
  166. user := dataprovider.User{
  167. BaseUser: sdk.BaseUser{
  168. Username: "user2",
  169. },
  170. }
  171. user.Permissions = make(map[string][]string)
  172. user.Permissions["/"] = []string{dataprovider.PermAny}
  173. check := RetentionCheck{
  174. Notifications: []RetentionCheckNotification{RetentionCheckNotificationHook},
  175. results: []folderRetentionCheckResult{
  176. {
  177. Path: "/",
  178. Retention: 24,
  179. DeletedFiles: 10,
  180. DeletedSize: 32657,
  181. Elapsed: 10 * time.Second,
  182. },
  183. },
  184. }
  185. conn := NewBaseConnection("", "", "", "", user)
  186. conn.SetProtocol(ProtocolDataRetention)
  187. conn.ID = fmt.Sprintf("data_retention_%v", user.Username)
  188. check.conn = conn
  189. check.sendNotifications(1*time.Second, nil)
  190. err := check.sendHookNotification(1*time.Second, nil)
  191. assert.NoError(t, err)
  192. Config.DataRetentionHook = fmt.Sprintf("http://%v/404", httpAddr)
  193. err = check.sendHookNotification(1*time.Second, nil)
  194. assert.ErrorIs(t, err, errUnexpectedHTTResponse)
  195. Config.DataRetentionHook = "http://foo\x7f.com/retention"
  196. err = check.sendHookNotification(1*time.Second, err)
  197. assert.Error(t, err)
  198. Config.DataRetentionHook = "relativepath"
  199. err = check.sendHookNotification(1*time.Second, err)
  200. assert.Error(t, err)
  201. if runtime.GOOS != osWindows {
  202. hookCmd, err := exec.LookPath("true")
  203. assert.NoError(t, err)
  204. Config.DataRetentionHook = hookCmd
  205. err = check.sendHookNotification(1*time.Second, err)
  206. assert.NoError(t, err)
  207. }
  208. Config.DataRetentionHook = dataRetentionHook
  209. }
  210. func TestRetentionPermissionsAndGetFolder(t *testing.T) {
  211. user := dataprovider.User{
  212. BaseUser: sdk.BaseUser{
  213. Username: "user1",
  214. },
  215. }
  216. user.Permissions = make(map[string][]string)
  217. user.Permissions["/"] = []string{dataprovider.PermListItems, dataprovider.PermDelete}
  218. user.Permissions["/dir1"] = []string{dataprovider.PermListItems}
  219. user.Permissions["/dir2/sub1"] = []string{dataprovider.PermCreateDirs}
  220. user.Permissions["/dir2/sub2"] = []string{dataprovider.PermDelete}
  221. check := RetentionCheck{
  222. Folders: []dataprovider.FolderRetention{
  223. {
  224. Path: "/dir2",
  225. Retention: 24 * 7,
  226. IgnoreUserPermissions: true,
  227. },
  228. {
  229. Path: "/dir3",
  230. Retention: 24 * 7,
  231. IgnoreUserPermissions: false,
  232. },
  233. {
  234. Path: "/dir2/sub1/sub",
  235. Retention: 24,
  236. IgnoreUserPermissions: true,
  237. },
  238. },
  239. }
  240. conn := NewBaseConnection("", "", "", "", user)
  241. conn.SetProtocol(ProtocolDataRetention)
  242. conn.ID = fmt.Sprintf("data_retention_%v", user.Username)
  243. check.conn = conn
  244. check.updateUserPermissions()
  245. assert.Equal(t, []string{dataprovider.PermListItems, dataprovider.PermDelete}, conn.User.Permissions["/"])
  246. assert.Equal(t, []string{dataprovider.PermListItems}, conn.User.Permissions["/dir1"])
  247. assert.Equal(t, []string{dataprovider.PermAny}, conn.User.Permissions["/dir2"])
  248. assert.Equal(t, []string{dataprovider.PermAny}, conn.User.Permissions["/dir2/sub1/sub"])
  249. assert.Equal(t, []string{dataprovider.PermCreateDirs}, conn.User.Permissions["/dir2/sub1"])
  250. assert.Equal(t, []string{dataprovider.PermDelete}, conn.User.Permissions["/dir2/sub2"])
  251. _, err := check.getFolderRetention("/")
  252. assert.Error(t, err)
  253. folder, err := check.getFolderRetention("/dir3")
  254. assert.NoError(t, err)
  255. assert.Equal(t, "/dir3", folder.Path)
  256. folder, err = check.getFolderRetention("/dir2/sub3")
  257. assert.NoError(t, err)
  258. assert.Equal(t, "/dir2", folder.Path)
  259. folder, err = check.getFolderRetention("/dir2/sub2")
  260. assert.NoError(t, err)
  261. assert.Equal(t, "/dir2", folder.Path)
  262. folder, err = check.getFolderRetention("/dir2/sub1")
  263. assert.NoError(t, err)
  264. assert.Equal(t, "/dir2", folder.Path)
  265. folder, err = check.getFolderRetention("/dir2/sub1/sub/sub")
  266. assert.NoError(t, err)
  267. assert.Equal(t, "/dir2/sub1/sub", folder.Path)
  268. }
  269. func TestRetentionCheckAddRemove(t *testing.T) {
  270. username := "username"
  271. user := dataprovider.User{
  272. BaseUser: sdk.BaseUser{
  273. Username: username,
  274. },
  275. }
  276. user.Permissions = make(map[string][]string)
  277. user.Permissions["/"] = []string{dataprovider.PermAny}
  278. check := RetentionCheck{
  279. Folders: []dataprovider.FolderRetention{
  280. {
  281. Path: "/",
  282. Retention: 48,
  283. },
  284. },
  285. Notifications: []RetentionCheckNotification{RetentionCheckNotificationHook},
  286. }
  287. assert.NotNil(t, RetentionChecks.Add(check, &user))
  288. checks := RetentionChecks.Get("")
  289. require.Len(t, checks, 1)
  290. assert.Equal(t, username, checks[0].Username)
  291. assert.Greater(t, checks[0].StartTime, int64(0))
  292. require.Len(t, checks[0].Folders, 1)
  293. assert.Equal(t, check.Folders[0].Path, checks[0].Folders[0].Path)
  294. assert.Equal(t, check.Folders[0].Retention, checks[0].Folders[0].Retention)
  295. require.Len(t, checks[0].Notifications, 1)
  296. assert.Equal(t, RetentionCheckNotificationHook, checks[0].Notifications[0])
  297. assert.Nil(t, RetentionChecks.Add(check, &user))
  298. assert.True(t, RetentionChecks.remove(username))
  299. require.Len(t, RetentionChecks.Get(""), 0)
  300. assert.False(t, RetentionChecks.remove(username))
  301. }
  302. func TestRetentionCheckRole(t *testing.T) {
  303. username := "retuser"
  304. role1 := "retrole1"
  305. role2 := "retrole2"
  306. user := dataprovider.User{
  307. BaseUser: sdk.BaseUser{
  308. Username: username,
  309. Role: role1,
  310. },
  311. }
  312. user.Permissions = make(map[string][]string)
  313. user.Permissions["/"] = []string{dataprovider.PermAny}
  314. check := RetentionCheck{
  315. Folders: []dataprovider.FolderRetention{
  316. {
  317. Path: "/",
  318. Retention: 48,
  319. },
  320. },
  321. Notifications: []RetentionCheckNotification{RetentionCheckNotificationHook},
  322. }
  323. assert.NotNil(t, RetentionChecks.Add(check, &user))
  324. checks := RetentionChecks.Get("")
  325. require.Len(t, checks, 1)
  326. assert.Empty(t, checks[0].Role)
  327. checks = RetentionChecks.Get(role1)
  328. require.Len(t, checks, 1)
  329. checks = RetentionChecks.Get(role2)
  330. require.Len(t, checks, 0)
  331. user.Role = ""
  332. assert.Nil(t, RetentionChecks.Add(check, &user))
  333. assert.True(t, RetentionChecks.remove(username))
  334. require.Len(t, RetentionChecks.Get(""), 0)
  335. }
  336. func TestCleanupErrors(t *testing.T) {
  337. user := dataprovider.User{
  338. BaseUser: sdk.BaseUser{
  339. Username: "u",
  340. },
  341. }
  342. user.Permissions = make(map[string][]string)
  343. user.Permissions["/"] = []string{dataprovider.PermAny}
  344. check := &RetentionCheck{
  345. Folders: []dataprovider.FolderRetention{
  346. {
  347. Path: "/path",
  348. Retention: 48,
  349. },
  350. },
  351. }
  352. check = RetentionChecks.Add(*check, &user)
  353. require.NotNil(t, check)
  354. err := check.removeFile("missing file", nil)
  355. assert.Error(t, err)
  356. err = check.cleanupFolder("/")
  357. assert.Error(t, err)
  358. assert.True(t, RetentionChecks.remove(user.Username))
  359. }