Skip to content

Commit cbe0f34

Browse files
committed
tidy up code
1 parent 093fecc commit cbe0f34

4 files changed

Lines changed: 33 additions & 21 deletions

File tree

cmd/grpc/client/manager/mod.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ package manager
22

33
import (
44
"context"
5-
"crypto/tls"
6-
"crypto/x509"
75
"encoding/binary"
86
"fmt"
97
"google.golang.org/grpc/credentials"
108
"log"
11-
"os"
129
"sync"
1310
"time"
1411

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

4340
// load servers certificates
44-
creds, err := loadClientTLSCredentials(m.config)
41+
creds, err := credentials.NewClientTLSFromFile(m.config.Creds.CertificateFile, "")
4542
if err != nil {
4643
return Actor{}, xerrors.Errorf("failed to load servers certificates: %v", err)
4744
}
4845

4946
for i, addr := range m.config.Addresses {
50-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
51-
defer cancel()
52-
53-
conn, err := grpc.DialContext(ctx, addr, grpc.WithTransportCredentials(creds),
54-
grpc.WithBlock())
47+
conn, err := grpc.NewClient(addr, grpc.WithTransportCredentials(creds))
48+
// conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(creds), grpc.WithBlock())
5549
if err != nil {
5650
return Actor{}, xerrors.Errorf("failed to connect to %s: %v", addr, err)
5751
}
@@ -65,6 +59,23 @@ func (m *Manager) Connect() (Actor, error) {
6559
}, nil
6660
}
6761

62+
/*
63+
func Connect() {
64+
// Load the CA certificate to verify the server
65+
creds, err := credentials.NewClientTLSFromFile("ca.crt", "")
66+
if err != nil {
67+
log.Fatalf("failed to load CA certificate: %v", err)
68+
}
69+
70+
// Set up a connection to the server with the TLS credentials
71+
conn, err := grpc.Dial("localhost:50051", grpc.WithTransportCredentials(creds), grpc.WithBlock())
72+
if err != nil {
73+
log.Fatalf("did not connect: %v", err)
74+
}
75+
defer conn.Close()
76+
77+
}
78+
6879
func loadClientTLSCredentials(config utils.Config) (credentials.TransportCredentials, error) {
6980
// Load certificate of the CA who signed server's certificate
7081
if config.Creds.CertificateFile == "" {
@@ -73,6 +84,8 @@ func loadClientTLSCredentials(config utils.Config) (credentials.TransportCredent
7384
}
7485
log.Printf("Loading servers certificates from %s", config.Creds.CertificateFile)
7586
87+
creds, err := credentials.NewClientTLSFromFile(config.Creds.CertificateFile, "")
88+
7689
pemServerCA, err := os.ReadFile(config.Creds.CertificateFile)
7790
if err != nil {
7891
return nil, xerrors.Errorf("failed to read server's certificate: %v", err)
@@ -108,8 +121,9 @@ func loadClientTLSCredentials(config utils.Config) (credentials.TransportCredent
108121
RootCAs: certPool,
109122
}
110123
111-
return credentials.NewTLS(tlsConfig), nil
124+
return creds, nil
112125
}
126+
*/
113127

114128
// Actor allows to perform operations on the servers.
115129
type Actor struct {

cmd/grpc/client/web/mod.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ const keyNotFoundErr string = "no key with the given email id is found"
4343
var staticPointConfig = &utils.Config{
4444
Servers: map[string]utils.Server{
4545
"0": {
46-
IP: "128.179.33.63",
46+
IP: "10.156.33.110",
4747
Port: 50050,
4848
},
4949
"1": {
50-
IP: "128.179.33.75",
50+
IP: "10.156.33.112",
5151
Port: 50051,
5252
},
5353
},
5454
Addresses: []string{
55-
"128.179.33.63:50050", "128.179.33.75:50051",
55+
"10.156.33.110:50050", "10.156.33.112:50051",
5656
},
5757
Creds: utils.Creds{
5858
CertificateFile: "/opt/apir/server-cert.pem"},
@@ -64,16 +64,16 @@ var staticPointConfig = &utils.Config{
6464
var staticComplexConfig = &utils.Config{
6565
Servers: map[string]utils.Server{
6666
"0": {
67-
IP: "128.179.33.63",
67+
IP: "10.156.33.110",
6868
Port: 50040,
6969
},
7070
"1": {
71-
IP: "128.179.33.75",
71+
IP: "10.156.33.112",
7272
Port: 50041,
7373
},
7474
},
7575
Addresses: []string{
76-
"128.179.33.63:50040", "128.179.33.75:50041",
76+
"10.156.33.110:50040", "10.156.33.112:50041",
7777
},
7878
Creds: utils.Creds{
7979
CertificateFile: "/opt/apir/server-cert.pem"},

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/golang/protobuf v1.5.4
1111
github.com/lukechampine/fastxor v0.0.0-20210322201628-b664bed5a5cc
1212
github.com/nikirill/go-crypto v0.0.0-20210204153324-694bf46cc691
13-
github.com/stretchr/testify v1.9.0
13+
github.com/stretchr/testify v1.10.0
1414
golang.org/x/crypto v0.29.0
1515
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da
1616
google.golang.org/grpc v1.68.0

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
4343
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4444
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
4545
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
46-
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
47-
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
46+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
47+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
4848
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
4949
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
5050
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
@@ -88,8 +88,6 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc
8888
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
8989
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da h1:noIWHXmPHxILtqtCOPIhSt0ABwskkZKjD3bXGnZGpNY=
9090
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90=
91-
google.golang.org/genproto/googleapis/rpc v0.0.0-20241113202542-65e8d215514f h1:C1QccEa9kUwvMgEUORqQD9S17QesQijxjZ84sO82mfo=
92-
google.golang.org/genproto/googleapis/rpc v0.0.0-20241113202542-65e8d215514f/go.mod h1:GX3210XPVPUjJbTUbvwI8f2IpZDMZuPJWDzDuebbviI=
9391
google.golang.org/genproto/googleapis/rpc v0.0.0-20241118233622-e639e219e697 h1:LWZqQOEjDyONlF1H6afSWpAL/znlREo2tHfLoe+8LMA=
9492
google.golang.org/genproto/googleapis/rpc v0.0.0-20241118233622-e639e219e697/go.mod h1:5uTbfoYQed2U9p3KIj2/Zzm02PYhndfdmML0qC3q3FU=
9593
google.golang.org/grpc v1.68.0 h1:aHQeeJbo8zAkAa3pRzrVjZlbz6uSfeOXlJNQM0RAbz0=

0 commit comments

Comments
 (0)