Skip to content

Commit

Permalink
Add dockerhub and ghcr providers
Browse files Browse the repository at this point in the history
Use a general container lister and implement sample command to test it
out

Signed-off-by: Juan Antonio Osorio <ozz@stacklok.com>
  • Loading branch information
JAORMX committed May 10, 2024
1 parent b900bfc commit 6be8c70
Show file tree
Hide file tree
Showing 45 changed files with 3,059 additions and 1,967 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package container provides the root command for the container subcommands
package container
package image

import (
"context"
Expand Down Expand Up @@ -48,7 +47,9 @@ func CmdVerify() *cobra.Command {
verifyCmd.Flags().StringP("owner", "o", "", "owner of the artifact")
verifyCmd.Flags().StringP("name", "n", "", "name of the artifact")
verifyCmd.Flags().StringP("digest", "s", "", "digest of the artifact")
//nolint:goconst // let's not use a const for this one
verifyCmd.Flags().StringP("token", "t", "", "token to authenticate to the provider."+
//nolint:goconst // let's not use a const for this one
"Can also be set via the AUTH_TOKEN environment variable.")
verifyCmd.Flags().StringP("tuf-root", "r", sigstore.SigstorePublicTrustedRootRepo, "TUF root to use for verification")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package container provides the root command for the container subcommands
package container
// Package image provides the root command for the image subcommands
package image

import "github.com/spf13/cobra"

// CmdContainer is the root command for the container subcommands
func CmdContainer() *cobra.Command {
// CmdImage is the root command for the container subcommands
func CmdImage() *cobra.Command {
var rtCmd = &cobra.Command{
Use: "container",
Short: "container provides utilities to test minder container support",
Use: "image",
Short: "image provides utilities to test minder container image support",
}

rtCmd.AddCommand(CmdVerify())
rtCmd.AddCommand(CmdList())
rtCmd.AddCommand(CmdListTags())

return rtCmd
}
102 changes: 102 additions & 0 deletions cmd/dev/app/image/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright 2023 Stacklok, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package image

import (
"context"
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/stacklok/minder/internal/providers/credentials"
"github.com/stacklok/minder/internal/providers/dockerhub"

Check failure on line 27 in cmd/dev/app/image/list.go

View workflow job for this annotation

GitHub Actions / lint / Go Lint

could not import github.com/stacklok/minder/internal/providers/dockerhub (-: # github.com/stacklok/minder/internal/providers/dockerhub
"github.com/stacklok/minder/internal/providers/github/ghcr"

Check failure on line 28 in cmd/dev/app/image/list.go

View workflow job for this annotation

GitHub Actions / lint / Go Lint

could not import github.com/stacklok/minder/internal/providers/github/ghcr (-: # github.com/stacklok/minder/internal/providers/github/ghcr
minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
provifv1 "github.com/stacklok/minder/pkg/providers/v1"
)

// CmdList returns the command for listing containers
func CmdList() *cobra.Command {
var listCmd = &cobra.Command{
Use: "list",
Short: "list images",
RunE: runCmdList,
SilenceUsage: true,
}

listCmd.Flags().StringP("provider", "p", "", "provider class to use for listing containers")
listCmd.Flags().StringP("namespace", "n", "", "namespace to list containers from")
//nolint:goconst // let's not use a const for this one
listCmd.Flags().StringP("token", "t", "", "token to authenticate to the provider."+
//nolint:goconst // let's not use a const for this one
"Can also be set via the AUTH_TOKEN environment variable.")

if err := viper.BindPFlag("auth.token", listCmd.Flags().Lookup("token")); err != nil {
fmt.Fprintf(os.Stderr, "Error binding flag: %s\n", err)
os.Exit(1)
}

viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))

return listCmd
}

func runCmdList(cmd *cobra.Command, _ []string) error {
ctx := context.Background()

// get the provider
pclass := cmd.Flag("provider")
if pclass.Value.String() == "" {
return fmt.Errorf("provider class is required")
}
ns := cmd.Flag("namespace")
if ns.Value.String() == "" {
return fmt.Errorf("namespace is required")
}

var prov provifv1.ImageLister
switch pclass.Value.String() {
case "dockerhub":
var err error
cred := credentials.NewOAuth2TokenCredential(viper.GetString("auth.token"))
prov, err = dockerhub.New(cred, &minderv1.DockerHubProviderConfig{
Namespace: ns.Value.String(),
})
if err != nil {
return err
}
case "ghcr":
cred := credentials.NewOAuth2TokenCredential(viper.GetString("auth.token"))
prov = ghcr.New(cred, ns.Value.String())
default:
return fmt.Errorf("unknown provider: %s", pclass.Value.String())
}

// get the containers
containers, err := prov.ListImages(ctx)
if err != nil {
return err
}

// print the containers
for _, container := range containers {
fmt.Println(container)
}

return nil
}
84 changes: 84 additions & 0 deletions cmd/dev/app/image/list_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2023 Stacklok, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package image

import (
"context"
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/stacklok/minder/internal/providers/credentials"
"github.com/stacklok/minder/internal/providers/oci"
)

// CmdListTags returns the command for listing container tags
func CmdListTags() *cobra.Command {
var listCmd = &cobra.Command{
Use: "list-tags",
Short: "list container tags",
RunE: runCmdListTags,
SilenceUsage: true,
}

listCmd.Flags().StringP("base-url", "b", "", "base URL for the OCI registry")
listCmd.Flags().StringP("container", "c", "", "container name to list tags for")
//nolint:goconst // let's not use a const for this one
listCmd.Flags().StringP("token", "t", "", "token to authenticate to the provider."+
"Can also be set via the AUTH_TOKEN environment variable.")

if err := viper.BindPFlag("auth.token", listCmd.Flags().Lookup("token")); err != nil {
fmt.Fprintf(os.Stderr, "Error binding flag: %s\n", err)
os.Exit(1)
}

viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))

return listCmd
}

func runCmdListTags(cmd *cobra.Command, _ []string) error {
ctx := context.Background()

// get the provider
baseURL := cmd.Flag("base-url")
contname := cmd.Flag("container")

if baseURL.Value.String() == "" {
return fmt.Errorf("base URL is required")
}
if contname.Value.String() == "" {
return fmt.Errorf("container name is required")
}

cred := credentials.NewOAuth2TokenCredential(viper.GetString("auth.token"))
prov := oci.New(cred, baseURL.Value.String())

// get the containers
containers, err := prov.ListTags(ctx, contname.Value.String())
if err != nil {
return err
}

// print the containers
for _, container := range containers {
fmt.Println(container)
}

return nil
}
4 changes: 2 additions & 2 deletions cmd/dev/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/spf13/cobra"

"github.com/stacklok/minder/cmd/dev/app/bundles"
"github.com/stacklok/minder/cmd/dev/app/container"
"github.com/stacklok/minder/cmd/dev/app/image"
"github.com/stacklok/minder/cmd/dev/app/rule_type"
"github.com/stacklok/minder/cmd/dev/app/testserver"
"github.com/stacklok/minder/internal/util/cli"
Expand All @@ -38,7 +38,7 @@ https://docs.stacklok.com/minder`,
}

cmd.AddCommand(rule_type.CmdRuleType())
cmd.AddCommand(container.CmdContainer())
cmd.AddCommand(image.CmdImage())
cmd.AddCommand(testserver.CmdTestServer())
cmd.AddCommand(bundles.CmdBundle())

Expand Down
13 changes: 13 additions & 0 deletions database/migrations/000055_oci_prov.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Copyright 2024 Stacklok, Inc
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
19 changes: 19 additions & 0 deletions database/migrations/000055_oci_prov.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- Copyright 2024 Stacklok, Inc
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

ALTER TYPE provider_type ADD VALUE 'image-lister';

-- Add `ghcr` and `dockerhub` provider classes
ALTER TYPE provider_class ADD VALUE 'ghcr';
ALTER TYPE provider_class ADD VALUE 'dockerhub';
16 changes: 16 additions & 0 deletions docs/docs/ref/proto.md

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

20 changes: 20 additions & 0 deletions internal/constants/useragent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2024 Stacklok, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package constants

var (
// ServerUserAgent is the user agent string for the server
ServerUserAgent = "Minder/" + Revision
)
4 changes: 4 additions & 0 deletions internal/constants/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ var (
// VerboseCLIVersion is the verbose version of the application.
// Note that this is set up at init time.
VerboseCLIVersion = ""
// Revision is the git commit hash. Note that this is set at compile time
// using ldflags.
Revision = "no-info"
)

type versionStruct struct {
Expand Down Expand Up @@ -66,6 +69,7 @@ func init() {
vvs.Time = kv.Value
case "vcs.revision":
vvs.Commit = kv.Value
Revision = kv.Value
case "vcs.modified":
vvs.Modified = kv.Value == "true"
case "GOOS":
Expand Down
2 changes: 1 addition & 1 deletion internal/db/artifacts.sql.go

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

2 changes: 1 addition & 1 deletion internal/db/db.go

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

2 changes: 1 addition & 1 deletion internal/db/entitlements.sql.go

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

2 changes: 1 addition & 1 deletion internal/db/entity_execution_lock.sql.go

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

Loading

0 comments on commit 6be8c70

Please sign in to comment.