Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass the Org Id and the Service Id for channel state status #515

Merged
merged 10 commits into from
Dec 16, 2020
2 changes: 1 addition & 1 deletion escrow/payment_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func PublishChannelStats(payment handler.Payment) (err *handler.GrpcError) {
channelStats.ServiceID = config.GetString(config.ServiceId)
log.Debugf("Payment channel payment handler is publishing channel statistics: %v", channelStats)
commonStats := &metrics.CommonStats{
GroupID: channelStats.GroupID, UserName: paymentTransaction.Channel().Sender.Hex()}
GroupID: channelStats.GroupID, UserName: paymentTransaction.Channel().Sender.Hex(), OrganizationID: channelStats.OrganizationID, ServiceID: channelStats.ServiceID}
status := metrics.Publish(channelStats, serviceURL, commonStats)

if !status {
Expand Down
32 changes: 16 additions & 16 deletions escrow/payment_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,26 +146,26 @@ func (suite *PaymentHandlerTestSuite) TestValidatePaymentIncorrectIncome() {
}

func Test_paymentChannelPaymentHandler_PublishChannelStats(t *testing.T) {
payment := &paymentTransaction{payment:Payment{Amount:big.NewInt(10),ChannelID:big.NewInt(6),
ChannelNonce:big.NewInt(1)},channel:&PaymentChannelData{FullAmount:big.NewInt(10)}}
payment := &paymentTransaction{payment: Payment{Amount: big.NewInt(10), ChannelID: big.NewInt(6),
ChannelNonce: big.NewInt(0)}, channel: &PaymentChannelData{FullAmount: big.NewInt(10), Nonce: big.NewInt(0)}}
tests := []struct {
name string
name string

wantErr *handler.GrpcError
wantErr *handler.GrpcError
setupFunc func()
}{
{name:"",wantErr:handler.NewGrpcErrorf(codes.Internal, "Cannot post latest offline channel state as metering is disabled !!"),setupFunc: func() {
},},

{name:"",wantErr:nil,setupFunc: func() {
config.Vip().Set(config.MeteringEnabled,true)
config.Vip().Set(config.PvtKeyForMetering,"063C00D18E147F4F734846E47FE6598FC7A6D56307862F7EDC92B9F43CC27EDD")
config.Vip().Set(config.MeteringEndPoint,"http://demo8325345.mockable.io")
},},

{name:"",wantErr:handler.NewGrpcErrorf(codes.Internal, "Unable to publish status error"),setupFunc: func() {
config.Vip().Set(config.MeteringEndPoint,"badurl")
},},
{name: "", wantErr: handler.NewGrpcErrorf(codes.Internal, "Cannot post latest offline channel state as metering is disabled !!"), setupFunc: func() {
}},

{name: "", wantErr: nil, setupFunc: func() {
config.Vip().Set(config.MeteringEnabled, true)
config.Vip().Set(config.PvtKeyForMetering, "063C00D18E147F4F734846E47FE6598FC7A6D56307862F7EDC92B9F43CC27EDD")
config.Vip().Set(config.MeteringEndPoint, "https://bkq2d3zjl4.execute-api.eu-west-1.amazonaws.com/main")
}},

{name: "", wantErr: handler.NewGrpcErrorf(codes.Internal, "Unable to publish status error"), setupFunc: func() {
config.Vip().Set(config.MeteringEndPoint, "badurl")
}},
}
for _, tt := range tests {
tt.setupFunc()
Expand Down
25 changes: 12 additions & 13 deletions metrics/clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ const (
)

type clientImplHeartBeat struct {

}

// Check implements `service Health`.
func (service *clientImplHeartBeat) Check( ctx context.Context, req *pb.HealthCheckRequest) (*pb.HealthCheckResponse, error) {
return &pb.HealthCheckResponse{Status:pb.HealthCheckResponse_SERVING},nil
func (service *clientImplHeartBeat) Check(ctx context.Context, req *pb.HealthCheckRequest) (*pb.HealthCheckResponse, error) {
return &pb.HealthCheckResponse{Status: pb.HealthCheckResponse_SERVING}, nil
}

func (service *clientImplHeartBeat) Watch(*pb.HealthCheckRequest, pb.Health_WatchServer) (error) {
func (service *clientImplHeartBeat) Watch(*pb.HealthCheckRequest, pb.Health_WatchServer) error {
return nil
}

Expand Down Expand Up @@ -63,39 +62,39 @@ func Test_callgRPCServiceHeartbeat(t *testing.T) {
assert.False(t, err != nil)

assert.NotEqual(t, `{}`, string(heartbeat), "Service Heartbeat must not be empty.")
assert.Equal(t,heartbeat.String(),pb.HealthCheckResponse_SERVING.String())
assert.Equal(t, heartbeat.String(), pb.HealthCheckResponse_SERVING.String())

serviceURL = "localhost:26000"
heartbeat, err = callgRPCServiceHeartbeat(serviceURL)
assert.True(t, err != nil)
}

func Test_callHTTPServiceHeartbeat(t *testing.T) {
serviceURL := "http://demo8325345.mockable.io/heartbeat"
serviceURL := "http://demo5343751.mockable.io/heartbeat"
heartbeat, err := callHTTPServiceHeartbeat(serviceURL)
assert.False(t, err != nil)
assert.NotEqual(t, string(heartbeat), `{}`, "Service Heartbeat must not be empty.")
assert.Equal(t, string(heartbeat), `{"serviceID":"SERVICE001", "status":"SERVING"}`,
"Unexpected service heartbeat")

/* var sHeartbeat pb.HeartbeatMsg
err = json.Unmarshal(heartbeat, &sHeartbeat)
assert.True(t, err != nil)
assert.Equal(t, sHeartbeat.ServiceID, "SERVICE001", "Unexpected service ID")
/* var sHeartbeat pb.HeartbeatMsg
err = json.Unmarshal(heartbeat, &sHeartbeat)
assert.True(t, err != nil)
assert.Equal(t, sHeartbeat.ServiceID, "SERVICE001", "Unexpected service ID")

*/ serviceURL = "http://demo8325345.mockable.io"
*/serviceURL = "http://demo8325345.mockable.io"
heartbeat, err = callHTTPServiceHeartbeat(serviceURL)
assert.True(t, err != nil)
}

func Test_callRegisterService(t *testing.T) {
serviceURL := "https://demo8325345.mockable.io/register"
serviceURL := "https://demo5343751.mockable.io/register"
daemonID := GetDaemonID()

result := callRegisterService(daemonID, serviceURL)
assert.Equal(t, true, result)

serviceURL = "https://demo8325345.mockable.io/registererror"
serviceURL = "https://demo5343751.mockable.io/registererror"
result = callRegisterService(daemonID, serviceURL)
assert.Equal(t, false, result)

Expand Down
4 changes: 2 additions & 2 deletions metrics/heartbeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestHeartbeatHandler(t *testing.T) {
}

func Test_GetHeartbeat(t *testing.T) {
serviceURL := "http://demo8325345.mockable.io/heartbeat"
serviceURL := "http://demo5343751.mockable.io/heartbeat"
serviceType := "http"
serviveID := "SERVICE001"

Expand All @@ -79,7 +79,7 @@ func Test_GetHeartbeat(t *testing.T) {
assert.Equal(t, sHeartbeat.Status, grpc_health_v1.HealthCheckResponse_SERVING.String())

// check with some timeout URL
serviceURL = "http://demo8325345.mockable.io"
serviceURL = "http://demo5343751.mockable.io"
dHeartbeat, _ = GetHeartbeat(serviceURL, serviceType, serviveID)
assert.NotNil(t, dHeartbeat, "heartbeat must not be nil")

Expand Down
4 changes: 2 additions & 2 deletions metrics/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func TestGetDaemonID(t *testing.T) {
}

func TestRegisterDaemon(t *testing.T) {
serviceURL := "https://demo8325345.mockable.io/register"
serviceURL := "https://demo5343751.mockable.io/register"

result := RegisterDaemon(serviceURL)
assert.Equal(t, true, result)

serviceURL = "https://demo8325345.mockable.io/registererror"
serviceURL = "https://demo5343751.mockable.io/registererror"
result = RegisterDaemon(serviceURL)
assert.Equal(t, false, result)
}
Expand Down
32 changes: 17 additions & 15 deletions metrics/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import (
"time"
)

const MeteringPrefix = "_usage"
const MeteringPrefix = "_usage"

//Get the value of the first Pair
func GetValue(md metadata.MD, key string) string {
array := md.Get(key)
Expand Down Expand Up @@ -52,60 +53,63 @@ func GenXid() string {
}

//convert the payload to JSON and publish it to the serviceUrl passed
func Publish(payload interface{}, serviceUrl string,commonStats *CommonStats) bool {
func Publish(payload interface{}, serviceUrl string, commonStats *CommonStats) bool {
jsonBytes, err := ConvertStructToJSON(payload)
if err != nil {
return false
}
status := publishJson(jsonBytes, serviceUrl, true,commonStats)
status := publishJson(jsonBytes, serviceUrl, true, commonStats)
if !status {
log.WithField("payload", string(jsonBytes)).WithField("url", serviceUrl).Warning("Unable to publish metrics")
}
return status
}

// Publish the json on the service end point, retry will be set to false when trying to re publish the payload
func publishJson(json []byte, serviceURL string, reTry bool,commonStats *CommonStats) bool {
response, err := sendRequest(json, serviceURL,commonStats)
func publishJson(json []byte, serviceURL string, reTry bool, commonStats *CommonStats) bool {
response, err := sendRequest(json, serviceURL, commonStats)
if err != nil {
log.WithError(err)
} else {
status, reRegister := checkForSuccessfulResponse(response)
if reRegister && reTry {
//if Daemon was registered successfully , retry to publish the payload
status = publishJson(json, serviceURL, false,commonStats)
status = publishJson(json, serviceURL, false, commonStats)
}
return status
}
return false
}

//Set all the headers before publishing
func sendRequest(json []byte, serviceURL string,commonStats *CommonStats ) (*http.Response, error) {
func sendRequest(json []byte, serviceURL string, commonStats *CommonStats) (*http.Response, error) {
req, err := http.NewRequest("POST", serviceURL, bytes.NewBuffer(json))
if err != nil {
log.WithField("serviceURL", serviceURL).WithError(err).Warningf("Unable to create service request to publish stats")
return nil, err
}
// sending the post request
commonStats.ServiceID = config.GetString(config.ServiceId)
commonStats.OrganizationID = config.GetString(config.OrganizationId)

client := &http.Client{}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Daemonid", GetDaemonID())
req.Header.Set("X-Token", daemonAuthorizationToken)
SignMessageForMetering(req,commonStats)
SignMessageForMetering(req, commonStats)

return client.Do(req)

}

func SignMessageForMetering(req *http.Request, commonStats *CommonStats) () {
func SignMessageForMetering(req *http.Request, commonStats *CommonStats) {

privateKey, err := getPrivateKeyForMetering()
if err != nil {
log.Error(err)
return
}
currentBlock, err := authutils.CurrentBlock();
currentBlock, err := authutils.CurrentBlock()
if err != nil {
log.Error(err)
return
Expand All @@ -122,19 +126,19 @@ func SignMessageForMetering(req *http.Request, commonStats *CommonStats) () {

}

func getPrivateKeyForMetering() (privateKey *ecdsa.PrivateKey,err error) {
func getPrivateKeyForMetering() (privateKey *ecdsa.PrivateKey, err error) {
if privateKeyString := config.GetString(config.PvtKeyForMetering); privateKeyString != "" {
privateKey, err = crypto.HexToECDSA(privateKeyString)
if err != nil {
return nil, err
}
log.WithField("public key",crypto.PubkeyToAddress(privateKey.PublicKey).String())
log.WithField("public key", crypto.PubkeyToAddress(privateKey.PublicKey).String())
}

return
}

func signForMeteringValidation(privateKey *ecdsa.PrivateKey, currentBlock *big.Int, prefix string,commonStats *CommonStats) []byte {
func signForMeteringValidation(privateKey *ecdsa.PrivateKey, currentBlock *big.Int, prefix string, commonStats *CommonStats) []byte {
message := bytes.Join([][]byte{
[]byte(prefix),
[]byte(commonStats.UserName),
Expand All @@ -148,8 +152,6 @@ func signForMeteringValidation(privateKey *ecdsa.PrivateKey, currentBlock *big.I
return authutils.GetSignature(message, privateKey)
}



//Check if the response received was proper
func checkForSuccessfulResponse(response *http.Response) (status bool, retry bool) {
if response == nil {
Expand Down
13 changes: 6 additions & 7 deletions metrics/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ func TestGetValue(t *testing.T) {
}

func TestPublish(t *testing.T) {
status := Publish(nil, "",nil)
status := Publish(nil, "", nil)
assert.Equal(t, status, false)
status = Publish(nil, "http://localhost:8080",nil)
status = Publish(nil, "http://localhost:8080", nil)
assert.Equal(t, status, false)

status = Publish(struct {
title string
}{
title: "abcd",
}, "http://localhost:8080",nil)
}, "http://localhost:8080", &CommonStats{})
assert.Equal(t, status, false)
}

Expand Down Expand Up @@ -78,16 +78,15 @@ func TestGetEpochTime(t *testing.T) {
assert.NotEqual(t, currentEpoch, secondEpoch, "Epochs msut not be the same")
}


func Test_getPrivateKeyForMetering(t *testing.T) {
config.Vip().Set(config.PvtKeyForMetering,"063C00D18E147F4F734846E47FE6598FC7A6D56307862F7EDC92B9F43CC27EDD")
key,err := getPrivateKeyForMetering()
config.Vip().Set(config.PvtKeyForMetering, "063C00D18E147F4F734846E47FE6598FC7A6D56307862F7EDC92B9F43CC27EDD")
key, err := getPrivateKeyForMetering()
if err == nil {
assert.Equal(t, crypto.PubkeyToAddress(key.PublicKey).String(), "0x94d04332C4f5273feF69c4a52D24f42a3aF1F207")
assert.NotNil(t, key)
assert.Nil(t, err)

bytesForMetering := signForMeteringValidation(key, big.NewInt(123), MeteringPrefix,&CommonStats{UserName:"test-user"})
bytesForMetering := signForMeteringValidation(key, big.NewInt(123), MeteringPrefix, &CommonStats{UserName: "test-user"})
signature := authutils.GetSignature(bytesForMetering, key)
signer, err := authutils.GetSignerAddressFromMessage(bytesForMetering, signature)
assert.Equal(t, signer.String(), "0x94d04332C4f5273feF69c4a52D24f42a3aF1F207")
Expand Down
29 changes: 14 additions & 15 deletions snetd/cmd/components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,33 @@ import (
)

func TestComponents_verifyMeteringConfigurations(t *testing.T) {
config.Vip().Set(config.MeteringEndPoint,"http://demo8325345.mockable.io")
config.Vip().Set(config.MeteringEndPoint, "http://demo5343751.mockable.io")

component := &Components{}
ok, err := component.verifyAuthenticationSetUpForFreeCall("http://demo8325345.mockable.io/verify",
ok, err := component.verifyAuthenticationSetUpForFreeCall("http://demo5343751.mockable.io/verify",
"testgroup")
ok, err = component.verifyAuthenticationSetUpForFreeCall("http://demo8325345.mockable.io/test","");
ok, err = component.verifyAuthenticationSetUpForFreeCall("http://demo5343751.mockable.io/test", "")
if err != nil {
assert.Equal(t,err.Error(),"you need a specify a valid private key 'pvt_key_for_metering' as part of service publication process.invalid length, need 256 bits")
assert.False(t,ok)
assert.Equal(t, err.Error(), "you need a specify a valid private key 'pvt_key_for_metering' as part of service publication process.invalid length, need 256 bits")
assert.False(t, ok)

}
config.Vip().Set(config.PvtKeyForMetering,"6996606c7854992c10d8cdc9a13d511a9d9db8ab8f21e59d6ac901a76367b36b")
ok, err = component.verifyAuthenticationSetUpForFreeCall("http://demo8325345.mockable.io/verify",
config.Vip().Set(config.PvtKeyForMetering, "6996606c7854992c10d8cdc9a13d511a9d9db8ab8f21e59d6ac901a76367b36b")
ok, err = component.verifyAuthenticationSetUpForFreeCall("http://demo5343751.mockable.io/verify",
"testgroup")
assert.Nil(t,err)
assert.True(t,ok)

assert.Nil(t, err)
assert.True(t, ok)

ok, err = component.verifyAuthenticationSetUpForFreeCall("http://demo8325345.mockable.io/badurl","");
ok, err = component.verifyAuthenticationSetUpForFreeCall("http://demo5343751.mockable.io/badurl", "")
if err != nil {
assert.Equal(t,err.Error(),"Service call failed with status code : 404 ")
assert.False(t,ok)
assert.Equal(t, err.Error(), "Service call failed with status code : 404 ")
assert.False(t, ok)

}

ok, err = component.verifyAuthenticationSetUpForFreeCall("http://demo8325345.mockable.io/failedresponse","")
ok, err = component.verifyAuthenticationSetUpForFreeCall("http://demo5343751.mockable.io/failedresponse", "")
if err != nil {
assert.Equal(t, "Error returned by by Metering Service http://demo8325345.mockable.io/verify Verification, "+
assert.Equal(t, "Error returned by by Metering Service http://demo5343751.mockable.io/verify Verification, "+
"pls check the pvt_key_for_metering set up. The public key in metering does not correspond to the private key in Daemon config.", err.Error())
assert.False(t, ok)
}
Expand Down