From 3ca3a3e172c8a12f8636680a9869c1c4fedb56f0 Mon Sep 17 00:00:00 2001 From: afeiszli Date: Mon, 5 Apr 2021 18:09:21 -0400 Subject: [PATCH] peer update functionality --- controllers/common.go | 11 ++- controllers/groupHttpController.go | 55 ++++++++++++++ controllers/nodeGrpcController.go | 1 + grpc/node.pb.go | 113 ++++++++++++++++------------- grpc/node.proto | 1 + models/group.go | 1 + models/node.go | 1 + models/structs.go | 1 + netclient/functions/common.go | 87 +++++++++++++++++++--- 9 files changed, 207 insertions(+), 64 deletions(-) diff --git a/controllers/common.go b/controllers/common.go index b4f3fb8d..5a6c9866 100644 --- a/controllers/common.go +++ b/controllers/common.go @@ -175,6 +175,7 @@ func UpdateNode(nodechange models.Node, node models.Node) (models.Node, error) { } if nodechange.PublicKey != "" { node.PublicKey = nodechange.PublicKey + node.KeyUpdateTimeStamp = time.Now().Unix() notifygroup = true } @@ -195,6 +196,7 @@ func UpdateNode(nodechange models.Node, node models.Node) (models.Node, error) { {"password", node.Password}, {"listenport", node.ListenPort}, {"publickey", node.PublicKey}, + {"keyupdatetimestamp", node.KeyUpdateTimeStamp}, {"endpoint", node.Endpoint}, {"postup", node.PostUp}, {"preup", node.PreUp}, @@ -307,7 +309,7 @@ func CreateNode(node models.Node, groupName string) (models.Node, error) { node.SetDefaultName() node.SetLastCheckIn() node.SetLastPeerUpdate() - + node.KeyUpdateTimeStamp = time.Now().Unix() //Create a JWT for the node tokenString, _ := functions.CreateJWT(node.MacAddress, groupName) @@ -366,7 +368,9 @@ func NodeCheckIn(node models.Node, groupName string) (models.CheckInResponse, er grouplm := parentgroup.GroupLastModified peerslm := parentgroup.NodesLastModified - peerlistlm := parentnode.LastPeerUpdate + gkeyupdate := parentgroup.KeyUpdateTimeStamp + nkeyupdate := parentnode.KeyUpdateTimeStamp + peerlistlm := parentnode.LastPeerUpdate parentnodelm := parentnode.LastModified parentnodelastcheckin := parentnode.LastCheckIn @@ -380,6 +384,9 @@ func NodeCheckIn(node models.Node, groupName string) (models.CheckInResponse, er if peerlistlm < peerslm { response.NeedPeerUpdate = true } + if nkeyupdate < gkeyupdate { + response.NeedKeyUpdate = true + } /* if postchanges { parentnode, err = UpdateNode(node, parentnode) diff --git a/controllers/groupHttpController.go b/controllers/groupHttpController.go index b95f733d..44c0b980 100644 --- a/controllers/groupHttpController.go +++ b/controllers/groupHttpController.go @@ -21,6 +21,7 @@ import ( func groupHandlers(r *mux.Router) { r.HandleFunc("/api/groups", securityCheck(http.HandlerFunc(getGroups))).Methods("GET") r.HandleFunc("/api/groups", securityCheck(http.HandlerFunc(createGroup))).Methods("POST") + r.HandleFunc("/api/groups/{groupname}/keyupdate", securityCheck(http.HandlerFunc(keyUpdate))).Methods("POST") r.HandleFunc("/api/groups/{groupname}", securityCheck(http.HandlerFunc(getGroup))).Methods("GET") r.HandleFunc("/api/groups/{groupname}/numnodes", securityCheck(http.HandlerFunc(getGroupNodeNumber))).Methods("GET") r.HandleFunc("/api/groups/{groupname}", securityCheck(http.HandlerFunc(updateGroup))).Methods("PUT") @@ -193,6 +194,59 @@ func getGroup(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(group) } +func keyUpdate(w http.ResponseWriter, r *http.Request) { + + w.Header().Set("Content-Type", "application/json") + + var params = mux.Vars(r) + + var group models.Group + + group, err := functions.GetParentGroup(params["groupname"]) + if err != nil { + return + } + + group.KeyUpdateTimeStamp = time.Now().Unix() + + collection := mongoconn.Client.Database("netmaker").Collection("groups") + + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + + filter := bson.M{"nameid": params["groupname"]} + + // prepare update model. + update := bson.D{ + {"$set", bson.D{ + {"addressrange", group.AddressRange}, + {"displayname", group.DisplayName}, + {"defaultlistenport", group.DefaultListenPort}, + {"defaultpostup", group.DefaultPostUp}, + {"defaultpreup", group.DefaultPreUp}, + {"defaultkeepalive", group.DefaultKeepalive}, + {"keyupdatetimestamp", group.KeyUpdateTimeStamp}, + {"defaultsaveconfig", group.DefaultSaveConfig}, + {"defaultinterface", group.DefaultInterface}, + {"nodeslastmodified", group.NodesLastModified}, + {"grouplastmodified", group.GroupLastModified}, + {"allowmanualsignup", group.AllowManualSignUp}, + {"defaultcheckininterval", group.DefaultCheckInInterval}, + }}, + } + + errN := collection.FindOneAndUpdate(ctx, filter, update).Decode(&group) + + defer cancel() + + if errN != nil { + mongoconn.GetError(errN, w) + fmt.Println(errN) + return + } + + json.NewEncoder(w).Encode(group) +} + //Update a group func updateGroup(w http.ResponseWriter, r *http.Request) { @@ -405,6 +459,7 @@ func createGroup(w http.ResponseWriter, r *http.Request) { group.SetDefaults() group.SetNodesLastModified() group.SetGroupLastModified() + group.KeyUpdateTimeStamp = time.Now().Unix() collection := mongoconn.Client.Database("netmaker").Collection("groups") diff --git a/controllers/nodeGrpcController.go b/controllers/nodeGrpcController.go index 32a008e2..04cd2571 100644 --- a/controllers/nodeGrpcController.go +++ b/controllers/nodeGrpcController.go @@ -172,6 +172,7 @@ func (s *NodeServiceServer) CheckIn(ctx context.Context, req *nodepb.CheckInReq) Success: checkinresponse.Success, Needpeerupdate: checkinresponse.NeedPeerUpdate, Needconfigupdate: checkinresponse.NeedConfigUpdate, + Needkeyupdate: checkinresponse.NeedKeyUpdate, Nodemessage: checkinresponse.NodeMessage, Ispending: checkinresponse.IsPending, }, diff --git a/grpc/node.pb.go b/grpc/node.pb.go index 3b25cc21..fdc55209 100644 --- a/grpc/node.pb.go +++ b/grpc/node.pb.go @@ -311,6 +311,7 @@ type CheckInResponse struct { Needconfigupdate bool `protobuf:"varint,3,opt,name=needconfigupdate,proto3" json:"needconfigupdate,omitempty"` Nodemessage string `protobuf:"bytes,4,opt,name=nodemessage,proto3" json:"nodemessage,omitempty"` Ispending bool `protobuf:"varint,5,opt,name=ispending,proto3" json:"ispending,omitempty"` + Needkeyupdate bool `protobuf:"varint,6,opt,name=needkeyupdate,proto3" json:"needkeyupdate,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` @@ -376,6 +377,13 @@ func (m *CheckInResponse) GetIspending() bool { return false } +func (m *CheckInResponse) GetNeedkeyupdate() bool { + if m != nil { + return m.Needkeyupdate + } + return false +} + type PeersResponse struct { Publickey string `protobuf:"bytes,5,opt,name=publickey,proto3" json:"publickey,omitempty"` Endpoint string `protobuf:"bytes,6,opt,name=endpoint,proto3" json:"endpoint,omitempty"` @@ -970,56 +978,57 @@ func init() { func init() { proto.RegisterFile("grpc/node.proto", fileDescriptor_d13bd996b67da4ef) } var fileDescriptor_d13bd996b67da4ef = []byte{ - // 813 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x56, 0x4d, 0x6f, 0xf3, 0x44, - 0x10, 0x56, 0xf2, 0x26, 0x4d, 0x32, 0x69, 0x9a, 0xbe, 0xdb, 0x16, 0xad, 0xac, 0xaa, 0x8a, 0x7c, - 0x40, 0x29, 0xa2, 0x49, 0x29, 0x12, 0xe2, 0x86, 0x44, 0x91, 0x10, 0x08, 0x2a, 0x64, 0xc4, 0x85, - 0xdb, 0xc6, 0x9e, 0xb8, 0x56, 0x9c, 0xdd, 0x8d, 0xd7, 0x4e, 0xd5, 0x5f, 0xc7, 0x89, 0x7f, 0xc4, - 0x91, 0x03, 0xda, 0x5d, 0x3b, 0xfe, 0x68, 0x48, 0xfb, 0xf6, 0x96, 0x79, 0x76, 0xbe, 0xe7, 0x99, - 0x89, 0x61, 0x1c, 0x26, 0xd2, 0x9f, 0x73, 0x11, 0xe0, 0x4c, 0x26, 0x22, 0x15, 0xa4, 0xa3, 0x7f, - 0xbb, 0x3f, 0xc3, 0xf1, 0x2f, 0x22, 0x8c, 0xb8, 0x87, 0x9b, 0x0c, 0x55, 0x4a, 0xae, 0x00, 0xd6, - 0xcc, 0x67, 0x41, 0x90, 0xa0, 0x52, 0xb4, 0x35, 0x69, 0x4d, 0x07, 0x5e, 0x05, 0x21, 0x0e, 0xf4, - 0x25, 0x53, 0xea, 0x49, 0x24, 0x01, 0x6d, 0x9b, 0xd7, 0x9d, 0xec, 0x7e, 0x05, 0xa3, 0xdc, 0x97, - 0x92, 0x82, 0x2b, 0x24, 0x13, 0x18, 0x32, 0xdf, 0x47, 0xa5, 0x52, 0xb1, 0x42, 0x9e, 0x7b, 0xab, - 0x42, 0xee, 0x3f, 0x1d, 0xe8, 0x3c, 0x88, 0x00, 0xc9, 0x09, 0xb4, 0xa3, 0x20, 0xd7, 0x68, 0x47, - 0x01, 0x21, 0xd0, 0xe1, 0x6c, 0x8d, 0x79, 0x0c, 0xf3, 0x9b, 0x50, 0xe8, 0x15, 0x89, 0x7d, 0x30, - 0x70, 0x21, 0xea, 0xac, 0xe3, 0x48, 0xa5, 0xc8, 0xa5, 0x48, 0x52, 0xda, 0x99, 0xb4, 0xa6, 0x5d, - 0xaf, 0x82, 0x90, 0x4b, 0x18, 0xc8, 0x6c, 0x11, 0x47, 0xfe, 0x0a, 0x9f, 0x69, 0xd7, 0xd8, 0x96, - 0x80, 0xae, 0x09, 0x79, 0x20, 0x45, 0xc4, 0x53, 0x7a, 0x64, 0x6b, 0x2a, 0xe4, 0x46, 0x3f, 0x7a, - 0x07, 0xfb, 0xd1, 0xaf, 0xf7, 0x43, 0x47, 0xd5, 0x3d, 0x0e, 0x13, 0x91, 0x49, 0x3a, 0xb0, 0x51, - 0x77, 0x80, 0x7e, 0x8d, 0x94, 0x44, 0x1e, 0x44, 0x3c, 0xa4, 0x30, 0x69, 0x4d, 0xfb, 0x5e, 0x09, - 0x90, 0xcf, 0xe0, 0x48, 0x0a, 0x95, 0x66, 0x92, 0x0e, 0x8d, 0x61, 0x2e, 0x91, 0x73, 0xe8, 0xca, - 0x04, 0x33, 0x49, 0x8f, 0x0d, 0x6c, 0x05, 0xed, 0x6b, 0x85, 0x28, 0x59, 0x1c, 0x6d, 0x91, 0x8e, - 0x4c, 0xf9, 0x25, 0xa0, 0x6b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21, 0xd6, 0x41, 0xf5, 0xaf, 0xd4, 0x39, 0xab, 0x61, 0xf9, 0x96, 0x7e, 0x03, 0x50, 0xd2, 0x97, - 0xe4, 0x2a, 0xb5, 0x0d, 0x70, 0xf6, 0x80, 0x8a, 0xdc, 0x42, 0xbf, 0xa0, 0x07, 0xf9, 0x68, 0x15, - 0x2a, 0x9c, 0x73, 0x5e, 0x40, 0x4a, 0x47, 0x2a, 0x69, 0x5c, 0x44, 0xaa, 0x6d, 0x82, 0xb3, 0x07, - 0x34, 0x76, 0x25, 0x15, 0x0a, 0xbb, 0x1a, 0xd7, 0x9c, 0x3d, 0xa0, 0x22, 0x77, 0xd0, 0x2f, 0x46, - 0x5a, 0x64, 0x58, 0xe1, 0x89, 0xf3, 0x02, 0x52, 0xb7, 0x2d, 0x72, 0x03, 0xbd, 0xbc, 0xe7, 0xe4, - 0xb4, 0x31, 0x82, 0x8d, 0xd3, 0x44, 0xd4, 0xf7, 0xf3, 0x3f, 0x6f, 0x42, 0x21, 0xc2, 0x18, 0x67, - 0xa1, 0x88, 0x19, 0x0f, 0x67, 0x22, 0x09, 0xe7, 0xe6, 0x6b, 0x66, 0x91, 0x2d, 0xe7, 0xe9, 0xb3, - 0x44, 0x35, 0x5f, 0x71, 0xf1, 0xc4, 0xcd, 0x77, 0x8e, 0x5c, 0x2c, 0x8e, 0xcc, 0xe3, 0xd7, 0xff, - 0x05, 0x00, 0x00, 0xff, 0xff, 0x04, 0x8b, 0xcd, 0xc5, 0xfd, 0x08, 0x00, 0x00, + // 827 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x56, 0x5b, 0x6f, 0xe3, 0x44, + 0x14, 0x56, 0xba, 0xb9, 0x9e, 0x34, 0x4d, 0x77, 0xda, 0x45, 0x23, 0x6b, 0xb5, 0x8a, 0x2c, 0x84, + 0xb2, 0x88, 0x26, 0xa5, 0x48, 0x88, 0x37, 0x24, 0x8a, 0x84, 0x40, 0xb0, 0x42, 0x46, 0xbc, 0xf0, + 0x36, 0xb1, 0x4f, 0xbc, 0x56, 0x9c, 0x99, 0x89, 0xc7, 0xce, 0xaa, 0x3f, 0x90, 0x9f, 0xc2, 0x3f, + 0xe0, 0x91, 0x07, 0x34, 0x17, 0xc7, 0x97, 0x86, 0x6c, 0xe9, 0x5b, 0xce, 0x37, 0xe7, 0x7e, 0xbe, + 0x73, 0x62, 0x98, 0xc6, 0x99, 0x0c, 0x97, 0x5c, 0x44, 0xb8, 0x90, 0x99, 0xc8, 0x05, 0xe9, 0xea, + 0xdf, 0xfe, 0x4f, 0x70, 0xfe, 0xb3, 0x88, 0x13, 0x1e, 0xe0, 0xae, 0x40, 0x95, 0x93, 0x37, 0x00, + 0x5b, 0x16, 0xb2, 0x28, 0xca, 0x50, 0x29, 0xda, 0x99, 0x75, 0xe6, 0xa3, 0xa0, 0x86, 0x10, 0x0f, + 0x86, 0x92, 0x29, 0xf5, 0x41, 0x64, 0x11, 0x3d, 0x33, 0xaf, 0x07, 0xd9, 0xff, 0x12, 0x26, 0xce, + 0x97, 0x92, 0x82, 0x2b, 0x24, 0x33, 0x18, 0xb3, 0x30, 0x44, 0xa5, 0x72, 0xb1, 0x41, 0xee, 0xbc, + 0xd5, 0x21, 0xff, 0xef, 0x2e, 0x74, 0xdf, 0x89, 0x08, 0xc9, 0x05, 0x9c, 0x25, 0x91, 0xd3, 0x38, + 0x4b, 0x22, 0x42, 0xa0, 0xcb, 0xd9, 0x16, 0x5d, 0x0c, 0xf3, 0x9b, 0x50, 0x18, 0x94, 0x89, 0xbd, + 0x30, 0x70, 0x29, 0xea, 0xac, 0xd3, 0x44, 0xe5, 0xc8, 0xa5, 0xc8, 0x72, 0xda, 0x9d, 0x75, 0xe6, + 0xbd, 0xa0, 0x86, 0x90, 0xd7, 0x30, 0x92, 0xc5, 0x2a, 0x4d, 0xc2, 0x0d, 0x3e, 0xd0, 0x9e, 0xb1, + 0xad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0x23, 0xea, 0xbb, 0xe5, 0x1f, 0x37, 0xb1, 0x10, 0x71, 0x8a, 0x8b, 0x58, 0xa4, + 0x8c, 0xc7, 0x0b, 0x91, 0xc5, 0x4b, 0xf3, 0xcd, 0xb3, 0x2a, 0xd6, 0xcb, 0xfc, 0x41, 0xa2, 0x5a, + 0x6e, 0xb8, 0xf8, 0xc0, 0xcd, 0xd7, 0x90, 0x5c, 0xad, 0xfa, 0xe6, 0xf1, 0xab, 0x7f, 0x03, 0x00, + 0x00, 0xff, 0xff, 0x32, 0x60, 0x03, 0x3b, 0x23, 0x09, 0x00, 0x00, } diff --git a/grpc/node.proto b/grpc/node.proto index b96e03a5..a0f0bd63 100644 --- a/grpc/node.proto +++ b/grpc/node.proto @@ -49,6 +49,7 @@ message CheckInResponse { bool needconfigupdate = 3; string nodemessage = 4; bool ispending = 5; + bool needkeyupdate = 6; } message PeersResponse { diff --git a/models/group.go b/models/group.go index 4c428dcf..3787a2a3 100644 --- a/models/group.go +++ b/models/group.go @@ -19,6 +19,7 @@ type Group struct { DefaultListenPort int32 `json:"defaultlistenport,omitempty" bson:"defaultlistenport,omitempty" validate:"omitempty,numeric,min=1024,max=65535"` DefaultPostUp string `json:"defaultpostup" bson:"defaultpostup"` DefaultPreUp string `json:"defaultpreup" bson:"defaultpreup"` + KeyUpdateTimeStamp int64 `json:"keyupdatetimestamp" bson:"keyupdatetimestamp"` DefaultKeepalive int32 `json:"defaultkeepalive" bson:"defaultkeepalive" validate: "omitempty,numeric,max=1000"` DefaultSaveConfig *bool `json:"defaultsaveconfig" bson:"defaultsaveconfig"` AccessKeys []AccessKey `json:"accesskeys" bson:"accesskeys"` diff --git a/models/node.go b/models/node.go index ffb4a79e..0f5d6406 100644 --- a/models/node.go +++ b/models/node.go @@ -31,6 +31,7 @@ type Node struct { AccessKey string `json:"accesskey" bson:"accesskey"` Interface string `json:"interface" bson:"interface"` LastModified int64 `json:"lastmodified" bson:"lastmodified"` + KeyUpdateTimeStamp int64 `json:"keyupdatetimestamp" bson:"keyupdatetimestamp"` LastPeerUpdate int64 `json:"lastpeerupdate" bson:"lastpeerupdate"` LastCheckIn int64 `json:"lastcheckin" bson:"lastcheckin"` MacAddress string `json:"macaddress" bson:"macaddress" validate:"required,macaddress_valid,macaddress_unique"` diff --git a/models/structs.go b/models/structs.go index 7a7ce792..61d3cd5c 100644 --- a/models/structs.go +++ b/models/structs.go @@ -84,6 +84,7 @@ type CheckInResponse struct{ Success bool `json:"success" bson:"success"` NeedPeerUpdate bool `json:"needpeerupdate" bson:"needpeerupdate"` NeedConfigUpdate bool `json:"needconfigupdate" bson:"needconfigupdate"` + NeedKeyUpdate bool `json:"needkeyupdate" bson:"needkeyupdate"` NodeMessage string `json:"nodemessage" bson:"nodemessage"` IsPending bool `json:"ispending" bson:"ispending"` } diff --git a/netclient/functions/common.go b/netclient/functions/common.go index b54c27c2..73859671 100644 --- a/netclient/functions/common.go +++ b/netclient/functions/common.go @@ -440,15 +440,7 @@ func getMacAddr() ([]string, error) { } return as, nil } -/* -func read(macaddress string, group string) error { - //this would be used for retrieving state as set by the server. -} -func checkLocalConfigChange() error { - -} -*/ func initWireguard(node *nodepb.Node, privkey string, peers []wgtypes.PeerConfig) error { @@ -586,6 +578,71 @@ func initWireguard(node *nodepb.Node, privkey string, peers []wgtypes.PeerConfig return err } +func setWGKeyConfig(network string, serveraddr string) error { + + ctx := context.Background() + var header metadata.MD + + var wcclient nodepb.NodeServiceClient + var requestOpts grpc.DialOption + requestOpts = grpc.WithInsecure() + conn, err := grpc.Dial(serveraddr, requestOpts) + if err != nil { + fmt.Printf("Cant dial GRPC server: %v", err) + return err + } + wcclient = nodepb.NewNodeServiceClient(conn) + + fmt.Println("Authenticating with GRPC Server") + ctx, err = SetJWT(wcclient, network) + if err != nil { + fmt.Printf("Failed to authenticate: %v", err) + return err + } + fmt.Println("Authenticated") + + + node := getNode(network) + + privatekey, err := wgtypes.GeneratePrivateKey() + if err != nil { + return err + } + privkeystring := privatekey.String() + publickey := privatekey.PublicKey() + + node.Publickey = publickey.String() + + err = storePrivKey(privkeystring) + if err != nil { + return err + } + err = modConfig(&node) + if err != nil { + return err + } + + + postnode := getNode(network) + + req := &nodepb.UpdateNodeReq{ + Node: &postnode, + } + + _, err = wcclient.UpdateNode(ctx, req, grpc.Header(&header)) + if err != nil { + return err + } + err = setWGConfig(network) + if err != nil { + return err + log.Fatalf("Error: %v", err) + } + + return err +} + + func setWGConfig(network string) error { cfg, err := config.ReadConfig(network) @@ -615,12 +672,12 @@ func setWGConfig(network string) error { func storePrivKey(key string) error{ d1 := []byte(key) - err := ioutil.WriteFile("/root/.wckey", d1, 0644) + err := ioutil.WriteFile("/etc/netclient/wgkey", d1, 0644) return err } func retrievePrivKey() (string, error) { - dat, err := ioutil.ReadFile("/root/.wckey") + dat, err := ioutil.ReadFile("/etc/netclient/wgkey") return string(dat), err } @@ -852,6 +909,16 @@ func CheckIn(network string) error { } setupcheck = false } + if checkinres.Checkinresponse.Needkeyupdate { + fmt.Println("Server has requested that node update key pairs.") + fmt.Println("Proceeding to re-generate key pairs for Wiregard.") + err = setWGKeyConfig(network, servercfg.Address) + if err != nil { + return err + log.Fatalf("Unable to process reset keys request: %v", err) + } + setupcheck = false + } if checkinres.Checkinresponse.Needpeerupdate { fmt.Println("Server has requested that node update peer list.") fmt.Println("Updating peer list from remote server.")