Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit 9211198

Browse files
authored
Merge pull request #1067 from riyazdf/delegation-key-import-export
Fix delegation key import/export GUN inference logic
2 parents 98ad8fa + fe187a2 commit 9211198

4 files changed

Lines changed: 56 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Changelog
22

3-
## [v0.4.3](https://github.com/docker/notary/releases/tag/v0.4.3) 11/15/2016
3+
## [v0.4.3](https://github.com/docker/notary/releases/tag/v0.4.3) 1/3/2017
44
+ Fix build tags for static notary client binaries in linux [#1039](https://github.com/docker/notary/pull/1039)
5+
+ Fix key import for exported delegation keys [#1067](https://github.com/docker/notary/pull/1067)
56

67
## [v0.4.2](https://github.com/docker/notary/releases/tag/v0.4.2) 9/30/2016
78
+ Bump the cross compiler to golang 1.7.1, since [1.6.3 builds binaries that could have non-deterministic bugs in OS X Sierra](https://groups.google.com/forum/#!msg/golang-dev/Jho5sBHZgAg/cq6d97S1AwAJ) [#984](https://github.com/docker/notary/pull/984)

cmd/notary/integration_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2562,3 +2562,43 @@ func TestExportImportFlow(t *testing.T) {
25622562
require.Contains(t, targString, "gun: gun")
25632563
require.True(t, strings.Contains(snapString, "role: snapshot") || strings.Contains(snapString, "role: target"))
25642564
}
2565+
2566+
// Tests import/export keys with delegations, which don't require a gun
2567+
func TestDelegationKeyImportExport(t *testing.T) {
2568+
// -- setup --
2569+
setUp(t)
2570+
2571+
tempDir := tempDirWithConfig(t, "{}")
2572+
defer os.RemoveAll(tempDir)
2573+
2574+
tempExportedDir := tempDirWithConfig(t, "{}")
2575+
defer os.RemoveAll(tempDir)
2576+
2577+
tempImportingDir := tempDirWithConfig(t, "{}")
2578+
defer os.RemoveAll(tempDir)
2579+
2580+
// Setup key in a file for import
2581+
keyFile, err := ioutil.TempFile("", "pemfile")
2582+
require.NoError(t, err)
2583+
defer os.Remove(keyFile.Name())
2584+
privKey, err := utils.GenerateRSAKey(rand.Reader, 2048)
2585+
require.NoError(t, err)
2586+
pemBytes, err := utils.EncryptPrivateKey(privKey, "", "", "")
2587+
require.NoError(t, err)
2588+
nBytes, err := keyFile.Write(pemBytes)
2589+
require.NoError(t, err)
2590+
keyFile.Close()
2591+
require.Equal(t, len(pemBytes), nBytes)
2592+
2593+
// import the key
2594+
_, err = runCommand(t, tempDir, "key", "import", keyFile.Name(), "-r", "user")
2595+
require.NoError(t, err)
2596+
2597+
// export the key
2598+
_, err = runCommand(t, tempDir, "key", "export", "-o", filepath.Join(tempExportedDir, "exported"))
2599+
require.NoError(t, err)
2600+
2601+
// re-import the key from the exported store to a new tempDir
2602+
_, err = runCommand(t, tempImportingDir, "key", "import", filepath.Join(tempExportedDir, "exported"))
2603+
require.NoError(t, err)
2604+
}

utils/keys.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import (
44
"encoding/pem"
55
"errors"
66
"fmt"
7-
"github.com/Sirupsen/logrus"
8-
"github.com/docker/notary"
9-
tufdata "github.com/docker/notary/tuf/data"
10-
"github.com/docker/notary/tuf/utils"
117
"io"
128
"io/ioutil"
139
"path/filepath"
1410
"sort"
1511
"strings"
12+
13+
"github.com/Sirupsen/logrus"
14+
"github.com/docker/notary"
15+
tufdata "github.com/docker/notary/tuf/data"
16+
"github.com/docker/notary/tuf/utils"
1617
)
1718

1819
// Exporter is a simple interface for the two functions we need from the Storage interface
@@ -110,8 +111,8 @@ func ImportKeys(from io.Reader, to []Importer, fallbackRole string, fallbackGun
110111
if rawPath := block.Headers["path"]; rawPath != "" {
111112
pathWOFileName := strings.TrimSuffix(rawPath, filepath.Base(rawPath))
112113
if strings.HasPrefix(pathWOFileName, notary.NonRootKeysSubdir) {
113-
gunName := strings.TrimPrefix(pathWOFileName, notary.NonRootKeysSubdir)
114-
gunName = gunName[1:(len(gunName) - 1)] // remove the slashes
114+
// remove the notary keystore-specific segment of the path, and any potential leading or trailing slashes
115+
gunName := strings.Trim(strings.TrimPrefix(pathWOFileName, notary.NonRootKeysSubdir), "/")
115116
if gunName != "" {
116117
block.Headers["gun"] = gunName
117118
}

utils/keys_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import (
55
"crypto/rand"
66
"encoding/pem"
77
"errors"
8-
"github.com/docker/notary"
9-
"github.com/docker/notary/tuf/data"
10-
"github.com/docker/notary/tuf/utils"
11-
"github.com/stretchr/testify/require"
128
"io/ioutil"
139
"os"
1410
"path/filepath"
1511
"testing"
12+
13+
"github.com/docker/notary"
14+
"github.com/docker/notary/tuf/data"
15+
"github.com/docker/notary/tuf/utils"
16+
"github.com/stretchr/testify/require"
1617
)
1718

1819
const cannedPassphrase = "passphrase"
@@ -363,8 +364,8 @@ func TestBlockHeaderPrecedenceGunFromPath(t *testing.T) {
363364
require.Equal(t, key, filepath.Join(notary.NonRootKeysSubdir, "anothergun", "12ba0e0a8e05e177bc2c3489bdb6d28836879469f078e68a4812fc8a2d521497"))
364365
final, rest := pem.Decode(s.data[key])
365366
require.Len(t, rest, 0)
366-
require.Equal(t, final.Headers["role"], "snapshot")
367-
require.Equal(t, final.Headers["gun"], "anothergun")
367+
require.Equal(t, "snapshot", final.Headers["role"])
368+
require.Equal(t, "anothergun", final.Headers["gun"])
368369
}
369370
}
370371

0 commit comments

Comments
 (0)