2023-03-15 16:00:52 +00:00
/ * *
* Copyright ( c ) HashiCorp , Inc .
* SPDX - License - Identifier : MPL - 2.0
* /
2022-09-21 14:41:44 +00:00
import Model , { attr } from '@ember-data/model' ;
2022-12-01 01:24:40 +00:00
import { inject as service } from '@ember/service' ;
2022-12-16 22:13:59 +00:00
import lazyCapabilities , { apiPath } from 'vault/macros/lazy-capabilities' ;
2022-12-01 01:24:40 +00:00
import { withFormFields } from 'vault/decorators/model-form-fields' ;
2022-12-08 22:22:33 +00:00
import { withModelValidations } from 'vault/decorators/model-validations' ;
2022-09-21 14:41:44 +00:00
2022-12-08 22:22:33 +00:00
const validations = {
type : [ { type : 'presence' , message : 'Type is required.' } ] ,
keyType : [ { type : 'presence' , message : 'Please select a key type.' } ] ,
2023-04-11 21:04:35 +00:00
keyName : [
{
validator ( model ) {
if ( model . keyName === 'default' ) return false ;
return true ;
} ,
message : ` Key name cannot be the reserved value 'default' ` ,
} ,
] ,
2022-12-08 22:22:33 +00:00
} ;
const displayFields = [ 'keyId' , 'keyName' , 'keyType' , 'keyBits' ] ;
const formFieldGroups = [ { default : [ 'keyName' , 'type' ] } , { 'Key parameters' : [ 'keyType' , 'keyBits' ] } ] ;
@ withModelValidations ( validations )
@ withFormFields ( displayFields , formFieldGroups )
2022-11-10 21:27:19 +00:00
export default class PkiKeyModel extends Model {
2022-12-01 01:24:40 +00:00
@ service secretMountPath ;
2022-11-23 19:45:49 +00:00
@ attr ( 'string' , { detailsLabel : 'Key ID' } ) keyId ;
2022-12-21 04:46:25 +00:00
@ attr ( 'string' , {
subText : ` Optional, human-readable name for this key. The name must be unique across all keys and cannot be 'default'. ` ,
} )
keyName ;
2022-12-08 22:22:33 +00:00
@ attr ( 'string' , {
noDefault : true ,
possibleValues : [ 'internal' , 'exported' ] ,
subText :
'The type of operation. If exported, the private key will be returned in the response; if internal the private key will not be returned and cannot be retrieved later.' ,
} )
type ;
@ attr ( 'string' , {
noDefault : true ,
possibleValues : [ 'rsa' , 'ec' , 'ed25519' ] ,
subText : 'The type of key that will be generated. Must be rsa, ed25519, or ec. ' ,
} )
keyType ;
@ attr ( 'string' , {
label : 'Key bits' ,
noDefault : true ,
subText : 'Bit length of the key to generate.' ,
} )
keyBits ; // no possibleValues because dependent on selected key type
2022-11-23 19:45:49 +00:00
2022-12-21 04:46:25 +00:00
@ attr ( 'string' ) pemBundle ;
@ attr ( 'string' ) privateKey ;
2022-12-01 01:24:40 +00:00
get backend ( ) {
return this . secretMountPath . currentPath ;
2022-11-23 19:45:49 +00:00
}
2022-12-16 22:13:59 +00:00
/ * C A P A B I L I T I E S
* Default to show UI elements unless we know they can ' t access the given path
* /
2023-01-04 02:00:29 +00:00
@ lazyCapabilities ( apiPath ` ${ 'backend' } /key/ ${ 'keyId' } ` , 'backend' , 'keyId' ) keyPath ;
2022-12-16 22:13:59 +00:00
get canRead ( ) {
return this . keyPath . get ( 'canRead' ) !== false ;
}
get canEdit ( ) {
return this . keyPath . get ( 'canUpdate' ) !== false ;
}
get canDelete ( ) {
return this . keyPath . get ( 'canDelete' ) !== false ;
}
@ lazyCapabilities ( apiPath ` ${ 'backend' } /keys/generate ` , 'backend' ) generatePath ;
get canGenerateKey ( ) {
return this . generatePath . get ( 'canUpdate' ) !== false ;
}
@ lazyCapabilities ( apiPath ` ${ 'backend' } /keys/import ` , 'backend' ) importPath ;
get canImportKey ( ) {
return this . importPath . get ( 'canUpdate' ) !== false ;
}
2022-09-21 14:41:44 +00:00
}