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

Support pulling container images from Azure Container Registry (ACR) #199

Merged
Merged
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
57 changes: 44 additions & 13 deletions internal/infra/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ package infra
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/docker/docker/pkg/archive"
"github.com/hexops/gotextdiff"
"github.com/hexops/gotextdiff/myers"
"github.com/hexops/gotextdiff/span"
"io"
"log"
"net/http"
Expand All @@ -18,9 +15,15 @@ import (
"syscall"
"time"

"github.com/docker/docker/pkg/archive"
"github.com/hexops/gotextdiff"
"github.com/hexops/gotextdiff/myers"
"github.com/hexops/gotextdiff/span"

"github.com/dependabot/cli/internal/model"
"github.com/dependabot/cli/internal/server"
"github.com/docker/docker/api/types"
"github.com/moby/moby/api/types/registry"
"github.com/moby/moby/client"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -448,19 +451,47 @@ func pullImage(ctx context.Context, cli *client.Client, image string) error {

// pull image if necessary
if err != nil {
var privilegeFunc types.RequestPrivilegeFunc
token := os.Getenv("LOCAL_GITHUB_ACCESS_TOKEN")
if token != "" {
auth := base64.StdEncoding.EncodeToString([]byte("x:" + token))
privilegeFunc = func() (string, error) {
return "Basic " + auth, nil
var imagePullOptions types.ImagePullOptions

if strings.HasPrefix(image, "ghcr.io/") {

token := os.Getenv("LOCAL_GITHUB_ACCESS_TOKEN")
if token != "" {
auth := base64.StdEncoding.EncodeToString([]byte("x:" + token))
imagePullOptions = types.ImagePullOptions{
RegistryAuth: fmt.Sprintf("Basic %s", auth),
}
} else {
log.Println("Failed to find credentials for GitHub container registry.")
}
} else if strings.Contains(image, ".azurecr.io/") {
username := os.Getenv("AZURE_REGISTRY_USERNAME")
password := os.Getenv("AZURE_REGISTRY_PASSWORD")

registryName := strings.Split(image, "/")[0]

if username != "" && password != "" {
authConfig := registry.AuthConfig{
Username: username,
Password: password,
ServerAddress: registryName,
}

encodedJSON, _ := json.Marshal(authConfig)
authStr := base64.URLEncoding.EncodeToString(encodedJSON)

imagePullOptions = types.ImagePullOptions{
RegistryAuth: authStr,
}
} else {
log.Println("Failed to find credentials for Azure container registry.")
}
} else {
log.Printf("Failed to find credentials for pulling image: %s\n.", image)
}

log.Printf("pulling image: %s\n", image)
out, err := cli.ImagePull(ctx, image, types.ImagePullOptions{
PrivilegeFunc: privilegeFunc,
})
out, err := cli.ImagePull(ctx, image, imagePullOptions)
if err != nil {
return fmt.Errorf("failed to pull %v: %w", image, err)
}
Expand Down
Loading