Skip to content

Commit

Permalink
Support pulling container images from Azure Container Registry (ACR) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMagee authored Dec 1, 2023
1 parent ca383f1 commit eca9c52
Showing 1 changed file with 44 additions and 13 deletions.
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

0 comments on commit eca9c52

Please sign in to comment.