EndpointSource.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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.get(`${servicesUrl.coriolis}/${projectId}/endpoints`).then(response => {
  40. let connections = []
  41. if (response.data.endpoints.length) {
  42. response.data.endpoints.forEach(endpoint => {
  43. connections.push(endpoint)
  44. })
  45. }
  46. connections.sort((c1, c2) => moment(c2.created_at).diff(moment(c1.created_at)))
  47. resolve(connections)
  48. }).catch(reject)
  49. } else {
  50. reject()
  51. }
  52. })
  53. }
  54. static delete(endpoint: Endpoint): Promise<string> {
  55. return new Promise((resolve, reject) => {
  56. let projectId: any = cookie.get('projectId')
  57. Api.send({
  58. url: `${servicesUrl.coriolis}/${projectId}/endpoints/${endpoint.id}`,
  59. method: 'DELETE',
  60. }).then(() => {
  61. if (endpoint.connection_info && endpoint.connection_info.secret_ref) {
  62. let uuidIndex = endpoint.connection_info.secret_ref.lastIndexOf('/')
  63. // $FlowIssue
  64. let uuid = endpoint.connection_info.secret_ref.substr(uuidIndex + 1)
  65. Api.send({
  66. url: `${servicesUrl.barbican}/v1/secrets/${uuid}`,
  67. method: 'DELETE',
  68. }).then(() => { resolve(endpoint.id) }).catch(reject)
  69. } else {
  70. resolve(endpoint.id)
  71. }
  72. }).catch(reject)
  73. })
  74. }
  75. static getConnectionInfo(endpoint: Endpoint): Promise<$PropertyType<Endpoint, 'connection_info'>> {
  76. let index = endpoint.connection_info.secret_ref && endpoint.connection_info.secret_ref.lastIndexOf('/')
  77. let uuid = index && endpoint.connection_info.secret_ref && endpoint.connection_info.secret_ref.substr(index + 1)
  78. return new Promise((resolve, reject) => {
  79. Api.send({
  80. url: `${servicesUrl.barbican}/v1/secrets/${uuid || ''}/payload`,
  81. responseType: 'text',
  82. headers: { Accept: 'text/plain' },
  83. }).then((response) => {
  84. resolve(response.data)
  85. }).catch(reject)
  86. })
  87. }
  88. static getConnectionsInfo(endpoints: Endpoint[]): Promise<Endpoint[]> {
  89. return new Promise(resolve => {
  90. if (!endpoints || endpoints.length === 0) {
  91. resolve([])
  92. return
  93. }
  94. let count = 0
  95. let connectionsInfo = []
  96. let isDone = () => {
  97. count += 1
  98. if (count === endpoints.length) {
  99. resolve(connectionsInfo)
  100. }
  101. }
  102. endpoints.forEach(endpoint => {
  103. let index = endpoint.connection_info.secret_ref ? endpoint.connection_info.secret_ref.lastIndexOf('/') : ''
  104. let uuid = endpoint.connection_info.secret_ref && index ? endpoint.connection_info.secret_ref.substr(index + 1) : ''
  105. Api.send({
  106. url: `${servicesUrl.barbican}/v1/secrets/${uuid}/payload`,
  107. responseType: 'text',
  108. headers: { Accept: 'text/plain' },
  109. }).then(response => {
  110. connectionsInfo.push({ ...endpoint, connection_info: response.data })
  111. isDone()
  112. }, isDone).catch(isDone)
  113. })
  114. })
  115. }
  116. static validate(endpoint: Endpoint): Promise<Validation> {
  117. return new Promise((resolve, reject) => {
  118. let projectId = cookie.get('projectId')
  119. Api.send({
  120. url: `${servicesUrl.coriolis}/${projectId || ''}/endpoints/${endpoint.id}/actions`,
  121. method: 'POST',
  122. data: { 'validate-connection': null },
  123. }).then(response => {
  124. resolve(response.data['validate-connection'])
  125. }).catch(reject)
  126. })
  127. }
  128. static update(endpoint: Endpoint): Promise<Endpoint> {
  129. return new Promise((resolve, reject) => {
  130. let projectId = cookie.get('projectId')
  131. let parsedEndpoint = SchemaParser.fieldsToPayload(endpoint)
  132. if (parsedEndpoint.connection_info && parsedEndpoint.connection_info.secret_ref) {
  133. // $FlowIgnore
  134. let uuidIndex = parsedEndpoint.connection_info.secret_ref.lastIndexOf('/')
  135. // $FlowIgnore
  136. let uuid = parsedEndpoint.connection_info.secret_ref.substr(uuidIndex + 1)
  137. Api.send({
  138. url: `${servicesUrl.barbican}/v1/secrets/${uuid}`,
  139. method: 'DELETE',
  140. })
  141. Api.send({
  142. url: `${servicesUrl.barbican}/v1/secrets`,
  143. method: 'POST',
  144. data: getBarbicanPayload(ObjectUtils.skipField(parsedEndpoint.connection_info, 'secret_ref')),
  145. }).then(response => {
  146. let connectionInfo = { secret_ref: response.data.secret_ref }
  147. let newPayload = {
  148. endpoint: {
  149. name: parsedEndpoint.name,
  150. description: parsedEndpoint.description,
  151. connection_info: connectionInfo,
  152. },
  153. }
  154. Api.send({
  155. url: `${servicesUrl.coriolis}/${projectId || ''}/endpoints/${endpoint.id}`,
  156. method: 'PUT',
  157. data: newPayload,
  158. }).then(putResponse => {
  159. uuidIndex = connectionInfo.secret_ref.lastIndexOf('/')
  160. uuid = connectionInfo.secret_ref.substr(uuidIndex + 1)
  161. let newEndpoint = putResponse.data.endpoint
  162. Api.send({
  163. url: `${servicesUrl.barbican}/v1/secrets/${uuid}/payload`,
  164. method: 'GET',
  165. responseType: 'text',
  166. headers: { Accept: 'text/plain' },
  167. }).then(conInfoResponse => {
  168. newEndpoint.connection_info = {
  169. ...newEndpoint.connection_info,
  170. ...conInfoResponse.data,
  171. }
  172. resolve(newEndpoint)
  173. }).catch(reject)
  174. }).catch(reject)
  175. }).catch(reject)
  176. } else {
  177. Api.send({
  178. url: `${servicesUrl.coriolis}/${projectId || ''}/endpoints/${endpoint.id}`,
  179. method: 'PUT',
  180. data: { endpoint: parsedEndpoint },
  181. }).then(response => {
  182. resolve(response.data.endpoint)
  183. }).catch(reject)
  184. }
  185. })
  186. }
  187. static add(endpoint: Endpoint, skipSchemaParser: boolean = false): Promise<Endpoint> {
  188. return new Promise((resolve, reject) => {
  189. let parsedEndpoint = skipSchemaParser ? { ...endpoint } : SchemaParser.fieldsToPayload(endpoint)
  190. let projectId = cookie.get('projectId')
  191. if (useSecret) {
  192. Api.send({
  193. url: `${servicesUrl.barbican}/v1/secrets`,
  194. method: 'POST',
  195. data: getBarbicanPayload(ObjectUtils.skipField(parsedEndpoint.connection_info, 'secret_ref')),
  196. }).then(response => {
  197. let connectionInfo = { secret_ref: response.data.secret_ref }
  198. let newPayload = {
  199. endpoint: {
  200. name: parsedEndpoint.name,
  201. description: parsedEndpoint.description,
  202. type: endpoint.type,
  203. connection_info: connectionInfo,
  204. },
  205. }
  206. Api.send({
  207. url: `${servicesUrl.coriolis}/${projectId || ''}/endpoints`,
  208. method: 'POST',
  209. data: newPayload,
  210. }).then(postResponse => {
  211. let uuidIndex = connectionInfo.secret_ref.lastIndexOf('/')
  212. let uuid = connectionInfo.secret_ref.substr(uuidIndex + 1)
  213. let newEndpoint = postResponse.data.endpoint
  214. Api.send({
  215. url: `${servicesUrl.barbican}/v1/secrets/${uuid}/payload`,
  216. responseType: 'text',
  217. headers: { Accept: 'text/plain' },
  218. }).then(conInfoResponse => {
  219. newEndpoint.connection_info = {
  220. ...newEndpoint.connection_info,
  221. ...conInfoResponse.data,
  222. }
  223. resolve(newEndpoint)
  224. }).catch(reject)
  225. }).catch(reject)
  226. }).catch(reject)
  227. } else {
  228. Api.send({
  229. url: `${servicesUrl.coriolis}/${projectId || ''}/endpoints`,
  230. method: 'POST',
  231. data: { endpoint: parsedEndpoint },
  232. }).then(response => {
  233. resolve(response.data.endpoint)
  234. }).catch(reject)
  235. }
  236. })
  237. }
  238. }
  239. export default EdnpointSource