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

Improvements and bug fixes #47

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@

# Ignore editor config
.vscode
# for VS Code debug sessions
**/__debug_bin*
**/ginkgo.report

# Ignore IntelliJ config
.idea/
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,7 @@ crc/login:
@crc console --credentials -ojson | jq -r .clusterConfig.adminCredentials.password | oc login --username kubeadmin --insecure-skip-tls-verify=true https://api.crc.testing:6443
@oc whoami --show-token | $(container_tool) login --username kubeadmin --password-stdin "$(external_image_registry)" --tls-verify=false
.PHONY: crc/login

.PHONY: fmt-imports
fmt-imports: $(GCI)
find . -name '*.go' -not -path './vendor/*' | xargs $(GCI) write -s standard -s default -s "prefix(k8s)" -s "prefix(sigs.k8s)" -s "prefix(github.com)" -s "prefix(gitlab)" -s "prefix(github.com/openshift-online/rh-trex)" --custom-order --skip-generated
4 changes: 3 additions & 1 deletion cmd/trex/environments/service_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ type GenericServiceLocator func() services.GenericService

func NewGenericServiceLocator(env *Env) GenericServiceLocator {
return func() services.GenericService {
return services.NewGenericService(dao.NewGenericDao(&env.Database.SessionFactory))
return services.NewGenericService(
dao.NewGenericDao(&env.Database.SessionFactory),
env.Clients.OCM)
}
}

Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ require (
github.com/jinzhu/inflection v1.0.0
github.com/lib/pq v1.10.5
github.com/mendsley/gojwk v0.0.0-20141217222730-4d5ec6e58103
github.com/onsi/ginkgo/v2 v2.8.1
github.com/onsi/gomega v1.27.1
github.com/openshift-online/ocm-sdk-go v0.1.334
github.com/prometheus/client_golang v1.16.0
github.com/segmentio/ksuid v1.0.2
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.5
github.com/yaacov/tree-search-language v0.0.0-20190923184055-1c2dad2e354b
go.uber.org/mock v0.4.0
gopkg.in/resty.v1 v1.12.0
gorm.io/driver/postgres v1.0.5
gorm.io/gorm v1.20.5
Expand All @@ -38,6 +40,7 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/gorilla/css v1.0.0 // indirect
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,6 @@ github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
github.com/onsi/ginkgo v1.16.4 h1:29JGrr5oVBm5ulCWet69zQkzWipVXIol6ygQUe/EzNc=
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
github.com/onsi/ginkgo/v2 v2.1.4/go.mod h1:um6tUpWM/cxCK3/FK8BXqEiUMUwRgSM4JXG47RKZmLU=
Expand Down Expand Up @@ -511,6 +510,8 @@ go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
Expand Down
9 changes: 8 additions & 1 deletion pkg/api/dinosaur_types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package api

import "gorm.io/gorm"
import (
"github.com/openshift-online/rh-trex/pkg/util"
"gorm.io/gorm"
)

var (
DinosaurTypeName = util.GetBaseType(Dinosaur{})
)

type Dinosaur struct {
Meta
Expand Down
11 changes: 2 additions & 9 deletions pkg/api/presenters/kind.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
package presenters

import (
"github.com/openshift-online/rh-trex/pkg/api"
"github.com/openshift-online/rh-trex/pkg/api/openapi"
"github.com/openshift-online/rh-trex/pkg/errors"
"github.com/openshift-online/rh-trex/pkg/util"
)

func ObjectKind(i interface{}) *string {
result := ""
switch i.(type) {
case api.Dinosaur, *api.Dinosaur:
result = "Dinosaur"
case errors.ServiceError, *errors.ServiceError:
result = "Error"
}
result := util.GetBaseType(i)

return openapi.PtrString(result)
}
21 changes: 21 additions & 0 deletions pkg/api/presenters/object_reference_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package presenters

import (
. "github.com/onsi/ginkgo/v2/dsl/core"
. "github.com/onsi/gomega"
"github.com/openshift-online/rh-trex/pkg/api"
)

var _ = Describe("Object Reference Presenter", func() {
It("Populates Kind", func() {
object := api.Dinosaur{
Meta: api.Meta{
ID: "123",
},
}
presented := PresentReference("123", object)
Expect(*presented.Id).To(Equal(object.ID))
Expect(*presented.Kind).To(Equal("Dinosaur"))
Expect(*presented.Href).To(Equal("/api/rh-trex/v1/dinosaurs/123"))
})
})
13 changes: 2 additions & 11 deletions pkg/api/presenters/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"fmt"

"github.com/openshift-online/rh-trex/pkg/api/openapi"

"github.com/openshift-online/rh-trex/pkg/api"
"github.com/openshift-online/rh-trex/pkg/errors"
"github.com/openshift-online/rh-trex/pkg/util"
)

const (
Expand All @@ -18,12 +16,5 @@ func ObjectPath(id string, obj interface{}) *string {
}

func path(i interface{}) string {
switch i.(type) {
case api.Dinosaur, *api.Dinosaur:
return "dinosaurs"
case errors.ServiceError, *errors.ServiceError:
return "errors"
default:
return ""
}
return fmt.Sprintf("%ss", util.ToSnakeCase(util.GetBaseType(i)))
}
13 changes: 13 additions & 0 deletions pkg/api/presenters/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package presenters

import (
"testing"

. "github.com/onsi/ginkgo/v2/dsl/core"
. "github.com/onsi/gomega"
)

func TestAccessProtection(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Presenters Suite")
}
9 changes: 9 additions & 0 deletions pkg/auth/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package auth

const (
GetAction = "get"
ListAction = "list"
UpdateAction = "update"
CreateAction = "create"
DeleteAction = "delete"
)
17 changes: 17 additions & 0 deletions pkg/client/ocm/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
azv1 "github.com/openshift-online/ocm-sdk-go/authorizations/v1"
)

//go:generate mockgen -source=authorization.go -package=ocm -destination=mock_authorization.go
type OCMAuthorization interface {
SelfAccessReview(ctx context.Context, action, resourceType, organizationID, subscriptionID, clusterID string) (allowed bool, err error)
AccessReview(ctx context.Context, username, action, resourceType, organizationID, subscriptionID, clusterID string) (allowed bool, err error)
ResourceReview(ctx context.Context, username string, action string, resource string) (*azv1.ResourceReview, error)
}

type authorization service
Expand Down Expand Up @@ -75,3 +77,18 @@ func (a authorization) AccessReview(ctx context.Context, username, action, resou

return response.Allowed(), nil
}

func (a authorization) ResourceReview(ctx context.Context, username string, action string, resource string) (*azv1.ResourceReview, error) {
con := a.client.connection
resourceReviewClient := con.Authorizations().V1().ResourceReview()

request, err := azv1.NewResourceReviewRequest().AccountUsername(username).Action(action).ResourceType(resource).Build()
if err != nil {
return nil, err
}
response, err := resourceReviewClient.Post().Request(request).SendContext(ctx)
if err != nil {
return nil, err
}
return response.Review(), nil
}
10 changes: 10 additions & 0 deletions pkg/client/ocm/authorization_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package ocm

import (
"context"

azv1 "github.com/openshift-online/ocm-sdk-go/authorizations/v1"
)

// authorizationMock returns allowed=true for every request
Expand All @@ -16,3 +18,11 @@ func (a authorizationMock) SelfAccessReview(ctx context.Context, action, resourc
func (a authorizationMock) AccessReview(ctx context.Context, username, action, resourceType, organizationID, subscriptionID, clusterID string) (allowed bool, err error) {
return true, nil
}

func (a authorizationMock) ResourceReview(ctx context.Context, username string, action string, resource string) (*azv1.ResourceReview, error) {
response, err := azv1.NewResourceReview().AccountUsername(username).Action(action).ResourceType(resource).OrganizationIDs("*").Build()
if err != nil {
return nil, err
}
return response, nil
}
85 changes: 85 additions & 0 deletions pkg/client/ocm/mock_authorization.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions pkg/client/ocm/resource_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ocm

type Resource string

const (
ClusterResource Resource = "Cluster"
SubscriptionResource Resource = "Subscription"
OrganizationResource Resource = "Organization"
)
20 changes: 20 additions & 0 deletions pkg/dao/dinosaur.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,28 @@ import (

"github.com/openshift-online/rh-trex/pkg/api"
"github.com/openshift-online/rh-trex/pkg/db"
"github.com/openshift-online/rh-trex/pkg/util"
)

var (
dinosaurTableName = util.ToSnakeCase(api.DinosaurTypeName) + "s"
dinosaurColumns = []string{
Copy link
Contributor

@tiwillia tiwillia Jun 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means to support searching a new column, it must be added to this list?

I see clusters-service implementing this requirement in this way, but AMS/OSDFM seem to handle this differently.

I'm not sure which is better tbh, the AMS implementation is explicit while this implementation is simpler. @markturansky can you provide your input here?

Copy link
Contributor Author

@gdbranco gdbranco Jun 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'd like to eventually be able to move away from this to have something that uses reflect or similar to have it be more dynamic

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd prefer to not explicitly set casing, as that negates the casing config allowed by gorm (that also does things like table name prefix, etc). can we do this through gorm somehow,?

"id",
"created_at",
"updated_at",
"species",
}
)

func DinosaurApiToModel() TableMappingRelation {
result := map[string]string{}
applyBaseMapping(result, dinosaurColumns, dinosaurTableName)
return TableMappingRelation{
Mapping: result,
relationTableName: dinosaurTableName,
}
}

type DinosaurDao interface {
Get(ctx context.Context, id string) (*api.Dinosaur, error)
Create(ctx context.Context, dinosaur *api.Dinosaur) (*api.Dinosaur, error)
Expand Down
Loading