Skip to content

Commit

Permalink
Fix SSH zero address OTP delete (#6390)
Browse files Browse the repository at this point in the history
* Fix SSH zero address OTP delete

Fixed bug where SSH OTP roles could not be deleted if a zero-address role
previously existed, and there currently exist no zero-address roles.

Fixes #6382

* Eliminate zeroAddressRoles remove function
  • Loading branch information
mbamber authored and briankassouf committed Mar 14, 2019
1 parent 4c243a5 commit cd0c36f
Showing 1 changed file with 3 additions and 34 deletions.
37 changes: 3 additions & 34 deletions builtin/logical/ssh/path_config_zeroaddress.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"

"github.com/hashicorp/vault/helper/strutil"

"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
Expand Down Expand Up @@ -125,44 +127,11 @@ func (b *backend) removeZeroAddressRole(ctx context.Context, s logical.Storage,
return nil
}

err = zeroAddressEntry.remove(roleName)
if err != nil {
return err
}
zeroAddressEntry.Roles = strutil.StrListDelete(zeroAddressEntry.Roles, roleName)

return b.putZeroAddressRoles(ctx, s, zeroAddressEntry.Roles)
}

// Removes a given role from the comma separated string
func (r *zeroAddressRoles) remove(roleName string) error {
var index int
for i, role := range r.Roles {
if role == roleName {
index = i
break
}
}
length := len(r.Roles)
if index >= length || index < 0 {
return fmt.Errorf("invalid index %d", index)
}
// If slice has zero or one item, remove the item by setting slice to nil.
if length < 2 {
r.Roles = nil
return nil
}

// Last item to be deleted
if length-1 == index {
r.Roles = r.Roles[:length-1]
return nil
}

// Delete the item by appending all items except the one at index
r.Roles = append(r.Roles[:index], r.Roles[index+1:]...)
return nil
}

const pathConfigZeroAddressSyn = `
Assign zero address as default CIDR block for select roles.
`
Expand Down

0 comments on commit cd0c36f

Please sign in to comment.