-
Notifications
You must be signed in to change notification settings - Fork 79
/
wwapird.go
113 lines (89 loc) · 2.83 KB
/
wwapird.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"os"
"flag"
"fmt"
"log"
"net/http"
"github.com/golang/glog"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"github.com/warewulf/warewulf/internal/pkg/api/apiconfig"
warewulfconf "github.com/warewulf/warewulf/internal/pkg/config"
gw "github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
"path"
)
func run() error {
log.Println("test0")
conf := warewulfconf.Get()
// Read the config file.
config, err := apiconfig.NewClientServer(path.Join(conf.Paths.Sysconfdir, "warewulf/wwapird.conf"))
if err != nil {
glog.Fatalf("Failed to read config file, err: %v", err)
}
grpcServerEndpoint := fmt.Sprintf("%s:%v", config.ClientApiConfig.Server, config.ClientApiConfig.Port)
httpServerEndpoint := fmt.Sprintf(":%v", config.ServerApiConfig.Port)
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// Register gRPC server endpoint (we are the client)
// Note: Make sure the gRPC server is running properly and accessible
mux := runtime.NewServeMux()
var opts []grpc.DialOption
if config.ClientTlsConfig.Enabled {
// Load the client cert and its key
clientCert, err := tls.LoadX509KeyPair(config.ClientTlsConfig.Cert, config.ClientTlsConfig.Key)
if err != nil {
log.Fatalf("Failed to load client cert and key. %s.", err)
}
// Load the CA cert.
var cacert []byte
cacert, err = os.ReadFile(config.ClientTlsConfig.CaCert)
if err != nil {
log.Fatalf("Failed to load cacert. err: %s\n", err)
}
// Put the CA cert into the cert pool.
certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM(cacert) {
log.Fatalf("Failed to append CA cert to certificate pool. %s.", err)
}
// Create the TLS configuration
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{clientCert},
RootCAs: certPool,
MinVersion: tls.VersionTLS13,
MaxVersion: tls.VersionTLS13,
}
// Create TLS credentials from the TLS configuration
creds := credentials.NewTLS(tlsConfig)
opts = append(opts, grpc.DialOption(grpc.WithTransportCredentials(creds)))
} else {
opts = []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
}
err = gw.RegisterWWApiHandlerFromEndpoint(ctx, mux, grpcServerEndpoint, opts)
if err != nil {
return err
}
// Start HTTP server (and proxy calls to gRPC server endpoint)
if config.ServerTlsConfig.Enabled {
return http.ListenAndServeTLS(
httpServerEndpoint,
config.ServerTlsConfig.ConcatCert,
config.ServerTlsConfig.Key,
mux)
}
// Insecure
return http.ListenAndServe(httpServerEndpoint, mux)
}
func main() {
flag.Parse() // Pretty sure glog wants this.
defer glog.Flush()
if err := run(); err != nil {
glog.Fatal(err)
}
}