EndpointSource.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. // @flow
  15. import cookie from 'js-cookie'
  16. import moment from 'moment'
  17. import Api from '../utils/ApiCaller'
  18. import { SchemaParser } from './Schemas'
  19. import ObjectUtils from '../utils/ObjectUtils'
  20. import type { Endpoint, Validation } from '../types/Endpoint'
  21. import { servicesUrl, useSecret } from '../config'
  22. let getBarbicanPayload = data => {
  23. return {
  24. payload: JSON.stringify(data),
  25. payload_content_type: 'text/plain',
  26. algorithm: 'aes',
  27. bit_length: 256,
  28. mode: 'cbc',
  29. content_types: {
  30. default: 'text/plain',
  31. },
  32. }
  33. }
  34. class EdnpointSource {
  35. static getEndpoints(): Promise<Endpoint[]> {
  36. return new Promise((resolve, reject) => {
  37. let projectId = cookie.get('projectId')
  38. if (projectId) {
  39. Api.sendAjaxRequest({
  40. url: `${servicesUrl.coriolis}/${projectId}/endpoints`,
  41. method: 'GET',
  42. }).then(response => {
  43. let connections = []
  44. if (response.data.endpoints.length) {
  45. response.data.endpoints.forEach(endpoint => {
  46. connections.push(endpoint)
  47. })
  48. }
  49. connections.sort((c1, c2) => moment(c2.created_at).diff(moment(c1.created_at)))
  50. resolve(connections)
  51. }, reject).catch(reject)
  52. } else {
  53. reject()
  54. }
  55. })
  56. }
  57. static delete(endpoint: Endpoint): Promise<string> {
  58. return new Promise((resolve, reject) => {
  59. let projectId :any = cookie.get('projectId')
  60. Api.sendAjaxRequest({
  61. url: `${servicesUrl.coriolis}/${projectId}/endpoints/${endpoint.id}`,
  62. method: 'DELETE',
  63. }).then(() => {
  64. if (endpoint.connection_info && endpoint.connection_info.secret_ref) {
  65. let uuidIndex = endpoint.connection_info.secret_ref.lastIndexOf('/')
  66. // $FlowIssue
  67. let uuid = endpoint.connection_info.secret_ref.substr(uuidIndex + 1)
  68. Api.sendAjaxRequest({
  69. url: `${servicesUrl.barbican}/v1/secrets/${uuid}`,
  70. method: 'DELETE',
  71. }).then(() => { resolve(endpoint.id) }, reject).catch(reject)
  72. } else {
  73. resolve(endpoint.id)
  74. }
  75. }, reject).catch(reject)
  76. })
  77. }
  78. static getConnectionInfo(endpoint: Endpoint): Promise<$PropertyType<Endpoint, 'connection_info'>> {
  79. let index = endpoint.connection_info.secret_ref && endpoint.connection_info.secret_ref.lastIndexOf('/')
  80. let uuid = index && endpoint.connection_info.secret_ref && endpoint.connection_info.secret_ref.substr(index + 1)
  81. return new Promise((resolve, reject) => {
  82. Api.sendAjaxRequest({
  83. url: `${servicesUrl.barbican}/v1/secrets/${uuid || ''}/payload`,
  84. method: 'GET',
  85. json: false,
  86. headers: { Accept: 'text/plain' },
  87. }).then((response) => {
  88. resolve(JSON.parse(response.data))
  89. }, reject).catch(reject)
  90. })
  91. }
  92. static validate(endpoint: Endpoint): Promise<Validation> {
  93. return new Promise((resolve, reject) => {
  94. let projectId = cookie.get('projectId')
  95. Api.sendAjaxRequest({
  96. url: `${servicesUrl.coriolis}/${projectId || ''}/endpoints/${endpoint.id}/actions`,
  97. method: 'POST',
  98. data: { 'validate-connection': null },
  99. }).then(response => {
  100. resolve(response.data['validate-connection'])
  101. }, reject).catch(reject)
  102. })
  103. }
  104. static update(endpoint: Endpoint): Promise<Endpoint> {
  105. return new Promise((resolve, reject) => {
  106. let projectId = cookie.get('projectId')
  107. let parsedEndpoint = SchemaParser.fieldsToPayload(endpoint)
  108. if (parsedEndpoint.connection_info && parsedEndpoint.connection_info.secret_ref) {
  109. // $FlowIgnore
  110. let uuidIndex = parsedEndpoint.connection_info.secret_ref.lastIndexOf('/')
  111. // $FlowIgnore
  112. let uuid = parsedEndpoint.connection_info.secret_ref.substr(uuidIndex + 1)
  113. Api.sendAjaxRequest({
  114. url: `${servicesUrl.barbican}/v1/secrets/${uuid}`,
  115. method: 'DELETE',
  116. })
  117. Api.sendAjaxRequest({
  118. url: `${servicesUrl.barbican}/v1/secrets`,
  119. method: 'POST',
  120. data: getBarbicanPayload(ObjectUtils.skipField(parsedEndpoint.connection_info, 'secret_ref')),
  121. }).then(response => {
  122. let connectionInfo = { secret_ref: response.data.secret_ref }
  123. let newPayload = {
  124. endpoint: {
  125. name: parsedEndpoint.name,
  126. description: parsedEndpoint.description,
  127. connection_info: connectionInfo,
  128. },
  129. }
  130. Api.sendAjaxRequest({
  131. url: `${servicesUrl.coriolis}/${projectId || ''}/endpoints/${endpoint.id}`,
  132. method: 'PUT',
  133. data: newPayload,
  134. }).then(putResponse => {
  135. uuidIndex = connectionInfo.secret_ref.lastIndexOf('/')
  136. uuid = connectionInfo.secret_ref.substr(uuidIndex + 1)
  137. let newEndpoint = putResponse.data.endpoint
  138. Api.sendAjaxRequest({
  139. url: `${servicesUrl.barbican}/v1/secrets/${uuid}/payload`,
  140. method: 'GET',
  141. json: false,
  142. headers: { Accept: 'text/plain' },
  143. }).then(conInfoResponse => {
  144. newEndpoint.connection_info = {
  145. ...newEndpoint.connection_info,
  146. ...JSON.parse(conInfoResponse.data),
  147. }
  148. resolve(newEndpoint)
  149. }, reject).catch(reject)
  150. }, reject).catch(reject)
  151. }, reject).catch(reject)
  152. } else {
  153. Api.sendAjaxRequest({
  154. url: `${servicesUrl.coriolis}/${projectId || ''}/endpoints/${endpoint.id}`,
  155. method: 'PUT',
  156. data: { endpoint: parsedEndpoint },
  157. }).then(response => {
  158. resolve(response.data.endpoint)
  159. }, reject).catch(reject)
  160. }
  161. })
  162. }
  163. static add(endpoint: Endpoint): Promise<Endpoint> {
  164. return new Promise((resolve, reject) => {
  165. let parsedEndpoint = SchemaParser.fieldsToPayload(endpoint)
  166. let projectId = cookie.get('projectId')
  167. if (useSecret) {
  168. Api.sendAjaxRequest({
  169. url: `${servicesUrl.barbican}/v1/secrets`,
  170. method: 'POST',
  171. data: getBarbicanPayload(ObjectUtils.skipField(parsedEndpoint.connection_info, 'secret_ref')),
  172. }).then(response => {
  173. let connectionInfo = { secret_ref: response.data.secret_ref }
  174. let newPayload = {
  175. endpoint: {
  176. name: parsedEndpoint.name,
  177. description: parsedEndpoint.description,
  178. type: endpoint.type,
  179. connection_info: connectionInfo,
  180. },
  181. }
  182. Api.sendAjaxRequest({
  183. url: `${servicesUrl.coriolis}/${projectId || ''}/endpoints`,
  184. method: 'POST',
  185. data: newPayload,
  186. }).then(postResponse => {
  187. let uuidIndex = connectionInfo.secret_ref.lastIndexOf('/')
  188. let uuid = connectionInfo.secret_ref.substr(uuidIndex + 1)
  189. let newEndpoint = postResponse.data.endpoint
  190. Api.sendAjaxRequest({
  191. url: `${servicesUrl.barbican}/v1/secrets/${uuid}/payload`,
  192. method: 'GET',
  193. json: false,
  194. headers: { Accept: 'text/plain' },
  195. }).then(conInfoResponse => {
  196. newEndpoint.connection_info = {
  197. ...newEndpoint.connection_info,
  198. ...JSON.parse(conInfoResponse.data),
  199. }
  200. resolve(newEndpoint)
  201. }, reject).catch(reject)
  202. }, reject).catch(reject)
  203. }, reject).catch(reject)
  204. } else {
  205. Api.sendAjaxRequest({
  206. url: `${servicesUrl.coriolis}/${projectId || ''}/endpoints`,
  207. method: 'POST',
  208. data: { endpoint: parsedEndpoint },
  209. }).then(response => {
  210. resolve(response.data.endpoint)
  211. }, reject).catch(reject)
  212. }
  213. })
  214. }
  215. }
  216. export default EdnpointSource