Vault SSH Cert Feature (#1596)

* feat: 🎸 Update models for vault ssh cert feature (#1591)

✅ Closes: ICU-7546

* feat: 🎸 Setup types in cred library forms and use HDS component (#1597)

* feat: 🎸 Setup types in cred library forms and use HDS component

✅ Closes: ICU-7673

* feat: 🎸 Add vault ssh cert type for credential library (#1608)

* feat: 🎸 Add vault ssh cert type for credential library

✅ Closes: ICU-7678

* feat: 🎸 Enable feature flag for vault ssh cert work (#1614)

* fix: 🐛 Change fields to optional as they get default by backend (#1615)

* Chore/cleanup vault ssh cert (#1616)
This commit is contained in:
Zhihe Li authored and GitHub committed 2023-02-01 13:46:23 -05:00
1 parent e3772352ea
commit f0530e1f8f
34 files changed
+1421 -182

No files matched your search

@@ -2,11 +2,11 @@ import ModelAbility from './model';
/**
* Provides abilities for credential.
*/
export default class CredentialLibaryAbility extends ModelAbility {
export default class CredentialLibraryAbility extends ModelAbility {
// =permissions
/**
* Only "known" credential libary types may be read.
* Only "known" credential library types may be read.
* @type {boolean}
*/
get canRead() {
@@ -67,4 +67,50 @@ export default class GeneratedCredentialLibraryModel extends BaseModel {
description: 'The path in Vault to request credentials from.',
})
path;
@attr('string', {
isNestedAttribute: true,
description: 'The username to use when making an SSH connection.',
})
username;
@attr('string', {
isNestedAttribute: true,
description: 'The desired key type to use when generating a private key.',
})
key_type;
@attr('number', {
isNestedAttribute: true,
description:
'Specifies the number of bits used when generating the private key. Not used if key type is ed25519',
})
key_bits;
@attr('string', {
isNestedAttribute: true,
description: 'Specifies the requested time to live for the certificate.',
})
ttl;
@attr('string', {
isNestedAttribute: true,
description:
'Specifies the key id that the created certificate should have.',
})
key_id;
@attr('object-as-array', {
isNestedAttribute: true,
description:
'Specifies a map of the critical options that the certificate should be signed for.',
})
critical_options;
@attr('object-as-array', {
isNestedAttribute: true,
description:
'Specifies a map of the extensions that the certificate should be signed for.',
})
extensions;
}
+30 -3
View File
@@ -5,12 +5,20 @@ import GeneratedCredentialLibraryModel from '../generated/models/credential-libr
*/
export const options = {
http_method: ['GET', 'POST'],
key_type: ['ed25519', 'ecdsa', 'rsa'],
};
export const TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC = 'vault-generic';
export const TYPE_CREDENTIAL_LIBRARY_VAULT_SSH_CERTIFICATE =
'vault-ssh-certificate';
/**
* Supported Credential Library types.
*/
export const types = ['vault'];
export const TYPES_CREDENTIAL_LIBRARY = Object.freeze([
TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
TYPE_CREDENTIAL_LIBRARY_VAULT_SSH_CERTIFICATE,
]);
export default class CredentialLibraryModel extends GeneratedCredentialLibraryModel {
// =attributes
@@ -20,7 +28,7 @@ export default class CredentialLibraryModel extends GeneratedCredentialLibraryMo
* @type {boolean}
*/
get isUnknown() {
return !types.includes(this.type);
return !TYPES_CREDENTIAL_LIBRARY.includes(this.type);
}
/**
@@ -28,6 +36,25 @@ export default class CredentialLibraryModel extends GeneratedCredentialLibraryMo
* @type {boolean}
*/
get isVault() {
return this.type === 'vault';
return (
this.type === TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC ||
this.type === TYPE_CREDENTIAL_LIBRARY_VAULT_SSH_CERTIFICATE
);
}
/**
* True if credential is a generic vault type.
* @type {boolean}
*/
get isVaultGeneric() {
return this.type === TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC;
}
/**
* True if credential is a vault ssh cert type.
* @type {boolean}
*/
get isVaultSSHCertificate() {
return this.type === TYPE_CREDENTIAL_LIBRARY_VAULT_SSH_CERTIFICATE;
}
}
@@ -1,4 +1,5 @@
import ApplicationSerializer from './application';
import { TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC } from '../models/credential-library';
export default class CredentialLibrarySerializer extends ApplicationSerializer {
// =properties
@@ -16,6 +17,10 @@ export default class CredentialLibrarySerializer extends ApplicationSerializer {
serialize() {
const serialized = super.serialize(...arguments);
if (serialized.attributes) {
if (serialized.type !== TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC) {
delete serialized.attributes.http_method;
}
// Serialize `http_request_body` only if `http_method` is POST
if (!serialized.attributes?.http_method?.match(/post/i))
delete serialized.attributes.http_request_body;
@@ -0,0 +1,22 @@
import Transform from '@ember-data/serializer/transform';
export default class ObjectAsArrayTransform extends Transform {
deserialize(serialized) {
const obj = serialized || {};
return Object.entries(obj).map(([key, value]) => ({ key, value }));
}
serialize(deserialized) {
const array = deserialized || [];
if (!array.length) {
return null;
}
return array.reduce((result, currentValue) => {
const { key, value } = currentValue;
result[key] = value;
return result;
}, {});
}
}
@@ -0,0 +1 @@
export { default } from 'api/transforms/object-as-array';
@@ -2,12 +2,15 @@ import factory from '../generated/factories/credential-library';
import { faker } from '@faker-js/faker';
import permissions from '../helpers/permissions';
import generateId from '../helpers/id';
const types = ['vault'];
import {
TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
TYPE_CREDENTIAL_LIBRARY_VAULT_SSH_CERTIFICATE,
TYPES_CREDENTIAL_LIBRARY,
} from 'api/models/credential-library';
export default factory.extend({
id: () => generateId('cl_'),
type: (i) => types[i % types.length],
type: (i) => TYPES_CREDENTIAL_LIBRARY[i % TYPES_CREDENTIAL_LIBRARY.length],
authorized_actions: () =>
permissions.authorizedActionsFor('credential-library') || [
@@ -19,12 +22,19 @@ export default factory.extend({
attributes() {
switch (this.type) {
case 'vault':
case TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC:
return {
http_method: 'GET',
http_request_body: faker.random.word(),
path: faker.system.directoryPath(),
};
case TYPE_CREDENTIAL_LIBRARY_VAULT_SSH_CERTIFICATE:
return {
username: faker.random.word(),
key_type: faker.random.word(),
key_bits: faker.datatype.number(999),
path: faker.system.directoryPath(),
};
}
},
});
@@ -1,5 +1,6 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC } from 'api/models/credential-library';
module('Unit | Model | credential library', function (hooks) {
setupTest(hooks);
@@ -13,7 +14,9 @@ module('Unit | Model | credential library', function (hooks) {
test('it has isUnknown property and returns the expected values', async function (assert) {
assert.expect(2);
const store = this.owner.lookup('service:store');
const modelA = store.createRecord('credential-library', { type: 'vault' });
const modelA = store.createRecord('credential-library', {
type: TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
});
const modelB = store.createRecord('credential-library', {
type: 'unknown',
});
@@ -1,5 +1,9 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import {
TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
TYPE_CREDENTIAL_LIBRARY_VAULT_SSH_CERTIFICATE,
} from 'api/models/credential-library';
module('Unit | Serializer | credential library', function (hooks) {
setupTest(hooks);
@@ -9,7 +13,7 @@ module('Unit | Serializer | credential library', function (hooks) {
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('credential-library');
const record = store.createRecord('credential-library', {
type: 'vault',
type: TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
name: 'Name',
description: 'Description',
path: '/vault/path',
@@ -19,7 +23,7 @@ module('Unit | Serializer | credential library', function (hooks) {
const snapshot = record._createSnapshot();
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
type: 'vault',
type: TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
credential_store_id: null,
name: 'Name',
description: 'Description',
@@ -27,6 +31,13 @@ module('Unit | Serializer | credential library', function (hooks) {
attributes: {
path: '/vault/path',
http_method: 'GET',
critical_options: null,
extensions: null,
key_bits: null,
key_id: null,
key_type: null,
ttl: null,
username: null,
},
});
});
@@ -40,7 +51,7 @@ module('Unit | Serializer | credential library', function (hooks) {
id: '1',
type: 'credential-library',
attributes: {
type: 'vault',
type: TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
name: 'Name',
description: 'Description',
path: '/vault/path',
@@ -53,13 +64,20 @@ module('Unit | Serializer | credential library', function (hooks) {
const snapshot = record._createSnapshot();
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
type: 'vault',
type: TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
credential_store_id: null,
name: 'Name',
description: 'Description',
attributes: {
path: '/vault/path',
http_method: 'GET',
critical_options: null,
extensions: null,
key_bits: null,
key_id: null,
key_type: null,
ttl: null,
username: null,
},
version: 1,
});
@@ -75,8 +93,14 @@ module('Unit | Serializer | credential library', function (hooks) {
let serializedRecord = record.serialize();
assert.deepEqual(serializedRecord, {
attributes: {
http_method: null,
path: null,
critical_options: null,
extensions: null,
key_bits: null,
key_id: null,
key_type: null,
ttl: null,
username: null,
},
credential_store_id: null,
description: null,
@@ -89,6 +113,7 @@ module('Unit | Serializer | credential library', function (hooks) {
assert.expect(2);
const store = this.owner.lookup('service:store');
const record = store.createRecord('credential-library', {
type: TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
http_method: 'GET',
http_request_body: 'body',
});
@@ -99,11 +124,18 @@ module('Unit | Serializer | credential library', function (hooks) {
attributes: {
http_method: 'GET',
path: null,
critical_options: null,
extensions: null,
key_bits: null,
key_id: null,
key_type: null,
ttl: null,
username: null,
},
credential_store_id: null,
description: null,
name: null,
type: null,
type: TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
},
'http_request_body attribute is not expected'
);
@@ -117,13 +149,66 @@ module('Unit | Serializer | credential library', function (hooks) {
http_method: 'POST',
http_request_body: 'body',
path: null,
critical_options: null,
extensions: null,
key_bits: null,
key_id: null,
key_type: null,
ttl: null,
username: null,
},
credential_store_id: null,
description: null,
name: null,
type: null,
type: TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
},
'http_request_body attribute is expected'
);
});
test('it serializes vault_ssh_certificate correctly', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('credential-library');
store.push({
data: {
id: '1',
type: 'credential-library',
attributes: {
type: TYPE_CREDENTIAL_LIBRARY_VAULT_SSH_CERTIFICATE,
version: 1,
name: 'Name',
description: 'Description',
path: '/vault/path',
username: 'user',
key_type: 'rsa',
key_bits: 100,
ttl: '100',
key_id: 'id',
extensions: [{ key: 'key', value: 'value' }],
critical_options: [{ key: 'key', value: 'value' }],
},
},
});
const record = store.peekRecord('credential-library', '1');
const snapshot = record._createSnapshot();
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
type: TYPE_CREDENTIAL_LIBRARY_VAULT_SSH_CERTIFICATE,
credential_store_id: null,
name: 'Name',
description: 'Description',
attributes: {
path: '/vault/path',
username: 'user',
key_type: 'rsa',
key_bits: 100,
ttl: '100',
key_id: 'id',
extensions: { key: 'value' },
critical_options: { key: 'value' },
},
version: 1,
});
});
});
@@ -0,0 +1,52 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Transform | object as array', function (hooks) {
setupTest(hooks);
test('it deserializes an object to an array of key value objects', function (assert) {
assert.expect(1);
let transform = this.owner.lookup('transform:object-as-array');
const deserialized = transform.deserialize({
first: 'Hey',
second: 'There',
third: 'Everyone',
});
assert.deepEqual(deserialized, [
{ key: 'first', value: 'Hey' },
{ key: 'second', value: 'There' },
{ key: 'third', value: 'Everyone' },
]);
});
test('it deserializes undefined to an empty object', function (assert) {
assert.expect(1);
let transform = this.owner.lookup('transform:object-as-array');
const deserialized = transform.deserialize(undefined);
assert.deepEqual(deserialized, []);
});
test('it serializes an array of key value objects to an object', function (assert) {
assert.expect(1);
let transform = this.owner.lookup('transform:object-as-array');
const serialized = transform.serialize([
{ key: 'first', value: 'Hey' },
{ key: 'second', value: 'There' },
{ key: 'third', value: 'Everyone' },
]);
assert.deepEqual(serialized, {
first: 'Hey',
second: 'There',
third: 'Everyone',
});
});
test('it serializes an empty array of key value objects to null', function (assert) {
assert.expect(1);
let transform = this.owner.lookup('transform:object-as-array');
const serialized = transform.serialize([]);
assert.deepEqual(serialized, null);
});
});