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 upgrade error #31

Merged
merged 2 commits into from
Apr 16, 2019
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions path_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"net/http"
"strings"
"time"

"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -65,6 +66,13 @@ func (b *versionedKVBackend) dataExistenceCheck() framework.ExistenceFunc {

meta, err := b.getKeyMetadata(ctx, req.Storage, key)
if err != nil {
// If we are returning a readonly error it means we are attempting
// to write the policy for the first time. This means no data exists
// yet and we can safely return false here.
if strings.Contains(err.Error(), logical.ErrReadOnly.Error()) {
return false, nil
}

return false, err
}

Expand Down
8 changes: 8 additions & 0 deletions path_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kv
import (
"context"
"fmt"
"strings"

"github.com/golang/protobuf/ptypes"
"github.com/hashicorp/vault/sdk/framework"
Expand Down Expand Up @@ -54,6 +55,13 @@ func (b *versionedKVBackend) metadataExistenceCheck() framework.ExistenceFunc {

meta, err := b.getKeyMetadata(ctx, req.Storage, key)
if err != nil {
// If we are returning a readonly error it means we are attempting
// to write the policy for the first time. This means no data exists
// yet and we can safely return false here.
if strings.Contains(err.Error(), logical.ErrReadOnly.Error()) {
return false, nil
}

return false, err
}

Expand Down
44 changes: 2 additions & 42 deletions upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ import (

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/helper/consts"
"github.com/hashicorp/vault/sdk/helper/keysutil"
"github.com/hashicorp/vault/sdk/helper/locksutil"
"github.com/hashicorp/vault/sdk/helper/pluginutil"
"github.com/hashicorp/vault/sdk/logical"
Expand Down Expand Up @@ -65,22 +63,7 @@ func (b *versionedKVBackend) upgradeDone(ctx context.Context, s logical.Storage)
}
}

if !upgradeInfo.Done {
return false, nil
}

// Also make sure the policy is found. This is created on first call to
// policy() but if that happens to be a secondary you get a readonly
// storage error -- not nice UX.
policy, err := keysutil.LoadPolicy(ctx, s, path.Join(b.storagePrefix, "policy/metadata"))
if err != nil {
return false, err
}
if policy == nil {
return false, nil
}

return true, nil
return upgradeInfo.Done, nil
}

func (b *versionedKVBackend) Upgrade(ctx context.Context, s logical.Storage) error {
Expand Down Expand Up @@ -135,30 +118,7 @@ func (b *versionedKVBackend) Upgrade(ctx context.Context, s logical.Storage) err
return nil
}

// See if we're already done, in which case we just need to ensure the
// policy was created
upgradeEntry, err := s.Get(ctx, path.Join(b.storagePrefix, "upgrading"))
if err != nil {
return err
}

upgradeInfo := new(UpgradeInfo)
if upgradeEntry != nil {
err := proto.Unmarshal(upgradeEntry.Value, upgradeInfo)
if err != nil {
return err
}
}

if upgradeInfo.Done {
// Just synchronously call policy
if _, err = b.policy(ctx, s); err != nil {
return errwrap.Wrapf("upgrade done but error checking/creating policy: {{err}}", err)
}
return nil
}

upgradeInfo = &UpgradeInfo{
upgradeInfo := &UpgradeInfo{
StartedTime: ptypes.TimestampNow(),
}

Expand Down