Skip to content

Commit

Permalink
addressed comments- made a bunch of fixes and added tests- main ones …
Browse files Browse the repository at this point in the history
…being encryption tests and export import testflow test

Signed-off-by: Avi Vaid <[email protected]>
  • Loading branch information
avaid96 committed Aug 9, 2016
1 parent cc2ef1d commit 6dbafff
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 92 deletions.
9 changes: 3 additions & 6 deletions cmd/notary/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -935,10 +935,6 @@ func TestClientDelegationsPublishing(t *testing.T) {
require.NoError(t, err)
require.Contains(t, output, "No delegations present in this repository.")

// publish repo
_, err = runCommand(t, tempDir, "-s", server.URL, "publish", "gun")
require.NoError(t, err)

// validate that we have all keys, including snapshot
assertNumKeys(t, tempDir, 1, 2, true)

Expand Down Expand Up @@ -1149,7 +1145,7 @@ func getUniqueKeys(t *testing.T, tempDir string) ([]string, []string) {
placeToGo map[string]bool
keyID string
)
if strings.TrimSpace(parts[0]) == "root" {
if strings.TrimSpace(parts[0]) == data.CanonicalRootRole {
// no gun, so there are only 3 fields
placeToGo, keyID = rootMap, parts[1]
} else {
Expand Down Expand Up @@ -2482,6 +2478,7 @@ func TestExportImportFlow(t *testing.T) {
snapBytes, _ := ioutil.ReadAll(snapKey)
snapString := string(snapBytes)
require.Contains(t, snapString, "gun: gun")
require.True(t, strings.Contains(snapString, "role: snapshot") || strings.Contains(snapString, "role: target"))
require.Contains(t, snapString, "role: snapshot")

// validate targets is imported correctly
Expand All @@ -2491,5 +2488,5 @@ func TestExportImportFlow(t *testing.T) {
targBytes, _ := ioutil.ReadAll(targKey)
targString := string(targBytes)
require.Contains(t, targString, "gun: gun")
require.Contains(t, targString, "role: target")
require.True(t, strings.Contains(snapString, "role: snapshot") || strings.Contains(snapString, "role: target"))
}
8 changes: 4 additions & 4 deletions cmd/notary/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ func (k *keyCommander) GetCommand() *cobra.Command {

cmdKeysImport := cmdKeyImportTemplate.ToCommand(k.importKeys)
cmdKeysImport.Flags().StringVarP(
&k.keysImportRole, "role", "r", "", "Role to import key with - Notary can use this to infer the path to store the key. A specified path will take precedence")
&k.keysImportRole, "role", "r", "", "Role to import key with, if a role is not already given in a PEM header")
cmdKeysImport.Flags().StringVarP(
&k.keysImportGUN, "gun", "g", "", "Gun to import key with - Notary can use this to infer the path to store the key. A specified path will take precedence")
&k.keysImportGUN, "gun", "g", "", "Gun to import key with, if a gun is not already given in a PEM header")
cmd.AddCommand(cmdKeysImport)
cmdExport := cmdKeyExportTemplate.ToCommand(k.exportKeys)
cmdExport.Flags().StringSliceVar(
Expand Down Expand Up @@ -415,8 +415,8 @@ func (k *keyCommander) importKeys(cmd *cobra.Command, args []string) error {
for _, file := range args {
from, err := os.OpenFile(file, os.O_RDONLY, notary.PrivKeyPerms)
defer from.Close()
passRetreiver := k.getRetriever()
if err = utils.ImportKeys(from, importers, k.keysImportRole, k.keysImportGUN, passRetreiver); err != nil {
passRetriever := k.getRetriever()
if err = utils.ImportKeys(from, importers, k.keysImportRole, k.keysImportGUN, passRetriever); err != nil {
return err
}
}
Expand Down
31 changes: 0 additions & 31 deletions fixtures/precedence.example.com.key

This file was deleted.

16 changes: 8 additions & 8 deletions tuf/utils/x509_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ func TestKeyOperations(t *testing.T) {
rsaKey, err := GenerateRSAKey(rand.Reader, 512)

// Encode our ED private key
edPEM, err := KeyToPEM(edKey, "root")
edPEM, err := KeyToPEM(edKey, data.CanonicalRootRole)
require.NoError(t, err)

// Encode our EC private key
ecPEM, err := KeyToPEM(ecKey, "root")
ecPEM, err := KeyToPEM(ecKey, data.CanonicalRootRole)
require.NoError(t, err)

// Encode our RSA private key
rsaPEM, err := KeyToPEM(rsaKey, "root")
rsaPEM, err := KeyToPEM(rsaKey, data.CanonicalRootRole)
require.NoError(t, err)

// Check to see if ED key it is encoded
Expand Down Expand Up @@ -124,15 +124,15 @@ func TestKeyOperations(t *testing.T) {
require.Equal(t, rsaKey.Private(), decodedRSAKey.Private())

// Encrypt our ED Key
encryptedEDKey, err := EncryptPrivateKey(edKey, "root", "", "ponies")
encryptedEDKey, err := EncryptPrivateKey(edKey, data.CanonicalRootRole, "", "ponies")
require.NoError(t, err)

// Encrypt our EC Key
encryptedECKey, err := EncryptPrivateKey(ecKey, "root", "", "ponies")
encryptedECKey, err := EncryptPrivateKey(ecKey, data.CanonicalRootRole, "", "ponies")
require.NoError(t, err)

// Encrypt our RSA Key
encryptedRSAKey, err := EncryptPrivateKey(rsaKey, "root", "", "ponies")
encryptedRSAKey, err := EncryptPrivateKey(rsaKey, data.CanonicalRootRole, "", "ponies")
require.NoError(t, err)

// Check to see if ED key it is encrypted
Expand Down Expand Up @@ -170,10 +170,10 @@ func TestKeyOperations(t *testing.T) {

// quick test that gun headers are being added appropriately
// Encrypt our RSA Key, one type of key should be enough since headers are treated the same
testGunKey, err := EncryptPrivateKey(rsaKey, "root", "ilove", "ponies")
testGunKey, err := EncryptPrivateKey(rsaKey, data.CanonicalRootRole, "ilove", "ponies")
require.NoError(t, err)

testNoGunKey, err := EncryptPrivateKey(rsaKey, "root", "", "ponies")
testNoGunKey, err := EncryptPrivateKey(rsaKey, data.CanonicalRootRole, "", "ponies")
require.NoError(t, err)

stringTestGunKey := string(testGunKey)
Expand Down
5 changes: 4 additions & 1 deletion utils/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ func ImportKeys(from io.Reader, to []Importer, fallbackRole string, fallbackGun
pathWOFileName := strings.TrimSuffix(rawPath, filepath.Base(rawPath))
if strings.HasPrefix(pathWOFileName, notary.NonRootKeysSubdir) {
gunName := strings.TrimPrefix(pathWOFileName, notary.NonRootKeysSubdir)
block.Headers["gun"] = gunName[1:(len(gunName) - 1)] //removes the slashes
gunName = gunName[1:(len(gunName) - 1)] // remove the slashes
if gunName != "" {
block.Headers["gun"] = gunName
}
}
}
if block.Headers["gun"] == "" {
Expand Down
Loading

0 comments on commit 6dbafff

Please sign in to comment.