From 4a46f69b1bf02b56461832df0eb6af769a9601f3 Mon Sep 17 00:00:00 2001 From: mardefasma Date: Fri, 8 Oct 2021 16:56:01 +0700 Subject: [PATCH] Security Workshop --- encryption/common.go | 2 +- go.mod | 2 +- main.go | 67 ++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 63 insertions(+), 8 deletions(-) diff --git a/encryption/common.go b/encryption/common.go index b0c03e0..52afff4 100644 --- a/encryption/common.go +++ b/encryption/common.go @@ -31,7 +31,7 @@ func GenerateKey() []byte { func SetPublicKeyAndPrivateKeyServiceA() error { // Public Key - pubKeyRead := strings.NewReader(strings.Trim(PrivateKeyServiceA, "\t")) + pubKeyRead := strings.NewReader(strings.Trim(PublicKeyServiceA, "\t")) publicKey, err := ioutil.ReadAll(pubKeyRead) if err != nil { return err diff --git a/go.mod b/go.mod index 9f0af0b..5d277df 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,6 @@ module github.com/ariefmaulidy/security-workshop go 1.16 require ( - github.com/json-iterator/go v1.1.12 + github.com/json-iterator/go v1.1.12 // indirect github.com/nsqio/go-nsq v1.0.8 ) diff --git a/main.go b/main.go index 4d38b81..7ebba4b 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,12 @@ package main import ( + "encoding/json" + "fmt" "log" "net/http" + "github.com/ariefmaulidy/security-workshop/encryption" "github.com/ariefmaulidy/security-workshop/messaging" "github.com/nsqio/go-nsq" ) @@ -53,8 +56,8 @@ func main() { // initiate consumer consumer, err := messaging.NewConsumer(messaging.ConsumerConfig{ - Topic: "test", // Change the topic - Channel: "test", // Change the channel + Topic: "fasma_security_workshop_del_soon", // Change the topic + Channel: "fasma_security_workshop_del_soon", // Change the channel LookupAddress: "172.18.59.254:4161", MaxAttempts: defaultConsumerMaxAttempts, MaxInFlight: defaultConsumerMaxInFlight, @@ -72,13 +75,65 @@ func main() { func handlePublish(w http.ResponseWriter, r *http.Request) { // Do Publish - topic := "" // TODO: update to given topic name - msg := "" // TODO: write your message here - producer.Publish(topic, msg) + topic := "fasma_security_workshop_del_soon" + msg := Payment{ + UserData: User{ + Name: "Fasma", + Email: "marde.aza@tokopedia.com", + PhoneNumber: "088888888", + Age: 22, + }, + ItemData: []Item{ + { + Name: "Simba", + Price: 50000, + Category: "cereal", + Quantity: 2, + TotalPrice: 100000, + }, + { + Name: "Coco Crunch", + Price: 20000, + Category: "cereal", + Quantity: 4, + TotalPrice: 80000, + }, + }, + TotalPayment: 180000, + } + fmt.Printf("[SERVICE A][Payload] %+v\n", msg) + + msgByte, err := json.Marshal(msg) + if err != nil { + log.Fatal("Failed marshal message", err) + } + fmt.Printf("[SERVICE A][After Marshal] %+v\n", msgByte) + + msgEncrypted, err := encryption.EncrpytRSA(encryption.ServiceA, msgByte) + if err != nil { + log.Fatal("Failed encryption", err) + } + fmt.Printf("[SERVICE A][After Encrypt] %+v\n", msgEncrypted) + + producer.Publish(topic, msgEncrypted) } func handleMessage(message *nsq.Message) error { - // Handle message NSQ here + fmt.Printf("[SERVICE B][Input Message] %+v\n", message.Body) + + msgDecrypted, err := encryption.DecryptRSA(encryption.ServiceA, string(message.Body)) + if err != nil { + log.Fatal("Failed decryption", err) + } + fmt.Printf("[SERVICE B][After Decrypt] %+v\n", msgDecrypted) + + payload := Payment{} + err = json.Unmarshal(msgDecrypted, &payload) + if err != nil { + log.Fatal("Failed unmarshal message", err) + } + + fmt.Printf("[SERVICE B][After Unmarshal] %+v\n", payload) message.Finish() return nil