utils

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2026 License: Apache-2.0 Imports: 34 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateNonExistingFolder

func CreateNonExistingFolder(path string, mode fs.FileMode) error

func FileExists

func FileExists(filename string) bool

func FormatFilePath

func FormatFilePath(path string) string

func GenerateCSRAndPrivateKey

func GenerateCSRAndPrivateKey(privateKey, keyType, domain string, SAN []string) (string, []byte, error)

GenerateCSRAndPrivateKey generates a Certificate Signing Request (CSR) and a private key.

func GenerateFingerprint

func GenerateFingerprint(content []byte) string

func GenerateRequestID added in v0.6.4

func GenerateRequestID() string

GenerateRequestID generates a unique request ID using crypto/rand

func GetKeyType

func GetKeyType(keyTypeStr string) (certcrypto.KeyType, error)

GetKeyType the type from which private keys should be generated.

func GetRequestID added in v0.6.4

func GetRequestID(ctx context.Context) string

GetRequestID retrieves the request ID from the context

func HashAPIKey added in v0.8.1

func HashAPIKey(apiKey string) string

HashAPIKey returns the hash used to store a management API key. The write/emit algorithm is centralized here (sha256).

func HashToken added in v0.8.1

func HashToken(token string) string

HashToken returns the hash used to store/emit a bearer token. The write/emit algorithm is centralized here (sha256).

func LoggerWithRequestID added in v0.6.4

func LoggerWithRequestID(ctx context.Context, logger log.Logger) log.Logger

LoggerWithRequestID returns a logger with the request ID from context

func RandomStringCrypto

func RandomStringCrypto(numBytes int) (string, error)

RandomStringCrypto returns a base64url-encoded string derived from numBytes of cryptographically secure random data. The full encoding is returned without truncation so the caller keeps the requested entropy.

func RandomWeekdayBeforeExpiration

func RandomWeekdayBeforeExpiration(expiration time.Time, minDays, maxDays int) time.Time

Function to generate a random weekday within a given range before an expiration date

func RequestLogHook

func RequestLogHook(logger *logrus.Logger) retryablehttp.RequestLogHook

RequestLogHook logs outgoing HTTP requests

func ResponseLogHook

func ResponseLogHook(logger *logrus.Logger, logJSONBody bool) retryablehttp.ResponseLogHook

ResponseLogHook logs the response status code and body

func ResponseLogHookDebug

func ResponseLogHookDebug(logger *logrus.Logger) retryablehttp.ResponseLogHook

ResponseLogHookDebug logs all responses with full details

func SHA1Hash

func SHA1Hash(content string) string

SHA1Hash returns the sha1 hex digest of a string. Kept for legacy dual-read verification only; do not use on write paths.

func SHA256Hash added in v0.8.1

func SHA256Hash(content string) string

SHA256Hash returns the sha256 hex digest of a string.

func SanitizedDomain

func SanitizedDomain(logger log.Logger, domain string) (string, error)

func SecureCompare added in v0.8.1

func SecureCompare(a, b string) bool

SecureCompare reports whether a and b are equal using a constant-time comparison to avoid leaking secret length/content through timing.

func SetTLSConfig

func SetTLSConfig(cert string, key string, ca string, insecure bool) (*tls.Config, error)

func SetupLoggers added in v0.7.1

func SetupLoggers(logLevel, logFormat string) (log.Logger, *logrus.Logger)

SetupLoggers creates and configures all loggers (logrus, go-kit, and lego slog). Returns the go-kit logger and logrus logger.

func StructToMapInterface

func StructToMapInterface(data interface{}) map[string]interface{}

func ValidateCertificateName added in v0.9.0

func ValidateCertificateName(name string) error

SanitizedDomain validates a domain and returns a filesystem/Vault-path safe representation. It rejects empty, over-long or malformed domains, and allows at most a single leading "*." wildcard label. On any failure it returns a non-nil error instead of a partial value so callers never build paths from unvalidated input. ValidateCertificateName rejects names that would escape the storage layout. A named certificate is stored at "<prefix>/<owner>/<name>" in Vault and on disk, so a name carrying a path separator or a parent reference would resolve outside the owner's namespace. Anything else is accepted, to stay compatible with names already in use.

func ValidateLabels

func ValidateLabels(labels string) (errors []error)

func ValidateRenewalDays

func ValidateRenewalDays(value string) (int, int, error)

func VerifyHash added in v0.8.1

func VerifyHash(storedHash, plaintext string) bool

VerifyHash implements a dual-read verification: it returns true when the stored hash constant-time-matches the sha256 hash of plaintext (current write algorithm) OR the legacy sha1 hash of plaintext. This lets existing sha1-stored hashes keep working while all new writes use sha256.

func WithRequestID added in v0.6.4

func WithRequestID(ctx context.Context, requestID string) context.Context

WithRequestID adds a request ID to the context

Types

type CustomTextFormatter

type CustomTextFormatter struct {
	TimestampFormat  string
	CallerPrettyfier func(*runtime.Frame) (string, string)
}

CustomTextFormatter is a custom logrus formatter

func (*CustomTextFormatter) Format

func (f *CustomTextFormatter) Format(entry *logrus.Entry) ([]byte, error)

Format implements the logrus.Formatter interface

type CustomWriter

type CustomWriter struct {
	Writer io.Writer
}

Custom Writer to block INFO messages containg [DEBUG] in message

func (*CustomWriter) Write

func (cw *CustomWriter) Write(p []byte) (n int, err error)

type DebugLevelHook

type DebugLevelHook struct {
	Logger *logrus.Logger
}

Hook to redirect logs with message starting with [DEBUG] from INFO to DEBUG level

func (*DebugLevelHook) Fire

func (hook *DebugLevelHook) Fire(entry *logrus.Entry) error

func (*DebugLevelHook) Levels

func (hook *DebugLevelHook) Levels() []logrus.Level

type UTCFormatter

type UTCFormatter struct {
	logrus.Formatter
}

func (UTCFormatter) Format

func (u UTCFormatter) Format(e *logrus.Entry) ([]byte, error)

Jump to

Keyboard shortcuts

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