Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

Private registries #13

Open
diegombeltran opened this issue Sep 27, 2022 · 2 comments
Open

Private registries #13

diegombeltran opened this issue Sep 27, 2022 · 2 comments

Comments

@diegombeltran
Copy link

diegombeltran commented Sep 27, 2022

Is it possible to pull/push images from/to private registries?

I understand it uses Moby Golang SDK and there is an ImagePullOptions parameter used to "basic-auth", but I struggled for the last few hours and all I get are unauthorized messages.

Here is a small snippet:

import images from 'k6/x/docker/images';
import encoding from 'k6/encoding';

export default function () {
  const registryAuth = encoding.b64encode(JSON.stringify({
    username: 'myUsername',
    password: 'myPassword'
  }), "url")

  const imageName = 'privateRegistry/myImage'
  const imagePullOptions = {
    RegistryAuth: registryAuth
  }

  const result = images.pull(imageName, imagePullOptions)

 console.log(`Image pulling result: ${result}`)
}

Is anything missing?

@diegombeltran
Copy link
Author

diegombeltran commented Oct 5, 2022

Hello again,

Just tested with Docker SDK for Golang. It works.

package main

import (
	"context"
	"encoding/base64"
	"encoding/json"
	"io"
	"os"

	"github.com/docker/docker/api/types"
	"github.com/docker/docker/client"
)

func main() {
	ctx := context.Background()
	cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
	if err != nil {
		panic(err)
	}

	authConfig := types.AuthConfig{
		Username: "myUsername",
		Password: "myPassword",
	}
	encodedJSON, err := json.Marshal(authConfig)
	if err != nil {
		panic(err)
	}
	authStr := base64.URLEncoding.EncodeToString(encodedJSON)
	println(authStr)

	out, err := cli.ImagePull(ctx, "myRegistry/myImage", types.ImagePullOptions{RegistryAuth: authStr})
	if err != nil {
		panic(err)
	}

	defer out.Close()
	io.Copy(os.Stdout, out)
}

Base64 authStr is the same as the k6 b64encode output. Looks like imagePullOptions is not right, but I can't debug why.

Any thoughts?

@diegombeltran
Copy link
Author

diegombeltran commented Oct 5, 2022

I just found this:
https://k6.io/docs/extensions/explanations/go-js-bridge/

Go field names convert from Pascal to Snake case. For example, the struct field ComparisonResult string becomes comparison_result in JS.

So the snippet looks like this:

import images from 'k6/x/docker/images';
import encoding from 'k6/encoding';

export default function () {
  const registryAuth = encoding.b64encode(JSON.stringify({
    username: 'myUsername',
    password: 'myPassword'
  }), "url")

  const imageName = 'privateRegistry/myImage'

  const imagePullOptions = {
    registry_auth: registryAuth
  }

  const result = images.pull(imageName, imagePullOptions)

 console.log(`Image pulling result: ${result}`)
}

Working. Just in case someone needs this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant