コード例 #1
0
ファイル: broker.go プロジェクト: msabansal/docker
// PublishLogs publishes log messages for a given subscription
func (lb *LogBroker) PublishLogs(stream api.LogBroker_PublishLogsServer) error {
	remote, err := ca.RemoteNode(stream.Context())
	if err != nil {
		return err
	}

	for {
		log, err := stream.Recv()
		if err == io.EOF {
			return stream.SendAndClose(&api.PublishLogsResponse{})
		}
		if err != nil {
			return err
		}

		if log.SubscriptionID == "" {
			return grpc.Errorf(codes.InvalidArgument, "missing subscription ID")
		}

		// Make sure logs are emitted using the right Node ID to avoid impersonation.
		for _, msg := range log.Messages {
			if msg.Context.NodeID != remote.NodeID {
				return grpc.Errorf(codes.PermissionDenied, "invalid NodeID: expected=%s;received=%s", remote.NodeID, msg.Context.NodeID)
			}
		}

		lb.publish(log)
	}
}
コード例 #2
0
ファイル: broker.go プロジェクト: haoshuwei/docker
// PublishLogs publishes log messages for a given subscription
func (lb *LogBroker) PublishLogs(stream api.LogBroker_PublishLogsServer) (err error) {
	remote, err := ca.RemoteNode(stream.Context())
	if err != nil {
		return err
	}

	var currentSubscription *subscription
	defer func() {
		if currentSubscription != nil {
			currentSubscription.Done(remote.NodeID, err)
		}
	}()

	for {
		log, err := stream.Recv()
		if err == io.EOF {
			return stream.SendAndClose(&api.PublishLogsResponse{})
		}
		if err != nil {
			return err
		}

		if log.SubscriptionID == "" {
			return grpc.Errorf(codes.InvalidArgument, "missing subscription ID")
		}

		if currentSubscription == nil {
			currentSubscription = lb.getSubscription(log.SubscriptionID)
			if currentSubscription == nil {
				return grpc.Errorf(codes.NotFound, "unknown subscription ID")
			}
		} else {
			if log.SubscriptionID != currentSubscription.message.ID {
				return grpc.Errorf(codes.InvalidArgument, "different subscription IDs in the same session")
			}
		}

		// Make sure logs are emitted using the right Node ID to avoid impersonation.
		for _, msg := range log.Messages {
			if msg.Context.NodeID != remote.NodeID {
				return grpc.Errorf(codes.PermissionDenied, "invalid NodeID: expected=%s;received=%s", remote.NodeID, msg.Context.NodeID)
			}
		}

		lb.publish(log)
	}
}