-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
57 lines (46 loc) · 1.59 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Based on https://github.com/spiffe/go-spiffe/blob/main/v2/examples/spiffe-http/client/main.go
package main
import (
"context"
"io/ioutil"
"log"
"net/http"
"github.com/spiffe/go-spiffe/v2/spiffeid"
"github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig"
"github.com/spiffe/go-spiffe/v2/workloadapi"
)
const (
// Workload API socket path
socketPath = "unix:///run/spire/sockets/agent.sock"
serverURL = "https://10.11.1.202:443/"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Create a `workloadapi.X509Source`, it will connect to Workload API using provided socket path
// If socket path is not defined using `workloadapi.SourceOption`, value from environment variable `SPIFFE_ENDPOINT_SOCKET` is used.
source, err := workloadapi.NewX509Source(ctx, workloadapi.WithClientOptions(workloadapi.WithAddr(socketPath)))
if err != nil {
log.Fatalf("Unable to create X509Source %v", err)
}
defer source.Close()
// Allowed SPIFFE ID
serverID := spiffeid.Must("example.org", "server")
// Create a `tls.Config` to allow mTLS connections, and verify that presented certificate has SPIFFE ID `spiffe://example.org/server`
tlsConfig := tlsconfig.MTLSClientConfig(source, source, tlsconfig.AuthorizeID(serverID))
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
}
r, err := client.Get(serverURL)
if err != nil {
log.Fatalf("Error connecting to %q: %v", serverURL, err)
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatalf("Unable to read body: %v", err)
}
log.Printf("%s", body)
}