Created
April 18, 2019 10:18
-
-
Save yurenju/430b47a8e2f2f6f96ca06bbb64f8d105 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
vaultApi "github.com/hashicorp/vault/api" | |
) | |
var ( | |
vaultHost string | |
vaultCAPath string | |
vaultServiceAccount string | |
vaultJWTPath string | |
) | |
func main() { | |
vaultJWTPath = "/var/run/secrets/kubernetes.io/serviceaccount/token" | |
vaultServiceAccount = "tx" | |
tlsConfig := &vaultApi.TLSConfig{ | |
CACert: vaultCAPath, | |
Insecure: false, | |
} | |
config := vaultApi.DefaultConfig() | |
config.Address = fmt.Sprintf("https://%s", vaultHost) | |
config.ConfigureTLS(tlsConfig) | |
client, _ := vaultApi.NewClient(config) | |
buf, _ := ioutil.ReadFile(vaultJWTPath) | |
jwt := string(buf) | |
options := map[string]interface{}{ | |
"jwt": jwt, | |
"role": vaultServiceAccount, | |
} | |
loginSecret, _ := client.Logical().Write("auth/kubernetes/login", options) | |
client.SetToken(loginSecret.Auth.ClientToken) | |
secret, _ := client.Logical().Read("database/creds/tx") | |
fmt.Println(secret) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment