database.go 8.2 KB

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