| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 services | 5 package services |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "crypto/subtle" |
| 8 "time" | 9 "time" |
| 9 | 10 |
| 10 "github.com/golang/protobuf/proto" | 11 "github.com/golang/protobuf/proto" |
| 11 ds "github.com/luci/gae/service/datastore" | 12 ds "github.com/luci/gae/service/datastore" |
| 12 "github.com/luci/luci-go/appengine/logdog/coordinator" | 13 "github.com/luci/luci-go/appengine/logdog/coordinator" |
| 13 "github.com/luci/luci-go/appengine/logdog/coordinator/hierarchy" | 14 "github.com/luci/luci-go/appengine/logdog/coordinator/hierarchy" |
| 14 "github.com/luci/luci-go/appengine/logdog/coordinator/mutations" | 15 "github.com/luci/luci-go/appengine/logdog/coordinator/mutations" |
| 15 "github.com/luci/luci-go/appengine/tumble" | 16 "github.com/luci/luci-go/appengine/tumble" |
| 16 "github.com/luci/luci-go/common/api/logdog_coordinator/services/v1" | 17 "github.com/luci/luci-go/common/api/logdog_coordinator/services/v1" |
| 17 "github.com/luci/luci-go/common/clock" | 18 "github.com/luci/luci-go/common/clock" |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 | 96 |
| 96 pcfg, err := coordinator.CurrentProjectConfig(c) | 97 pcfg, err := coordinator.CurrentProjectConfig(c) |
| 97 if err != nil { | 98 if err != nil { |
| 98 log.WithError(err).Errorf(c, "Failed to load current project con
figuration.") | 99 log.WithError(err).Errorf(c, "Failed to load current project con
figuration.") |
| 99 return nil, grpcutil.Internal | 100 return nil, grpcutil.Internal |
| 100 } | 101 } |
| 101 | 102 |
| 102 // Determine the archival expiration. | 103 // Determine the archival expiration. |
| 103 archiveDelayMax := resolveArchiveDelay(cfg.Coordinator, pcfg) | 104 archiveDelayMax := resolveArchiveDelay(cfg.Coordinator, pcfg) |
| 104 | 105 |
| 105 » // Register our Prefix. | 106 » // Load our Prefix. It must be registered. |
| 106 » // | 107 » di := ds.Get(c) |
| 107 » // This will also verify that our request secret matches the registered
one, | 108 |
| 108 » // if one is registered. | 109 » pfx := &coordinator.LogPrefix{ID: coordinator.LogPrefixID(prefix)} |
| 109 » // | 110 » if err := di.Get(pfx); err != nil { |
| 110 » // Note: This step will not be necessary once a "register prefix" RPC ca
ll is | 111 » » log.Fields{ |
| 111 » // implemented. | 112 » » » log.ErrorKey: err, |
| 112 » lsp := logStreamPrefix{ | 113 » » » "id": pfx.ID, |
| 113 » » prefix: string(prefix), | 114 » » » "prefix": prefix, |
| 114 » » secret: req.Secret, | 115 » » }.Errorf(c, "Failed to load log stream prefix.") |
| 116 » » if err == ds.ErrNoSuchEntity { |
| 117 » » » return nil, grpcutil.Errf(codes.FailedPrecondition, "pre
fix is not registered") |
| 118 » » } |
| 119 » » return nil, grpcutil.Internal |
| 115 } | 120 } |
| 116 » pfx, err := registerPrefix(c, &lsp) | 121 |
| 117 » if err != nil { | 122 » // The prefix secret must match the request secret. If it does, we know
this |
| 118 » » log.Errorf(c, "Failed to register/validate log stream prefix.") | 123 » // is a legitimate registration attempt. |
| 119 » » return nil, err | 124 » if subtle.ConstantTimeCompare(pfx.Secret, req.Secret) != 1 { |
| 125 » » log.Errorf(c, "Request secret does not match prefix secret.") |
| 126 » » return nil, grpcutil.Errf(codes.InvalidArgument, "invalid secret
") |
| 120 } | 127 } |
| 121 log.Fields{ | |
| 122 "prefix": pfx.Prefix, | |
| 123 "prefixCreated": pfx.Created, | |
| 124 }.Debugf(c, "Loaded log stream prefix.") | |
| 125 | 128 |
| 126 » di := ds.Get(c) | 129 » // Check for registration, and that the prefix did not expire |
| 130 » // (non-transactional). |
| 127 ls := &coordinator.LogStream{ID: coordinator.LogStreamID(path)} | 131 ls := &coordinator.LogStream{ID: coordinator.LogStreamID(path)} |
| 128 lst := ls.State(di) | 132 lst := ls.State(di) |
| 129 | 133 |
| 130 » // Check for registration (non-transactional). | 134 » if err := di.GetMulti([]interface{}{ls, lst}); err != nil { |
| 131 » if err := checkRegisterStream(di, ls, lst); err != nil { | |
| 132 if !anyNoSuchEntity(err) { | 135 if !anyNoSuchEntity(err) { |
| 133 log.WithError(err).Errorf(c, "Failed to check for log st
ream.") | 136 log.WithError(err).Errorf(c, "Failed to check for log st
ream.") |
| 134 return nil, err | 137 return nil, err |
| 135 } | 138 } |
| 136 | 139 |
| 137 // The stream is not registered. Perform a transactional registr
ation via | 140 // The stream is not registered. Perform a transactional registr
ation via |
| 138 // mutation. | 141 // mutation. |
| 139 // | 142 // |
| 140 // Determine which hierarchy components we need to add. | 143 // Determine which hierarchy components we need to add. |
| 141 comps := hierarchy.Components(path) | 144 comps := hierarchy.Components(path) |
| 142 if comps, err = hierarchy.Missing(di, comps); err != nil { | 145 if comps, err = hierarchy.Missing(di, comps); err != nil { |
| 143 log.WithError(err).Warningf(c, "Failed to probe for miss
ing hierarchy components.") | 146 log.WithError(err).Warningf(c, "Failed to probe for miss
ing hierarchy components.") |
| 144 } | 147 } |
| 145 | 148 |
| 146 // Before we go into transaction, try and put these entries. Thi
s should not | 149 // Before we go into transaction, try and put these entries. Thi
s should not |
| 147 // be contested, since components don't share an entity root. | 150 // be contested, since components don't share an entity root. |
| 148 if err := hierarchy.PutMulti(di, comps); err != nil { | 151 if err := hierarchy.PutMulti(di, comps); err != nil { |
| 149 » » » log.WithError(err).Infof(c, "Failed to add missing hiera
rchy components.") | 152 » » » log.WithError(err).Errorf(c, "Failed to add missing hier
archy components.") |
| 150 return nil, grpcutil.Internal | 153 return nil, grpcutil.Internal |
| 151 } | 154 } |
| 152 | 155 |
| 153 // The stream does not exist. Proceed with transactional registr
ation. | 156 // The stream does not exist. Proceed with transactional registr
ation. |
| 154 err = tumble.RunMutation(c, ®isterStreamMutation{ | 157 err = tumble.RunMutation(c, ®isterStreamMutation{ |
| 155 RegisterStreamRequest: req, | 158 RegisterStreamRequest: req, |
| 156 desc: &desc, | 159 desc: &desc, |
| 157 pfx: pfx, | 160 pfx: pfx, |
| 158 lst: lst, | 161 lst: lst, |
| 159 ls: ls, | 162 ls: ls, |
| 160 archiveDelay: archiveDelayMax, | 163 archiveDelay: archiveDelayMax, |
| 161 }) | 164 }) |
| 162 if err != nil { | 165 if err != nil { |
| 163 log.Fields{ | 166 log.Fields{ |
| 164 log.ErrorKey: err, | 167 log.ErrorKey: err, |
| 165 }.Errorf(c, "Failed to register LogStream.") | 168 }.Errorf(c, "Failed to register LogStream.") |
| 166 return nil, err | 169 return nil, err |
| 167 } | 170 } |
| 168 } | 171 } |
| 169 | 172 |
| 170 return &logdog.RegisterStreamResponse{ | 173 return &logdog.RegisterStreamResponse{ |
| 171 Id: string(ls.ID), | 174 Id: string(ls.ID), |
| 172 State: buildLogStreamState(ls, lst), | 175 State: buildLogStreamState(ls, lst), |
| 173 }, nil | 176 }, nil |
| 174 } | 177 } |
| 175 | 178 |
| 176 func checkRegisterStream(di ds.Interface, ls *coordinator.LogStream, lst *coordi
nator.LogStreamState) error { | |
| 177 // Load the existing log stream state. | |
| 178 // | |
| 179 // We have already verified that the secrets match when the Prefix was | |
| 180 // registered. This will have to verify secrets when prefix registration | |
| 181 // moves its own RPC. | |
| 182 return di.GetMulti([]interface{}{ls, lst}) | |
| 183 } | |
| 184 | |
| 185 type registerStreamMutation struct { | 179 type registerStreamMutation struct { |
| 186 *logdog.RegisterStreamRequest | 180 *logdog.RegisterStreamRequest |
| 187 | 181 |
| 188 desc *logpb.LogStreamDescriptor | 182 desc *logpb.LogStreamDescriptor |
| 189 pfx *coordinator.LogPrefix | 183 pfx *coordinator.LogPrefix |
| 190 ls *coordinator.LogStream | 184 ls *coordinator.LogStream |
| 191 lst *coordinator.LogStreamState | 185 lst *coordinator.LogStreamState |
| 192 archiveDelay time.Duration | 186 archiveDelay time.Duration |
| 193 } | 187 } |
| 194 | 188 |
| 195 func (m *registerStreamMutation) RollForward(c context.Context) ([]tumble.Mutati
on, error) { | 189 func (m *registerStreamMutation) RollForward(c context.Context) ([]tumble.Mutati
on, error) { |
| 196 di := ds.Get(c) | 190 di := ds.Get(c) |
| 197 | 191 |
| 198 » // Check if our stream is registered (transactional). | 192 » // Load our state and stream (transactional). |
| 199 » if err := checkRegisterStream(di, m.ls, m.lst); err != nil { | 193 » if err := di.GetMulti([]interface{}{m.ls, m.lst}); err != nil { |
| 200 if !anyNoSuchEntity(err) { | 194 if !anyNoSuchEntity(err) { |
| 201 log.WithError(err).Errorf(c, "Failed to check for stream
registration (transactional).") | 195 log.WithError(err).Errorf(c, "Failed to check for stream
registration (transactional).") |
| 202 return nil, err | 196 return nil, err |
| 203 } | 197 } |
| 204 | 198 |
| 205 // The stream is not yet registered. | 199 // The stream is not yet registered. |
| 206 log.Infof(c, "Registering new log stream.") | 200 log.Infof(c, "Registering new log stream.") |
| 207 | 201 |
| 208 now := clock.Now(c).UTC() | 202 now := clock.Now(c).UTC() |
| 209 | 203 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 return nil, grpcutil.Internal | 246 return nil, grpcutil.Internal |
| 253 } | 247 } |
| 254 } | 248 } |
| 255 | 249 |
| 256 return nil, nil | 250 return nil, nil |
| 257 } | 251 } |
| 258 | 252 |
| 259 func (m *registerStreamMutation) Root(c context.Context) *ds.Key { | 253 func (m *registerStreamMutation) Root(c context.Context) *ds.Key { |
| 260 return ds.Get(c).KeyForObj(m.ls) | 254 return ds.Get(c).KeyForObj(m.ls) |
| 261 } | 255 } |
| OLD | NEW |