Giter VIP home page Giter VIP logo

redact's Introduction

redact GoDoc Badge

redact is a Golang library for redacting sensitive values from a struct. This is useful when you're logging requests and responses on a server, but some payloads contain secrets or other values that shouldn't be logged.

Example

redact provides a Redact function that takes a path and the value you want to mutate. It works on struct elements:

import "github.com/ucarion/redact"

type User struct {
  Name string
  Password string
}

user := User{Name: "John", Password: "letmein"}
fmt.Println(user) // {John letmein}

redact.Redact([]string{"Password"}, &user)
fmt.Println(user) // {John }

This works even if the data you want to redact is nested:

type CreateUserRequest struct {
  RequestID string
  User      User
}

req := CreateUserRequest{RequestID: "abc", User: User{Name: "John", Password: "letmein"}}
fmt.Println(req) // {abc {John letmein}}

redact.Redact([]string{"User", "Password"}, &req)
fmt.Println(req) // {abc {John }}

It also works on arrays and slices. In that case, every element of the array or slice gets recursively redacted:

users := []User{
  User{Name: "John", Password: "letmein"},
  User{Name: "Mary", Password: "123456"},
}

fmt.Println(users) // [{John letmein}, {Mary 123456}]

redact.Redact([]string{"Password"}, &users)
fmt.Println(users) // [{John }, {Mary }]

It also works on maps in a way similar to structs.

user := map[string]string{
  "Name": "John",
  "Password": "letmein",
}
fmt.Println(user) // {Name: John, Password: letmein}

redact.Redact([]string{"Password"}, &user)
fmt.Println(user) // {Name: John, Password: }

Note, however, that Go does not support mutating map elements. That's not something any package can work around. So if you want to mutate elements of a map (in the example above, we replaced Password, not modified it). You'll have to make the map elements be pointers:

users := map[string]*User{
  "a": &User{Name: "John", Password: "letmein"},
}
fmt.Println(users["a"]) // &{John letmein}

redact.Redact([]string{"a", "Password"}, &users)
fmt.Println(users["a"]) // &{John }

redact's People

Contributors

ucarion avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.