Skip to content

Commit

Permalink
Remove authC
Browse files Browse the repository at this point in the history
Signed-off-by: Juraci Paixão Kröhling <juraci@kroehling.de>
  • Loading branch information
jpkrohling committed Oct 25, 2021
1 parent f465342 commit 126164a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 134 deletions.
59 changes: 3 additions & 56 deletions config/configauth/pdatacontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ package configauth

import (
"context"
"encoding/json"
"fmt"
"reflect"

"go.opentelemetry.io/collector/model/pdata"
)
Expand All @@ -28,15 +25,15 @@ type ctxKey struct{}
type AuthContext interface {
Equal(other interface{}) bool
GetAttribute(attrName string) interface{}
GetAttributeNames() []string
}

func InjectAuthContext(ctx context.Context, attrs map[interface{}]interface{}) context.Context {
ac := &authC{delegate: attrs}
func InjectAuthContext(ctx context.Context, ac AuthContext) context.Context {
return context.WithValue(ctx, ctxKey{}, ac)
}

func ExtractAuthContext(ctx context.Context) (AuthContext, bool) {
ac, ok := ctx.Value(ctxKey{}).(*authC)
ac, ok := ctx.Value(ctxKey{}).(AuthContext)
if !ok {
return nil, false
}
Expand All @@ -50,53 +47,3 @@ func InjectPDataContext(pda pdata.PDataContext, ac AuthContext) {
func ExtractPDataContext(pda pdata.PDataContext) AuthContext {
return pda.Get(ctxKey{}).(AuthContext)
}

type authC struct {
delegate map[interface{}]interface{}
}

func (ac authC) Equal(other interface{}) bool {
if other == nil {
return false
}
otherAuthC, ok := other.(*authC)
if !ok {
return false
}
return reflect.DeepEqual(ac.delegate, otherAuthC.delegate)
}

func (ac authC) GetAttribute(attrName string) interface{} {
return ac.delegate[attrName]
}

func (ac authC) String() string {
return fmt.Sprintf("Auth Context: %v", ac.delegate)
}

// MarshalJSON serializes our context into a JSON blob. Note that interface{} is converted to string or []string.
func (ac *authC) MarshalJSON() ([]byte, error) {
strmap := make(map[string]interface{})
for k, v := range ac.delegate {
strmap[fmt.Sprintf("%v", k)] = v
}
return json.Marshal(strmap)
}

// UnmarshalJSON deserializes our context from a JSON blob. Note that we cannot infer the original type before the serialization,
// so all entries are either string or []string.
func (ac *authC) UnmarshalJSON(data []byte) error {
strmap := make(map[string]interface{})
if err := json.Unmarshal(data, &strmap); err != nil {
return err
}

// converts the map[string]interface{} to map[interface{}]interface{}
delegate := make(map[interface{}]interface{})
for k, v := range strmap {
delegate[k] = v
}
ac.delegate = delegate

return nil
}
78 changes: 0 additions & 78 deletions config/configauth/pdatacontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,81 +13,3 @@
// limitations under the License.

package configauth

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)

func TestMarshalJSON(t *testing.T) {
for _, tt := range []struct {
name string
ac *authC
expected string
}{
{
name: "simple",
ac: &authC{
delegate: map[interface{}]interface{}{
"subject": "username",
"tenant": "acme",
},
},
expected: `{"subject":"username","tenant":"acme"}`,
},
{
name: "groups",
ac: &authC{
delegate: map[interface{}]interface{}{
"subject": "username",
"membership": []string{"dev", "ops"},
},
},
expected: `{"membership":["dev","ops"],"subject":"username"}`,
},
} {
t.Run(tt.name, func(t *testing.T) {
json, err := tt.ac.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, tt.expected, string(json))
})
}
}

func TestUnmarshalJSON(t *testing.T) {
for _, tt := range []struct {
name string
json string
expected *authC
}{
{
name: "simple",
json: `{"subject":"username","tenant":"acme"}`,
expected: &authC{
delegate: map[interface{}]interface{}{
"subject": "username",
"tenant": "acme",
},
},
},
{
name: "groups",
json: `{"membership":["dev","ops"],"subject":"username"}`,
expected: &authC{
delegate: map[interface{}]interface{}{
"subject": "username",
"membership": []interface{}{"dev", "ops"},
},
},
},
} {
t.Run(tt.name, func(t *testing.T) {
ac := &authC{}
err := json.Unmarshal([]byte(tt.json), ac)
assert.NoError(t, err)
assert.Equal(t, tt.expected, ac)
})
}
}

0 comments on commit 126164a

Please sign in to comment.