示例#1
0
// handleShadow takes a /etc/shadow style file and returns a cloud-config
func handleShadow(contents string, scripts_dir string) (*initialize.CloudConfig, error) {
	config := initialize.CloudConfig{}
	passwd := contents

	// root:$1$NyBnu0Gl$GBoj9u6lx3R8nyqHuxPwz/:15839:0:::::
	re := regexp.MustCompile("root:([^:]+):.+\n")
	keys := re.FindStringSubmatch(passwd)
	if len(keys) == 2 {
		passwd_hash := keys[1]

		// set the password for both users
		root := system.User{
			Name:         "root",
			PasswordHash: passwd_hash,
		}
		config.Users = append(config.Users, root)
		core := system.User{
			Name:         "core",
			PasswordHash: passwd_hash,
		}
		config.Users = append(config.Users, core)
	} else {
		return nil, errors.New("unable to parse password hash from shadow")
	}
	return &config, nil
}
示例#2
0
// setKey core and root users authorized_keys to the passed key
func setKey(config *initialize.CloudConfig, key string) *initialize.CloudConfig {
	config.SSHAuthorizedKeys = append(config.SSHAuthorizedKeys, key)
	// set the password for both users
	if len(config.Users) == 0 {
		root := system.User{
			Name: "root",
		}
		root.SSHAuthorizedKeys = append(root.SSHAuthorizedKeys, key)
		config.Users = append(config.Users, root)
	} else {
		config.Users[0].SSHAuthorizedKeys = append(config.Users[0].SSHAuthorizedKeys, key)
	}
	return config
}