Skip to content

Commit d4bea90

Browse files
authored
Prepare v3 release (#35)
Prepare for v3 release Rename `.RedactHint()` to `.RedactAs()` as more meaningful name that is more inline with its purpose. Rename `.Value()` to `.Secret()` as more meaningful name to return secret string. Polish doc comments. Restructure and cleanup README to be in sync with latest changes.
1 parent 4526b39 commit d4bea90

File tree

3 files changed

+54
-58
lines changed

3 files changed

+54
-58
lines changed

README.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,32 @@
22
[![Build Status](https://github.com/rsjethani/secret/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/rsjethani/secret/actions)
33
[![Go Report Card](https://goreportcard.com/badge/github.com/rsjethani/secret)](https://goreportcard.com/report/github.com/rsjethani/secret)
44

5-
# secret v2
6-
7-
### What secret is?
8-
- It provides simple Go types like [secret.Text](https://pkg.go.dev/github.com/rsjethani/secret/v2#Text) to encapsulate your secret. Example:
5+
# Installation
6+
```
7+
go get github.com/rsjethani/secret/v3
98
```
9+
10+
# What secret is?
11+
It provides simple Go types like `Text` to securely store secrets. For example:
12+
```go
1013
type Login struct {
1114
User string
1215
Password secret.Text
1316
}
1417
```
15-
- The encapsulated secret remains inaccessible to operations like printing, logging, JSON serializtion etc. A (customizable) redact hint like `*****` is returned instead.
16-
- The only way to access the actual secret value is by asking explicitly via the `.Value()` method.
17-
- See [godev reference](https://pkg.go.dev/github.com/rsjethani/secret/v2#pkg-examples) for usage examples.
18+
The encapsulated secret remains inaccessible to operations like printing, logging, JSON serialization etc. A (customizable) redact hint like `*****` is returned instead. The only way to access the actual secret value is by asking explicitly via the `.Secret()` method. See package documentation more reference and examples.
1819

19-
### What secret is not?
20+
# What secret is not?
2021
- It is not a secret management service or your local password manager.
2122
- It is not a Go client to facilitate communication with secret managers like Hashicorp Vault, AWS secret Manager etc. Checkout [teller](https://github.com/spectralops/teller) if that is what you are looking for.
2223

23-
### Installation
24-
```
25-
go get github.com/rsjethani/secret/v2
26-
```
27-
NOTE: v1 is deprecated now.
24+
# Versions
25+
26+
### v3
27+
Current version, same functionality as v2 but much cleaner API.
28+
29+
### v2
30+
Perfectly usable but no longer maintained.
2831

32+
### v1
33+
Deprecated.

example_test.go

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,22 @@ import (
77
"github.com/rsjethani/secret/v2"
88
)
99

10-
func ExampleText() {
11-
s := secret.Text{}
12-
fmt.Println(s, s.Value())
13-
14-
// Output: *****
15-
}
16-
1710
func ExampleNew() {
1811
s := secret.New("$ecre!")
19-
fmt.Println(s, s.Value())
12+
fmt.Println(s, s.Secret())
2013

2114
// Output: ***** $ecre!
2215
}
2316

24-
func ExampleRedactHint() {
25-
s := secret.New("$ecre!", secret.RedactHint(secret.FiveX))
26-
fmt.Println(s, s.Value())
17+
func ExampleRedactAs() {
18+
s := secret.New("$ecre!", secret.RedactAs(secret.FiveX))
19+
fmt.Println(s, s.Secret())
2720

28-
s = secret.New("$ecre!", secret.RedactHint(secret.Redacted))
29-
fmt.Println(s, s.Value())
21+
s = secret.New("$ecre!", secret.RedactAs(secret.Redacted))
22+
fmt.Println(s, s.Secret())
3023

31-
s = secret.New("$ecre!", secret.RedactHint("my redact hint"))
32-
fmt.Println(s, s.Value())
24+
s = secret.New("$ecre!", secret.RedactAs("my redact hint"))
25+
fmt.Println(s, s.Secret())
3326

3427
// Output:
3528
// XXXXX $ecre!
@@ -68,7 +61,7 @@ func ExampleText_UnmarshalText() {
6861
}
6962

7063
fmt.Printf("%+v\n", login)
71-
fmt.Println(login.Password.Value())
64+
fmt.Println(login.Password.Secret())
7265

7366
// Output:
7467
// {User:John Password:*****}
@@ -77,7 +70,7 @@ func ExampleText_UnmarshalText() {
7770

7871
func ExampleEqual() {
7972
tx1 := secret.New("hello")
80-
tx2 := secret.New("hello", secret.RedactHint(secret.Redacted))
73+
tx2 := secret.New("hello", secret.RedactAs(secret.Redacted))
8174
tx3 := secret.New("world")
8275
fmt.Println(secret.Equal(tx1, tx2))
8376
fmt.Println(secret.Equal(tx1, tx3))

secret.go

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
// Package secret provides types to guard your secret values from leaking into logs, std* etc.
2-
//
3-
// The objective is to disallow writing/serializing of secret values to std*, logs, JSON string
4-
// etc. but provide access to the secret when requested explicitly.
51
package secret
62

7-
// Text provides a way to safely store your secret value and a corresponding redact hint. This
8-
// redact hint is what is used in operations like printing and serializing.
3+
// Text provides a way to safely store your secret string until you actually need it. Operations
4+
// like printing and serializing see a proxy/redact string there by avoiding leaking the secret.
5+
// Once created, the instance is readonly except for the [Text.UnmarshalText] operation, but that
6+
// too only modifies the local copy. Hence the type is concurrent safe.
97
type Text struct {
108
secret *string
119
redact *string
1210
}
1311

14-
// New returns [Text] for the secret with [FiveStar] as the default redact hint. Provide options
15-
// like [RedactHint] to modify default behavior.
12+
// New returns [Text] for the secret with [FiveStar] as the default redact string. Provide options
13+
// like [RedactAs] to modify default behavior.
1614
func New(secret string, options ...func(*Text)) Text {
1715
tx := Text{
1816
secret: new(string),
@@ -29,36 +27,36 @@ func New(secret string, options ...func(*Text)) Text {
2927
return tx
3028
}
3129

32-
// Some common redact hints.
30+
// RedactAs is a functional option to set r as the redact string for [Text]. You can use one of
31+
// the common redact strings provided with this package like [FiveX] or provide your own.
32+
func RedactAs(r string) func(*Text) {
33+
return func(t *Text) {
34+
*t.redact = r
35+
}
36+
}
37+
38+
// Some common redact strings.
3339
const (
3440
FiveX string = "XXXXX"
3541
FiveStar string = "*****"
3642
Redacted string = "[REDACTED]"
3743
)
3844

39-
// RedactHint is a functional option to set r as the redact hint for the [Text]. You can use one of
40-
// the common redact hints provided with this package like [FiveX] or provide your own string.
41-
func RedactHint(r string) func(*Text) {
42-
return func(t *Text) {
43-
*t.redact = r
44-
}
45-
}
46-
47-
// String implements the [fmt.Stringer] interface and returns only the redact hint. This prevents the
48-
// secret value from being printed to std*, logs etc.
45+
// String implements the [fmt.Stringer] interface and returns only the redact string. This prevents
46+
// the actual secret string from being sent to std*, logs etc.
4947
func (tx Text) String() string {
50-
if tx.redact == nil {
51-
return FiveStar
48+
if tx.redact != nil {
49+
return *tx.redact
5250
}
53-
return *tx.redact
51+
return FiveStar
5452
}
5553

56-
// Value gives you access to the actual secret value stored inside Text.
57-
func (tx Text) Value() string {
58-
if tx.secret == nil {
59-
return ""
54+
// Secret returns the actual secret string stored inside [Text].
55+
func (tx Text) Secret() string {
56+
if tx.secret != nil {
57+
return *tx.secret
6058
}
61-
return *tx.secret
59+
return ""
6260
}
6361

6462
// MarshalText implements [encoding.TextMarshaler]. It marshals redact string into bytes rather than
@@ -74,7 +72,7 @@ func (tx *Text) UnmarshalText(b []byte) error {
7472

7573
// If the original redact is not nil then use it otherwise fallback to default.
7674
if tx.redact != nil {
77-
*tx = New(s, RedactHint(*tx.redact))
75+
*tx = New(s, RedactAs(*tx.redact))
7876
} else {
7977
*tx = New(s)
8078
}

0 commit comments

Comments
 (0)