2015-09-09 19:24:45 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2015-09-09 19:24:45 +00:00
|
|
|
"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
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *SystemBackend) tuneMountTTLs(ctx context.Context, path string, me *MountEntry, newDefault, newMax time.Duration) error {
|
2017-09-05 14:57:25 +00:00
|
|
|
zero := time.Duration(0)
|
2017-03-02 19:37:59 +00:00
|
|
|
|
2017-09-05 14:57:25 +00:00
|
|
|
switch {
|
|
|
|
case newDefault == zero && newMax == zero:
|
|
|
|
// No checks needed
|
2015-09-09 19:24:45 +00:00
|
|
|
|
2017-09-05 14:57:25 +00:00
|
|
|
case newDefault == zero && newMax != zero:
|
|
|
|
// No default/max conflict, no checks needed
|
2015-09-09 19:24:45 +00:00
|
|
|
|
2017-09-05 14:57:25 +00:00
|
|
|
case newDefault != zero && newMax == zero:
|
|
|
|
// No default/max conflict, no checks needed
|
2015-09-21 13:39:37 +00:00
|
|
|
|
2017-09-05 14:57:25 +00:00
|
|
|
case newDefault != zero && newMax != zero:
|
|
|
|
if newMax < newDefault {
|
|
|
|
return fmt.Errorf("backend max lease TTL of %d would be less than backend default lease TTL of %d",
|
|
|
|
int(newMax.Seconds()), int(newDefault.Seconds()))
|
2015-09-09 19:24:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-05 14:57:25 +00:00
|
|
|
origMax := me.Config.MaxLeaseTTL
|
|
|
|
origDefault := me.Config.DefaultLeaseTTL
|
2016-05-03 18:24:04 +00:00
|
|
|
|
2017-09-05 14:57:25 +00:00
|
|
|
me.Config.MaxLeaseTTL = newMax
|
|
|
|
me.Config.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 {
|
2017-10-23 19:35:28 +00:00
|
|
|
case strings.HasPrefix(path, credentialRoutePrefix):
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Core.persistAuth(ctx, b.Core.auth, me.Local)
|
2016-05-03 18:24:04 +00:00
|
|
|
default:
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Core.persistMounts(ctx, b.Core.mounts, me.Local)
|
2016-05-03 18:24:04 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2017-09-05 14:57:25 +00:00
|
|
|
me.Config.MaxLeaseTTL = origMax
|
|
|
|
me.Config.DefaultLeaseTTL = origDefault
|
2016-05-03 18:24:04 +00:00
|
|
|
return fmt.Errorf("failed to update mount table, rolling back TTL changes")
|
2015-09-09 19:24:45 +00:00
|
|
|
}
|
2016-08-19 20:45:17 +00:00
|
|
|
if b.Core.logger.IsInfo() {
|
2017-09-05 14:57:25 +00:00
|
|
|
b.Core.logger.Info("core: mount tuning of leases successful", "path", path)
|
2016-08-19 20:45:17 +00:00
|
|
|
}
|
2015-09-09 19:24:45 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|