keyscmd

package
v0.0.0-...-9b6c51a Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

README

Package cloudeng.io/cmdutil/keys/keyscmd

import cloudeng.io/cmdutil/keys/keyscmd

Package keyscmd provides a set of utilities for reading and writing multiple keys stored in a single item in a file system using the format used by keys.InMemoryKeyStore.

Variables

ErrKeyInfoNotFound
ErrKeyInfoNotFound = errors.New("key info not found")

ErrUpdateNotAllowed
ErrUpdateNotAllowed = errors.New("update not allowed")

Functions

Func CopyContents
func CopyContents(ctx context.Context, srcFS file.ReadFileFS, src string, dstFS file.WriteFileFS, dst string, perm fs.FileMode) error

CopyContents copies the contents of the source file in srcFS to the destination file in dstFS.

Func ExtensionSpec
func ExtensionSpec(name string) string

ExtensionSpec returns the subcmd extension tree for managing key info items in a keychain/secrets store, formatted with the provided name.

Func IsDstSafe
func IsDstSafe(dst string) error

SafeStdout checks if the provided name refers to Stdout ("-" or empty) and if so, verifies that Stdout is piped. If Stdout is not piped, it returns an error.

Func IsStdoutStdin
func IsStdoutStdin(name string) bool

IsStdoutStdin returns true if the provided name is "-" or empty, indicating that the operation should read from stdin or write to stdout.

Func NewKeyInfoExtension
func NewKeyInfoExtension(name string, appendFn func(cmd *subcmd.CommandSetYAML) error) subcmd.Extension

NewKeyInfoExtension creates a new subcmd.Extension for key info management commands, name specifies the name of the command tree and also the name of the template variable used to include it in the parent command tree.

Func ReadFSWithStdin
func ReadFSWithStdin(fs file.ReadFileFS, name string) file.ReadFileFS

ReadFSWithStdin returns a file.ReadFileFS that reads from stdin if the name is "-" or empty.

Func ReadFromLocal
func ReadFromLocal(ctx context.Context, filename string, dstFS file.WriteFileFS, dst string, perm fs.FileMode) error
Func ReadKeyInfoFromLocal
func ReadKeyInfoFromLocal(ctx context.Context, filename string, unmarshal func([]byte, any) error) (keys.Info, error)

ReadKeyInfoFromLocal reads key information from a local file or stdin, unmarshals it using the provided unmarshal function, and returns the resulting keys.Info.

Func ReadKeyInfoFromLocalJSON
func ReadKeyInfoFromLocalJSON(ctx context.Context, filename string) (keys.Info, error)

ReadKeyInfoFromLocalJSON reads key information from a local JSON file or stdin and returns the resulting keys.Info.

Func ReadKeyInfoFromLocalYAML
func ReadKeyInfoFromLocalYAML(ctx context.Context, filename string) (keys.Info, error)

ReadKeyInfoFromLocalYAML reads key information from a local YAML file or stdin and returns the resulting keys.Info.

Func ReadWriteFSWithStdout
func ReadWriteFSWithStdout(fs file.ReadWriteFileFS, name string) file.ReadWriteFileFS

ReadWriterFSWithStdout returns a file.ReadWriteFileFS that reads from fs and writes to stdout if the name is "-" or empty.

Func SafeWriteKeyInfoJSON
func SafeWriteKeyInfoJSON(ctx context.Context, ki keys.Info, dst string, perm fs.FileMode) error
Func SafeWriteKeyInfoToLocal
func SafeWriteKeyInfoToLocal(ctx context.Context, ki keys.Info, marshal func(any) ([]byte, error), dst string, perm fs.FileMode) error
Func SafeWriteKeyInfoYAML
func SafeWriteKeyInfoYAML(ctx context.Context, ki keys.Info, dst string, perm fs.FileMode) error
Func SafeWriteToLocal
func SafeWriteToLocal(ctx context.Context, srcFS file.ReadFileFS, src string, filename string, perm fs.FileMode) error
Func WriteFSWithStdout
func WriteFSWithStdout(fs file.WriteFileFS, name string) file.WriteFileFS

WriteFSWithStdout returns a file.WriteFileFS that writes to stdout if the name is "-" or empty.

Types

Type KeyReader
type KeyReader struct {
	// contains filtered or unexported fields
}

KeyReader provides methods to read keys from a file system in the InMemoryKeyStore

Functions
func NewKeyReader(fs file.ReadFileFS) KeyReader

NewKeyReader creates a new KeyReader that reads keys stored using the InMemoryKeyStore format using the provided file.ReadFileFS.

Methods
func (r *KeyReader) GetKey(ctx context.Context, name string, spec keys.KeySpec) (keys.Info, error)

GetKey retrieves a specific key from the specified item in the file system based on the provided keys.KeySpec. If the key is not found, it returns an error.

func (r *KeyReader) GetKeys(ctx context.Context, name string) ([]keys.Info, error)

GetKeys reads all keys from the specified item in the file system and returns them as a slice of keys.Info.

Type KeySpecFlags
type KeySpecFlags struct {
	ID   string `subcmd:"key-id,,key id"`
	User string `subcmd:"key-user,,key user"`
}

KeySpecFlags defines command-line flags for specifying a key's ID and user.

Methods
func (f KeySpecFlags) KeySpec() keys.KeySpec

KeySpec returns a keys.KeySpec constructed from the KeySpecFlags.

Type KeyWriter
type KeyWriter struct {
	KeyReader
	// contains filtered or unexported fields
}

KeyWriter provides methods to write keys to a file system in the InMemoryKeyStore format.

Functions
func NewKeyWriter(fs file.ReadWriteFileFS) KeyWriter

NewKeyWriter creates a new KeyWriter that writes keys to an InMemoryKeyStore using the provided file.ReadWriteFileFS in the InMemoryKeyStore format.

Methods
func (w *KeyWriter) DeleteKey(ctx context.Context, name string, spec keys.KeySpec) error

DeleteKey removes a specific key from the specified item in the file system based on the provided keys.KeySpec. It works by reading all of the existing keys, removing the specified key, and then writing the updated list back to the file system.

func (w *KeyWriter) SetKeys(ctx context.Context, name string, update bool, keys ...keys.Info) error

SetKeys adds or updates keys in the specified item in the file system. If update is false, it will return an error if any of the keys already exist in the item. If update is true, it will overwrite existing keys with the same user and ID.

Type SecretConfig
type SecretConfig struct {
	Size   int          `yaml:"key-size" doc:"size of the secret in bytes"`
	Format SecretFormat `yaml:"key-format" doc:"format of the secret, one of raw, hex, base64"`
	ID     string       `yaml:"key-id" doc:"id of the key"`
	User   string       `yaml:"key-user" doc:"user/owner associated with the key"`
}
Methods
func (sc SecretConfig) New() (keys.Info, error)

NewSecret generates a new random secret of the specified size in bytes and format and returns it as a keys.Info object.

Type SecretConfigFlags
type SecretConfigFlags struct {
	Size   int                      `subcmd:"size,32,size of the secret in bytes"`
	Format flags.Enum[SecretFormat] `subcmd:"format,hex,'format of the secret, one of raw, hex, base64'"`
	ID     string                   `subcmd:"id,,id of the key"`
	User   string                   `subcmd:"user,,user/owner associated with the key"`
}
Methods
func (sf SecretConfigFlags) SecretConfig() SecretConfig
Type SecretFormat
type SecretFormat int

SecretFormat represents the format in which the secret is represented, such as raw bytes, hexadecimal, or base64 encoding.

Constants
SecretFormatRaw, SecretFormatHex, SecretFormatBase64
// SecretFormatRaw indicates that the secret is stored in raw byte format.
SecretFormatRaw SecretFormat = iota
// SecretFormatHex indicates that the secret is stored in hexadecimal format.
SecretFormatHex
// SecretFormatBase64 indicates that the secret is stored in base64.StdEncoding format.
SecretFormatBase64

Methods
func (SecretFormat) EnumValues() map[string]SecretFormat

EnumValues implements flags.EnumValues for SecretFormat. A new map is returned on each call so that callers cannot mutate the values shared by every SecretFormat.

Documentation

Overview

Package keyscmd provides a set of utilities for reading and writing multiple keys stored in a single item in a file system using the format used by keys.InMemoryKeyStore.

Index

Constants

This section is empty.

Variables

View Source
var ErrKeyInfoNotFound = errors.New("key info not found")
View Source
var ErrUpdateNotAllowed = errors.New("update not allowed")

Functions

func CopyContents

func CopyContents(ctx context.Context, srcFS file.ReadFileFS, src string, dstFS file.WriteFileFS, dst string, perm fs.FileMode) error

CopyContents copies the contents of the source file in srcFS to the destination file in dstFS.

func ExtensionSpec

func ExtensionSpec(name string) string

ExtensionSpec returns the subcmd extension tree for managing key info items in a keychain/secrets store, formatted with the provided name.

func IsDstSafe

func IsDstSafe(dst string) error

SafeStdout checks if the provided name refers to Stdout ("-" or empty) and if so, verifies that Stdout is piped. If Stdout is not piped, it returns an error.

func IsStdoutStdin

func IsStdoutStdin(name string) bool

IsStdoutStdin returns true if the provided name is "-" or empty, indicating that the operation should read from stdin or write to stdout.

func NewKeyInfoExtension

func NewKeyInfoExtension(name string, appendFn func(cmd *subcmd.CommandSetYAML) error) subcmd.Extension

NewKeyInfoExtension creates a new subcmd.Extension for key info management commands, name specifies the name of the command tree and also the name of the template variable used to include it in the parent command tree.

func ReadFSWithStdin

func ReadFSWithStdin(fs file.ReadFileFS, name string) file.ReadFileFS

ReadFSWithStdin returns a file.ReadFileFS that reads from stdin if the name is "-" or empty.

func ReadFromLocal

func ReadFromLocal(ctx context.Context, filename string, dstFS file.WriteFileFS, dst string, perm fs.FileMode) error

func ReadKeyInfoFromLocal

func ReadKeyInfoFromLocal(ctx context.Context, filename string, unmarshal func([]byte, any) error) (keys.Info, error)

ReadKeyInfoFromLocal reads key information from a local file or stdin, unmarshals it using the provided unmarshal function, and returns the resulting keys.Info.

func ReadKeyInfoFromLocalJSON

func ReadKeyInfoFromLocalJSON(ctx context.Context, filename string) (keys.Info, error)

ReadKeyInfoFromLocalJSON reads key information from a local JSON file or stdin and returns the resulting keys.Info.

func ReadKeyInfoFromLocalYAML

func ReadKeyInfoFromLocalYAML(ctx context.Context, filename string) (keys.Info, error)

ReadKeyInfoFromLocalYAML reads key information from a local YAML file or stdin and returns the resulting keys.Info.

func ReadWriteFSWithStdout

func ReadWriteFSWithStdout(fs file.ReadWriteFileFS, name string) file.ReadWriteFileFS

ReadWriterFSWithStdout returns a file.ReadWriteFileFS that reads from fs and writes to stdout if the name is "-" or empty.

func SafeWriteKeyInfoJSON

func SafeWriteKeyInfoJSON(ctx context.Context, ki keys.Info, dst string, perm fs.FileMode) error

func SafeWriteKeyInfoToLocal

func SafeWriteKeyInfoToLocal(ctx context.Context, ki keys.Info, marshal func(any) ([]byte, error), dst string, perm fs.FileMode) error

func SafeWriteKeyInfoYAML

func SafeWriteKeyInfoYAML(ctx context.Context, ki keys.Info, dst string, perm fs.FileMode) error

func SafeWriteToLocal

func SafeWriteToLocal(ctx context.Context, srcFS file.ReadFileFS, src string, filename string, perm fs.FileMode) error

func WriteFSWithStdout

func WriteFSWithStdout(fs file.WriteFileFS, name string) file.WriteFileFS

WriteFSWithStdout returns a file.WriteFileFS that writes to stdout if the name is "-" or empty.

Types

type KeyReader

type KeyReader struct {
	// contains filtered or unexported fields
}

KeyReader provides methods to read keys from a file system in the InMemoryKeyStore

func NewKeyReader

func NewKeyReader(fs file.ReadFileFS) KeyReader

NewKeyReader creates a new KeyReader that reads keys stored using the InMemoryKeyStore format using the provided file.ReadFileFS.

func (*KeyReader) GetKey

func (r *KeyReader) GetKey(ctx context.Context, name string, spec keys.KeySpec) (keys.Info, error)

GetKey retrieves a specific key from the specified item in the file system based on the provided keys.KeySpec. If the key is not found, it returns an error.

func (*KeyReader) GetKeys

func (r *KeyReader) GetKeys(ctx context.Context, name string) ([]keys.Info, error)

GetKeys reads all keys from the specified item in the file system and returns them as a slice of keys.Info.

type KeySpecFlags

type KeySpecFlags struct {
	ID   string `subcmd:"key-id,,key id"`
	User string `subcmd:"key-user,,key user"`
}

KeySpecFlags defines command-line flags for specifying a key's ID and user.

func (KeySpecFlags) KeySpec

func (f KeySpecFlags) KeySpec() keys.KeySpec

KeySpec returns a keys.KeySpec constructed from the KeySpecFlags.

type KeyWriter

type KeyWriter struct {
	KeyReader
	// contains filtered or unexported fields
}

KeyWriter provides methods to write keys to a file system in the InMemoryKeyStore format.

func NewKeyWriter

func NewKeyWriter(fs file.ReadWriteFileFS) KeyWriter

NewKeyWriter creates a new KeyWriter that writes keys to an InMemoryKeyStore using the provided file.ReadWriteFileFS in the InMemoryKeyStore format.

func (*KeyWriter) DeleteKey

func (w *KeyWriter) DeleteKey(ctx context.Context, name string, spec keys.KeySpec) error

DeleteKey removes a specific key from the specified item in the file system based on the provided keys.KeySpec. It works by reading all of the existing keys, removing the specified key, and then writing the updated list back to the file system.

func (*KeyWriter) SetKeys

func (w *KeyWriter) SetKeys(ctx context.Context, name string, update bool, keys ...keys.Info) error

SetKeys adds or updates keys in the specified item in the file system. If update is false, it will return an error if any of the keys already exist in the item. If update is true, it will overwrite existing keys with the same user and ID.

type SecretConfig

type SecretConfig struct {
	Size   int          `yaml:"key-size" doc:"size of the secret in bytes"`
	Format SecretFormat `yaml:"key-format" doc:"format of the secret, one of raw, hex, base64"`
	ID     string       `yaml:"key-id" doc:"id of the key"`
	User   string       `yaml:"key-user" doc:"user/owner associated with the key"`
}

func (SecretConfig) New

func (sc SecretConfig) New() (keys.Info, error)

NewSecret generates a new random secret of the specified size in bytes and format and returns it as a keys.Info object.

type SecretConfigFlags

type SecretConfigFlags struct {
	Size   int                      `subcmd:"size,32,size of the secret in bytes"`
	Format flags.Enum[SecretFormat] `subcmd:"format,hex,'format of the secret, one of raw, hex, base64'"`
	ID     string                   `subcmd:"id,,id of the key"`
	User   string                   `subcmd:"user,,user/owner associated with the key"`
}

func (SecretConfigFlags) SecretConfig

func (sf SecretConfigFlags) SecretConfig() SecretConfig

type SecretFormat

type SecretFormat int

SecretFormat represents the format in which the secret is represented, such as raw bytes, hexadecimal, or base64 encoding.

const (
	// SecretFormatRaw indicates that the secret is stored in raw byte format.
	SecretFormatRaw SecretFormat = iota
	// SecretFormatHex indicates that the secret is stored in hexadecimal format.
	SecretFormatHex
	// SecretFormatBase64 indicates that the secret is stored in base64.StdEncoding format.
	SecretFormatBase64
)

func (SecretFormat) EnumValues

func (SecretFormat) EnumValues() map[string]SecretFormat

EnumValues implements flags.EnumValues for SecretFormat. A new map is returned on each call so that callers cannot mutate the values shared by every SecretFormat.

Jump to

Keyboard shortcuts

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