-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Manage encoding when replacing values
There is an unused option in kustomize `ReplacementTransformer` named `encoding`. We make this option usable an provide 3 encodings: - base64 - hex - bcrypt This is useful to have values in plain text in replacement sources and encode them in the destination. The `bcrypt` option may provide some issues has the value would change for each run.
- Loading branch information
1 parent
b1f6e4a
commit fa6ba12
Showing
8 changed files
with
142 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package extras | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/hex" | ||
"fmt" | ||
"strings" | ||
|
||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
// EncodingType enumerates the existing encoding types. | ||
// | ||
//go:generate go run golang.org/x/tools/cmd/stringer -type=EncodingType | ||
type EncodingType int | ||
|
||
const ( | ||
UnknownEncoding EncodingType = iota | ||
Base64Encoding | ||
BCryptEncoding | ||
HexEncoding | ||
) | ||
|
||
var stringToEncodingTypeMap map[string]EncodingType | ||
|
||
func init() { //nolint:gochecknoinits | ||
stringToEncodingTypeMap = makeStringToEncodingTypeMap() | ||
} | ||
|
||
// makeStringToEncodingTypeMap makes a map to get the appropriate | ||
// [EncodingType] given its name. | ||
func makeStringToEncodingTypeMap() (result map[string]EncodingType) { | ||
result = make(map[string]EncodingType, 3) | ||
for k := range EncoderFactories { | ||
result[strings.Replace(strings.ToLower(k.String()), "encoding", "", 1)] = k | ||
} | ||
return | ||
} | ||
|
||
// getEncodingType returns the appropriate [EncodingType] for the passed | ||
// encoding type name | ||
func getEncodingType(n string) EncodingType { | ||
result, ok := stringToEncodingTypeMap[strings.ToLower(n)] | ||
if ok { | ||
return result | ||
} | ||
return UnknownEncoding | ||
} | ||
|
||
// Encoder is an encoder function | ||
type Encoder func(value string) (string, error) | ||
|
||
// EncodeBase64 encodes value in base64 | ||
func EncodeBase64(value string) (string, error) { | ||
return base64.StdEncoding.EncodeToString([]byte(value)), nil | ||
} | ||
|
||
// EncodeBcrypt generates the bcrypt hash of value. | ||
func EncodeBcrypt(value string) (string, error) { | ||
encoded, err := bcrypt.GenerateFromPassword([]byte(value), 10) | ||
|
||
return string(encoded), err | ||
} | ||
|
||
// EncodeHex returns the hex string of value | ||
func EncodeHex(value string) (string, error) { | ||
return hex.EncodeToString([]byte(value)), nil | ||
} | ||
|
||
// EncoderFactories register the [Encoder] factory functions for each | ||
// [EncoderType]. | ||
var EncoderFactories = map[EncodingType]Encoder{ | ||
Base64Encoding: EncodeBase64, | ||
BCryptEncoding: EncodeBcrypt, | ||
HexEncoding: EncodeHex, | ||
} | ||
|
||
func GetEncodedValue(value string, encoding string) (string, error) { | ||
et := getEncodingType(encoding) | ||
if f, ok := EncoderFactories[et]; ok { | ||
return f(value) | ||
} | ||
|
||
return "", fmt.Errorf("encoding %s is unknown (%d)", encoding, et) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,3 +34,4 @@ data: | |
teamNameField: slug | ||
orgs: | ||
- name: johndoe | ||
b64encoded: dGhpc2lzZmFrZXZhbHVl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ data: | |
repo: antoinemartin/autocloud | ||
email: [email protected] | ||
clientID: thisisfakeclientid | ||
to_encode: This is the value to encode in base64 |