storage.pool.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. package main
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "net/http"
  7. "os"
  8. "regexp"
  9. "runtime"
  10. "sort"
  11. "strings"
  12. "time"
  13. "imuslab.com/arozos/mod/filesystem/arozfs"
  14. "imuslab.com/arozos/mod/storage/access"
  15. "imuslab.com/arozos/mod/utils"
  16. fs "imuslab.com/arozos/mod/filesystem"
  17. prout "imuslab.com/arozos/mod/prouter"
  18. storage "imuslab.com/arozos/mod/storage"
  19. )
  20. /*
  21. Storage Pool Handler
  22. author: tobychui
  23. This script handle the storage pool editing of different permission groups.
  24. A pool is where a drive is configured and which group it belongs to. Who
  25. may use a drive is set on the drive itself, see storage.access.go.
  26. */
  27. const (
  28. storagePoolSystem = "system"
  29. reloadAllPools = "1eb201a3-d0f6-6630-5e6d-2f40480115c5" //Super long string just to prevent any typo
  30. driveKindFixed = "fixed" //user:/ and tmp:/, open to everyone
  31. driveKindBuiltin = "builtin" //share:/ and cluster:/, built by code, with an access list
  32. driveKindConfigured = "configured" //Has a row in the config of its pool
  33. driveStateOnline = "online"
  34. driveStateStopped = "stopped" //Stopped by an admin until the next restart or reload
  35. driveStateOffline = "offline" //Configured, but not mounted
  36. driveStateRemoved = "removed" //Mounted, but its config row is gone. Leaves at the next reload
  37. driveStateNotActive = "notactive" //cluster:/ on a node with no cluster or no volume yet
  38. driveTestTimeout = 20 * time.Second
  39. )
  40. // A new drive id ends up in every virtual path of the drive
  41. var driveIDFormat = regexp.MustCompile(`^[A-Za-z0-9_-]+$`)
  42. func StoragePoolEditorInit() {
  43. adminRouter := prout.NewModuleRouter(prout.RouterOption{
  44. ModuleName: "System Settings",
  45. AdminOnly: true,
  46. UserHandler: userHandler,
  47. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  48. utils.SendErrorResponse(w, "Permission Denied")
  49. },
  50. })
  51. //Create the required folder structure
  52. err := os.MkdirAll("./system/storage", 0775)
  53. if err != nil {
  54. systemWideLogger.PrintAndLog("System", "Create storage pool setting folder failed: ", nil)
  55. systemWideLogger.PrintAndLog("System", fmt.Sprint(err), nil)
  56. os.Exit(1)
  57. }
  58. adminRouter.HandleFunc("/system/storage/drives", HandleListDrives)
  59. adminRouter.HandleFunc("/system/storage/access", HandleDriveAccess)
  60. adminRouter.HandleFunc("/system/storage/access/explain", HandleDriveAccessExplain)
  61. adminRouter.HandleFunc("/system/storage/pool/removeHandler", HandleStoragePoolRemove)
  62. adminRouter.HandleFunc("/system/storage/pool/reload", HandleStoragePoolReload)
  63. adminRouter.HandleFunc("/system/storage/pool/toggle", HandleFSHToggle)
  64. adminRouter.HandleFunc("/system/storage/pool/edit", HandleFSHEdit)
  65. adminRouter.HandleFunc("/system/storage/pool/test", HandleFSHTest)
  66. }
  67. /*
  68. Pool config files
  69. */
  70. // storagePoolKnown reports whether a pool of this name exists
  71. func storagePoolKnown(pool string) bool {
  72. return pool == storagePoolSystem || permissionHandler.GroupExists(pool)
  73. }
  74. // poolConfigFile returns the config file of a pool
  75. func poolConfigFile(pool string) string {
  76. if pool == storagePoolSystem {
  77. return *storage_config_file
  78. }
  79. return "./system/storage/" + pool + ".json"
  80. }
  81. // readPoolConfig returns the drives configured in a pool. A pool with no
  82. // config file has none.
  83. func readPoolConfig(pool string) ([]fs.FileSystemOption, error) {
  84. options := []fs.FileSystemOption{}
  85. configFile := poolConfigFile(pool)
  86. if !fs.FileExists(configFile) {
  87. return options, nil
  88. }
  89. content, err := os.ReadFile(configFile)
  90. if err != nil {
  91. return nil, err
  92. }
  93. if err := json.Unmarshal(content, &options); err != nil {
  94. systemWideLogger.PrintAndLog("Storage", "Unable to parse storage config "+configFile, err)
  95. return nil, err
  96. }
  97. return options, nil
  98. }
  99. // writePoolConfig replaces the drives configured in a pool
  100. func writePoolConfig(pool string, options []fs.FileSystemOption) error {
  101. configFile := poolConfigFile(pool)
  102. if len(options) == 0 {
  103. err := os.Remove(configFile)
  104. if err != nil && !errors.Is(err, os.ErrNotExist) {
  105. return err
  106. }
  107. return nil
  108. }
  109. js, err := json.MarshalIndent(options, "", " ")
  110. if err != nil {
  111. return err
  112. }
  113. return os.WriteFile(configFile, js, 0775)
  114. }
  115. // findPoolOption returns the config row of a drive in a pool, or nil
  116. func findPoolOption(options []fs.FileSystemOption, uuid string) *fs.FileSystemOption {
  117. for i := range options {
  118. if options[i].Uuid == uuid {
  119. return &options[i]
  120. }
  121. }
  122. return nil
  123. }
  124. // Constract a fsoption from form
  125. func buildOptionFromRequestForm(payload string) (fs.FileSystemOption, error) {
  126. newFsOption := fs.FileSystemOption{}
  127. err := json.Unmarshal([]byte(payload), &newFsOption)
  128. if err != nil {
  129. return fs.FileSystemOption{}, err
  130. }
  131. //Do data cleaning
  132. newFsOption.Name = strings.TrimSpace(newFsOption.Name)
  133. newFsOption.Uuid = strings.TrimSpace(newFsOption.Uuid)
  134. newFsOption.Path = strings.TrimSpace(newFsOption.Path)
  135. if newFsOption.Filesystem == "" {
  136. //A plain folder: the form does not ask, so use what this host has
  137. newFsOption.Filesystem = localFilesystemType()
  138. }
  139. return newFsOption, nil
  140. }
  141. // localFilesystemType names the file system of a local folder on this host
  142. func localFilesystemType() string {
  143. if runtime.GOOS == "windows" {
  144. return "ntfs"
  145. }
  146. return "ext4"
  147. }
  148. /*
  149. Mounting
  150. Saving a drive mounts it right away, so most changes need no pool reload.
  151. */
  152. func mountDrive(poolOwner string, option fs.FileSystemOption) error {
  153. pool, err := GetStoragePoolByOwner(poolOwner)
  154. if err != nil {
  155. return err
  156. }
  157. handler, err := fs.NewFileSystemHandler(option, fs.RuntimePersistenceConfig{
  158. LocalBufferPath: *tmp_directory,
  159. })
  160. if err != nil {
  161. return err
  162. }
  163. if err := pool.AttachFsHandler(handler); err != nil {
  164. handler.Close()
  165. return err
  166. }
  167. systemWideLogger.PrintAndLog("Storage", handler.Name+" Mounted as "+handler.UUID+":/ for "+poolOwner, nil)
  168. return nil
  169. }
  170. func unmountDrive(poolOwner string, uuid string) {
  171. pool, err := GetStoragePoolByOwner(poolOwner)
  172. if err != nil {
  173. return
  174. }
  175. fsh, err := pool.GetFsHandlerByUUID(uuid)
  176. if err != nil {
  177. return
  178. }
  179. pool.DetachFsHandler(uuid)
  180. if !fsh.Closed {
  181. fsh.Close()
  182. }
  183. }
  184. // sendMountResult reports a saved drive. The config is kept when the mount
  185. // fails, so the drive shows as offline and can be fixed from the page.
  186. func sendMountResult(w http.ResponseWriter, mountErr error) {
  187. result := struct {
  188. Mounted bool `json:"mounted"`
  189. Reason string `json:"reason,omitempty"`
  190. }{Mounted: mountErr == nil}
  191. if mountErr != nil {
  192. result.Reason = mountErr.Error()
  193. }
  194. js, _ := json.Marshal(result)
  195. utils.SendJSONResponse(w, string(js))
  196. }
  197. /*
  198. Editing
  199. */
  200. // Handle editing of a given File System Handler
  201. func HandleFSHEdit(w http.ResponseWriter, r *http.Request) {
  202. opr, _ := utils.PostPara(r, "opr")
  203. group, err := utils.PostPara(r, "group")
  204. if err != nil {
  205. utils.SendErrorResponse(w, "Invalid group given")
  206. return
  207. }
  208. if !storagePoolKnown(group) {
  209. utils.SendErrorResponse(w, "Group not exists: "+group)
  210. return
  211. }
  212. switch opr {
  213. case "get":
  214. handleDriveGet(w, r, group)
  215. case "set":
  216. handleDriveSet(w, r, group)
  217. case "new":
  218. handleDriveNew(w, r, group)
  219. default:
  220. utils.SendErrorResponse(w, "Unknown opr given")
  221. }
  222. }
  223. func handleDriveGet(w http.ResponseWriter, r *http.Request, group string) {
  224. uuid, err := utils.PostPara(r, "uuid")
  225. if err != nil {
  226. utils.SendErrorResponse(w, "Invalid UUID")
  227. return
  228. }
  229. fshOption, err := getFSHConfigFromGroupAndUUID(group, uuid)
  230. if err != nil {
  231. utils.SendErrorResponse(w, err.Error())
  232. return
  233. }
  234. //Hide the password info
  235. fshOption.Username = ""
  236. fshOption.Password = ""
  237. js, _ := json.Marshal(fshOption)
  238. utils.SendJSONResponse(w, string(js))
  239. }
  240. func handleDriveSet(w http.ResponseWriter, r *http.Request, group string) {
  241. uuid, err := utils.PostPara(r, "uuid")
  242. if err != nil {
  243. utils.SendErrorResponse(w, "Invalid UUID")
  244. return
  245. }
  246. config, err := utils.PostPara(r, "config")
  247. if err != nil {
  248. utils.SendErrorResponse(w, "Invalid config")
  249. return
  250. }
  251. newFsOption, err := buildOptionFromRequestForm(config)
  252. if err != nil {
  253. utils.SendErrorResponse(w, err.Error())
  254. return
  255. }
  256. //Paths, share links and the access list all carry the id. Looking the old
  257. //row up by a changed id would also leave it behind as a second drive.
  258. if newFsOption.Uuid != uuid {
  259. utils.SendErrorResponse(w, "The id of a drive cannot be changed")
  260. return
  261. }
  262. if access.IsBuiltin(uuid) {
  263. utils.SendErrorResponse(w, "Built-in drives cannot be edited")
  264. return
  265. }
  266. err = setFSHConfigByGroupAndId(group, uuid, &newFsOption)
  267. if err != nil {
  268. utils.SendErrorResponse(w, err.Error())
  269. return
  270. }
  271. //Replace the running drive with the new settings
  272. unmountDrive(group, uuid)
  273. sendMountResult(w, mountDrive(group, newFsOption))
  274. driveAccessChanged()
  275. }
  276. func handleDriveNew(w http.ResponseWriter, r *http.Request, group string) {
  277. config, err := utils.PostPara(r, "config")
  278. if err != nil {
  279. utils.SendErrorResponse(w, "Invalid config")
  280. return
  281. }
  282. newFsOption, err := buildOptionFromRequestForm(config)
  283. if err != nil {
  284. utils.SendErrorResponse(w, err.Error())
  285. return
  286. }
  287. //The access list comes with the drive, so that it is not reachable by the
  288. //default rule for even a moment
  289. var rule *access.Rule
  290. if ruleJSON, _ := utils.PostPara(r, "access"); ruleJSON != "" {
  291. rule = &access.Rule{}
  292. if err := json.Unmarshal([]byte(ruleJSON), rule); err != nil {
  293. utils.SendErrorResponse(w, "Invalid access list")
  294. return
  295. }
  296. if err := rule.Validate(); err != nil {
  297. utils.SendErrorResponse(w, err.Error())
  298. return
  299. }
  300. }
  301. if err := validateNewDrive(&newFsOption, collectPoolStates()); err != nil {
  302. utils.SendErrorResponse(w, err.Error())
  303. return
  304. }
  305. options, err := readPoolConfig(group)
  306. if err != nil {
  307. utils.SendErrorResponse(w, err.Error())
  308. return
  309. }
  310. if err := writePoolConfig(group, append(options, newFsOption)); err != nil {
  311. utils.SendErrorResponse(w, err.Error())
  312. return
  313. }
  314. if rule != nil {
  315. if err := driveAccessStore.Set(newFsOption.Uuid, *rule); err != nil {
  316. utils.SendErrorResponse(w, err.Error())
  317. return
  318. }
  319. }
  320. sendMountResult(w, mountDrive(group, newFsOption))
  321. driveAccessChanged()
  322. }
  323. // validateNewDrive checks a drive about to be added. Drive ids are global:
  324. // the access list and every path lookup find a drive by its id alone.
  325. func validateNewDrive(option *fs.FileSystemOption, pools []poolState) error {
  326. if err := fs.ValidateOption(option); err != nil {
  327. return err
  328. }
  329. if !driveIDFormat.MatchString(option.Uuid) {
  330. return errors.New("A drive id can only contain letters, numbers, - and _")
  331. }
  332. for _, pool := range pools {
  333. if pool.holds(option.Uuid) {
  334. return errors.New("Drive id " + option.Uuid + " is already used in the " + pool.owner + " pool")
  335. }
  336. }
  337. return nil
  338. }
  339. // Get the FSH configuration for the given group and uuid
  340. func getFSHConfigFromGroupAndUUID(group string, uuid string) (*fs.FileSystemOption, error) {
  341. options, err := readPoolConfig(group)
  342. if err != nil {
  343. return nil, err
  344. }
  345. option := findPoolOption(options, uuid)
  346. if option == nil {
  347. return nil, errors.New("No FSH config found with the uuid")
  348. }
  349. return option, nil
  350. }
  351. // setFSHConfigByGroupAndId replaces a config row in place, keeping the stored
  352. // login when the form left it empty. The stored login is written back into
  353. // options, which the caller goes on to mount.
  354. func setFSHConfigByGroupAndId(group string, uuid string, options *fs.FileSystemOption) error {
  355. loadedConfig, err := readPoolConfig(group)
  356. if err != nil {
  357. return err
  358. }
  359. current := findPoolOption(loadedConfig, uuid)
  360. if current == nil {
  361. return errors.New("No FSH config found with the uuid")
  362. }
  363. if options.Username == "" {
  364. options.Username = current.Username
  365. }
  366. if options.Password == "" {
  367. options.Password = current.Password
  368. }
  369. if err := fs.ValidateOption(options); err != nil {
  370. return err
  371. }
  372. *current = *options
  373. return writePoolConfig(group, loadedConfig)
  374. }
  375. // Handle Storage Pool toggle on-off
  376. func HandleFSHToggle(w http.ResponseWriter, r *http.Request) {
  377. fsh, _ := utils.PostPara(r, "fsh")
  378. if fsh == "" {
  379. utils.SendErrorResponse(w, "Invalid File System Handler ID")
  380. return
  381. }
  382. group, _ := utils.PostPara(r, "group")
  383. if group == "" {
  384. utils.SendErrorResponse(w, "Invalid group ID")
  385. return
  386. }
  387. //Check if group exists
  388. if !storagePoolKnown(group) {
  389. utils.SendErrorResponse(w, "Group not exists")
  390. return
  391. }
  392. //Not allow to modify system reserved fsh
  393. if access.IsBuiltin(fsh) {
  394. utils.SendErrorResponse(w, "Cannot toggle system reserved File System Handler")
  395. return
  396. }
  397. storagePool, err := GetStoragePoolByOwner(group)
  398. if err != nil {
  399. utils.SendErrorResponse(w, err.Error())
  400. return
  401. }
  402. targetFSH, err := storagePool.GetFsHandlerByUUID(fsh)
  403. if err != nil {
  404. utils.SendErrorResponse(w, "Target File System Handler not found, given: "+fsh)
  405. return
  406. }
  407. targetFSH.Closed = !targetFSH.Closed
  408. //Return ok
  409. utils.SendOK(w)
  410. }
  411. // reloadBasePool closes and reopens the system pool, built-in drives included
  412. func reloadBasePool() {
  413. baseStoragePool.Close()
  414. emptyPool := storage.StoragePool{}
  415. baseStoragePool = &emptyPool
  416. //Start BasePool again
  417. err := LoadBaseStoragePool()
  418. if err != nil {
  419. systemWideLogger.PrintAndLog("Storage", err.Error(), err)
  420. return
  421. }
  422. //Update userHandler's basePool
  423. userHandler.UpdateStoragePool(baseStoragePool)
  424. }
  425. // Handle reload of storage pool
  426. func HandleStoragePoolReload(w http.ResponseWriter, r *http.Request) {
  427. pool, _ := utils.PostPara(r, "pool")
  428. switch pool {
  429. case reloadAllPools:
  430. reloadBasePool()
  431. for _, pg := range permissionHandler.PermissionGroups {
  432. reloadGroupPool(pg.Name)
  433. }
  434. case storagePoolSystem:
  435. reloadBasePool()
  436. default:
  437. if !permissionHandler.GroupExists(pool) {
  438. utils.SendErrorResponse(w, "Permission Pool owner not exists")
  439. return
  440. }
  441. reloadGroupPool(pool)
  442. }
  443. driveAccessChanged()
  444. utils.SendOK(w)
  445. }
  446. // reloadGroupPool closes and reopens the pool of a permission group
  447. func reloadGroupPool(group string) {
  448. systemWideLogger.PrintAndLog("Storage", "Reloading Storage Pool for: "+group, nil)
  449. pg := permissionHandler.GetPermissionGroupByName(group)
  450. //Pool should be exists. Close it
  451. pg.StoragePool.Close()
  452. //Create an empty pool for this permission group
  453. newEmptyPool := storage.StoragePool{}
  454. pg.StoragePool = &newEmptyPool
  455. //Recreate a new pool for this permission group
  456. //If there is no handler in config, the empty one will be kept
  457. LoadStoragePoolForGroup(pg)
  458. }
  459. func HandleStoragePoolRemove(w http.ResponseWriter, r *http.Request) {
  460. groupname, err := utils.PostPara(r, "group")
  461. if err != nil {
  462. utils.SendErrorResponse(w, "group not defined")
  463. return
  464. }
  465. uuid, err := utils.PostPara(r, "uuid")
  466. if err != nil {
  467. utils.SendErrorResponse(w, "File system handler UUID not defined")
  468. return
  469. }
  470. if access.IsBuiltin(uuid) {
  471. utils.SendErrorResponse(w, "Cannot remove system reserved file system handlers")
  472. return
  473. }
  474. if !storagePoolKnown(groupname) {
  475. utils.SendErrorResponse(w, "Group not exists")
  476. return
  477. }
  478. //Generate new confic by filtering
  479. oldConfigs, err := readPoolConfig(groupname)
  480. if err != nil {
  481. utils.SendErrorResponse(w, "Failed to parse original config file")
  482. return
  483. }
  484. newConfigs := []fs.FileSystemOption{}
  485. for _, config := range oldConfigs {
  486. if config.Uuid != uuid {
  487. newConfigs = append(newConfigs, config)
  488. }
  489. }
  490. if err := writePoolConfig(groupname, newConfigs); err != nil {
  491. utils.SendErrorResponse(w, err.Error())
  492. return
  493. }
  494. unmountDrive(groupname, uuid)
  495. //The rule goes with the last drive of that id
  496. if _, stillUsed := drivePoolHome(uuid); !stillUsed {
  497. if err := driveAccessStore.Remove(uuid); err != nil {
  498. systemWideLogger.PrintAndLog("Storage", "Unable to remove the access rule of "+uuid+":/", err)
  499. }
  500. }
  501. driveAccessChanged()
  502. utils.SendOK(w)
  503. }
  504. /*
  505. Testing a drive before saving it
  506. */
  507. // HandleFSHTest tries the settings of the add and edit drive form
  508. func HandleFSHTest(w http.ResponseWriter, r *http.Request) {
  509. config, err := utils.PostPara(r, "config")
  510. if err != nil {
  511. utils.SendErrorResponse(w, "Invalid config")
  512. return
  513. }
  514. option, err := buildOptionFromRequestForm(config)
  515. if err != nil {
  516. utils.SendErrorResponse(w, err.Error())
  517. return
  518. }
  519. if !arozfs.IsNetworkDrive(option.Filesystem) {
  520. if !fs.FileExists(option.Path) {
  521. utils.SendErrorResponse(w, "Path not exists, given: "+option.Path)
  522. return
  523. }
  524. utils.SendOK(w)
  525. return
  526. }
  527. //Editing a drive leaves the login fields empty to keep the stored login
  528. group, _ := utils.PostPara(r, "group")
  529. if option.Username == "" && option.Password == "" && storagePoolKnown(group) {
  530. if stored, err := getFSHConfigFromGroupAndUUID(group, option.Uuid); err == nil {
  531. option.Username = stored.Username
  532. option.Password = stored.Password
  533. }
  534. }
  535. if err := testNetworkDrive(option); err != nil {
  536. utils.SendErrorResponse(w, err.Error())
  537. return
  538. }
  539. utils.SendOK(w)
  540. }
  541. // testNetworkDrive connects to a network drive and disconnects again. A server
  542. // that does not answer must not hold the request up for as long as its
  543. // connection attempt takes.
  544. func testNetworkDrive(option fs.FileSystemOption) error {
  545. if option.Uuid == "" {
  546. option.Uuid = "connectiontest"
  547. }
  548. option.Automount = false
  549. result := make(chan error, 1)
  550. go func() {
  551. handler, err := fs.NewFileSystemHandler(option, fs.RuntimePersistenceConfig{
  552. LocalBufferPath: *tmp_directory,
  553. })
  554. if err == nil {
  555. handler.Close()
  556. }
  557. result <- err
  558. }()
  559. select {
  560. case err := <-result:
  561. return err
  562. case <-time.After(driveTestTimeout):
  563. return errors.New("The server did not answer in time")
  564. }
  565. }
  566. /*
  567. The drive list
  568. The state of a drive used to be guessed by the page from the running pools
  569. and the raw config files, which sent the stored logins to the browser and
  570. called every drive built by code "Removed". It is worked out here instead.
  571. */
  572. // poolState is what is known about one pool, running and configured
  573. type poolState struct {
  574. owner string
  575. live []*fs.FileSystemHandler
  576. config []fs.FileSystemOption
  577. }
  578. // holds reports whether this pool runs or configures a drive
  579. func (p poolState) holds(uuid string) bool {
  580. for _, fsh := range p.live {
  581. if fsh.UUID == uuid {
  582. return true
  583. }
  584. }
  585. return findPoolOption(p.config, uuid) != nil
  586. }
  587. // collectPoolStates reads every pool, the system pool first
  588. func collectPoolStates() []poolState {
  589. states := []poolState{}
  590. for _, pool := range GetAllStoragePools() {
  591. state := poolState{owner: pool.Owner, live: pool.Storages}
  592. if options, err := readPoolConfig(pool.Owner); err == nil {
  593. state.config = options
  594. }
  595. states = append(states, state)
  596. }
  597. return states
  598. }
  599. // duplicateDriveIDs lists the drive ids used by more than one pool
  600. func duplicateDriveIDs(pools []poolState) []string {
  601. useCount := map[string]int{}
  602. for _, pool := range pools {
  603. seen := map[string]bool{}
  604. for _, fsh := range pool.live {
  605. seen[fsh.UUID] = true
  606. }
  607. for _, option := range pool.config {
  608. seen[option.Uuid] = true
  609. }
  610. for uuid := range seen {
  611. useCount[uuid]++
  612. }
  613. }
  614. duplicates := []string{}
  615. for uuid, count := range useCount {
  616. if count > 1 {
  617. duplicates = append(duplicates, uuid)
  618. }
  619. }
  620. sort.Strings(duplicates)
  621. return duplicates
  622. }
  623. // driveInfo is one drive of the Storage Pools page. It never carries a login.
  624. type driveInfo struct {
  625. UUID string `json:"uuid"`
  626. Name string `json:"name"`
  627. Pool string `json:"pool"`
  628. Kind string `json:"kind"`
  629. State string `json:"state"`
  630. Path string `json:"path"`
  631. Filesystem string `json:"filesystem"`
  632. Hierarchy string `json:"hierarchy"`
  633. ReadOnly bool `json:"readOnly"`
  634. Duplicate bool `json:"duplicate"`
  635. Rule access.Rule `json:"rule"`
  636. Editable bool `json:"ruleEditable"`
  637. Replicated bool `json:"ruleReplicated"`
  638. }
  639. func driveKindOf(uuid string) string {
  640. switch {
  641. case access.IsFixed(uuid):
  642. return driveKindFixed
  643. case access.IsBuiltin(uuid):
  644. return driveKindBuiltin
  645. default:
  646. return driveKindConfigured
  647. }
  648. }
  649. // homeOfPool turns a pool owner into the home group of its drives
  650. func homeOfPool(owner string) string {
  651. if owner == storagePoolSystem {
  652. return ""
  653. }
  654. return owner
  655. }
  656. // buildDriveList lists every drive of the given pools with its state and rule
  657. func buildDriveList(pools []poolState, rules access.Source) []driveInfo {
  658. duplicates := map[string]bool{}
  659. for _, uuid := range duplicateDriveIDs(pools) {
  660. duplicates[uuid] = true
  661. }
  662. drives := []driveInfo{}
  663. for _, pool := range pools {
  664. home := homeOfPool(pool.owner)
  665. live := map[string]bool{}
  666. for _, fsh := range pool.live {
  667. live[fsh.UUID] = true
  668. kind := driveKindOf(fsh.UUID)
  669. state := driveStateOnline
  670. if fsh.Closed {
  671. state = driveStateStopped
  672. } else if kind == driveKindConfigured && findPoolOption(pool.config, fsh.UUID) == nil {
  673. state = driveStateRemoved
  674. }
  675. drives = append(drives, driveInfo{
  676. UUID: fsh.UUID,
  677. Name: fsh.Name,
  678. Pool: pool.owner,
  679. Kind: kind,
  680. State: state,
  681. Path: fsh.Path,
  682. Filesystem: fsh.Filesystem,
  683. Hierarchy: fsh.Hierarchy,
  684. ReadOnly: fsh.ReadOnly,
  685. Duplicate: duplicates[fsh.UUID],
  686. Rule: effectiveDriveRule(rules, fsh.UUID, home),
  687. Editable: kind != driveKindFixed,
  688. })
  689. }
  690. for _, option := range pool.config {
  691. if live[option.Uuid] {
  692. continue
  693. }
  694. drives = append(drives, driveInfo{
  695. UUID: option.Uuid,
  696. Name: option.Name,
  697. Pool: pool.owner,
  698. Kind: driveKindConfigured,
  699. State: driveStateOffline,
  700. Path: option.Path,
  701. Filesystem: option.Filesystem,
  702. Hierarchy: option.Hierarchy,
  703. ReadOnly: option.Access == arozfs.FsReadOnly,
  704. Duplicate: duplicates[option.Uuid],
  705. Rule: effectiveDriveRule(rules, option.Uuid, home),
  706. Editable: true,
  707. })
  708. }
  709. }
  710. return drives
  711. }
  712. // applyClusterDriveState completes the row of cluster:/. Its rule belongs to
  713. // the cluster, and a node that is not in one has nowhere to save it. Without
  714. // a mounted drive it is listed anyway, so an admin can tell it exists.
  715. func applyClusterDriveState(drives []driveInfo, inCluster bool, rules access.Source) []driveInfo {
  716. for i := range drives {
  717. if drives[i].UUID == access.ClusterDriveID {
  718. drives[i].Editable = inCluster
  719. drives[i].Replicated = true
  720. return drives
  721. }
  722. }
  723. return append(drives, driveInfo{
  724. UUID: access.ClusterDriveID,
  725. Name: "Cluster",
  726. Pool: storagePoolSystem,
  727. Kind: driveKindBuiltin,
  728. State: driveStateNotActive,
  729. Path: access.ClusterDriveID + ":/",
  730. Filesystem: "cluster",
  731. Hierarchy: "public",
  732. Rule: effectiveDriveRule(rules, access.ClusterDriveID, ""),
  733. Editable: inCluster,
  734. Replicated: true,
  735. })
  736. }
  737. // HandleListDrives returns every drive of this host with its state and rule,
  738. // and the permission groups a rule can name
  739. func HandleListDrives(w http.ResponseWriter, r *http.Request) {
  740. type groupInfo struct {
  741. Name string `json:"name"`
  742. IsAdmin bool `json:"isAdmin"`
  743. }
  744. rules := driveRuleSource{}
  745. drives := buildDriveList(collectPoolStates(), rules)
  746. if !*disable_cluster {
  747. drives = applyClusterDriveState(drives, clusterManager != nil && clusterManager.InCluster(), rules)
  748. }
  749. groups := []groupInfo{}
  750. for _, pg := range permissionHandler.PermissionGroups {
  751. groups = append(groups, groupInfo{Name: pg.Name, IsAdmin: pg.IsAdmin})
  752. }
  753. result := struct {
  754. Groups []groupInfo `json:"groups"`
  755. Drives []driveInfo `json:"drives"`
  756. RuleError string `json:"ruleError,omitempty"`
  757. }{Groups: groups, Drives: drives}
  758. if err := driveAccessStore.Err(); err != nil {
  759. result.RuleError = err.Error()
  760. }
  761. js, err := json.Marshal(result)
  762. if err != nil {
  763. utils.SendErrorResponse(w, err.Error())
  764. return
  765. }
  766. utils.SendJSONResponse(w, string(js))
  767. }
  768. // HandleDriveAccess reads (GET) or saves (POST) the access list of a drive
  769. func HandleDriveAccess(w http.ResponseWriter, r *http.Request) {
  770. if r.Method != http.MethodPost {
  771. uuid, err := utils.GetPara(r, "uuid")
  772. if err != nil {
  773. utils.SendErrorResponse(w, "Invalid drive id")
  774. return
  775. }
  776. home, known := drivePoolHome(uuid)
  777. if !known && uuid != access.ClusterDriveID {
  778. utils.SendErrorResponse(w, "Drive not found: "+uuid)
  779. return
  780. }
  781. js, _ := json.Marshal(effectiveDriveRule(driveRuleSource{}, uuid, home))
  782. utils.SendJSONResponse(w, string(js))
  783. return
  784. }
  785. uuid, err := utils.PostPara(r, "uuid")
  786. if err != nil {
  787. utils.SendErrorResponse(w, "Invalid drive id")
  788. return
  789. }
  790. ruleJSON, err := utils.PostPara(r, "rule")
  791. if err != nil {
  792. utils.SendErrorResponse(w, "Invalid access list")
  793. return
  794. }
  795. rule := access.Rule{}
  796. if err := json.Unmarshal([]byte(ruleJSON), &rule); err != nil {
  797. utils.SendErrorResponse(w, "Invalid access list")
  798. return
  799. }
  800. home, known := drivePoolHome(uuid)
  801. if !known && uuid != access.ClusterDriveID {
  802. utils.SendErrorResponse(w, "Drive not found: "+uuid)
  803. return
  804. }
  805. if err := prepareDriveRule(uuid, home, &rule); err != nil {
  806. utils.SendErrorResponse(w, err.Error())
  807. return
  808. }
  809. if err := setDriveRule(uuid, rule); err != nil {
  810. utils.SendErrorResponse(w, err.Error())
  811. return
  812. }
  813. driveAccessChanged()
  814. utils.SendOK(w)
  815. }
  816. // prepareDriveRule checks a rule against the drive it is for. A row for the
  817. // home group would change nothing, those members always have full access.
  818. func prepareDriveRule(uuid string, home string, rule *access.Rule) error {
  819. if access.IsFixed(uuid) {
  820. return errors.New(uuid + ":/ is open to everyone and has no access list")
  821. }
  822. if err := rule.Validate(); err != nil {
  823. return err
  824. }
  825. if uuid == access.ShareDriveID {
  826. //The drive is read only, so it is either visible or hidden
  827. if rule.Other == access.ReadWrite {
  828. return errors.New("share:/ is read only")
  829. }
  830. for _, level := range rule.Groups {
  831. if level == access.ReadWrite {
  832. return errors.New("share:/ is read only")
  833. }
  834. }
  835. }
  836. if home != "" {
  837. delete(rule.Groups, home)
  838. }
  839. return nil
  840. }
  841. // HandleDriveAccessExplain shows which rule decided what a user may do on a drive
  842. func HandleDriveAccessExplain(w http.ResponseWriter, r *http.Request) {
  843. username, err := utils.GetPara(r, "user")
  844. if err != nil {
  845. utils.SendErrorResponse(w, "Invalid user")
  846. return
  847. }
  848. uuid, err := utils.GetPara(r, "drive")
  849. if err != nil {
  850. utils.SendErrorResponse(w, "Invalid drive id")
  851. return
  852. }
  853. userinfo, err := userHandler.GetUserInfoFromUsername(username)
  854. if err != nil {
  855. utils.SendErrorResponse(w, "User not found: "+username)
  856. return
  857. }
  858. decision, found := userinfo.ExplainDriveAccess(uuid)
  859. if !found {
  860. utils.SendErrorResponse(w, "Drive not found: "+uuid)
  861. return
  862. }
  863. js, _ := json.Marshal(decision)
  864. utils.SendJSONResponse(w, string(js))
  865. }