2015-09-09 19:24:45 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-05-03 18:24:04 +00:00
|
|
|
"strings"
|
2015-09-21 13:39:37 +00:00
|
|
|
"time"
|
2015-09-09 19:24:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// tuneMount is used to set config on a mount point
|
2015-09-21 13:39:37 +00:00
|
|
|
func (b *SystemBackend) tuneMountTTLs(path string, meConfig *MountConfig, newDefault, newMax *time.Duration) error {
|
|
|
|
if newDefault == nil && newMax == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if newDefault == nil && newMax != nil &&
|
|
|
|
*newMax == meConfig.MaxLeaseTTL {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if newMax == nil && newDefault != nil &&
|
|
|
|
*newDefault == meConfig.DefaultLeaseTTL {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if newMax != nil && newDefault != nil &&
|
|
|
|
*newDefault == meConfig.DefaultLeaseTTL &&
|
|
|
|
*newMax == meConfig.MaxLeaseTTL {
|
2015-09-09 19:24:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-21 13:39:37 +00:00
|
|
|
if newMax != nil && newDefault != nil && *newMax < *newDefault {
|
2015-09-25 13:46:20 +00:00
|
|
|
return fmt.Errorf("new backend max lease TTL of %d less than new backend default lease TTL of %d",
|
|
|
|
int(newMax.Seconds()), int(newDefault.Seconds()))
|
2015-09-09 19:24:45 +00:00
|
|
|
}
|
|
|
|
|
2015-09-21 13:39:37 +00:00
|
|
|
if newMax != nil && newDefault == nil {
|
|
|
|
if meConfig.DefaultLeaseTTL != 0 && *newMax < meConfig.DefaultLeaseTTL {
|
2015-09-25 13:46:20 +00:00
|
|
|
return fmt.Errorf("new backend max lease TTL of %d less than backend default lease TTL of %d",
|
|
|
|
int(newMax.Seconds()), int(meConfig.DefaultLeaseTTL.Seconds()))
|
2015-09-09 19:24:45 +00:00
|
|
|
}
|
2015-09-21 13:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if newDefault != nil {
|
|
|
|
if meConfig.MaxLeaseTTL == 0 {
|
2015-10-12 18:57:43 +00:00
|
|
|
if newMax == nil && *newDefault > b.Core.maxLeaseTTL {
|
2015-09-25 13:46:20 +00:00
|
|
|
return fmt.Errorf("new backend default lease TTL of %d greater than system max lease TTL of %d",
|
|
|
|
int(newDefault.Seconds()), int(b.Core.maxLeaseTTL.Seconds()))
|
2015-09-21 13:39:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-05-12 11:22:00 +00:00
|
|
|
if newMax == nil && *newDefault > meConfig.MaxLeaseTTL {
|
2015-09-25 13:46:20 +00:00
|
|
|
return fmt.Errorf("new backend default lease TTL of %d greater than backend max lease TTL of %d",
|
|
|
|
int(newDefault.Seconds()), int(meConfig.MaxLeaseTTL.Seconds()))
|
2015-09-21 13:39:37 +00:00
|
|
|
}
|
2015-09-09 19:24:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 18:24:04 +00:00
|
|
|
origMax := meConfig.MaxLeaseTTL
|
|
|
|
origDefault := meConfig.DefaultLeaseTTL
|
|
|
|
|
2015-09-21 13:39:37 +00:00
|
|
|
if newMax != nil {
|
|
|
|
meConfig.MaxLeaseTTL = *newMax
|
|
|
|
}
|
|
|
|
if newDefault != nil {
|
|
|
|
meConfig.DefaultLeaseTTL = *newDefault
|
|
|
|
}
|
2015-09-09 19:24:45 +00:00
|
|
|
|
|
|
|
// Update the mount table
|
2016-05-03 18:24:04 +00:00
|
|
|
var err error
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(path, "auth/"):
|
|
|
|
err = b.Core.persistAuth(b.Core.auth)
|
|
|
|
default:
|
|
|
|
err = b.Core.persistMounts(b.Core.mounts)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
meConfig.MaxLeaseTTL = origMax
|
|
|
|
meConfig.DefaultLeaseTTL = origDefault
|
|
|
|
return fmt.Errorf("failed to update mount table, rolling back TTL changes")
|
2015-09-09 19:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b.Core.logger.Printf("[INFO] core: tuned '%s'", path)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|