forked from flyteorg/flyte
-
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.
Added base64 encoding for metadata field (flyteorg#426)
* Added base64 encoding for metadata field Signed-off-by: Prafulla Mahindrakar <[email protected]> * Fixed unit test Signed-off-by: Prafulla Mahindrakar <[email protected]> * removed empty test Signed-off-by: Prafulla Mahindrakar <[email protected]> * returing error during decode Signed-off-by: Prafulla Mahindrakar <[email protected]>
- Loading branch information
1 parent
d0bda09
commit c0e62f4
Showing
5 changed files
with
58 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
|
||
"github.com/flyteorg/flytestdlib/logger" | ||
) | ||
|
||
// EncodeBase64 returns the base64 encoded version of the data | ||
func EncodeBase64(raw []byte) string { | ||
return base64.RawStdEncoding.EncodeToString(raw) | ||
} | ||
|
||
// DecodeFromBase64 returns the original encoded bytes and logs warning in case of error | ||
func DecodeFromBase64(encodedData string) ([]byte, error) { | ||
decodedData, err := base64.StdEncoding.DecodeString(encodedData) | ||
if err != nil { | ||
logger.Warnf(context.TODO(), "Unable to decode %v due to %v", encodedData, err) | ||
} | ||
return decodedData, err | ||
} |
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,31 @@ | ||
package auth | ||
|
||
import ( | ||
"encoding/base64" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEncodeAscii(t *testing.T) { | ||
assert.Equal(t, "bmls", EncodeBase64([]byte("nil"))) | ||
assert.Equal(t, "w4RwZmVs", EncodeBase64([]byte("Äpfel"))) | ||
} | ||
|
||
func TestDecodeFromAscii(t *testing.T) { | ||
type data struct { | ||
decoded string | ||
encoded string | ||
expectedErr error | ||
} | ||
tt := []data{ | ||
{decoded: "nil", encoded: "bmls", expectedErr: nil}, | ||
{decoded: "Äpfel", encoded: "w4RwZmVs", expectedErr: nil}, | ||
{decoded: "", encoded: "Äpfel", expectedErr: base64.CorruptInputError(0)}, | ||
} | ||
for _, testdata := range tt { | ||
actualDecoded, actualErr := DecodeFromBase64(testdata.encoded) | ||
assert.Equal(t, []byte(testdata.decoded), actualDecoded) | ||
assert.Equal(t, testdata.expectedErr, actualErr) | ||
} | ||
} |
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