| 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 | 10 |
| 11 ds "github.com/luci/gae/service/datastore" | 11 ds "github.com/luci/gae/service/datastore" |
| 12 "github.com/luci/luci-go/appengine/logdog/coordinator" | 12 "github.com/luci/luci-go/appengine/logdog/coordinator" |
| 13 "github.com/luci/luci-go/appengine/logdog/coordinator/mutations" | 13 "github.com/luci/luci-go/appengine/logdog/coordinator/mutations" |
| 14 "github.com/luci/luci-go/appengine/tumble" | 14 "github.com/luci/luci-go/appengine/tumble" |
| 15 "github.com/luci/luci-go/common/api/logdog_coordinator/services/v1" | 15 "github.com/luci/luci-go/common/api/logdog_coordinator/services/v1" |
| 16 "github.com/luci/luci-go/common/clock" | 16 "github.com/luci/luci-go/common/clock" |
| 17 "github.com/luci/luci-go/common/config" |
| 17 "github.com/luci/luci-go/common/grpcutil" | 18 "github.com/luci/luci-go/common/grpcutil" |
| 18 "github.com/luci/luci-go/common/logdog/types" | 19 "github.com/luci/luci-go/common/logdog/types" |
| 19 log "github.com/luci/luci-go/common/logging" | 20 log "github.com/luci/luci-go/common/logging" |
| 20 "github.com/luci/luci-go/common/proto/logdog/logpb" | 21 "github.com/luci/luci-go/common/proto/logdog/logpb" |
| 21 "golang.org/x/net/context" | 22 "golang.org/x/net/context" |
| 22 "google.golang.org/grpc" | 23 "google.golang.org/grpc" |
| 23 "google.golang.org/grpc/codes" | 24 "google.golang.org/grpc/codes" |
| 24 ) | 25 ) |
| 25 | 26 |
| 26 func matchesLogStream(r *logdog.RegisterStreamRequest, ls *coordinator.LogStream
) error { | 27 func matchesLogStream(r *logdog.RegisterStreamRequest, ls *coordinator.LogStream
) error { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 40 if err != nil { | 41 if err != nil { |
| 41 return errors.New("log stream has invalid descriptor value") | 42 return errors.New("log stream has invalid descriptor value") |
| 42 } | 43 } |
| 43 if !dv.Equal(r.Desc) { | 44 if !dv.Equal(r.Desc) { |
| 44 return errors.New("descriptor protobufs do not match") | 45 return errors.New("descriptor protobufs do not match") |
| 45 } | 46 } |
| 46 | 47 |
| 47 return nil | 48 return nil |
| 48 } | 49 } |
| 49 | 50 |
| 50 func loadLogStreamState(ls *coordinator.LogStream) *logdog.LogStreamState { | 51 func loadLogStreamState(project config.ProjectName, ls *coordinator.LogStream) *
logdog.LogStreamState { |
| 51 st := logdog.LogStreamState{ | 52 st := logdog.LogStreamState{ |
| 53 Project: string(project), |
| 52 Path: string(ls.Path()), | 54 Path: string(ls.Path()), |
| 53 ProtoVersion: ls.ProtoVersion, | 55 ProtoVersion: ls.ProtoVersion, |
| 54 TerminalIndex: ls.TerminalIndex, | 56 TerminalIndex: ls.TerminalIndex, |
| 55 Archived: ls.Archived(), | 57 Archived: ls.Archived(), |
| 56 Purged: ls.Purged, | 58 Purged: ls.Purged, |
| 57 } | 59 } |
| 58 if !ls.Terminated() { | 60 if !ls.Terminated() { |
| 59 st.TerminalIndex = -1 | 61 st.TerminalIndex = -1 |
| 60 } | 62 } |
| 61 return &st | 63 return &st |
| 62 } | 64 } |
| 63 | 65 |
| 64 // RegisterStream is an idempotent stream state register operation. | 66 // RegisterStream is an idempotent stream state register operation. |
| 65 func (s *server) RegisterStream(c context.Context, req *logdog.RegisterStreamReq
uest) (*logdog.RegisterStreamResponse, error) { | 67 func (s *server) RegisterStream(c context.Context, req *logdog.RegisterStreamReq
uest) (*logdog.RegisterStreamResponse, error) { |
| 66 log.Fields{ | 68 log.Fields{ |
| 67 » » "path": req.Path, | 69 » » "project": req.Project, |
| 70 » » "path": req.Path, |
| 68 }.Infof(c, "Registration request for log stream.") | 71 }.Infof(c, "Registration request for log stream.") |
| 69 | 72 |
| 70 path := types.StreamPath(req.Path) | 73 path := types.StreamPath(req.Path) |
| 71 if err := path.Validate(); err != nil { | 74 if err := path.Validate(); err != nil { |
| 72 return nil, grpcutil.Errf(codes.InvalidArgument, "Invalid path (
%s): %s", path, err) | 75 return nil, grpcutil.Errf(codes.InvalidArgument, "Invalid path (
%s): %s", path, err) |
| 73 } | 76 } |
| 74 | 77 |
| 75 switch { | 78 switch { |
| 76 case req.ProtoVersion == "": | 79 case req.ProtoVersion == "": |
| 77 return nil, grpcutil.Errf(codes.InvalidArgument, "No protobuf ve
rsion supplied.") | 80 return nil, grpcutil.Errf(codes.InvalidArgument, "No protobuf ve
rsion supplied.") |
| 78 case req.ProtoVersion != logpb.Version: | 81 case req.ProtoVersion != logpb.Version: |
| 79 return nil, grpcutil.Errf(codes.InvalidArgument, "Unrecognized p
rotobuf version.") | 82 return nil, grpcutil.Errf(codes.InvalidArgument, "Unrecognized p
rotobuf version.") |
| 80 case len(req.Secret) != types.StreamSecretLength: | |
| 81 return nil, grpcutil.Errf(codes.InvalidArgument, "Invalid secret
length (%d != %d)", | |
| 82 len(req.Secret), types.StreamSecretLength) | |
| 83 case req.Desc == nil: | 83 case req.Desc == nil: |
| 84 return nil, grpcutil.Errf(codes.InvalidArgument, "Missing log st
ream descriptor.") | 84 return nil, grpcutil.Errf(codes.InvalidArgument, "Missing log st
ream descriptor.") |
| 85 } | 85 } |
| 86 | 86 |
| 87 secret := types.PrefixSecret(req.Secret) |
| 88 if err := secret.Validate(); err != nil { |
| 89 return nil, grpcutil.Errf(codes.InvalidArgument, "Invalid prefix
secret: %s", err) |
| 90 } |
| 91 |
| 87 prefix, name := path.Split() | 92 prefix, name := path.Split() |
| 88 if err := req.Desc.Validate(true); err != nil { | 93 if err := req.Desc.Validate(true); err != nil { |
| 89 return nil, grpcutil.Errf(codes.InvalidArgument, "Invalid log st
ream descriptor: %s", err) | 94 return nil, grpcutil.Errf(codes.InvalidArgument, "Invalid log st
ream descriptor: %s", err) |
| 90 } | 95 } |
| 91 switch { | 96 switch { |
| 92 case req.Desc.Prefix != string(prefix): | 97 case req.Desc.Prefix != string(prefix): |
| 93 return nil, grpcutil.Errf(codes.InvalidArgument, "Descriptor pre
fix does not match path (%s != %s)", | 98 return nil, grpcutil.Errf(codes.InvalidArgument, "Descriptor pre
fix does not match path (%s != %s)", |
| 94 req.Desc.Prefix, prefix) | 99 req.Desc.Prefix, prefix) |
| 95 case req.Desc.Name != string(name): | 100 case req.Desc.Name != string(name): |
| 96 return nil, grpcutil.Errf(codes.InvalidArgument, "Descriptor nam
e does not match path (%s != %s)", | 101 return nil, grpcutil.Errf(codes.InvalidArgument, "Descriptor nam
e does not match path (%s != %s)", |
| (...skipping 18 matching lines...) Expand all Loading... |
| 115 }.Errorf(c, "Failed to register LogStream.") | 120 }.Errorf(c, "Failed to register LogStream.") |
| 116 return nil, filterError(err) | 121 return nil, filterError(err) |
| 117 } | 122 } |
| 118 | 123 |
| 119 default: | 124 default: |
| 120 log.WithError(err).Errorf(c, "Failed to check for log stream.") | 125 log.WithError(err).Errorf(c, "Failed to check for log stream.") |
| 121 return nil, grpcutil.Internal | 126 return nil, grpcutil.Internal |
| 122 } | 127 } |
| 123 | 128 |
| 124 return &logdog.RegisterStreamResponse{ | 129 return &logdog.RegisterStreamResponse{ |
| 125 » » State: loadLogStreamState(ls), | 130 » » State: loadLogStreamState(coordinator.Project(c), ls), |
| 126 Secret: ls.Secret, | 131 Secret: ls.Secret, |
| 127 }, nil | 132 }, nil |
| 128 } | 133 } |
| 129 | 134 |
| 130 func filterError(err error) error { | 135 func filterError(err error) error { |
| 131 switch { | 136 switch { |
| 132 case err == nil: | 137 case err == nil: |
| 133 return nil | 138 return nil |
| 134 case grpc.Code(err) == codes.Unknown: | 139 case grpc.Code(err) == codes.Unknown: |
| 135 return grpcutil.Internal | 140 return grpcutil.Internal |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 return []tumble.Mutation{ | 197 return []tumble.Mutation{ |
| 193 &mutations.PutHierarchyMutation{ | 198 &mutations.PutHierarchyMutation{ |
| 194 Path: m.Path(), | 199 Path: m.Path(), |
| 195 }, | 200 }, |
| 196 }, nil | 201 }, nil |
| 197 } | 202 } |
| 198 | 203 |
| 199 func (m registerStreamMutation) Root(c context.Context) *ds.Key { | 204 func (m registerStreamMutation) Root(c context.Context) *ds.Key { |
| 200 return ds.Get(c).KeyForObj(m.LogStream) | 205 return ds.Get(c).KeyForObj(m.LogStream) |
| 201 } | 206 } |
| OLD | NEW |