Skip to content

Commit 67d2bce

Browse files
Copybara Servicecopybara-github
authored andcommitted
Reorganize code to remove assumption that HTTPS is served directly by the server
PiperOrigin-RevId: 856321718
1 parent 32d0fef commit 67d2bce

2 files changed

Lines changed: 17 additions & 7 deletions

File tree

fleetspeak/src/server/https/file_server.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,26 @@ import (
2626
log "github.com/golang/glog"
2727
"github.com/google/fleetspeak/fleetspeak/src/common"
2828
"github.com/google/fleetspeak/fleetspeak/src/server/authorizer"
29+
"github.com/google/fleetspeak/fleetspeak/src/server/comms"
2930
"golang.org/x/time/rate"
3031
)
3132

3233
// unauthorizedLogging is used to rate-limit logging of unauthorized file
3334
// requests to avoid spam during potential DoS attacks.
3435
var unauthorizedLogging = rate.Sometimes{Interval: time.Minute}
3536

36-
// fileServer wraps a Communicator in order to serve files.
37+
// fileServer uses a subset of Communicator in order to serve files.
3738
type fileServer struct {
38-
*Communicator
39+
fs func() comms.Context
40+
p Params
41+
}
42+
43+
// NewFileServer allows reuse of fileServer in non-HTTPS communicators.
44+
func NewFileServer(fs func() comms.Context, p Params) fileServer {
45+
return fileServer{
46+
fs: fs,
47+
p: p,
48+
}
3949
}
4050

4151
// ServeHTTP implements http.Handler
@@ -67,9 +77,9 @@ func (s fileServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
6777
}
6878

6979
ctx := req.Context()
70-
data, modtime, err := s.fs.ReadFile(ctx, service, name)
80+
data, modtime, err := s.fs().ReadFile(ctx, service, name)
7181
if err != nil {
72-
if s.fs.IsNotFound(err) {
82+
if s.fs().IsNotFound(err) {
7383
http.Error(res, "file not found", http.StatusNotFound)
7484
return
7585
}
@@ -86,7 +96,7 @@ func (s fileServer) authorizeFileRequest(req *http.Request) error {
8696
return err
8797
}
8898
addr := net.TCPAddrFromAddrPort(addrPort)
89-
if !s.fs.Authorizer().Allow1(addr) {
99+
if !s.fs().Authorizer().Allow1(addr) {
90100
return fmt.Errorf("unauthorized via Allow1 (addr: %v)", addr)
91101
}
92102

@@ -105,7 +115,7 @@ func (s fileServer) authorizeFileRequest(req *http.Request) error {
105115
ClientLabels: req.Header["X-Fleetspeak-Labels"],
106116
}
107117

108-
if !s.fs.Authorizer().Allow2(addr, ci) {
118+
if !s.fs().Authorizer().Allow2(addr, ci) {
109119
return fmt.Errorf("unauthorized via Allow2 (addr: %v, contact: %v)", addr, ci)
110120
}
111121
return nil

fleetspeak/src/server/https/https.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func NewCommunicator(p Params) (*Communicator, error) {
173173
}
174174
mux.Handle("/streaming-message", &CompressionHandler{sms})
175175
}
176-
mux.Handle("/files/", fileServer{h})
176+
mux.Handle("/files/", fileServer{fs: func() comms.Context { return h.fs }, p: h.p})
177177

178178
switch l := h.p.Listener.(type) {
179179
case *net.TCPListener:

0 commit comments

Comments
 (0)