Skip to content

Commit

Permalink
Merge pull request #282 from jkl73/miscfixfeb
Browse files Browse the repository at this point in the history
[launcher] Fix image pulling in launcher
  • Loading branch information
jkl73 authored Feb 15, 2023
2 parents 2ed3096 + 276fea5 commit 4a63ee0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
7 changes: 6 additions & 1 deletion launcher/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package launcher

import (
"encoding/json"
"strings"

"cloud.google.com/go/compute/metadata"
"github.com/containerd/containerd/remotes"
Expand Down Expand Up @@ -31,7 +32,11 @@ func Resolver(token string) remotes.Resolver {
options := docker.ResolverOptions{}

credentials := func(host string) (string, string, error) {
return "_token", token, nil
// append the token if is talking to Artifact Registry or GCR Registry
if strings.HasSuffix(host, "docker.pkg.dev") || strings.HasSuffix(host, "gcr.io") {
return "_token", token, nil
}
return "", "", nil
}
authOpts := []docker.AuthorizerOpt{docker.WithAuthCreds(credentials)}
options.Authorizer = docker.NewDockerAuthorizer(authOpts...)
Expand Down
18 changes: 10 additions & 8 deletions launcher/container_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ func (r *ContainerRunner) fetchAndWriteTokenWithRetry(ctx context.Context,
select {
case <-ctx.Done():
timer.Stop()
r.logger.Printf("token refreshing stopped: %v", ctx.Err())
r.logger.Println("token refreshing stopped")
return
case <-timer.C:
var duration time.Duration
Expand Down Expand Up @@ -516,16 +516,18 @@ func (r *ContainerRunner) Run(ctx context.Context) error {
}

func initImage(ctx context.Context, cdClient *containerd.Client, launchSpec spec.LaunchSpec, token oauth2.Token, logger *log.Logger) (containerd.Image, error) {
var remoteOpt containerd.RemoteOpt
if token.Valid() {
remoteOpt = containerd.WithResolver(Resolver(token.AccessToken))
} else {
logger.Println("invalid auth token, will use empty auth")
}
remoteOpt := containerd.WithResolver(Resolver(token.AccessToken))

image, err := cdClient.Pull(ctx, launchSpec.ImageRef, containerd.WithPullUnpack, remoteOpt)
image, err := cdClient.Pull(ctx, launchSpec.ImageRef, containerd.WithPullUnpack, remoteOpt)
if err != nil {
return nil, fmt.Errorf("cannot pull the image: %w", err)
}
return image, nil
}
image, err := cdClient.Pull(ctx, launchSpec.ImageRef, containerd.WithPullUnpack)
if err != nil {
return nil, fmt.Errorf("cannot pull image: %w", err)
return nil, fmt.Errorf("cannot pull the image (no token, only works for a public image): %w", err)
}
return image, nil
}
Expand Down
33 changes: 33 additions & 0 deletions launcher/container_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ import (
"time"

"github.com/cenkalti/backoff/v4"
"github.com/containerd/containerd"
"github.com/containerd/containerd/defaults"
"github.com/containerd/containerd/namespaces"
"github.com/golang-jwt/jwt/v4"
"github.com/google/go-tpm-tools/cel"
"github.com/google/go-tpm-tools/launcher/spec"
"golang.org/x/oauth2"
"google.golang.org/api/option"
)

Expand Down Expand Up @@ -464,3 +469,31 @@ func TestGetNextRefresh(t *testing.T) {
}
}
}

func TestInitImageDockerPublic(t *testing.T) {
// testing image fetching using a dummy token and a docker repo url
containerdClient, err := containerd.New(defaults.DefaultAddress)
if err != nil {
t.Skipf("test needs containerd daemon: %v", err)
}

ctx := namespaces.WithNamespace(context.Background(), "test")
// This is a "valid" token (formatwise)
validToken := oauth2.Token{AccessToken: "000000", Expiry: time.Now().Add(time.Hour)}
if _, err := initImage(ctx, containerdClient, spec.LaunchSpec{ImageRef: "docker.io/library/hello-world:latest"}, validToken, log.Default()); err != nil {
t.Error(err)
} else {
if err := containerdClient.ImageService().Delete(ctx, "docker.io/library/hello-world:latest"); err != nil {
t.Error(err)
}
}

invalidToken := oauth2.Token{}
if _, err := initImage(ctx, containerdClient, spec.LaunchSpec{ImageRef: "docker.io/library/hello-world:latest"}, invalidToken, log.Default()); err != nil {
t.Error(err)
} else {
if err := containerdClient.ImageService().Delete(ctx, "docker.io/library/hello-world:latest"); err != nil {
t.Error(err)
}
}
}
2 changes: 1 addition & 1 deletion launcher/launcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func startLauncher() error {

token, err := launcher.RetrieveAuthToken(mdsClient)
if err != nil {
logger.Printf("failed to retrieve auth token: %v, using empty auth", err)
logger.Printf("failed to retrieve auth token: %v, using empty auth for image pulling\n", err)
}

ctx := namespaces.WithNamespace(context.Background(), namespaces.Default)
Expand Down

0 comments on commit 4a63ee0

Please sign in to comment.