| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. | 1 // Copyright 2016 The LUCI Authors. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 package registration | 15 package registration |
| 16 | 16 |
| 17 import ( | 17 import ( |
| 18 ds "github.com/luci/gae/service/datastore" | 18 ds "github.com/luci/gae/service/datastore" |
| 19 | 19 |
| 20 "github.com/luci/luci-go/common/clock" | 20 "github.com/luci/luci-go/common/clock" |
| 21 "github.com/luci/luci-go/common/data/rand/cryptorand" | 21 "github.com/luci/luci-go/common/data/rand/cryptorand" |
| 22 "github.com/luci/luci-go/common/gcloud/pubsub" | 22 "github.com/luci/luci-go/common/gcloud/pubsub" |
| 23 log "github.com/luci/luci-go/common/logging" | 23 log "github.com/luci/luci-go/common/logging" |
| 24 "github.com/luci/luci-go/common/proto/google" | 24 "github.com/luci/luci-go/common/proto/google" |
| 25 "github.com/luci/luci-go/grpc/grpcutil" | 25 "github.com/luci/luci-go/grpc/grpcutil" |
| 26 "github.com/luci/luci-go/logdog/api/endpoints/coordinator/registration/v
1" | 26 "github.com/luci/luci-go/logdog/api/endpoints/coordinator/registration/v
1" |
| 27 "github.com/luci/luci-go/logdog/appengine/coordinator" | 27 "github.com/luci/luci-go/logdog/appengine/coordinator" |
| 28 "github.com/luci/luci-go/logdog/appengine/coordinator/endpoints" | 28 "github.com/luci/luci-go/logdog/appengine/coordinator/endpoints" |
| 29 "github.com/luci/luci-go/logdog/appengine/coordinator/hierarchy" | |
| 30 "github.com/luci/luci-go/logdog/common/types" | 29 "github.com/luci/luci-go/logdog/common/types" |
| 31 | 30 |
| 32 "golang.org/x/net/context" | 31 "golang.org/x/net/context" |
| 33 "google.golang.org/grpc/codes" | 32 "google.golang.org/grpc/codes" |
| 34 ) | 33 ) |
| 35 | 34 |
| 36 func (s *server) RegisterPrefix(c context.Context, req *logdog.RegisterPrefixReq
uest) (*logdog.RegisterPrefixResponse, error) { | 35 func (s *server) RegisterPrefix(c context.Context, req *logdog.RegisterPrefixReq
uest) (*logdog.RegisterPrefixResponse, error) { |
| 37 log.Fields{ | 36 log.Fields{ |
| 38 "project": req.Project, | 37 "project": req.Project, |
| 39 "prefix": req.Prefix, | 38 "prefix": req.Prefix, |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 | 101 |
| 103 pubsubTopic := pubsub.NewTopic(cfgTransportPubSub.Project, cfgTransportP
ubSub.Topic) | 102 pubsubTopic := pubsub.NewTopic(cfgTransportPubSub.Project, cfgTransportP
ubSub.Topic) |
| 104 if err := pubsubTopic.Validate(); err != nil { | 103 if err := pubsubTopic.Validate(); err != nil { |
| 105 log.Fields{ | 104 log.Fields{ |
| 106 log.ErrorKey: err, | 105 log.ErrorKey: err, |
| 107 "topic": pubsubTopic, | 106 "topic": pubsubTopic, |
| 108 }.Errorf(c, "Invalid transport Pub/Sub topic.") | 107 }.Errorf(c, "Invalid transport Pub/Sub topic.") |
| 109 return nil, grpcutil.Internal | 108 return nil, grpcutil.Internal |
| 110 } | 109 } |
| 111 | 110 |
| 112 // Best effort: register the stream prefix hierarchy components, includi
ng the | |
| 113 // separator. | |
| 114 // | |
| 115 // Determine which hierarchy components we need to add. | |
| 116 comps := hierarchy.Components(prefix.AsPathPrefix(""), false) | |
| 117 if comps, err = hierarchy.Missing(c, comps); err != nil { | |
| 118 log.WithError(err).Warningf(c, "Failed to probe for missing hier
archy components.") | |
| 119 } | |
| 120 | |
| 121 // Before we go into transaction, try and put these entries. This should
not | |
| 122 // be contested, since components don't share an entity root. | |
| 123 // | |
| 124 // If this fails, that's okay; we'll handle this when the stream gets | |
| 125 // registered. | |
| 126 if err := hierarchy.PutMulti(c, comps); err != nil { | |
| 127 log.WithError(err).Infof(c, "Failed to add missing hierarchy com
ponents.") | |
| 128 } | |
| 129 | |
| 130 // The prefix doesn't appear to be registered. Prepare to transactionall
y | 111 // The prefix doesn't appear to be registered. Prepare to transactionall
y |
| 131 // register it. | 112 // register it. |
| 132 now := clock.Now(c).UTC() | 113 now := clock.Now(c).UTC() |
| 133 | 114 |
| 134 // Generate a prefix secret. | 115 // Generate a prefix secret. |
| 135 secret := make(types.PrefixSecret, types.PrefixSecretLength) | 116 secret := make(types.PrefixSecret, types.PrefixSecretLength) |
| 136 if _, err := cryptorand.Read(c, []byte(secret)); err != nil { | 117 if _, err := cryptorand.Read(c, []byte(secret)); err != nil { |
| 137 log.WithError(err).Errorf(c, "Failed to generate prefix secret."
) | 118 log.WithError(err).Errorf(c, "Failed to generate prefix secret."
) |
| 138 return nil, grpcutil.Internal | 119 return nil, grpcutil.Internal |
| 139 } | 120 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 if err != nil { | 154 if err != nil { |
| 174 log.WithError(err).Errorf(c, "Failed to register prefix (transac
tional).") | 155 log.WithError(err).Errorf(c, "Failed to register prefix (transac
tional).") |
| 175 return nil, err | 156 return nil, err |
| 176 } | 157 } |
| 177 | 158 |
| 178 return &logdog.RegisterPrefixResponse{ | 159 return &logdog.RegisterPrefixResponse{ |
| 179 Secret: []byte(secret), | 160 Secret: []byte(secret), |
| 180 LogBundleTopic: string(pubsubTopic), | 161 LogBundleTopic: string(pubsubTopic), |
| 181 }, nil | 162 }, nil |
| 182 } | 163 } |
| OLD | NEW |