secret

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KeyLength = security.KeyLength
)

Variables

View Source
var (
	// ErrProfileID is returned when no profile ID is provided.
	ErrProfileID = errors.New("a profile ID must be provided")
	// ErrStorage is returned when no storage is provided.
	ErrStorage = errors.New("a storage path must be provided when using default storage")
	// ErrLoadCollection is returned when a collection load fails.
	ErrLoadCollection = errors.New("load collection failed")
	// ErrSaveCollection is returned when a collection save fails.
	ErrSaveCollection = errors.New("save collection failed")
	// ErrSecretNotFound is returned when a secret cannot be found.
	ErrSecretNotFound = errors.New("a secret with that identifier cannot be found")
)
View Source
var (
	// ErrInvalidKey is returned when the provided key is invalid.
	ErrInvalidKey = security.ErrInvalidKey
	// ErrInvalidKeyLength is returned when the provided key is not the correct size.
	ErrInvalidKeyLength = security.ErrInvalidKeyLength
)
View Source
var (
	// ErrSecretEncrypt is returned when an error is encountered when encrypting a secret.
	ErrSecretEncrypt = errors.New("encrypting secret")
	// ErrSecretDecrypt is returned when an error is encountered when decrypting a secret.
	ErrSecretDecrypt = errors.New("decrypting secret")
)
View Source
var (
	// ErrSecretAlreadyExists is returned when a secret already exists.
	ErrSecretAlreadyExists = errors.New("a secret with that ID or name already exists")
)

Functions

func Generate added in v0.2.0

func Generate(length int, specialChars bool) string

Generate a random string of the specified amount of characters, and if special characters should be included.

Types

type Collection

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

Collection represents a collection of secrets.

func NewCollection

func NewCollection(profileID string, options ...CollectionOption) Collection

NewCollection creates and returns a new collections.

func (*Collection) Add

func (c *Collection) Add(secret Secret) error

Add a secret to the collection. Returns true if secret was added, false if not (secret already exists).

func (Collection) Get

func (c Collection) Get(id string) Secret

Get a secret by ID.

func (Collection) GetByID

func (c Collection) GetByID(id string) Secret

GetByID gets a secret by the provided ID.

func (Collection) GetByName

func (c Collection) GetByName(name string) Secret

GetByName gets a secret by the provided name.

func (*Collection) GobDecode

func (c *Collection) GobDecode(b []byte) error

GobDecode populates the Collection from a binary format.

func (Collection) GobEncode

func (c Collection) GobEncode() ([]byte, error)

GobEncode serializes the Collection into a binary format.

func (Collection) List

func (c Collection) List() []Secret

List all secrets.

func (*Collection) Remove

func (c *Collection) Remove(id string) error

Remove a secret by the provided ID.

func (*Collection) RemoveByID

func (c *Collection) RemoveByID(id string) error

RemoveByID removes a secret by the provided ID.

func (*Collection) RemoveByName

func (c *Collection) RemoveByName(name string) error

RemoveByID removes a secret by the provided name.

func (*Collection) Set

func (c *Collection) Set(options ...CollectionOption)

Set options for a collection.

func (*Collection) Update

func (c *Collection) Update(secret Secret) error

Update a secret.

func (Collection) Updated

func (c Collection) Updated() time.Time

Updated returns when the collection was last modified.

type CollectionOption

type CollectionOption func(o *CollectionOptions)

CollectionOption is a function that sets options to CollectionOptions.

func WithExpireInterval

func WithExpireInterval(d time.Duration) CollectionOption

WithExpireInterfal sets expire interval on a collection.

type CollectionOptions

type CollectionOptions struct {
	Expires        time.Time
	ExpireInterval time.Duration
}

CollectionOptions contains options for a Collection.

type Handler

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

Handler represents a handler for a Collection and the storage configurations.

func NewHandler

func NewHandler(profileID string, storageKey, key security.Key, storage Storage, options ...HandlerOption) (*Handler, error)

NewHandler creates and returns a new Handler.

func (Handler) AddSecret

func (h Handler) AddSecret(name, value string, options ...SecretOption) (Secret, error)

AddSecret adds a new secret to the collection.

func (Handler) Collection

func (h Handler) Collection() *Collection

Collection returns the current collection set to the handler.

func (Handler) DeleteSecretByID

func (h Handler) DeleteSecretByID(id string) error

DeleteSecretByID deletes a secret by ID.

func (Handler) DeleteSecretByName

func (h Handler) DeleteSecretByName(name string) error

DeleteSecretByName deletes a secret by name.

func (Handler) GetSecretByID

func (h Handler) GetSecretByID(id string) (Secret, error)

GetSecretByID retrieves a secret by ID.

func (Handler) GetSecretByName

func (h Handler) GetSecretByName(name string) (Secret, error)

GetSecretByName retrieves a secret by Name.

func (Handler) ListSecrets

func (h Handler) ListSecrets() (Secrets, error)

ListSecrets lists all secrets.

func (*Handler) Load

func (h *Handler) Load() error

Load collection into Handler.

func (*Handler) Save

func (h *Handler) Save() error

Save collection.

func (*Handler) Sync

func (h *Handler) Sync() error

Sync current collection with collection from secondary storage (if any).

func (*Handler) UpdateKey

func (h *Handler) UpdateKey(key security.Key) error

UpdateKey updates the key on the handler and all all secrets.

func (Handler) UpdateSecretByID

func (h Handler) UpdateSecretByID(id string, options ...SecretOption) (Secret, error)

UpdateSecretByUD updates a secret in the collection by ID.

func (Handler) UpdateSecretByName

func (h Handler) UpdateSecretByName(name string, options ...SecretOption) (Secret, error)

UpdateSecretByName updates a secret in the collection by name.

type HandlerOption

type HandlerOption func(o *HandlerOptions)

HandlerOption is a function that sets HandlerOptions.

func WithLoadCollection

func WithLoadCollection() HandlerOption

WithLoadCollection() sets that collections should be loaded when creating a new handler.

func WithSecondaryStorage

func WithSecondaryStorage(storage Storage) HandlerOption

WithSecondaryStorage sets secondary storage for the Handler.

type HandlerOptions

type HandlerOptions struct {
	SecondaryStorage Storage
	LoadCollection   bool
}

HandlerOptions contains options for a Handler.

type Secret

type Secret struct {
	ID          string `json:"id"`
	Name        string `json:"name"`
	DisplayName string `json:"displayName,omitempty"`
	// Value is the encrypted value of a secret.
	Value   []byte            `json:"-"`
	Type    Type              `json:"type"`
	Labels  []string          `json:"labels,omitempty"`
	Tags    map[string]string `json:"tags,omitempty"`
	Created time.Time         `json:"created,omitempty"`
	Updated time.Time         `json:"updated,omitempty"`
	// contains filtered or unexported fields
}

Secret represents a secret and its data.

func NewSecret

func NewSecret(name, value string, key []byte, options ...SecretOption) (Secret, error)

NewSecret creates a new secret.

func (*Secret) Decrypt

func (s *Secret) Decrypt(options ...SecretOption) ([]byte, error)

Decrypt and return the Value of the Secret.

func (Secret) JSON

func (s Secret) JSON() []byte

JSON returns the JSON encoding of Secret.

func (*Secret) Set

func (s *Secret) Set(options ...SecretOption) error

Set options to a secret.

func (Secret) Valid

func (s Secret) Valid() bool

Valid returns true if the secret is valid, false if not.

type SecretOption

type SecretOption func(options *SecretOptions)

SecretOption is a function to set SecretOptions.

func WithKey

func WithKey(key []byte) SecretOption

WithKey sets key to SecretOptions.

func WithValue

func WithValue(value []byte) SecretOption

WithValue sets the value to SecretOptions.

type SecretOptions

type SecretOptions struct {
	DisplayName string
	Value       []byte
	Type        Type
	Labels      []string
	Tags        map[string]string
	Updated     time.Time
	// contains filtered or unexported fields
}

SecretOptions contains options for a secret.

type Secrets

type Secrets []Secret

Secrets is a slice of Secret.

func (Secrets) JSON

func (s Secrets) JSON() []byte

JSON returns the JSON encoding of Secrets.

type Storage

type Storage interface {
	Save(data []byte) error
	Load() ([]byte, error)
	Updated() (time.Time, error)
}

Storage is the interface that wraps around methods Save, Load and Updated.

type Type

type Type uint8

Type represents the type of secret.

const (
	// TypeGeneric represents a generic secret.
	TypeGeneric Type = iota
	// TypeCredential represents a credential secret.
	TypeCredential
	// TypeNote represents a secret note.
	TypeNote
	// TypeFile represents a secret file.
	TypeFile
)

func (Type) MarshalJSON

func (t Type) MarshalJSON() ([]byte, error)

MarshalJSON marshals the Type to its string representation for JSON.

func (Type) String

func (t Type) String() string

String returns the string representation of a secret type.

func (*Type) UnmarhsalJSON

func (t *Type) UnmarhsalJSON(data []byte) error

UnmarhsalJSON unmarshals the Type to its Type (uint8) representation.

Jump to

Keyboard shortcuts

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