helpers

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2022 License: Apache-2.0 Imports: 32 Imported by: 0

Documentation

Overview

Copyright 2020 New Relic Corporation. All rights reserved. SPDX-License-Identifier: Apache-2.0

Copyright 2020 New Relic Corporation. All rights reserved. SPDX-License-Identifier: Apache-2.0

Copyright 2020 New Relic Corporation. All rights reserved. SPDX-License-Identifier: Apache-2.0

Copyright 2020 New Relic Corporation. All rights reserved. SPDX-License-Identifier: Apache-2.0

Copyright 2020 New Relic Corporation. All rights reserved. SPDX-License-Identifier: Apache-2.0

Copyright 2020 New Relic Corporation. All rights reserved. SPDX-License-Identifier: Apache-2.0

Copyright 2020 New Relic Corporation. All rights reserved. SPDX-License-Identifier: Apache-2.0

Copyright 2020 New Relic Corporation. All rights reserved. SPDX-License-Identifier: Apache-2.0

Index

Constants

View Source
const (
	// Specific flavors of Linux
	LINUX_UNKNOWN = iota
	LINUX_DEBIAN
	LINUX_REDHAT
	LINUX_AWS_REDHAT
	LINUX_SUSE

	// Major OS families
	OS_OSX
	OS_WINDOWS
	OS_LINUX
	OS_UNKNOWN

	LINUX_COREOS
	FREEBSD
)
View Source
const (
	MaxBackoffErrorCount = 31
	// The Maximum size of the cache used to store sanitized file names.
	SanitizeFileNameCacheSize = 1000
)
View Source
const (
	// file path without etc prefix, use host etc in init to get appropriate prefix
	DefaultLinuxOsReleasePath = "/os-release"
)

Variables

View Source
var (
	JsonFilesRegexp = regexp.MustCompile("^[^~]+.json$")

	HiddenField = "<HIDDEN>"
)
View Source
var (
	ErrNoDockerd = errors.New("no active docker instance found")
)
View Source
var ISO8601RE = regexp.MustCompile(`(\d{4})-(\d{2})-(\d{2})T(\d{2})\:(\d{2})\:(\d{2})([zZ]|[\+|-]\d{2}:\d{2})`)
View Source
var SensitiveKeys = []string{"key", "secret", "password", "token", "passphrase", "credential"}

Functions

func CloseQuietly

func CloseQuietly(file io.Closer)

func CopyFile

func CopyFile(src, dest string) error

func DebugStackf

func DebugStackf(format string, args ...interface{})

DebugStackf logs a formatted debug message with information of the invokers' stacktrace. This function makes sure that no memory is generated if logger level lower to log.DebugLevel. This function is aimed only to debug messages from functions that can be invoked from multiple parts of the code.

func ExpBackoff

func ExpBackoff(base, max time.Duration, count uint32) time.Duration

Calculates the backoff as a function of the base and the maximum intervals, and the count of retries

func FileMD5

func FileMD5(filename string) (hash []byte, err error)

presupposes a regular file

func FlattenJson

func FlattenJson(parentKey string, data map[string]interface{}, jsonMap map[string]interface{}) map[string]interface{}

func GenerateMD5

func GenerateMD5(data string) ([]byte, error)

Helper to compute the MD5 digest of a particular string

func GetEnv

func GetEnv(key string, dfault string, combineWith ...string) string

GetEnv retrieves the environment variable key. If it does not exist it returns the default. From gopsutil

func GetLinuxDistro

func GetLinuxDistro() int

detect running linux platform/distro

func GetLinuxOSInfo

func GetLinuxOSInfo() (info map[string]string, err error)

func GetOS

func GetOS() int

func HostEtc

func HostEtc(combineWith ...string) string

func HostProc

func HostProc(combineWith ...string) string

func HostSys

func HostSys(combineWith ...string) string

func HostVar

func HostVar(combineWith ...string) string

func IsAmazonOS

func IsAmazonOS() bool

Detect whether the host is running Amazon Linux by looking for known AWS Linux OS files.

func IsDockerRunning

func IsDockerRunning() bool

func LogStructureDetails

func LogStructureDetails(logEntry log.Entry, sample interface{}, name, phase string, optionalFields logrus.Fields)

LogStructureDetails transforms a Go structure into json and logs it as a payload trace

func ObfuscateSensitiveData

func ObfuscateSensitiveData(value string) (matched, isField bool, result string)

ObfuscateSensitiveData is used to detect sensitive data like tokens/passwords etc and replace them by *. e.g. NRIA_CUSTOM_PASSWORD=1234 => NRIA_CUSTOM_PASSWORD=*

/usr/bin/custom_cmd -pwd 1234 -arg2 abc => /usr/bin/custom_cmd -pwd * -arg2 abc

func ObfuscateSensitiveDataFromArray

func ObfuscateSensitiveDataFromArray(data []string) []string

ObfuscateSensitiveDataFromArray will is used to detect sensitive data like tokens/passwords etc and replace them by *. If a value is matched and is considered a field(matched but no tokens/passwords founded) the next value will be considered related so obfuscated.

func ObfuscateSensitiveDataFromError

func ObfuscateSensitiveDataFromError(originalError error) (obfuscateError error)

func ObfuscateSensitiveDataFromMap

func ObfuscateSensitiveDataFromMap(data map[string]string) map[string]string

ObfuscateSensitiveDataFromMap will is used to detect sensitive data like tokens/passwords etc and replace them by *. If a key is matched is considered a field(matched but no tokens/passwords founded) the value will be obfuscated.

func ObfuscateSensitiveDataFromString

func ObfuscateSensitiveDataFromString(originalString string) (obfuscateString string)

func ReadFirstLine

func ReadFirstLine(filename string) string

ReadFirstLine will return just the first line of the file.

func RemoveEmptyAndDuplicateEntries

func RemoveEmptyAndDuplicateEntries(entries []string) []string

RemoveEmptyAndDuplicateEntries removes the empty and duplicate entries of a string slice, keeping the original order of the input slice

func RunCommand

func RunCommand(command string, stdin string, arguments ...string) (string, error)

func SanitizeCommandLine

func SanitizeCommandLine(cmd string) string

Cleans the command line to remove wrappers like quotation marks.

func SanitizeFileName

func SanitizeFileName(fileName string) string

Removes the next characters from a string so the resulting string can be used as a file name in any major operating system (Unix/Linux, Windows and Mac). - slash (/): folder separator in Unix - backslash (\): folder separator in Windows - colon (:): mountpoint drive in Windows - asterisk (*): wilcard in Unix and Windows - question mark (?): wildcard in Unix and Windows - quote ("): beginning and end of filename containing spaces in Windows - less than (<): redirect input - greater than (>): redirect output - vertical bar (|): software pipelining in Unix and Windows

func SanitizeJson

func SanitizeJson(jsonMap map[string]interface{}) map[string]interface{}

func SplitRightSubstring added in v0.1.0

func SplitRightSubstring(output, substring, separator string) string

SplitRightSubstring returns the first remaining part from the end of a substring and a given separator, if no substring or separator is found, and empty string is returned. Example: SplitRightSubstring("Hello: bye$", "Hello: ", "$") -> "bye"

Types

type Command

type Command struct {
	*exec.Cmd
}

func NewCommand

func NewCommand(command string, arguments ...string) *Command

func (*Command) Run

func (c *Command) Run() (string, error)

func (*Command) WithStdin

func (c *Command) WithStdin(stdin string) *Command

type Docker

type Docker interface {
	Initialize(apiVersion string) error
	Containers() ([]types.Container, error)
	ContainerTop(containerID string) (titles []string, processes [][]string, err error)
}

type DockerClient

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

func (*DockerClient) ContainerTop

func (dc *DockerClient) ContainerTop(containerID string) (titles []string, processes [][]string, err error)

func (*DockerClient) Containers

func (dc *DockerClient) Containers() ([]types.Container, error)

func (*DockerClient) Initialize

func (dc *DockerClient) Initialize(apiVersion string) (err error)

type NoCacheEntry

type NoCacheEntry struct{}

func (NoCacheEntry) Error

func (self NoCacheEntry) Error() string

type PersistentSocketTransport

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

func NewSocketTransport

func NewSocketTransport(path string) (*PersistentSocketTransport, error)

func (*PersistentSocketTransport) RoundTrip

func (self *PersistentSocketTransport) RoundTrip(req *http.Request) (r *http.Response, err error)

type ServiceDetails

type ServiceDetails struct {
	Pid       string    `json:"pid"`
	Ppid      string    `json:"ppid"`
	Uids      string    `json:"uids"`         // proc/pid/status
	Gids      string    `json:"gids"`         // proc/pid/status
	Listening string    `json:"listen_socks"` // combination of /proc/pid/fds and /proc/net/(tcp|udp)
	Started   time.Time `json:"-"`
}

func GetPidDetails

func GetPidDetails(pid int) (d ServiceDetails, err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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