Example #1
0
// Summary processes a summary request.
func Summary(jsonIn []byte) ([]byte, error) {
	var s summary
	keycache.Refresh()

	if err := json.Unmarshal(jsonIn, &s); err != nil {
		return jsonStatusError(err)
	}

	if passvault.NumRecords() == 0 {
		return jsonStatusError(errors.New("Vault is not created yet"))
	}

	if err := validateAdmin(s.Name, s.Password); err != nil {
		log.Printf("Error validating admin status of %s: %s", s.Name, err)
		return jsonStatusError(err)
	}

	return jsonSummary()
}
func TestModify(t *testing.T) {
	summaryJson := []byte("{\"Name\":\"Alice\",\"Password\":\"Hello\"}")
	summaryJson2 := []byte("{\"Name\":\"Carol\",\"Password\":\"Hello\"}")
	delegateJson := []byte("{\"Name\":\"Alice\",\"Password\":\"Hello\",\"Time\":\"0s\",\"Uses\":0}")
	delegateJson2 := []byte("{\"Name\":\"Bob\",\"Password\":\"Hello\",\"Time\":\"0s\",\"Uses\":0}")
	delegateJson3 := []byte("{\"Name\":\"Carol\",\"Password\":\"Hello\",\"Time\":\"0s\",\"Uses\":0}")
	modifyJson := []byte("{\"Name\":\"Alice\",\"Password\":\"Hello\",\"ToModify\":\"Alice\",\"Command\":\"admin\"}")
	modifyJson2 := []byte("{\"Name\":\"Carol\",\"Password\":\"Hello\",\"ToModify\":\"Alice\",\"Command\":\"revoke\"}")
	modifyJson3 := []byte("{\"Name\":\"Alice\",\"Password\":\"Hello\",\"ToModify\":\"Carol\",\"Command\":\"admin\"}")
	modifyJson4 := []byte("{\"Name\":\"Carol\",\"Password\":\"Hello\",\"ToModify\":\"Alice\",\"Command\":\"revoke\"}")
	modifyJson5 := []byte("{\"Name\":\"Carol\",\"Password\":\"Hello\",\"ToModify\":\"Alice\",\"Command\":\"delete\"}")

	os.Remove("/tmp/db1.json")
	Init("/tmp/db1.json")

	// check for summary of initialized vault with new member
	var s responseData
	respJson, err := Create(delegateJson)
	if err != nil {
		t.Fatalf("Error in creating account, %v", err)
	}
	err = json.Unmarshal(respJson, &s)
	if err != nil {
		t.Fatalf("Error in creating account, %v", err)
	}
	if s.Status != "ok" {
		t.Fatalf("Error in creating account, %v", s.Status)
	}

	respJson, err = Delegate(delegateJson2)
	if err != nil {
		t.Fatalf("Error in delegating account, %v", err)
	}
	err = json.Unmarshal(respJson, &s)
	if err != nil {
		t.Fatalf("Error in delegating account, %v", err)
	}
	if s.Status != "ok" {
		t.Fatalf("Error in delegating account, %v", s.Status)
	}

	respJson, err = Delegate(delegateJson3)
	if err != nil {
		t.Fatalf("Error in delegating account, %v", err)
	}
	err = json.Unmarshal(respJson, &s)
	if err != nil {
		t.Fatalf("Error in delegating account, %v", err)
	}
	if s.Status != "ok" {
		t.Fatalf("Error in delegating account, %v", s.Status)
	}

	// check summary to see if none are delegated
	keycache.Refresh()
	respJson, err = Summary(summaryJson)
	if err != nil {
		t.Fatalf("Error in summary, %v", err)
	}
	var sum summaryData
	err = json.Unmarshal(respJson, &sum)
	if err != nil {
		t.Fatalf("Error in summary, %v", err)
	}
	if sum.Status != "ok" {
		t.Fatalf("Error in summary, %v", sum.Status)
	}
	if len(sum.Live) != 0 {
		t.Fatalf("Error in summary, %v", sum.Status)
	}

	// Modify from non-admin (fail)
	respJson, err = Modify(modifyJson)
	if err != nil {
		t.Fatalf("Error in modify, %v", err)
	}
	err = json.Unmarshal(respJson, &s)
	if err != nil {
		t.Fatalf("Error in modify, %v", err)
	}
	if s.Status == "ok" {
		t.Fatalf("Error in modify, %v", s.Status)
	}

	// Modify self from admin (fail)
	respJson, err = Modify(modifyJson2)
	if err != nil {
		t.Fatalf("Error in modify, %v", err)
	}
	err = json.Unmarshal(respJson, &s)
	if err != nil {
		t.Fatalf("Error in modify, %v", err)
	}
	if s.Status == "ok" {
		t.Fatalf("Error in modify, %v", s.Status)
	}

	// Modify admin from admin
	respJson, err = Modify(modifyJson3)
	if err != nil {
		t.Fatalf("Error in modify, %v", err)
	}
	err = json.Unmarshal(respJson, &s)
	if err != nil {
		t.Fatalf("Error in modify, %v", err)
	}
	if s.Status != "ok" {
		t.Fatalf("Error in modify, %v", s.Status)
	}

	respJson, err = Summary(summaryJson)
	if err != nil {
		t.Fatalf("Error in summary, %v", err)
	}
	err = json.Unmarshal(respJson, &sum)
	if err != nil {
		t.Fatalf("Error in summary, %v", err)
	}
	if sum.Status != "ok" {
		t.Fatalf("Error in summary, %v", sum.Status)
	}
	if sum.All["Carol"].Admin != true {
		t.Fatalf("Error in summary, %v", sum.All)
	}

	// Revoke admin from admin
	respJson, err = Modify(modifyJson4)
	if err != nil {
		t.Fatalf("Error in modify, %v", err)
	}
	err = json.Unmarshal(respJson, &s)
	if err != nil {
		t.Fatalf("Error in modify, %v", err)
	}
	if s.Status != "ok" {
		t.Fatalf("Error in modify, %v", s.Status)
	}

	respJson, err = Summary(summaryJson2)
	if err != nil {
		t.Fatalf("Error in summary, %v", err)
	}
	err = json.Unmarshal(respJson, &sum)
	if err != nil {
		t.Fatalf("Error in summary, %v", err)
	}
	if sum.Status != "ok" {
		t.Fatalf("Error in summary, %v", sum.Status)
	}
	if sum.All["Alice"].Admin == true {
		t.Fatalf("Error in summary, %v", sum.All)
	}

	// Delete from admin
	respJson, err = Modify(modifyJson5)
	if err != nil {
		t.Fatalf("Error in modify, %v", err)
	}
	err = json.Unmarshal(respJson, &s)
	if err != nil {
		t.Fatalf("Error in modify, %v", err)
	}
	if s.Status != "ok" {
		t.Fatalf("Error in modify, %v", s.Status)
	}

	var sum3 summaryData
	respJson, err = Summary(summaryJson2)
	if err != nil {
		t.Fatalf("Error in summary, %v", err)
	}
	err = json.Unmarshal(respJson, &sum3)
	if err != nil {
		t.Fatalf("Error in summary, %v", err)
	}
	if sum3.Status != "ok" {
		t.Fatalf("Error in summary, %v", sum3.Status)
	}
	if len(sum3.All) != 2 {
		t.Fatalf("Error in summary, %v", sum3.All)
	}

	keycache.FlushCache()

	os.Remove("/tmp/db1.json")
}
func TestEncryptDecrypt(t *testing.T) {
	summaryJson := []byte("{\"Name\":\"Alice\",\"Password\":\"Hello\"}")
	delegateJson := []byte("{\"Name\":\"Alice\",\"Password\":\"Hello\",\"Time\":\"0s\",\"Uses\":0}")
	delegateJson2 := []byte("{\"Name\":\"Bob\",\"Password\":\"Hello\",\"Time\":\"0s\",\"Uses\":0}")
	delegateJson3 := []byte("{\"Name\":\"Carol\",\"Password\":\"Hello\",\"Time\":\"0s\",\"Uses\":0}")
	delegateJson4 := []byte("{\"Name\":\"Bob\",\"Password\":\"Hello\",\"Time\":\"10s\",\"Uses\":2}")
	delegateJson5 := []byte("{\"Name\":\"Carol\",\"Password\":\"Hello\",\"Time\":\"10s\",\"Uses\":2}")
	encryptJson := []byte("{\"Name\":\"Carol\",\"Password\":\"Hello\",\"Minumum\":2,\"Owners\":[\"Alice\",\"Bob\",\"Carol\"],\"Data\":\"SGVsbG8gSmVsbG8=\"}")
	encryptJson2 := []byte("{\"Name\":\"Alice\",\"Password\":\"Hello\",\"Minumum\":2,\"Owners\":[\"Alice\",\"Bob\",\"Carol\"],\"Data\":\"SGVsbG8gSmVsbG8=\"}")
	os.Remove("/tmp/db1.json")

	Init("/tmp/db1.json")

	// check for summary of initialized vault with new member
	var s responseData
	respJson, err := Create(delegateJson)
	if err != nil {
		t.Fatalf("Error in creating account, %v", err)
	}
	err = json.Unmarshal(respJson, &s)
	if err != nil {
		t.Fatalf("Error in creating account, %v", err)
	}
	if s.Status != "ok" {
		t.Fatalf("Error in creating account, %v", s.Status)
	}

	respJson, err = Delegate(delegateJson2)
	if err != nil {
		t.Fatalf("Error in delegating account, %v", err)
	}
	err = json.Unmarshal(respJson, &s)
	if err != nil {
		t.Fatalf("Error in delegating account, %v", err)
	}
	if s.Status != "ok" {
		t.Fatalf("Error in delegating account, %v", s.Status)
	}

	respJson, err = Delegate(delegateJson3)
	if err != nil {
		t.Fatalf("Error in delegating account, %v", err)
	}
	err = json.Unmarshal(respJson, &s)
	if err != nil {
		t.Fatalf("Error in delegating account, %v", err)
	}
	if s.Status != "ok" {
		t.Fatalf("Error in delegating account, %v", s.Status)
	}

	// check summary to see if none are delegated
	keycache.Refresh()
	respJson, err = Summary(summaryJson)
	if err != nil {
		t.Fatalf("Error in summary, %v", err)
	}
	var sum summaryData
	err = json.Unmarshal(respJson, &sum)
	if err != nil {
		t.Fatalf("Error in summary, %v", err)
	}
	if sum.Status != "ok" {
		t.Fatalf("Error in summary, %v", sum.Status)
	}
	if len(sum.Live) != 0 {
		t.Fatalf("Error in summary, %v", sum.Status)
	}

	// Encrypt with non-admin (fail)
	respJson, err = Encrypt(encryptJson)
	if err != nil {
		t.Fatalf("Error in encrypt, %v", err)
	}
	err = json.Unmarshal(respJson, &s)
	if err != nil {
		t.Fatalf("Error in encrypt, %v", err)
	}
	if s.Status == "ok" {
		t.Fatalf("Error in encrypt, %v", s.Status)
	}

	// Encrypt
	respJson, err = Encrypt(encryptJson2)
	if err != nil {
		t.Fatalf("Error in encrypt, %v", err)
	}
	err = json.Unmarshal(respJson, &s)
	if err != nil {
		t.Fatalf("Error in encrypt, %v", err)
	}
	if s.Status != "ok" {
		t.Fatalf("Error in encrypt, %v", s.Status)
	}

	// decrypt file
	decryptJson, err := json.Marshal(decrypt{Name: "Alice", Password: "******", Data: s.Response})
	if err != nil {
		t.Fatalf("Error in marshalling decryption, %v", err)
	}

	respJson2, err := Decrypt(decryptJson)
	if err != nil {
		t.Fatalf("Error in decrypt, %v", err)
	}
	err = json.Unmarshal(respJson2, &s)
	if err != nil {
		t.Fatalf("Error in decrypt, %v", err)
	}
	if s.Status == "ok" {
		t.Fatalf("Error in decrypt, %v", s.Status)
	}

	// delegate two valid decryptors
	respJson, err = Delegate(delegateJson4)
	if err != nil {
		t.Fatalf("Error in delegating account, %v", err)
	}
	err = json.Unmarshal(respJson, &s)
	if err != nil {
		t.Fatalf("Error in delegating account, %v", err)
	}
	if s.Status != "ok" {
		t.Fatalf("Error in delegating account, %v", s.Status)
	}

	respJson, err = Delegate(delegateJson5)
	if err != nil {
		t.Fatalf("Error in delegating account, %v", err)
	}
	err = json.Unmarshal(respJson, &s)
	if err != nil {
		t.Fatalf("Error in delegating account, %v", err)
	}
	if s.Status != "ok" {
		t.Fatalf("Error in delegating account, %v", s.Status)
	}

	// verify the presence of the two delgations
	keycache.Refresh()
	var sum2 summaryData
	respJson, err = Summary(summaryJson)
	if err != nil {
		t.Fatalf("Error in summary, %v", err)
	}
	err = json.Unmarshal(respJson, &sum2)
	if err != nil {
		t.Fatalf("Error in summary, %v", err)
	}
	if sum2.Status != "ok" {
		t.Fatalf("Error in summary, %v", sum2.Status)
	}
	if len(sum2.Live) != 2 {
		t.Fatalf("Error in summary, %v", sum2.Live)
	}

	respJson2, err = Decrypt(decryptJson)
	if err != nil {
		t.Fatalf("Error in decrypt, %v", err)
	}
	err = json.Unmarshal(respJson2, &s)
	if err != nil {
		t.Fatalf("Error in decrypt, %v", err)
	}
	if s.Status != "ok" {
		t.Fatalf("Error in decrypt, %v", s.Status)
	}
	if string(s.Response) != "Hello Jello" {
		t.Fatalf("Error in decrypt, %v", string(s.Response))
	}

	keycache.FlushCache()

	os.Remove("/tmp/db1.json")
}