Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion encryption/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
67 changes: 61 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down