Skip to content

Commit cd05973

Browse files
Merge pull request #2 from nwn2dev/feature/ssl
Adds ability to define a certPath
2 parents bcb2c7f + 2eaf6f6 commit cd05973

File tree

5 files changed

+38
-3
lines changed

5 files changed

+38
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ Replace go_ with the language you wish to use (java_, cpp_, etc.) and the *_out
5353
With a created external application, configure your xp_rpc settings for the client from the xp_rpc.yml file.
5454

5555
```yaml
56+
auth:
57+
certPath: path/to/cert
5658
log:
5759
logLevel: info
5860
perClient:

plugin/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ import (
3333
const pluginID string = "RPC" // Plugin ID used for identification in the list
3434
const pluginName string = "NWNX RPC Plugin" // Plugin name passed to hook
3535
const pluginDescription string = "A better way to integrate services with NWN2"
36-
const pluginVersion string = "0.4.2" // Plugin version passed to hook
37-
const pluginContact string = "(c) 2021-2023 by ihatemundays ([email protected])"
36+
const pluginVersion string = "0.5.0" // Plugin version passed to hook
37+
const pluginContact string = "(c) 2021-2024 by ihatemundays ([email protected])"
3838

3939
const logFilename string = "xp_rpc.log"
4040
const configFilename string = "xp_rpc.yml"

plugin/rpc_config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package main
33
import "time"
44

55
type rpcConfig struct {
6+
Auth struct {
7+
CertPath *string `yaml:"certPath"`
8+
} `yaml:"auth"`
69
Log struct {
710
LogLevel string `yaml:"logLevel" default:"info"`
811
} `yaml:"log"`

plugin/rpc_plugin.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
log "github.com/sirupsen/logrus"
5+
"google.golang.org/grpc/credentials"
56
"google.golang.org/grpc/credentials/insecure"
67
pb "nwnx4.org/nwn2dev/xp_rpc/proto"
78
"strings"
@@ -32,6 +33,7 @@ const rpcEndBuildGeneric int32 = 2
3233

3334
type rpcPlugin struct {
3435
config rpcConfig
36+
certPath *string
3537
clients map[string]*rpcClient
3638
globalExBuildGenericRequest *pb.ExBuildGenericRequest
3739
globalExBuildGenericResponse *pb.ExBuildGenericResponse
@@ -41,6 +43,7 @@ type rpcPlugin struct {
4143
func newRpcPlugin() *rpcPlugin {
4244
return &rpcPlugin{
4345
config: rpcConfig{},
46+
certPath: nil,
4447
clients: make(map[string]*rpcClient),
4548
globalExBuildGenericRequest: newExBuildGenericRequest(),
4649
globalExBuildGenericResponse: newExBuildGenericResponse(),
@@ -85,7 +88,31 @@ func (p *rpcPlugin) init() {
8588
// Runs on an rpcPlugin and adds a client by name and URL
8689
func (p *rpcPlugin) addRpcClient(name, url string) {
8790
log.Infof("Adding client: %s@%s", name, url)
88-
conn, err := grpc.Dial(url, grpc.WithTransportCredentials(insecure.NewCredentials()))
91+
92+
// Load the certificate
93+
var conn *grpc.ClientConn
94+
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+
}
114+
115+
// Dial with the loaded certificate
89116
if err != nil {
90117
log.Errorf("Unable to attach client: %s@%s", name, url)
91118

@@ -101,6 +128,7 @@ func (p *rpcPlugin) addRpcClient(name, url string) {
101128
return
102129
}
103130

131+
// Create gRPC clients with the connection
104132
p.clients[name] = &rpcClient{
105133
isValid: true,
106134
name: name,

plugin/xp_rpc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
auth:
2+
# certPath:
13
log:
24
logLevel: info
35
perClient:

0 commit comments

Comments
 (0)