2023-03-15 16:00:52 +00:00
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
2016-02-24 21:42:20 +00:00
package cert
import (
2018-01-08 18:31:38 +00:00
"context"
2021-04-22 15:20:59 +00:00
"fmt"
2016-02-24 21:42:20 +00:00
2019-04-12 21:54:35 +00:00
"github.com/hashicorp/vault/sdk/framework"
2019-04-13 07:44:06 +00:00
"github.com/hashicorp/vault/sdk/logical"
2016-02-24 21:42:20 +00:00
)
2022-11-21 16:39:24 +00:00
const maxCacheSize = 100000
2016-02-24 21:42:20 +00:00
func pathConfig ( b * backend ) * framework . Path {
return & framework . Path {
Pattern : "config" ,
2023-04-07 17:12:56 +00:00
DisplayAttrs : & framework . DisplayAttributes {
OperationPrefix : operationPrefixCert ,
} ,
2016-02-24 21:42:20 +00:00
Fields : map [ string ] * framework . FieldSchema {
2021-04-08 16:43:39 +00:00
"disable_binding" : {
2016-02-24 21:42:20 +00:00
Type : framework . TypeBool ,
2016-02-25 01:34:07 +00:00
Default : false ,
Description : ` If set, during renewal, skips the matching of presented client identity with the client identity used during login. Defaults to false. ` ,
2016-02-24 21:42:20 +00:00
} ,
2022-08-23 18:03:53 +00:00
"enable_identity_alias_metadata" : {
Type : framework . TypeBool ,
Default : false ,
Description : ` If set, metadata of the certificate including the metadata corresponding to allowed_metadata_extensions will be stored in the alias. Defaults to false. ` ,
} ,
2022-11-21 16:39:24 +00:00
"ocsp_cache_size" : {
Type : framework . TypeInt ,
Default : 100 ,
Description : ` The size of the in memory OCSP response cache, shared by all configured certs ` ,
} ,
2016-02-24 21:42:20 +00:00
} ,
2023-04-07 17:12:56 +00:00
Operations : map [ logical . Operation ] framework . OperationHandler {
logical . UpdateOperation : & framework . PathOperation {
Callback : b . pathConfigWrite ,
DisplayAttrs : & framework . DisplayAttributes {
OperationVerb : "configure" ,
} ,
} ,
logical . ReadOperation : & framework . PathOperation {
Callback : b . pathConfigRead ,
DisplayAttrs : & framework . DisplayAttributes {
OperationSuffix : "configuration" ,
} ,
} ,
2016-02-24 21:42:20 +00:00
} ,
}
}
2018-01-08 18:31:38 +00:00
func ( b * backend ) pathConfigWrite ( ctx context . Context , req * logical . Request , data * framework . FieldData ) ( * logical . Response , error ) {
2022-11-21 16:39:24 +00:00
config , err := b . Config ( ctx , req . Storage )
2016-02-24 21:42:20 +00:00
if err != nil {
return nil , err
}
2022-11-21 16:39:24 +00:00
if disableBindingRaw , ok := data . GetOk ( "disable_binding" ) ; ok {
config . DisableBinding = disableBindingRaw . ( bool )
}
if enableIdentityAliasMetadataRaw , ok := data . GetOk ( "enable_identity_alias_metadata" ) ; ok {
config . EnableIdentityAliasMetadata = enableIdentityAliasMetadataRaw . ( bool )
}
if cacheSizeRaw , ok := data . GetOk ( "ocsp_cache_size" ) ; ok {
cacheSize := cacheSizeRaw . ( int )
if cacheSize < 2 || cacheSize > maxCacheSize {
return logical . ErrorResponse ( "invalid cache size, must be >= 2 and <= %d" , maxCacheSize ) , nil
}
config . OcspCacheSize = cacheSize
}
if err := b . storeConfig ( ctx , req . Storage , config ) ; err != nil {
2016-02-24 21:42:20 +00:00
return nil , err
}
return nil , nil
}
2022-08-23 18:03:53 +00:00
func ( b * backend ) pathConfigRead ( ctx context . Context , req * logical . Request , d * framework . FieldData ) ( * logical . Response , error ) {
cfg , err := b . Config ( ctx , req . Storage )
if err != nil {
return nil , err
}
data := map [ string ] interface { } {
"disable_binding" : cfg . DisableBinding ,
"enable_identity_alias_metadata" : cfg . EnableIdentityAliasMetadata ,
2022-11-21 16:39:24 +00:00
"ocsp_cache_size" : cfg . OcspCacheSize ,
2022-08-23 18:03:53 +00:00
}
return & logical . Response {
Data : data ,
} , nil
}
2016-02-24 21:42:20 +00:00
// Config returns the configuration for this backend.
2018-01-19 06:44:44 +00:00
func ( b * backend ) Config ( ctx context . Context , s logical . Storage ) ( * config , error ) {
entry , err := s . Get ( ctx , "config" )
2016-02-24 21:42:20 +00:00
if err != nil {
return nil , err
}
2016-02-25 01:34:07 +00:00
// Returning a default configuration if an entry is not found
2016-02-24 21:42:20 +00:00
var result config
2016-02-25 01:34:07 +00:00
if entry != nil {
if err := entry . DecodeJSON ( & result ) ; err != nil {
2021-04-22 15:20:59 +00:00
return nil , fmt . Errorf ( "error reading configuration: %w" , err )
2016-02-25 01:34:07 +00:00
}
2016-02-24 21:42:20 +00:00
}
return & result , nil
}
type config struct {
2022-08-23 18:03:53 +00:00
DisableBinding bool ` json:"disable_binding" `
EnableIdentityAliasMetadata bool ` json:"enable_identity_alias_metadata" `
2022-11-21 16:39:24 +00:00
OcspCacheSize int ` json:"ocsp_cache_size" `
2016-02-24 21:42:20 +00:00
}