2015-09-21 06:31:12 -04:00
|
|
|
/*
|
|
|
|
Copyright 2015 Home Office All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
)
|
|
|
|
|
2015-09-21 11:35:59 -04:00
|
|
|
const (
|
|
|
|
renewalMinimum = 0.8
|
|
|
|
renewalMaximum = 0.95
|
|
|
|
)
|
2015-09-21 06:31:12 -04:00
|
|
|
|
2015-10-12 06:14:50 -04:00
|
|
|
// watchedResource is a resource which is being watched - i.e. when the item is coming up for renewal
|
2015-09-21 06:31:12 -04:00
|
|
|
// lets grab it and renew the lease
|
|
|
|
type watchedResource struct {
|
|
|
|
// the resource itself
|
2015-09-23 17:39:50 -04:00
|
|
|
resource *VaultResource
|
2015-09-21 06:31:12 -04:00
|
|
|
// the last time the resource was retrieved
|
|
|
|
lastUpdated time.Time
|
|
|
|
// the time which the lease expires
|
|
|
|
leaseExpireTime time.Time
|
|
|
|
// the duration until we next time to renew lease
|
|
|
|
renewalTime time.Duration
|
|
|
|
// the secret
|
|
|
|
secret *api.Secret
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:14:50 -04:00
|
|
|
// notifyOnRenewal creates a trigger and notifies when a resource is up for renewal
|
2015-09-21 06:31:12 -04:00
|
|
|
func (r *watchedResource) notifyOnRenewal(ch chan *watchedResource) {
|
|
|
|
go func() {
|
|
|
|
// step: check if the resource has a pre-configured renewal time
|
|
|
|
r.renewalTime = r.resource.update
|
|
|
|
// step: if the answer is no, we set the notification between 80-95% of the lease time of the secret
|
|
|
|
if r.renewalTime <= 0 {
|
2015-09-21 11:35:59 -04:00
|
|
|
r.renewalTime = r.calculateRenewal()
|
2015-09-21 06:31:12 -04:00
|
|
|
}
|
|
|
|
glog.V(3).Infof("setting a renewal notification on resource: %s, time: %s", r.resource, r.renewalTime)
|
|
|
|
// step: wait for the duration
|
|
|
|
<-time.After(r.renewalTime)
|
|
|
|
// step: send the notification on the renewal channel
|
|
|
|
ch <- r
|
|
|
|
}()
|
|
|
|
}
|
2015-09-21 11:35:59 -04:00
|
|
|
|
2015-10-12 06:14:50 -04:00
|
|
|
// calculateRenewal calculate the renewal between
|
2015-09-21 11:35:59 -04:00
|
|
|
func (r watchedResource) calculateRenewal() time.Duration {
|
2015-10-14 09:17:17 -04:00
|
|
|
return time.Duration(getDurationWithin(
|
2015-09-21 11:35:59 -04:00
|
|
|
int(float64(r.secret.LeaseDuration)*renewalMinimum),
|
|
|
|
int(float64(r.secret.LeaseDuration)*renewalMaximum))) * time.Second
|
|
|
|
}
|