func TestConfig(t *testing.T) { var tests = []struct { filename, protocol, host, port, path string }{ {utils.GetUserHome() + "/.mdfs/stnode/.stnode_conf.json", "tcp", "localhost", "8081", utils.GetUserHome() + "/.mdfs/stnode/"}, {utils.GetUserHome() + "/.mdfs/mdservice/.mdservice_conf.json", "tcp", "localhost", "1994", utils.GetUserHome() + "/.mdfs/mdservice/"}, } for _, c := range tests { got := config.ParseConfiguration(c.filename) if got.Path != c.path || got.Host != c.host || got.Port != c.port || got.Protocol != c.protocol { t.Error("Configuration variables does not contain expected values\n") fmt.Printf("%s,%s,%s,%s,%s,%s,%s,%s\n", got.Path, c.path, got.Host, c.host, got.Port, c.port, got.Protocol, c.protocol) } } }
func TestHash(t *testing.T) { var tests = []struct { path, filename string want bool }{ {utils.GetUserHome() + "/.testing_files/", "test.txt", true}, {utils.GetUserHome() + "/.testing_files/", "test.jpg", true}, {utils.GetUserHome() + "/.testing_files/", "testing", false}, } for _, c := range tests { got := utils.CheckForHash(c.path, c.filename) if got != c.want { t.Errorf("CheckForHash(%q, %q) == %t, want %t", c.path, c.filename, got, c.want) } } }
func (st *StorageNode) setUnid(unid string) (err error) { st.conf.Unid = unid err = config.SetConfiguration(st.conf, utils.GetUserHome()+"/.mdfs/stnode/.stnode_conf.json") if err != nil { return err } return err }
func main() { err := os.MkdirAll(utils.GetUserHome()+"/.testing_files/", 0777) if err != nil { panic(err) } fo, err := os.Create(utils.GetUserHome() + "/.testing_files/test.txt") if err != nil { panic(err) } fi, err := os.Open("./testing_files/test.txt") if err != nil { panic(err) } _, err = io.Copy(fo, fi) if err != nil { panic(err) } fi.Close() fo.Close() fo, err = os.Create(utils.GetUserHome() + "/.testing_files/test.jpg") if err != nil { panic(err) } fi, err = os.Open("./testing_files/test.jpg") if err != nil { panic(err) } _, err = io.Copy(fo, fi) if err != nil { panic(err) } fi.Close() fo.Close() }
// for testing setup func main() { err := os.MkdirAll(utils.GetUserHome()+"/.mdfs/mdservice", 0700) if err != nil { panic(err) } err = os.MkdirAll(utils.GetUserHome()+"/.mdfs/mdservice/files/", 0700) if err != nil { panic(err) } conf := config.ParseConfiguration("./mdservice/config/mdservice_conf.json") conf.Path = utils.GetUserHome() + "/.mdfs/mdservice/" // save the new configuration to file err = config.SetConfiguration(conf, conf.Path+"/.mdservice_conf.json") if err != nil { panic(err) } }
func request(r *bufio.Reader, w *bufio.Writer, currentDir string, args []string, thisUser *utils.User) (err error) { // should have args format: // request [remote filename] [local filename] // currently: // request [remote filename] // Local filename can be a relative or absolute path if len(args) < 2 { return err } // START SENDCODE BLOCK err = w.WriteByte(5) w.Flush() if err != nil { panic(err) } // END SENDCODE BLOCK // Send current dir w.WriteString(currentDir + "\n") w.Flush() // Format the file to send w.WriteString(args[1] + "\n") w.Flush() fmt.Println("Requesting: " + path.Join(currentDir, args[1])) success, _ := r.ReadByte() if success == 3 { fmt.Println("You do not have permission to request this file") return nil } else if success != 4 { fmt.Println("Invalid file request") return nil } protected := false enc, _ := r.ReadByte() if enc == 1 { protected = true } success, _ = r.ReadByte() if success != 2 { fmt.Println("No stnodes for your file") return nil } hash, _ := r.ReadString('\n') protocol, _ := r.ReadString('\n') nAddress, _ := r.ReadString('\n') hash = strings.TrimSpace(hash) protocol = strings.TrimSpace(protocol) nAddress = strings.TrimSpace(nAddress) conn, err := net.Dial(protocol, nAddress) if err != nil { fmt.Println("Error connecting to stnode") } ws := bufio.NewWriter(conn) rs := bufio.NewReader(conn) ws.WriteByte(1) bytehash, err := hex.DecodeString(hash) if err != nil { return err } err = utils.WriteHash(ws, bytehash) if err != nil { return err } success, _ = rs.ReadByte() if success != 3 { fmt.Println("File cannot be found on stnode") return err } output := utils.GetUserHome() + "/.mdfs/client/" + thisUser.Uname + "/files/" + path.Base(args[1]) if protected { encrypFile := os.TempDir() + "/" + path.Base(args[1]) utils.ReceiveFile(conn, rs, encrypFile) err = utils.DecryptFile(encrypFile, output, *thisUser) if err != nil { fmt.Println("Your key does not match the lock for this file") return nil } fmt.Println("Protected file successfully unlocked") } else { utils.ReceiveFile(conn, rs, output) } fmt.Println("Successfully received file") fmt.Println("File stored at: " + output) return }
func setup(r *bufio.Reader, w *bufio.Writer, thisUser *utils.User) (err error) { fmt.Println("Please enter your username:\n") fmt.Print("Username: "******"/.mdfs/client/" + uname + "/.user_data") if exists != nil { // not exist // Make sure the local user dir exists err := os.MkdirAll(utils.GetUserHome()+"/.mdfs/client/"+uname+"/files", 0777) if err != nil { return err } // notify mdservice that this is a new user (SENDCODE 10) err = w.WriteByte(10) // if err != nil { return err } w.Flush() // local user setup utils.GenUserKeys(utils.GetUserHome() + "/.mdfs/client/" + uname + "/.private_key") err = utils.FileToStruct(utils.GetUserHome()+"/.mdfs/client/"+uname+"/.private_key", &thisUser.Privkey) if err != nil { return err } thisUser.Pubkey = &thisUser.Privkey.PublicKey // send username and keys w.WriteString(uname + "\n") w.Write([]byte(thisUser.Pubkey.N.String() + "\n")) w.Write([]byte(strconv.Itoa(thisUser.Pubkey.E) + "\n")) w.Flush() uuid, _ := r.ReadString('\n') thisUser.Uuid, err = strconv.ParseUint(strings.TrimSpace(uuid), 10, 64) if err != nil { return err } err = utils.StructToFile(*thisUser, utils.GetUserHome()+"/.mdfs/client/"+uname+"/.user_data") if err != nil { return err } //NOTE: NOT COMPLETE } else { err = utils.FileToStruct(utils.GetUserHome()+"/.mdfs/client/"+uname+"/.user_data", &thisUser) w.WriteByte(9) w.Flush() } return err // if none exist, will send a code to mdserv to notify as new user, // and get a uuid, create userkeys, send pubkey to mdserv }
func main() { fmt.Println("This is the installation program for the MDFS") fmt.Println("----------------------------------------------------------") fmt.Println("Please select which software you would like to install") fmt.Println("1) Storage node software") fmt.Println("2) Metadata service") // get user selection reader := bufio.NewReader(os.Stdin) fmt.Print("\nEnter selection: ") selection, _ := reader.ReadString('\n') fmt.Println("----------------------------------------------------------") selection = strings.TrimSpace(selection) home := utils.GetUserHome() switch selection { case "1": // configure storage node path := home + "/.mdfs/stnode/" port := getPort(reader) host := getHost(reader) mdhost := getMdHost(reader) mdport := getMdPort(reader) err := setup(path, port, host, mdport, mdhost, "./storagenode/config/", "stnode_conf.json") if err != nil { panic(err) } // create a subdirectory called files err = os.MkdirAll(path+"./files", 0700) if err != nil { panic(err) } fmt.Println("Storage node has been initialised.") fmt.Printf("The configuration file can be found at %s.stnode_conf.json.\n", path) case "2": // configure metadata service path := home + "/.mdfs/mdservice/" port := getPort(reader) host := getHost(reader) err := setup(path, port, host, "", "", "./mdservice/config/", "mdservice_conf.json") if err != nil { panic(err) } // create a subdirectory called files err = os.MkdirAll(path+"./files", 0700) if err != nil { panic(err) } fmt.Println("Metadata service has been initialised.") fmt.Printf("The configuration file can be found at %s/.mdservice_conf.json.\n", path) default: fmt.Println("Invalid Selection.") } }
func CheckCrypto() (success bool, err error) { usrHome := utils.GetUserHome() path := "/.testing_files/" keys := usrHome + path + ".private_key_mdfs" source1 := usrHome + path + "test.txt" encryp1 := usrHome + path + "test.enc" result1 := usrHome + path + "result.txt" source2 := usrHome + path + "test.jpg" encryp2 := usrHome + path + "test.jpg.enc" result2 := usrHome + path + "result.jpg" utils.GenUserKeys(keys) var prk *rsa.PrivateKey var puk *rsa.PublicKey err = utils.FileToStruct(keys, &prk) if err != nil { return false, err } puk = &prk.PublicKey user1 := utils.User{Uuid: 1, Pubkey: puk, Privkey: prk} user2 := utils.User{Uuid: 2, Pubkey: puk, Privkey: prk} //test two files for encryption and then decryption // Test 1st file users := []utils.User{user1, user2} err = utils.EncryptFile(source1, encryp1, users...) if err != nil { return false, err } err = utils.DecryptFile(encryp1, result1, user1) if err != nil { return false, err } err = utils.DecryptFile(encryp1, result1, user2) if err != nil { return false, err } test1 := compareFiles(source1, result1) // Test 2nd file err = utils.EncryptFile(source2, encryp2, user1) if err != nil { return false, err } err = utils.DecryptFile(encryp2, result2, user1) if err != nil { return false, err } test2 := compareFiles(source2, result2) return test1 && test2, err }
// StorageNode methods func (st *StorageNode) parseConfig() { st.conf = config.ParseConfiguration(utils.GetUserHome() + "/.mdfs/stnode/.stnode_conf.json") }
// MDService methods // initialise its memeber variable with values from config file func (md *MDService) parseConfig() { md.conf = config.ParseConfiguration(utils.GetUserHome() + "/.mdfs/mdservice/.mdservice_conf.json") }