-
Notifications
You must be signed in to change notification settings - Fork 0
/
attested_secret_writer.go
66 lines (60 loc) · 1.48 KB
/
attested_secret_writer.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
package attested_secret_writer
import (
"context"
"github.com/edgelesssys/ego/enclave"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
func GetOneSecret() ([]byte, error) {
// Create a TLS config with a self-signed certificate and an embedded report.
tlsCfg, err := enclave.CreateAttestationServerTLSConfig()
if err != nil {
panic(err)
}
secretChannel := make(chan []byte)
// Create HTTPS server.
srv := http.Server{Addr: "0.0.0.0:8080", TLSConfig: tlsCfg}
go func() {
defer close(secretChannel)
// Handle requests and send a signal to shutdown after the first request
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Only POST requests are allowed", http.StatusMethodNotAllowed)
return
}
data, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
secretChannel <- data
w.WriteHeader(http.StatusOK)
})
if err := srv.ListenAndServeTLS("", ""); err != nil {
if err != http.ErrServerClosed {
log.Fatal(err)
}
}
}()
secret := <-secretChannel
// Gracefully shut down the server
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err = srv.Shutdown(ctx)
if err != nil {
return nil, err
}
return secret, nil
}
func WriteSecret() {
secret, err := GetOneSecret()
if err != nil {
log.Fatal(err)
}
if err := os.WriteFile("/secret", secret, 0600); err != nil {
log.Fatal(err)
}
}