Skip to content

Commit 4e7d9b7

Browse files
committed
Added random generated XID ID to messages, regenerated protobuf, and ID mismatch handling.
1 parent c2c945b commit 4e7d9b7

File tree

7 files changed

+141
-119
lines changed

7 files changed

+141
-119
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.15
55
require (
66
github.com/golang/protobuf v1.4.1
77
github.com/mitchellh/go-homedir v1.1.0
8+
github.com/rs/xid v1.2.1
89
github.com/rs/zerolog v1.20.0
910
github.com/spf13/cobra v1.1.1
1011
github.com/spf13/viper v1.7.1

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7z
156156
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
157157
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
158158
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
159+
github.com/rs/xid v1.2.1 h1:mhH9Nq+C1fY2l1XIpgxIiUOfNpRBYH1kKcr+qfKgjRc=
159160
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
160161
github.com/rs/zerolog v1.20.0 h1:38k9hgtUBdxFwE34yS8rTHmHBa4eN16E4DJlv177LNs=
161162
github.com/rs/zerolog v1.20.0/go.mod h1:IzD0RJ65iWH0w97OQQebJEvTZYvsCUm9WVLWBQrJRjo=

locker/agent.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"vikingPingvin/locker/locker/messaging"
1515
"vikingPingvin/locker/locker/messaging/protobuf"
1616

17+
"github.com/rs/xid"
1718
"github.com/rs/zerolog/log"
1819
"google.golang.org/protobuf/proto"
1920
)
@@ -26,6 +27,7 @@ type InputData struct {
2627
FileInput string
2728
FileName string
2829
FileHash []byte
30+
ID xid.ID
2931
}
3032

3133
type Agent interface {
@@ -52,7 +54,7 @@ func (a ArtifactAgent) Start(inputData *InputData) bool {
5254
parseAndSendPayload(connection, inputData)
5355

5456
// Listen for ACK from server
55-
listenForACK(connection)
57+
listenForACK(connection, inputData)
5658

5759
return true
5860
}
@@ -71,10 +73,11 @@ func parseAndSendMetaData(connection net.Conn, inputData *InputData) (fileInfo o
7173
Str("file name", fileInfo.Name()).
7274
Str("size", fmt.Sprintf("%d", fileInfo.Size())).
7375
Str("hash", fmt.Sprintf("%v", inputData.FileHash)).
76+
Str("id", inputData.ID.String()).
7477
Msg("Artifact metadata parsing finished")
7578

7679
message, err := messaging.CreateMessage_FileMeta(
77-
2234,
80+
inputData.ID.Bytes(),
7881
protobuf.MessageType_META,
7982
"test_namespace",
8083
"test_project",
@@ -111,7 +114,7 @@ func parseAndSendPayload(connection net.Conn, inputData *InputData) {
111114
isPayloadFinal = true
112115
// Send terminating payload protobuf message
113116
terminalMessage, err := messaging.CreateMessage_FilePackage(
114-
2234,
117+
inputData.ID.Bytes(),
115118
protobuf.MessageType_PACKAGE,
116119
make([]byte, 1),
117120
isPayloadFinal,
@@ -124,7 +127,7 @@ func parseAndSendPayload(connection net.Conn, inputData *InputData) {
124127
}
125128

126129
message, err := messaging.CreateMessage_FilePackage(
127-
2234,
130+
inputData.ID.Bytes(),
128131
protobuf.MessageType_PACKAGE,
129132
(buffer)[:n],
130133
isPayloadFinal)
@@ -153,7 +156,7 @@ func hashFile(path string) (hash []byte) {
153156
return hasher.Sum(nil)
154157
}
155158

156-
func listenForACK(connection net.Conn) {
159+
func listenForACK(connection net.Conn, inputData *InputData) {
157160

158161
// TODO: Make into const in message_handler (also server.go)
159162
// sizePrefix is 4 bytes protobug message size
@@ -172,7 +175,18 @@ func listenForACK(connection net.Conn) {
172175
if genericProto.GetAck().ProtoReflect().IsValid() {
173176
ackPacket := genericProto.GetAck()
174177

175-
log.Info().Msgf("ACK packet recieved from server with success flag: %v", ackPacket.GetServerSuccess())
178+
serverResult := ackPacket.GetServerSuccess()
179+
ackID, _ := xid.FromBytes(ackPacket.GetId())
180+
if ackID != inputData.ID {
181+
log.Warn().
182+
Str("respone_id", ackID.String()).
183+
Str("original_id", inputData.ID.String()).
184+
Msg("Response ID mismatch.")
185+
}
186+
187+
log.Info().
188+
Str("id_back", ackID.String()).
189+
Msgf("ACK packet recieved from server with success flag: %v", serverResult)
176190
}
177191
}
178192

@@ -202,6 +216,7 @@ func parseInputArguments() *InputData {
202216
}
203217
data := &InputData{
204218
FileInput: inputPath,
219+
ID: xid.New(),
205220
}
206221
return data
207222
}

locker/messaging/message_handler.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"google.golang.org/protobuf/proto"
1111
)
1212

13-
func CreateMessage_ServerACk(id int32, messageType pb.MessageType, isSuccess bool) (protoMessage *pb.LockerMessage, err error) {
13+
func CreateMessage_ServerACk(id []byte, messageType pb.MessageType, isSuccess bool) (protoMessage *pb.LockerMessage, err error) {
1414

1515
protoMessage = &pb.LockerMessage{
1616
Message: &pb.LockerMessage_Ack{
@@ -30,7 +30,7 @@ func CreateMessage_ServerACk(id int32, messageType pb.MessageType, isSuccess boo
3030
// msgType
3131
// fileName
3232
// fileHash
33-
func CreateMessage_FileMeta(id int32, msgType pb.MessageType, namesSpace string, projectName string, fileName string, fileHash []byte) (protoMessage *pb.LockerMessage, err error) {
33+
func CreateMessage_FileMeta(id []byte, msgType pb.MessageType, namesSpace string, projectName string, fileName string, fileHash []byte) (protoMessage *pb.LockerMessage, err error) {
3434

3535
protoMessage = &pb.LockerMessage{
3636
Message: &pb.LockerMessage_Meta{
@@ -48,7 +48,7 @@ func CreateMessage_FileMeta(id int32, msgType pb.MessageType, namesSpace string,
4848
return protoMessage, err
4949
}
5050

51-
func CreateMessage_FilePackage(id int32, msgType pb.MessageType, payload []byte, isFinalPayload bool) (protoMessage *pb.LockerMessage, err error) {
51+
func CreateMessage_FilePackage(id []byte, msgType pb.MessageType, payload []byte, isFinalPayload bool) (protoMessage *pb.LockerMessage, err error) {
5252

5353
protoMessage = &pb.LockerMessage{
5454
Message: &pb.LockerMessage_Package{

0 commit comments

Comments
 (0)