sshkey

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: 18 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 sshkey is the sshkey behavior: the authorized SSH keys of a user.

Index

Constants

This section is empty.

Variables

View Source
var Behavior = behavior.Register(behavior.Define(
	"sshkey", "The authorized SSH keys of a user",
	behavior.Title("User SSH key"),
	behavior.Node,
	behavior.API,
	behavior.Area(area.Security),
	behavior.CLI,
	behavior.Of(user.Behavior),
	behavior.Requires(user.Behavior),
	behavior.Platform(behavior.PlatformDebian, newDebian),
	behavior.Platform(behavior.PlatformLinux, newReader),
	behavior.Platform(behavior.PlatformDarwin, newReader),
	behavior.RunsIfPresent("dscl"),
	behavior.Writes("/home/*/.ssh/authorized_keys"),
	behavior.Read("list", List).
		Describe("List the authorized SSH keys of a user").
		Explain("Reads ~/.ssh/authorized_keys of the user through gohai, with the type, the "+
			"SHA256 fingerprint, the comment and the options of each key. A user with no "+
			"file has no keys. A user that the host does not have is not found.").
		Example("A user with keys", ListInput{Name: "osapitest"},
			[]Key{
				{Type: "ssh-ed25519", Fingerprint: fp1, Comment: "osapitest@example"},
				{
					Type:        "ssh-ed25519",
					Fingerprint: fp2,
					Comment:     "other@example",
					Options:     `from="10.0.0.0/8"`,
				},
			},
			append(slices.Clone(osapitest), behavior.Exists(
				authorizedKeys,
				key1+"\n# a comment\n"+`from="10.0.0.0/8" `+key2+"\n",
			), behavior.Live("name", behavior.FactUser))...).
		Example("A user with no keys", ListInput{Name: "osapitest"}, []Key{},
			append(slices.Clone(osapitest), behavior.Live("name", behavior.FactUser))...).
		Fails("A user that the host does not have", ListInput{Name: "nobody"}, "not found",
			osapitest...).
		Example("A host key that gohai cannot read", ListInput{Name: "osapitest"},
			[]Key{{Type: "ssh-ed25519", Fingerprint: fp1, Comment: "osapitest@example"}},
			append(slices.Clone(osapitest),
				behavior.Exists("/etc/ssh/ssh_host_ed25519_key.pub", "ssh-ed25519"),
				behavior.Exists(authorizedKeys, key1+"\n"),
				behavior.Live("name", behavior.FactUser))...).
		Fails("A name that is not a user name", ListInput{Name: "Bad;name"}, "Name").
		Fails("A name over 32 characters", ListInput{Name: strings.Repeat("a", 33)}, "max"),
	behavior.Write("create", Create).
		AdminOnly().
		On(behavior.PlatformDebian).
		Describe("Add an authorized SSH key to a user").
		Explain("Adds the key line to the end of ~/.ssh/authorized_keys of the user, and makes "+
			"the directory and the file if they are missing, with modes 700 and 600, owned by "+
			"the user. A key that the file has already, by its fingerprint, does not change "+
			"it. A line with no public key is refused. A user that the host does not have is "+
			"not found. It runs on the Debian family only, and elsewhere reports skipped.").
		Sudo("chown").
		Example("Add a key", CreateInput{Name: "osapitest", Key: key1},
			Result{Fingerprint: fp1, Changed: true},
			with(
				behavior.Wrote(authorizedKeys, key1+"\n"),
				chown,
				behavior.Reads(ListInput{Name: "osapitest"}, Key{Fingerprint: "SHA256:mnpIO1El26FsWnOeLzppADcNXgXRNHh9MPNPLPvSCwk", Comment: "osapitest@example"}),
			)...,
		).
		Example("Add a second key", CreateInput{Name: "osapitest", Key: key2},
			Result{Fingerprint: fp2, Changed: true},
			with(
				behavior.After(CreateInput{Name: "osapitest", Key: key1}),
				behavior.Wrote(authorizedKeys, key1+"\n"+key2+"\n"),
				chown,
				behavior.Reads(ListInput{Name: "osapitest"}, Key{Fingerprint: "SHA256:a5CeY6Lh3ZXkJOqGyuwb24kQeVjgag1gc9ujzv++nhE", Comment: "other@example"}),
			)...,
		).
		Example("A key after a last line with no line break", CreateInput{Name: "osapitest", Key: key2},
			Result{Fingerprint: fp2, Changed: true},
			with(
				behavior.Exists(authorizedKeys, key1),
				behavior.Wrote(authorizedKeys, key1+"\n"+key2+"\n"),
				chown,
				behavior.Reads(ListInput{Name: "osapitest"}, Key{Fingerprint: "SHA256:a5CeY6Lh3ZXkJOqGyuwb24kQeVjgag1gc9ujzv++nhE"}),
			)...,
		).
		Example("A key the user has", CreateInput{Name: "osapitest", Key: key1},
			Result{Fingerprint: fp1},
			with(
				behavior.After(CreateInput{Name: "osapitest", Key: key1}),
				behavior.Reads(ListInput{Name: "osapitest"}, Key{Fingerprint: "SHA256:mnpIO1El26FsWnOeLzppADcNXgXRNHh9MPNPLPvSCwk"}),
			)...,
		).
		Fails("A line with no public key", CreateInput{Name: "osapitest", Key: "not a key"},
			"not an SSH public key", with()...).
		Fails("A key that is not base64", CreateInput{Name: "osapitest", Key: "ssh-ed25519 !!!"},
			"not an SSH public key", with()...).
		Fails("A user that the host does not have", CreateInput{Name: "nobody", Key: key1},
			"not found", with()...).
		Fails("A name that is not a user name", CreateInput{Name: "Bad;name", Key: key1}, "Name").
		Fails("A name over 32 characters", CreateInput{Name: strings.Repeat("a", 33), Key: key1}, "max").
		Fails("A key with a line break", CreateInput{Name: "osapitest", Key: key1 + "\n" + key2},
			"no_linebreak"),
	behavior.Write("delete", Delete).
		AdminOnly().
		On(behavior.PlatformDebian).
		Describe("Remove an authorized SSH key of a user").
		Explain("Removes each line of the key with the fingerprint from ~/.ssh/authorized_keys "+
			"of the user, and keeps the other lines as they are. A key that the file does not "+
			"have, a user with no file, or a user that the host does not have, changes "+
			"nothing. It runs on the Debian family only, and elsewhere reports "+
			"skipped.").
		Example("Remove a key", DeleteInput{Name: "osapitest", Fingerprint: fp1},
			Result{Fingerprint: fp1, Changed: true},
			with(
				behavior.After(CreateInput{Name: "osapitest", Key: key1}),
				behavior.After(CreateInput{Name: "osapitest", Key: key2}),
				behavior.Wrote(authorizedKeys, key2+"\n"),
				behavior.ReadsNone(ListInput{Name: "osapitest"}, Key{Fingerprint: "SHA256:mnpIO1El26FsWnOeLzppADcNXgXRNHh9MPNPLPvSCwk"}, "fingerprint"),
			)...,
		).
		Example("A key the user does not have", DeleteInput{Name: "osapitest", Fingerprint: fp2},
			Result{Fingerprint: fp2},
			with(
				behavior.After(CreateInput{Name: "osapitest", Key: key1}),
				behavior.ReadsNone(ListInput{Name: "osapitest"}, Key{Fingerprint: "SHA256:a5CeY6Lh3ZXkJOqGyuwb24kQeVjgag1gc9ujzv++nhE"}, "fingerprint"),
			)...,
		).
		Example("A user that the host does not have", DeleteInput{Name: "nobody", Fingerprint: fp1},
			Result{Fingerprint: fp1}, with(
				behavior.NoReadBack("the user nobody has no keys of osapi, and the read of a user that has no home fails"),
			)...,
		).
		Fails("A fingerprint that is not SHA256", DeleteInput{Name: "osapitest", Fingerprint: "MD5:aa"},
			"Fingerprint").
		Fails("A fingerprint with a line break",
			DeleteInput{Name: "osapitest", Fingerprint: fp1 + "\n" + fp2}, "no_linebreak").
		Fails("A name that is not a user name", DeleteInput{Name: "Bad;name", Fingerprint: fp1}, "Name").
		Fails("A name over 32 characters",
			DeleteInput{Name: strings.Repeat("a", 33), Fingerprint: fp1}, "max"),
))

Behavior is the authorized SSH keys of a user.

Functions

This section is empty.

Types

type CreateInput

type CreateInput struct {
	Name string `` /* 167-byte string literal not displayed */
	Key  string `` /* 150-byte string literal not displayed */
}

CreateInput is a key to add to a user's authorized_keys.

type DeleteInput

type DeleteInput struct {
	Name        string `` /* 145-byte string literal not displayed */
	Fingerprint string `` /* 145-byte string literal not displayed */
}

DeleteInput names a key of a user by its fingerprint. The fingerprint stays in the body: its base64 can have a slash, which cannot go in a path segment.

type Key

type Key struct {
	Type        string `json:"type"              doc:"The key type, such as ssh-ed25519"`
	Fingerprint string `json:"fingerprint"       doc:"The SHA256 fingerprint of the key"`
	Comment     string `json:"comment,omitempty" doc:"The comment after the key, such as alice@laptop"`
	Options     string `json:"options,omitempty" doc:"The options before the key, such as from=\"10.0.0.0/8\""`
}

Key is one key of a user's authorized_keys.

func List

func List(
	ctx context.Context,
	p Provider,
	in ListInput,
) ([]Key, error)

List is the keys of a user.

type ListInput

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

ListInput names the user whose keys to list.

type Provider

type Provider interface {
	// Keys is the keys of the user's authorized_keys. A user that the host
	// does not have is not found; one with no file has none.
	Keys(ctx context.Context, name string) ([]Key, error)
	// Add appends the key line to the user's authorized_keys, and returns
	// its fingerprint. A key that the file has already is unchanged.
	Add(ctx context.Context, name string, line string) (string, bool, error)
	// Remove drops each line of the key with the fingerprint.
	Remove(ctx context.Context, name string, fingerprint string) (bool, error)
}

Provider reads and changes the authorized SSH keys of a user.

type Result

type Result struct {
	Fingerprint string `json:"fingerprint" doc:"The SHA256 fingerprint of the key"`
	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 key to a user.

func Delete

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

Delete removes a key of a user.

Directories

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

Jump to

Keyboard shortcuts

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