MCPcopy Create free account
hub / github.com/docker/cli / TestLoadKeyFromPath

Function TestLoadKeyFromPath

cmd/docker-trust/trust/key_load_test.go:115–157  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

113}
114
115func TestLoadKeyFromPath(t *testing.T) {
116 skip.If(t, runtime.GOOS == "windows")
117 for keyID, keyBytes := range testKeys {
118 t.Run(fmt.Sprintf("load-key-id-%s-from-path", keyID), func(t *testing.T) {
119 privKeyFilepath := filepath.Join(t.TempDir(), "privkey.pem")
120 assert.NilError(t, os.WriteFile(privKeyFilepath, keyBytes, notary.PrivNoExecPerms))
121
122 keyStorageDir := t.TempDir()
123
124 keyFileStore, err := storage.NewPrivateKeyFileStorage(keyStorageDir, notary.KeyExtension)
125 assert.NilError(t, err)
126 privKeyImporters := []trustmanager.Importer{keyFileStore}
127
128 // get the privKeyBytes
129 privKeyBytes, err := getPrivKeyBytesFromPath(privKeyFilepath)
130 assert.NilError(t, err)
131
132 // import the key to our keyStorageDir
133 assert.Check(t, loadPrivKeyBytesToStore(privKeyBytes, privKeyImporters, privKeyFilepath, "signer-name", testPassRetriever))
134
135 // check that the appropriate ~/<trust_dir>/private/<key_id>.key file exists
136 expectedImportKeyPath := filepath.Join(keyStorageDir, notary.PrivDir, keyID+"."+notary.KeyExtension)
137 _, err = os.Stat(expectedImportKeyPath)
138 assert.NilError(t, err)
139
140 // verify the key content
141 from, _ := os.OpenFile(expectedImportKeyPath, os.O_RDONLY, notary.PrivExecPerms)
142 defer from.Close()
143 fromBytes, _ := io.ReadAll(from)
144 keyPEM, _ := pem.Decode(fromBytes)
145 assert.Check(t, is.Equal("signer-name", keyPEM.Headers["role"]))
146 // the default GUN is empty
147 assert.Check(t, is.Equal("", keyPEM.Headers["gun"]))
148 // assert encrypted header
149 assert.Check(t, is.Equal("ENCRYPTED PRIVATE KEY", keyPEM.Type))
150
151 decryptedKey, err := tufutils.ParsePKCS8ToTufKey(keyPEM.Bytes, []byte(testPass))
152 assert.NilError(t, err)
153 fixturePEM, _ := pem.Decode(keyBytes)
154 assert.Check(t, is.DeepEqual(fixturePEM.Bytes, decryptedKey.Private()))
155 })
156 }
157}
158
159func TestLoadKeyTooPermissive(t *testing.T) {
160 skip.If(t, runtime.GOOS == "windows")

Callers

nothing calls this directly

Calls 3

getPrivKeyBytesFromPathFunction · 0.85
loadPrivKeyBytesToStoreFunction · 0.85
CloseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…