| 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 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 // Setup our LogDog client. | 53 // Setup our LogDog client. |
| 54 var err error | 54 var err error |
| 55 if as.Client, err = NewClient(c, ""); err != nil { | 55 if as.Client, err = NewClient(c, ""); err != nil { |
| 56 log.WithError(err).Errorf(c, "Failed to generate LogDog client."
) | 56 log.WithError(err).Errorf(c, "Failed to generate LogDog client."
) |
| 57 return nil, &miloerror.Error{ | 57 return nil, &miloerror.Error{ |
| 58 Code: http.StatusInternalServerError, | 58 Code: http.StatusInternalServerError, |
| 59 } | 59 } |
| 60 } | 60 } |
| 61 | 61 |
| 62 // Load the Milo annotation protobuf from the annotation stream. | 62 // Load the Milo annotation protobuf from the annotation stream. |
| 63 » if _, err := as.Load(c); err != nil { | 63 » if _, err := as.Fetch(c); err != nil { |
| 64 return nil, err | 64 return nil, err |
| 65 } | 65 } |
| 66 | 66 |
| 67 // Convert the Milo Annotation protobuf to Milo objects. | 67 // Convert the Milo Annotation protobuf to Milo objects. |
| 68 return &templates.Args{ | 68 return &templates.Args{ |
| 69 "Build": as.toMiloBuild(c), | 69 "Build": as.toMiloBuild(c), |
| 70 }, nil | 70 }, nil |
| 71 } | 71 } |
| 72 | 72 |
| 73 func resolveHost(host string) (string, error) { |
| 74 // Resolveour our Host, and validate it against a host whitelist. |
| 75 switch host { |
| 76 case "": |
| 77 return defaultLogDogHost, nil |
| 78 case defaultLogDogHost, "luci-logdog-dev.appspot.com": |
| 79 return host, nil |
| 80 default: |
| 81 return "", errors.Reason("host %(host)q is not whitelisted"). |
| 82 D("host", host). |
| 83 Err() |
| 84 } |
| 85 } |
| 86 |
| 73 // NewClient generates a new LogDog client that issues requests on behalf of the | 87 // NewClient generates a new LogDog client that issues requests on behalf of the |
| 74 // current user. | 88 // current user. |
| 75 func NewClient(c context.Context, host string) (*coordinator.Client, error) { | 89 func NewClient(c context.Context, host string) (*coordinator.Client, error) { |
| 76 » if host == "" { | 90 » var err error |
| 77 » » host = defaultLogDogHost | 91 » if host, err = resolveHost(host); err != nil { |
| 92 » » return nil, err |
| 78 } | 93 } |
| 79 | 94 |
| 80 // Initialize the LogDog client authentication. | 95 // Initialize the LogDog client authentication. |
| 81 t, err := auth.GetRPCTransport(c, auth.AsUser) | 96 t, err := auth.GetRPCTransport(c, auth.AsUser) |
| 82 if err != nil { | 97 if err != nil { |
| 83 return nil, errors.New("failed to get transport for LogDog serve
r") | 98 return nil, errors.New("failed to get transport for LogDog serve
r") |
| 84 } | 99 } |
| 85 | 100 |
| 86 // Setup our LogDog client. | 101 // Setup our LogDog client. |
| 87 return coordinator.NewClient(&prpc.Client{ | 102 return coordinator.NewClient(&prpc.Client{ |
| 88 C: &http.Client{ | 103 C: &http.Client{ |
| 89 Transport: t, | 104 Transport: t, |
| 90 }, | 105 }, |
| 91 Host: host, | 106 Host: host, |
| 92 }), nil | 107 }), nil |
| 93 } | 108 } |
| OLD | NEW |