utils

package
v4.34.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2022 License: Apache-2.0 Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// RFC3339Zero is the default value for time.Time.Unix().
	RFC3339Zero = int64(-62135596800)

	// TLS13 is the textual representation of TLS 1.3.
	TLS13 = "1.3"

	// TLS12 is the textual representation of TLS 1.2.
	TLS12 = "1.2"

	// TLS11 is the textual representation of TLS 1.1.
	TLS11 = "1.1"

	// TLS10 is the textual representation of TLS 1.0.
	TLS10 = "1.0"
)
View Source
const (
	// Hour is an int based representation of the time unit.
	Hour = time.Minute * 60 //nolint:revive

	// Day is an int based representation of the time unit.
	Day = Hour * 24

	// Week is an int based representation of the time unit.
	Week = Day * 7

	// Year is an int based representation of the time unit.
	Year = Day * 365

	// Month is an int based representation of the time unit.
	Month = Year / 12
)
View Source
const (
	DurationUnitDays   = "d"
	DurationUnitWeeks  = "w"
	DurationUnitMonths = "M"
	DurationUnitYears  = "y"
)

Duration unit types.

View Source
const (
	HoursInDay   = 24
	HoursInWeek  = HoursInDay * 7
	HoursInMonth = HoursInDay * 30
	HoursInYear  = HoursInDay * 365
)

Number of hours in particular measurements of time.

Variables

View Source
var (
	// AlphaNumericCharacters are literally just valid alphanumeric chars.
	AlphaNumericCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
)
View Source
var BuildBranch = "master"

BuildBranch is replaced by LDFLAGS at build time with the current branch.

View Source
var BuildCommit = "unknown"

BuildCommit is replaced by LDFLAGS at build time with the current commit.

View Source
var BuildDate = ""

BuildDate is replaced by LDFLAGS at build time with the date the build started.

View Source
var BuildExtra = ""

BuildExtra is replaced by LDFLAGS at build time with a blank string by default. People porting Authelia can use this to add a suffix to their versions.

View Source
var BuildNumber = "0"

BuildNumber is replaced by LDFLAGS at build time with the CI build number.

View Source
var BuildState = "untagged dirty"

BuildState is replaced by LDFLAGS at build time with `tagged` or `untagged` depending on if the commit is tagged, and `clean` or `dirty` depending on the working tree state. For example if the commit was tagged and the working tree was dirty it would be "tagged dirty". This is used to determine the version string output mode.

View Source
var BuildTag = "unknown"

BuildTag is replaced by LDFLAGS at build time with the latest tag at or before the current commit.

View Source
var ErrTLSVersionNotSupported = errors.New("supplied tls version isn't supported")

ErrTLSVersionNotSupported returned when an unknown TLS version supplied.

View Source
var ErrTimeoutReached = errors.New("timeout reached")

ErrTimeoutReached error thrown when a timeout is reached.

Functions

func CheckUntil

func CheckUntil(interval time.Duration, timeout time.Duration, predicate func() (bool, error)) error

CheckUntil regularly check a predicate until it's true or time out is reached.

func Command

func Command(name string, args ...string) *exec.Cmd

Command create a command at the project root.

func CommandWithStdout

func CommandWithStdout(name string, args ...string) *exec.Cmd

CommandWithStdout create a command forwarding stdout and stderr to the OS streams.

func Decrypt

func Decrypt(ciphertext []byte, key *[32]byte) (plaintext []byte, err error)

Decrypt decrypts data using 256-bit AES-GCM. This both hides the content of the data and provides a check that it hasn't been altered. Expects input form nonce|ciphertext|tag where '|' indicates concatenation.

func DirectoryExists

func DirectoryExists(path string) (exists bool, err error)

DirectoryExists returns true if the given path exists and is a directory.

func Encrypt

func Encrypt(plaintext []byte, key *[32]byte) (ciphertext []byte, err error)

Encrypt encrypts data using 256-bit AES-GCM. This both hides the content of the data and provides a check that it hasn't been altered. Output takes the form nonce|ciphertext|tag where '|' indicates concatenation.

func ExportRsaPrivateKeyAsPemStr

func ExportRsaPrivateKeyAsPemStr(privkey *rsa.PrivateKey) string

ExportRsaPrivateKeyAsPemStr marshal a rsa private key into PEM string.

func ExportRsaPublicKeyAsPemStr

func ExportRsaPublicKeyAsPemStr(pubkey *rsa.PublicKey) (string, error)

ExportRsaPublicKeyAsPemStr marshal a RSA public into a PEM string.

func FileExists

func FileExists(path string) (exists bool, err error)

FileExists returns true if the given path exists and is a file.

func GenerateRsaKeyPair

func GenerateRsaKeyPair(bits int) (*rsa.PrivateKey, *rsa.PublicKey)

GenerateRsaKeyPair generate an RSA key pair. bits can be 2048 or 4096.

func GetExpectedErrTxt

func GetExpectedErrTxt(err string) string

GetExpectedErrTxt returns error text for expected errs.

func HashSHA256FromPath

func HashSHA256FromPath(path string) (output string, err error)

HashSHA256FromPath takes a path string and calculates the SHA256 checksum of the file at the path returning it as a base16 hash string.

func HashSHA256FromString

func HashSHA256FromString(input string) (output string)

HashSHA256FromString takes an input string and calculates the SHA256 checksum returning it as a base16 hash string.

func IsRedirectionSafe

func IsRedirectionSafe(url url.URL, protectedDomain string) bool

IsRedirectionSafe determines whether the URL is safe to be redirected to.

func IsRedirectionURISafe

func IsRedirectionURISafe(uri, protectedDomain string) (bool, error)

IsRedirectionURISafe determines whether the URI is safe to be redirected to.

func IsStringAbsURL

func IsStringAbsURL(input string) (err error)

IsStringAbsURL checks a string can be parsed as a URL and that is IsAbs and if it can't it returns an error describing why.

func IsStringAlphaNumeric

func IsStringAlphaNumeric(input string) bool

IsStringAlphaNumeric returns false if any rune in the string is not alpha-numeric.

func IsStringInSlice

func IsStringInSlice(needle string, haystack []string) (inSlice bool)

IsStringInSlice checks if a single string is in a slice of strings.

func IsStringInSliceContains

func IsStringInSliceContains(needle string, haystack []string) (inSlice bool)

IsStringInSliceContains checks if a single string is in an array of strings.

func IsStringInSliceFold

func IsStringInSliceFold(needle string, haystack []string) (inSlice bool)

IsStringInSliceFold checks if a single string is in a slice of strings but uses strings.EqualFold to compare them.

func IsStringInSliceSuffix

func IsStringInSliceSuffix(needle string, haystack []string) (hasSuffix bool)

IsStringInSliceSuffix checks if the needle string has one of the suffixes in the haystack.

func IsStringSliceContainsAll added in v4.33.0

func IsStringSliceContainsAll(needles []string, haystack []string) (inSlice bool)

IsStringSliceContainsAll checks if the haystack contains all strings in the needles.

func IsStringSliceContainsAny added in v4.33.0

func IsStringSliceContainsAny(needles []string, haystack []string) (inSlice bool)

IsStringSliceContainsAny checks if the haystack contains any of the strings in the needles.

func IsStringSlicesDifferent

func IsStringSlicesDifferent(a, b []string) (different bool)

IsStringSlicesDifferent checks two slices of strings and on the first occurrence of a string item not existing in the other slice returns true, otherwise returns false.

func IsStringSlicesDifferentFold

func IsStringSlicesDifferentFold(a, b []string) (different bool)

IsStringSlicesDifferentFold checks two slices of strings and on the first occurrence of a string item not existing in the other slice (case insensitive) returns true, otherwise returns false.

func NewTLSConfig

func NewTLSConfig(config *schema.TLSConfig, defaultMinVersion uint16, certPool *x509.CertPool) (tlsConfig *tls.Config)

NewTLSConfig generates a tls.Config from a schema.TLSConfig and a x509.CertPool.

func NewX509CertPool

func NewX509CertPool(directory string) (certPool *x509.CertPool, warnings []error, errors []error)

NewX509CertPool generates a x509.CertPool from the system PKI and the directory specified.

func ParseDurationString

func ParseDurationString(input string) (duration time.Duration, err error)

ParseDurationString standardizes a duration string with StandardizeDurationString then uses time.ParseDuration to convert it into a time.Duration.

func ParseRsaPrivateKeyFromPemStr

func ParseRsaPrivateKeyFromPemStr(privPEM string) (*rsa.PrivateKey, error)

ParseRsaPrivateKeyFromPemStr parse a RSA private key from PEM string.

func ParseRsaPublicKeyFromPemStr

func ParseRsaPublicKeyFromPemStr(pubPEM string) (*rsa.PublicKey, error)

ParseRsaPublicKeyFromPemStr parse RSA public key from a PEM string.

func PathExists

func PathExists(path string) (exists bool, err error)

PathExists returns true if the given path exists.

func RandomBytes added in v4.33.0

func RandomBytes(n int, characters string, crypto bool) (bytes []byte)

RandomBytes returns a random []byte with a given length with values from the provided characters. When crypto is set to false we use math/rand and when it's set to true we use crypto/rand. The crypto option should always be set to true excluding when the task is time sensitive and would not benefit from extra randomness.

func RandomString

func RandomString(n int, characters string, crypto bool) (randomString string)

RandomString returns a random string with a given length with values from the provided characters. When crypto is set to false we use math/rand and when it's set to true we use crypto/rand. The crypto option should always be set to true excluding when the task is time sensitive and would not benefit from extra randomness.

func RunCommandAndReturnOutput

func RunCommandAndReturnOutput(command string) (output string, exitCode int, err error)

RunCommandAndReturnOutput runs a shell command then returns the stdout and the exit code.

func RunCommandUntilCtrlC

func RunCommandUntilCtrlC(cmd *exec.Cmd)

RunCommandUntilCtrlC run a command until ctrl-c is hit.

func RunCommandWithTimeout

func RunCommandWithTimeout(cmd *exec.Cmd, timeout time.Duration) error

RunCommandWithTimeout run a command with timeout.

func RunFuncUntilCtrlC

func RunFuncUntilCtrlC(fn func() error) error

RunFuncUntilCtrlC run a function until ctrl-c is hit.

func RunFuncWithRetry

func RunFuncWithRetry(attempts int, sleep time.Duration, f func() error) (err error)

RunFuncWithRetry run a function for n attempts with a sleep of n duration between each attempt.

func Shell

func Shell(command string) *exec.Cmd

Shell create a shell command.

func SliceString

func SliceString(s string, d int) (array []string)

SliceString splits a string s into an array with each item being a max of int d d = denominator, n = numerator, q = quotient, r = remainder.

func StandardizeDurationString added in v4.34.0

func StandardizeDurationString(input string) (output string, err error)

StandardizeDurationString converts units of time that stdlib is unaware of to hours.

func StringHTMLEscape

func StringHTMLEscape(input string) (output string)

StringHTMLEscape escapes chars for a HTML body.

func StringSlicesDelta

func StringSlicesDelta(before, after []string) (added, removed []string)

StringSlicesDelta takes a before and after []string and compares them returning a added and removed []string.

func TLSStringToTLSConfigVersion

func TLSStringToTLSConfigVersion(input string) (version uint16, err error)

TLSStringToTLSConfigVersion returns a go crypto/tls version for a tls.Config based on string input.

func Version

func Version() (versionString string)

Version returns the Authelia version.

The format of the string is dependent on the values in BuildState. If tagged and clean are present it returns the BuildTag i.e. v1.0.0. If dirty and tagged are present it returns <BuildTag>-dirty. Otherwise the following is the format: untagged-<BuildTag>-dirty-<BuildExtra> (<BuildBranch>, <BuildCommit>).

Types

type Clock

type Clock interface {
	Now() time.Time
	After(d time.Duration) <-chan time.Time
}

Clock is an interface for a clock.

type ErrSliceSortAlphabetical

type ErrSliceSortAlphabetical []error

ErrSliceSortAlphabetical is a helper type that can be used with sort.Sort to sort a slice of errors in alphabetical order. Usage is simple just do sort.Sort(ErrSliceSortAlphabetical([]error{})).

func (ErrSliceSortAlphabetical) Len

func (s ErrSliceSortAlphabetical) Len() int

func (ErrSliceSortAlphabetical) Less

func (s ErrSliceSortAlphabetical) Less(i, j int) bool

func (ErrSliceSortAlphabetical) Swap

func (s ErrSliceSortAlphabetical) Swap(i, j int)

type RealClock

type RealClock struct{}

RealClock is the implementation of a clock for production code.

func (RealClock) After

func (RealClock) After(d time.Duration) <-chan time.Time

After return a channel receiving the time after the defined duration.

func (RealClock) Now

func (RealClock) Now() time.Time

Now return the current time.

Jump to

Keyboard shortcuts

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