-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
149 lines (126 loc) · 5.28 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
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"encoding/json"
"errors"
"fmt"
"strings"
"os"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
// Request for Lambda, this is a interface{} since the Request payload can be different based on operation
type Request map[string]interface{}
//Response for Lambda
type Response struct {
Message string `json:"message"`
OK bool `json:"ok"`
}
//SignResponse type to represent response for Signing Operation
type SignResponse struct {
SignText string
HashText string
}
//VerifySignResposne type to represent response for verification
type VerifySignResposne struct {
VerifiedStatus bool
}
//Handler function to be invoked by AWS Lambda
func Handler(request Request) (Response, error) {
operation := request["operation"]
fmt.Println("Operation requested is ", operation)
response := Response{Message: "", OK: false}
switch operation {
case "SignSimple":
messageText := request["value"].(map[string]interface{})["messagetext"].(string)
secretKey := request["value"].(map[string]interface{})["secretkey"].(string)
signString, hashString, _ := SignSimple(messageText, secretKey)
signResponse := SignResponse{signString, hashString}
signSimpleJSON, marshalErr := json.Marshal(signResponse)
if marshalErr != nil {
fmt.Println("error in SignSimple Marshal:", marshalErr)
return response, errors.New("Marshall of request failed")
}
response = Response{Message: string(signSimpleJSON[:]), OK: true}
case "VerifySignSimple":
signVerified, _ := VerifySignSimple(request["value"].(map[string]interface{})["signtext"].(string),
request["value"].(map[string]interface{})["messagetext"].(string), request["value"].(map[string]interface{})["hashtext"].(string),
request["value"].(map[string]interface{})["publickey"].(string))
verifyResponse := VerifySignResposne{(signVerified == 0)}
verifySimpleJSON, marshalErr := json.Marshal(verifyResponse)
if marshalErr != nil {
fmt.Println("error in VerifySignSimple Marshal:", marshalErr)
return response, errors.New("Marshall of request failed")
}
response = Response{Message: string(verifySimpleJSON), OK: true}
case "SignFileSimple":
fileName := strings.Replace(request["value"].(map[string]interface{})["s3item"].(string), "/", "_", -1)
file, fileOpenErr := os.Create(fileName)
if fileOpenErr != nil {
fmt.Println("error in Open File for S3:", fileOpenErr)
return response, errors.New("error in Open File for S3")
}
defer file.Close()
sess, _ := session.NewSession(&aws.Config{
Region: aws.String("us-east-1")},
)
downloader := s3manager.NewDownloader(sess)
numBytes, fileDownloadErr := downloader.Download(file,
&s3.GetObjectInput{
Bucket: aws.String(request["value"].(map[string]interface{})["s3bucket"].(string)),
Key: aws.String(request["value"].(map[string]interface{})["s3item"].(string)),
})
if fileDownloadErr != nil || numBytes == 0 {
fmt.Println("error in Downloading File from S3:", fileDownloadErr)
return response, errors.New("error in Downloading File from S3")
}
signString, hashString, _ := SignFileSimple(fileName, request["value"].(map[string]interface{})["secretkey"].(string))
signResponse := SignResponse{signString, hashString}
signSimpleJSON, marshalErr := json.Marshal(signResponse)
if marshalErr != nil {
fmt.Println("error in signFileSimpleRequest Marshal:", marshalErr)
return response, errors.New("Marshall of request failed")
}
response = Response{Message: string(signSimpleJSON[:]), OK: true}
case "VerifyFileSimple":
fileName := strings.Replace(request["value"].(map[string]interface{})["s3item"].(string), "/", "_", -1)
file, fileOpenErr := os.Create(fileName)
if fileOpenErr != nil {
fmt.Println("error in Open File for S3 in verifyFileSimpleRequest:", fileOpenErr)
return response, errors.New("error in Open File for S3")
}
defer file.Close()
sess, _ := session.NewSession(&aws.Config{
Region: aws.String("us-east-1")},
)
downloader := s3manager.NewDownloader(sess)
numBytes, fileDownloadErr := downloader.Download(file,
&s3.GetObjectInput{
Bucket: aws.String(request["value"].(map[string]interface{})["s3bucket"].(string)),
Key: aws.String(request["value"].(map[string]interface{})["s3item"].(string)),
})
if fileDownloadErr != nil || numBytes == 0 {
fmt.Println("error in Downloading File from S3:", fileDownloadErr)
return response, errors.New("error in Downloading File from S3")
}
signVerified, _ := VerifyFileSimple(request["value"].(map[string]interface{})["signtext"].(string), fileName,
request["value"].(map[string]interface{})["hashtext"].(string),
request["value"].(map[string]interface{})["publickey"].(string))
verifyResponse := VerifySignResposne{(signVerified == 0)}
verifySimpleJSON, marshalErr := json.Marshal(verifyResponse)
if marshalErr != nil {
fmt.Println("error in VerifySignSimple Marshal:", marshalErr)
return response, errors.New("Marshall of request failed")
}
response = Response{Message: string(verifySimpleJSON), OK: true}
default:
fmt.Println("Unknown Operation")
return response, errors.New("Unkown Operation Requested")
}
return response, nil
}
func main() {
lambda.Start(Handler)
}