db.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. package configserver
  2. import (
  3. "bytes"
  4. "database/sql"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "net"
  9. "time"
  10. "git.aetherial.dev/aeth/yosai/pkg/config"
  11. )
  12. var (
  13. ErrDuplicate = errors.New("record already exists")
  14. ErrNotExists = errors.New("row not exists")
  15. ErrUpdateFailed = errors.New("update failed")
  16. ErrDeleteFailed = errors.New("delete failed")
  17. )
  18. type DatabaseIO interface {
  19. Migrate()
  20. AddUser(config.Username) (config.User, error)
  21. UpdateUser(config.Username, config.Configuration) error
  22. Log(...string)
  23. GetConfigByUser(config.Username) (config.Configuration, error)
  24. }
  25. type SQLiteRepo struct {
  26. db *sql.DB
  27. out io.Writer
  28. }
  29. /*
  30. Create a new SQL lite repo
  31. :param db: a pointer to a sql.DB to write the database into
  32. */
  33. func NewSQLiteRepo(db *sql.DB, out io.Writer) *SQLiteRepo {
  34. return &SQLiteRepo{
  35. db: db,
  36. out: out,
  37. }
  38. }
  39. func (s *SQLiteRepo) Log(msg ...string) {
  40. data := "config.SQLiteRepo: "
  41. for i := range msg {
  42. data = data + " " + msg[i] + "\n"
  43. }
  44. s.out.Write([]byte(fmt.Sprintf(LogMsgTmpl, time.Now().String(), data)))
  45. }
  46. func (s *SQLiteRepo) Migrate() {
  47. userTable := `
  48. CREATE TABLE IF NOT EXISTS users(
  49. id SERIAL PRIMARY KEY,
  50. name TEXT NOT NULL
  51. );
  52. `
  53. cloudTable := `
  54. CREATE TABLE IF NOT EXISTS cloud(
  55. user_id INTEGER NOT NULL,
  56. image TEXT NOT NULL,
  57. region TEXT NOT NULL,
  58. linode_type TEXT NOT NULL
  59. );
  60. `
  61. ansibleTable := `
  62. CREATE TABLE IF NOT EXISTS ansible(
  63. user_id INTEGER NOT NULL,
  64. repo_url TEXT NOT NULL,
  65. branch TEXT NOT NULL,
  66. playbook_name TEXT NOT NULL,
  67. ansible_backend TEXT NOT NULL,
  68. ansible_backend_url TEXT NOT NULL
  69. );
  70. `
  71. serverTable := `
  72. CREATE TABLE IF NOT EXISTS servers(
  73. user_id INTEGER NOT NULL,
  74. name TEXT NOT NULL,
  75. wan_ipv4 TEXT NOT NULL,
  76. vpn_ipv4 TEXT NOT NULL
  77. );
  78. `
  79. clientTable := `
  80. CREATE TABLE IF NOT EXISTS clients(
  81. user_id INTEGER NOT NULL,
  82. name TEXT NOT NULL,
  83. pubkey TEXT NOT NULL,
  84. vpn_ipv4 TEXT NOT NULL,
  85. default_client BOOLEAN NOT NULL
  86. );
  87. `
  88. serviceTable := `
  89. CREATE TABLE IF NOT EXISTS service(
  90. user_id INTEGER NOT NULL,
  91. vpn_ip TEXT NOT NULL,
  92. vpn_subnet_mask INTEGER NOT NULL,
  93. vpn_server_port INTEGER NOT NULL,
  94. secrets_backend TEXT NOT NULL,
  95. secrets_backend_url TEXT NOT NULL
  96. );
  97. `
  98. queries := []string{
  99. userTable,
  100. cloudTable,
  101. ansibleTable,
  102. serverTable,
  103. clientTable,
  104. serviceTable,
  105. }
  106. for i := range queries {
  107. _, err := s.db.Exec(queries[i])
  108. if err != nil {
  109. s.Log(err.Error())
  110. }
  111. }
  112. }
  113. /*
  114. Retrieve a user struct from, querying by their username
  115. :param name: the username of the querying user Note -> must validate the username before calling
  116. */
  117. func (s *SQLiteRepo) GetUser(name config.Username) (config.User, error) {
  118. row := s.db.QueryRow("SELECT * FROM users WHERE name = $1", name)
  119. var user config.User
  120. if err := row.Scan(&user.Id, &user.Name); err != nil {
  121. if errors.Is(err, sql.ErrNoRows) {
  122. return user, ErrNotExists
  123. }
  124. return user, err
  125. }
  126. return user, nil
  127. }
  128. /*
  129. Update all of the data for a users configuration
  130. :param config: a config.Configuration to put into the database
  131. :param user: the config.User struct representing the calling user
  132. */
  133. func (s *SQLiteRepo) UpdateUser(username config.Username, config config.Configuration) error {
  134. trx, err := s.db.Begin()
  135. if err != nil {
  136. s.Log("Error creating DB transaction: ", err.Error())
  137. return err
  138. }
  139. defer trx.Rollback()
  140. user, err := s.GetUser(username)
  141. if err != nil {
  142. s.Log("Error getting the user: ", string(username), err.Error())
  143. return err
  144. }
  145. _, err = trx.Exec("UPDATE cloud SET image = $1, region = $2, linode_type = $3 WHERE user_id = $4",
  146. config.Cloud.Image,
  147. config.Cloud.Region,
  148. config.Cloud.LinodeType,
  149. user.Id)
  150. if err != nil {
  151. return err
  152. }
  153. _, err = trx.Exec("UPDATE ansible SET repo_url = $1, branch = $2, playbook_name = $3, ansible_backend = $4, ansible_backend_url = $5 WHERE user_id = $6",
  154. config.Ansible.Repo,
  155. config.Ansible.Branch,
  156. config.Ansible.PlaybookName,
  157. config.Service.AnsibleBackend,
  158. config.Service.AnsibleBackendUrl,
  159. user.Id)
  160. if err != nil {
  161. return err
  162. }
  163. _, err = trx.Exec("DELETE FROM servers WHERE user_id = $1", user.Id)
  164. if err != nil {
  165. s.Log("Failed to drop the users server entries: ", err.Error())
  166. return err
  167. }
  168. err = s.insertServer(user, config, trx)
  169. if err != nil {
  170. s.Log("Failed to propogate the VPN servers into the appropriate table: ", err.Error())
  171. return err
  172. }
  173. _, err = trx.Exec("DELETE FROM clients WHERE user_id = $1", user.Id)
  174. if err != nil {
  175. s.Log("Failed to drop the users client entries: ", err.Error())
  176. return err
  177. }
  178. err = s.insertClient(user, config, trx)
  179. if err != nil {
  180. s.Log("Failed to propogate the VPN clients into the appropriate table: ", err.Error())
  181. return err
  182. }
  183. _, err = trx.Exec("UPDATE service SET vpn_ip = $1, vpn_subnet_mask = $2, vpn_server_port = $3, secrets_backend = $4, secrets_backend_url = $5 WHERE user_id = $6",
  184. config.Service.VpnAddressSpace.String(),
  185. config.Service.VpnMask,
  186. config.Service.VpnServerPort,
  187. config.Service.SecretsBackend,
  188. config.Service.SecretsBackendUrl,
  189. user.Id)
  190. if err != nil {
  191. return err
  192. }
  193. err = trx.Commit()
  194. if err != nil {
  195. return err
  196. }
  197. s.Log("Transaction commited.")
  198. return nil
  199. }
  200. /*
  201. Create an entry in the vpn information table
  202. :param user: the calling config.User
  203. :param config: the config.Configuration with the configuration data
  204. */
  205. func (s *SQLiteRepo) insertServiceInfo(user config.User, config config.Configuration, trx *sql.Tx) error {
  206. rows, err := trx.Query("SELECT * FROM service WHERE user_id = $1", user.Id)
  207. if err != nil {
  208. s.Log("Failed to perform pre-insert check", err.Error())
  209. return err
  210. }
  211. if rows.Next() { // Checking if the 'length' of returned rows is non-zero
  212. s.Log("Duplicate INSERT attempted, update instead.", err.Error())
  213. return ErrDuplicate
  214. }
  215. _, err = trx.Exec("INSERT INTO service(user_id, vpn_ip, vpn_subnet_mask, vpn_server_port, secrets_backend, secrets_backend_url) values($1,$2,$3,$4,$5,$6)",
  216. user.Id,
  217. config.Service.VpnAddressSpace.String(),
  218. config.Service.VpnMask,
  219. config.Service.VpnServerPort,
  220. config.Service.SecretsBackend,
  221. config.Service.SecretsBackendUrl)
  222. if err != nil {
  223. s.Log(string(user.Id), config.Service.VpnAddressSpace.String(), string(config.Service.VpnMask), string(config.Service.VpnServerPort), config.Service.SecretsBackend, config.Service.SecretsBackendUrl)
  224. s.Log("Error occured writing to the database: ", err.Error())
  225. return err
  226. }
  227. return nil
  228. }
  229. /*
  230. Create an entry in the client table for a user
  231. :param user: the calling config.User
  232. :param cloudConfig: the cloud specific configuration for the user
  233. */
  234. func (s *SQLiteRepo) insertClient(user config.User, config config.Configuration, trx *sql.Tx) error {
  235. rows, err := trx.Query("SELECT * FROM clients WHERE user_id = $1", user.Id)
  236. if err != nil {
  237. s.Log("Failed to perform pre-insert check", err.Error())
  238. return err
  239. }
  240. if rows.Next() { // Checking if the 'length' of returned rows is non-zero
  241. s.Log("Duplicate INSERT attempted, update instead.", err.Error())
  242. return ErrDuplicate
  243. }
  244. for i := range config.Service.Clients {
  245. client := config.Service.Clients[i]
  246. _, err = trx.Exec("INSERT INTO clients(user_id, name, pubkey, vpn_ipv4, default_client) values($1,$2,$3,$4,$5)",
  247. user.Id,
  248. client.Name,
  249. client.Pubkey,
  250. client.VpnIpv4.String(),
  251. client.Default)
  252. if err != nil {
  253. s.Log(fmt.Sprintf("%+v\n", client), fmt.Sprintf("%+v\n", user))
  254. s.Log("Failed to create row: ", err.Error())
  255. return err
  256. }
  257. }
  258. return nil
  259. }
  260. /*
  261. Create an entry in the server table for a user
  262. :param user: the calling config.User
  263. :param cloudConfig: the cloud specific configuration for the user
  264. */
  265. func (s *SQLiteRepo) insertServer(user config.User, config config.Configuration, trx *sql.Tx) error {
  266. rows, err := trx.Query("SELECT * FROM servers WHERE user_id = $1", user.Id)
  267. if err != nil {
  268. s.Log("Failed to perform pre-insert check", err.Error())
  269. return err
  270. }
  271. if rows.Next() { // Checking if the 'length' of returned rows is non-zero
  272. s.Log("Duplicate INSERT attempted, update instead.", err.Error())
  273. return ErrDuplicate
  274. }
  275. for i := range config.Service.Servers {
  276. server := config.Service.Servers[i]
  277. _, err = trx.Exec("INSERT INTO servers(user_id, name, wan_ipv4, vpn_ipv4) values($1,$2,$3,$4)",
  278. user.Id,
  279. server.Name,
  280. server.WanIpv4,
  281. server.VpnIpv4.String())
  282. if err != nil {
  283. s.Log("Failed to create row: ", err.Error())
  284. return err
  285. }
  286. }
  287. return nil
  288. }
  289. /*
  290. Create an entry in the ansible table for a user
  291. :param user: the calling config.User
  292. :param cloudConfig: the cloud specific configuration for the user
  293. */
  294. func (s *SQLiteRepo) insertUserAnsible(user config.User, config config.Configuration, trx *sql.Tx) error {
  295. rows, err := trx.Query("SELECT * FROM ansible WHERE user_id = $1", user.Id)
  296. if err != nil {
  297. s.Log("Failed to perform pre-insert check", err.Error())
  298. return err
  299. }
  300. if rows.Next() { // Checking if the 'length' of returned rows is non-zero
  301. s.Log("Duplicate INSERT attempted, update instead.", err.Error())
  302. return ErrDuplicate
  303. }
  304. _, err = trx.Exec("INSERT INTO ansible(user_id, repo_url, branch, playbook_name, ansible_backend, ansible_backend_url) values($1,$2,$3,$4,$5,$6)",
  305. user.Id,
  306. config.Ansible.Repo,
  307. config.Ansible.Branch,
  308. config.Ansible.PlaybookName,
  309. config.Service.AnsibleBackend,
  310. config.Service.AnsibleBackendUrl)
  311. if err != nil {
  312. s.Log("Failed to create row: ", err.Error())
  313. return err
  314. }
  315. return nil
  316. }
  317. /*
  318. Create an entry in the cloud table for a user
  319. :param user: the calling config.User
  320. :param cloudConfig: the cloud specific configuration for the user
  321. */
  322. func (s *SQLiteRepo) insertUserCloud(user config.User, config config.Configuration, trx *sql.Tx) error {
  323. rows, err := trx.Query("SELECT * FROM cloud WHERE user_id = $1", user.Id)
  324. if err != nil {
  325. s.Log("Failed to perform pre-insert check", err.Error())
  326. return err
  327. }
  328. if rows.Next() { // Checking if the 'length' of returned rows is non-zero
  329. s.Log("Duplicate INSERT attempted, update instead.", err.Error())
  330. return ErrDuplicate
  331. }
  332. _, err = trx.Exec("INSERT INTO cloud(user_id, image, region, linode_type) values($1,$2,$3,$4)",
  333. user.Id,
  334. config.Cloud.Image,
  335. config.Cloud.Region,
  336. config.Cloud.LinodeType)
  337. if err != nil {
  338. s.Log("Failed to create row: ", err.Error())
  339. return err
  340. }
  341. return nil
  342. }
  343. /*
  344. Populate the different db tables with the users configuration
  345. :param user: the calling user
  346. :param config: the config.Configuration to populate into the db
  347. */
  348. func (s *SQLiteRepo) SeedUser(user config.User, cfg config.Configuration) error {
  349. trx, err := s.db.Begin()
  350. if err != nil {
  351. s.Log("Failed to spawn a transaction in SQLiteRepo.SeedUser: ", err.Error())
  352. return err
  353. }
  354. seedFuncs := []func(config.User, config.Configuration, *sql.Tx) error{
  355. s.insertClient,
  356. s.insertServer,
  357. s.insertUserAnsible,
  358. s.insertUserCloud,
  359. s.insertServiceInfo,
  360. }
  361. for i := range seedFuncs {
  362. err := seedFuncs[i](user, cfg, trx)
  363. if err != nil {
  364. return err
  365. }
  366. }
  367. err = trx.Commit()
  368. if err != nil {
  369. return err
  370. }
  371. s.Log("Transaction commited.")
  372. return nil
  373. }
  374. /*
  375. Add a user to the database and return a config.User struct
  376. :param name: the name of the user
  377. */
  378. func (s *SQLiteRepo) AddUser(name config.Username) (config.User, error) {
  379. var user config.User
  380. _, err := s.db.Exec("INSERT INTO users(name) values($1)", name)
  381. if err != nil {
  382. return user, err
  383. }
  384. return s.GetUser(name)
  385. }
  386. /*
  387. Get the configuration for the passed user
  388. :param user: the calling user
  389. */
  390. func (s *SQLiteRepo) GetConfigByUser(username config.Username) (config.Configuration, error) {
  391. cfg := config.NewConfiguration(bytes.NewBuffer([]byte{}), username)
  392. user, err := s.GetUser(username)
  393. if err != nil {
  394. return *cfg, err
  395. }
  396. row := s.db.QueryRow("SELECT * FROM cloud WHERE user_id = $1", user.Id)
  397. if err := row.Scan(&user.Id, &cfg.Cloud.Image, &cfg.Cloud.Region, &cfg.Cloud.LinodeType); err != nil {
  398. if errors.Is(err, sql.ErrNoRows) {
  399. return *cfg, ErrNotExists
  400. }
  401. return *cfg, err
  402. }
  403. row = s.db.QueryRow("SELECT * FROM ansible WHERE user_id = $1", user.Id)
  404. if err := row.Scan(
  405. &user.Id,
  406. &cfg.Ansible.Repo,
  407. &cfg.Ansible.Branch,
  408. &cfg.Ansible.PlaybookName,
  409. &cfg.Service.AnsibleBackend,
  410. &cfg.Service.AnsibleBackendUrl); err != nil {
  411. if errors.Is(err, sql.ErrNoRows) {
  412. return *cfg, ErrNotExists
  413. }
  414. return *cfg, err
  415. }
  416. rows, err := s.db.Query("SELECT * FROM servers WHERE user_id = $1", user.Id)
  417. if err != nil {
  418. return *cfg, err
  419. }
  420. for rows.Next() {
  421. var server config.VpnServer
  422. var ipStr string
  423. if err := rows.Scan(&user.Id, &server.Name, &server.WanIpv4, &ipStr); err != nil {
  424. return *cfg, err
  425. }
  426. s.Log(ipStr)
  427. ipParsed := net.ParseIP(ipStr)
  428. if ipParsed == nil {
  429. s.Log("Couldnt parse address: ", ipStr, err.Error())
  430. }
  431. server.VpnIpv4 = ipParsed
  432. cfg.Service.Servers[server.Name] = server
  433. }
  434. if err = rows.Err(); err != nil {
  435. return *cfg, err
  436. }
  437. rows, err = s.db.Query("SELECT * FROM clients WHERE user_id = $1", user.Id)
  438. if err != nil {
  439. return *cfg, err
  440. }
  441. for rows.Next() {
  442. var client config.VpnClient
  443. var ipStr string
  444. if err := rows.Scan(&user.Id, &client.Name, &client.Pubkey, &ipStr, &client.Default); err != nil {
  445. return *cfg, err
  446. }
  447. s.Log(ipStr)
  448. ipParsed := net.ParseIP(ipStr)
  449. if ipParsed == nil {
  450. s.Log("Couldnt parse address: ", ipStr, err.Error())
  451. }
  452. client.VpnIpv4 = ipParsed
  453. cfg.Service.Clients[client.Name] = client
  454. }
  455. row = s.db.QueryRow("SELECT * FROM service WHERE user_id = $1", user.Id)
  456. var vpnIp string
  457. if err = row.Scan(&user.Id, &vpnIp, &cfg.Service.VpnMask, &cfg.Service.VpnServerPort, &cfg.Service.SecretsBackend, &cfg.Service.SecretsBackendUrl); err != nil {
  458. return *cfg, err
  459. }
  460. _, vpnIpv4, _ := net.ParseCIDR(vpnIp)
  461. cfg.Service.VpnAddressSpace = *vpnIpv4
  462. return *cfg, nil
  463. }