本文整理匯總了Golang中github.com/hashicorp/vault/api.NewClient函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewClient函數的具體用法?Golang NewClient怎麽用?Golang NewClient使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewClient函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: mountServer
func (d *driver) mountServer(mountpoint string) (*fuse.Server, error) {
if err := os.MkdirAll(filepath.Dir(mountpoint), 0755); err != nil {
return nil, err
}
conf := api.DefaultConfig()
client, err := api.NewClient(conf)
if err != nil {
return nil, err
}
ownership := keywhizfs.NewOwnership("root", "root")
kwfs, root := NewFs(client, ownership)
mountOptions := &fuse.MountOptions{
AllowOther: true,
Name: kwfs.String(),
Options: []string{"default_permissions"},
}
// Empty Options struct avoids setting a global uid/gid override.
conn := nodefs.NewFileSystemConnector(root, &nodefs.Options{})
server, err := fuse.NewServer(conn.RawFS(), mountpoint, mountOptions)
if err != nil {
log.Printf("Mount fail: %v\n", err)
return nil, err
}
go server.Serve()
return server, nil
}
示例2: NewClient
// NewClient returns a VaultClient object or error
func NewClient(config *VaultConfig) (*VaultClient, error) {
vc := VaultClient{}
c, err := api.NewClient(&api.Config{Address: config.Server})
vc.client = c
vc.config = config
return &vc, err
}
示例3: remove
func remove(keyname string) error {
TOKEN = vLogin("", "", "ldap")
if TOKEN == "" {
return fmt.Errorf("%s", "User Login failed")
}
// Get element list
EPATH := fmt.Sprint("secret/usr/", ADNAME)
ELIST := getVaultValue(EPATH, TOKEN)
//Split the list into an array
EARRAY := strings.Split(ELIST.Data[LISTNAME].(string), ",")
TNUM := -1
// Search the array for the keyname value
for lup, v := range EARRAY {
if v == keyname {
TNUM = lup
break
}
}
// If value isn't found, report it
if TNUM == -1 {
fmt.Println("Key name was not found in the list")
return &errorString{"KeyError"}
}
// Delete the keyname
config := tls.Config{InsecureSkipVerify: true}
cnfg := vault.DefaultConfig()
cnfg.Address = VAULTSERVER
cnfg.HttpClient.Transport = &http.Transport{
TLSClientConfig: &config,
}
client, err := vault.NewClient(cnfg)
if err != nil {
return err
}
client.SetToken(TOKEN)
SPATH := fmt.Sprint("secret/usr/", ADNAME, "/", keyname)
_, err = client.Logical().Delete(SPATH)
if err != nil {
return err
}
//Remove the key from the key list
var s map[string]interface{}
if TNUM == -1 || TNUM == 0 {
s = map[string]interface{}{LISTNAME: ""}
} else if TNUM > 0 {
elst1 := strings.Join(EARRAY[:TNUM], ",")
elst2 := strings.Join(EARRAY[TNUM+1:], ",")
elst := strings.Join([]string{elst1, elst2}, ",")
s = map[string]interface{}{LISTNAME: elst}
}
// And write it to the Vault
err = putVaultValue(EPATH, TOKEN, s)
return err
}
示例4: Client
func Client(token string) (*api.Client, error) {
client, err := api.NewClient(DefaultConfig)
if err != nil {
return nil, err
}
client.SetToken(token)
return client, nil
}
示例5: newVaultClient
// newVaultClient creates a new client for connecting to vault.
func newVaultClient(config *Config) (*vaultapi.Client, error) {
log.Printf("[INFO] (runner) creating vault/api client")
vaultConfig := vaultapi.DefaultConfig()
if config.Vault.Address != "" {
log.Printf("[DEBUG] (runner) setting vault address to %s", config.Vault.Address)
vaultConfig.Address = config.Vault.Address
}
if config.Vault.SSL.Enabled {
log.Printf("[DEBUG] (runner) enabling vault SSL")
tlsConfig := &tls.Config{}
if config.Vault.SSL.Cert != "" {
cert, err := tls.LoadX509KeyPair(config.Vault.SSL.Cert, config.Vault.SSL.Cert)
if err != nil {
return nil, err
}
tlsConfig.Certificates = []tls.Certificate{cert}
}
if config.Vault.SSL.CaCert != "" {
cacert, err := ioutil.ReadFile(config.Vault.SSL.CaCert)
if err != nil {
return nil, err
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(cacert)
tlsConfig.RootCAs = caCertPool
}
tlsConfig.BuildNameToCertificate()
if !config.Vault.SSL.Verify {
log.Printf("[WARN] (runner) disabling vault SSL verification")
tlsConfig.InsecureSkipVerify = true
}
vaultConfig.HttpClient.Transport = &http.Transport{
TLSClientConfig: tlsConfig,
}
}
client, err := vaultapi.NewClient(vaultConfig)
if err != nil {
return nil, err
}
if config.Vault.Token != "" {
log.Printf("[DEBUG] (runner) setting vault token")
client.SetToken(config.Vault.Token)
}
return client, nil
}
示例6: TestHTTP_Fallback_Disabled
func TestHTTP_Fallback_Disabled(t *testing.T) {
handler1 := http.NewServeMux()
handler2 := http.NewServeMux()
handler3 := http.NewServeMux()
coreConfig := &vault.CoreConfig{
LogicalBackends: map[string]logical.Factory{
"transit": transit.Factory,
},
ClusterAddr: "empty",
}
// Chicken-and-egg: Handler needs a core. So we create handlers first, then
// add routes chained to a Handler-created handler.
cores := vault.TestCluster(t, []http.Handler{handler1, handler2, handler3}, coreConfig, true)
for _, core := range cores {
defer core.CloseListeners()
}
handler1.Handle("/", Handler(cores[0].Core))
handler2.Handle("/", Handler(cores[1].Core))
handler3.Handle("/", Handler(cores[2].Core))
// make it easy to get access to the active
core := cores[0].Core
vault.TestWaitActive(t, core)
root := cores[0].Root
addrs := []string{
fmt.Sprintf("https://127.0.0.1:%d", cores[1].Listeners[0].Address.Port),
fmt.Sprintf("https://127.0.0.1:%d", cores[2].Listeners[0].Address.Port),
}
for _, addr := range addrs {
config := api.DefaultConfig()
config.Address = addr
config.HttpClient = cleanhttp.DefaultClient()
config.HttpClient.Transport.(*http.Transport).TLSClientConfig = cores[0].TLSConfig
client, err := api.NewClient(config)
if err != nil {
t.Fatal(err)
}
client.SetToken(root)
secret, err := client.Auth().Token().LookupSelf()
if err != nil {
t.Fatal(err)
}
if secret == nil {
t.Fatal("secret is nil")
}
if secret.Data["id"].(string) != root {
t.Fatal("token mismatch")
}
}
}
示例7: newClientFromConfig
func newClientFromConfig(config *api.Config, token string) (*api.Client, error) {
client, err := api.NewClient(config)
if err != nil {
return nil, maskAny(err)
}
if token != "" {
client.SetToken(token)
}
return client, nil
}
示例8: Client
func Client(token string) (*api.Client, error) {
if DefaultConfig == nil {
return nil, fmt.Errorf("Config was nil")
}
client, err := api.NewClient(DefaultConfig)
if err != nil {
return nil, err
}
client.SetToken(token)
return client, nil
}
示例9: testClient
func testClient(t *testing.T, addr string, token string) *api.Client {
config := api.DefaultConfig()
config.Address = addr
client, err := api.NewClient(config)
if err != nil {
t.Fatalf("err: %s", err)
}
client.SetToken(token)
return client
}
示例10: createVaultClient
func createVaultClient() *vault.Client {
log.Info("Create vault client")
config := &vault.Config{
Address: "http://localhost:8200",
HttpClient: http.DefaultClient,
}
vc, err := vault.NewClient(config)
fatal(err)
return vc
}
示例11: vaultAuthenticate
func (vb *vaultBackend) vaultAuthenticate(vaultURI string, token string) error {
vb.config = api.DefaultConfig()
vb.config.Address = vaultURI
client, err := api.NewClient(vb.config) //can probably be global
if err != nil {
glog.Errorf("Error authenticating %s\n", err.Error())
return err
}
client.SetToken(token) //TODO put here the howler token to be read from file
vb.client = client
return nil
}
示例12: New
// New returns a new VaultFS
func New(config *api.Config, mountpoint, token, root string) (*VaultFS, error) {
client, err := api.NewClient(config)
if err != nil {
return nil, err
}
client.SetToken(token)
return &VaultFS{
Client: client,
root: root,
mountpoint: mountpoint,
}, nil
}
示例13: initVault
func initVault() {
// set up vault client
var client *vault.Client
if viper.GetString("vault-cubbyhole-token") != "" || viper.GetString("vault-token") != "" {
config := vault.DefaultConfig()
err := config.ReadEnvironment()
if err != nil {
log.WithError(err).Fatal("Error reading environment for Vault configuration")
}
client, err = vault.NewClient(config)
if err != nil {
log.WithError(err).Fatal("Error initializing Vault client")
}
}
// unwrap real token
if wrapped := viper.GetString("vault-cubbyhole-token"); wrapped != "" {
token, err := client.Logical().Unwrap(wrapped)
if err != nil {
log.WithError(err).Fatal("Error unwrapping token")
} else if token.WrapInfo != nil {
log.Fatal("Secret appears to be doubly wrapped")
} else if token.Auth == nil {
log.Fatal("Secret contained no auth data")
}
viper.Set("vault-token", token.Auth.ClientToken)
}
// read secrets from vault
if token := viper.GetString("vault-token"); token != "" {
client.SetToken(token)
secret, err := client.Logical().Read("secret/mantl-api")
if err != nil {
log.WithError(err).Fatal("Error reading secret/mantl-api")
}
for _, secretName := range []string{
"mesos-principal", "mesos-secret",
"marathon-user", "marathon-password",
} {
secretValue, ok := secret.Data[secretName].(string)
if ok {
viper.Set(secretName, secretValue)
} else {
log.Warnf("secret/mantl-api didn't contain %s", secretName)
}
}
}
}
示例14: Fingerprint
func (f *VaultFingerprint) Fingerprint(config *client.Config, node *structs.Node) (bool, error) {
if config.VaultConfig == nil || !config.VaultConfig.IsEnabled() {
return false, nil
}
// Only create the client once to avoid creating too many connections to
// Vault.
if f.client == nil {
vaultConfig, err := config.VaultConfig.ApiConfig()
if err != nil {
return false, fmt.Errorf("Failed to initialize the Vault client config: %v", err)
}
f.client, err = vapi.NewClient(vaultConfig)
if err != nil {
return false, fmt.Errorf("Failed to initialize Vault client: %s", err)
}
}
// Connect to vault and parse its information
status, err := f.client.Sys().SealStatus()
if err != nil {
// Clear any attributes set by a previous fingerprint.
f.clearVaultAttributes(node)
// Print a message indicating that Vault is not available anymore
if f.lastState == vaultAvailable {
f.logger.Printf("[INFO] fingerprint.vault: Vault is unavailable")
}
f.lastState = vaultUnavailable
return false, nil
}
node.Attributes["vault.accessible"] = strconv.FormatBool(true)
// We strip the Vault prefix becasue < 0.6.2 the version looks like:
// status.Version = "Vault v0.6.1"
node.Attributes["vault.version"] = strings.TrimPrefix(status.Version, "Vault ")
node.Attributes["vault.cluster_id"] = status.ClusterID
node.Attributes["vault.cluster_name"] = status.ClusterName
// If Vault was previously unavailable print a message to indicate the Agent
// is available now
if f.lastState == vaultUnavailable {
f.logger.Printf("[INFO] fingerprint.vault: Vault is available")
}
f.lastState = vaultAvailable
return true, nil
}
示例15: NewVaultClient
func NewVaultClient(vaultEndpoint string, token string) (interfaces.VaultClient, error) {
cfg := api.DefaultConfig()
cfg.ReadEnvironment()
cfg.Address = vaultEndpoint
vault, err := api.NewClient(cfg)
if err != nil {
return nil, err
}
vault.SetToken(token)
return &VaultClient{
vaultClient: vault,
config: cfg,
}, nil
}