database.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package database
  2. import (
  3. "crypto/rand"
  4. "encoding/json"
  5. "errors"
  6. "time"
  7. "github.com/google/uuid"
  8. "github.com/gravitl/netmaker/logger"
  9. "github.com/gravitl/netmaker/models"
  10. "github.com/gravitl/netmaker/netclient/ncutils"
  11. "github.com/gravitl/netmaker/servercfg"
  12. "golang.org/x/crypto/nacl/box"
  13. )
  14. const (
  15. // == Table Names ==
  16. // NETWORKS_TABLE_NAME - networks table
  17. NETWORKS_TABLE_NAME = "networks"
  18. // NODES_TABLE_NAME - nodes table
  19. NODES_TABLE_NAME = "nodes"
  20. // DELETED_NODES_TABLE_NAME - deleted nodes table
  21. DELETED_NODES_TABLE_NAME = "deletednodes"
  22. // USERS_TABLE_NAME - users table
  23. USERS_TABLE_NAME = "users"
  24. // CERTS_TABLE_NAME - certificates table
  25. CERTS_TABLE_NAME = "certs"
  26. // DNS_TABLE_NAME - dns table
  27. DNS_TABLE_NAME = "dns"
  28. // EXT_CLIENT_TABLE_NAME - ext client table
  29. EXT_CLIENT_TABLE_NAME = "extclients"
  30. // PEERS_TABLE_NAME - peers table
  31. PEERS_TABLE_NAME = "peers"
  32. // SERVERCONF_TABLE_NAME - stores server conf
  33. SERVERCONF_TABLE_NAME = "serverconf"
  34. // SERVER_UUID_TABLE_NAME - stores unique netmaker server data
  35. SERVER_UUID_TABLE_NAME = "serveruuid"
  36. // SERVER_UUID_RECORD_KEY - telemetry thing
  37. SERVER_UUID_RECORD_KEY = "serveruuid"
  38. // DATABASE_FILENAME - database file name
  39. DATABASE_FILENAME = "netmaker.db"
  40. // GENERATED_TABLE_NAME - stores server generated k/v
  41. GENERATED_TABLE_NAME = "generated"
  42. // NODE_ACLS_TABLE_NAME - stores the node ACL rules
  43. NODE_ACLS_TABLE_NAME = "nodeacls"
  44. // SSO_STATE_CACHE - holds sso session information for OAuth2 sign-ins
  45. SSO_STATE_CACHE = "ssostatecache"
  46. // METRICS_TABLE_NAME - stores network metrics
  47. METRICS_TABLE_NAME = "metrics"
  48. // NETWORK_USER_TABLE_NAME - network user table tracks stats for a network user per network
  49. NETWORK_USER_TABLE_NAME = "networkusers"
  50. // USER_GROUPS_TABLE_NAME - table for storing usergroups
  51. USER_GROUPS_TABLE_NAME = "usergroups"
  52. // CACHE_TABLE_NAME - caching table
  53. CACHE_TABLE_NAME = "cache"
  54. // HOSTS_TABLE_NAME - the table name for hosts
  55. HOSTS_TABLE_NAME = "hosts"
  56. // == ERROR CONSTS ==
  57. // NO_RECORD - no singular result found
  58. NO_RECORD = "no result found"
  59. // NO_RECORDS - no results found
  60. NO_RECORDS = "could not find any records"
  61. // == DB Constants ==
  62. // INIT_DB - initialize db
  63. INIT_DB = "init"
  64. // CREATE_TABLE - create table const
  65. CREATE_TABLE = "createtable"
  66. // INSERT - insert into db const
  67. INSERT = "insert"
  68. // INSERT_PEER - insert peer into db const
  69. INSERT_PEER = "insertpeer"
  70. // DELETE - delete db record const
  71. DELETE = "delete"
  72. // DELETE_ALL - delete a table const
  73. DELETE_ALL = "deleteall"
  74. // FETCH_ALL - fetch table contents const
  75. FETCH_ALL = "fetchall"
  76. // CLOSE_DB - graceful close of db const
  77. CLOSE_DB = "closedb"
  78. )
  79. func getCurrentDB() map[string]interface{} {
  80. switch servercfg.GetDB() {
  81. case "rqlite":
  82. return RQLITE_FUNCTIONS
  83. case "sqlite":
  84. return SQLITE_FUNCTIONS
  85. case "postgres":
  86. return PG_FUNCTIONS
  87. default:
  88. return SQLITE_FUNCTIONS
  89. }
  90. }
  91. // InitializeDatabase - initializes database
  92. func InitializeDatabase() error {
  93. logger.Log(0, "connecting to", servercfg.GetDB())
  94. tperiod := time.Now().Add(10 * time.Second)
  95. for {
  96. if err := getCurrentDB()[INIT_DB].(func() error)(); err != nil {
  97. logger.Log(0, "unable to connect to db, retrying . . .")
  98. if time.Now().After(tperiod) {
  99. return err
  100. }
  101. } else {
  102. break
  103. }
  104. time.Sleep(2 * time.Second)
  105. }
  106. createTables()
  107. return initializeUUID()
  108. }
  109. func createTables() {
  110. createTable(NETWORKS_TABLE_NAME)
  111. createTable(NODES_TABLE_NAME)
  112. createTable(CERTS_TABLE_NAME)
  113. createTable(DELETED_NODES_TABLE_NAME)
  114. createTable(USERS_TABLE_NAME)
  115. createTable(DNS_TABLE_NAME)
  116. createTable(EXT_CLIENT_TABLE_NAME)
  117. createTable(PEERS_TABLE_NAME)
  118. createTable(SERVERCONF_TABLE_NAME)
  119. createTable(SERVER_UUID_TABLE_NAME)
  120. createTable(GENERATED_TABLE_NAME)
  121. createTable(NODE_ACLS_TABLE_NAME)
  122. createTable(SSO_STATE_CACHE)
  123. createTable(METRICS_TABLE_NAME)
  124. createTable(NETWORK_USER_TABLE_NAME)
  125. createTable(USER_GROUPS_TABLE_NAME)
  126. createTable(CACHE_TABLE_NAME)
  127. createTable(HOSTS_TABLE_NAME)
  128. }
  129. func createTable(tableName string) error {
  130. return getCurrentDB()[CREATE_TABLE].(func(string) error)(tableName)
  131. }
  132. // IsJSONString - checks if valid json
  133. func IsJSONString(value string) bool {
  134. var jsonInt interface{}
  135. var nodeInt models.Node
  136. return json.Unmarshal([]byte(value), &jsonInt) == nil || json.Unmarshal([]byte(value), &nodeInt) == nil
  137. }
  138. // Insert - inserts object into db
  139. func Insert(key string, value string, tableName string) error {
  140. if key != "" && value != "" && IsJSONString(value) {
  141. return getCurrentDB()[INSERT].(func(string, string, string) error)(key, value, tableName)
  142. } else {
  143. return errors.New("invalid insert " + key + " : " + value)
  144. }
  145. }
  146. // InsertPeer - inserts peer into db
  147. func InsertPeer(key string, value string) error {
  148. if key != "" && value != "" && IsJSONString(value) {
  149. return getCurrentDB()[INSERT_PEER].(func(string, string) error)(key, value)
  150. } else {
  151. return errors.New("invalid peer insert " + key + " : " + value)
  152. }
  153. }
  154. // DeleteRecord - deletes a record from db
  155. func DeleteRecord(tableName string, key string) error {
  156. return getCurrentDB()[DELETE].(func(string, string) error)(tableName, key)
  157. }
  158. // DeleteAllRecords - removes a table and remakes
  159. func DeleteAllRecords(tableName string) error {
  160. err := getCurrentDB()[DELETE_ALL].(func(string) error)(tableName)
  161. if err != nil {
  162. return err
  163. }
  164. err = createTable(tableName)
  165. if err != nil {
  166. return err
  167. }
  168. return nil
  169. }
  170. // FetchRecord - fetches a record
  171. func FetchRecord(tableName string, key string) (string, error) {
  172. results, err := FetchRecords(tableName)
  173. if err != nil {
  174. return "", err
  175. }
  176. if results[key] == "" {
  177. return "", errors.New(NO_RECORD)
  178. }
  179. return results[key], nil
  180. }
  181. // FetchRecords - fetches all records in given table
  182. func FetchRecords(tableName string) (map[string]string, error) {
  183. return getCurrentDB()[FETCH_ALL].(func(string) (map[string]string, error))(tableName)
  184. }
  185. // initializeUUID - create a UUID record for server if none exists
  186. func initializeUUID() error {
  187. records, err := FetchRecords(SERVER_UUID_TABLE_NAME)
  188. if err != nil {
  189. if !IsEmptyRecord(err) {
  190. return err
  191. }
  192. } else if len(records) > 0 {
  193. return nil
  194. }
  195. // setup encryption keys
  196. var trafficPubKey, trafficPrivKey, errT = box.GenerateKey(rand.Reader) // generate traffic keys
  197. if errT != nil {
  198. return errT
  199. }
  200. tPriv, err := ncutils.ConvertKeyToBytes(trafficPrivKey)
  201. if err != nil {
  202. return err
  203. }
  204. tPub, err := ncutils.ConvertKeyToBytes(trafficPubKey)
  205. if err != nil {
  206. return err
  207. }
  208. telemetry := models.Telemetry{
  209. UUID: uuid.NewString(),
  210. TrafficKeyPriv: tPriv,
  211. TrafficKeyPub: tPub,
  212. }
  213. telJSON, err := json.Marshal(&telemetry)
  214. if err != nil {
  215. return err
  216. }
  217. return Insert(SERVER_UUID_RECORD_KEY, string(telJSON), SERVER_UUID_TABLE_NAME)
  218. }
  219. // CloseDB - closes a database gracefully
  220. func CloseDB() {
  221. getCurrentDB()[CLOSE_DB].(func())()
  222. }