github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/bindings/test/auth_test.go (about)

     1  package bindings_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"time"
     7  
     8  	"github.com/containers/common/pkg/auth"
     9  	"github.com/containers/image/v5/types"
    10  	podmanRegistry "github.com/hanks177/podman/v4/hack/podman-registry-go"
    11  	"github.com/hanks177/podman/v4/pkg/bindings/images"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  	"github.com/onsi/gomega/gexec"
    15  )
    16  
    17  var _ = Describe("Podman images", func() {
    18  	var (
    19  		registry *podmanRegistry.Registry
    20  		bt       *bindingTest
    21  		s        *gexec.Session
    22  		err      error
    23  	)
    24  
    25  	BeforeEach(func() {
    26  		// Note: we need to start the registry **before** setting up
    27  		// the test. Otherwise, the registry is not reachable for
    28  		// currently unknown reasons.
    29  		registry, err = podmanRegistry.Start()
    30  		Expect(err).To(BeNil())
    31  
    32  		bt = newBindingTest()
    33  		bt.RestoreImagesFromCache()
    34  		s = bt.startAPIService()
    35  		time.Sleep(1 * time.Second)
    36  		err := bt.NewConnection()
    37  		Expect(err).To(BeNil())
    38  	})
    39  
    40  	AfterEach(func() {
    41  		s.Kill()
    42  		bt.cleanup()
    43  		err := registry.Stop()
    44  		Expect(err).To(BeNil())
    45  	})
    46  
    47  	// Test using credentials.
    48  	It("tag + push + pull (with credentials)", func() {
    49  
    50  		imageRep := "localhost:" + registry.Port + "/test"
    51  		imageTag := "latest"
    52  		imageRef := imageRep + ":" + imageTag
    53  
    54  		// Tag the alpine image and verify it has worked.
    55  		err = images.Tag(bt.conn, alpine.shortName, imageTag, imageRep, nil)
    56  		Expect(err).To(BeNil())
    57  		_, err = images.GetImage(bt.conn, imageRef, nil)
    58  		Expect(err).To(BeNil())
    59  
    60  		// Now push the image.
    61  		pushOpts := new(images.PushOptions)
    62  		err = images.Push(bt.conn, imageRef, imageRef, pushOpts.WithUsername(registry.User).WithPassword(registry.Password).WithSkipTLSVerify(true))
    63  		Expect(err).To(BeNil())
    64  
    65  		// Now pull the image.
    66  		pullOpts := new(images.PullOptions)
    67  		_, err = images.Pull(bt.conn, imageRef, pullOpts.WithSkipTLSVerify(true).WithPassword(registry.Password).WithUsername(registry.User))
    68  		Expect(err).To(BeNil())
    69  	})
    70  
    71  	// Test using authfile.
    72  	It("tag + push + pull + search (with authfile)", func() {
    73  
    74  		imageRep := "localhost:" + registry.Port + "/test"
    75  		imageTag := "latest"
    76  		imageRef := imageRep + ":" + imageTag
    77  
    78  		// Create a temporary authentication file.
    79  		tmpFile, err := ioutil.TempFile("", "auth.json.")
    80  		Expect(err).To(BeNil())
    81  		_, err = tmpFile.Write([]byte{'{', '}'})
    82  		Expect(err).To(BeNil())
    83  		err = tmpFile.Close()
    84  		Expect(err).To(BeNil())
    85  
    86  		authFilePath := tmpFile.Name()
    87  
    88  		// Now login to a) test the credentials and to b) store them in
    89  		// the authfile for later use.
    90  		sys := types.SystemContext{
    91  			AuthFilePath:                authFilePath,
    92  			DockerInsecureSkipTLSVerify: types.OptionalBoolTrue,
    93  		}
    94  		loginOptions := auth.LoginOptions{
    95  			Username: registry.User,
    96  			Password: registry.Password,
    97  			AuthFile: authFilePath,
    98  			Stdin:    os.Stdin,
    99  			Stdout:   os.Stdout,
   100  		}
   101  		err = auth.Login(bt.conn, &sys, &loginOptions, []string{imageRep})
   102  		Expect(err).To(BeNil())
   103  
   104  		// Tag the alpine image and verify it has worked.
   105  		err = images.Tag(bt.conn, alpine.shortName, imageTag, imageRep, nil)
   106  		Expect(err).To(BeNil())
   107  		_, err = images.GetImage(bt.conn, imageRef, nil)
   108  		Expect(err).To(BeNil())
   109  
   110  		// Now push the image.
   111  		pushOpts := new(images.PushOptions)
   112  		err = images.Push(bt.conn, imageRef, imageRef, pushOpts.WithAuthfile(authFilePath).WithSkipTLSVerify(true))
   113  		Expect(err).To(BeNil())
   114  
   115  		// Now pull the image.
   116  		pullOpts := new(images.PullOptions)
   117  		_, err = images.Pull(bt.conn, imageRef, pullOpts.WithAuthfile(authFilePath).WithSkipTLSVerify(true))
   118  		Expect(err).To(BeNil())
   119  
   120  		// Last, but not least, exercise search.
   121  		searchOptions := new(images.SearchOptions)
   122  		_, err = images.Search(bt.conn, imageRef, searchOptions.WithSkipTLSVerify(true).WithAuthfile(authFilePath))
   123  		Expect(err).To(BeNil())
   124  	})
   125  
   126  })