|
8 | 8 | "os"
|
9 | 9 | "path/filepath"
|
10 | 10 | "secret-vm-attest-rest-server/pkg/html"
|
| 11 | + "strconv" |
11 | 12 | "text/template"
|
12 | 13 | "time"
|
13 | 14 | )
|
@@ -99,41 +100,91 @@ func MakeAttestationHTMLHandler(fileName, attestationType string) http.HandlerFu
|
99 | 100 | return
|
100 | 101 | }
|
101 | 102 |
|
102 |
| - // Determine Title and Description per type (Self uses "Report") |
103 |
| - var titleText, descText string |
104 |
| - if attestationType == "Self" { |
105 |
| - titleText = fmt.Sprintf("%s Attestation Report", attestationType) |
106 |
| - descText = fmt.Sprintf("Below is the %s attestation report. Click the copy button to copy it to your clipboard.", attestationType) |
107 |
| - } else { |
108 |
| - titleText = fmt.Sprintf("%s Attestation Quote", attestationType) |
109 |
| - descText = fmt.Sprintf("Below is the %s attestation quote. Click the copy button to copy it to your clipboard.", attestationType) |
110 |
| - } |
111 |
| - |
112 |
| - // Prepare template data |
113 |
| - data := struct { |
114 |
| - Title string |
115 |
| - Description string |
116 |
| - Quote string |
117 |
| - ShowVerify bool |
118 |
| - }{ |
119 |
| - Title: titleText, |
120 |
| - Description: descText, |
121 |
| - Quote: string(content), |
122 |
| - ShowVerify: attestationType == "CPU", // only CPU shows verification link |
123 |
| - } |
124 |
| - |
125 |
| - // Parse and execute HTML template |
126 |
| - tmpl, err := template.New("attestationHtml").Parse(html.HtmlTemplate) |
127 |
| - if err != nil { |
128 |
| - log.Printf("Error parsing HTML template: %v", err) |
129 |
| - http.Error(w, "Internal server error", http.StatusInternalServerError) |
130 |
| - return |
131 |
| - } |
132 |
| - w.Header().Set("Content-Type", "text/html; charset=utf-8") |
133 |
| - if err := tmpl.Execute(w, data); err != nil { |
134 |
| - log.Printf("Error executing HTML template: %v", err) |
135 |
| - http.Error(w, "Internal server error", http.StatusInternalServerError) |
136 |
| - } |
| 103 | + // Determine Title and Description per type (Self uses "Report") |
| 104 | + var titleText, descText string |
| 105 | + if attestationType == "Self" { |
| 106 | + titleText = fmt.Sprintf("%s Attestation Report", attestationType) |
| 107 | + descText = fmt.Sprintf("Below is the %s attestation report. Click the copy button to copy it to your clipboard.", attestationType) |
| 108 | + } else { |
| 109 | + titleText = fmt.Sprintf("%s Attestation Quote", attestationType) |
| 110 | + descText = fmt.Sprintf("Below is the %s attestation quote. Click the copy button to copy it to your clipboard.", attestationType) |
| 111 | + } |
| 112 | + |
| 113 | + // Prepare template data |
| 114 | + data := struct { |
| 115 | + Title string |
| 116 | + Description string |
| 117 | + Quote string |
| 118 | + ShowVerify bool |
| 119 | + }{ |
| 120 | + Title: titleText, |
| 121 | + Description: descText, |
| 122 | + Quote: string(content), |
| 123 | + ShowVerify: attestationType == "CPU", // only CPU shows verification link |
| 124 | + } |
| 125 | + |
| 126 | + // Parse and execute HTML template |
| 127 | + tmpl, err := template.New("attestationHtml").Parse(html.HtmlTemplate) |
| 128 | + if err != nil { |
| 129 | + log.Printf("Error parsing HTML template: %v", err) |
| 130 | + http.Error(w, "Internal server error", http.StatusInternalServerError) |
| 131 | + return |
| 132 | + } |
| 133 | + w.Header().Set("Content-Type", "text/html; charset=utf-8") |
| 134 | + if err := tmpl.Execute(w, data); err != nil { |
| 135 | + log.Printf("Error executing HTML template: %v", err) |
| 136 | + http.Error(w, "Internal server error", http.StatusInternalServerError) |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +// MakeDockerLogsHandler serves plain-text Docker logs, |
| 142 | +// supporting a 'lines' query param (default 1000). |
| 143 | +func MakeDockerLogsHandler() http.HandlerFunc { |
| 144 | + return func(w http.ResponseWriter, r *http.Request) { |
| 145 | + if r.Method != http.MethodGet { |
| 146 | + respondWithError(w, http.StatusMethodNotAllowed, "Method not allowed", "Only GET requests are supported") |
| 147 | + return |
| 148 | + } |
| 149 | + lines := 1000 |
| 150 | + if l := r.URL.Query().Get("lines"); l != "" { |
| 151 | + if v, err := strconv.Atoi(l); err == nil { |
| 152 | + lines = v |
| 153 | + } |
| 154 | + } |
| 155 | + logs, err := fetchDockerLogs(lines) |
| 156 | + if err != nil { |
| 157 | + log.Printf("Error fetching Docker logs: %v", err) |
| 158 | + respondWithError(w, http.StatusInternalServerError, "Failed to fetch Docker logs", err.Error()) |
| 159 | + return |
| 160 | + } |
| 161 | + w.Header().Set("Content-Type", "text/plain; charset=utf-8") |
| 162 | + w.Header().Set("X-Content-Type-Options", "nosniff") |
| 163 | + w.WriteHeader(http.StatusOK) |
| 164 | + w.Write([]byte(logs)) |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +// MakeDockerLiveLogsHandler serves the live-updating HTML page |
| 169 | +// that polls /docker_logs with a selectable line count. |
| 170 | +func MakeDockerLiveLogsHandler() http.HandlerFunc { |
| 171 | + return func(w http.ResponseWriter, r *http.Request) { |
| 172 | + if r.Method != http.MethodGet { |
| 173 | + respondWithError(w, http.StatusMethodNotAllowed, "Method not allowed", "Only GET requests are supported") |
| 174 | + return |
| 175 | + } |
| 176 | + data := struct{ Title string }{Title: "Live Docker Container Logs"} |
| 177 | + tmpl, err := template.New("liveLogs").Parse(html.DockerLiveLogsTemplate) |
| 178 | + if err != nil { |
| 179 | + log.Printf("Error parsing live logs template: %v", err) |
| 180 | + http.Error(w, "Internal server error", http.StatusInternalServerError) |
| 181 | + return |
| 182 | + } |
| 183 | + w.Header().Set("Content-Type", "text/html; charset=utf-8") |
| 184 | + if err := tmpl.Execute(w, data); err != nil { |
| 185 | + log.Printf("Error executing live logs template: %v", err) |
| 186 | + http.Error(w, "Internal server error", http.StatusInternalServerError) |
| 187 | + } |
137 | 188 | }
|
138 | 189 | }
|
139 | 190 |
|
|
0 commit comments