-
Notifications
You must be signed in to change notification settings - Fork 276
/
main.go
88 lines (71 loc) · 2.49 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright The Athenz Authors
// Licensed under the terms of the Apache version 2.0 license. See LICENSE file for terms.
// Get-access is a demo program to query if the current principal has
// "Access" to a specified resource, in a given domain.
package main
import (
"crypto/tls"
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/AthenZ/athenz/clients/go/zms"
)
func main() {
var key, cert, zmsURL, domain, action, resource string
var skipVerify bool
flag.StringVar(&cert, "cert", "", "certificate file")
flag.StringVar(&key, "key", "", "key file")
flag.StringVar(&zmsURL, "zms", "", "zms endpoint")
flag.StringVar(&domain, "domain", "", "domain the principal is from")
flag.StringVar(&action, "action", "", "action for get access check call")
flag.StringVar(&resource, "resource", "", "fully qualified resource for get access check call")
flag.BoolVar(&skipVerify, "skipVerify", false, "boolean flag to skip verifying server for TLS connection, if using self-signed certs")
flag.Parse()
if zmsURL == "" {
log.Fatalf("A valid zms url needs to be specified")
}
idx := strings.Index(resource, ":")
if idx < 0 {
resource = domain + ":" + resource
}
tlsConfig, err := getTLSConfigFromFiles(key, cert)
if err != nil {
log.Fatalf("Unable to load client TLS Config, error: %v", err)
}
transport := &http.Transport{
TLSClientConfig: tlsConfig,
}
if skipVerify {
transport.TLSClientConfig.InsecureSkipVerify = true
}
client := zms.NewClient(zmsURL, transport)
access, err := client.GetAccess(zms.ActionName(action), zms.ResourceName(resource), "", "")
if err != nil {
log.Fatalf("unable to do GetAccess, err: %v, action: %q, resource: %q", err, action, resource)
}
log.Printf("Access Granted: %t", access.Granted)
}
func getTLSConfigFromFiles(keyFile, certFile string) (*tls.Config, error) {
keypem, err := os.ReadFile(keyFile)
if err != nil {
return nil, fmt.Errorf("unable to read keyfile: %q, error: %v", keyFile, err)
}
certpem, err := os.ReadFile(certFile)
if err != nil {
return nil, fmt.Errorf("unable to read certfile: %q, error: %v", certFile, err)
}
return getTLSConfig(certpem, keypem)
}
func getTLSConfig(certpem, keypem []byte) (*tls.Config, error) {
clientCert, err := tls.X509KeyPair(certpem, keypem)
if err != nil {
return nil, fmt.Errorf("unable to formulate clientCert from key and cert bytes, error: %v", err)
}
config := &tls.Config{}
config.Certificates = make([]tls.Certificate, 1)
config.Certificates[0] = clientCert
return config, nil
}