| 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 "crypto/subtle" |
| 9 "errors" | 9 "errors" |
| 10 "time" | 10 "time" |
| 11 | 11 |
| 12 ds "github.com/luci/gae/service/datastore" | 12 ds "github.com/luci/gae/service/datastore" |
| 13 "github.com/luci/luci-go/appengine/logdog/coordinator" | 13 "github.com/luci/luci-go/appengine/logdog/coordinator" |
| 14 "github.com/luci/luci-go/appengine/logdog/coordinator/mutations" | 14 "github.com/luci/luci-go/appengine/logdog/coordinator/mutations" |
| 15 "github.com/luci/luci-go/appengine/tumble" | 15 "github.com/luci/luci-go/appengine/tumble" |
| 16 "github.com/luci/luci-go/common/api/logdog_coordinator/services/v1" | 16 "github.com/luci/luci-go/common/api/logdog_coordinator/services/v1" |
| 17 "github.com/luci/luci-go/common/clock" | 17 "github.com/luci/luci-go/common/clock" |
| 18 "github.com/luci/luci-go/common/config" |
| 18 "github.com/luci/luci-go/common/grpcutil" | 19 "github.com/luci/luci-go/common/grpcutil" |
| 19 "github.com/luci/luci-go/common/logdog/types" | 20 "github.com/luci/luci-go/common/logdog/types" |
| 20 log "github.com/luci/luci-go/common/logging" | 21 log "github.com/luci/luci-go/common/logging" |
| 21 "github.com/luci/luci-go/common/proto/logdog/logpb" | 22 "github.com/luci/luci-go/common/proto/logdog/logpb" |
| 22 "golang.org/x/net/context" | 23 "golang.org/x/net/context" |
| 23 "google.golang.org/grpc" | 24 "google.golang.org/grpc" |
| 24 "google.golang.org/grpc/codes" | 25 "google.golang.org/grpc/codes" |
| 25 ) | 26 ) |
| 26 | 27 |
| 27 func matchesLogStream(r *logdog.RegisterStreamRequest, ls *coordinator.LogStream
) error { | 28 func matchesLogStream(r *logdog.RegisterStreamRequest, ls *coordinator.LogStream
) error { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 41 if err != nil { | 42 if err != nil { |
| 42 return errors.New("log stream has invalid descriptor value") | 43 return errors.New("log stream has invalid descriptor value") |
| 43 } | 44 } |
| 44 if !dv.Equal(r.Desc) { | 45 if !dv.Equal(r.Desc) { |
| 45 return errors.New("descriptor protobufs do not match") | 46 return errors.New("descriptor protobufs do not match") |
| 46 } | 47 } |
| 47 | 48 |
| 48 return nil | 49 return nil |
| 49 } | 50 } |
| 50 | 51 |
| 51 func loadLogStreamState(ls *coordinator.LogStream) *logdog.LogStreamState { | 52 func loadLogStreamState(project config.ProjectName, ls *coordinator.LogStream) *
logdog.LogStreamState { |
| 52 st := logdog.LogStreamState{ | 53 st := logdog.LogStreamState{ |
| 54 Project: string(project), |
| 53 Path: string(ls.Path()), | 55 Path: string(ls.Path()), |
| 54 ProtoVersion: ls.ProtoVersion, | 56 ProtoVersion: ls.ProtoVersion, |
| 55 TerminalIndex: ls.TerminalIndex, | 57 TerminalIndex: ls.TerminalIndex, |
| 56 Archived: ls.Archived(), | 58 Archived: ls.Archived(), |
| 57 Purged: ls.Purged, | 59 Purged: ls.Purged, |
| 58 } | 60 } |
| 59 if !ls.Terminated() { | 61 if !ls.Terminated() { |
| 60 st.TerminalIndex = -1 | 62 st.TerminalIndex = -1 |
| 61 } | 63 } |
| 62 return &st | 64 return &st |
| 63 } | 65 } |
| 64 | 66 |
| 65 // RegisterStream is an idempotent stream state register operation. | 67 // RegisterStream is an idempotent stream state register operation. |
| 66 func (s *server) RegisterStream(c context.Context, req *logdog.RegisterStreamReq
uest) (*logdog.RegisterStreamResponse, error) { | 68 func (s *server) RegisterStream(c context.Context, req *logdog.RegisterStreamReq
uest) (*logdog.RegisterStreamResponse, error) { |
| 67 log.Fields{ | 69 log.Fields{ |
| 68 » » "path": req.Path, | 70 » » "project": req.Project, |
| 71 » » "path": req.Path, |
| 69 }.Infof(c, "Registration request for log stream.") | 72 }.Infof(c, "Registration request for log stream.") |
| 70 | 73 |
| 71 path := types.StreamPath(req.Path) | 74 path := types.StreamPath(req.Path) |
| 72 if err := path.Validate(); err != nil { | 75 if err := path.Validate(); err != nil { |
| 73 return nil, grpcutil.Errf(codes.InvalidArgument, "Invalid path (
%s): %s", path, err) | 76 return nil, grpcutil.Errf(codes.InvalidArgument, "Invalid path (
%s): %s", path, err) |
| 74 } | 77 } |
| 75 | 78 |
| 76 switch { | 79 switch { |
| 77 case req.ProtoVersion == "": | 80 case req.ProtoVersion == "": |
| 78 return nil, grpcutil.Errf(codes.InvalidArgument, "No protobuf ve
rsion supplied.") | 81 return nil, grpcutil.Errf(codes.InvalidArgument, "No protobuf ve
rsion supplied.") |
| 79 case req.ProtoVersion != logpb.Version: | 82 case req.ProtoVersion != logpb.Version: |
| 80 return nil, grpcutil.Errf(codes.InvalidArgument, "Unrecognized p
rotobuf version.") | 83 return nil, grpcutil.Errf(codes.InvalidArgument, "Unrecognized p
rotobuf version.") |
| 81 case len(req.Secret) != types.StreamSecretLength: | |
| 82 return nil, grpcutil.Errf(codes.InvalidArgument, "Invalid secret
length (%d != %d)", | |
| 83 len(req.Secret), types.StreamSecretLength) | |
| 84 case req.Desc == nil: | 84 case req.Desc == nil: |
| 85 return nil, grpcutil.Errf(codes.InvalidArgument, "Missing log st
ream descriptor.") | 85 return nil, grpcutil.Errf(codes.InvalidArgument, "Missing log st
ream descriptor.") |
| 86 } | 86 } |
| 87 | 87 |
| 88 secret := types.PrefixSecret(req.Secret) |
| 89 if err := secret.Validate(); err != nil { |
| 90 return nil, grpcutil.Errf(codes.InvalidArgument, "Invalid prefix
secret: %s", err) |
| 91 } |
| 92 |
| 88 prefix, name := path.Split() | 93 prefix, name := path.Split() |
| 89 if err := req.Desc.Validate(true); err != nil { | 94 if err := req.Desc.Validate(true); err != nil { |
| 90 return nil, grpcutil.Errf(codes.InvalidArgument, "Invalid log st
ream descriptor: %s", err) | 95 return nil, grpcutil.Errf(codes.InvalidArgument, "Invalid log st
ream descriptor: %s", err) |
| 91 } | 96 } |
| 92 switch { | 97 switch { |
| 93 case req.Desc.Prefix != string(prefix): | 98 case req.Desc.Prefix != string(prefix): |
| 94 return nil, grpcutil.Errf(codes.InvalidArgument, "Descriptor pre
fix does not match path (%s != %s)", | 99 return nil, grpcutil.Errf(codes.InvalidArgument, "Descriptor pre
fix does not match path (%s != %s)", |
| 95 req.Desc.Prefix, prefix) | 100 req.Desc.Prefix, prefix) |
| 96 case req.Desc.Name != string(name): | 101 case req.Desc.Name != string(name): |
| 97 return nil, grpcutil.Errf(codes.InvalidArgument, "Descriptor nam
e does not match path (%s != %s)", | 102 return nil, grpcutil.Errf(codes.InvalidArgument, "Descriptor nam
e does not match path (%s != %s)", |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 }.Errorf(c, "Failed to register LogStream.") | 141 }.Errorf(c, "Failed to register LogStream.") |
| 137 return nil, filterError(err) | 142 return nil, filterError(err) |
| 138 } | 143 } |
| 139 | 144 |
| 140 default: | 145 default: |
| 141 log.WithError(err).Errorf(c, "Failed to check for log stream.") | 146 log.WithError(err).Errorf(c, "Failed to check for log stream.") |
| 142 return nil, grpcutil.Internal | 147 return nil, grpcutil.Internal |
| 143 } | 148 } |
| 144 | 149 |
| 145 return &logdog.RegisterStreamResponse{ | 150 return &logdog.RegisterStreamResponse{ |
| 146 » » State: loadLogStreamState(ls), | 151 » » State: loadLogStreamState(coordinator.Project(c), ls), |
| 147 Secret: ls.Secret, | 152 Secret: ls.Secret, |
| 148 }, nil | 153 }, nil |
| 149 } | 154 } |
| 150 | 155 |
| 151 func filterError(err error) error { | 156 func filterError(err error) error { |
| 152 switch { | 157 switch { |
| 153 case err == nil: | 158 case err == nil: |
| 154 return nil | 159 return nil |
| 155 case grpc.Code(err) == codes.Unknown: | 160 case grpc.Code(err) == codes.Unknown: |
| 156 return grpcutil.Internal | 161 return grpcutil.Internal |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 return []tumble.Mutation{ | 235 return []tumble.Mutation{ |
| 231 &mutations.PutHierarchyMutation{ | 236 &mutations.PutHierarchyMutation{ |
| 232 Path: m.Path(), | 237 Path: m.Path(), |
| 233 }, | 238 }, |
| 234 }, nil | 239 }, nil |
| 235 } | 240 } |
| 236 | 241 |
| 237 func (m registerStreamMutation) Root(c context.Context) *ds.Key { | 242 func (m registerStreamMutation) Root(c context.Context) *ds.Key { |
| 238 return ds.Get(c).KeyForObj(m.LogStream) | 243 return ds.Get(c).KeyForObj(m.LogStream) |
| 239 } | 244 } |
| OLD | NEW |