| 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/settings" | |
| 18 "github.com/luci/luci-go/milo/common/miloerror" | |
| 19 "github.com/luci/luci-go/server/auth" | |
| 20 "github.com/luci/luci-go/server/templates" | |
| 21 | |
| 22 "github.com/julienschmidt/httprouter" | |
| 23 "golang.org/x/net/context" | |
| 24 ) | |
| 25 | |
| 26 // AnnotationStreamHandler is a ThemedHandler that renders a LogDog Milo | |
| 27 // annotation protobuf stream. | |
| 28 // | |
| 29 // The protobuf stream is fetched live from LogDog and cached locally, either | |
| 30 // temporarily (if incomplete) or indefinitely (if complete). | |
| 31 type AnnotationStreamHandler struct{} | |
| 32 | |
| 33 // GetTemplateName implements settings.ThemedHandler. | |
| 34 func (s *AnnotationStreamHandler) GetTemplateName(t settings.Theme) string { | |
| 35 return "build.html" | |
| 36 } | |
| 37 | |
| 38 // Render implements settings.ThemedHandler. | |
| 39 func (s *AnnotationStreamHandler) Render(c context.Context, req *http.Request, p
httprouter.Params) ( | |
| 40 *templates.Args, error) { | |
| 41 | |
| 42 as := AnnotationStream{ | |
| 43 Project: cfgtypes.ProjectName(p.ByName("project")), | |
| 44 Path: types.StreamPath(strings.Trim(p.ByName("path"), "/")), | |
| 45 } | |
| 46 if err := as.Normalize(); err != nil { | |
| 47 return nil, &miloerror.Error{ | |
| 48 Message: err.Error(), | |
| 49 Code: http.StatusBadRequest, | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 // Setup our LogDog client. | |
| 54 var err error | |
| 55 if as.Client, err = NewClient(c, ""); err != nil { | |
| 56 log.WithError(err).Errorf(c, "Failed to generate LogDog client."
) | |
| 57 return nil, &miloerror.Error{ | |
| 58 Code: http.StatusInternalServerError, | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 // Load the Milo annotation protobuf from the annotation stream. | |
| 63 if _, err := as.Fetch(c); err != nil { | |
| 64 switch errors.Unwrap(err) { | |
| 65 case coordinator.ErrNoSuchStream: | |
| 66 return nil, &miloerror.Error{ | |
| 67 Message: "Stream does not exist", | |
| 68 Code: http.StatusNotFound, | |
| 69 } | |
| 70 | |
| 71 case coordinator.ErrNoAccess: | |
| 72 return nil, &miloerror.Error{ | |
| 73 Message: "No access to stream", | |
| 74 Code: http.StatusForbidden, | |
| 75 } | |
| 76 | |
| 77 default: | |
| 78 return nil, &miloerror.Error{ | |
| 79 Message: "Failed to load stream", | |
| 80 Code: http.StatusInternalServerError, | |
| 81 } | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 // Convert the Milo Annotation protobuf to Milo objects. | |
| 86 return &templates.Args{ | |
| 87 "Build": as.toMiloBuild(c), | |
| 88 }, nil | |
| 89 } | |
| 90 | |
| 91 func resolveHost(host string) (string, error) { | |
| 92 // Resolveour our Host, and validate it against a host whitelist. | |
| 93 switch host { | |
| 94 case "": | |
| 95 return defaultLogDogHost, nil | |
| 96 case defaultLogDogHost, "luci-logdog-dev.appspot.com": | |
| 97 return host, nil | |
| 98 default: | |
| 99 return "", errors.Reason("host %(host)q is not whitelisted"). | |
| 100 D("host", host). | |
| 101 Err() | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 // NewClient generates a new LogDog client that issues requests on behalf of the | |
| 106 // current user. | |
| 107 func NewClient(c context.Context, host string) (*coordinator.Client, error) { | |
| 108 var err error | |
| 109 if host, err = resolveHost(host); err != nil { | |
| 110 return nil, err | |
| 111 } | |
| 112 | |
| 113 // Initialize the LogDog client authentication. | |
| 114 t, err := auth.GetRPCTransport(c, auth.AsUser) | |
| 115 if err != nil { | |
| 116 return nil, errors.New("failed to get transport for LogDog serve
r") | |
| 117 } | |
| 118 | |
| 119 // Setup our LogDog client. | |
| 120 return coordinator.NewClient(&prpc.Client{ | |
| 121 C: &http.Client{ | |
| 122 Transport: t, | |
| 123 }, | |
| 124 Host: host, | |
| 125 }), nil | |
| 126 } | |
| OLD | NEW |