Skip to content

Commit

Permalink
Use Trim to only shorten the GUN if it contains a leading or trailing…
Browse files Browse the repository at this point in the history
… slash

Also updates a test to fix the expected vs actual values.

Signed-off-by: Riyaz Faizullabhoy <[email protected]>
  • Loading branch information
riyazdf committed Dec 23, 2016
1 parent e754a31 commit ba2f421
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
13 changes: 7 additions & 6 deletions utils/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"encoding/pem"
"errors"
"fmt"
"github.com/Sirupsen/logrus"
"github.com/docker/notary"
tufdata "github.com/docker/notary/tuf/data"
"github.com/docker/notary/tuf/utils"
"io"
"io/ioutil"
"path/filepath"
"sort"
"strings"

"github.com/Sirupsen/logrus"
"github.com/docker/notary"
tufdata "github.com/docker/notary/tuf/data"
"github.com/docker/notary/tuf/utils"
)

// Exporter is a simple interface for the two functions we need from the Storage interface
Expand Down Expand Up @@ -110,8 +111,8 @@ func ImportKeys(from io.Reader, to []Importer, fallbackRole string, fallbackGun
if rawPath := block.Headers["path"]; rawPath != "" {
pathWOFileName := strings.TrimSuffix(rawPath, filepath.Base(rawPath))
if strings.HasPrefix(pathWOFileName, notary.NonRootKeysSubdir) {
gunName := strings.TrimPrefix(pathWOFileName, notary.NonRootKeysSubdir)
gunName = gunName[1:(len(gunName) - 1)] // remove the slashes
// remove the notary keystore-specific segment of the path, and any potential leading or trailing slashes
gunName := strings.Trim(strings.TrimPrefix(pathWOFileName, notary.NonRootKeysSubdir), "/")
if gunName != "" {
block.Headers["gun"] = gunName
}
Expand Down
13 changes: 7 additions & 6 deletions utils/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"crypto/rand"
"encoding/pem"
"errors"
"github.com/docker/notary"
"github.com/docker/notary/tuf/data"
"github.com/docker/notary/tuf/utils"
"github.com/stretchr/testify/require"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/docker/notary"
"github.com/docker/notary/tuf/data"
"github.com/docker/notary/tuf/utils"
"github.com/stretchr/testify/require"
)

const cannedPassphrase = "passphrase"
Expand Down Expand Up @@ -363,8 +364,8 @@ func TestBlockHeaderPrecedenceGunFromPath(t *testing.T) {
require.Equal(t, key, filepath.Join(notary.NonRootKeysSubdir, "anothergun", "12ba0e0a8e05e177bc2c3489bdb6d28836879469f078e68a4812fc8a2d521497"))
final, rest := pem.Decode(s.data[key])
require.Len(t, rest, 0)
require.Equal(t, final.Headers["role"], "snapshot")
require.Equal(t, final.Headers["gun"], "anothergun")
require.Equal(t, "snapshot", final.Headers["role"])
require.Equal(t, "anothergun", final.Headers["gun"])
}
}

Expand Down

0 comments on commit ba2f421

Please sign in to comment.