Skip to content

Commit 430046b

Browse files
author
Sergiu Miclea
committed
Add support for non-active secret when updating
When updating or adding a new endpoint, its Barbican secret may not be active immediately. A polling is made to make sure the secret is active. If after 10 retries (2 second waiting period after each request) the secret is still inactive, the secret payload call gets rejected.
1 parent fd4c2f8 commit 430046b

3 files changed

Lines changed: 52 additions & 16 deletions

File tree

src/components/organisms/Endpoint/Endpoint.jsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ class Endpoint extends React.Component<Props, State> {
155155

156156
componentDidMount() {
157157
providerStore.getConnectionInfoSchema(this.getEndpointType())
158-
KeyboardManager.onEnter('endpoint', () => { if (this.isValidateButtonEnabled) this.handleValidateClick() }, 2)
158+
KeyboardManager.onEnter('endpoint', () => {
159+
if (this.isValidateButtonEnabled) this.handleValidateClick()
160+
}, 2)
159161
}
160162

161163
componentWillReceiveProps(props: Props) {
@@ -281,9 +283,14 @@ class Endpoint extends React.Component<Props, State> {
281283
}
282284

283285
endpointStore.update(this.state.endpoint).then(() => {
286+
let endpoint = endpointStore.endpoints.find(e => this.state.endpoint && e.id === this.state.endpoint.id)
287+
if (!endpoint) {
288+
throw new Error('endpoint not found')
289+
}
290+
291+
this.setState({ endpoint: ObjectUtils.flatten(endpoint) })
284292
notificationStore.alert('Validating endpoint ...')
285-
// $FlowIssue
286-
endpointStore.validate(this.state.endpoint)
293+
endpointStore.validate(endpoint)
287294
})
288295
}
289296

@@ -386,7 +393,9 @@ class Endpoint extends React.Component<Props, State> {
386393
passwordFields,
387394
getFieldValue: field => this.getFieldValue(field),
388395
highlightRequired: () => this.highlightRequired(),
389-
handleFieldChange: (field, value) => { if (field) this.handleFieldsChange([{ field, value }]) },
396+
handleFieldChange: (field, value) => {
397+
if (field) this.handleFieldsChange([{ field, value }])
398+
},
390399
handleFieldsChange: fields => { this.handleFieldsChange(fields) },
391400
handleValidateClick: () => { this.handleValidateClick() },
392401
handleCancelClick: () => { this.handleCancelClick() },

src/sources/EndpointSource.js

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
import moment from 'moment'
1818

1919
import Api from '../utils/ApiCaller'
20+
import notificationStore from '../stores/NotificationStore'
2021
import { SchemaParser } from './Schemas'
2122
import ObjectUtils from '../utils/ObjectUtils'
2223
import 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

src/stores/EndpointStore.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ class EndpointStore {
153153
this.endpoints = updateEndpoint(updatedEndpoint, this.endpoints)
154154
this.connectionInfo = { ...updatedEndpoint.connection_info }
155155
this.updating = false
156+
}).catch(e => {
157+
this.updating = false
158+
throw e
156159
})
157160
}
158161

@@ -171,8 +174,9 @@ class EndpointStore {
171174

172175
this.connectionInfo = addedEndpoint.connection_info
173176
this.adding = false
174-
}).catch(() => {
177+
}).catch(e => {
175178
this.adding = false
179+
throw e
176180
})
177181
}
178182

0 commit comments

Comments
 (0)