Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
notifications-bot
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
archives
notifications-bot
Commits
bd54c959
Commit
bd54c959
authored
Apr 19, 2021
by
Jonah Husson
Browse files
Options
Downloads
Patches
Plain Diff
remove a whole bunch of old code
parent
639ca3b2
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
cmd/root.go
+9
-54
9 additions, 54 deletions
cmd/root.go
notifications/notifications.go
+16
-58
16 additions, 58 deletions
notifications/notifications.go
notifications/notifications_test.go
+2
-39
2 additions, 39 deletions
notifications/notifications_test.go
with
27 additions
and
151 deletions
cmd/root.go
+
9
−
54
View file @
bd54c959
...
...
@@ -11,7 +11,6 @@ package cmd
import
(
"fmt"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/spf13/cobra"
jww
"github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
...
...
@@ -19,14 +18,11 @@ import (
"gitlab.com/elixxir/notifications-bot/notifications"
"gitlab.com/elixxir/notifications-bot/storage"
"gitlab.com/xx_network/comms/connect"
"gitlab.com/xx_network/comms/signature"
"gitlab.com/xx_network/primitives/id"
"gitlab.com/xx_network/primitives/ndf"
"gitlab.com/xx_network/primitives/utils"
"net"
"os"
"path"
"strings"
)
var
(
...
...
@@ -97,10 +93,16 @@ var rootCmd = &cobra.Command{
port
,
)
//
Set up the notifications server connections
err
=
setupConnection
(
impl
,
viper
.
GetString
(
"permissioningCertPath"
)
,
viper
.
GetString
(
"permissioningAddress"
)
)
//
Read in permissioning certificate
cert
,
err
:
=
utils
.
ReadFile
(
viper
.
GetString
(
"permissioningCertPath"
))
if
err
!=
nil
{
jww
.
FATAL
.
Panicf
(
"Failed to set up connections: %+v"
,
err
)
jww
.
FATAL
.
Panicf
(
"Could not read permissioning cert: %+v"
,
err
)
}
// Add host for permissioning server
_
,
err
=
impl
.
Comms
.
AddHost
(
&
id
.
Permissioning
,
viper
.
GetString
(
"permissioningAddress"
),
cert
,
connect
.
GetDefaultHostParams
())
if
err
!=
nil
{
jww
.
FATAL
.
Panicf
(
"Failed to Create permissioning host: %+v"
,
err
)
}
// Start ephemeral ID tracking
...
...
@@ -115,53 +117,6 @@ var rootCmd = &cobra.Command{
},
}
// setupConnection handles connecting to permissioning and polling for the NDF once connected
func
setupConnection
(
impl
*
notifications
.
Impl
,
permissioningCertPath
,
permissioningAddr
string
)
error
{
// Read in permissioning certificate
cert
,
err
:=
utils
.
ReadFile
(
permissioningCertPath
)
if
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"Could not read permissioning cert"
)
}
// Add host for permissioning server
h
,
err
:=
impl
.
Comms
.
AddHost
(
&
id
.
Permissioning
,
permissioningAddr
,
cert
,
connect
.
GetDefaultHostParams
())
if
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"Failed to Create permissioning host"
)
}
// Loop until an NDF is received
var
def
*
ndf
.
NetworkDefinition
for
def
==
nil
{
// TODO: get the ndf
ndfResponse
,
err
:=
impl
.
Comms
.
RequestNdf
(
h
,
&
mixmessages
.
NDFHash
{
Hash
:
nil
,
})
// Don't stop if error is expected
if
err
!=
nil
&&
!
strings
.
Contains
(
err
.
Error
(),
ndf
.
NO_NDF
)
{
return
errors
.
Wrap
(
err
,
"Failed to get NDF"
)
}
if
ndfResponse
==
nil
||
ndfResponse
.
GetNdf
()
==
nil
{
continue
}
err
=
signature
.
Verify
(
ndfResponse
,
h
.
GetPubKey
())
if
err
!=
nil
{
return
errors
.
WithMessage
(
err
,
"Failed to verify signature on received NDF"
)
}
def
,
err
=
ndf
.
Unmarshal
(
ndfResponse
.
Ndf
)
if
err
!=
nil
{
return
errors
.
WithMessage
(
err
,
"Failed to decode received NDF"
)
}
}
// Update NDF & gateway host
err
=
impl
.
UpdateNdf
(
def
)
if
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"Failed to update impl's NDF"
)
}
return
nil
}
// Execute adds all child commands to the root command and sets flags
// appropriately. This is called by main.main(). It only needs to
// happen once to the rootCmd.
...
...
This diff is collapsed.
Click to expand it.
notifications/notifications.go
+
16
−
58
View file @
bd54c959
...
...
@@ -9,7 +9,6 @@
package
notifications
import
(
"crypto/x509"
"firebase.google.com/go/messaging"
"github.com/pkg/errors"
jww
"github.com/spf13/jwalterweatherman"
...
...
@@ -21,7 +20,6 @@ import (
"gitlab.com/elixxir/notifications-bot/storage"
"gitlab.com/xx_network/comms/connect"
"gitlab.com/xx_network/crypto/signature/rsa"
"gitlab.com/xx_network/crypto/tls"
"gitlab.com/xx_network/primitives/id"
"gitlab.com/xx_network/primitives/id/ephemeral"
"gitlab.com/xx_network/primitives/ndf"
...
...
@@ -31,7 +29,6 @@ import (
)
// Function type definitions for the main operations (poll and notify)
type
PollFunc
func
(
*
Impl
)
([][]
byte
,
error
)
type
NotifyFunc
func
(
*
pb
.
NotificationData
,
*
messaging
.
Client
,
*
firebase
.
FirebaseComm
,
*
storage
.
Storage
)
error
// Params struct holds info passed in for configuration
...
...
@@ -45,29 +42,16 @@ type Params struct {
// Local impl for notifications; holds comms, storage object, creds and main functions
type
Impl
struct
{
Comms
*
notificationBot
.
Comms
inst
*
network
.
Instance
Storage
*
storage
.
Storage
notificationCert
*
x509
.
Certificate
notificationKey
*
rsa
.
PrivateKey
certFromFile
string
ndf
*
ndf
.
NetworkDefinition
pollFunc
PollFunc
inst
*
network
.
Instance
notifyFunc
NotifyFunc
fcm
*
messaging
.
Client
gwId
*
id
.
ID
ndfStopper
Stopper
updateChan
,
deleteChan
chan
int64
}
// StartNotifications creates an Impl from the information passed in
func
StartNotifications
(
params
Params
,
noTLS
,
noFirebase
bool
)
(
*
Impl
,
error
)
{
impl
:=
&
Impl
{
updateChan
:
make
(
chan
int64
),
deleteChan
:
make
(
chan
int64
),
}
var
cert
,
key
[]
byte
var
err
error
...
...
@@ -77,10 +61,6 @@ func StartNotifications(params Params, noTLS, noFirebase bool) (*Impl, error) {
if
err
!=
nil
{
return
nil
,
errors
.
Wrapf
(
err
,
"failed to read key at %+v"
,
params
.
KeyPath
)
}
impl
.
notificationKey
,
err
=
rsa
.
LoadPrivateKeyFromPem
(
key
)
if
err
!=
nil
{
return
nil
,
errors
.
Wrapf
(
err
,
"Failed to parse notifications server key (%+v)"
,
impl
.
notificationKey
)
}
}
else
{
jww
.
WARN
.
Println
(
"Running without key..."
)
}
...
...
@@ -91,18 +71,21 @@ func StartNotifications(params Params, noTLS, noFirebase bool) (*Impl, error) {
if
err
!=
nil
{
return
nil
,
errors
.
Wrapf
(
err
,
"failed to read certificate at %+v"
,
params
.
CertPath
)
}
// Set globals for notification server
impl
.
certFromFile
=
string
(
cert
)
impl
.
notificationCert
,
err
=
tls
.
LoadCertificate
(
string
(
cert
))
}
// Set up firebase messaging client
var
app
*
messaging
.
Client
if
!
noFirebase
{
app
,
err
=
firebase
.
SetupMessagingApp
(
params
.
FBCreds
)
if
err
!=
nil
{
return
nil
,
errors
.
Wrapf
(
err
,
"Failed to parse notifications server cert. "
+
"Notifications cert is %+v"
,
impl
.
notificationCert
)
return
nil
,
errors
.
Wrap
(
err
,
"Failed to setup firebase messaging app"
)
}
}
// set up stored functions
// impl.pollFunc = pollForNotifications
impl
.
notifyFunc
=
notifyUser
impl
:=
&
Impl
{
notifyFunc
:
notifyUser
,
fcm
:
app
,
}
// Start notification comms server
handler
:=
NewImplementation
(
impl
)
...
...
@@ -113,14 +96,6 @@ func StartNotifications(params Params, noTLS, noFirebase bool) (*Impl, error) {
return
nil
,
errors
.
WithMessage
(
err
,
"Failed to start instance"
)
}
impl
.
inst
=
i
// Set up firebase messaging client
if
!
noFirebase
{
app
,
err
:=
firebase
.
SetupMessagingApp
(
params
.
FBCreds
)
if
err
!=
nil
{
return
nil
,
errors
.
Wrap
(
err
,
"Failed to setup firebase messaging app"
)
}
impl
.
fcm
=
app
}
return
impl
,
nil
}
...
...
@@ -245,23 +220,6 @@ func (nb *Impl) UnregisterForNotifications(request *pb.NotificationUnregisterReq
return
nil
}
// Update stored NDF and add host for gateway to poll
func
(
nb
*
Impl
)
UpdateNdf
(
ndf
*
ndf
.
NetworkDefinition
)
error
{
gw
:=
ndf
.
Gateways
[
len
(
ndf
.
Gateways
)
-
1
]
var
err
error
nb
.
gwId
,
err
=
id
.
Unmarshal
(
ndf
.
Nodes
[
len
(
ndf
.
Nodes
)
-
1
]
.
ID
)
if
err
!=
nil
{
return
errors
.
WithMessage
(
err
,
"Failed to unmarshal ID"
)
}
_
,
err
=
nb
.
Comms
.
AddHost
(
nb
.
gwId
,
gw
.
Address
,
[]
byte
(
gw
.
TlsCertificate
),
connect
.
GetDefaultHostParams
())
if
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"Failed to add gateway host from NDF"
)
}
nb
.
ndf
=
ndf
return
nil
}
// ReceiveNotificationBatch receives the batch of notification data from gateway.
func
(
nb
*
Impl
)
ReceiveNotificationBatch
(
notifBatch
*
pb
.
NotificationBatch
,
auth
*
connect
.
Auth
)
error
{
if
!
auth
.
IsAuthenticated
{
...
...
This diff is collapsed.
Click to expand it.
notifications/notifications_test.go
+
2
−
39
View file @
bd54c959
...
...
@@ -93,35 +93,17 @@ func TestStartNotifications(t *testing.T) {
Address
:
"0.0.0.0:42010"
,
}
params
.
KeyPath
=
wd
+
"/../testutil/badkey"
n
,
err
:=
StartNotifications
(
params
,
false
,
true
)
if
err
==
nil
||
!
strings
.
Contains
(
err
.
Error
(),
"Failed to parse notifications server key"
)
{
t
.
Errorf
(
"Should have thrown an error bad key"
)
}
params
.
KeyPath
=
wd
+
"/../testutil/cmix.rip.key"
n
,
err
=
StartNotifications
(
params
,
false
,
true
)
_
,
err
=
StartNotifications
(
params
,
false
,
true
)
if
err
==
nil
||
!
strings
.
Contains
(
err
.
Error
(),
"failed to read certificate at"
)
{
t
.
Errorf
(
"Should have thrown an error for no cert path"
)
}
params
.
CertPath
=
wd
+
"/../testutil/badkey"
n
,
err
=
StartNotifications
(
params
,
false
,
true
)
if
err
==
nil
||
!
strings
.
Contains
(
err
.
Error
(),
"Failed to parse notifications server cert"
)
{
t
.
Errorf
(
"Should have thrown an error for bad certificate"
)
}
params
.
CertPath
=
wd
+
"/../testutil/cmix.rip.crt"
n
,
err
=
StartNotifications
(
params
,
false
,
true
)
_
,
err
=
StartNotifications
(
params
,
false
,
true
)
if
err
!=
nil
{
t
.
Errorf
(
"Failed to start notifications successfully: %+v"
,
err
)
}
if
n
.
notificationKey
==
nil
{
t
.
Error
(
"Did not set key"
)
}
if
n
.
notificationCert
==
nil
{
t
.
Errorf
(
"Did not set cert"
)
}
}
// unit test for newimplementation
...
...
@@ -253,25 +235,6 @@ func TestImpl_RegisterForNotifications(t *testing.T) {
}
}
// Unit test that tests to see that updateNDF will in fact update the ndf object inside of IMPL
func
TestImpl_UpdateNdf
(
t
*
testing
.
T
)
{
impl
:=
getNewImpl
()
testNdf
,
err
:=
ndf
.
Unmarshal
([]
byte
(
ExampleNdfJSON
))
if
err
!=
nil
{
t
.
Logf
(
"%+v"
,
err
)
}
err
=
impl
.
UpdateNdf
(
testNdf
)
if
err
!=
nil
{
t
.
Errorf
(
"Failed to update ndf: %+v"
,
err
)
}
if
impl
.
ndf
!=
testNdf
{
t
.
Logf
(
"Failed to change ndf"
)
t
.
Fail
()
}
}
// Unit test for UnregisterForNotifications
func
TestImpl_UnregisterForNotifications
(
t
*
testing
.
T
)
{
impl
:=
getNewImpl
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment