1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
package layout
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/containers/image/v5/internal/private"
"github.com/containers/image/v5/pkg/blobinfocache/memory"
"github.com/containers/image/v5/types"
digest "github.com/opencontainers/go-digest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var _ private.ImageSource = (*ociImageSource)(nil)
const RemoteLayerContent = "This is the remote layer content"
var httpServerAddr string
func TestMain(m *testing.M) {
httpServer, err := startRemoteLayerServer()
if err != nil {
fmt.Fprintf(os.Stderr, "Error starting test TLS server: %v", err.Error())
os.Exit(1)
}
httpServerAddr = strings.Replace(httpServer.URL, "127.0.0.1", "localhost", 1)
code := m.Run()
httpServer.Close()
os.Exit(code)
}
func TestGetBlobForRemoteLayers(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello world")
}))
defer ts.Close()
cache := memory.New()
imageSource := createImageSource(t, &types.SystemContext{})
defer imageSource.Close()
layerInfo := types.BlobInfo{
Digest: digest.FromBytes([]byte("Hello world")),
Size: -1,
URLs: []string{
"brokenurl",
ts.URL,
},
}
reader, _, err := imageSource.GetBlob(context.Background(), layerInfo, cache)
require.NoError(t, err)
defer reader.Close()
data, err := io.ReadAll(reader)
require.NoError(t, err)
assert.Contains(t, string(data), "Hello world")
}
func TestGetBlobForRemoteLayersWithTLS(t *testing.T) {
imageSource := createImageSource(t, &types.SystemContext{
OCICertPath: "fixtures/accepted_certs",
})
defer imageSource.Close()
cache := memory.New()
layer, size, err := imageSource.GetBlob(context.Background(), types.BlobInfo{
URLs: []string{httpServerAddr},
}, cache)
require.NoError(t, err)
layerContent, _ := io.ReadAll(layer)
assert.Equal(t, RemoteLayerContent, string(layerContent))
assert.Equal(t, int64(len(RemoteLayerContent)), size)
}
func TestGetBlobForRemoteLayersOnTLSFailure(t *testing.T) {
imageSource := createImageSource(t, &types.SystemContext{
OCICertPath: "fixtures/rejected_certs",
})
defer imageSource.Close()
cache := memory.New()
layer, size, err := imageSource.GetBlob(context.Background(), types.BlobInfo{
URLs: []string{httpServerAddr},
}, cache)
require.Error(t, err)
assert.Nil(t, layer)
assert.Equal(t, int64(0), size)
}
func remoteLayerContent(w http.ResponseWriter, req *http.Request) {
fmt.Fprint(w, RemoteLayerContent)
}
func startRemoteLayerServer() (*httptest.Server, error) {
certBytes, err := os.ReadFile("fixtures/accepted_certs/cert.cert")
if err != nil {
return nil, err
}
clientCertPool := x509.NewCertPool()
if !clientCertPool.AppendCertsFromPEM(certBytes) {
return nil, fmt.Errorf("Could not append certificate")
}
cert, err := tls.LoadX509KeyPair("fixtures/accepted_certs/cert.cert", "fixtures/accepted_certs/cert.key")
if err != nil {
return nil, err
}
tlsConfig := &tls.Config{
// Reject any TLS certificate that cannot be validated
ClientAuth: tls.RequireAndVerifyClientCert,
// Ensure that we only use our "CA" to validate certificates
ClientCAs: clientCertPool,
Certificates: []tls.Certificate{cert},
}
httpServer := httptest.NewUnstartedServer(http.HandlerFunc(remoteLayerContent))
httpServer.TLS = tlsConfig
httpServer.StartTLS()
return httpServer, nil
}
func createImageSource(t *testing.T, sys *types.SystemContext) types.ImageSource {
imageRef, err := NewReference("fixtures/manifest", "")
require.NoError(t, err)
imageSource, err := imageRef.NewImageSource(context.Background(), sys)
require.NoError(t, err)
return imageSource
}
|