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 Apr 11, 2024
1 parent fc52805 commit 56a6168
Show file tree
Hide file tree
Showing 35 changed files with 629 additions and 39 deletions.
2 changes: 2 additions & 0 deletions cmd/dev/app/container/cmd_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,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
2 changes: 2 additions & 0 deletions cmd/dev/app/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func CmdContainer() *cobra.Command {
}

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

return rtCmd
}
92 changes: 92 additions & 0 deletions cmd/dev/app/container/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// 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 container provides the root command for the container subcommands
package container

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"
"github.com/stacklok/minder/internal/providers/github/ghcr"
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 containers",
RunE: runCmdList,
SilenceUsage: true,
}

listCmd.Flags().StringP("provider", "p", "", "provider class to use for listing containers")
//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")

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, "devopsfaith")
if err != nil {
return err
}
case "ghcr":
cred := credentials.NewOAuth2TokenCredential(viper.GetString("auth.token"))
prov = ghcr.New(cred, "jaormx")
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
}
85 changes: 85 additions & 0 deletions cmd/dev/app/container/list_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// 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 container provides the root command for the container subcommands
package container

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
}
3 changes: 3 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.

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.

17 changes: 11 additions & 6 deletions internal/db/models.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/profile_status.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/profiles.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/projects.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/provider_access_tokens.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/provider_github_app_installations.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/providers.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/pull_requests.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/querier.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/repositories.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/rule_types.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/session_store.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/subscriptions.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/users.sql.go

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

Loading

0 comments on commit 56a6168

Please sign in to comment.