示例#1
0
func TestEnsureFileExists(t *testing.T) {
	Convey("Given a unique file name, a user name and file permissions", t, func() {
		currentUser, err := nss.GetCurrentUser()
		So(err, ShouldBeNil)

		fname, err := file.UniqueName(os.TempDir(), "filetests")
		So(err, ShouldBeNil)

		perms := os.FileMode(0700)

		userID, _ := strconv.Atoi(currentUser.Uid)
		groupID, _ := strconv.Atoi(currentUser.Gid)

		Convey("It should create a new file if it doesn't exist", func() {
			err := file.EnsureExists(fname, perms, userID, groupID)
			So(err, ShouldBeNil)
			defer os.Remove(fname)

			UID, GID, err := file.IDsFor(fname)
			So(err, ShouldBeNil)

			So(currentUser.Uid, ShouldEqual, UID)
			So(currentUser.Gid, ShouldEqual, GID)
		})
	})
}
示例#2
0
func TestEnsureFileExists(t *testing.T) {
	Convey("Given a unique file name, a user name and file permissions", t, func() {
		currentUser, err := user.Current()
		So(err, ShouldBeNil)

		fname, err := file.UniqueName(os.TempDir(), "filetests")
		So(err, ShouldBeNil)

		perms := os.FileMode(0700)

		userID, _ := strconv.Atoi(currentUser.Uid)
		groupID, _ := strconv.Atoi(currentUser.Gid)

		// TODO(tmrts): Refactor the test
		Convey("It should create a new file if it doesn't exist", func() {
			err := file.EnsureExists(fname, perms, userID, groupID)
			So(err, ShouldBeNil)
			defer os.Remove(fname)

			fi, err := os.Lstat(fname)
			So(err, ShouldBeNil)

			uid := strconv.Itoa(int(fi.Sys().(*syscall.Stat_t).Uid))
			So(currentUser.Uid, ShouldEqual, uid)

			gid := strconv.Itoa(int(fi.Sys().(*syscall.Stat_t).Gid))
			So(currentUser.Gid, ShouldEqual, gid)
		})
	})
}
示例#3
0
// InitializeFor checks whether .ssh directory exists in the given user's home
// directory and creates the directory if it doesn't exist.
func InitializeFor(usr *user.User) error {
	userSSHDirPath := filepath.Join(usr.HomeDir, DirPath)
	userAuthorizedKeysPath := filepath.Join(usr.HomeDir, AuthorizedKeysPath)

	userID, err := strconv.Atoi(usr.Uid)
	if err != nil {
		return err
	}

	groupID, err := strconv.Atoi(usr.Gid)
	if err != nil {
		return err
	}

	err = file.EnsureDirectoryExists(userSSHDirPath, 0700, userID, groupID)
	if err != nil {
		return err
	}

	return file.EnsureExists(userAuthorizedKeysPath, 0600, userID, groupID)
}
示例#4
0
文件: ssh.go 项目: rtnpro/flamingo
func InitializeFor(owner *user.User) error {
	userSSHDirPath := owner.HomeDir + "/" + SSHDirPath
	userAuthorizedKeysPath := owner.HomeDir + "/" + AuthorizedKeysPath

	userID, err := strconv.Atoi(owner.Uid)
	if err != nil {
		return err
	}

	groupID, err := strconv.Atoi(owner.Gid)
	if err != nil {
		return err
	}

	err = file.EnsureDirectoryExists(userSSHDirPath, 0700, userID, groupID)
	if err != nil {
		return err
	}

	return file.EnsureExists(userAuthorizedKeysPath, 0600, userID, groupID)
}