2017-08-15 23:16:38 -07:00
|
|
|
import Ember from 'ember';
|
2017-06-01 13:11:26 -07:00
|
|
|
import { datasetClassifiers } from 'wherehows-web/constants/dataset-classification';
|
|
|
|
|
2017-08-15 23:16:38 -07:00
|
|
|
const { assert } = Ember;
|
|
|
|
|
2017-03-24 20:50:42 -07:00
|
|
|
/**
|
2017-05-19 03:05:25 -07:00
|
|
|
* Builds a default shape for securitySpecification & privacyCompliancePolicy with default / unset values
|
2017-04-28 22:02:46 -07:00
|
|
|
* for non null properties as per Avro schema
|
2017-05-19 09:52:19 -07:00
|
|
|
* @param {Number} datasetId id for the dataset that this privacy object applies to
|
2017-04-01 14:13:28 -07:00
|
|
|
*/
|
2017-05-19 10:52:58 -07:00
|
|
|
const createInitialComplianceInfo = datasetId => ({
|
2017-05-19 03:05:25 -07:00
|
|
|
datasetId,
|
|
|
|
// default to first item in compliance types list
|
|
|
|
complianceType: 'AUTO_PURGE',
|
2017-05-19 08:45:05 -07:00
|
|
|
complianceEntities: [],
|
2017-05-19 03:05:25 -07:00
|
|
|
fieldClassification: {},
|
|
|
|
datasetClassification: {},
|
|
|
|
geographicAffinity: { affinity: '' },
|
|
|
|
recordOwnerType: '',
|
|
|
|
retentionPolicy: { retentionType: '' }
|
|
|
|
});
|
2017-04-28 22:02:46 -07:00
|
|
|
|
2017-06-01 13:11:26 -07:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @type {{complianceEntities: {type: string, of: {type: string, keys: [*]}}, datasetClassification: {type: string, keys: (*)}, fieldClassification: {type: string}}}
|
|
|
|
*/
|
|
|
|
const policyShape = {
|
|
|
|
complianceEntities: {
|
|
|
|
type: 'array',
|
|
|
|
of: {
|
|
|
|
type: 'object',
|
|
|
|
keys: [
|
|
|
|
'identifierField:string',
|
|
|
|
'identifierType:string',
|
2017-08-15 23:16:38 -07:00
|
|
|
'securityClassification:string',
|
2017-06-01 13:11:26 -07:00
|
|
|
'logicalType:string|object|undefined'
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
2017-08-15 23:16:38 -07:00
|
|
|
datasetClassification: { type: 'object', keys: Object.keys(datasetClassifiers).map(key => `${key}:boolean`) }
|
2017-06-01 13:11:26 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks that a policy is valid
|
|
|
|
* @param candidatePolicy
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
|
|
|
const isPolicyExpectedShape = (candidatePolicy = {}) => {
|
|
|
|
const candidateMatchesShape = policyKey => {
|
2017-08-15 23:16:38 -07:00
|
|
|
assert(
|
|
|
|
`Expected each compliance policy attribute to be one of ${Object.keys(policyShape)}, but got ${policyKey}`,
|
|
|
|
policyShape.hasOwnProperty(policyKey)
|
|
|
|
);
|
|
|
|
|
2017-06-01 13:11:26 -07:00
|
|
|
const policyProps = policyShape[policyKey];
|
|
|
|
const expectedType = policyProps.type;
|
|
|
|
const policyKeyValue = candidatePolicy[policyKey];
|
2017-08-15 23:16:38 -07:00
|
|
|
const isValueExpectedType =
|
|
|
|
expectedType === 'array' ? Array.isArray(policyKeyValue) : typeof policyKeyValue === expectedType;
|
|
|
|
const typeDeclarations =
|
|
|
|
{
|
|
|
|
get array() {
|
|
|
|
return policyProps.of.keys;
|
|
|
|
},
|
|
|
|
get object() {
|
|
|
|
return policyProps.keys;
|
|
|
|
}
|
|
|
|
}[expectedType] || [];
|
2017-06-01 13:11:26 -07:00
|
|
|
|
|
|
|
if (!policyKeyValue || !isValueExpectedType) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expectedType === 'array') {
|
|
|
|
return policyKeyValue.every(value => {
|
|
|
|
if (!value && typeof value !== policyProps.of.type) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return typeDeclarations.every(typeString => {
|
|
|
|
const [key, type] = typeString.split(':');
|
|
|
|
return type.includes(typeof value[key]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expectedType === typeof {}) {
|
|
|
|
return typeDeclarations.every(typeString => {
|
|
|
|
const [key, type] = typeString.split(':');
|
|
|
|
return type.includes(typeof policyKeyValue[key]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (typeof candidatePolicy === 'object' && candidatePolicy) {
|
|
|
|
return Object.keys(policyShape).every(candidateMatchesShape);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
export { createInitialComplianceInfo, isPolicyExpectedShape };
|