-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
137 lines (115 loc) · 3.68 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// --------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// --------------------------------------------------------------------------------------------
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
crand "crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"github.com/microsoft/confidential-container-demos/kafka/util"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azeventhubs"
)
const eventHubNamespace = "EVENTHUB_NAMESPACE"
const eventHub = "EVENTHUB"
const msg = "MSG"
var eventId = 0
var logLocation = util.GetEnv("LOG_FILE")
func main() {
if len(logLocation) > 0 {
f, err := os.OpenFile(logLocation, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0740)
if err != nil {
log.Printf("Unable to open file log location: %s", err.Error())
}
log.SetOutput(f)
defer f.Close()
}
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
credential, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Printf("Retrieving Azure Credential failed: %s", err.Error())
os.Exit(1)
}
eventHubNamespace := fmt.Sprintf(
"%s.servicebus.windows.net",
util.GetEnv(eventHubNamespace))
// Event Hubs producer
producerClient, err := azeventhubs.NewProducerClient(
eventHubNamespace,
util.GetEnv(eventHub),
credential,
nil)
if err != nil {
log.Printf("Creating Producer Client failed: %s", err.Error())
os.Exit(1)
}
defer producerClient.Close(context.Background())
for {
event := createEventsForDemo()
newBatchOptions := &azeventhubs.EventDataBatchOptions{}
// Creates an EventDataBatch, which you can use to pack multiple events together, allowing for efficient transfer.
batch, err := producerClient.NewEventDataBatch(context.Background(), newBatchOptions)
if err != nil {
log.Printf("Creating event batch failed: %s", err.Error())
os.Exit(1)
}
err = batch.AddEventData(event, nil)
if err != nil {
log.Printf("Adding event data to batch failed: %s", err.Error())
}
if err := producerClient.SendEventDataBatch(context.Background(), batch, nil); err != nil {
log.Printf("Event sending failed %s", err.Error())
}
select {
case sig := <-signals:
log.Printf("Got signal: %v", sig)
return
default:
}
time.Sleep(time.Second * 1)
}
}
func createEventsForDemo() *azeventhubs.EventData {
eventId += 1
rawMessage := util.GetEnv(msg)
value := fmt.Sprintf("Message Id %d: %s", eventId, rawMessage)
encryptedValue, err := encryptMessage(value)
if err != nil {
log.Printf("Encrypting message failed: %s", err.Error())
}
return &azeventhubs.EventData{Body: []byte(encryptedValue)}
}
func encryptMessage(plaintext string) (string, error) {
var pubpem []byte
var err error
if pkey := util.GetEnv("PUBKEY"); len(pkey) > 0 {
pubpem = []byte(pkey)
}
block, _ := pem.Decode([]byte(pubpem))
key, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return "Invalid public key", fmt.Errorf("invalid public key: %v", err)
}
var ciphertext []byte
if pubkey, ok := key.(*rsa.PublicKey); ok {
ciphertext, err = rsa.EncryptOAEP(sha256.New(), crand.Reader, pubkey, []byte(plaintext), nil)
if err != nil {
return "Failed to encrypt with the public key", fmt.Errorf("failed to encrypt with the public key: %v", err)
}
} else {
return "Invalid public RSA key", fmt.Errorf("invalid public RSA key")
}
return base64.StdEncoding.EncodeToString(ciphertext), nil
}