safecli

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2024 License: Apache-2.0 Imports: 1 Imported by: 2

README

SafeCLI: Secure Command Line Interface Package

Go Report Card GitHub Actions

SafeCLI is a specialized Go package aimed at facilitating the secure construction, redaction, and logging of command-line arguments. It's designed particularly to handle sensitive values, ensuring they are managed securely throughout the process of command-line argument preparation and execution. This package is part of the Kanister project.

Features

  • Secure Argument Handling: Safely handles sensitive information in command-line arguments, ensuring that sensitive details are never exposed in logs or error messages.
  • Flexible CLI Construction: Provides a builder pattern for constructing command-line arguments dynamically, allowing for clean and readable code.
  • Redaction Interface: Integrates a redaction system that automatically obscures sensitive information when arguments are converted to strings for logging or debugging.
  • Logging Utility: Includes a logging utility that ensures sensitive information is redacted, while still providing helpful output for debugging and monitoring.

Installation

You can install SafeCLI by running:

go get -u github.com/kanisterio/safecli

Usage

Basic CLI Construction

To construct a CLI command, you can use the NewBuilder function which initializes a new command builder. You can append arguments, both loggable and sensitive, to the builder:

package main

import "github.com/kanisterio/safecli"

func main() {
    builder := safecli.NewBuilder("zip").
        AppendLoggableKV("--output", "/path/to/output").
        AppendRedactedKV("--password", "secretPass").
        AppendLoggable("input_file1", "input_file2")

    command := builder.Build()
    // Use `command` as required, e.g., execute it
}

In the above example, --password is a sensitive argument, and its value will be redacted in logs.

Secure Logging

To log a CLI command securely, ensuring that sensitive information is redacted:

logger := safecli.NewLogger(builder)
logOutput := logger.Log()
// Use `logOutput` for logging

Contribution

Contributions to the SafeCLI package are welcome. Please follow the contribution guidelines of the Kanister project when submitting patches or features.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CommandArgumentFormatter

func CommandArgumentFormatter(a Argument) string

CommandArgumentFormatter implements regular command formatter.

func LogArgumentFormatter

func LogArgumentFormatter(a Argument) string

LogArgumentFormatter implements log formatter.

Types

type Argument

type Argument struct {
	Key   string
	Value Redactor
}

Argument stores a key=value pair where the value is subject to redaction.

type ArgumentFormatter

type ArgumentFormatter func(a Argument) string

ArgumentFormatter defines a function that formats a command argument to the string.

type Builder

type Builder struct {
	// Args stores the command arguments.
	Args []Argument
	// Formatter defines a function that formats a command argument to the string.
	Formatter ArgumentFormatter
}

Builder is used for constructing CLI.

func NewBuilder

func NewBuilder(values ...string) *Builder

NewBuilder creates a new command builder instance. It takes a slice of string values which are appended to the builder.

func (*Builder) Append

func (b *Builder) Append(command CommandArguments) *Builder

Append combines the command arguments with the builder.

func (*Builder) AppendLoggable

func (b *Builder) AppendLoggable(values ...string) *Builder

AppendLoggable adds loggable values to the builder. These values can be logged and displayed as they do not have sensitive info.

func (*Builder) AppendLoggableKV

func (b *Builder) AppendLoggableKV(kvPairs ...string) *Builder

AppendLoggableKV adds key=value pairs to the builder. Key and value are loggable.

func (*Builder) AppendRedacted

func (b *Builder) AppendRedacted(values ...string) *Builder

AppendRedacted adds redacted values to the builder. These values are sensitive and should be display in logs as <****>.

func (*Builder) AppendRedactedKV

func (b *Builder) AppendRedactedKV(kvPairs ...string) *Builder

AppendRedactedKV adds key=value pairs to the builder. Key is loggable and value is sensitive. The value should be display in logs as <****>.

func (*Builder) Arguments

func (b *Builder) Arguments() []Argument

Arguments returns the command arguments.

func (*Builder) Build

func (b *Builder) Build() []string

Build builds and returns the command.

func (*Builder) String

func (b *Builder) String() string

String returns a string representation of the builder with hidden sensitive fields.

type CommandAppender

type CommandAppender interface {
	AppendLoggable(values ...string) *Builder
	AppendLoggableKV(kvPairs ...string) *Builder
	AppendRedacted(values ...string) *Builder
	AppendRedactedKV(kvPairs ...string) *Builder
	Append(command CommandArguments) *Builder
}

CommandAppender appends the command arguments to the builder.

type CommandArguments

type CommandArguments interface {
	Arguments() []Argument
}

CommandArguments provides an interface for accessing command arguments.

type CommandBuilder

type CommandBuilder interface {
	Build() []string
}

CommandBuilder builds and returns the command for execution.

type CommandLogger

type CommandLogger interface {
	Log() string
}

CommandLogger returns a string representation of the command for logging.

type Logger

type Logger struct {
	// Command stores the Command arguments.
	Command CommandArguments
	// Formatter defines a function that formats a command argument to the string.
	Formatter ArgumentFormatter
}

Logger is used for logging command arguments.

func NewLogger

func NewLogger(command CommandArguments) *Logger

NewLogger creates a new Logger instance.

func (*Logger) Log

func (l *Logger) Log() string

Log builds the loggable command string from the command arguments.

type PlainValue

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

PlainValue implements Redactor interface for non-sensitive data.

func (PlainValue) PlainString

func (l PlainValue) PlainString() string

PlainString returns a string as is.

func (PlainValue) String

func (l PlainValue) String() string

String returns a string as is, without redaction.

type Redactor

type Redactor interface {
	PlainString() string
	String() string
}

Redactor defines an interface for handling sensitive value in the way that it can be represented both in a plain and redacted text form.

type SensitiveValue

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

SensitiveValue implements Redactor interface for sensitive data.

func (SensitiveValue) GoString

func (r SensitiveValue) GoString() string

GoString returns a redacted string, never the actual value for a %#v format too.

func (SensitiveValue) PlainString

func (r SensitiveValue) PlainString() string

PlainString returns the original sensitive value.

func (SensitiveValue) String

func (r SensitiveValue) String() string

String returns a redacted string, never the actual value.

Jump to

Keyboard shortcuts

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