github.com/hairyhenderson/templater@v3.5.0+incompatible/data/datasource_awssmp.go (about)

     1  package data
     2  
     3  import (
     4  	"path"
     5  
     6  	"github.com/aws/aws-sdk-go/aws"
     7  	"github.com/aws/aws-sdk-go/service/ssm"
     8  	"github.com/pkg/errors"
     9  
    10  	gaws "github.com/hairyhenderson/gomplate/aws"
    11  )
    12  
    13  // awssmpGetter - A subset of SSM API for use in unit testing
    14  type awssmpGetter interface {
    15  	GetParameter(*ssm.GetParameterInput) (*ssm.GetParameterOutput, error)
    16  }
    17  
    18  func parseAWSSMPArgs(origPath string, args ...string) (paramPath string, err error) {
    19  	paramPath = origPath
    20  	if len(args) >= 1 {
    21  		paramPath = path.Join(paramPath, args[0])
    22  	}
    23  
    24  	if len(args) >= 2 {
    25  		err = errors.New("Maximum two arguments to aws+smp datasource: alias, extraPath")
    26  	}
    27  	return
    28  }
    29  
    30  func readAWSSMP(source *Source, args ...string) (output []byte, err error) {
    31  	if source.asmpg == nil {
    32  		source.asmpg = ssm.New(gaws.SDKSession())
    33  	}
    34  
    35  	paramPath, err := parseAWSSMPArgs(source.URL.Path, args...)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	source.mediaType = jsonMimetype
    41  	return readAWSSMPParam(source, paramPath)
    42  }
    43  
    44  func readAWSSMPParam(source *Source, paramPath string) ([]byte, error) {
    45  	input := &ssm.GetParameterInput{
    46  		Name:           aws.String(paramPath),
    47  		WithDecryption: aws.Bool(true),
    48  	}
    49  
    50  	response, err := source.asmpg.GetParameter(input)
    51  
    52  	if err != nil {
    53  		return nil, errors.Wrapf(err, "Error reading aws+smp from AWS using GetParameter with input %v", input)
    54  	}
    55  
    56  	result := *response.Parameter
    57  
    58  	output, err := ToJSON(result)
    59  	return []byte(output), err
    60  }