github.com/hashicorp/vault/sdk@v0.13.0/logical/lease.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package logical
     5  
     6  import (
     7  	"time"
     8  )
     9  
    10  // LeaseOptions is an embeddable struct to capture common lease
    11  // settings between a Secret and Auth
    12  type LeaseOptions struct {
    13  	// TTL is the duration that this secret is valid for. Vault
    14  	// will automatically revoke it after the duration.
    15  	TTL time.Duration `json:"lease"`
    16  
    17  	// MaxTTL is the maximum duration that this secret is valid for.
    18  	MaxTTL time.Duration `json:"max_ttl"`
    19  
    20  	// Renewable, if true, means that this secret can be renewed.
    21  	Renewable bool `json:"renewable"`
    22  
    23  	// Increment will be the lease increment that the user requested.
    24  	// This is only available on a Renew operation and has no effect
    25  	// when returning a response.
    26  	Increment time.Duration `json:"-"`
    27  
    28  	// IssueTime is the time of issue for the original lease. This is
    29  	// only available on Renew and Revoke operations and has no effect when returning
    30  	// a response. It can be used to enforce maximum lease periods by
    31  	// a logical backend.
    32  	IssueTime time.Time `json:"-"`
    33  }
    34  
    35  // LeaseEnabled checks if leasing is enabled
    36  func (l *LeaseOptions) LeaseEnabled() bool {
    37  	return l.TTL > 0
    38  }
    39  
    40  // LeaseTotal is the lease duration with a guard against a negative TTL
    41  func (l *LeaseOptions) LeaseTotal() time.Duration {
    42  	if l.TTL <= 0 {
    43  		return 0
    44  	}
    45  
    46  	return l.TTL
    47  }
    48  
    49  // ExpirationTime computes the time until expiration including the grace period
    50  func (l *LeaseOptions) ExpirationTime() time.Time {
    51  	var expireTime time.Time
    52  	if l.LeaseEnabled() {
    53  		expireTime = time.Now().Add(l.LeaseTotal())
    54  	}
    55  	return expireTime
    56  }