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