Skip to content

Commit

Permalink
Added base64 encoding for metadata field (flyteorg#426)
Browse files Browse the repository at this point in the history
* 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
pmahindrakar-oss authored May 19, 2022
1 parent d0bda09 commit c0e62f4
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
22 changes: 22 additions & 0 deletions auth/encoding_utils.go
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
}
31 changes: 31 additions & 0 deletions auth/encoding_utils_test.go
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)
}
}
2 changes: 1 addition & 1 deletion auth/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func GetHTTPRequestCookieToMetadataHandler(authCtx interfaces.AuthenticationCont
}

if len(raw) > 0 {
meta.Set(UserInfoMDKey, string(raw))
meta.Set(UserInfoMDKey, EncodeBase64(raw))
}

return meta
Expand Down
1 change: 1 addition & 0 deletions auth/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ func TestGetHTTPRequestCookieToMetadataHandler(t *testing.T) {
req.AddCookie(&idCookie)

assert.Equal(t, "IDToken a.b.c", handler(ctx, req)["authorization"][0])
assert.Equal(t, "bnVsbA", handler(ctx, req).Get(UserInfoMDKey)[0])
}

func TestGetHTTPMetadataTaggingHandler(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions auth/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ func GRPCGetIdentityFromIDToken(ctx context.Context, clientID string, provider *
}

meta := metautils.ExtractIncoming(ctx)
userInfoStr := meta.Get(UserInfoMDKey)
userInfoDecoded, _ := DecodeFromBase64(meta.Get(UserInfoMDKey))
userInfo := &service.UserInfoResponse{}
if len(userInfoStr) > 0 {
err = json.Unmarshal([]byte(userInfoStr), userInfo)
if len(userInfoDecoded) > 0 {
err = json.Unmarshal(userInfoDecoded, userInfo)
if err != nil {
logger.Infof(ctx, "Could not unmarshal user info from metadata %v", err)
}
Expand Down

0 comments on commit c0e62f4

Please sign in to comment.