import ( "github.com/hashicorp/vault/api" "github.com/hashicorp/vault/logical" "github.com/hashicorp/vault/logical/storage" ) // initialize Vault API client config := &api.Config{Address: "http://localhost:8200"} client, err := api.NewClient(config) if err != nil { panic(err) } // initialize storage backend storageConfig := &storage.Config{ConnectionURL: "http://localhost:8200"} storage, err := storage.New(storageConfig) if err != nil { panic(err) } // initialize logical backend with storage logical, err := logical.New(storage, &logical.BackendConfig{MountInput: &api.MountInput{ Type: "generic", DefaultLeaseTTL: "10m", MaxLeaseTTL: "30m", }}) if err != nil { panic(err) } // retrieve secret from the generic backend key := "example/secret" secret, err := logical.Read(key) if err != nil { panic(err) } decodedSecret := map[string]interface{}{} err = secret.DecodeJSON(&decodedSecret) if err != nil { panic(err) } fmt.Println(decodedSecret)
import ( "github.com/hashicorp/vault/api" "github.com/hashicorp/vault/logical" "github.com/hashicorp/vault/logical/storage" ) // initialize Vault API client config := &api.Config{Address: "http://localhost:8200"} client, err := api.NewClient(config) if err != nil { panic(err) } // initialize storage backend storageConfig := &storage.Config{ConnectionURL: "http://localhost:8200"} storage, err := storage.New(storageConfig) if err != nil { panic(err) } // initialize logical backend with storage logical, err := logical.New(storage, &logical.BackendConfig{MountInput: &api.MountInput{ Type: "password", DefaultLeaseTTL: "10m", MaxLeaseTTL: "30m", }}) if err != nil { panic(err) } // generate a new password options := map[string]interface{}{ "length": 20, "exclude_upper": true, "exclude_lower": false, "exclude_numbers": false, "exclude_special": true, } password, err := logical.Write("password/generate/example", options) if err != nil { panic(err) } fmt.Println(password.Data["password"])Both examples are using the github.com.hashicorp.vault.logical Storage package to instantiate a logical backend with a specific storage backend (generic and password) and then perform operations on those backends (read and write).