user

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package user is the user behavior: the accounts of the host. It reads them through gohai on every platform, and adds, changes and removes them on the Debian family. A password is hashed on the controller, so the job data and NATS have only its hash. The password behavior, under the user, sets the password of a user that exists.

Index

Constants

This section is empty.

Variables

View Source
var Behavior = behavior.Register(behavior.Define(
	"user", "The users of the host",
	behavior.Node,
	behavior.API,
	behavior.Area(area.Security),
	behavior.CLI,
	behavior.Platform(behavior.PlatformDebian, newDebian),
	behavior.Platform(behavior.PlatformLinux, newReader),
	behavior.Platform(behavior.PlatformDarwin, newReader),
	behavior.RunsIfPresent("id", "dscl"),
	behavior.Read("list", List).
		Describe("List the users of the host").
		Explain("Lists each user with its IDs, home, shell, groups and whether it is "+
			"locked, through gohai: from /etc/passwd, /etc/group and /etc/shadow on "+
			"Linux, and from the directory service with dscl on macOS. The groups are "+
			"those that list the user as a member, besides its primary group.").
		Example("A host with users", ListInput{},
			[]Account{alice, {Name: "root", Home: "/root", Shell: "/bin/bash", Locked: true}},
			withHost()...,
		),
	behavior.Read("get", Get).Key("name").
		Describe("Get one user").
		Explain("Reads one user from the same list. A user that the host does not have "+
			"is not found.").
		Example("A user", GetInput{Name: "alice"}, alice,
			append(withHost(), behavior.Live("name", behavior.FactUser))...).
		Fails("A user that the host does not have", GetInput{Name: "nobody"}, "not found",
			behavior.NotLive("each real host has nobody")).
		Fails("A name that is an option", GetInput{Name: "-r"}, "account_name").
		Fails("A name longer than 32 characters",
			GetInput{Name: strings.Repeat("a", 33)}, "max"),
	behavior.Write("create", Create).
		AdminOnly().
		On(behavior.PlatformDebian).
		Sudo("useradd", "chpasswd").
		Before(hashCreate).
		Describe("Add a user").
		Explain("Adds the user with useradd, with its home directory, and sets its "+
			"password with chpasswd when one is given. The controller hashes the "+
			"password, so the job and NATS have only the hash. A user that the host "+
			"has already does not change and reports changed: false. It runs on the "+
			"Debian family only, and elsewhere reports skipped.").
		Example("Add a user", CreateInput{Name: "osapitest"},
			Result{Name: "osapitest", Changed: true},
			withHost(newUser, behavior.Ran("useradd", "--create-home", "--", "osapitest"),
				behavior.Reads(GetInput{Name: "osapitest"}, Account{Name: "osapitest"}),
			)...,
		).
		Example("Add a user with a shell, groups and a password",
			CreateInput{
				Name: "osapitest", Shell: "/bin/sh", Groups: []string{"root"},
				Password: "osapi-test-password", PasswordHash: testHash,
			},
			Result{Name: "osapitest", Changed: true},
			withHost(
				behavior.ExistsAfter(
					"useradd --create-home -s /bin/sh -G root -- osapitest", "/etc/passwd",
					etcPasswd+"osapitest:x:1001:1001::/home/osapitest:/bin/sh\n"),
				behavior.Ran("useradd", "--create-home", "-s", "/bin/sh", "-G", "root", "--", "osapitest"),
				behavior.Ran("chpasswd", "-e"),
				behavior.Reads(GetInput{Name: "osapitest"}, Account{Name: "osapitest", Shell: "/bin/sh"}),
			)...,
		).
		Example("Add a system user with its IDs and its home",
			CreateInput{
				Name:   "osapitest",
				UID:    1999,
				GID:    100,
				Home:   "/home/osapitest",
				System: true,
			},
			Result{Name: "osapitest", Changed: true},
			withHost(
				behavior.ExistsAfter(
					"useradd --create-home -u 1999 -g 100 -d /home/osapitest -r -- osapitest",
					"/etc/passwd", etcPasswd+"osapitest:x:1999:100::/home/osapitest:/bin/sh\n"),
				behavior.Ran("useradd", "--create-home", "-u", "1999", "-g", "100",
					"-d", "/home/osapitest", "-r", "--", "osapitest"),
				behavior.Reads(GetInput{Name: "osapitest"}, Account{Name: "osapitest", UID: 1999, GID: 100, Home: "/home/osapitest"}),
			)...,
		).
		Example("A user that the host has", CreateInput{Name: "osapitest"},
			Result{Name: "osapitest"},
			withHost(newUser, behavior.After(CreateInput{Name: "osapitest"}),
				behavior.Reads(GetInput{Name: "osapitest"}, Account{Name: "osapitest"}),
			)...,
		).
		Fails("A name that is an option", CreateInput{Name: "-r"}, "account_name").
		Fails("A name longer than 32 characters",
			CreateInput{Name: strings.Repeat("a", 33)}, "max").
		Fails("A negative user ID", CreateInput{Name: "osapitest", UID: -1}, "min").
		Fails("A negative group ID", CreateInput{Name: "osapitest", GID: -1}, "min").
		Fails("A home that is not absolute", CreateInput{Name: "osapitest", Home: "home"}, "startswith").
		Fails("A home with a line break", CreateInput{Name: "osapitest", Home: "/home/a\nb"}, "no_linebreak").
		Fails("A shell that is not absolute", CreateInput{Name: "osapitest", Shell: "bash"}, "startswith").
		Fails("A shell with a line break", CreateInput{Name: "osapitest", Shell: "/bin/a\nb"}, "no_linebreak").
		Fails("A group that is an option",
			CreateInput{Name: "osapitest", Groups: []string{"-r"}}, "account_name").
		Fails("A group longer than 32 characters",
			CreateInput{Name: "osapitest", Groups: []string{strings.Repeat("a", 33)}}, "max").
		Fails("A password with a line break",
			CreateInput{Name: "osapitest", Password: "a\nb"}, "no_linebreak").
		Fails("A hash that is not a crypt hash",
			CreateInput{Name: "osapitest", PasswordHash: "plain"}, "crypt hash",
			withHost()...),
	behavior.Write("update", Update).Key("name").
		AdminOnly().
		On(behavior.PlatformDebian).
		Sudo("usermod").
		Describe("Change a user").
		Explain("Changes what the input gives and the user does not have already, with "+
			"usermod: its shell, its home, which usermod moves the files to, its groups, "+
			"which replace the groups it has, and whether it is locked. The same values "+
			"report changed: false. A user that the host does not have is not found. It "+
			"runs on the Debian family only, and elsewhere reports skipped.").
		Example("Set the shell", UpdateInput{Name: "osapitest", Shell: "/bin/bash"},
			Result{Name: "osapitest", Changed: true},
			withHost(
				newUser,
				behavior.ExistsAfter("usermod -s /bin/bash -- osapitest", "/etc/passwd",
					etcPasswd+"osapitest:x:1001:1001::/home/osapitest:/bin/bash\n"),
				behavior.After(CreateInput{Name: "osapitest"}),
				behavior.Ran("usermod", "-s", "/bin/bash", "--", "osapitest"),
				behavior.Reads(GetInput{Name: "osapitest"}, Account{Name: "osapitest", Shell: "/bin/bash"}),
			)...,
		).
		Example("The shell it has", UpdateInput{Name: "osapitest", Shell: "/bin/bash"},
			Result{Name: "osapitest"},
			withHost(
				newUser,
				behavior.ExistsAfter("usermod -s /bin/bash -- osapitest", "/etc/passwd",
					etcPasswd+"osapitest:x:1001:1001::/home/osapitest:/bin/bash\n"),
				behavior.After(CreateInput{Name: "osapitest"}),
				behavior.After(UpdateInput{Name: "osapitest", Shell: "/bin/bash"}),
				behavior.Reads(GetInput{Name: "osapitest"}, Account{Name: "osapitest", Shell: "/bin/bash"}),
			)...,
		).
		Example("Move the home, set the groups and lock it",
			UpdateInput{
				Name:   "osapitest",
				Home:   "/home/osapitest2",
				Groups: []string{"root"},
				Lock:   "locked",
			},
			Result{Name: "osapitest", Changed: true},
			withHost(append(
				withPassword(),
				behavior.ExistsAfter("usermod -d /home/osapitest2 -m -G root -L -- osapitest", "/etc/passwd",
					etcPasswd+"osapitest:x:1001:1001::/home/osapitest2:/bin/sh\n"),
				behavior.ExistsAfter("usermod -d /home/osapitest2 -m -G root -L -- osapitest", "/etc/group",
					"root:x:0:osapitest\nalice:x:1000:\ndevelopers:x:1500:alice\n"),
				behavior.ExistsAfter("usermod -d /home/osapitest2 -m -G root -L -- osapitest", "/etc/shadow",
					etcShadow+shadowLocked),
				behavior.Ran("usermod", "-d", "/home/osapitest2", "-m", "-G", "root", "-L", "--", "osapitest"),
				behavior.Reads(GetInput{Name: "osapitest"}, Account{Name: "osapitest", Home: "/home/osapitest2", Locked: true}),
			)...)...,
		).
		Example("Unlock it", UpdateInput{Name: "osapitest", Lock: "unlocked"},
			Result{Name: "osapitest", Changed: true},
			withHost(append(
				withPassword(),
				behavior.ExistsAfter("usermod -L -- osapitest", "/etc/shadow", etcShadow+shadowLocked),
				behavior.ExistsAfter("usermod -U -- osapitest", "/etc/shadow", etcShadow+shadowSet),
				behavior.After(UpdateInput{Name: "osapitest", Lock: "locked"}),
				behavior.Ran("usermod", "-U", "--", "osapitest"),
				behavior.Reads(GetInput{Name: "osapitest"}, Account{Name: "osapitest"}, "name", "locked"),
			)...)...,
		).
		Fails("A user that the host does not have", UpdateInput{Name: "nobody"}, "not found",
			withHost()...).
		Fails("A name that is an option", UpdateInput{Name: "-r"}, "account_name").
		Fails("A name longer than 32 characters",
			UpdateInput{Name: strings.Repeat("a", 33)}, "max").
		Fails("A shell that is not absolute", UpdateInput{Name: "alice", Shell: "bash"}, "startswith").
		Fails("A shell with a line break", UpdateInput{Name: "alice", Shell: "/bin/a\nb"}, "no_linebreak").
		Fails("A home that is not absolute", UpdateInput{Name: "alice", Home: "home"}, "startswith").
		Fails("A home with a line break", UpdateInput{Name: "alice", Home: "/home/a\nb"}, "no_linebreak").
		Fails("A group that is an option",
			UpdateInput{Name: "alice", Groups: []string{"-r"}}, "account_name").
		Fails("A group longer than 32 characters",
			UpdateInput{Name: "alice", Groups: []string{strings.Repeat("a", 33)}}, "max").
		Fails("A lock that is not locked or unlocked",
			UpdateInput{Name: "alice", Lock: "maybe"}, "enum"),
	behavior.Write("delete", Delete).Key("name").
		AdminOnly().
		On(behavior.PlatformDebian).
		Sudo("userdel").
		Describe("Remove a user").
		Explain("Removes the user and its home directory with userdel -r. A user that "+
			"the host does not have does not change and reports changed: false. It runs "+
			"on the Debian family only, and elsewhere reports skipped.").
		Example("Remove a user", DeleteInput{Name: "osapitest"},
			Result{Name: "osapitest", Changed: true},
			withHost(newUser,
				behavior.ExistsAfter("userdel -r -- osapitest", "/etc/passwd", etcPasswd),
				behavior.After(CreateInput{Name: "osapitest"}),
				behavior.Ran("userdel", "-r", "--", "osapitest"),
				behavior.ReadsNone(GetInput{Name: "osapitest"}, Account{Name: "osapitest"}, "name"),
			)...,
		).
		Example("A user that the host does not have", DeleteInput{Name: "osapitest"},
			Result{Name: "osapitest"},
			withHost(
				behavior.ReadsNone(GetInput{Name: "osapitest"}, Account{Name: "osapitest"}, "name"),
			)...,
		).
		Fails("A name that is an option", DeleteInput{Name: "-r"}, "account_name").
		Fails("A name longer than 32 characters",
			DeleteInput{Name: strings.Repeat("a", 33)}, "max"),
))

Behavior is the user behavior: the users of the host. The integration test runs the write examples as root on CI's runner, each from its user removed first. osapitest is a name nothing on the runner has.

Functions

This section is empty.

Types

type Account

type Account struct {
	Name   string   `json:"name"             doc:"The user"`
	UID    int      `json:"uid"              doc:"Its numeric user ID"`
	GID    int      `json:"gid"              doc:"Its primary group ID"`
	Home   string   `json:"home"             doc:"Its home directory"`
	Shell  string   `json:"shell"            doc:"Its login shell"`
	Groups []string `json:"groups,omitempty" doc:"The groups it is a member of, besides its primary group" none:"none"`
	Locked bool     `json:"locked"           doc:"Whether a login with its password is refused"`
}

Account is a user of the host.

func Get

func Get(
	ctx context.Context,
	p Provider,
	in GetInput,
) (Account, error)

Get reads one user.

func List

func List(
	ctx context.Context,
	p Provider,
	_ ListInput,
) ([]Account, error)

List reads the users of the host.

type CreateInput

type CreateInput struct {
	Name         string       `` /* 168-byte string literal not displayed */
	UID          int          `` /* 140-byte string literal not displayed */
	GID          int          `` /* 137-byte string literal not displayed */
	Home         string       `` /* 127-byte string literal not displayed */
	Shell        string       `` /* 151-byte string literal not displayed */
	Groups       []string     `` /* 139-byte string literal not displayed */
	System       bool         `` /* 141-byte string literal not displayed */
	Password     secret.Value `` /* 151-byte string literal not displayed */
	PasswordHash string       `` /* 181-byte string literal not displayed */
}

CreateInput is the user to add. The caller sends password; the controller's step hashes it into password_hash, so the agent, the job data and NATS have only the hash.

type DeleteInput

type DeleteInput struct {
	Name string `json:"name" validate:"required,max=32,account_name" doc:"The user to remove, with its home directory, such as alice"`
}

DeleteInput names the user to remove.

type GetInput

type GetInput struct {
	Name string `json:"name" validate:"required,max=32,account_name" doc:"The user, such as alice"`
}

GetInput names the user to read.

type ListInput

type ListInput struct{}

ListInput is list's input: it takes none.

type Provider

type Provider interface {
	List(ctx context.Context) ([]Account, error)
	Get(ctx context.Context, name string) (Account, error)
	Create(ctx context.Context, in CreateInput) (bool, error)
	Update(ctx context.Context, in UpdateInput) (bool, error)
	Delete(ctx context.Context, name string) (bool, error)
}

Provider reads the users of the host and changes them. reader.go reads them through gohai on every platform, and debian.go changes them on the Debian family.

type Result

type Result struct {
	Name    string `json:"name"    doc:"The user"`
	Changed bool   `json:"changed" doc:"Whether the host changed"`
}

Result is what a write did.

func Create

func Create(
	ctx context.Context,
	p Provider,
	in CreateInput,
) (Result, error)

Create adds a user.

func Delete

func Delete(
	ctx context.Context,
	p Provider,
	in DeleteInput,
) (Result, error)

Delete removes a user.

func Update

func Update(
	ctx context.Context,
	p Provider,
	in UpdateInput,
) (Result, error)

Update changes a user.

type UpdateInput

type UpdateInput struct {
	Name   string     `json:"name"             validate:"required,max=32,account_name"        doc:"The user, such as alice"`
	Shell  string     `json:"shell,omitempty"  validate:"omitempty,startswith=/,no_linebreak" doc:"Its login shell, such as /bin/bash"`
	Home   string     `` /* 129-byte string literal not displayed */
	Groups []string   `` /* 138-byte string literal not displayed */
	Lock   types.Lock `` /* 162-byte string literal not displayed */
}

UpdateInput changes a user. An empty field keeps what the user has.

Directories

Path Synopsis
Package mocks holds the generated double of the user provider, for the methods' tests.
Package mocks holds the generated double of the user provider, for the methods' tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL