func CmdAdd(name, path string, ik IKeys, id deploykeys.IDeployKeys) error { if strings.ContainsAny(name, config.InvalidChars) { return fmt.Errorf("Invalid key name. Names must not contain the following characters: %s", config.InvalidChars) } fullPath, err := homedir.Expand(path) if err != nil { return err } keyBytes, err := ioutil.ReadFile(fullPath) if err != nil { return err } k, err := id.ParsePublicKey(keyBytes) if err != nil { return err } key := ssh.MarshalAuthorizedKey(k) if err != nil { return err } err = ik.Add(name, string(key)) if err != nil { return err } logrus.Printf("Key '%s' added to your account.", name) logrus.Println("If you use an ssh-agent, make sure you add this key to your ssh-agent in order to push code") return nil }
func CmdList(ik IKeys, id deploykeys.IDeployKeys) error { keys, err := ik.List() if err != nil { return err } if keys == nil || len(*keys) == 0 { logrus.Println("No keys found") return nil } invalidKeys := map[string]string{} data := [][]string{{"NAME", "FINGERPRINT"}} for _, key := range *keys { s, err := id.ParsePublicKey([]byte(key.Key)) if err != nil { invalidKeys[key.Name] = err.Error() continue } h := sha256.New() h.Write(s.Marshal()) fingerprint := base64.StdEncoding.EncodeToString(h.Sum(nil)) data = append(data, []string{key.Name, fmt.Sprintf("SHA256:%s", strings.TrimRight(fingerprint, "="))}) } table := tablewriter.NewWriter(logrus.StandardLogger().Out) table.SetBorder(false) table.SetRowLine(false) table.SetCenterSeparator("") table.SetColumnSeparator("") table.SetRowSeparator("") table.AppendBulk(data) table.Render() if len(invalidKeys) > 0 { logrus.Println("\nInvalid Keys:") for keyName, reason := range invalidKeys { logrus.Printf("%s: %s", keyName, reason) } } return nil }
func CmdRemove(name, privateKeyPath string, ik IKeys, id deploykeys.IDeployKeys) error { if privateKeyPath != "" { // if ssh key auth is being used, don't let the key used for key auth be removed b, err := ioutil.ReadFile(privateKeyPath) if err != nil { return err } privKey, err := id.ParsePrivateKey(b) if err != nil { return err } pubKey, err := id.ExtractPublicKey(privKey) if err != nil { return err } pubKeyString := string(ssh.MarshalAuthorizedKey(pubKey)) userKeys, err := ik.List() if err != nil { return err } for _, uk := range *userKeys { if strings.TrimSpace(pubKeyString) == strings.TrimSpace(uk.Key) { return errors.New("You cannot remove the key that is currently being used for authentication. Run \"catalyze clear --private-key\" to remove your SSH key authentication settings.") } } } if strings.ContainsAny(name, config.InvalidChars) { return fmt.Errorf("Invalid key name. Names must not contain the following characters: %s", config.InvalidChars) } err := ik.Remove(name) if err != nil { return err } logrus.Printf("Key '%s' has been removed from your account.", name) return nil }