Skip to content

Commit

Permalink
Move properties fetcher methods into Provider (#4181)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Antonio Osorio <[email protected]>
  • Loading branch information
JAORMX authored Aug 19, 2024
1 parent ca914f1 commit 02a42be
Show file tree
Hide file tree
Showing 20 changed files with 533 additions and 148 deletions.
7 changes: 3 additions & 4 deletions internal/engine/actions/remediate/remediate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ import (
"github.com/stacklok/minder/internal/engine/actions/remediate/rest"
engif "github.com/stacklok/minder/internal/engine/interfaces"
"github.com/stacklok/minder/internal/providers/credentials"
"github.com/stacklok/minder/internal/providers/git"
httpclient "github.com/stacklok/minder/internal/providers/http"
"github.com/stacklok/minder/internal/providers/telemetry"
"github.com/stacklok/minder/internal/providers/testproviders"
pb "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
provifv1 "github.com/stacklok/minder/pkg/providers/v1"
)
Expand Down Expand Up @@ -144,13 +143,13 @@ func TestNewRuleRemediator(t *testing.T) {

func HTTPProvider() (provifv1.Provider, error) {
cfg := pb.RESTProviderConfig{BaseUrl: proto.String("https://api.github.com/")}
return httpclient.NewREST(
return testproviders.NewRESTProvider(
&cfg,
telemetry.NewNoopMetrics(),
credentials.NewGitHubTokenCredential("token"),
)
}

func GitProvider() (provifv1.Provider, error) {
return git.NewGit(credentials.NewEmptyCredential()), nil
return testproviders.NewGitProvider(credentials.NewEmptyCredential()), nil
}
4 changes: 2 additions & 2 deletions internal/engine/actions/remediate/rest/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ import (
"github.com/stacklok/minder/internal/profiles/models"
"github.com/stacklok/minder/internal/providers/credentials"
"github.com/stacklok/minder/internal/providers/github/clients"
httpclient "github.com/stacklok/minder/internal/providers/http"
"github.com/stacklok/minder/internal/providers/ratecache"
"github.com/stacklok/minder/internal/providers/telemetry"
"github.com/stacklok/minder/internal/providers/testproviders"
pb "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
provifv1 "github.com/stacklok/minder/pkg/providers/v1"
)
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestNewRestRemediate(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

restProvider, err := httpclient.NewREST(
restProvider, err := testproviders.NewRESTProvider(
&pb.RESTProviderConfig{
BaseUrl: proto.String("https://api.github.com/"),
},
Expand Down
20 changes: 11 additions & 9 deletions internal/engine/ingester/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
gitengine "github.com/stacklok/minder/internal/engine/ingester/git"
"github.com/stacklok/minder/internal/providers/credentials"
gitclient "github.com/stacklok/minder/internal/providers/git"
"github.com/stacklok/minder/internal/providers/testproviders"
pb "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
v1 "github.com/stacklok/minder/pkg/providers/v1"
)
Expand All @@ -37,7 +38,7 @@ func TestGitIngestWithCloneURLFromRepo(t *testing.T) {
MaxFiles: 100,
MaxBytes: 1_000_000,
}
gitClient := gitclient.NewGit(
gitClient := testproviders.NewGitProvider(
credentials.NewEmptyCredential(),
gitclient.WithConfig(cfg),
)
Expand Down Expand Up @@ -78,7 +79,7 @@ func TestGitIngestWithCloneURLFromParams(t *testing.T) {

gi, err := gitengine.NewGitIngester(
&pb.GitType{Branch: "master"},
gitclient.NewGit(credentials.NewEmptyCredential()),
testproviders.NewGitProvider(credentials.NewEmptyCredential()),
)
require.NoError(t, err, "expected no error")

Expand Down Expand Up @@ -106,7 +107,7 @@ func TestGitIngestWithCustomBranchFromParams(t *testing.T) {

gi, err := gitengine.NewGitIngester(
&pb.GitType{Branch: "master"},
gitclient.NewGit(credentials.NewEmptyCredential()),
testproviders.NewGitProvider(credentials.NewEmptyCredential()),
)
require.NoError(t, err, "expected no error")

Expand Down Expand Up @@ -136,7 +137,7 @@ func TestGitIngestWithBranchFromRepoEntity(t *testing.T) {
gi, err := gitengine.NewGitIngester(
// &pb.GitType{Branch: "master"},
&pb.GitType{},
gitclient.NewGit(credentials.NewEmptyCredential()),
testproviders.NewGitProvider(credentials.NewEmptyCredential()),
)
require.NoError(t, err, "expected no error")

Expand Down Expand Up @@ -166,7 +167,7 @@ func TestGitIngestWithUnexistentBranchFromParams(t *testing.T) {

gi, err := gitengine.NewGitIngester(
&pb.GitType{Branch: "master"},
gitclient.NewGit(credentials.NewEmptyCredential()),
testproviders.NewGitProvider(credentials.NewEmptyCredential()),
)

require.NoError(t, err, "expected no error")
Expand All @@ -186,7 +187,7 @@ func TestGitIngestFailsBecauseOfAuthorization(t *testing.T) {
// foobar is not a valid token
gi, err := gitengine.NewGitIngester(
&pb.GitType{Branch: "master"},
gitclient.NewGit(credentials.NewGitHubTokenCredential("foobar")),
testproviders.NewGitProvider(credentials.NewGitHubTokenCredential("foobar")),
)

require.NoError(t, err, "expected no error")
Expand All @@ -201,7 +202,8 @@ func TestGitIngestFailsBecauseOfAuthorization(t *testing.T) {
func TestGitIngestFailsBecauseOfUnexistentCloneUrl(t *testing.T) {
t.Parallel()

gi, err := gitengine.NewGitIngester(&pb.GitType{}, gitclient.NewGit(credentials.NewEmptyCredential()))
gi, err := gitengine.NewGitIngester(
&pb.GitType{}, testproviders.NewGitProvider(credentials.NewEmptyCredential()))
require.NoError(t, err, "expected no error")

got, err := gi.Ingest(context.Background(), &pb.Artifact{}, map[string]any{
Expand All @@ -219,7 +221,7 @@ func TestGitIngestFailsWhenRepoTooLarge(t *testing.T) {
MaxFiles: 100,
MaxBytes: 1,
}
gitClient := gitclient.NewGit(
gitClient := testproviders.NewGitProvider(
credentials.NewEmptyCredential(),
gitclient.WithConfig(cfg),
)
Expand Down Expand Up @@ -247,7 +249,7 @@ func TestGitIngestFailsWhenRepoHasTooManyFiles(t *testing.T) {
MaxFiles: 1,
MaxBytes: 1_000_000,
}
gitClient := gitclient.NewGit(
gitClient := testproviders.NewGitProvider(
credentials.NewEmptyCredential(),
gitclient.WithConfig(cfg),
)
Expand Down
4 changes: 2 additions & 2 deletions internal/engine/ingester/rest/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ import (
engif "github.com/stacklok/minder/internal/engine/interfaces"
"github.com/stacklok/minder/internal/providers/credentials"
"github.com/stacklok/minder/internal/providers/github/clients"
httpclient "github.com/stacklok/minder/internal/providers/http"
"github.com/stacklok/minder/internal/providers/ratecache"
"github.com/stacklok/minder/internal/providers/telemetry"
"github.com/stacklok/minder/internal/providers/testproviders"
pb "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
provifv1 "github.com/stacklok/minder/pkg/providers/v1"
)
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestNewRestRuleDataIngest(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

rest, err := httpclient.NewREST(
rest, err := testproviders.NewRESTProvider(
&pb.RESTProviderConfig{
BaseUrl: proto.String("https://api.github.com/"),
},
Expand Down
4 changes: 2 additions & 2 deletions internal/engine/rtengine/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/stacklok/minder/internal/db"
dbf "github.com/stacklok/minder/internal/db/fixtures"
"github.com/stacklok/minder/internal/engine/ingestcache"
"github.com/stacklok/minder/internal/providers/git"
"github.com/stacklok/minder/internal/providers/testproviders"
)

func TestGetRuleEngine(t *testing.T) {
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestGetRuleEngine(t *testing.T) {

cache := ruleEngineCache{
store: store,
provider: git.NewGit(nil),
provider: testproviders.NewGitProvider(nil),
ingestCache: ingestcache.NewNoopCache(),
engines: scenario.Cache,
}
Expand Down
15 changes: 15 additions & 0 deletions internal/providers/dockerhub/dockerhub.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"golang.org/x/oauth2"

"github.com/stacklok/minder/internal/db"
"github.com/stacklok/minder/internal/entities/properties"
"github.com/stacklok/minder/internal/providers/oci"
minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
provifv1 "github.com/stacklok/minder/pkg/providers/v1"
Expand Down Expand Up @@ -174,3 +175,17 @@ func (d *dockerHubImageLister) ListImages(ctx context.Context) ([]string, error)

return containers, nil
}

// FetchAllProperties implements the provider interface
// TODO: Implement this
func (_ *dockerHubImageLister) FetchAllProperties(
_ context.Context, _ string, _ minderv1.Entity) (*properties.Properties, error) {
return nil, nil
}

// FetchProperty implements the provider interface
// TODO: Implement this
func (_ *dockerHubImageLister) FetchProperty(
_ context.Context, _ string, _ minderv1.Entity, _ string) (*properties.Property, error) {
return nil, nil
}
12 changes: 2 additions & 10 deletions internal/providers/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ import (

"github.com/stacklok/minder/internal/config/server"
"github.com/stacklok/minder/internal/providers/git/memboxfs"
minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
provifv1 "github.com/stacklok/minder/pkg/providers/v1"
)

// Git is the struct that contains the GitHub REST API client
// It implements helper functions that a provider that
// uses the `git` trait can use.
type Git struct {
credential provifv1.GitCredential
maxFiles int64
Expand All @@ -42,9 +43,6 @@ type Git struct {

const maxCachedObjectSize = 100 * 1024 // 100KiB

// Ensure that the Git client implements the Git interface
var _ provifv1.Git = (*Git)(nil)

// Options implements the "functional options" pattern for Git
type Options func(*Git)

Expand All @@ -67,12 +65,6 @@ func WithConfig(cfg server.GitConfig) Options {
}
}

// CanImplement returns true/false depending on whether the Provider
// can implement the specified trait
func (_ *Git) CanImplement(trait minderv1.ProviderType) bool {
return trait == minderv1.ProviderType_PROVIDER_TYPE_GIT
}

// Clone clones a git repository
func (g *Git) Clone(ctx context.Context, url, branch string) (*git.Repository, error) {
opts := &git.CloneOptions{
Expand Down
13 changes: 13 additions & 0 deletions internal/providers/github/ghcr/ghcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"golang.org/x/oauth2"
"google.golang.org/protobuf/proto"

"github.com/stacklok/minder/internal/entities/properties"
"github.com/stacklok/minder/internal/verifier/verifyif"
minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
provifv1 "github.com/stacklok/minder/pkg/providers/v1"
Expand Down Expand Up @@ -135,3 +136,15 @@ func (g *ImageLister) ListImages(ctx context.Context) ([]string, error) {

return allContainers, nil
}

// FetchAllProperties implements the provider interface
// TODO: Implement this
func (_ *ImageLister) FetchAllProperties(_ context.Context, _ string, _ minderv1.Entity) (*properties.Properties, error) {
return nil, nil
}

// FetchProperty implements the provider interface
// TODO: Implement this
func (_ *ImageLister) FetchProperty(_ context.Context, _ string, _ minderv1.Entity, _ string) (*properties.Property, error) {
return nil, nil
}
Loading

0 comments on commit 02a42be

Please sign in to comment.