github.com/hashicorp/vault/sdk@v0.11.0/helper/pluginidentityutil/fields.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package pluginidentityutil
     5  
     6  import (
     7  	"fmt"
     8  	"time"
     9  
    10  	"github.com/hashicorp/vault/sdk/framework"
    11  )
    12  
    13  // PluginIdentityTokenParams contains a set of common parameters that plugins
    14  // can use for setting plugin identity token behavior.
    15  type PluginIdentityTokenParams struct {
    16  	// IdentityTokenTTL is the duration that tokens will be valid for
    17  	IdentityTokenTTL time.Duration `json:"identity_token_ttl"`
    18  	// IdentityTokenAudience identifies the recipient of the token
    19  	IdentityTokenAudience string `json:"identity_token_audience"`
    20  }
    21  
    22  // ParsePluginIdentityTokenFields provides common field parsing to embedding structs.
    23  func (p *PluginIdentityTokenParams) ParsePluginIdentityTokenFields(d *framework.FieldData) error {
    24  	if tokenTTLRaw, ok := d.GetOk("identity_token_ttl"); ok {
    25  		p.IdentityTokenTTL = time.Duration(tokenTTLRaw.(int)) * time.Second
    26  	}
    27  
    28  	if tokenAudienceRaw, ok := d.GetOk("identity_token_audience"); ok {
    29  		p.IdentityTokenAudience = tokenAudienceRaw.(string)
    30  	}
    31  
    32  	return nil
    33  }
    34  
    35  // PopulatePluginIdentityTokenData adds PluginIdentityTokenParams info into the given map.
    36  func (p *PluginIdentityTokenParams) PopulatePluginIdentityTokenData(m map[string]interface{}) {
    37  	m["identity_token_ttl"] = int64(p.IdentityTokenTTL.Seconds())
    38  	m["identity_token_audience"] = p.IdentityTokenAudience
    39  }
    40  
    41  // AddPluginIdentityTokenFields adds plugin identity token fields to the given
    42  // field schema map.
    43  func AddPluginIdentityTokenFields(m map[string]*framework.FieldSchema) {
    44  	fields := map[string]*framework.FieldSchema{
    45  		"identity_token_audience": {
    46  			Type:        framework.TypeString,
    47  			Description: "Audience of plugin identity tokens",
    48  			Default:     "",
    49  		},
    50  		"identity_token_ttl": {
    51  			Type:        framework.TypeDurationSecond,
    52  			Description: "Time-to-live of plugin identity tokens",
    53  			Default:     3600,
    54  		},
    55  	}
    56  
    57  	for name, schema := range fields {
    58  		if _, ok := m[name]; ok {
    59  			panic(fmt.Sprintf("adding field %q would overwrite existing field", name))
    60  		}
    61  		m[name] = schema
    62  	}
    63  }