Implement purge action to remove all delegates, closes #48

This commit is contained in:
Giulio Iotti
2015-07-20 23:48:16 -07:00
committed by Kyle Isom
parent b5f83e9061
commit 6e4957554c
6 changed files with 110 additions and 1 deletions
+9
View File
@@ -216,6 +216,15 @@ Example input JSON format:
-d '{"Name":"Alice","Password":"Lewis","ToModify":"Bill","Command":"admin"}'
{"Status":"ok"}
### Purge
Purge deletes all delegates for an encryption key.
Example input JSON format:
$ curl --cacert cert/server.crt https://localhost:8080/purge \
-d '{"Name":"Alice","Password":"Lewis"}'
{"Status":"ok"}
### Web interface
+15
View File
@@ -144,6 +144,21 @@ func (c *RemoteServer) Delegate(req core.DelegateRequest) (*core.ResponseData, e
return unmarshalResponseData(respBytes)
}
// Purge issues a purge request to the remote server
func (c *RemoteServer) Purge(req core.DelegateRequest) (*core.ResponseData, error) {
reqBytes, err := json.Marshal(req)
if err != nil {
return nil, err
}
respBytes, err := c.doAction("purge", reqBytes)
if err != nil {
return nil, err
}
return unmarshalResponseData(respBytes)
}
// Modify issues a modify request to the remote server
func (c *RemoteServer) Modify(req core.ModifyRequest) (*core.ResponseData, error) {
reqBytes, err := json.Marshal(req)
+31
View File
@@ -36,6 +36,11 @@ type SummaryRequest struct {
Password string
}
type PurgeRequest struct {
Name string
Password string
}
type DelegateRequest struct {
Name string
Password string
@@ -251,6 +256,32 @@ func Summary(jsonIn []byte) ([]byte, error) {
return jsonSummary()
}
// Purge processes a delegation purge request.
func Purge(jsonIn []byte) ([]byte, error) {
var s PurgeRequest
if err := json.Unmarshal(jsonIn, &s); err != nil {
return jsonStatusError(err)
}
if records.NumRecords() == 0 {
return jsonStatusError(errors.New("Vault is not created yet"))
}
// Validate the Name and Password as valid and admin
if err := validateUser(s.Name, s.Password, true); err != nil {
log.Printf("failed to validate %s as admin for purge request: %s", s.Name, err)
return jsonStatusError(err)
}
if err := records.DeleteNonAdmin(); err != nil {
log.Printf("failed to purge non-admin delegates: %s", err)
return jsonStatusError(err)
}
cache.FlushCache()
return jsonStatusOk()
}
// Delegate processes a delegation request.
func Delegate(jsonIn []byte) ([]byte, error) {
var s DelegateRequest
+44 -1
View File
@@ -7,8 +7,8 @@ package core
import (
"bytes"
"encoding/json"
"reflect"
"os"
"reflect"
"sort"
"testing"
@@ -161,6 +161,49 @@ func TestSummary(t *testing.T) {
if dataLive.Type != passvault.DefaultRecordType {
t.Fatalf("Error in summary of account, record missing")
}
var s1 SummaryData
// check for summary of initialized vault without non-admin members after purge
respJson, err = Purge(createJson)
if err != nil {
t.Fatalf("Error in purging, %v", err)
}
err = json.Unmarshal(respJson, &s1)
if err != nil {
t.Fatalf("Error in purging, %v", err)
}
if s.Status != "ok" {
t.Fatalf("Error in purging, %v", s.Status)
}
respJson, err = Summary(createJson)
if err != nil {
t.Fatalf("Error in summary of account with no vault, %v", err)
}
err = json.Unmarshal(respJson, &s1)
if err != nil {
t.Fatalf("Error in summary of account with no vault, %v", err)
}
if s.Status != "ok" {
t.Fatalf("Error in summary of account with no vault, %v", s.Status)
}
data, ok = s1.All["Alice"]
if !ok {
t.Fatalf("Error in summary of account, record missing")
}
if data.Admin != true {
t.Fatalf("Error in summary of account, record missing")
}
if data.Type != passvault.DefaultRecordType {
t.Fatalf("Error in summary of account, record missing")
}
_, ok = s1.All["Bob"]
if ok {
t.Fatalf("Error in summary of account, record not purged")
}
}
func TestPassword(t *testing.T) {
+10
View File
@@ -434,6 +434,16 @@ func (records *Records) DeleteRecord(name string) error {
return errors.New("Record missing")
}
// DeleteNonAdmin removes all record without admin status.
func (records *Records) DeleteNonAdmin() error {
for name, pr := range records.Passwords {
if !pr.IsAdmin() {
delete(records.Passwords, name)
}
}
return records.WriteRecordsToDisk()
}
// RevokeRecord removes admin status from a record.
func (records *Records) RevokeRecord(name string) error {
if rec, ok := records.GetRecord(name); ok {
+1
View File
@@ -29,6 +29,7 @@ import (
var functions = map[string]func([]byte) ([]byte, error){
"/create": core.Create,
"/summary": core.Summary,
"/purge": core.Purge,
"/delegate": core.Delegate,
"/password": core.Password,
"/encrypt": core.Encrypt,