Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Unmarshaler for Consumer Identity #927

Merged
merged 4 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions api/credentials/internal/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"sync"

"github.com/sirupsen/logrus"

"ocm.software/ocm/api/utils/runtime"
)

func init() {
Expand Down Expand Up @@ -91,6 +93,26 @@ func orMatcher(list []IdentityMatcher) IdentityMatcher {
// ConsumerIdentity describes the identity of a credential consumer.
type ConsumerIdentity map[string]string

func (c *ConsumerIdentity) UnmarshalJSON(data []byte) error {
var m map[string]interface{}
err := runtime.DefaultJSONEncoding.Unmarshal(data, &m)
if err != nil {
return err
}

*c = make(map[string]string, len(m))
for k, v := range m {
switch v.(type) {
case map[string]interface{}:
return fmt.Errorf("cannot unmarshal complex type into consumer identity")
case []interface{}:
return fmt.Errorf("cannot unmarshal complex type into consumer identity")
}
(*c)[k] = fmt.Sprintf("%v", v)
}
return nil
}

func NewConsumerIdentity(typ string, attrs ...string) ConsumerIdentity {
r := map[string]string{}
r[ID_TYPE] = typ
Expand Down
73 changes: 73 additions & 0 deletions api/credentials/internal/identity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package internal_test

import (
. "github.com/mandelsoft/goutils/testutils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"sigs.k8s.io/yaml"

"ocm.software/ocm/api/credentials/identity/hostpath"
"ocm.software/ocm/api/credentials/internal"
)

var _ = Describe("unmarshal cunsomer identity", func() {
It("with int", func() {
data := `
scheme: http
hostname: 127.0.0.1
port: 5001
`
cid := internal.ConsumerIdentity{}
MustBeSuccessful(yaml.Unmarshal([]byte(data), &cid))
Expect(cid[hostpath.ID_SCHEME]).To(Equal("http"))
Expect(cid[hostpath.ID_HOSTNAME]).To(Equal("127.0.0.1"))
Expect(cid[hostpath.ID_PORT]).To(Equal("5001"))
})
It("with float", func() {
data := `
scheme: http
hostname: 127.0.0.1
port: 3.14
`
cid := internal.ConsumerIdentity{}
MustBeSuccessful(yaml.Unmarshal([]byte(data), &cid))
Expect(cid[hostpath.ID_SCHEME]).To(Equal("http"))
Expect(cid[hostpath.ID_HOSTNAME]).To(Equal("127.0.0.1"))
Expect(cid[hostpath.ID_PORT]).To(Equal("3.14"))
})
It("with bool", func() {
data := `
scheme: http
hostname: 127.0.0.1
port: true
`
cid := internal.ConsumerIdentity{}
MustBeSuccessful(yaml.Unmarshal([]byte(data), &cid))
Expect(cid[hostpath.ID_SCHEME]).To(Equal("http"))
Expect(cid[hostpath.ID_HOSTNAME]).To(Equal("127.0.0.1"))
Expect(cid[hostpath.ID_PORT]).To(Equal("true"))
})
It("with complex value", func() {
data := `
scheme: http
hostname: 127.0.0.1
port:
pre: 50
post: 01
`
cid := internal.ConsumerIdentity{}
Expect(yaml.Unmarshal([]byte(data), &cid)).NotTo(Succeed())
})
It("with slice value", func() {
data := `
scheme: http
hostname: 127.0.0.1
port:
- 50
- 01
`
cid := internal.ConsumerIdentity{}
Expect(yaml.Unmarshal([]byte(data), &cid)).NotTo(Succeed())
})
})
2 changes: 1 addition & 1 deletion api/ocm/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
. "github.com/mandelsoft/goutils/testutils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"ocm.software/ocm/api/ocm/selectors"
. "ocm.software/ocm/api/ocm/testhelper"

"ocm.software/ocm/api/datacontext"
"ocm.software/ocm/api/ocm"
metav1 "ocm.software/ocm/api/ocm/compdesc/meta/v1"
resourcetypes "ocm.software/ocm/api/ocm/extensions/artifacttypes"
"ocm.software/ocm/api/ocm/extensions/repositories/composition"
"ocm.software/ocm/api/ocm/selectors"
"ocm.software/ocm/api/utils/blobaccess"
"ocm.software/ocm/api/utils/mime"
)
Expand Down
6 changes: 3 additions & 3 deletions api/ocm/compdesc/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
. "github.com/mandelsoft/goutils/testutils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"ocm.software/ocm/api/ocm/selectors/labelsel"
"ocm.software/ocm/api/ocm/selectors/refsel"
"ocm.software/ocm/api/ocm/selectors/rscsel"

"ocm.software/ocm/api/ocm/compdesc"
v1 "ocm.software/ocm/api/ocm/compdesc/meta/v1"
"ocm.software/ocm/api/ocm/extensions/repositories/ocireg"
"ocm.software/ocm/api/ocm/selectors/labelsel"
"ocm.software/ocm/api/ocm/selectors/refsel"
"ocm.software/ocm/api/ocm/selectors/rscsel"
)

var _ = Describe("helper", func() {
Expand Down
2 changes: 1 addition & 1 deletion api/ocm/cpi/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
. "github.com/mandelsoft/goutils/testutils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"ocm.software/ocm/api/ocm/selectors"
. "ocm.software/ocm/api/ocm/testhelper"

"github.com/mandelsoft/vfs/pkg/memoryfs"
Expand All @@ -18,6 +17,7 @@ import (
resourcetypes "ocm.software/ocm/api/ocm/extensions/artifacttypes"
"ocm.software/ocm/api/ocm/extensions/attrs/compositionmodeattr"
"ocm.software/ocm/api/ocm/extensions/repositories/ctf"
"ocm.software/ocm/api/ocm/selectors"
"ocm.software/ocm/api/utils/accessio"
"ocm.software/ocm/api/utils/accessobj"
"ocm.software/ocm/api/utils/blobaccess"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
. "github.com/mandelsoft/goutils/testutils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"ocm.software/ocm/api/ocm/selectors"
. "ocm.software/ocm/api/ocm/testhelper"

"github.com/mandelsoft/filepath/pkg/filepath"
Expand All @@ -22,6 +21,7 @@ import (
resourcetypes "ocm.software/ocm/api/ocm/extensions/artifacttypes"
"ocm.software/ocm/api/ocm/extensions/digester/digesters/blob"
"ocm.software/ocm/api/ocm/extensions/repositories/comparch"
"ocm.software/ocm/api/ocm/selectors"
"ocm.software/ocm/api/tech/signing/hasher/sha256"
"ocm.software/ocm/api/utils/accessio"
"ocm.software/ocm/api/utils/accessobj"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
. "github.com/mandelsoft/goutils/testutils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"ocm.software/ocm/api/ocm/selectors"

"github.com/mandelsoft/goutils/finalizer"
"github.com/mandelsoft/vfs/pkg/memoryfs"
Expand All @@ -16,6 +15,7 @@ import (
resourcetypes "ocm.software/ocm/api/ocm/extensions/artifacttypes"
me "ocm.software/ocm/api/ocm/extensions/repositories/composition"
"ocm.software/ocm/api/ocm/ocmutils"
"ocm.software/ocm/api/ocm/selectors"
"ocm.software/ocm/api/utils/accessio"
"ocm.software/ocm/api/utils/accessobj"
"ocm.software/ocm/api/utils/blobaccess/blobaccess"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
. "github.com/mandelsoft/goutils/testutils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"ocm.software/ocm/api/ocm/selectors"

"github.com/mandelsoft/goutils/finalizer"

Expand All @@ -13,6 +12,7 @@ import (
resourcetypes "ocm.software/ocm/api/ocm/extensions/artifacttypes"
me "ocm.software/ocm/api/ocm/extensions/repositories/composition"
"ocm.software/ocm/api/ocm/ocmutils"
"ocm.software/ocm/api/ocm/selectors"
"ocm.software/ocm/api/utils/blobaccess"
"ocm.software/ocm/api/utils/blobaccess/bpi"
"ocm.software/ocm/api/utils/mime"
Expand Down
5 changes: 3 additions & 2 deletions api/ocm/resolvers/component_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package resolvers_test

import (
"github.com/mandelsoft/goutils/sliceutils"
. "github.com/mandelsoft/goutils/testutils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "ocm.software/ocm/api/helper/builder"
"ocm.software/ocm/api/ocm/resolvers"

"github.com/mandelsoft/goutils/sliceutils"

"ocm.software/ocm/api/ocm"
"ocm.software/ocm/api/ocm/extensions/repositories/ctf"
"ocm.software/ocm/api/ocm/resolvers"
"ocm.software/ocm/api/utils/accessio"
"ocm.software/ocm/api/utils/accessobj"
)
Expand Down
2 changes: 1 addition & 1 deletion cmds/ocm/commands/ocmcmds/references/add/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"ocm.software/ocm/api/ocm/selectors/refsel"
. "ocm.software/ocm/cmds/ocm/testhelper"

"github.com/mandelsoft/goutils/testutils"

"ocm.software/ocm/api/ocm/compdesc"
metav1 "ocm.software/ocm/api/ocm/compdesc/meta/v1"
"ocm.software/ocm/api/ocm/extensions/repositories/comparch"
"ocm.software/ocm/api/ocm/selectors/refsel"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion examples/lib/comparison-scenario/09-localize.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/mandelsoft/vfs/pkg/memoryfs"
"github.com/mandelsoft/vfs/pkg/vfs"
"helm.sh/helm/v3/pkg/chart"
"ocm.software/ocm/api/ocm/selectors/rscsel"
"sigs.k8s.io/yaml"

"ocm.software/ocm/api/ocm"
Expand All @@ -17,6 +16,7 @@ import (
"ocm.software/ocm/api/ocm/ocmutils"
"ocm.software/ocm/api/ocm/ocmutils/localize"
"ocm.software/ocm/api/ocm/resourcerefs"
"ocm.software/ocm/api/ocm/selectors/rscsel"
"ocm.software/ocm/api/tech/helm/loader"
"ocm.software/ocm/api/utils"
"ocm.software/ocm/api/utils/runtime"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"os"

"github.com/mandelsoft/goutils/errors"
"ocm.software/ocm/api/ocm/selectors"

"ocm.software/ocm/api/credentials"
"ocm.software/ocm/api/oci"
"ocm.software/ocm/api/ocm"
"ocm.software/ocm/api/ocm/extensions/repositories/ocireg"
"ocm.software/ocm/api/ocm/selectors"
"ocm.software/ocm/api/tech/oci/identity"
"ocm.software/ocm/examples/lib/helper"
)
Expand Down