| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package registration | 5 package registration |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "time" |
| 9 |
| 8 ds "github.com/luci/gae/service/datastore" | 10 ds "github.com/luci/gae/service/datastore" |
| 9 "golang.org/x/net/context" | 11 "golang.org/x/net/context" |
| 10 "google.golang.org/grpc/codes" | 12 "google.golang.org/grpc/codes" |
| 11 | 13 |
| 12 "github.com/luci/luci-go/appengine/logdog/coordinator" | 14 "github.com/luci/luci-go/appengine/logdog/coordinator" |
| 13 "github.com/luci/luci-go/appengine/logdog/coordinator/hierarchy" | 15 "github.com/luci/luci-go/appengine/logdog/coordinator/hierarchy" |
| 14 "github.com/luci/luci-go/common/api/logdog_coordinator/registration/v1" | 16 "github.com/luci/luci-go/common/api/logdog_coordinator/registration/v1" |
| 15 "github.com/luci/luci-go/common/clock" | 17 "github.com/luci/luci-go/common/clock" |
| 16 "github.com/luci/luci-go/common/cryptorand" | 18 "github.com/luci/luci-go/common/cryptorand" |
| 17 "github.com/luci/luci-go/common/gcloud/pubsub" | 19 "github.com/luci/luci-go/common/gcloud/pubsub" |
| 18 "github.com/luci/luci-go/common/grpcutil" | 20 "github.com/luci/luci-go/common/grpcutil" |
| 19 "github.com/luci/luci-go/common/logdog/types" | 21 "github.com/luci/luci-go/common/logdog/types" |
| 20 log "github.com/luci/luci-go/common/logging" | 22 log "github.com/luci/luci-go/common/logging" |
| 23 "github.com/luci/luci-go/common/proto/google" |
| 21 ) | 24 ) |
| 22 | 25 |
| 23 func (s *server) RegisterPrefix(c context.Context, req *logdog.RegisterPrefixReq
uest) (*logdog.RegisterPrefixResponse, error) { | 26 func (s *server) RegisterPrefix(c context.Context, req *logdog.RegisterPrefixReq
uest) (*logdog.RegisterPrefixResponse, error) { |
| 24 log.Fields{ | 27 log.Fields{ |
| 25 "project": req.Project, | 28 "project": req.Project, |
| 26 "prefix": req.Prefix, | 29 "prefix": req.Prefix, |
| 27 "source": req.SourceInfo, | 30 "source": req.SourceInfo, |
| 28 "expiration": req.Expiration.Duration(), | 31 "expiration": req.Expiration.Duration(), |
| 29 }.Debugf(c, "Registering log prefix.") | 32 }.Debugf(c, "Registering log prefix.") |
| 30 | 33 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 51 } | 54 } |
| 52 | 55 |
| 53 // Load our service and project configurations. | 56 // Load our service and project configurations. |
| 54 svcs := coordinator.GetServices(c) | 57 svcs := coordinator.GetServices(c) |
| 55 cfg, err := svcs.Config(c) | 58 cfg, err := svcs.Config(c) |
| 56 if err != nil { | 59 if err != nil { |
| 57 log.WithError(err).Errorf(c, "Failed to load service configurati
on.") | 60 log.WithError(err).Errorf(c, "Failed to load service configurati
on.") |
| 58 return nil, grpcutil.Internal | 61 return nil, grpcutil.Internal |
| 59 } | 62 } |
| 60 | 63 |
| 64 pcfg, err := coordinator.CurrentProjectConfig(c) |
| 65 if err != nil { |
| 66 log.WithError(err).Errorf(c, "Failed to load project configurati
on.") |
| 67 return nil, grpcutil.Internal |
| 68 } |
| 69 |
| 70 // Determine our prefix expiration. This must be > 0, else there will be
no |
| 71 // window when log streams can be registered and this prefix is useless. |
| 72 // |
| 73 // We will choose the shortest expiration window defined by our request
and |
| 74 // our project and service configurations. |
| 75 expiration := resolveExpiration(req.Expiration, cfg.Coordinator.PrefixEx
piration, pcfg.PrefixExpiration) |
| 76 if expiration <= 0 { |
| 77 log.Errorf(c, "Refusing to register prefix in expired state.") |
| 78 return nil, grpcutil.Errf(codes.InvalidArgument, "no prefix expi
ration defined") |
| 79 } |
| 80 |
| 61 // Determine our Pub/Sub topic. | 81 // Determine our Pub/Sub topic. |
| 62 cfgTransport := cfg.Transport | 82 cfgTransport := cfg.Transport |
| 63 if cfgTransport == nil { | 83 if cfgTransport == nil { |
| 64 log.Errorf(c, "Missing transport configuration.") | 84 log.Errorf(c, "Missing transport configuration.") |
| 65 return nil, grpcutil.Internal | 85 return nil, grpcutil.Internal |
| 66 } | 86 } |
| 67 | 87 |
| 68 cfgTransportPubSub := cfgTransport.GetPubsub() | 88 cfgTransportPubSub := cfgTransport.GetPubsub() |
| 69 if cfgTransportPubSub == nil { | 89 if cfgTransportPubSub == nil { |
| 70 log.Errorf(c, "Missing transport Pub/Sub configuration.") | 90 log.Errorf(c, "Missing transport Pub/Sub configuration.") |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 case exists: | 146 case exists: |
| 127 log.Errorf(c, "The prefix is already registered (transac
tional).") | 147 log.Errorf(c, "The prefix is already registered (transac
tional).") |
| 128 return grpcutil.AlreadyExists | 148 return grpcutil.AlreadyExists |
| 129 } | 149 } |
| 130 | 150 |
| 131 // The Prefix is not registered, so let's register it. | 151 // The Prefix is not registered, so let's register it. |
| 132 pfx.Created = now | 152 pfx.Created = now |
| 133 pfx.Prefix = string(prefix) | 153 pfx.Prefix = string(prefix) |
| 134 pfx.Source = req.SourceInfo | 154 pfx.Source = req.SourceInfo |
| 135 pfx.Secret = []byte(secret) | 155 pfx.Secret = []byte(secret) |
| 156 pfx.Expiration = now.Add(expiration) |
| 136 | 157 |
| 137 if err := di.Put(pfx); err != nil { | 158 if err := di.Put(pfx); err != nil { |
| 138 log.WithError(err).Errorf(c, "Failed to register prefix.
") | 159 log.WithError(err).Errorf(c, "Failed to register prefix.
") |
| 139 return grpcutil.Internal | 160 return grpcutil.Internal |
| 140 } | 161 } |
| 141 | 162 |
| 142 log.Infof(c, "The prefix was successfully registered.") | 163 log.Infof(c, "The prefix was successfully registered.") |
| 143 return nil | 164 return nil |
| 144 }, nil) | 165 }, nil) |
| 145 if err != nil { | 166 if err != nil { |
| 146 log.WithError(err).Errorf(c, "Failed to register prefix (transac
tional).") | 167 log.WithError(err).Errorf(c, "Failed to register prefix (transac
tional).") |
| 147 return nil, err | 168 return nil, err |
| 148 } | 169 } |
| 149 | 170 |
| 150 return &logdog.RegisterPrefixResponse{ | 171 return &logdog.RegisterPrefixResponse{ |
| 151 Secret: []byte(secret), | 172 Secret: []byte(secret), |
| 152 LogBundleTopic: string(pubsubTopic), | 173 LogBundleTopic: string(pubsubTopic), |
| 153 }, nil | 174 }, nil |
| 154 } | 175 } |
| 176 |
| 177 func resolveExpiration(candidates ...*google.Duration) (exp time.Duration) { |
| 178 for _, c := range candidates { |
| 179 if cd := c.Duration(); cd > 0 && (exp <= 0 || cd < exp) { |
| 180 exp = cd |
| 181 } |
| 182 } |
| 183 return |
| 184 } |
| OLD | NEW |