Skip to content

Commit a5fa01f

Browse files
committed
minor edits main.go, added challenge main.go
1 parent 90f19dc commit a5fa01f

File tree

3 files changed

+102
-7
lines changed

3 files changed

+102
-7
lines changed

challenge/main.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package main
5+
6+
import (
7+
"context"
8+
"log"
9+
"os"
10+
11+
"github.com/gin-gonic/gin"
12+
13+
vault "github.com/hashicorp/vault/api"
14+
auth "github.com/hashicorp/vault/api/auth/kubernetes"
15+
)
16+
17+
func main() {
18+
config := vault.DefaultConfig()
19+
20+
// initialize Vault client
21+
client, err := vault.NewClient(config)
22+
if err != nil {
23+
log.Printf("unable to initialize Vault client: %v", err)
24+
os.Exit(1)
25+
}
26+
27+
// determine where the application is running and set the
28+
// Vault address and token accordingly
29+
if _, exists := os.LookupEnv("VAULT_ADDR"); exists {
30+
config.Address = os.Getenv("VAULT_ADDR")
31+
} else {
32+
log.Println("Cannot find a set VAULT_ADDR Exiting.")
33+
os.Exit(1)
34+
}
35+
36+
// The service-account token will be read from the path where the token's
37+
// Kubernetes Secret is mounted. By default, Kubernetes will mount it to
38+
// /var/run/secrets/kubernetes.io/serviceaccount/token.
39+
k8sAuth, err := auth.NewKubernetesAuth(
40+
"vault-kube-auth-role",
41+
)
42+
if err != nil {
43+
log.Printf("unable to initialize Kubernetes auth method: %v", err)
44+
os.Exit(1)
45+
}
46+
47+
authInfo, err := client.Auth().Login(context.Background(), k8sAuth)
48+
if err != nil {
49+
log.Printf("unable to log in with Kubernetes auth!: %v", err)
50+
os.Exit(1)
51+
}
52+
if authInfo == nil {
53+
log.Printf("no auth info was returned after login")
54+
os.Exit(1)
55+
}
56+
57+
// set up Gin router
58+
router := gin.Default()
59+
router.SetTrustedProxies([]string{"127.0.0.1", "192.168.1.2", "10.0.0.0/8"})
60+
61+
// using the token returned from Vault get secret from the default
62+
// mount path for KV v2 secret
63+
secret, err := client.KVv2("secret").Get(context.Background(), "myapp/api-key")
64+
if err != nil {
65+
log.Printf("unable to read secret: %v", err)
66+
os.Exit(1)
67+
}
68+
69+
// data map can contain more than one key-value pair,
70+
// in this case we're just grabbing one of them
71+
value, ok := secret.Data["access_key"].(string)
72+
if !ok {
73+
log.Printf("value type assertion failed: %T %#v", secret.Data["access_key"], secret.Data["access_key"])
74+
os.Exit(1)
75+
}
76+
77+
pass, ok := secret.Data["secret_access_key"].(string)
78+
if !ok {
79+
log.Printf("value type assertion failed: %T %#v", secret.Data["secret_access_key"], secret.Data["secret_access_key"])
80+
os.Exit(1)
81+
}
82+
83+
apiURL, ok := secret.Data["api_url"].(string)
84+
if !ok {
85+
log.Printf("value type assertion failed: %T %#v", secret.Data["api_url"], secret.Data["api_url"])
86+
os.Exit(1)
87+
}
88+
89+
log.Println("Access granted!")
90+
log.Printf("Retrieved secret values - Access Key: %s, Secret Key: %s, API URL: %s", value, pass, apiURL)
91+
92+
// Run Gin at the default port of 8080. The application will be accessible at http://localhost:8080 when port forwarding is set up.
93+
router.GET("/", func(c *gin.Context) {
94+
c.JSON(200, gin.H{
95+
"access_key": value,
96+
"secret_access_key": pass,
97+
"api_url": apiURL,
98+
})
99+
})
100+
101+
router.Run(":8080")
102+
}

main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616

1717
func main() {
1818
config := vault.DefaultConfig()
19-
//token_location := "token"
2019

2120
// initialize Vault client
2221
client, err := vault.NewClient(config)

manifests/vault-auth-secret.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
# Copyright (c) HashiCorp, Inc.
22
# SPDX-License-Identifier: MPL-2.0
33

4-
apiVersion: v1
5-
kind: ServiceAccount
6-
metadata:
7-
name: vault-auth
8-
namespace: default
9-
---
104
apiVersion: v1
115
kind: Secret
126
metadata:

0 commit comments

Comments
 (0)