-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
250 lines (212 loc) · 7.09 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package main
import (
"crypto/rsa"
"encoding/json"
"errors"
"fmt"
"github.com/golang-jwt/jwt/v5"
"github.com/spf13/viper"
"github.com/thibauult/tee-mock-server/pki"
"log"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
const (
socketPath = "/run/container_launcher/teeserver.sock"
// default values
defaultGoogleServiceAccount = "tee-mock-server@localhost.gserviceaccount.com"
defaultTokenExpirationInMinutes = 5
)
type attestationTokenRequest struct {
Audience string `json:"audience"`
TokenType string `json:"token_type"`
Nonces []string `json:"nonces"`
}
type tokenConfig struct {
signingKey *rsa.PrivateKey
chain interface{}
googleServiceAccount string
tokenExpirationInMinutes int
}
func main() {
config := loadConfig()
// Create a Unix domain socket and listen for incoming connections.
socket, err := net.Listen("unix", socketPath)
if err != nil {
log.Fatal(err)
}
// Cleanup the socket file.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go removeSocketOnInterrupt(c)
// Load RSA private key
config.signingKey, err = pki.GetSigningPrivateKey()
if err != nil {
log.Printf("Failed to load private key: %v\n", err)
log.Fatal(err)
}
config.chain = pki.GetCertificateChain()
log.Printf("Root Certificate:\n\n%s\n", pki.GetRootCertificate())
m := http.NewServeMux()
m.HandleFunc("/v1/token", newPostTokenHandler(config))
server := http.Server{Handler: m}
log.Println("Start serving on socket", socketPath, "...")
if err := server.Serve(socket); err != nil {
log.Fatal(err)
}
}
func loadConfig() tokenConfig {
const googleServiceAccount = "google_service_account"
const tokenExpirationInMinutes = "token_expiration_in_minutes"
viper.SetDefault(googleServiceAccount, defaultGoogleServiceAccount)
viper.SetDefault(tokenExpirationInMinutes, defaultTokenExpirationInMinutes)
viper.SetEnvPrefix("tee")
err := viper.BindEnv(tokenExpirationInMinutes, googleServiceAccount)
if err != nil {
log.Fatal(err)
}
config := tokenConfig{}
config.googleServiceAccount = viper.GetString(googleServiceAccount)
config.tokenExpirationInMinutes = viper.GetInt(tokenExpirationInMinutes)
log.Println(config)
return config
}
func newPostTokenHandler(config tokenConfig) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
_, _ = fmt.Fprintf(w, "{ \"message\": \"only POST is supported\" }")
return
}
tokenRequest, err := parseAndValidateAttestationTokenRequest(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = fmt.Fprintf(w, "{ \"message\": \"%s\" }", err.Error())
return
}
signedJwt := newToken(config, tokenRequest)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(signedJwt + "\n"))
}
}
func parseAndValidateAttestationTokenRequest(r *http.Request) (*attestationTokenRequest, error) {
tokenRequest := attestationTokenRequest{}
err := json.NewDecoder(r.Body).Decode(&tokenRequest)
if err != nil {
return nil, errors.New("failed to parse attestation token request")
}
if tokenRequest.TokenType != "PKI" {
return nil, fmt.Errorf("invalid token type: %s", tokenRequest.TokenType)
}
if len(tokenRequest.Audience) == 0 {
return nil, fmt.Errorf("audience not set")
}
return &tokenRequest, nil
}
func newToken(config tokenConfig, tokenRequest *attestationTokenRequest) string {
log.Println("Creating new Token...")
// Create a new token
token := jwt.NewWithClaims(jwt.SigningMethodRS256, createClaims(config, tokenRequest.Audience, tokenRequest.Nonces))
token.Header["x5c"] = config.chain
// Sign the token with the RSA private key
signedToken, err := token.SignedString(config.signingKey)
if err != nil {
log.Printf("Error signing token: %v\n", err)
}
log.Println("New Token successfully signed")
return signedToken
}
func createClaims(config tokenConfig, audience string, nonces []string) jwt.MapClaims {
if nonces == nil {
nonces = []string{}
}
return jwt.MapClaims{
"iss": "https://confidentialcomputing.googleapis.com",
"sub": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/us-central1-a/instances/INSTANCE_NAME", // TODO set PROJECT_ID and INSTANCE_NAME
"aud": audience,
"eat_nonce": nonces,
"exp": time.Now().Add(5 * time.Minute).Unix(),
"iat": time.Now().Unix(),
"nbf": time.Now().Unix(),
// Confidential Space Claims
"eat_profile": "https://cloud.google.com/confidential-computing/confidential-space/docs/reference/token-claims",
"secboot": true,
"oemid": 11129,
"google_service_accounts": []string{
config.googleServiceAccount,
},
"hwmodel": "GCP_AMD_SEV",
"swname": "CONFIDENTIAL_SPACE",
"swversion": []string{"240900"},
"submods": map[string]interface{}{
"confidental_space": map[string]interface{}{
"monitoring_enabled": map[string]bool{
"memory": false,
},
"support_attributes": []string{
"LATEST", "STABLE", "USABLE",
},
},
"container": map[string]interface{}{
"args": []string{
"/customnonce",
"/docker-entrypoint.sh",
"nginx",
"-g",
"daemon off;",
},
"env": map[string]string{
"HOSTNAME": "HOST_NAME", // TODO set HOST_NAME
"NGINX_VERSION": "1.27.0",
"NJS_RELEASE": "2~bookworm",
"NJS_VERSION": "0.8.4",
"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"PKG_RELEASE": "2~bookworm",
},
"image_digest": "sha256:67682bda769fae1ccf5183192b8daf37b64cae99c6c3302650f6f8bf5f0f95df",
"image_id": "sha256:fffffc90d343cbcb01a5032edac86db5998c536cd0a366514121a45c6723765c",
"image_reference": "docker.io/library/nginx:latest",
"image_signatures": []interface{}{
map[string]string{
"key_id": "<hexadecimal-sha256-fingerprint-public-key1>",
"signature": "<base64-encoded-signature>",
"signature_algorithm": "RSASSA_PSS_SHA256",
},
map[string]string{
"key_id": "<hexadecimal-sha256-fingerprint-public-key2>",
"signature": "<base64-encoded-signature>",
"signature_algorithm": "RSASSA_PSS_SHA256",
},
map[string]string{
"key_id": "<hexadecimal-sha256-fingerprint-public-key3>",
"signature": "<base64-encoded-signature>",
"signature_algorithm": "ECDSA_P256_SHA256",
},
},
"restart_policy": "Never",
},
"gce": map[string]string{
"instance_id": "INSTANCE_ID", // TODO set INSTANCE_ID
"instance_name": "INSTANCE_NAME", // TODO set INSTANCE_NAME
"project_id": "PROJECT_ID", // TODO set PROJECT_ID
"project_number": "PROJECT_NUMBER", // TODO set PROJECT_NUMBER
"zone": "us-central1-a", // TODO set ZONE
},
},
}
}
func removeSocketOnInterrupt(c chan os.Signal) {
<-c
log.Println("Removing socket...")
err := os.Remove(socketPath)
if err != nil {
log.Println("Failed to remove", socketPath, err)
} else {
log.Println("Successfully removed", socketPath)
}
os.Exit(1)
}