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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
package layout
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"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.FromString("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
}
func TestGetLocalBlobPath(t *testing.T) {
tmpDir := loadFixture(t, "delete_image_multiple_images")
ref, err := NewReference(tmpDir, "latest")
require.NoError(t, err)
src, err := ref.NewImageSource(context.Background(), &types.SystemContext{})
require.NoError(t, err)
defer src.Close()
// success cases
for _, dig := range []digest.Digest{
"sha256:a2f798327b3f25e3eff54badcb769953de235e62e3e32051d57a5e66246de4a1",
"sha256:557ac7d133b7770216a8101268640edf4e88beab1b4e1e1bfc9b1891a1cab861",
} {
path, err := GetLocalBlobPath(context.Background(), src, dig)
require.NoError(t, err)
algo, hash, _ := strings.Cut(string(dig), ":")
expect := filepath.Join(tmpDir, "blobs", algo, hash)
assert.Equal(t, expect, path)
}
// error cases
for _, dig := range []digest.Digest{
// Invalid digest must error.
"sha256:as",
// Valid digest but they don't exist in the oci layout thus they must error.
"sha256:abababababababababababababababababababababababababababababababab",
"sha512:a2f798327b3f25e3eff54badcb769953de235e62e3e32051d57a5e66246de4a1557ac7d133b7770216a8101268640edf4e88beab1b4e1e1bfc9b1891a1cab861",
} {
_, err = GetLocalBlobPath(context.Background(), src, dig)
require.Error(t, err)
}
}
|