MCPcopy Create free account
hub / github.com/cloudfoundry/credhub-cli / ExampleCredHub

Function ExampleCredHub

credhub/example_test.go:12–57  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

10)
11
12func ExampleCredHub() {
13 _ = func() {
14 // Use a CredHub server on "https://example.com" using UAA password grant
15 ch, err := credhub.New("https://example.com",
16 credhub.SkipTLSValidation(true),
17 credhub.Auth(auth.UaaPassword("credhub_cli", "", "username", "password")))
18
19 if err != nil {
20 panic("credhub client configured incorrectly: " + err.Error())
21 }
22
23 authUrl, err := ch.AuthURL()
24 if err != nil {
25 panic("couldn't fetch authurl")
26 }
27
28 fmt.Println("CredHub server: ", ch.ApiURL)
29 fmt.Println("Auth server: ", authUrl)
30
31 // Retrieve a password stored at "/my/password"
32 password, err := ch.GetLatestPassword("/my/password")
33 if err != nil {
34 panic("password not found")
35 }
36
37 fmt.Println("My password: ", password.Value)
38
39 // Manually refresh the access token
40 uaa, ok := ch.Auth.(*auth.OAuthStrategy) // This works because we authenticated with auth.UaaPasswordGrant
41 if !ok {
42 panic("not using uaa")
43 }
44
45 fmt.Println("Old access token: ", uaa.AccessToken())
46
47 uaa.Refresh() // For demo purposes only, tokens will be automatically refreshed by auth.OAuthStrategy
48
49 fmt.Println("New access token:", uaa.AccessToken())
50 // Sample Output:
51 // CredHub server: https://example.com
52 // Auth server: https://uaa.example.com
53 // My password: random-password
54 // Old access token: some-access-token
55 // New access token: new-access-token
56 }
57}
58
59func ExampleNew() {
60 _ = func() {

Callers

nothing calls this directly

Calls 9

NewFunction · 0.92
SkipTLSValidationFunction · 0.92
AuthFunction · 0.92
UaaPasswordFunction · 0.92
GetLatestPasswordMethod · 0.80
AccessTokenMethod · 0.80
RefreshMethod · 0.80
AuthURLMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…