11package main
22
33import (
4+ "crypto/tls"
5+ "crypto/x509"
46 log "github.com/sirupsen/logrus"
7+ "golang.org/x/crypto/pkcs12"
58 "google.golang.org/grpc/credentials"
69 "google.golang.org/grpc/credentials/insecure"
710 pb "nwnx4.org/nwn2dev/xp_rpc/proto"
11+ "os"
812 "strings"
913 "time"
1014)
@@ -33,7 +37,7 @@ const rpcEndBuildGeneric int32 = 2
3337
3438type rpcPlugin struct {
3539 config rpcConfig
36- certPath * string
40+ creds credentials. TransportCredentials
3741 clients map [string ]* rpcClient
3842 globalExBuildGenericRequest * pb.ExBuildGenericRequest
3943 globalExBuildGenericResponse * pb.ExBuildGenericResponse
@@ -43,7 +47,7 @@ type rpcPlugin struct {
4347func newRpcPlugin () * rpcPlugin {
4448 return & rpcPlugin {
4549 config : rpcConfig {},
46- certPath : nil ,
50+ creds : insecure . NewCredentials () ,
4751 clients : make (map [string ]* rpcClient ),
4852 globalExBuildGenericRequest : newExBuildGenericRequest (),
4953 globalExBuildGenericResponse : newExBuildGenericResponse (),
@@ -67,6 +71,49 @@ func newExBuildGenericResponse() *pb.ExBuildGenericResponse {
6771func (p * rpcPlugin ) init () {
6872 log .Info ("Initializing RPC plugin" )
6973
74+ // Add a certificate
75+ getCredentials := func () {
76+ if p .config .Auth .PfxFilePath == nil && p .config .Auth .PfxPassword == nil {
77+ log .Info ("Using insecure auth. settings" )
78+
79+ return
80+ }
81+
82+ pfxFilePath , pfxPassword := * p .config .Auth .PfxFilePath , ""
83+
84+ if p .config .Auth .PfxPassword != nil {
85+ pfxPassword = * p .config .Auth .PfxPassword
86+ }
87+
88+ // Load the PFX file
89+ pfxData , err := os .ReadFile (pfxFilePath )
90+ if err != nil {
91+ log .Fatalf ("Error reading PFX file: %v" , err )
92+ }
93+
94+ // Parse the PFX data to get the certificate
95+ _ , cert , err := pkcs12 .Decode (pfxData , pfxPassword )
96+ if err != nil {
97+ log .Fatalf ("Error decoding PFX file: %v" , err )
98+ }
99+
100+ // Create a new certificate pool and add the certificate
101+ caCertPool := x509 .NewCertPool ()
102+ caCertPool .AddCert (cert )
103+
104+ // Create a TLS configuration using the parsed certificate
105+ tlsConfig := & tls.Config {
106+ RootCAs : caCertPool ,
107+ InsecureSkipVerify : true ,
108+ }
109+
110+ // Create a credentials object from the TLS configuration
111+ p .creds = credentials .NewTLS (tlsConfig )
112+
113+ log .Info ("Using secure auth. settings" )
114+ }
115+ getCredentials ()
116+
70117 // Set the log level based on what was passed if it matches a level
71118 for _ , logLevel := range log .AllLevels {
72119 if strings .EqualFold (logLevel .String (), p .config .Log .LogLevel ) {
@@ -92,38 +139,12 @@ func (p *rpcPlugin) addRpcClient(name, url string) {
92139 // Load the certificate
93140 var conn * grpc.ClientConn
94141 var err error
95- if p .certPath != nil {
96- creds , err := credentials .NewClientTLSFromFile (* p .certPath , "" )
97- if err != nil {
98- log .Errorf ("Unable to load certificate: %v" , err )
99- p .clients [name ] = & rpcClient {
100- isValid : false ,
101- name : name ,
102- url : url ,
103- exServiceClient : nil ,
104- nwnxServiceClient : nil ,
105- scorcoServiceClient : nil ,
106- }
107- return
108- }
109-
110- conn , err = grpc .Dial (url , grpc .WithTransportCredentials (creds ))
111- } else {
112- conn , err = grpc .Dial (url , grpc .WithTransportCredentials (insecure .NewCredentials ()))
113- }
142+ conn , err = grpc .Dial (url , grpc .WithTransportCredentials (p .creds ))
114143
115144 // Dial with the loaded certificate
116145 if err != nil {
117146 log .Errorf ("Unable to attach client: %s@%s" , name , url )
118-
119- p .clients [name ] = & rpcClient {
120- isValid : false ,
121- name : name ,
122- url : url ,
123- exServiceClient : nil ,
124- nwnxServiceClient : nil ,
125- scorcoServiceClient : nil ,
126- }
147+ p .clients [name ] = newRpcClient (name , url )
127148
128149 return
129150 }
0 commit comments