github.com/hashicorp/vault/sdk@v0.13.0/plugin/mock/path_env.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package mock
     5  
     6  import (
     7  	"context"
     8  	"os"
     9  
    10  	"github.com/hashicorp/vault/sdk/framework"
    11  	"github.com/hashicorp/vault/sdk/logical"
    12  )
    13  
    14  // pathEnv is used to interrogate plugin env vars.
    15  func pathEnv(b *backend) *framework.Path {
    16  	return &framework.Path{
    17  		Pattern: "env/" + framework.GenericNameRegex("key"),
    18  		Fields: map[string]*framework.FieldSchema{
    19  			"key": {
    20  				Type:        framework.TypeString,
    21  				Required:    true,
    22  				Description: "The name of the environment variable to read.",
    23  			},
    24  		},
    25  		Callbacks: map[logical.Operation]framework.OperationFunc{
    26  			logical.ReadOperation: b.pathEnvRead,
    27  		},
    28  	}
    29  }
    30  
    31  func (b *backend) pathEnvRead(_ context.Context, _ *logical.Request, data *framework.FieldData) (*logical.Response, error) {
    32  	// Return the secret
    33  	return &logical.Response{
    34  		Data: map[string]interface{}{
    35  			"key": os.Getenv(data.Get("key").(string)),
    36  		},
    37  	}, nil
    38  }