controller_handlers.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package config
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "strings"
  9. "github.com/julienschmidt/httprouter"
  10. proto "github.com/opencost/opencost/core/pkg/protocol"
  11. "github.com/opencost/opencost/pkg/cloud"
  12. "github.com/opencost/opencost/pkg/cloud/aws"
  13. "github.com/opencost/opencost/pkg/cloud/azure"
  14. "github.com/opencost/opencost/pkg/cloud/gcp"
  15. "github.com/opencost/opencost/pkg/env"
  16. )
  17. var protocol = proto.HTTP()
  18. func (c *Controller) cloudCostChecks() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  19. // If Pipeline is nil, always return 503
  20. if c == nil {
  21. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  22. http.Error(w, "ConfigController: is nil", http.StatusServiceUnavailable)
  23. }
  24. }
  25. if !env.IsCloudCostEnabled() {
  26. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  27. http.Error(w, "ConfigController: is not enabled", http.StatusServiceUnavailable)
  28. }
  29. }
  30. return nil
  31. }
  32. // GetEnableConfigHandler creates a handler from a http request which enables an integration via the integrationController
  33. func (c *Controller) GetExportConfigHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  34. // perform basic checks to ensure that the pipeline can be accessed
  35. fn := c.cloudCostChecks()
  36. if fn != nil {
  37. return fn
  38. }
  39. // Return valid handler func
  40. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  41. w.Header().Set("Content-Type", "application/json")
  42. integrationKey := r.URL.Query().Get("integrationKey")
  43. configs, err := c.ExportConfigs(integrationKey)
  44. if err != nil {
  45. http.Error(w, err.Error(), http.StatusBadRequest)
  46. return
  47. }
  48. protocol.WriteDataWithMessage(w, configs, "Configurations have been sanitized to protect secrets")
  49. }
  50. }
  51. // GetEnableConfigHandler creates a handler from a http request which enables an integration via the integrationController
  52. func (c *Controller) GetAddConfigHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  53. // perform basic checks to ensure that the pipeline can be accessed
  54. fn := c.cloudCostChecks()
  55. if fn != nil {
  56. return fn
  57. }
  58. // Return valid handler func
  59. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  60. w.Header().Set("Content-Type", "application/json")
  61. configType := r.URL.Query().Get("type")
  62. config, err := parseConfig(configType, r.Body)
  63. if err != nil {
  64. http.Error(w, err.Error(), http.StatusBadRequest)
  65. return
  66. }
  67. err = c.CreateConfig(config)
  68. if err != nil {
  69. http.Error(w, err.Error(), http.StatusBadRequest)
  70. return
  71. }
  72. protocol.WriteData(w, fmt.Sprintf("Successfully added integration with key %s", config.Key()))
  73. }
  74. }
  75. func parseConfig(configType string, body io.Reader) (cloud.KeyedConfig, error) {
  76. buf := new(bytes.Buffer)
  77. _, err := buf.ReadFrom(body)
  78. if err != nil {
  79. return nil, fmt.Errorf("failed to read body: %w", err)
  80. }
  81. bytes := buf.Bytes()
  82. switch strings.ToLower(configType) {
  83. case S3ConfigType:
  84. config := &aws.S3Configuration{}
  85. err = json.Unmarshal(bytes, config)
  86. if err != nil {
  87. return nil, fmt.Errorf("error unmarshalling S3 Configuration: %w", err)
  88. }
  89. return config, nil
  90. case AthenaConfigType:
  91. config := &aws.AthenaConfiguration{}
  92. err = json.Unmarshal(bytes, config)
  93. if err != nil {
  94. return nil, fmt.Errorf("error unmarshalling Athena Configuration: %w", err)
  95. }
  96. return config, nil
  97. case BigQueryConfigType:
  98. config := &gcp.BigQueryConfiguration{}
  99. err = json.Unmarshal(bytes, config)
  100. if err != nil {
  101. return nil, fmt.Errorf("error unmarshalling Big Query Configuration: %w", err)
  102. }
  103. return config, nil
  104. case AzureStorageConfigType:
  105. config := &azure.StorageConfiguration{}
  106. err = json.Unmarshal(bytes, config)
  107. if err != nil {
  108. return nil, fmt.Errorf("error unmarshalling Azure Storage Configuration: %w", err)
  109. }
  110. return config, nil
  111. }
  112. return nil, fmt.Errorf("provided config type was not recognised %s", configType)
  113. }
  114. // GetEnableConfigHandler creates a handler from a http request which enables an integration via the integrationController
  115. func (c *Controller) GetEnableConfigHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  116. // perform basic checks to ensure that the pipeline can be accessed
  117. fn := c.cloudCostChecks()
  118. if fn != nil {
  119. return fn
  120. }
  121. // Return valid handler func
  122. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  123. w.Header().Set("Content-Type", "application/json")
  124. integrationKey := r.URL.Query().Get("integrationKey")
  125. if integrationKey == "" {
  126. http.Error(w, "required parameter 'integrationKey' is missing", http.StatusBadRequest)
  127. return
  128. }
  129. source := r.URL.Query().Get("source")
  130. if source == "" {
  131. http.Error(w, "required parameter 'source' is missing", http.StatusBadRequest)
  132. return
  133. }
  134. err := c.EnableConfig(integrationKey, source)
  135. if err != nil {
  136. http.Error(w, err.Error(), http.StatusBadRequest)
  137. return
  138. }
  139. protocol.WriteData(w, fmt.Sprintf("Successfully enabled integration with key %s from source %s", integrationKey, source))
  140. }
  141. }
  142. // GetDisableConfigHandler creates a handler from a http request which disables an integration via the integrationController
  143. func (c *Controller) GetDisableConfigHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  144. // perform basic checks to ensure that the pipeline can be accessed
  145. fn := c.cloudCostChecks()
  146. if fn != nil {
  147. return fn
  148. }
  149. // Return valid handler func
  150. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  151. w.Header().Set("Content-Type", "application/json")
  152. integrationKey := r.URL.Query().Get("integrationKey")
  153. if integrationKey == "" {
  154. http.Error(w, "required parameter 'integrationKey' is missing", http.StatusBadRequest)
  155. return
  156. }
  157. source := r.URL.Query().Get("source")
  158. if source == "" {
  159. http.Error(w, "required parameter 'source' is missing", http.StatusBadRequest)
  160. return
  161. }
  162. err := c.DisableConfig(integrationKey, source)
  163. if err != nil {
  164. http.Error(w, err.Error(), http.StatusBadRequest)
  165. return
  166. }
  167. protocol.WriteData(w, fmt.Sprintf("Successfully disabled integration with key %s from source %s", integrationKey, source))
  168. }
  169. }
  170. // GetDeleteConfigHandler creates a handler from a http request which deletes an integration via the integrationController
  171. // if there are no other integrations with the given integration key, it also clears the data.
  172. func (c *Controller) GetDeleteConfigHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  173. // perform basic checks to ensure that the pipeline can be accessed
  174. fn := c.cloudCostChecks()
  175. if fn != nil {
  176. return fn
  177. }
  178. // Return valid handler func
  179. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  180. w.Header().Set("Content-Type", "application/json")
  181. integrationKey := r.URL.Query().Get("integrationKey")
  182. if integrationKey == "" {
  183. http.Error(w, "required parameter 'integrationKey' is missing", http.StatusBadRequest)
  184. return
  185. }
  186. err := c.DeleteConfig(integrationKey, ConfigControllerSource.String())
  187. if err != nil {
  188. http.Error(w, err.Error(), http.StatusBadRequest)
  189. return
  190. }
  191. protocol.WriteData(w, fmt.Sprintf("Successfully deleted integration with key %s", integrationKey))
  192. }
  193. }