EndpointSource.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 getConnectionsInfo(endpoints: Endpoint[]): Promise<Endpoint[]> {
  93. return new Promise(resolve => {
  94. if (!endpoints || endpoints.length === 0) {
  95. resolve([])
  96. return
  97. }
  98. let count = 0
  99. let connectionsInfo = []
  100. let isDone = () => {
  101. count += 1
  102. if (count === endpoints.length) {
  103. resolve(connectionsInfo)
  104. }
  105. }
  106. endpoints.forEach(endpoint => {
  107. let index = endpoint.connection_info.secret_ref ? endpoint.connection_info.secret_ref.lastIndexOf('/') : ''
  108. let uuid = endpoint.connection_info.secret_ref && index ? endpoint.connection_info.secret_ref.substr(index + 1) : ''
  109. Api.sendAjaxRequest({
  110. url: `${servicesUrl.barbican}/v1/secrets/${uuid}/payload`,
  111. method: 'GET',
  112. json: false,
  113. headers: { Accept: 'text/plain' },
  114. }).then(response => {
  115. connectionsInfo.push({ ...endpoint, connection_info: JSON.parse(response.data) })
  116. isDone()
  117. }, isDone).catch(isDone)
  118. })
  119. })
  120. }
  121. static validate(endpoint: Endpoint): Promise<Validation> {
  122. return new Promise((resolve, reject) => {
  123. let projectId = cookie.get('projectId')
  124. Api.sendAjaxRequest({
  125. url: `${servicesUrl.coriolis}/${projectId || ''}/endpoints/${endpoint.id}/actions`,
  126. method: 'POST',
  127. data: { 'validate-connection': null },
  128. }).then(response => {
  129. resolve(response.data['validate-connection'])
  130. }, reject).catch(reject)
  131. })
  132. }
  133. static update(endpoint: Endpoint): Promise<Endpoint> {
  134. return new Promise((resolve, reject) => {
  135. let projectId = cookie.get('projectId')
  136. let parsedEndpoint = SchemaParser.fieldsToPayload(endpoint)
  137. if (parsedEndpoint.connection_info && parsedEndpoint.connection_info.secret_ref) {
  138. // $FlowIgnore
  139. let uuidIndex = parsedEndpoint.connection_info.secret_ref.lastIndexOf('/')
  140. // $FlowIgnore
  141. let uuid = parsedEndpoint.connection_info.secret_ref.substr(uuidIndex + 1)
  142. Api.sendAjaxRequest({
  143. url: `${servicesUrl.barbican}/v1/secrets/${uuid}`,
  144. method: 'DELETE',
  145. })
  146. Api.sendAjaxRequest({
  147. url: `${servicesUrl.barbican}/v1/secrets`,
  148. method: 'POST',
  149. data: getBarbicanPayload(ObjectUtils.skipField(parsedEndpoint.connection_info, 'secret_ref')),
  150. }).then(response => {
  151. let connectionInfo = { secret_ref: response.data.secret_ref }
  152. let newPayload = {
  153. endpoint: {
  154. name: parsedEndpoint.name,
  155. description: parsedEndpoint.description,
  156. connection_info: connectionInfo,
  157. },
  158. }
  159. Api.sendAjaxRequest({
  160. url: `${servicesUrl.coriolis}/${projectId || ''}/endpoints/${endpoint.id}`,
  161. method: 'PUT',
  162. data: newPayload,
  163. }).then(putResponse => {
  164. uuidIndex = connectionInfo.secret_ref.lastIndexOf('/')
  165. uuid = connectionInfo.secret_ref.substr(uuidIndex + 1)
  166. let newEndpoint = putResponse.data.endpoint
  167. Api.sendAjaxRequest({
  168. url: `${servicesUrl.barbican}/v1/secrets/${uuid}/payload`,
  169. method: 'GET',
  170. json: false,
  171. headers: { Accept: 'text/plain' },
  172. }).then(conInfoResponse => {
  173. newEndpoint.connection_info = {
  174. ...newEndpoint.connection_info,
  175. ...JSON.parse(conInfoResponse.data),
  176. }
  177. resolve(newEndpoint)
  178. }, reject).catch(reject)
  179. }, reject).catch(reject)
  180. }, reject).catch(reject)
  181. } else {
  182. Api.sendAjaxRequest({
  183. url: `${servicesUrl.coriolis}/${projectId || ''}/endpoints/${endpoint.id}`,
  184. method: 'PUT',
  185. data: { endpoint: parsedEndpoint },
  186. }).then(response => {
  187. resolve(response.data.endpoint)
  188. }, reject).catch(reject)
  189. }
  190. })
  191. }
  192. static add(endpoint: Endpoint): Promise<Endpoint> {
  193. return new Promise((resolve, reject) => {
  194. let parsedEndpoint = SchemaParser.fieldsToPayload(endpoint)
  195. let projectId = cookie.get('projectId')
  196. if (useSecret) {
  197. Api.sendAjaxRequest({
  198. url: `${servicesUrl.barbican}/v1/secrets`,
  199. method: 'POST',
  200. data: getBarbicanPayload(ObjectUtils.skipField(parsedEndpoint.connection_info, 'secret_ref')),
  201. }).then(response => {
  202. let connectionInfo = { secret_ref: response.data.secret_ref }
  203. let newPayload = {
  204. endpoint: {
  205. name: parsedEndpoint.name,
  206. description: parsedEndpoint.description,
  207. type: endpoint.type,
  208. connection_info: connectionInfo,
  209. },
  210. }
  211. Api.sendAjaxRequest({
  212. url: `${servicesUrl.coriolis}/${projectId || ''}/endpoints`,
  213. method: 'POST',
  214. data: newPayload,
  215. }).then(postResponse => {
  216. let uuidIndex = connectionInfo.secret_ref.lastIndexOf('/')
  217. let uuid = connectionInfo.secret_ref.substr(uuidIndex + 1)
  218. let newEndpoint = postResponse.data.endpoint
  219. Api.sendAjaxRequest({
  220. url: `${servicesUrl.barbican}/v1/secrets/${uuid}/payload`,
  221. method: 'GET',
  222. json: false,
  223. headers: { Accept: 'text/plain' },
  224. }).then(conInfoResponse => {
  225. newEndpoint.connection_info = {
  226. ...newEndpoint.connection_info,
  227. ...JSON.parse(conInfoResponse.data),
  228. }
  229. resolve(newEndpoint)
  230. }, reject).catch(reject)
  231. }, reject).catch(reject)
  232. }, reject).catch(reject)
  233. } else {
  234. Api.sendAjaxRequest({
  235. url: `${servicesUrl.coriolis}/${projectId || ''}/endpoints`,
  236. method: 'POST',
  237. data: { endpoint: parsedEndpoint },
  238. }).then(response => {
  239. resolve(response.data.endpoint)
  240. }, reject).catch(reject)
  241. }
  242. })
  243. }
  244. }
  245. export default EdnpointSource