@@ -114,99 +114,91 @@ var testKeys = map[string][]byte{
114114func TestLoadKeyFromPath (t * testing.T ) {
115115 skip .If (t , runtime .GOOS == "windows" )
116116 for keyID , keyBytes := range testKeys {
117- keyID , keyBytes := keyID , keyBytes
117+ privKeyID , privKeyFixture := keyID , keyBytes
118118 t .Run (fmt .Sprintf ("load-key-id-%s-from-path" , keyID ), func (t * testing.T ) {
119- testLoadKeyFromPath (t , keyID , keyBytes )
119+ privKeyFilepath := filepath .Join (t .TempDir (), "privkey.pem" )
120+ assert .NilError (t , os .WriteFile (privKeyFilepath , privKeyFixture , notary .PrivNoExecPerms ))
121+
122+ keyStorageDir := t .TempDir ()
123+
124+ const passwd = "password"
125+ cannedPasswordRetriever := passphrase .ConstantRetriever (passwd )
126+ keyFileStore , err := storage .NewPrivateKeyFileStorage (keyStorageDir , notary .KeyExtension )
127+ assert .NilError (t , err )
128+ privKeyImporters := []trustmanager.Importer {keyFileStore }
129+
130+ // get the privKeyBytes
131+ privKeyBytes , err := getPrivKeyBytesFromPath (privKeyFilepath )
132+ assert .NilError (t , err )
133+
134+ // import the key to our keyStorageDir
135+ assert .Check (t , loadPrivKeyBytesToStore (privKeyBytes , privKeyImporters , privKeyFilepath , "signer-name" , cannedPasswordRetriever ))
136+
137+ // check that the appropriate ~/<trust_dir>/private/<key_id>.key file exists
138+ expectedImportKeyPath := filepath .Join (keyStorageDir , notary .PrivDir , privKeyID + "." + notary .KeyExtension )
139+ _ , err = os .Stat (expectedImportKeyPath )
140+ assert .NilError (t , err )
141+
142+ // verify the key content
143+ from , _ := os .OpenFile (expectedImportKeyPath , os .O_RDONLY , notary .PrivExecPerms )
144+ defer from .Close ()
145+ fromBytes , _ := io .ReadAll (from )
146+ keyPEM , _ := pem .Decode (fromBytes )
147+ assert .Check (t , is .Equal ("signer-name" , keyPEM .Headers ["role" ]))
148+ // the default GUN is empty
149+ assert .Check (t , is .Equal ("" , keyPEM .Headers ["gun" ]))
150+ // assert encrypted header
151+ assert .Check (t , is .Equal ("ENCRYPTED PRIVATE KEY" , keyPEM .Type ))
152+
153+ decryptedKey , err := tufutils .ParsePKCS8ToTufKey (keyPEM .Bytes , []byte (passwd ))
154+ assert .NilError (t , err )
155+ fixturePEM , _ := pem .Decode (privKeyFixture )
156+ assert .Check (t , is .DeepEqual (fixturePEM .Bytes , decryptedKey .Private ()))
120157 })
121158 }
122159}
123160
124- func testLoadKeyFromPath (t * testing.T , privKeyID string , privKeyFixture []byte ) {
125- privKeyFilepath := filepath .Join (t .TempDir (), "privkey.pem" )
126- assert .NilError (t , os .WriteFile (privKeyFilepath , privKeyFixture , notary .PrivNoExecPerms ))
127-
128- keyStorageDir := t .TempDir ()
129-
130- const passwd = "password"
131- cannedPasswordRetriever := passphrase .ConstantRetriever (passwd )
132- keyFileStore , err := storage .NewPrivateKeyFileStorage (keyStorageDir , notary .KeyExtension )
133- assert .NilError (t , err )
134- privKeyImporters := []trustmanager.Importer {keyFileStore }
135-
136- // get the privKeyBytes
137- privKeyBytes , err := getPrivKeyBytesFromPath (privKeyFilepath )
138- assert .NilError (t , err )
139-
140- // import the key to our keyStorageDir
141- assert .Check (t , loadPrivKeyBytesToStore (privKeyBytes , privKeyImporters , privKeyFilepath , "signer-name" , cannedPasswordRetriever ))
142-
143- // check that the appropriate ~/<trust_dir>/private/<key_id>.key file exists
144- expectedImportKeyPath := filepath .Join (keyStorageDir , notary .PrivDir , privKeyID + "." + notary .KeyExtension )
145- _ , err = os .Stat (expectedImportKeyPath )
146- assert .NilError (t , err )
147-
148- // verify the key content
149- from , _ := os .OpenFile (expectedImportKeyPath , os .O_RDONLY , notary .PrivExecPerms )
150- defer from .Close ()
151- fromBytes , _ := io .ReadAll (from )
152- keyPEM , _ := pem .Decode (fromBytes )
153- assert .Check (t , is .Equal ("signer-name" , keyPEM .Headers ["role" ]))
154- // the default GUN is empty
155- assert .Check (t , is .Equal ("" , keyPEM .Headers ["gun" ]))
156- // assert encrypted header
157- assert .Check (t , is .Equal ("ENCRYPTED PRIVATE KEY" , keyPEM .Type ))
158-
159- decryptedKey , err := tufutils .ParsePKCS8ToTufKey (keyPEM .Bytes , []byte (passwd ))
160- assert .NilError (t , err )
161- fixturePEM , _ := pem .Decode (privKeyFixture )
162- assert .Check (t , is .DeepEqual (fixturePEM .Bytes , decryptedKey .Private ()))
163- }
164-
165161func TestLoadKeyTooPermissive (t * testing.T ) {
166162 skip .If (t , runtime .GOOS == "windows" )
167163 for keyID , keyBytes := range testKeys {
168- keyID , keyBytes := keyID , keyBytes
164+ keyID , privKeyFixture := keyID , keyBytes
169165 t .Run (fmt .Sprintf ("load-key-id-%s-too-permissive" , keyID ), func (t * testing.T ) {
170- testLoadKeyTooPermissive (t , keyBytes )
171- })
172- }
173- }
174-
175- func testLoadKeyTooPermissive (t * testing.T , privKeyFixture []byte ) {
176- privKeyDir := t .TempDir ()
177- privKeyFilepath := filepath .Join (privKeyDir , "privkey477.pem" )
178- assert .NilError (t , os .WriteFile (privKeyFilepath , privKeyFixture , 0o477 ))
166+ privKeyDir := t .TempDir ()
167+ privKeyFilepath := filepath .Join (privKeyDir , "privkey477.pem" )
168+ assert .NilError (t , os .WriteFile (privKeyFilepath , privKeyFixture , 0o477 ))
179169
180- // import the key to our keyStorageDir
181- _ , err := getPrivKeyBytesFromPath (privKeyFilepath )
182- expected := fmt .Sprintf ("private key file %s must not be readable or writable by others" , privKeyFilepath )
183- assert .Error (t , err , expected )
170+ // import the key to our keyStorageDir
171+ _ , err := getPrivKeyBytesFromPath (privKeyFilepath )
172+ expected := fmt .Sprintf ("private key file %s must not be readable or writable by others" , privKeyFilepath )
173+ assert .Error (t , err , expected )
184174
185- privKeyFilepath = filepath .Join (privKeyDir , "privkey667.pem" )
186- assert .NilError (t , os .WriteFile (privKeyFilepath , privKeyFixture , 0o677 ))
175+ privKeyFilepath = filepath .Join (privKeyDir , "privkey667.pem" )
176+ assert .NilError (t , os .WriteFile (privKeyFilepath , privKeyFixture , 0o677 ))
187177
188- _ , err = getPrivKeyBytesFromPath (privKeyFilepath )
189- expected = fmt .Sprintf ("private key file %s must not be readable or writable by others" , privKeyFilepath )
190- assert .Error (t , err , expected )
178+ _ , err = getPrivKeyBytesFromPath (privKeyFilepath )
179+ expected = fmt .Sprintf ("private key file %s must not be readable or writable by others" , privKeyFilepath )
180+ assert .Error (t , err , expected )
191181
192- privKeyFilepath = filepath .Join (privKeyDir , "privkey777.pem" )
193- assert .NilError (t , os .WriteFile (privKeyFilepath , privKeyFixture , 0o777 ))
182+ privKeyFilepath = filepath .Join (privKeyDir , "privkey777.pem" )
183+ assert .NilError (t , os .WriteFile (privKeyFilepath , privKeyFixture , 0o777 ))
194184
195- _ , err = getPrivKeyBytesFromPath (privKeyFilepath )
196- expected = fmt .Sprintf ("private key file %s must not be readable or writable by others" , privKeyFilepath )
197- assert .Error (t , err , expected )
185+ _ , err = getPrivKeyBytesFromPath (privKeyFilepath )
186+ expected = fmt .Sprintf ("private key file %s must not be readable or writable by others" , privKeyFilepath )
187+ assert .Error (t , err , expected )
198188
199- privKeyFilepath = filepath .Join (privKeyDir , "privkey400.pem" )
200- assert .NilError (t , os .WriteFile (privKeyFilepath , privKeyFixture , 0o400 ))
189+ privKeyFilepath = filepath .Join (privKeyDir , "privkey400.pem" )
190+ assert .NilError (t , os .WriteFile (privKeyFilepath , privKeyFixture , 0o400 ))
201191
202- _ , err = getPrivKeyBytesFromPath (privKeyFilepath )
203- assert .NilError (t , err )
192+ _ , err = getPrivKeyBytesFromPath (privKeyFilepath )
193+ assert .NilError (t , err )
204194
205- privKeyFilepath = filepath .Join (privKeyDir , "privkey600.pem" )
206- assert .NilError (t , os .WriteFile (privKeyFilepath , privKeyFixture , 0o600 ))
195+ privKeyFilepath = filepath .Join (privKeyDir , "privkey600.pem" )
196+ assert .NilError (t , os .WriteFile (privKeyFilepath , privKeyFixture , 0o600 ))
207197
208- _ , err = getPrivKeyBytesFromPath (privKeyFilepath )
209- assert .NilError (t , err )
198+ _ , err = getPrivKeyBytesFromPath (privKeyFilepath )
199+ assert .NilError (t , err )
200+ })
201+ }
210202}
211203
212204var pubKeyFixture = []byte (`-----BEGIN PUBLIC KEY-----
0 commit comments