forked from google/certificate-transparency-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_server.go
149 lines (131 loc) · 4.44 KB
/
proxy_server.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
// Copyright 2019 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package submission
import (
"context"
"encoding/json"
"fmt"
"html/template"
"net/http"
"os"
"strings"
"time"
ct "github.com/ctylim/certificate-transparency-go-p192"
"github.com/ctylim/certificate-transparency-go-p192/trillian/ctfe"
"github.com/google/trillian/monitoring"
)
// ProxyServer wraps Proxy and handles HTTP-requests for it.
type ProxyServer struct {
p *Proxy
addTimeout time.Duration
loadPendingLogs bool
}
// NewProxyServer creates ProxyServer instance. Call Run() to init.
func NewProxyServer(logListPath string, dBuilder DistributorBuilder, reqTimeout time.Duration, mf monitoring.MetricFactory) *ProxyServer {
s := &ProxyServer{addTimeout: reqTimeout}
s.p = NewProxy(NewLogListManager(NewLogListRefresher(logListPath), mf), dBuilder, mf)
return s
}
// Run starts regular Log list updates in the background, running until the
// context is canceled. Blocks until initialization happens.
func (s *ProxyServer) Run(ctx context.Context, logListRefreshInterval time.Duration, rootsRefreshInterval time.Duration, loadPendingLogs bool) {
s.loadPendingLogs = loadPendingLogs
s.p.Run(ctx, logListRefreshInterval, rootsRefreshInterval)
<-s.p.Init
}
// SCTBatch represents JSON response to add-pre-chain method of proxy.
type SCTBatch struct {
SCTs []ct.SignedCertificateTimestamp `json:"scts"`
}
func marshalSCTs(scts []*AssignedSCT) ([]byte, error) {
var jsonSCTsObj SCTBatch
jsonSCTsObj.SCTs = make([]ct.SignedCertificateTimestamp, 0, len(scts))
for _, sct := range scts {
jsonSCTsObj.SCTs = append(jsonSCTsObj.SCTs, *sct.SCT)
}
return json.Marshal(jsonSCTsObj)
}
// handleAddSomeChain is helper func choosing between AddChain and AddPreChain
// based on asPreChain value
func (s *ProxyServer) handleAddSomeChain(w http.ResponseWriter, r *http.Request, asPreChain bool) {
if r.Method != http.MethodPost {
http.NotFound(w, r)
return
}
addChainReq, err := ctfe.ParseBodyAsJSONChain(r)
if err != nil {
rc := http.StatusBadRequest
pre := ""
if asPreChain {
pre = "pre-"
}
http.Error(w, fmt.Sprintf("proxy: failed to parse add-%schain body: %s", pre, err), rc)
return
}
ctx, cancel := context.WithTimeout(r.Context(), s.addTimeout)
defer cancel()
var scts []*AssignedSCT
if asPreChain {
scts, err = s.p.AddPreChain(ctx, addChainReq.Chain, s.loadPendingLogs)
} else {
scts, err = s.p.AddChain(ctx, addChainReq.Chain, s.loadPendingLogs)
}
if err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
data, err := marshalSCTs(scts)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, string(data))
}
// HandleAddPreChain handles multiplexed add-pre-chain HTTP request.
func (s *ProxyServer) HandleAddPreChain(w http.ResponseWriter, r *http.Request) {
s.handleAddSomeChain(w, r, true /* asPreChain*/)
}
// HandleAddChain handles multiplexed add-chain HTTP request.
func (s *ProxyServer) HandleAddChain(w http.ResponseWriter, r *http.Request) {
s.handleAddSomeChain(w, r, false /* asPreChain*/)
}
func stringToHTML(s string) template.HTML {
return template.HTML(strings.Replace(template.HTMLEscapeString(string(s)), "\n", "<br>", -1))
}
// InfoData wraps data field required for info-page.
type InfoData struct {
PolicyName string
LogListPath template.HTML
LogListJSON template.HTML
}
// HandleInfo handles info-page request.
func (s *ProxyServer) HandleInfo(w http.ResponseWriter, r *http.Request) {
data := InfoData{
s.p.dist.policy.Name(),
stringToHTML(s.p.llWatcher.Source()),
stringToHTML(string(s.p.llWatcher.LastJSON())),
}
wd, err := os.Getwd()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
t, err := template.ParseFiles(wd + "/submission/view/info.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
t.Execute(w, data)
}