@@ -17,6 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
1717import moment from 'moment'
1818
1919import Api from '../utils/ApiCaller'
20+ import notificationStore from '../stores/NotificationStore'
2021import { SchemaParser } from './Schemas'
2122import ObjectUtils from '../utils/ObjectUtils'
2223import type { Endpoint , Validation , Storage } from '../types/Endpoint'
@@ -87,6 +88,27 @@ class EdnpointSource {
8788 } )
8889 }
8990
91+ static getSecretPayload ( uuid : string , count : number = 0 ) {
92+ let delay = ( ) => new Promise ( r => { setTimeout ( ( ) => { r ( ) } , 2000 ) } )
93+
94+ if ( count >= 10 ) {
95+ return Promise . reject ( { secretCustomError : `The secret '${ uuid } ' is not active after ${ count } retries.` } )
96+ }
97+
98+ return Api . send ( {
99+ url : `${ servicesUrl . barbican } /v1/secrets/${ uuid } ` ,
100+ headers : { Accept : 'application/json' } ,
101+ } ) . then ( response => {
102+ if ( response . data . status === 'ACTIVE' ) {
103+ return Api . send ( {
104+ url : `${ servicesUrl . barbican } /v1/secrets/${ uuid } /payload` ,
105+ headers : { Accept : 'text/plain' } ,
106+ } )
107+ }
108+ return delay ( ) . then ( ( ) => this . getSecretPayload ( uuid , count + 1 ) )
109+ } )
110+ }
111+
90112 static getConnectionsInfo ( endpoints : Endpoint [ ] ) : Promise < Endpoint [ ] > {
91113 return Promise . all ( endpoints . map ( endpoint => {
92114 let index = endpoint . connection_info . secret_ref ? endpoint . connection_info . secret_ref . lastIndexOf ( '/' ) : ''
@@ -151,18 +173,18 @@ class EdnpointSource {
151173 uuidIndex = connectionInfo . secret_ref . lastIndexOf ( '/' )
152174 uuid = connectionInfo . secret_ref . substr ( uuidIndex + 1 )
153175 newEndpoint = putResponse . data . endpoint
154- return Api . send ( {
155- url : `${ servicesUrl . barbican } /v1/secrets/${ uuid } /payload` ,
156- method : 'GET' ,
157- responseType : 'text' ,
158- headers : { Accept : 'text/plain' } ,
159- } )
176+ return this . getSecretPayload ( uuid )
160177 } ) . then ( conInfoResponse => {
161178 newEndpoint . connection_info = {
162179 ...newEndpoint . connection_info ,
163180 ...conInfoResponse . data ,
164181 }
165182 return newEndpoint
183+ } ) . catch ( e => {
184+ if ( e . secretCustomError ) {
185+ notificationStore . alert ( e . secretCustomError , 'error' )
186+ }
187+ throw e
166188 } )
167189 }
168190
@@ -205,17 +227,18 @@ class EdnpointSource {
205227 let uuid = connectionInfo . secret_ref . substr ( uuidIndex + 1 )
206228 newEndpoint = postResponse . data . endpoint
207229
208- return Api . send ( {
209- url : `${ servicesUrl . barbican } /v1/secrets/${ uuid } /payload` ,
210- responseType : 'text' ,
211- headers : { Accept : 'text/plain' } ,
212- } )
230+ return this . getSecretPayload ( uuid )
213231 } ) . then ( conInfoResponse => {
214232 newEndpoint . connection_info = {
215233 ...newEndpoint . connection_info ,
216234 ...conInfoResponse . data ,
217235 }
218236 return newEndpoint
237+ } ) . catch ( e => {
238+ if ( e . secretCustomError ) {
239+ notificationStore . alert ( e . secretCustomError , 'error' )
240+ }
241+ throw e
219242 } )
220243 }
221244
0 commit comments