-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
pki-ca-certificate.js
68 lines (61 loc) · 2.32 KB
/
pki-ca-certificate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { parsePkiCert } from '../helpers/parse-pki-cert';
import ApplicationAdapter from './application';
export default ApplicationAdapter.extend({
namespace: 'v1',
url(snapshot, action) {
const { backend, caType, type } = snapshot.attributes();
if (action === 'sign-intermediate') {
return `/v1/${backend}/root/sign-intermediate`;
}
if (action === 'set-signed-intermediate') {
return `/v1/${backend}/intermediate/set-signed`;
}
if (action === 'upload') {
return `/v1/${backend}/config/ca`;
}
return `/v1/${backend}/${caType}/generate/${type}`;
},
createRecordOrUpdate(store, type, snapshot, requestType) {
const serializer = store.serializerFor('application');
const isUpload = snapshot.attr('uploadPemBundle');
const isSetSignedIntermediate = snapshot.adapterOptions.method === 'setSignedIntermediate';
let action = snapshot.adapterOptions.method === 'signIntermediate' ? 'sign-intermediate' : null;
let data;
if (isUpload) {
action = 'upload';
data = { pem_bundle: snapshot.attr('pemBundle') };
} else if (isSetSignedIntermediate) {
action = 'set-signed-intermediate';
data = { certificate: snapshot.attr('certificate') };
} else {
data = serializer.serialize(snapshot, requestType);
}
return this.ajax(this.url(snapshot, action), 'POST', { data }).then(response => {
// uploading CA, setting signed intermediate cert, and attempting to generate
// a new CA if one exists, all return a 204
if (!response) {
response = {};
}
response.id = snapshot.id;
response.modelName = type.modelName;
// only parse if certificate is attached to response
if (response.data && response.data.certificate) {
const caCertMetadata = parsePkiCert([response.data]);
const transformedResponse = { ...response, ...caCertMetadata };
store.pushPayload(type.modelName, transformedResponse);
} else {
store.pushPayload(type.modelName, response);
}
});
},
createRecord() {
return this.createRecordOrUpdate(...arguments);
},
updateRecord() {
return this.createRecordOrUpdate(...arguments);
},
deleteRecord(store, type, snapshot) {
const backend = snapshot.attr('backend');
return this.ajax(`/v1/${backend}/root`, 'DELETE');
},
});