vault/ui/app/serializers/pki/action.js
hashicorp-copywrite[bot] 0b12cdcfd1
[COMPLIANCE] License changes (#22290)
* Adding explicit MPL license for sub-package.

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Adding explicit MPL license for sub-package.

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Updating the license from MPL to Business Source License.

Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl.

* add missing license headers

* Update copyright file headers to BUS-1.1

* Fix test that expected exact offset on hcl file

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
Co-authored-by: Sarah Thompson <sthompson@hashicorp.com>
Co-authored-by: Brian Kassouf <bkassouf@hashicorp.com>
2023-08-10 18:14:03 -07:00

105 lines
3.1 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import { underscore } from '@ember/string';
import { keyParamsByType } from 'pki/utils/action-params';
import ApplicationSerializer from '../application';
import { parseCertificate } from 'vault/utils/parse-pki-cert';
export default class PkiActionSerializer extends ApplicationSerializer {
attrs = {
customTtl: { serialize: false },
type: { serialize: false },
};
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
if (payload.data.certificate) {
// Parse certificate back from the API and add to payload
const parsedCert = parseCertificate(payload.data.certificate);
const data = {
...payload.data,
common_name: parsedCert.common_name,
parsed_certificate: parsedCert,
};
return super.normalizeResponse(store, primaryModelClass, { ...payload, data }, id, requestType);
}
return super.normalizeResponse(...arguments);
}
serialize(snapshot, requestType) {
const data = super.serialize(snapshot);
// requestType is a custom value specified from the pki/action adapter
const allowedPayloadAttributes = this._allowedParamsByType(requestType, snapshot.record.type);
if (!allowedPayloadAttributes) return data;
// the backend expects the subject's serial number param to be 'serial_number'
// we label it as subject_serial_number to differentiate from the vault generated UUID
data.serial_number = data.subject_serial_number;
const payload = {};
allowedPayloadAttributes.forEach((key) => {
if ('undefined' !== typeof data[key]) {
payload[key] = data[key];
}
});
return payload;
}
_allowedParamsByType(actionType, type) {
const keyFields = keyParamsByType(type).map((attrName) => underscore(attrName).toLowerCase());
const commonProps = [
'alt_names',
'common_name',
'country',
'exclude_cn_from_sans',
'format',
'ip_sans',
'locality',
'organization',
'other_sans',
'ou',
'postal_code',
'province',
'serial_number',
'street_address',
'type',
'uri_sans',
...keyFields,
];
switch (actionType) {
case 'import':
return ['pem_bundle'];
case 'generate-root':
return [
...commonProps,
'issuer_name',
'max_path_length',
'not_after',
'not_before_duration',
'permitted_dns_domains',
'private_key_format',
'ttl',
];
case 'rotate-root':
return [
...commonProps,
'issuer_name',
'max_path_length',
'not_after',
'not_before_duration',
'permitted_dns_domains',
'private_key_format',
'ttl',
];
case 'generate-csr':
return [...commonProps, 'add_basic_constraints'];
case 'sign-intermediate':
return ['common_name', 'issuer_name', 'csr'];
default:
// if type doesn't match, serialize all
return null;
}
}
}