Node selection 0/4: Update statdb auth/functionality (#698)

* remove api key from statdb server reqs; add statdb UpdateUptime and UpdateAuditSuccess to server

* update api key authentication in statdb server

* add todos for future statdb updates

* add UpdateUptime and UpdateAuditSuccess to statdb server

* fix apikey stuff in config.go and statdb_test.go

* fix tests

* update sdbclient.NewClient call in audit package

* fix UpdateUptime and UpdateAuditSuccess in sdbclient

* set api key from statdb/config.go

* change package for statdb tests

* linter fixes

* remove todo comments

* fix sdbclient err checking

* move validate auth functionality to auth package

* update description for statdb api key

* remove import
This commit is contained in:
Maximillian von Briesen 2018-11-26 12:08:29 -05:00 committed by GitHub
parent 0cd58ec770
commit 5014a785a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 576 additions and 224 deletions

View File

@ -20,7 +20,7 @@ import (
var (
port string
apiKey = []byte("")
apiKey = ""
ctx = context.Background()
)

View File

@ -32,7 +32,7 @@ func NewReporter(ctx context.Context, statDBPort string, maxRetries int, apiKey
return nil, err
}
client, err := sdbclient.NewClient(identity, statDBPort, []byte(apiKey))
client, err := sdbclient.NewClient(identity, statDBPort, apiKey)
if err != nil {
return nil, err
}

View File

@ -3,7 +3,10 @@
package auth
import "context"
import (
"context"
"crypto/subtle"
)
// The key type is unexported to prevent collisions with context keys defined in
// other packages.
@ -22,3 +25,17 @@ func GetAPIKey(ctx context.Context) ([]byte, bool) {
key, ok := ctx.Value(apiKey).([]byte)
return key, ok
}
// ValidateAPIKey compares the context api key with the key passed in as an argument
func ValidateAPIKey(ctx context.Context, actualKey []byte) error {
expectedKey, ok := GetAPIKey(ctx)
if !ok {
return Error.New("Could not get api key from context")
}
matches := (1 == subtle.ConstantTimeCompare(actualKey, expectedKey))
if !matches {
return Error.New("Invalid API credential")
}
return nil
}

View File

@ -5,6 +5,7 @@ package statdb
import (
"context"
"flag"
"go.uber.org/zap"
@ -19,6 +20,10 @@ const (
ctxKeyStats CtxKey = iota
)
var (
apiKey = flag.String("stat-db.auth.api-key", "", "statdb api key")
)
// Config is a configuration struct that is everything you need to start a
// StatDB responsibility
type Config struct {
@ -28,7 +33,7 @@ type Config struct {
// Run implements the provider.Responsibility interface
func (c Config) Run(ctx context.Context, server *provider.Provider) error {
ns, err := NewServer(c.DatabaseDriver, c.DatabaseURL, zap.L())
ns, err := NewServer(c.DatabaseDriver, c.DatabaseURL, *apiKey, zap.L())
if err != nil {
return err
}

View File

@ -41,7 +41,7 @@ func (m *Node) Reset() { *m = Node{} }
func (m *Node) String() string { return proto.CompactTextString(m) }
func (*Node) ProtoMessage() {}
func (*Node) Descriptor() ([]byte, []int) {
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{0}
return fileDescriptor_statdb_823b3386d7576b72, []int{0}
}
func (m *Node) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_Node.Unmarshal(m, b)
@ -129,7 +129,7 @@ func (m *NodeStats) Reset() { *m = NodeStats{} }
func (m *NodeStats) String() string { return proto.CompactTextString(m) }
func (*NodeStats) ProtoMessage() {}
func (*NodeStats) Descriptor() ([]byte, []int) {
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{1}
return fileDescriptor_statdb_823b3386d7576b72, []int{1}
}
func (m *NodeStats) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_NodeStats.Unmarshal(m, b)
@ -209,7 +209,6 @@ func (m *NodeStats) GetUptimeSuccessCount() int64 {
type CreateRequest struct {
Node *Node `protobuf:"bytes,1,opt,name=node,proto3" json:"node,omitempty"`
Stats *NodeStats `protobuf:"bytes,2,opt,name=stats,proto3" json:"stats,omitempty"`
APIKey []byte `protobuf:"bytes,3,opt,name=APIKey,proto3" json:"APIKey,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
@ -219,7 +218,7 @@ func (m *CreateRequest) Reset() { *m = CreateRequest{} }
func (m *CreateRequest) String() string { return proto.CompactTextString(m) }
func (*CreateRequest) ProtoMessage() {}
func (*CreateRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{2}
return fileDescriptor_statdb_823b3386d7576b72, []int{2}
}
func (m *CreateRequest) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_CreateRequest.Unmarshal(m, b)
@ -253,13 +252,6 @@ func (m *CreateRequest) GetStats() *NodeStats {
return nil
}
func (m *CreateRequest) GetAPIKey() []byte {
if m != nil {
return m.APIKey
}
return nil
}
// CreateResponse is a response message for the Create rpc call
type CreateResponse struct {
Stats *NodeStats `protobuf:"bytes,1,opt,name=stats,proto3" json:"stats,omitempty"`
@ -272,7 +264,7 @@ func (m *CreateResponse) Reset() { *m = CreateResponse{} }
func (m *CreateResponse) String() string { return proto.CompactTextString(m) }
func (*CreateResponse) ProtoMessage() {}
func (*CreateResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{3}
return fileDescriptor_statdb_823b3386d7576b72, []int{3}
}
func (m *CreateResponse) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_CreateResponse.Unmarshal(m, b)
@ -302,7 +294,6 @@ func (m *CreateResponse) GetStats() *NodeStats {
// GetRequest is a request message for the Get rpc call
type GetRequest struct {
NodeId []byte `protobuf:"bytes,1,opt,name=node_id,json=nodeId,proto3" json:"node_id,omitempty"`
APIKey []byte `protobuf:"bytes,2,opt,name=APIKey,proto3" json:"APIKey,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
@ -312,7 +303,7 @@ func (m *GetRequest) Reset() { *m = GetRequest{} }
func (m *GetRequest) String() string { return proto.CompactTextString(m) }
func (*GetRequest) ProtoMessage() {}
func (*GetRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{4}
return fileDescriptor_statdb_823b3386d7576b72, []int{4}
}
func (m *GetRequest) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_GetRequest.Unmarshal(m, b)
@ -339,13 +330,6 @@ func (m *GetRequest) GetNodeId() []byte {
return nil
}
func (m *GetRequest) GetAPIKey() []byte {
if m != nil {
return m.APIKey
}
return nil
}
// GetResponse is a response message for the Get rpc call
type GetResponse struct {
Stats *NodeStats `protobuf:"bytes,1,opt,name=stats,proto3" json:"stats,omitempty"`
@ -358,7 +342,7 @@ func (m *GetResponse) Reset() { *m = GetResponse{} }
func (m *GetResponse) String() string { return proto.CompactTextString(m) }
func (*GetResponse) ProtoMessage() {}
func (*GetResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{5}
return fileDescriptor_statdb_823b3386d7576b72, []int{5}
}
func (m *GetResponse) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_GetResponse.Unmarshal(m, b)
@ -389,7 +373,6 @@ func (m *GetResponse) GetStats() *NodeStats {
type FindValidNodesRequest struct {
NodeIds [][]byte `protobuf:"bytes,1,rep,name=node_ids,json=nodeIds,proto3" json:"node_ids,omitempty"`
MinStats *NodeStats `protobuf:"bytes,2,opt,name=min_stats,json=minStats,proto3" json:"min_stats,omitempty"`
APIKey []byte `protobuf:"bytes,3,opt,name=APIKey,proto3" json:"APIKey,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
@ -399,7 +382,7 @@ func (m *FindValidNodesRequest) Reset() { *m = FindValidNodesRequest{} }
func (m *FindValidNodesRequest) String() string { return proto.CompactTextString(m) }
func (*FindValidNodesRequest) ProtoMessage() {}
func (*FindValidNodesRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{6}
return fileDescriptor_statdb_823b3386d7576b72, []int{6}
}
func (m *FindValidNodesRequest) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_FindValidNodesRequest.Unmarshal(m, b)
@ -433,13 +416,6 @@ func (m *FindValidNodesRequest) GetMinStats() *NodeStats {
return nil
}
func (m *FindValidNodesRequest) GetAPIKey() []byte {
if m != nil {
return m.APIKey
}
return nil
}
// FindValidNodesResponse is a response message for the FindValidNodes rpc call
type FindValidNodesResponse struct {
PassedIds [][]byte `protobuf:"bytes,1,rep,name=passed_ids,json=passedIds,proto3" json:"passed_ids,omitempty"`
@ -453,7 +429,7 @@ func (m *FindValidNodesResponse) Reset() { *m = FindValidNodesResponse{}
func (m *FindValidNodesResponse) String() string { return proto.CompactTextString(m) }
func (*FindValidNodesResponse) ProtoMessage() {}
func (*FindValidNodesResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{7}
return fileDescriptor_statdb_823b3386d7576b72, []int{7}
}
func (m *FindValidNodesResponse) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_FindValidNodesResponse.Unmarshal(m, b)
@ -490,7 +466,6 @@ func (m *FindValidNodesResponse) GetFailedIds() [][]byte {
// UpdateRequest is a request message for the Update rpc call
type UpdateRequest struct {
Node *Node `protobuf:"bytes,1,opt,name=node,proto3" json:"node,omitempty"`
APIKey []byte `protobuf:"bytes,2,opt,name=APIKey,proto3" json:"APIKey,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
@ -500,7 +475,7 @@ func (m *UpdateRequest) Reset() { *m = UpdateRequest{} }
func (m *UpdateRequest) String() string { return proto.CompactTextString(m) }
func (*UpdateRequest) ProtoMessage() {}
func (*UpdateRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{8}
return fileDescriptor_statdb_823b3386d7576b72, []int{8}
}
func (m *UpdateRequest) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_UpdateRequest.Unmarshal(m, b)
@ -527,13 +502,6 @@ func (m *UpdateRequest) GetNode() *Node {
return nil
}
func (m *UpdateRequest) GetAPIKey() []byte {
if m != nil {
return m.APIKey
}
return nil
}
// UpdateRequest is a response message for the Update rpc call
type UpdateResponse struct {
Stats *NodeStats `protobuf:"bytes,1,opt,name=stats,proto3" json:"stats,omitempty"`
@ -546,7 +514,7 @@ func (m *UpdateResponse) Reset() { *m = UpdateResponse{} }
func (m *UpdateResponse) String() string { return proto.CompactTextString(m) }
func (*UpdateResponse) ProtoMessage() {}
func (*UpdateResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{9}
return fileDescriptor_statdb_823b3386d7576b72, []int{9}
}
func (m *UpdateResponse) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_UpdateResponse.Unmarshal(m, b)
@ -573,10 +541,165 @@ func (m *UpdateResponse) GetStats() *NodeStats {
return nil
}
// UpdateUptimeRequest is a request message for the UpdateUptime rpc call
type UpdateUptimeRequest struct {
Node *Node `protobuf:"bytes,1,opt,name=node,proto3" json:"node,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *UpdateUptimeRequest) Reset() { *m = UpdateUptimeRequest{} }
func (m *UpdateUptimeRequest) String() string { return proto.CompactTextString(m) }
func (*UpdateUptimeRequest) ProtoMessage() {}
func (*UpdateUptimeRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_statdb_823b3386d7576b72, []int{10}
}
func (m *UpdateUptimeRequest) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_UpdateUptimeRequest.Unmarshal(m, b)
}
func (m *UpdateUptimeRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_UpdateUptimeRequest.Marshal(b, m, deterministic)
}
func (dst *UpdateUptimeRequest) XXX_Merge(src proto.Message) {
xxx_messageInfo_UpdateUptimeRequest.Merge(dst, src)
}
func (m *UpdateUptimeRequest) XXX_Size() int {
return xxx_messageInfo_UpdateUptimeRequest.Size(m)
}
func (m *UpdateUptimeRequest) XXX_DiscardUnknown() {
xxx_messageInfo_UpdateUptimeRequest.DiscardUnknown(m)
}
var xxx_messageInfo_UpdateUptimeRequest proto.InternalMessageInfo
func (m *UpdateUptimeRequest) GetNode() *Node {
if m != nil {
return m.Node
}
return nil
}
// UpdateUptimeResponse is a response message for the UpdateUptime rpc call
type UpdateUptimeResponse struct {
Stats *NodeStats `protobuf:"bytes,1,opt,name=stats,proto3" json:"stats,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *UpdateUptimeResponse) Reset() { *m = UpdateUptimeResponse{} }
func (m *UpdateUptimeResponse) String() string { return proto.CompactTextString(m) }
func (*UpdateUptimeResponse) ProtoMessage() {}
func (*UpdateUptimeResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_statdb_823b3386d7576b72, []int{11}
}
func (m *UpdateUptimeResponse) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_UpdateUptimeResponse.Unmarshal(m, b)
}
func (m *UpdateUptimeResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_UpdateUptimeResponse.Marshal(b, m, deterministic)
}
func (dst *UpdateUptimeResponse) XXX_Merge(src proto.Message) {
xxx_messageInfo_UpdateUptimeResponse.Merge(dst, src)
}
func (m *UpdateUptimeResponse) XXX_Size() int {
return xxx_messageInfo_UpdateUptimeResponse.Size(m)
}
func (m *UpdateUptimeResponse) XXX_DiscardUnknown() {
xxx_messageInfo_UpdateUptimeResponse.DiscardUnknown(m)
}
var xxx_messageInfo_UpdateUptimeResponse proto.InternalMessageInfo
func (m *UpdateUptimeResponse) GetStats() *NodeStats {
if m != nil {
return m.Stats
}
return nil
}
// UpdateAuditSuccessRequest is a request message for the UpdateAuditSuccess rpc call
type UpdateAuditSuccessRequest struct {
Node *Node `protobuf:"bytes,1,opt,name=node,proto3" json:"node,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *UpdateAuditSuccessRequest) Reset() { *m = UpdateAuditSuccessRequest{} }
func (m *UpdateAuditSuccessRequest) String() string { return proto.CompactTextString(m) }
func (*UpdateAuditSuccessRequest) ProtoMessage() {}
func (*UpdateAuditSuccessRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_statdb_823b3386d7576b72, []int{12}
}
func (m *UpdateAuditSuccessRequest) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_UpdateAuditSuccessRequest.Unmarshal(m, b)
}
func (m *UpdateAuditSuccessRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_UpdateAuditSuccessRequest.Marshal(b, m, deterministic)
}
func (dst *UpdateAuditSuccessRequest) XXX_Merge(src proto.Message) {
xxx_messageInfo_UpdateAuditSuccessRequest.Merge(dst, src)
}
func (m *UpdateAuditSuccessRequest) XXX_Size() int {
return xxx_messageInfo_UpdateAuditSuccessRequest.Size(m)
}
func (m *UpdateAuditSuccessRequest) XXX_DiscardUnknown() {
xxx_messageInfo_UpdateAuditSuccessRequest.DiscardUnknown(m)
}
var xxx_messageInfo_UpdateAuditSuccessRequest proto.InternalMessageInfo
func (m *UpdateAuditSuccessRequest) GetNode() *Node {
if m != nil {
return m.Node
}
return nil
}
// UpdateAuditSuccessResponse is a response message for the UpdateAuditSuccess rpc call
type UpdateAuditSuccessResponse struct {
Stats *NodeStats `protobuf:"bytes,1,opt,name=stats,proto3" json:"stats,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *UpdateAuditSuccessResponse) Reset() { *m = UpdateAuditSuccessResponse{} }
func (m *UpdateAuditSuccessResponse) String() string { return proto.CompactTextString(m) }
func (*UpdateAuditSuccessResponse) ProtoMessage() {}
func (*UpdateAuditSuccessResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_statdb_823b3386d7576b72, []int{13}
}
func (m *UpdateAuditSuccessResponse) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_UpdateAuditSuccessResponse.Unmarshal(m, b)
}
func (m *UpdateAuditSuccessResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_UpdateAuditSuccessResponse.Marshal(b, m, deterministic)
}
func (dst *UpdateAuditSuccessResponse) XXX_Merge(src proto.Message) {
xxx_messageInfo_UpdateAuditSuccessResponse.Merge(dst, src)
}
func (m *UpdateAuditSuccessResponse) XXX_Size() int {
return xxx_messageInfo_UpdateAuditSuccessResponse.Size(m)
}
func (m *UpdateAuditSuccessResponse) XXX_DiscardUnknown() {
xxx_messageInfo_UpdateAuditSuccessResponse.DiscardUnknown(m)
}
var xxx_messageInfo_UpdateAuditSuccessResponse proto.InternalMessageInfo
func (m *UpdateAuditSuccessResponse) GetStats() *NodeStats {
if m != nil {
return m.Stats
}
return nil
}
// UpdateBatchRequest is a request message for the UpdateBatch rpc call
type UpdateBatchRequest struct {
NodeList []*Node `protobuf:"bytes,1,rep,name=node_list,json=nodeList,proto3" json:"node_list,omitempty"`
APIKey []byte `protobuf:"bytes,2,opt,name=APIKey,proto3" json:"APIKey,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
@ -586,7 +709,7 @@ func (m *UpdateBatchRequest) Reset() { *m = UpdateBatchRequest{} }
func (m *UpdateBatchRequest) String() string { return proto.CompactTextString(m) }
func (*UpdateBatchRequest) ProtoMessage() {}
func (*UpdateBatchRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{10}
return fileDescriptor_statdb_823b3386d7576b72, []int{14}
}
func (m *UpdateBatchRequest) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_UpdateBatchRequest.Unmarshal(m, b)
@ -613,13 +736,6 @@ func (m *UpdateBatchRequest) GetNodeList() []*Node {
return nil
}
func (m *UpdateBatchRequest) GetAPIKey() []byte {
if m != nil {
return m.APIKey
}
return nil
}
// UpdateBatchResponse is a response message for the UpdateBatch rpc call
type UpdateBatchResponse struct {
StatsList []*NodeStats `protobuf:"bytes,1,rep,name=stats_list,json=statsList,proto3" json:"stats_list,omitempty"`
@ -633,7 +749,7 @@ func (m *UpdateBatchResponse) Reset() { *m = UpdateBatchResponse{} }
func (m *UpdateBatchResponse) String() string { return proto.CompactTextString(m) }
func (*UpdateBatchResponse) ProtoMessage() {}
func (*UpdateBatchResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{11}
return fileDescriptor_statdb_823b3386d7576b72, []int{15}
}
func (m *UpdateBatchResponse) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_UpdateBatchResponse.Unmarshal(m, b)
@ -670,7 +786,6 @@ func (m *UpdateBatchResponse) GetFailedNodes() []*Node {
// CreateEntryIfNotExistsRequest is a request message for the CreateEntryIfNotExists rpc call
type CreateEntryIfNotExistsRequest struct {
Node *Node `protobuf:"bytes,1,opt,name=node,proto3" json:"node,omitempty"`
APIKey []byte `protobuf:"bytes,2,opt,name=APIKey,proto3" json:"APIKey,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
@ -680,7 +795,7 @@ func (m *CreateEntryIfNotExistsRequest) Reset() { *m = CreateEntryIfNotE
func (m *CreateEntryIfNotExistsRequest) String() string { return proto.CompactTextString(m) }
func (*CreateEntryIfNotExistsRequest) ProtoMessage() {}
func (*CreateEntryIfNotExistsRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{12}
return fileDescriptor_statdb_823b3386d7576b72, []int{16}
}
func (m *CreateEntryIfNotExistsRequest) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_CreateEntryIfNotExistsRequest.Unmarshal(m, b)
@ -707,13 +822,6 @@ func (m *CreateEntryIfNotExistsRequest) GetNode() *Node {
return nil
}
func (m *CreateEntryIfNotExistsRequest) GetAPIKey() []byte {
if m != nil {
return m.APIKey
}
return nil
}
// CreateEntryIfNotExistsResponse is a response message for the CreateEntryIfNotExists rpc call
type CreateEntryIfNotExistsResponse struct {
Stats *NodeStats `protobuf:"bytes,1,opt,name=stats,proto3" json:"stats,omitempty"`
@ -726,7 +834,7 @@ func (m *CreateEntryIfNotExistsResponse) Reset() { *m = CreateEntryIfNot
func (m *CreateEntryIfNotExistsResponse) String() string { return proto.CompactTextString(m) }
func (*CreateEntryIfNotExistsResponse) ProtoMessage() {}
func (*CreateEntryIfNotExistsResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{13}
return fileDescriptor_statdb_823b3386d7576b72, []int{17}
}
func (m *CreateEntryIfNotExistsResponse) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_CreateEntryIfNotExistsResponse.Unmarshal(m, b)
@ -764,6 +872,10 @@ func init() {
proto.RegisterType((*FindValidNodesResponse)(nil), "statdb.FindValidNodesResponse")
proto.RegisterType((*UpdateRequest)(nil), "statdb.UpdateRequest")
proto.RegisterType((*UpdateResponse)(nil), "statdb.UpdateResponse")
proto.RegisterType((*UpdateUptimeRequest)(nil), "statdb.UpdateUptimeRequest")
proto.RegisterType((*UpdateUptimeResponse)(nil), "statdb.UpdateUptimeResponse")
proto.RegisterType((*UpdateAuditSuccessRequest)(nil), "statdb.UpdateAuditSuccessRequest")
proto.RegisterType((*UpdateAuditSuccessResponse)(nil), "statdb.UpdateAuditSuccessResponse")
proto.RegisterType((*UpdateBatchRequest)(nil), "statdb.UpdateBatchRequest")
proto.RegisterType((*UpdateBatchResponse)(nil), "statdb.UpdateBatchResponse")
proto.RegisterType((*CreateEntryIfNotExistsRequest)(nil), "statdb.CreateEntryIfNotExistsRequest")
@ -788,8 +900,12 @@ type StatDBClient interface {
Get(ctx context.Context, in *GetRequest, opts ...grpc.CallOption) (*GetResponse, error)
// FindValidNodes gets a subset of storagenodes that fit minimum reputation args
FindValidNodes(ctx context.Context, in *FindValidNodesRequest, opts ...grpc.CallOption) (*FindValidNodesResponse, error)
// Update updates storagenode stats for a single storagenode
// Update updates all stats for a single storagenode
Update(ctx context.Context, in *UpdateRequest, opts ...grpc.CallOption) (*UpdateResponse, error)
// UpdateUptime updates uptime stats for a single storagenode
UpdateUptime(ctx context.Context, in *UpdateUptimeRequest, opts ...grpc.CallOption) (*UpdateUptimeResponse, error)
// UpdateAuditSuccess updates audit success stats for a single storagenode
UpdateAuditSuccess(ctx context.Context, in *UpdateAuditSuccessRequest, opts ...grpc.CallOption) (*UpdateAuditSuccessResponse, error)
// UpdateBatch updates storagenode stats for multiple farmers at a time
UpdateBatch(ctx context.Context, in *UpdateBatchRequest, opts ...grpc.CallOption) (*UpdateBatchResponse, error)
// CreateEntryIfNotExists creates a db entry if it didn't exist
@ -840,6 +956,24 @@ func (c *statDBClient) Update(ctx context.Context, in *UpdateRequest, opts ...gr
return out, nil
}
func (c *statDBClient) UpdateUptime(ctx context.Context, in *UpdateUptimeRequest, opts ...grpc.CallOption) (*UpdateUptimeResponse, error) {
out := new(UpdateUptimeResponse)
err := c.cc.Invoke(ctx, "/statdb.StatDB/UpdateUptime", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *statDBClient) UpdateAuditSuccess(ctx context.Context, in *UpdateAuditSuccessRequest, opts ...grpc.CallOption) (*UpdateAuditSuccessResponse, error) {
out := new(UpdateAuditSuccessResponse)
err := c.cc.Invoke(ctx, "/statdb.StatDB/UpdateAuditSuccess", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *statDBClient) UpdateBatch(ctx context.Context, in *UpdateBatchRequest, opts ...grpc.CallOption) (*UpdateBatchResponse, error) {
out := new(UpdateBatchResponse)
err := c.cc.Invoke(ctx, "/statdb.StatDB/UpdateBatch", in, out, opts...)
@ -866,8 +1000,12 @@ type StatDBServer interface {
Get(context.Context, *GetRequest) (*GetResponse, error)
// FindValidNodes gets a subset of storagenodes that fit minimum reputation args
FindValidNodes(context.Context, *FindValidNodesRequest) (*FindValidNodesResponse, error)
// Update updates storagenode stats for a single storagenode
// Update updates all stats for a single storagenode
Update(context.Context, *UpdateRequest) (*UpdateResponse, error)
// UpdateUptime updates uptime stats for a single storagenode
UpdateUptime(context.Context, *UpdateUptimeRequest) (*UpdateUptimeResponse, error)
// UpdateAuditSuccess updates audit success stats for a single storagenode
UpdateAuditSuccess(context.Context, *UpdateAuditSuccessRequest) (*UpdateAuditSuccessResponse, error)
// UpdateBatch updates storagenode stats for multiple farmers at a time
UpdateBatch(context.Context, *UpdateBatchRequest) (*UpdateBatchResponse, error)
// CreateEntryIfNotExists creates a db entry if it didn't exist
@ -950,6 +1088,42 @@ func _StatDB_Update_Handler(srv interface{}, ctx context.Context, dec func(inter
return interceptor(ctx, in, info, handler)
}
func _StatDB_UpdateUptime_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(UpdateUptimeRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(StatDBServer).UpdateUptime(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/statdb.StatDB/UpdateUptime",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(StatDBServer).UpdateUptime(ctx, req.(*UpdateUptimeRequest))
}
return interceptor(ctx, in, info, handler)
}
func _StatDB_UpdateAuditSuccess_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(UpdateAuditSuccessRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(StatDBServer).UpdateAuditSuccess(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/statdb.StatDB/UpdateAuditSuccess",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(StatDBServer).UpdateAuditSuccess(ctx, req.(*UpdateAuditSuccessRequest))
}
return interceptor(ctx, in, info, handler)
}
func _StatDB_UpdateBatch_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(UpdateBatchRequest)
if err := dec(in); err != nil {
@ -1006,6 +1180,14 @@ var _StatDB_serviceDesc = grpc.ServiceDesc{
MethodName: "Update",
Handler: _StatDB_Update_Handler,
},
{
MethodName: "UpdateUptime",
Handler: _StatDB_UpdateUptime_Handler,
},
{
MethodName: "UpdateAuditSuccess",
Handler: _StatDB_UpdateAuditSuccess_Handler,
},
{
MethodName: "UpdateBatch",
Handler: _StatDB_UpdateBatch_Handler,
@ -1019,53 +1201,56 @@ var _StatDB_serviceDesc = grpc.ServiceDesc{
Metadata: "statdb.proto",
}
func init() { proto.RegisterFile("statdb.proto", fileDescriptor_statdb_b838e72ff4f67ff3) }
func init() { proto.RegisterFile("statdb.proto", fileDescriptor_statdb_823b3386d7576b72) }
var fileDescriptor_statdb_b838e72ff4f67ff3 = []byte{
// 714 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x55, 0xd1, 0x6e, 0x12, 0x41,
0x14, 0x0d, 0x2c, 0x5d, 0xe0, 0xee, 0x42, 0xd2, 0xc1, 0x22, 0x62, 0xa8, 0x48, 0x53, 0xc5, 0x17,
0x24, 0x35, 0xb1, 0xe1, 0xc1, 0x87, 0xb6, 0xb6, 0x0d, 0xb1, 0xa9, 0x66, 0x9b, 0xd6, 0xf8, 0xb4,
0xd9, 0xb2, 0x53, 0x9d, 0x84, 0xee, 0x22, 0x33, 0x24, 0xad, 0xdf, 0xe4, 0x27, 0xf8, 0x65, 0x3e,
0x99, 0xb9, 0x77, 0x36, 0xec, 0x22, 0xb4, 0x36, 0x3e, 0xee, 0xb9, 0x87, 0x73, 0xce, 0x9e, 0xb9,
0x3b, 0x80, 0x2b, 0x55, 0xa0, 0xc2, 0xcb, 0xde, 0x64, 0x1a, 0xab, 0x98, 0xd9, 0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1, 0x5d, 0xb0, 0xe9, 0x90, 0xe7, 0xc9, 0x33,
0x3b, 0x3d, 0x4f, 0xbe, 0xb0, 0x9f, 0x47, 0xe0, 0xa4, 0xb6, 0x83, 0x35, 0xb3, 0xb4, 0xf4, 0x2e,
0x36, 0x9f, 0x2e, 0x9d, 0x19, 0x9d, 0xaf, 0x50, 0x5f, 0x7e, 0x20, 0x6c, 0x3b, 0xdb, 0xd9, 0x8a,
0x5d, 0x68, 0xbe, 0xb8, 0x8f, 0x46, 0x46, 0x97, 0x36, 0xfe, 0xe5, 0xbf, 0xf9, 0x13, 0x00, 0x00,
0xff, 0xff, 0x9c, 0x3e, 0x97, 0xa1, 0x02, 0x08, 0x00, 0x00,
var fileDescriptor_statdb_823b3386d7576b72 = []byte{
// 760 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x56, 0x5d, 0x4f, 0xdb, 0x4a,
0x10, 0x55, 0x3e, 0x70, 0x92, 0xb1, 0x83, 0xc4, 0x06, 0xb8, 0xc1, 0xdc, 0x70, 0x83, 0x11, 0xf7,
0xe6, 0xbe, 0xa4, 0x29, 0x95, 0x8a, 0x78, 0xa8, 0x10, 0x50, 0x40, 0x91, 0x10, 0x95, 0x8c, 0xc2,
0x43, 0xfb, 0x60, 0x99, 0xd8, 0xb4, 0x96, 0x82, 0x9d, 0x66, 0x37, 0x12, 0xfc, 0xb7, 0xfe, 0x8c,
0xfe, 0x9a, 0x3e, 0x55, 0x3b, 0xb3, 0x56, 0xbc, 0xc1, 0x29, 0xe4, 0x8d, 0x9c, 0x39, 0x7b, 0xce,
0xe4, 0xec, 0xcc, 0x12, 0xb0, 0xb8, 0xf0, 0x45, 0x70, 0xd7, 0x1d, 0x4f, 0x12, 0x91, 0x30, 0x83,
0x3e, 0x39, 0xbf, 0x0a, 0x50, 0xbe, 0x4e, 0x82, 0x90, 0xfd, 0x05, 0x95, 0x38, 0x09, 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 0xeb, 0x5c, 0xdb, 0xc5, 0x59,
0xe7, 0x73, 0xfb, 0xd6, 0x07, 0x2b, 0xbb, 0x0d, 0x6c, 0x5b, 0xe7, 0x69, 0xcb, 0x65, 0xff, 0x9d,
0x5f, 0x54, 0x52, 0x5f, 0xd2, 0x89, 0xd4, 0xfe, 0x57, 0xee, 0xea, 0x67, 0x72, 0x76, 0xc6, 0x76,
0xfe, 0x44, 0x51, 0xe2, 0x17, 0x60, 0x66, 0xa6, 0x95, 0xd9, 0xfa, 0x91, 0xec, 0x0e, 0xd8, 0xdb,
0xb9, 0x35, 0xa5, 0xf3, 0x15, 0x36, 0xf3, 0x07, 0x87, 0xed, 0xeb, 0x77, 0xbb, 0x60, 0x36, 0xed,
0x7f, 0x5f, 0xa2, 0x91, 0xd1, 0x9d, 0x81, 0x3f, 0x85, 0xde, 0xfd, 0x0e, 0x00, 0x00, 0xff, 0xff,
0x4b, 0x3a, 0x60, 0xaf, 0x1a, 0x09, 0x00, 0x00,
}

View File

@ -12,8 +12,12 @@ service StatDB {
rpc Get(GetRequest) returns (GetResponse);
// FindValidNodes gets a subset of storagenodes that fit minimum reputation args
rpc FindValidNodes(FindValidNodesRequest) returns (FindValidNodesResponse);
// Update updates storagenode stats for a single storagenode
// Update updates all stats for a single storagenode
rpc Update(UpdateRequest) returns (UpdateResponse);
// UpdateUptime updates uptime stats for a single storagenode
rpc UpdateUptime(UpdateUptimeRequest) returns (UpdateUptimeResponse);
// UpdateAuditSuccess updates audit success stats for a single storagenode
rpc UpdateAuditSuccess(UpdateAuditSuccessRequest) returns (UpdateAuditSuccessResponse);
// UpdateBatch updates storagenode stats for multiple farmers at a time
rpc UpdateBatch(UpdateBatchRequest) returns (UpdateBatchResponse);
// CreateEntryIfNotExists creates a db entry if it didn't exist
@ -47,7 +51,6 @@ message NodeStats {
message CreateRequest {
Node node = 1;
NodeStats stats = 2;
bytes APIKey = 3;
}
// CreateResponse is a response message for the Create rpc call
@ -58,7 +61,6 @@ message CreateResponse {
// GetRequest is a request message for the Get rpc call
message GetRequest {
bytes node_id = 1;
bytes APIKey = 2;
}
// GetResponse is a response message for the Get rpc call
@ -70,7 +72,6 @@ message GetResponse {
message FindValidNodesRequest {
repeated bytes node_ids = 1;
NodeStats min_stats = 2;
bytes APIKey = 3;
}
// FindValidNodesResponse is a response message for the FindValidNodes rpc call
@ -82,7 +83,6 @@ message FindValidNodesResponse {
// UpdateRequest is a request message for the Update rpc call
message UpdateRequest {
Node node = 1;
bytes APIKey = 2;
}
// UpdateRequest is a response message for the Update rpc call
@ -90,10 +90,29 @@ message UpdateResponse {
NodeStats stats = 1;
}
// UpdateUptimeRequest is a request message for the UpdateUptime rpc call
message UpdateUptimeRequest {
Node node = 1;
}
// UpdateUptimeResponse is a response message for the UpdateUptime rpc call
message UpdateUptimeResponse {
NodeStats stats = 1;
}
// UpdateAuditSuccessRequest is a request message for the UpdateAuditSuccess rpc call
message UpdateAuditSuccessRequest {
Node node = 1;
}
// UpdateAuditSuccessResponse is a response message for the UpdateAuditSuccess rpc call
message UpdateAuditSuccessResponse {
NodeStats stats = 1;
}
// UpdateBatchRequest is a request message for the UpdateBatch rpc call
message UpdateBatchRequest {
repeated Node node_list = 1;
bytes APIKey = 2;
}
// UpdateBatchResponse is a response message for the UpdateBatch rpc call
@ -105,7 +124,6 @@ message UpdateBatchResponse {
// CreateEntryIfNotExistsRequest is a request message for the CreateEntryIfNotExists rpc call
message CreateEntryIfNotExistsRequest {
Node node = 1;
bytes APIKey = 2;
}
// CreateEntryIfNotExistsResponse is a response message for the CreateEntryIfNotExists rpc call

View File

@ -6,8 +6,10 @@ package sdbclient
import (
"context"
"google.golang.org/grpc"
monkit "gopkg.in/spacemonkeygo/monkit.v2"
"storj.io/storj/pkg/auth/grpcauth"
"storj.io/storj/pkg/provider"
pb "storj.io/storj/pkg/statdb/proto"
"storj.io/storj/pkg/transport"
@ -38,17 +40,19 @@ type Client interface {
}
// NewClient initializes a new statdb client
func NewClient(identity *provider.FullIdentity, address string, APIKey []byte) (Client, error) {
func NewClient(identity *provider.FullIdentity, address string, APIKey string) (Client, error) {
apiKeyInjector := grpcauth.NewAPIKeyInjector(APIKey)
tc := transport.NewClient(identity)
conn, err := tc.DialAddress(context.Background(), address)
conn, err := tc.DialAddress(
context.Background(),
address,
grpc.WithUnaryInterceptor(apiKeyInjector),
)
if err != nil {
return nil, err
}
return &StatDB{
client: pb.NewStatDBClient(conn),
APIKey: APIKey,
}, nil
return &StatDB{client: pb.NewStatDBClient(conn)}, nil
}
// a compiler trick to make sure *StatDB implements Client
@ -62,8 +66,7 @@ func (sdb *StatDB) Create(ctx context.Context, nodeID []byte) (err error) {
NodeId: nodeID,
}
createReq := &pb.CreateRequest{
Node: &node,
APIKey: sdb.APIKey,
Node: &node,
}
_, err = sdb.client.Create(ctx, createReq)
@ -79,9 +82,8 @@ func (sdb *StatDB) CreateWithStats(ctx context.Context, nodeID []byte, stats *pb
NodeId: nodeID,
}
createReq := &pb.CreateRequest{
Node: node,
Stats: stats,
APIKey: sdb.APIKey,
Node: node,
Stats: stats,
}
_, err = sdb.client.Create(ctx, createReq)
@ -94,14 +96,13 @@ func (sdb *StatDB) Get(ctx context.Context, nodeID []byte) (stats *pb.NodeStats,
getReq := &pb.GetRequest{
NodeId: nodeID,
APIKey: sdb.APIKey,
}
res, err := sdb.client.Get(ctx, getReq)
if err != nil {
return nil, err
}
return res.Stats, err
return res.Stats, nil
}
// FindValidNodes is used for retrieving a subset of nodes that meet a minimum reputation requirement
@ -113,7 +114,6 @@ func (sdb *StatDB) FindValidNodes(ctx context.Context, nodeIDs [][]byte,
findValidNodesReq := &pb.FindValidNodesRequest{
NodeIds: nodeIDs,
MinStats: minStats,
APIKey: sdb.APIKey,
}
res, err := sdb.client.FindValidNodes(ctx, findValidNodesReq)
@ -139,8 +139,7 @@ func (sdb *StatDB) Update(ctx context.Context, nodeID []byte,
UpdateLatency: true,
}
updateReq := &pb.UpdateRequest{
Node: &node,
APIKey: sdb.APIKey,
Node: &node,
}
res, err := sdb.client.Update(ctx, updateReq)
@ -148,7 +147,7 @@ func (sdb *StatDB) Update(ctx context.Context, nodeID []byte,
return nil, err
}
return res.Stats, err
return res.Stats, nil
}
// UpdateUptime is used for updating a node's uptime in statdb
@ -157,20 +156,19 @@ func (sdb *StatDB) UpdateUptime(ctx context.Context, nodeID []byte,
defer mon.Task()(&ctx)(&err)
node := pb.Node{
NodeId: nodeID,
IsUp: isUp,
UpdateAuditSuccess: false,
UpdateUptime: true,
UpdateLatency: false,
NodeId: nodeID,
IsUp: isUp,
}
updateReq := &pb.UpdateRequest{
Node: &node,
APIKey: sdb.APIKey,
updateReq := &pb.UpdateUptimeRequest{
Node: &node,
}
res, err := sdb.client.Update(ctx, updateReq)
res, err := sdb.client.UpdateUptime(ctx, updateReq)
if err != nil {
return nil, err
}
return res.Stats, err
return res.Stats, nil
}
// UpdateAuditSuccess is used for updating a node's audit success in statdb
@ -179,20 +177,19 @@ func (sdb *StatDB) UpdateAuditSuccess(ctx context.Context, nodeID []byte,
defer mon.Task()(&ctx)(&err)
node := pb.Node{
NodeId: nodeID,
AuditSuccess: passed,
UpdateAuditSuccess: true,
UpdateUptime: false,
UpdateLatency: false,
NodeId: nodeID,
AuditSuccess: passed,
}
updateReq := &pb.UpdateRequest{
Node: &node,
APIKey: sdb.APIKey,
updateReq := &pb.UpdateAuditSuccessRequest{
Node: &node,
}
res, err := sdb.client.Update(ctx, updateReq)
res, err := sdb.client.UpdateAuditSuccess(ctx, updateReq)
if err != nil {
return nil, err
}
return res.Stats, err
return res.Stats, nil
}
// UpdateBatch is used for updating multiple nodes' stats in the stats db
@ -201,7 +198,6 @@ func (sdb *StatDB) UpdateBatch(ctx context.Context, nodes []*pb.Node) (statsList
updateBatchReq := &pb.UpdateBatchRequest{
NodeList: nodes,
APIKey: sdb.APIKey,
}
res, err := sdb.client.UpdateBatch(ctx, updateBatchReq)
@ -209,7 +205,7 @@ func (sdb *StatDB) UpdateBatch(ctx context.Context, nodes []*pb.Node) (statsList
return nil, nil, err
}
return res.StatsList, res.FailedNodes, err
return res.StatsList, res.FailedNodes, nil
}
// CreateEntryIfNotExists creates a db entry for a node if entry doesn't already exist
@ -218,8 +214,7 @@ func (sdb *StatDB) CreateEntryIfNotExists(ctx context.Context, nodeID []byte) (s
node := &pb.Node{NodeId: nodeID}
createReq := &pb.CreateEntryIfNotExistsRequest{
Node: node,
APIKey: sdb.APIKey,
Node: node,
}
res, err := sdb.client.CreateEntryIfNotExists(ctx, createReq)
@ -227,5 +222,5 @@ func (sdb *StatDB) CreateEntryIfNotExists(ctx context.Context, nodeID []byte) (s
return nil, err
}
return res.Stats, err
return res.Stats, nil
}

View File

@ -15,7 +15,7 @@ import (
monkit "gopkg.in/spacemonkeygo/monkit.v2"
"storj.io/storj/internal/migrate"
"storj.io/storj/pkg/pointerdb/auth"
"storj.io/storj/pkg/auth"
dbx "storj.io/storj/pkg/statdb/dbx"
pb "storj.io/storj/pkg/statdb/proto"
)
@ -30,10 +30,11 @@ var (
type Server struct {
DB *dbx.DB
logger *zap.Logger
apiKey []byte
}
// NewServer creates instance of Server
func NewServer(driver, source string, logger *zap.Logger) (*Server, error) {
func NewServer(driver, source, apiKey string, logger *zap.Logger) (*Server, error) {
db, err := dbx.Open(driver, source)
if err != nil {
return nil, err
@ -47,13 +48,14 @@ func NewServer(driver, source string, logger *zap.Logger) (*Server, error) {
return &Server{
DB: db,
logger: logger,
apiKey: []byte(apiKey),
}, nil
}
func (s *Server) validateAuth(APIKeyBytes []byte) error {
if !auth.ValidateAPIKey(string(APIKeyBytes)) {
s.logger.Error("unauthorized request: ", zap.Error(status.Errorf(codes.Unauthenticated, "Invalid API credential")))
return status.Errorf(codes.Unauthenticated, "Invalid API credential")
func (s *Server) validateAuth(ctx context.Context) error {
err := auth.ValidateAPIKey(ctx, s.apiKey)
if err != nil {
return err
}
return nil
}
@ -63,8 +65,7 @@ func (s *Server) Create(ctx context.Context, createReq *pb.CreateRequest) (resp
defer mon.Task()(&ctx)(&err)
s.logger.Debug("entering statdb Create")
APIKeyBytes := createReq.APIKey
if err := s.validateAuth(APIKeyBytes); err != nil {
if err := s.validateAuth(ctx); err != nil {
return nil, err
}
@ -127,8 +128,7 @@ func (s *Server) Get(ctx context.Context, getReq *pb.GetRequest) (resp *pb.GetRe
defer mon.Task()(&ctx)(&err)
s.logger.Debug("entering statdb Get")
APIKeyBytes := getReq.APIKey
err = s.validateAuth(APIKeyBytes)
err = s.validateAuth(ctx)
if err != nil {
return nil, err
}
@ -210,8 +210,7 @@ func (s *Server) Update(ctx context.Context, updateReq *pb.UpdateRequest) (resp
defer mon.Task()(&ctx)(&err)
s.logger.Debug("entering statdb Update")
APIKeyBytes := updateReq.APIKey
err = s.validateAuth(APIKeyBytes)
err = s.validateAuth(ctx)
if err != nil {
return nil, err
}
@ -219,8 +218,7 @@ func (s *Server) Update(ctx context.Context, updateReq *pb.UpdateRequest) (resp
node := updateReq.GetNode()
createIfReq := &pb.CreateEntryIfNotExistsRequest{
Node: updateReq.GetNode(),
APIKey: APIKeyBytes,
Node: updateReq.GetNode(),
}
_, err = s.CreateEntryIfNotExists(ctx, createIfReq)
@ -280,18 +278,114 @@ func (s *Server) Update(ctx context.Context, updateReq *pb.UpdateRequest) (resp
}, nil
}
// UpdateUptime updates a single storagenode's uptime stats in the db
func (s *Server) UpdateUptime(ctx context.Context, updateReq *pb.UpdateUptimeRequest) (resp *pb.UpdateUptimeResponse, err error) {
defer mon.Task()(&ctx)(&err)
s.logger.Debug("entering statdb UpdateUptime")
err = s.validateAuth(ctx)
if err != nil {
return nil, err
}
node := updateReq.GetNode()
dbNode, err := s.DB.Get_Node_By_Id(ctx, dbx.Node_Id(node.NodeId))
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
uptimeSuccessCount := dbNode.UptimeSuccessCount
totalUptimeCount := dbNode.TotalUptimeCount
var uptimeRatio float64
updateFields := dbx.Node_Update_Fields{}
uptimeSuccessCount, totalUptimeCount, uptimeRatio = updateRatioVars(
node.IsUp,
uptimeSuccessCount,
totalUptimeCount,
)
updateFields.UptimeSuccessCount = dbx.Node_UptimeSuccessCount(uptimeSuccessCount)
updateFields.TotalUptimeCount = dbx.Node_TotalUptimeCount(totalUptimeCount)
updateFields.UptimeRatio = dbx.Node_UptimeRatio(uptimeRatio)
dbNode, err = s.DB.Update_Node_By_Id(ctx, dbx.Node_Id(node.NodeId), updateFields)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
nodeStats := &pb.NodeStats{
NodeId: dbNode.Id,
AuditSuccessRatio: dbNode.AuditSuccessRatio,
UptimeRatio: dbNode.UptimeRatio,
AuditCount: dbNode.TotalAuditCount,
}
return &pb.UpdateUptimeResponse{
Stats: nodeStats,
}, nil
}
// UpdateAuditSuccess updates a single storagenode's uptime stats in the db
func (s *Server) UpdateAuditSuccess(ctx context.Context, updateReq *pb.UpdateAuditSuccessRequest) (resp *pb.UpdateAuditSuccessResponse, err error) {
defer mon.Task()(&ctx)(&err)
s.logger.Debug("entering statdb UpdateAuditSuccess")
err = s.validateAuth(ctx)
if err != nil {
return nil, err
}
node := updateReq.GetNode()
dbNode, err := s.DB.Get_Node_By_Id(ctx, dbx.Node_Id(node.NodeId))
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
auditSuccessCount := dbNode.AuditSuccessCount
totalAuditCount := dbNode.TotalAuditCount
var auditRatio float64
updateFields := dbx.Node_Update_Fields{}
auditSuccessCount, totalAuditCount, auditRatio = updateRatioVars(
node.AuditSuccess,
auditSuccessCount,
totalAuditCount,
)
updateFields.AuditSuccessCount = dbx.Node_AuditSuccessCount(auditSuccessCount)
updateFields.TotalAuditCount = dbx.Node_TotalAuditCount(totalAuditCount)
updateFields.AuditSuccessRatio = dbx.Node_AuditSuccessRatio(auditRatio)
dbNode, err = s.DB.Update_Node_By_Id(ctx, dbx.Node_Id(node.NodeId), updateFields)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
nodeStats := &pb.NodeStats{
NodeId: dbNode.Id,
AuditSuccessRatio: dbNode.AuditSuccessRatio,
UptimeRatio: dbNode.UptimeRatio,
AuditCount: dbNode.TotalAuditCount,
}
return &pb.UpdateAuditSuccessResponse{
Stats: nodeStats,
}, nil
}
// UpdateBatch for updating multiple farmers' stats in the db
func (s *Server) UpdateBatch(ctx context.Context, updateBatchReq *pb.UpdateBatchRequest) (resp *pb.UpdateBatchResponse, err error) {
defer mon.Task()(&ctx)(&err)
s.logger.Debug("entering statdb UpdateBatch")
APIKeyBytes := updateBatchReq.APIKey
var nodeStatsList []*pb.NodeStats
var failedNodes []*pb.Node
for _, node := range updateBatchReq.NodeList {
updateReq := &pb.UpdateRequest{
Node: node,
APIKey: APIKeyBytes,
Node: node,
}
updateRes, err := s.Update(ctx, updateReq)
@ -312,18 +406,17 @@ func (s *Server) UpdateBatch(ctx context.Context, updateBatchReq *pb.UpdateBatch
// CreateEntryIfNotExists creates a statdb node entry and saves to statdb if it didn't already exist
func (s *Server) CreateEntryIfNotExists(ctx context.Context, createIfReq *pb.CreateEntryIfNotExistsRequest) (resp *pb.CreateEntryIfNotExistsResponse, err error) {
APIKeyBytes := createIfReq.APIKey
defer mon.Task()(&ctx)(&err)
s.logger.Debug("entering statdb CreateEntryIfNotExists")
getReq := &pb.GetRequest{
NodeId: createIfReq.Node.NodeId,
APIKey: APIKeyBytes,
}
getRes, err := s.Get(ctx, getReq)
// TODO: figure out better way to confirm error is type dbx.ErrorCode_NoRows
if err != nil && strings.Contains(err.Error(), "no rows in result set") {
createReq := &pb.CreateRequest{
Node: createIfReq.Node,
APIKey: APIKeyBytes,
Node: createIfReq.Node,
}
res, err := s.Create(ctx, createReq)
if err != nil {

View File

@ -1,7 +1,7 @@
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package statdb
package statdb_test
import (
"context"
@ -12,12 +12,15 @@ import (
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"storj.io/storj/pkg/auth"
"storj.io/storj/pkg/statdb"
dbx "storj.io/storj/pkg/statdb/dbx"
pb "storj.io/storj/pkg/statdb/proto"
)
var (
ctx = context.Background()
apiKey = []byte("")
ctx = auth.WithAPIKey(context.Background(), apiKey)
)
func TestCreateDoesNotExist(t *testing.T) {
@ -25,12 +28,10 @@ func TestCreateDoesNotExist(t *testing.T) {
statdb, db, err := getServerAndDB(dbPath)
assert.NoError(t, err)
apiKey := []byte("")
nodeID := []byte("testnodeid")
node := &pb.Node{NodeId: nodeID}
createReq := &pb.CreateRequest{
Node: node,
APIKey: apiKey,
Node: node,
}
resp, err := statdb.Create(ctx, createReq)
assert.NoError(t, err)
@ -51,7 +52,6 @@ func TestCreateExists(t *testing.T) {
statdb, db, err := getServerAndDB(dbPath)
assert.NoError(t, err)
apiKey := []byte("")
nodeID := []byte("testnodeid")
auditSuccessCount, totalAuditCount, auditRatio := getRatio(4, 10)
@ -62,8 +62,7 @@ func TestCreateExists(t *testing.T) {
node := &pb.Node{NodeId: nodeID}
createReq := &pb.CreateRequest{
Node: node,
APIKey: apiKey,
Node: node,
}
_, err = statdb.Create(ctx, createReq)
assert.Error(t, err)
@ -75,7 +74,6 @@ func TestCreateWithStats(t *testing.T) {
auditSuccessCount, totalAuditCount, auditRatio := getRatio(4, 10)
uptimeSuccessCount, totalUptimeCount, uptimeRatio := getRatio(8, 25)
apiKey := []byte("")
nodeID := []byte("testnodeid")
node := &pb.Node{NodeId: nodeID}
stats := &pb.NodeStats{
@ -85,9 +83,8 @@ func TestCreateWithStats(t *testing.T) {
UptimeSuccessCount: uptimeSuccessCount,
}
createReq := &pb.CreateRequest{
Node: node,
Stats: stats,
APIKey: apiKey,
Node: node,
Stats: stats,
}
resp, err := statdb.Create(ctx, createReq)
assert.NoError(t, err)
@ -108,7 +105,6 @@ func TestGetExists(t *testing.T) {
statdb, db, err := getServerAndDB(dbPath)
assert.NoError(t, err)
apiKey := []byte("")
nodeID := []byte("testnodeid")
auditSuccessCount, totalAuditCount, auditRatio := getRatio(4, 10)
@ -120,7 +116,6 @@ func TestGetExists(t *testing.T) {
getReq := &pb.GetRequest{
NodeId: nodeID,
APIKey: apiKey,
}
resp, err := statdb.Get(ctx, getReq)
assert.NoError(t, err)
@ -135,12 +130,10 @@ func TestGetDoesNotExist(t *testing.T) {
statdb, _, err := getServerAndDB(dbPath)
assert.NoError(t, err)
apiKey := []byte("")
nodeID := []byte("testnodeid")
getReq := &pb.GetRequest{
NodeId: nodeID,
APIKey: apiKey,
}
_, err = statdb.Get(ctx, getReq)
assert.Error(t, err)
@ -151,8 +144,6 @@ func TestFindValidNodes(t *testing.T) {
statdb, db, err := getServerAndDB(dbPath)
assert.NoError(t, err)
apiKey := []byte("")
for _, tt := range []struct {
nodeID []byte
auditSuccessCount int64
@ -186,7 +177,6 @@ func TestFindValidNodes(t *testing.T) {
UptimeRatio: 0.95,
AuditCount: 15,
},
APIKey: apiKey,
}
resp, err := statdb.FindValidNodes(ctx, findValidNodesReq)
@ -204,7 +194,6 @@ func TestUpdateExists(t *testing.T) {
statdb, db, err := getServerAndDB(dbPath)
assert.NoError(t, err)
apiKey := []byte("")
nodeID := []byte("testnodeid")
auditSuccessCount, totalAuditCount, auditRatio := getRatio(4, 10)
@ -221,8 +210,7 @@ func TestUpdateExists(t *testing.T) {
IsUp: false,
}
updateReq := &pb.UpdateRequest{
Node: node,
APIKey: apiKey,
Node: node,
}
resp, err := statdb.Update(ctx, updateReq)
assert.NoError(t, err)
@ -234,12 +222,71 @@ func TestUpdateExists(t *testing.T) {
assert.EqualValues(t, newUptimeRatio, stats.UptimeRatio)
}
func TestUpdateUptimeExists(t *testing.T) {
dbPath := getDBPath()
statdb, db, err := getServerAndDB(dbPath)
assert.NoError(t, err)
nodeID := []byte("testnodeid")
auditSuccessCount, totalAuditCount, auditRatio := getRatio(4, 10)
uptimeSuccessCount, totalUptimeCount, uptimeRatio := getRatio(8, 25)
err = createNode(ctx, db, nodeID, auditSuccessCount, totalAuditCount, auditRatio,
uptimeSuccessCount, totalUptimeCount, uptimeRatio)
assert.NoError(t, err)
node := &pb.Node{
NodeId: nodeID,
IsUp: false,
}
updateReq := &pb.UpdateUptimeRequest{
Node: node,
}
resp, err := statdb.UpdateUptime(ctx, updateReq)
assert.NoError(t, err)
_, _, newUptimeRatio := getRatio(int(uptimeSuccessCount), int(totalUptimeCount+1))
stats := resp.Stats
assert.EqualValues(t, auditRatio, stats.AuditSuccessRatio)
assert.EqualValues(t, totalAuditCount, stats.AuditCount)
assert.EqualValues(t, newUptimeRatio, stats.UptimeRatio)
}
func TestUpdateAuditSuccessExists(t *testing.T) {
dbPath := getDBPath()
statdb, db, err := getServerAndDB(dbPath)
assert.NoError(t, err)
nodeID := []byte("testnodeid")
auditSuccessCount, totalAuditCount, auditRatio := getRatio(4, 10)
uptimeSuccessCount, totalUptimeCount, uptimeRatio := getRatio(8, 25)
err = createNode(ctx, db, nodeID, auditSuccessCount, totalAuditCount, auditRatio,
uptimeSuccessCount, totalUptimeCount, uptimeRatio)
assert.NoError(t, err)
node := &pb.Node{
NodeId: nodeID,
AuditSuccess: false,
}
updateReq := &pb.UpdateAuditSuccessRequest{
Node: node,
}
resp, err := statdb.UpdateAuditSuccess(ctx, updateReq)
assert.NoError(t, err)
_, _, newAuditRatio := getRatio(int(auditSuccessCount), int(totalAuditCount+1))
stats := resp.Stats
assert.EqualValues(t, newAuditRatio, stats.AuditSuccessRatio)
assert.EqualValues(t, totalAuditCount+1, stats.AuditCount)
assert.EqualValues(t, uptimeRatio, stats.UptimeRatio)
}
func TestUpdateBatchExists(t *testing.T) {
dbPath := getDBPath()
statdb, db, err := getServerAndDB(dbPath)
assert.NoError(t, err)
apiKey := []byte("")
nodeID1 := []byte("testnodeid1")
nodeID2 := []byte("testnodeid2")
@ -269,7 +316,6 @@ func TestUpdateBatchExists(t *testing.T) {
}
updateBatchReq := &pb.UpdateBatchRequest{
NodeList: []*pb.Node{node1, node2},
APIKey: apiKey,
}
resp, err := statdb.UpdateBatch(ctx, updateBatchReq)
assert.NoError(t, err)
@ -290,7 +336,6 @@ func TestUpdateBatchDoesNotExist(t *testing.T) {
statdb, db, err := getServerAndDB(dbPath)
assert.NoError(t, err)
apiKey := []byte("")
nodeID1 := []byte("testnodeid1")
nodeID2 := []byte("testnodeid2")
@ -315,7 +360,6 @@ func TestUpdateBatchDoesNotExist(t *testing.T) {
}
updateBatchReq := &pb.UpdateBatchRequest{
NodeList: []*pb.Node{node1, node2},
APIKey: apiKey,
}
_, err = statdb.UpdateBatch(ctx, updateBatchReq)
assert.NoError(t, err)
@ -326,7 +370,6 @@ func TestUpdateBatchEmpty(t *testing.T) {
statdb, db, err := getServerAndDB(dbPath)
assert.NoError(t, err)
apiKey := []byte("")
nodeID1 := []byte("testnodeid1")
auditSuccessCount1, totalAuditCount1, auditRatio1 := getRatio(4, 10)
@ -337,7 +380,6 @@ func TestUpdateBatchEmpty(t *testing.T) {
updateBatchReq := &pb.UpdateBatchRequest{
NodeList: []*pb.Node{},
APIKey: apiKey,
}
resp, err := statdb.UpdateBatch(ctx, updateBatchReq)
assert.NoError(t, err)
@ -349,7 +391,6 @@ func TestCreateEntryIfNotExists(t *testing.T) {
statdb, db, err := getServerAndDB(dbPath)
assert.NoError(t, err)
apiKey := []byte("")
nodeID1 := []byte("testnodeid1")
nodeID2 := []byte("testnodeid2")
@ -361,8 +402,7 @@ func TestCreateEntryIfNotExists(t *testing.T) {
node1 := &pb.Node{NodeId: nodeID1}
createIfNotExistsReq1 := &pb.CreateEntryIfNotExistsRequest{
Node: node1,
APIKey: apiKey,
Node: node1,
}
_, err = statdb.CreateEntryIfNotExists(ctx, createIfNotExistsReq1)
assert.NoError(t, err)
@ -375,8 +415,7 @@ func TestCreateEntryIfNotExists(t *testing.T) {
node2 := &pb.Node{NodeId: nodeID2}
createIfNotExistsReq2 := &pb.CreateEntryIfNotExistsRequest{
Node: node2,
APIKey: apiKey,
Node: node2,
}
_, err = statdb.CreateEntryIfNotExists(ctx, createIfNotExistsReq2)
assert.NoError(t, err)
@ -392,16 +431,16 @@ func getDBPath() string {
return fmt.Sprintf("file:memdb%d?mode=memory&cache=shared", rand.Int63())
}
func getServerAndDB(path string) (statdb *Server, db *dbx.DB, err error) {
statdb, err = NewServer("sqlite3", path, zap.NewNop())
func getServerAndDB(path string) (sdb *statdb.Server, db *dbx.DB, err error) {
sdb, err = statdb.NewServer("sqlite3", path, string(apiKey), zap.NewNop())
if err != nil {
return &Server{}, &dbx.DB{}, err
return &statdb.Server{}, &dbx.DB{}, err
}
db, err = dbx.Open("sqlite3", path)
if err != nil {
return &Server{}, &dbx.DB{}, err
return &statdb.Server{}, &dbx.DB{}, err
}
return statdb, db, err
return sdb, db, err
}
func createNode(ctx context.Context, db *dbx.DB, nodeID []byte,