sensitive

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2020 License: MIT Imports: 3 Imported by: 0

README

Package sensitive

Project status Build Status GoDoc License

Package sensitive provides base types who's values should never be seen by the human eye, but still used for configuration.

What? Explain

Sometimes you have a variable, such as a password, passed into your program via arguments or ENV variables. Some of these variables are very sensitive! and should not in any circumstance be loggged or sent via JSON, despite JSON's "-", which people may forget. These variables, which are just typed primitive types, have their overridden fmt.Formatter, encoding.MarshalText & json.Marshal implementations.

As an added bonus using them as their base type eg. String => string, you have to explicitly cast the eg. string(s) This makes you think about what you're doing and why you casting it providing additional safelty.

Variables:

  • String - The most useful
  • Bool
  • Float32
  • Float64
  • Int
  • Int8
  • Int16
  • Int32
  • Int64
  • Uint
  • Uint8
  • Uint16
  • Uint32
  • Uint64

Example

// go run _examples/basic/main.go mypassword
package main

import (
	"encoding/json"
	"fmt"
	"os"

	"github.com/go-playground/sensitive"
)

func main() {
	password := sensitive.String(os.Args[1])

	fmt.Printf("%s\n", password)
	fmt.Printf("%v\n", password)

	b, _ := json.Marshal(password)
	fmt.Println(string(b))

	var empty *sensitive.String
	b, _ = json.Marshal(empty)
	fmt.Println(string(b))

	// output:
	//
	//
	// ""
	// null
}

Custom Formatting

package main

import (
	"encoding/json"
	"fmt"
	"io"
	"os"

	"github.com/go-playground/sensitive"
)

func init() {
	// override default Formatter
	sensitive.FormatStringFn = func(s sensitive.String, f fmt.State, c rune) {
		switch c {
		case 's':
			_, _ = io.WriteString(f, "redacted")
		case 'v':
			_, _ = io.WriteString(f, string(s)[:4]+"*******")
		}
	}
}

func main() {
	password := sensitive.String(os.Args[1])

	fmt.Printf("%s\n", password)
	fmt.Printf("%v\n", password)

	b, _ := json.Marshal(password)
	fmt.Println(string(b))

	var empty *sensitive.String
	b, _ = json.Marshal(empty)
	fmt.Println(string(b))

	// output:
	// redacted
	// mypa*******
	// "redacted"
	// null
}

License

Distributed under MIT License, please see license file in code for more details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	FormatBoolFn = func(s Bool, f fmt.State, c rune) {}
)
View Source
var (
	FormatFloat32Fn = func(s Float32, f fmt.State, c rune) {}
)
View Source
var (
	FormatFloat64Fn = func(s Float64, f fmt.State, c rune) {}
)
View Source
var (
	FormatInt16Fn = func(s Int16, f fmt.State, c rune) {}
)
View Source
var (
	FormatInt32Fn = func(s Int32, f fmt.State, c rune) {}
)
View Source
var (
	FormatInt64Fn = func(s Int64, f fmt.State, c rune) {}
)
View Source
var (
	FormatInt8Fn = func(s Int8, f fmt.State, c rune) {}
)
View Source
var (
	FormatIntFn = func(s Int, f fmt.State, c rune) {}
)
View Source
var (
	FormatStringFn = func(s String, f fmt.State, c rune) {}
)
View Source
var (
	FormatUint16Fn = func(s Uint16, f fmt.State, c rune) {}
)
View Source
var (
	FormatUint32Fn = func(s Uint32, f fmt.State, c rune) {}
)
View Source
var (
	FormatUint64Fn = func(s Uint64, f fmt.State, c rune) {}
)
View Source
var (
	FormatUint8Fn = func(s Uint8, f fmt.State, c rune) {}
)
View Source
var (
	FormatUintFn = func(s Uint, f fmt.State, c rune) {}
)

Functions

This section is empty.

Types

type Bool

type Bool bool

func (Bool) Format

func (s Bool) Format(f fmt.State, c rune)

func (Bool) MarshalJSON

func (s Bool) MarshalJSON() ([]byte, error)

func (Bool) MarshalText

func (s Bool) MarshalText() (text []byte, err error)

type Float32

type Float32 float32

func (Float32) Format

func (s Float32) Format(f fmt.State, c rune)

func (Float32) MarshalJSON

func (s Float32) MarshalJSON() ([]byte, error)

func (Float32) MarshalText

func (s Float32) MarshalText() (text []byte, err error)

type Float64

type Float64 float64

func (Float64) Format

func (s Float64) Format(f fmt.State, c rune)

func (Float64) MarshalJSON

func (s Float64) MarshalJSON() ([]byte, error)

func (Float64) MarshalText

func (s Float64) MarshalText() (text []byte, err error)

type Int

type Int int

func (Int) Format

func (s Int) Format(f fmt.State, c rune)

func (Int) MarshalJSON

func (s Int) MarshalJSON() ([]byte, error)

func (Int) MarshalText

func (s Int) MarshalText() (text []byte, err error)

type Int16

type Int16 int16

func (Int16) Format

func (s Int16) Format(f fmt.State, c rune)

func (Int16) MarshalJSON

func (s Int16) MarshalJSON() ([]byte, error)

func (Int16) MarshalText

func (s Int16) MarshalText() (text []byte, err error)

type Int32

type Int32 int32

func (Int32) Format

func (s Int32) Format(f fmt.State, c rune)

func (Int32) MarshalJSON

func (s Int32) MarshalJSON() ([]byte, error)

func (Int32) MarshalText

func (s Int32) MarshalText() (text []byte, err error)

type Int64

type Int64 int64

func (Int64) Format

func (s Int64) Format(f fmt.State, c rune)

func (Int64) MarshalJSON

func (s Int64) MarshalJSON() ([]byte, error)

func (Int64) MarshalText

func (s Int64) MarshalText() (text []byte, err error)

type Int8

type Int8 int8

func (Int8) Format

func (s Int8) Format(f fmt.State, c rune)

func (Int8) MarshalJSON

func (s Int8) MarshalJSON() ([]byte, error)

func (Int8) MarshalText

func (s Int8) MarshalText() (text []byte, err error)

type State

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

func (State) Flag

func (s State) Flag(c int) bool

func (State) Precision

func (s State) Precision() (prec int, ok bool)

func (State) Width

func (s State) Width() (wid int, ok bool)

func (*State) Write

func (s *State) Write(b []byte) (n int, err error)

type String

type String string

func (String) Format

func (s String) Format(f fmt.State, c rune)

func (String) MarshalJSON

func (s String) MarshalJSON() ([]byte, error)

func (String) MarshalText

func (s String) MarshalText() (text []byte, err error)

type Uint

type Uint uint

func (Uint) Format

func (s Uint) Format(f fmt.State, c rune)

func (Uint) MarshalJSON

func (s Uint) MarshalJSON() ([]byte, error)

func (Uint) MarshalText

func (s Uint) MarshalText() (text []byte, err error)

type Uint16

type Uint16 uint16

func (Uint16) Format

func (s Uint16) Format(f fmt.State, c rune)

func (Uint16) MarshalJSON

func (s Uint16) MarshalJSON() ([]byte, error)

func (Uint16) MarshalText

func (s Uint16) MarshalText() (text []byte, err error)

type Uint32

type Uint32 uint32

func (Uint32) Format

func (s Uint32) Format(f fmt.State, c rune)

func (Uint32) MarshalJSON

func (s Uint32) MarshalJSON() ([]byte, error)

func (Uint32) MarshalText

func (s Uint32) MarshalText() (text []byte, err error)

type Uint64

type Uint64 uint64

func (Uint64) Format

func (s Uint64) Format(f fmt.State, c rune)

func (Uint64) MarshalJSON

func (s Uint64) MarshalJSON() ([]byte, error)

func (Uint64) MarshalText

func (s Uint64) MarshalText() (text []byte, err error)

type Uint8

type Uint8 uint8

func (Uint8) Format

func (s Uint8) Format(f fmt.State, c rune)

func (Uint8) MarshalJSON

func (s Uint8) MarshalJSON() ([]byte, error)

func (Uint8) MarshalText

func (s Uint8) MarshalText() (text []byte, err error)

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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