server.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package controller
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strings"
  6. "github.com/gorilla/mux"
  7. "github.com/gravitl/netmaker/logger"
  8. "github.com/gravitl/netmaker/logic"
  9. "github.com/gravitl/netmaker/models"
  10. "github.com/gravitl/netmaker/servercfg"
  11. "github.com/gravitl/netmaker/tls"
  12. )
  13. func serverHandlers(r *mux.Router) {
  14. // r.HandleFunc("/api/server/addnetwork/{network}", securityCheckServer(true, http.HandlerFunc(addNetwork))).Methods("POST")
  15. r.HandleFunc("/api/server/getconfig", securityCheckServer(false, http.HandlerFunc(getConfig))).Methods("GET")
  16. r.HandleFunc("/api/server/removenetwork/{network}", securityCheckServer(true, http.HandlerFunc(removeNetwork))).Methods("DELETE")
  17. r.HandleFunc("/api/server/register", http.HandlerFunc(register)).Methods("POST")
  18. }
  19. //Security check is middleware for every function and just checks to make sure that its the master calling
  20. //Only admin should have access to all these network-level actions
  21. //or maybe some Users once implemented
  22. func securityCheckServer(adminonly bool, next http.Handler) http.HandlerFunc {
  23. return func(w http.ResponseWriter, r *http.Request) {
  24. var errorResponse = models.ErrorResponse{
  25. Code: http.StatusInternalServerError, Message: "W1R3: It's not you it's me.",
  26. }
  27. bearerToken := r.Header.Get("Authorization")
  28. var tokenSplit = strings.Split(bearerToken, " ")
  29. var authToken = ""
  30. if len(tokenSplit) < 2 {
  31. errorResponse = models.ErrorResponse{
  32. Code: http.StatusUnauthorized, Message: "W1R3: You are unauthorized to access this endpoint.",
  33. }
  34. returnErrorResponse(w, r, errorResponse)
  35. return
  36. } else {
  37. authToken = tokenSplit[1]
  38. }
  39. //all endpoints here require master so not as complicated
  40. //still might not be a good way of doing this
  41. user, _, isadmin, err := logic.VerifyUserToken(authToken)
  42. errorResponse = models.ErrorResponse{
  43. Code: http.StatusUnauthorized, Message: "W1R3: You are unauthorized to access this endpoint.",
  44. }
  45. if !adminonly && (err != nil || user == "") {
  46. returnErrorResponse(w, r, errorResponse)
  47. return
  48. }
  49. if adminonly && !isadmin && !authenticateMaster(authToken) {
  50. returnErrorResponse(w, r, errorResponse)
  51. return
  52. }
  53. next.ServeHTTP(w, r)
  54. }
  55. }
  56. func removeNetwork(w http.ResponseWriter, r *http.Request) {
  57. // Set header
  58. w.Header().Set("Content-Type", "application/json")
  59. // get params
  60. var params = mux.Vars(r)
  61. err := logic.DeleteNetwork(params["network"])
  62. if err != nil {
  63. json.NewEncoder(w).Encode("Could not remove server from network " + params["network"])
  64. return
  65. }
  66. json.NewEncoder(w).Encode("Server removed from network " + params["network"])
  67. }
  68. func getConfig(w http.ResponseWriter, r *http.Request) {
  69. // Set header
  70. w.Header().Set("Content-Type", "application/json")
  71. // get params
  72. scfg := servercfg.GetServerConfig()
  73. json.NewEncoder(w).Encode(scfg)
  74. //w.WriteHeader(http.StatusOK)
  75. }
  76. // func addNetwork(w http.ResponseWriter, r *http.Request) {
  77. // // Set header
  78. // w.Header().Set("Content-Type", "application/json")
  79. // // get params
  80. // var params = mux.Vars(r)
  81. // var networkName = params["network"]
  82. // var networkSettings, err := logic.GetNetwork(netwnetworkName)
  83. // success, err := serverctl.AddNetwork(params["network"])
  84. // if err != nil || !success {
  85. // json.NewEncoder(w).Encode("Could not add server to network " + params["network"])
  86. // return
  87. // }
  88. // json.NewEncoder(w).Encode("Server added to network " + params["network"])
  89. // }
  90. // register - registers a client with the server and return the CA cert
  91. func register(w http.ResponseWriter, r *http.Request) {
  92. w.Header().Set("Content-Type", "application/json")
  93. bearerToken := r.Header.Get("Authorization")
  94. var tokenSplit = strings.Split(bearerToken, " ")
  95. var token = ""
  96. if len(tokenSplit) < 2 {
  97. errorResponse := models.ErrorResponse{
  98. Code: http.StatusUnauthorized, Message: "W1R3: You are unauthorized to access this endpoint.",
  99. }
  100. returnErrorResponse(w, r, errorResponse)
  101. return
  102. } else {
  103. token = tokenSplit[1]
  104. }
  105. found := false
  106. networks, err := logic.GetNetworks()
  107. if err != nil {
  108. logger.Log(3, "no networks")
  109. errorResponse := models.ErrorResponse{
  110. Code: http.StatusNotFound, Message: "no networks",
  111. }
  112. returnErrorResponse(w, r, errorResponse)
  113. }
  114. for _, network := range networks {
  115. for _, key := range network.AccessKeys {
  116. if key.AccessString == token {
  117. found = true
  118. break
  119. }
  120. }
  121. }
  122. if !found {
  123. logger.Log(2, "valid access key not found")
  124. errorResponse := models.ErrorResponse{
  125. Code: http.StatusUnauthorized, Message: "You are unauthorized to access this endpoint.",
  126. }
  127. returnErrorResponse(w, r, errorResponse)
  128. return
  129. }
  130. ca, err := tls.ReadCert("/etc/netmaker/root.pem")
  131. if err != nil {
  132. logger.Log(2, "root ca not found")
  133. errorResponse := models.ErrorResponse{
  134. Code: http.StatusNotFound, Message: "root ca not found",
  135. }
  136. returnErrorResponse(w, r, errorResponse)
  137. return
  138. //return err
  139. }
  140. w.WriteHeader(http.StatusOK)
  141. json.NewEncoder(w).Encode(*ca)
  142. }