-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
141 lines (110 loc) · 3.15 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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/go-resty/resty/v2"
"github.com/hyperledger-labs/fabric-private-chaincode/client_sdk/go/sample/irb/pkg/fpc"
"github.com/hyperledger-labs/fabric-private-chaincode/client_sdk/go/sample/irb/pkg/k8s"
)
func main() {
startServer()
}
var (
flagPort string
)
func init() {
flag.StringVar(&flagPort, "port", "4000", "Port to listen on")
}
func startServer() {
config := cors.DefaultConfig()
config.AllowAllOrigins = true
config.AllowHeaders = append(config.AllowHeaders, "x-user")
r := gin.Default()
r.Use(cors.New(config))
// controller
r.POST("/api/proposal", submitProposal)
r.POST("/api/launch", launch)
r.POST("/api/kill", kill)
r.POST("/api/execute", execute)
r.Run(":" + flagPort)
}
func submitProposal(c *gin.Context) {
fmt.Println("new proposal")
// submit new proposal via FPC
// return answer
c.IndentedJSON(http.StatusOK, "proposalSubmitted")
}
func launch(c *gin.Context) {
fmt.Println("Launching worker ...")
// create instance
r, err := k8s.Launch()
if err != nil {
c.IndentedJSON(http.StatusInternalServerError, err.Error())
return
}
fmt.Printf("K8s: %s\n", r)
// TODO this json hack is not ideal but it works for now
type attestation struct {
PublicKey string
Attestation string
}
var a attestation
// do attestation call worker/api/attestation
fmt.Printf("Requesting attestation from worker ...\n")
client := resty.New()
_, err = client.R().SetResult(&a).Post("http://localhost:5000/api/attestation")
if err != nil {
c.IndentedJSON(http.StatusInternalServerError, err.Error())
return
}
fmt.Printf("Attestation: %s\n", a)
// TODO do some attestation evidence transformation here
// Take the quote and send it to IAS
// Return attestation evidence from IAS to client
fmt.Printf("Perform attestation evidence transformation\n")
// return answer
c.IndentedJSON(http.StatusOK, a)
}
func kill(c *gin.Context) {
fmt.Printf("Killing worker")
resp, err := k8s.Kill()
if err != nil {
c.IndentedJSON(http.StatusInternalServerError, err.Error())
return
}
c.IndentedJSON(http.StatusOK, resp)
}
func execute(c *gin.Context) {
fmt.Printf("Requesting evaluation pack from FPC Chaincode ...\n")
// retrieve evaluation pack from FPC
evaluationPack := fpc.FetchEvaluationPack()
serializedEvaluationPack, err := json.MarshalIndent(evaluationPack, "", " ")
if err != nil {
c.IndentedJSON(http.StatusInternalServerError, err.Error())
return
}
type ExecutionRequest struct {
EncryptedEvaluationPack []byte `json:"data" binding:"required"`
}
fmt.Printf("Sending evaluation pack to worker and execute ...\n")
// call worker/api/execute
client := resty.New()
resp, err := client.R().
SetBody(ExecutionRequest{serializedEvaluationPack}).
Post("http://localhost:5000/api/execute")
if err != nil {
c.IndentedJSON(http.StatusInternalServerError, err.Error())
return
}
fmt.Printf("Received result: %s\n", string(resp.Body()))
// return answer
c.IndentedJSON(http.StatusOK, string(resp.Body()))
}