Esempio n. 1
0
// keysRemoveKey deletes a private key based on ID
func keysRemoveKey(cmd *cobra.Command, args []string) {
	if len(args) < 1 {
		cmd.Usage()
		fatalf("must specify the key ID of the key to remove")
	}

	parseConfig()

	keyStoreManager, err := keystoremanager.NewKeyStoreManager(trustDir, retriever)
	if err != nil {
		fatalf("failed to create a new truststore manager with directory: %s", trustDir)
	}

	keyID := args[0]

	// This is an invalid ID
	if len(keyID) != idSize {
		fatalf("invalid key ID provided: %s", keyID)
	}

	// List the key about to be removed
	fmt.Println("Are you sure you want to remove the following key?")
	fmt.Printf("%s\n(yes/no)\n", keyID)

	// Ask for confirmation before removing the key, unless -y is passed
	if !keyRemoveYes {
		confirmed := askConfirm()
		if !confirmed {
			fatalf("aborting action.")
		}
	}

	// Choose the correct filestore to remove the key from
	var keyStoreToRemove *trustmanager.KeyFileStore
	var keyMap map[string]string
	if keyRemoveRoot {
		keyStoreToRemove = keyStoreManager.RootKeyStore()
		keyMap = keyStoreManager.RootKeyStore().ListKeys()
	} else {
		keyStoreToRemove = keyStoreManager.NonRootKeyStore()
		keyMap = keyStoreManager.NonRootKeyStore().ListKeys()
	}

	// Attempt to find the full GUN to the key in the map
	// This is irrelevant for removing root keys, but does no harm
	var keyWithGUN string
	for k := range keyMap {
		if filepath.Base(k) == keyID {
			keyWithGUN = k
		}
	}

	// If empty, we didn't find any matches
	if keyWithGUN == "" {
		fatalf("key with key ID: %s not found\n", keyID)
	}

	// Attempt to remove the key
	err = keyStoreToRemove.RemoveKey(keyWithGUN)
	if err != nil {
		fatalf("failed to remove key with key ID: %s, %v", keyID, err)
	}
}