mirror of
https://github.com/cloudflare/redoctober.git
synced 2026-09-05 07:37:11 +00:00
Add purge command to clear delegations. Closes #48.
This commit is contained in:
+14
-9
@@ -259,22 +259,27 @@ func Summary(jsonIn []byte) ([]byte, error) {
|
||||
// Purge processes a delegation purge request.
|
||||
func Purge(jsonIn []byte) ([]byte, error) {
|
||||
var s PurgeRequest
|
||||
if err := json.Unmarshal(jsonIn, &s); err != nil {
|
||||
var err error
|
||||
|
||||
defer func() {
|
||||
if err != nil {
|
||||
log.Printf("core.purge failed: user=%s %v", s.Name, err)
|
||||
} else {
|
||||
log.Printf("core.purge success: user=%s", s.Name)
|
||||
}
|
||||
}()
|
||||
|
||||
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)
|
||||
err = errors.New("vault has not been created")
|
||||
return jsonStatusError(err)
|
||||
}
|
||||
|
||||
if err := records.DeleteNonAdmin(); err != nil {
|
||||
log.Printf("failed to purge non-admin delegates: %s", err)
|
||||
// Validate the Name and Password as valid and admin
|
||||
if err = validateUser(s.Name, s.Password, true); err != nil {
|
||||
return jsonStatusError(err)
|
||||
}
|
||||
|
||||
|
||||
+11
-2
@@ -163,6 +163,10 @@ func TestSummary(t *testing.T) {
|
||||
}
|
||||
|
||||
var s1 SummaryData
|
||||
delegations := cache.GetSummary()
|
||||
if len(delegations) == 0 {
|
||||
t.Fatal("no delegations active")
|
||||
}
|
||||
|
||||
// check for summary of initialized vault without non-admin members after purge
|
||||
respJson, err = Purge(createJson)
|
||||
@@ -201,8 +205,13 @@ func TestSummary(t *testing.T) {
|
||||
}
|
||||
|
||||
_, ok = s1.All["Bob"]
|
||||
if ok {
|
||||
t.Fatalf("Error in summary of account, record not purged")
|
||||
if !ok {
|
||||
t.Fatal("Bob was removed from the list of users")
|
||||
}
|
||||
|
||||
delegations = cache.GetSummary()
|
||||
if len(delegations) != 0 {
|
||||
t.Fatalf("purge failed to clear delegations (%d delegations remain)", len(delegations))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -434,16 +434,6 @@ 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 {
|
||||
|
||||
Reference in New Issue
Block a user