Example #1
0
func main() {
	flag.Parse()

	if *flagHelp {
		flag.Usage()
		os.Exit(1)
	}

	usr, err := user.Lookup(*flagUser)
	if err != nil {
		stderr("Failed to lookup user: %v", *flagUser)
		os.Exit(2)
	}

	akd, err := keys.Open(usr, true)
	if err != nil {
		stderr("Failed to open: %v", err)
		os.Exit(3)
	}
	defer akd.Close()

	switch {
	case *flagList:
		if err := printKeys(akd); err != nil {
			stderr("Failed to print keys: %v", err)
			os.Exit(4)
		}
	case *flagAdd != "":
		if err := addKeys(akd, *flagAdd, false); err != nil {
			stderr("failed to add keys %q: %v", *flagAdd, err)
			os.Exit(5)
		}
	case *flagAddForce != "":
		if err := addKeys(akd, *flagAddForce, true); err != nil {
			stderr("failed to add keys %q: %v", *flagAddForce, err)
			os.Exit(6)
		}
	case *flagDelete != "":
		if err := akd.Remove(*flagDelete); err != nil {
			stderr("failed to delete key %q: %v", *flagDelete, err)
			os.Exit(7)
		}
	case *flagDisable != "":
		if err := akd.Disable(*flagDisable); err != nil {
			stderr("failed to disable key %q: %v", *flagDisable, err)
			os.Exit(8)
		}
	}

	if err := akd.Sync(); err != nil {
		stderr("Failed sync: %v", err)
		os.Exit(9)
	}
	stdout("Updated %s", akd.KeysFilePath())

	os.Exit(0)
}
Example #2
0
// Add the provided SSH public keys to the user's authorized keys.
func (u Util) AuthorizeSSHKeys(c types.User) error {
	if len(c.SSHAuthorizedKeys) == 0 {
		return nil
	}

	return u.LogOp(func() error {
		usr, err := u.userLookup(c.Name)
		if err != nil {
			return fmt.Errorf("unable to lookup user %q", c.Name)
		}

		akd, err := keys.Open(usr, true)
		if err != nil {
			return err
		}
		defer akd.Close()

		// TODO(vc): introduce key names to config?
		// TODO(vc): validate c.SSHAuthorizedKeys well-formedness.
		ks := strings.Join(c.SSHAuthorizedKeys, "\n")
		// XXX(vc): for now ensure the addition is always
		// newline-terminated.  A future version of akd will handle this
		// for us in addition to validating the ssh keys for
		// well-formedness.
		if !strings.HasSuffix(ks, "\n") {
			ks = ks + "\n"
		}

		if err := akd.Add("coreos-ignition", []byte(ks), true, true); err != nil {
			return err
		}

		if err := akd.Sync(); err != nil {
			return err
		}

		return nil
	}, "adding ssh keys to user %q", c.Name)
}
Example #3
0
func writeMetadataKeys(username string, metadata providers.Metadata) error {
	if username == "" || metadata.SshKeys == nil {
		return nil
	}

	usr, err := user.Lookup(username)
	if err != nil {
		return fmt.Errorf("unable to lookup user %q: %v", username, err)
	}

	akd, err := authorized_keys_d.Open(usr, true)
	if err != nil {
		return err
	}
	defer akd.Close()

	ks := strings.Join(metadata.SshKeys, "\n")
	if err := akd.Add("coreos-metadata", []byte(ks), true, true); err != nil {
		return err
	}

	return akd.Sync()
}