github.com/blend/go-sdk@v1.20220411.3/examples/vault/config/main.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package main
     9  
    10  import (
    11  	"context"
    12  	"encoding/base64"
    13  	"fmt"
    14  	"os"
    15  
    16  	"gopkg.in/yaml.v3"
    17  
    18  	"github.com/blend/go-sdk/logger"
    19  	"github.com/blend/go-sdk/vault"
    20  )
    21  
    22  type myConfig struct {
    23  	Environment string `yaml:"environment" env:"SERVICE_ENV" secret:"environment"`
    24  	Version     string `yaml:"version" env:"SERVICE_VERSION"`
    25  	Secret      string `yaml:"secret" secret:"secret,base64"`
    26  }
    27  
    28  func main() {
    29  	log := logger.All()
    30  	client, _ := vault.New(vault.OptConfigFromEnv(), vault.OptLog(log))
    31  
    32  	keyPath := "configTest"
    33  	err := client.WriteInto(context.TODO(), keyPath, myConfig{
    34  		Environment: "test",
    35  		Version:     "wont be in output",
    36  		Secret:      base64.StdEncoding.EncodeToString([]byte("a super secure one")),
    37  	})
    38  	if err != nil {
    39  		log.Fatal(err)
    40  		os.Exit(1)
    41  	}
    42  
    43  	var cfg myConfig
    44  	err = client.ReadInto(context.TODO(), keyPath, &cfg)
    45  	if err != nil {
    46  		log.Fatal(err)
    47  		os.Exit(1)
    48  	}
    49  	contents, err := yaml.Marshal(cfg)
    50  	if err != nil {
    51  		log.Fatal(err)
    52  		os.Exit(1)
    53  	}
    54  	fmt.Printf("%v\n", string(contents))
    55  }