Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SSH zero address OTP delete #6390

Merged
merged 2 commits into from
Mar 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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