OLD | NEW |
| (Empty) |
1 // Copyright 2016 The LUCI Authors. All rights reserved. | |
2 // Use of this source code is governed under the Apache License, Version 2.0 | |
3 // that can be found in the LICENSE file. | |
4 | |
5 package logdog | |
6 | |
7 import ( | |
8 "net/http" | |
9 "strings" | |
10 | |
11 "github.com/luci/luci-go/common/errors" | |
12 log "github.com/luci/luci-go/common/logging" | |
13 "github.com/luci/luci-go/grpc/prpc" | |
14 "github.com/luci/luci-go/logdog/client/coordinator" | |
15 "github.com/luci/luci-go/logdog/common/types" | |
16 "github.com/luci/luci-go/luci_config/common/cfgtypes" | |
17 "github.com/luci/luci-go/milo/appengine/common" | |
18 "github.com/luci/luci-go/server/auth" | |
19 "github.com/luci/luci-go/server/router" | |
20 "github.com/luci/luci-go/server/templates" | |
21 | |
22 "golang.org/x/net/context" | |
23 ) | |
24 | |
25 // AnnotationStreamHandler is a Handler that renders a LogDog Milo | |
26 // annotation protobuf stream. | |
27 // | |
28 // The protobuf stream is fetched live from LogDog and cached locally, either | |
29 // temporarily (if incomplete) or indefinitely (if complete). | |
30 type AnnotationStreamHandler struct{} | |
31 | |
32 func BuildHandler(c *router.Context) { | |
33 (&AnnotationStreamHandler{}).Render(c) | |
34 return | |
35 } | |
36 | |
37 // Render implements settings.ThemedHandler. | |
38 func (s *AnnotationStreamHandler) Render(c *router.Context) { | |
39 as := AnnotationStream{ | |
40 Project: cfgtypes.ProjectName(c.Params.ByName("project")), | |
41 Path: types.StreamPath(strings.Trim(c.Params.ByName("path"),
"/")), | |
42 } | |
43 if err := as.Normalize(); err != nil { | |
44 common.ErrorPage(c, http.StatusBadRequest, err.Error()) | |
45 return | |
46 } | |
47 | |
48 // Setup our LogDog client. | |
49 var err error | |
50 host := strings.TrimSpace(c.Params.ByName("logdog_host")) | |
51 if as.Client, err = NewClient(c.Context, host); err != nil { | |
52 log.WithError(err).Errorf(c.Context, "Failed to generate LogDog
client.") | |
53 common.ErrorPage(c, http.StatusInternalServerError, "Failed to g
enerate LogDog client") | |
54 return | |
55 } | |
56 | |
57 // Load the Milo annotation protobuf from the annotation stream. | |
58 if _, err := as.Fetch(c.Context); err != nil { | |
59 switch errors.Unwrap(err) { | |
60 case coordinator.ErrNoSuchStream: | |
61 common.ErrorPage(c, http.StatusNotFound, "Stream does no
t exist") | |
62 | |
63 case coordinator.ErrNoAccess: | |
64 common.ErrorPage(c, http.StatusForbidden, "No access to
LogDog stream") | |
65 | |
66 case errNotMilo, errNotDatagram: | |
67 // The user requested a LogDog url that isn't a Milo ann
otation. | |
68 common.ErrorPage(c, http.StatusBadRequest, err.Error()) | |
69 | |
70 default: | |
71 log.WithError(err).Errorf(c.Context, "Failed to load Log
Dog stream.") | |
72 common.ErrorPage(c, http.StatusInternalServerError, "Fai
led to load LogDog stream") | |
73 } | |
74 return | |
75 } | |
76 | |
77 templates.MustRender(c.Context, c.Writer, "pages/build.html", templates.
Args{ | |
78 "Build": as.toMiloBuild(c.Context), | |
79 }) | |
80 } | |
81 | |
82 func resolveHost(host string) (string, error) { | |
83 // Resolveour our Host, and validate it against a host whitelist. | |
84 switch host { | |
85 case "": | |
86 return defaultLogDogHost, nil | |
87 case defaultLogDogHost, "luci-logdog-dev.appspot.com": | |
88 return host, nil | |
89 default: | |
90 return "", errors.Reason("host %(host)q is not whitelisted"). | |
91 D("host", host). | |
92 Err() | |
93 } | |
94 } | |
95 | |
96 // NewClient generates a new LogDog client that issues requests on behalf of the | |
97 // current user. | |
98 func NewClient(c context.Context, host string) (*coordinator.Client, error) { | |
99 var err error | |
100 if host, err = resolveHost(host); err != nil { | |
101 return nil, err | |
102 } | |
103 | |
104 // Initialize the LogDog client authentication. | |
105 t, err := auth.GetRPCTransport(c, auth.AsUser) | |
106 if err != nil { | |
107 return nil, errors.New("failed to get transport for LogDog serve
r") | |
108 } | |
109 | |
110 // Setup our LogDog client. | |
111 return coordinator.NewClient(&prpc.Client{ | |
112 C: &http.Client{ | |
113 Transport: t, | |
114 }, | |
115 Host: host, | |
116 }), nil | |
117 } | |
OLD | NEW |