Skip to content

Commit

Permalink
tidy up code
Browse files Browse the repository at this point in the history
  • Loading branch information
jbsv committed Nov 23, 2024
1 parent 093fecc commit 71977aa
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 66 deletions.
57 changes: 2 additions & 55 deletions cmd/grpc/client/manager/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package manager

import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/binary"
"fmt"
"google.golang.org/grpc/credentials"
"log"
"os"
"sync"
"time"

Expand Down Expand Up @@ -41,17 +38,13 @@ func (m *Manager) Connect() (Actor, error) {
servers := make([]server, len(m.config.Addresses))

// load servers certificates
creds, err := loadClientTLSCredentials(m.config)
creds, err := credentials.NewClientTLSFromFile(m.config.Creds.CertificateFile, "")
if err != nil {
return Actor{}, xerrors.Errorf("failed to load servers certificates: %v", err)
}

for i, addr := range m.config.Addresses {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

conn, err := grpc.DialContext(ctx, addr, grpc.WithTransportCredentials(creds),
grpc.WithBlock())
conn, err := grpc.NewClient(addr, grpc.WithTransportCredentials(creds))
if err != nil {
return Actor{}, xerrors.Errorf("failed to connect to %s: %v", addr, err)
}
Expand All @@ -65,52 +58,6 @@ func (m *Manager) Connect() (Actor, error) {
}, nil
}

func loadClientTLSCredentials(config utils.Config) (credentials.TransportCredentials, error) {
// Load certificate of the CA who signed server's certificate
if config.Creds.CertificateFile == "" {
log.Println("No servers certificates file provided, using default")
config.Creds.CertificateFile = "/opt/apir/server-cert.pem"
}
log.Printf("Loading servers certificates from %s", config.Creds.CertificateFile)

pemServerCA, err := os.ReadFile(config.Creds.CertificateFile)
if err != nil {
return nil, xerrors.Errorf("failed to read server's certificate: %v", err)
}

certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM(pemServerCA) {
return nil, xerrors.Errorf("failed to add server CA's certificate")
}

// Load client's certificate and private key
// Load certificate of the CA who signed server's certificate
if config.ClientCertFile == "" {
log.Println("No certificate file provided, using default")
config.ClientCertFile = "/opt/apir/client-cert.pem"
}
if config.ClientKeyFile == "" {
log.Println("No key file provided, using default")
config.ClientKeyFile = "/opt/apir/server-key.pem"
}

log.Printf("Loading client certificates from %s", config.ClientCertFile)
log.Printf("Loading client key from %s", config.ClientKeyFile)

clientCert, err := tls.LoadX509KeyPair(config.ClientCertFile, config.ClientKeyFile)
if err != nil {
log.Fatalf("failed to load X509 key pair: %v", err)
}

// Create the credentials and return it
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{clientCert},
RootCAs: certPool,
}

return credentials.NewTLS(tlsConfig), nil
}

// Actor allows to perform operations on the servers.
type Actor struct {
servers []server
Expand Down
12 changes: 6 additions & 6 deletions cmd/grpc/client/web/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ const keyNotFoundErr string = "no key with the given email id is found"
var staticPointConfig = &utils.Config{
Servers: map[string]utils.Server{
"0": {
IP: "128.179.33.63",
IP: "10.156.33.110",
Port: 50050,
},
"1": {
IP: "128.179.33.75",
IP: "10.156.33.112",
Port: 50051,
},
},
Addresses: []string{
"128.179.33.63:50050", "128.179.33.75:50051",
"10.156.33.110:50050", "10.156.33.112:50051",
},
Creds: utils.Creds{
CertificateFile: "/opt/apir/server-cert.pem"},
Expand All @@ -64,16 +64,16 @@ var staticPointConfig = &utils.Config{
var staticComplexConfig = &utils.Config{
Servers: map[string]utils.Server{
"0": {
IP: "128.179.33.63",
IP: "10.156.33.110",
Port: 50040,
},
"1": {
IP: "128.179.33.75",
IP: "10.156.33.112",
Port: 50041,
},
},
Addresses: []string{
"128.179.33.63:50040", "128.179.33.75:50041",
"10.156.33.110:50040", "10.156.33.112:50041",
},
Creds: utils.Creds{
CertificateFile: "/opt/apir/server-cert.pem"},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/golang/protobuf v1.5.4
github.com/lukechampine/fastxor v0.0.0-20210322201628-b664bed5a5cc
github.com/nikirill/go-crypto v0.0.0-20210204153324-694bf46cc691
github.com/stretchr/testify v1.9.0
github.com/stretchr/testify v1.10.0
golang.org/x/crypto v0.29.0
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da
google.golang.org/grpc v1.68.0
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
Expand Down Expand Up @@ -88,8 +88,6 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da h1:noIWHXmPHxILtqtCOPIhSt0ABwskkZKjD3bXGnZGpNY=
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241113202542-65e8d215514f h1:C1QccEa9kUwvMgEUORqQD9S17QesQijxjZ84sO82mfo=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241113202542-65e8d215514f/go.mod h1:GX3210XPVPUjJbTUbvwI8f2IpZDMZuPJWDzDuebbviI=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241118233622-e639e219e697 h1:LWZqQOEjDyONlF1H6afSWpAL/znlREo2tHfLoe+8LMA=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241118233622-e639e219e697/go.mod h1:5uTbfoYQed2U9p3KIj2/Zzm02PYhndfdmML0qC3q3FU=
google.golang.org/grpc v1.68.0 h1:aHQeeJbo8zAkAa3pRzrVjZlbz6uSfeOXlJNQM0RAbz0=
Expand Down

0 comments on commit 71977aa

Please sign in to comment.