diff --git a/cmd/oras/internal/option/remote.go b/cmd/oras/internal/option/remote.go index fa28bfafd..74f128bf2 100644 --- a/cmd/oras/internal/option/remote.go +++ b/cmd/oras/internal/option/remote.go @@ -50,6 +50,8 @@ import ( type Remote struct { DistributionSpec CACertFilePath string + CertFilePath string + KeyFilePath string Insecure bool Configs []string Username string @@ -111,6 +113,8 @@ func (opts *Remote) ApplyFlagsWithPrefix(fs *pflag.FlagSet, prefix, description return *plainHTTP, fs.Changed(plainHTTPFlagName) } fs.StringVarP(&opts.CACertFilePath, flagPrefix+"ca-file", "", "", "server certificate authority file for the remote "+notePrefix+"registry") + fs.StringVarP(&opts.CertFilePath, flagPrefix+"cert-file", "", "", "client certificate authority file for the remote "+notePrefix+"registry") + fs.StringVarP(&opts.KeyFilePath, flagPrefix+"key-file", "", "", "client private key file for the remote "+notePrefix+"registry") fs.StringArrayVarP(&opts.resolveFlag, flagPrefix+"resolve", "", nil, "customized DNS for "+notePrefix+"registry, formatted in `host:port:address[:address_port]`") fs.StringArrayVarP(&opts.Configs, flagPrefix+"registry-config", "", nil, "`path` of the authentication file for "+notePrefix+"registry") fs.StringArrayVarP(&opts.headerFlags, flagPrefix+"header", shortHeader, nil, "add custom headers to "+notePrefix+"requests") @@ -191,6 +195,13 @@ func (opts *Remote) tlsConfig() (*tls.Config, error) { return nil, err } } + if opts.CertFilePath != "" && opts.KeyFilePath != "" { + cert, err := tls.LoadX509KeyPair(opts.CertFilePath, opts.KeyFilePath) + if err != nil { + return nil, err + } + config.Certificates = []tls.Certificate{cert} + } return config, nil } diff --git a/cmd/oras/internal/option/remote_test.go b/cmd/oras/internal/option/remote_test.go index a55a4edc3..f7ae345a3 100644 --- a/cmd/oras/internal/option/remote_test.go +++ b/cmd/oras/internal/option/remote_test.go @@ -18,9 +18,10 @@ package option import ( "context" "crypto/rand" + "crypto/tls" + "crypto/x509" "encoding/base64" "encoding/json" - "encoding/pem" "fmt" "net/http" "net/http/httptest" @@ -28,6 +29,7 @@ import ( "os" "path/filepath" "reflect" + "strings" "testing" "github.com/sirupsen/logrus" @@ -43,9 +45,213 @@ var testTagList = struct { Tags: []string{"tag"}, } +// localhostServerCert is a PEM-encoded TLS cert with SAN IPs +// "127.0.0.1" and "[::1]", expiring at Jan 29 16:00:00 2084 GMT. +// adapted from src/crypto/tls: +// go run generate_cert.go --rsa-bits 4096 --host 127.0.0.1,::1,oras.land --ca --start-date "Jan 1 00:00:00 1970" --duration=1000000h +var localhostServerCert = []byte(`-----BEGIN CERTIFICATE----- +MIIFMjCCAxqgAwIBAgIRAPzEFsDt4E8GxCNLsF76j18wDQYJKoZIhvcNAQELBQAw +DzENMAsGA1UEChMET1JBUzAgFw03MDAxMDEwMDAwMDBaGA8yMDg0MDEyOTE2MDAw +MFowDzENMAsGA1UEChMET1JBUzCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoC +ggIBAKcbdhkxvx0CXeDZuEz3kKgVsQpyNRXP0AWKS8FqxbVe+NLDZl9jzDtQBvCt +BW3UjgIxnUMG5tGrtTHvzLJNfkX4DvTkWUOiLu8VxcAT6vG5v87xyrq86taMLrCm +o42wHpJNETPlCtJquGEADPHs4D+EOAMWfaCvy5rEYt6JD/oy6/VehPInE3Q634NK +98OD25xykOtq1sIvoZibIgq8T5GlmBrxAb+axfhX8tp7NEJ4wvOUorEnww3cZJb6 +7Q6ijvzEWT2RD58prR7l4yvsb+HmSEWoJcd5JZd0CUiVIM44L3E4qllXJ2vwT9Fv +dXwfwx29zQ9VVLlNibPzJ4a0ZEIhi8ZMGtoLR7gEhsM+PraZz+2TL5APJWI2TRZk +A2m7v2DMv4ZbUOOy1OivOIwJqA6k9lsm2oMOPrTIZ0pZIXu1bmF0/Z8js4LNx5HU +4SsIHYpj1efH27YgjfBY9ROX3iITQsKyUpCpF9z845tzbdw66tRTazLvumNVrdlz +fKceFDbmgi6uz/lILSppy/kzKkoaY6+NTogW6Kg/2icPH8rg6LpxHpQlAHqABB8t +25w39DQoD5sPGt/ChEX5Pb0bLUWlnl+6vKa4vfctiZ2DevGePLrK1lW+L4K05yCF +CpqPWKvm75aVs9HZGFbIsJTF2B4IQnYmm9IhqFuSh1sZBf2dAgMBAAGjgYYwgYMw +DgYDVR0PAQH/BAQDAgKkMBMGA1UdJQQMMAoGCCsGAQUFBwMBMA8GA1UdEwEB/wQF +MAMBAf8wHQYDVR0OBBYEFHSvVlBTGCMOip+SGZ7QO+0isE8tMCwGA1UdEQQlMCOC +CW9yYXMubGFuZIcEfwAAAYcQAAAAAAAAAAAAAAAAAAAAATANBgkqhkiG9w0BAQsF +AAOCAgEANrLbIqIe+7hd51BZDJn1tSE/Ld/kgwJ8mBZdB6XF2WLoCgVOx9VGmJH3 +Z09b0KqJpp3s2HgYJNc5AHNAyH/u1E4HUAyKajuI420Z/GdV3CK2uwcp5mkkF4qK +ew6PEYUQRjZf95k+6T8VR8P9O0kiigr4Srgqal2EolSf8e27EO+JERueVn3NgRms +FrJv2+LILKFfBt5o9S1D5xNa9qu6NfW3x6F0VwaBxgjnUpqePUQaFzSSZ+VXmKmh +rx7I8PaNrac5DslDtmpVj/z9RwuExPZAB+/utocUWmSU0epTyqXiKQy8sgSVhsVe +yyClPguXtuD3IV/i5uAcSVf1xrCSSifkYnmhfMA5lUgjOmDuY/Am4f2DJcjBETb2 +hsac+8C6IppGrZOuOinTp5ZIocFKjNxcjGnmqxk0hWW6GuF4truKSEicIiBvoqZp +rearfvDbM6g4CobowU1S6vNkUc2ziCE23AmoY65V3Vnmj72p5Mi7P932jzf2qEA1 +vCGB9hrxO3Yr5wkOXUGQI/nU+KQTCdf/4j4kUh+N1pByGMPG05GrOpmy1Tems9sp +BddDm85WsThpxTf+Rp5xp5/FQh72eigkXa/ezeldEGI/rhKRJJtujU6Kq6SoKX29 +YRSTpM+MeFPJDVffVUFXQrxnGIXRzxhx49wMSoKFYR/225exX1c= +-----END CERTIFICATE-----`) + +// localhostServerKey is the private key for localhostServerCert. +var localhostServerKey = []byte(testingKey(`-----BEGIN RS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LwyViIAgaZvunID5Q== +-----END RSA TESTING KEY-----`)) + +// localhostClientCert is a PEM-encoded TLS cert with SAN IPs +// "127.0.0.1" and "[::1]", expiring at Jan 29 16:00:00 2084 GMT. +// adapted from src/crypto/tls: +// go run generate_cert.go --rsa-bits 4096 --host 127.0.0.1,::1,oras.land --ca --start-date "Jan 1 00:00:00 1970" --duration=1000000h +var localhostClientCert = []byte(`-----BEGIN CERTIFICATE----- +MIIFMjCCAxqgAwIBAgIRAMow8iHaN1G+R2GjMyOC5LQwDQYJKoZIhvcNAQELBQAw +DzENMAsGA1UEChMET1JBUzAgFw03MDAxMDEwMDAwMDBaGA8yMDg0MDEyOTE2MDAw +MFowDzENMAsGA1UEChMET1JBUzCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoC +ggIBAM01pjqP4xZmX8zAT5p7k90FpbZ1W5S8qLIkajvzzoGeksSH2FgG+rXK+yJ6 +TZo74a5ZKOUHtdjurRTvMgCjCvFIWv0wqnkeVFmXDLXQZiEamatPInGF2vJnv9BW +O4sLdopyXxAd4BQiC7ot8JO2PRiLubPRHDp2xExsDbqiAPU49qvwVFEfdnU+nHdt +jmmcZKzeD4iiu9DeZZJhv9h+OEZNXyHNDFV9/CRNrwO9Xfx8OvTZNklSYOyF0YER +nr1mmdQBZ4SCYYGBBdWvjkII7hZNkT9/qOBB2p/JXquF+6GYGruVAfAYRfADXCg1 ++T+M8X1XZuja5wIxvwbH8JV5RXN/vk9mt36EXGSuesAOlVYKnv3Ux1bxPSuWT3RU +47GM/8vmelopHTIUtcLPnNFk3xlWyjNx7Sv67CxWniJFuOXrqrI0DLj/EQzOxoMa +uD0WO0BXHHNSFF7dhrd6FTNu3EppYFWQt9wBsZ8jgDRsHh2GeuyEo1YlSSB+rvs4 +20w9cCSYGduJ4BKqcGRDY6c9QCRgZ5VWs+feS3awBK6Gx4mWh4bjM1ICGbiPeiw9 +2DeHjjAqunlMN0z7Pa28lE0GE3rZpcwxtS3Blnm6Q/lDbCBmOWrGH4ngQ02HeaY4 +t7xUayaiz3lkZjcTrdZa9OxHLZwmGmqY7JB3X/TReePr9jT5AgMBAAGjgYYwgYMw +DgYDVR0PAQH/BAQDAgKkMBMGA1UdJQQMMAoGCCsGAQUFBwMCMA8GA1UdEwEB/wQF +MAMBAf8wHQYDVR0OBBYEFDpedkP2AJKzCgJkZf9PpIOIXRHbMCwGA1UdEQQlMCOC +CW9yYXMubGFuZIcEfwAAAYcQAAAAAAAAAAAAAAAAAAAAATANBgkqhkiG9w0BAQsF +AAOCAgEAGtRCFG27HZqu2kz0IW6UuYUKv7gqtN4/47At3CHFAd3UAAErKrammwFH +fWB4zy4MU9fHMgJhc8ctZg0QR8f1fnpN8MlnmV3fqEbUVwbZov2x46wgwLD7PRZM +NPh+Z2reFRE2VNi+rZsqcTAPy48FQ0WdCpzQheeonincLuY6GYV1DjhHn3HNFqkn +lslS5FRRkkLxXLgr0p0fNAEUMqce16VjVcM2BbEuCRvlepZZF7uVvEOQvtEMJSdF +APmUUsS7x6jvIxmZpxzanBmu2u4kQwTMW4UDpkNVwdCPGEM/SAdQUkqOYIQU807n +93QrXXY3XIlDp7U6xA2AfZQ9fiV7cDul06IzsXcisTeK8uSfhHRROqbt9MotiIve +xEVTyh4tfue7kFowq7wd3H2UTSLgByfJBkEAN+BTDUpEwT7gZOVcxbzpkL+vyPfr +CVdIbE9ZlBzCc5i0RWaMrEJfFy93a7ZptlXytfQM/MFOlKaIDXFjoylmkW9IhrFd +Ger/gr4AFzD7AQRF9+c0Cu4je3A+ob9R0CvvwEthjQCrOWjVgbUxR+qMVNCtCRwk +Bt4BEd7qH0bM8EAY7BwPEP7vfvp7iblp0FNNLYQCbxd0eK69Tb2olzQRKKyNpqls +/cqUr8lpYbGAdorLJSeCj8EZ7+o9ZJXbxilkcMl3R8wV8mn5Olw= +-----END CERTIFICATE-----`) + +// localhostClientKey is the private key for localhostClientCert. +var localhostClientKey = []byte(testingKey(`-----BEGIN RSA TESTING KEY----- +MIIJQg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----END RSA TESTING KEY-----`)) + +func testingKey(s string) string { return strings.ReplaceAll(s, "TESTING KEY", "PRIVATE KEY") } + +func loadTestingTLSConfig() *tls.Config { + + clientCertPool := x509.NewCertPool() + clientCertPool.AppendCertsFromPEM(localhostClientCert) + + tlsConfig := &tls.Config{ + Certificates: []tls.Certificate{loadTestingCert(localhostServerCert, localhostServerKey)}, + ClientAuth: tls.VerifyClientCertIfGiven, + ClientCAs: clientCertPool, + } + + return tlsConfig +} + +func loadTestingCert(certificate, key []byte) tls.Certificate { + cert, err := tls.X509KeyPair(certificate, key) + if err != nil { + panic(fmt.Sprintf("Unable to load testing certificate: %v", err)) + } + + return cert + +} + func TestMain(m *testing.M) { // Test server - ts = httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ts = httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { p := r.URL.Path m := r.Method switch { @@ -57,6 +263,8 @@ func TestMain(m *testing.M) { } } })) + ts.TLS = loadTestingTLSConfig() + ts.StartTLS() defer ts.Close() m.Run() } @@ -116,7 +324,7 @@ func TestRemote_authClient_skipTlsVerify(t *testing.T) { func TestRemote_authClient_CARoots(t *testing.T) { caPath := filepath.Join(t.TempDir(), "oras-test.pem") - if err := os.WriteFile(caPath, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: ts.Certificate().Raw}), 0644); err != nil { + if err := os.WriteFile(caPath, localhostServerCert, 0644); err != nil { t.Fatalf("unexpected error: %v", err) } @@ -174,7 +382,7 @@ func plainHTTPNotSpecified() (plainHTTP bool, fromFlag bool) { func TestRemote_NewRegistry(t *testing.T) { caPath := filepath.Join(t.TempDir(), "oras-test.pem") - if err := os.WriteFile(caPath, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: ts.Certificate().Raw}), 0644); err != nil { + if err := os.WriteFile(caPath, localhostServerCert, 0644); err != nil { t.Fatalf("unexpected error: %v", err) } @@ -203,15 +411,63 @@ func TestRemote_NewRegistry(t *testing.T) { func TestRemote_NewRepository(t *testing.T) { caPath := filepath.Join(t.TempDir(), "oras-test.pem") - if err := os.WriteFile(caPath, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: ts.Certificate().Raw}), 0644); err != nil { + if err := os.WriteFile(caPath, localhostServerCert, 0644); err != nil { + t.Fatalf("unexpected error: %v", err) + } + opts := struct { + Remote + Common + }{ + Remote{ + CACertFilePath: caPath, + plainHTTP: plainHTTPNotSpecified, + }, + Common{}, + } + + uri, err := url.ParseRequestURI(ts.URL) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + repo, err := opts.NewRepository(uri.Host+"/"+testRepo, opts.Common, logrus.New()) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if err = repo.Tags(context.Background(), "", func(got []string) error { + want := []string{"tag"} + if len(got) != len(testTagList.Tags) || !reflect.DeepEqual(got, want) { + return fmt.Errorf("expect: %v, got: %v", testTagList.Tags, got) + } + return nil + }); err != nil { + t.Fatalf("unexpected error: %v", err) + } +} + +func TestRemote_NewRepositoryMTLS(t *testing.T) { + caPath := filepath.Join(t.TempDir(), "oras-test.pem") + if err := os.WriteFile(caPath, localhostServerCert, 0644); err != nil { + t.Fatalf("unexpected error: %v", err) + } + + clientCertPath := filepath.Join(t.TempDir(), "oras-test-client.pem") + if err := os.WriteFile(clientCertPath, localhostClientCert, 0644); err != nil { + t.Fatalf("unexpected error: %v", err) + } + + clientKeyPath := filepath.Join(t.TempDir(), "oras-test-client.key") + if err := os.WriteFile(clientKeyPath, localhostClientKey, 0644); err != nil { t.Fatalf("unexpected error: %v", err) } + opts := struct { Remote Common }{ Remote{ CACertFilePath: caPath, + CertFilePath: clientCertPath, + KeyFilePath: clientKeyPath, plainHTTP: plainHTTPNotSpecified, }, Common{}, @@ -238,11 +494,11 @@ func TestRemote_NewRepository(t *testing.T) { func TestRemote_NewRepository_Retry(t *testing.T) { caPath := filepath.Join(t.TempDir(), "oras-test.pem") - if err := os.WriteFile(caPath, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: ts.Certificate().Raw}), 0644); err != nil { + if err := os.WriteFile(caPath, localhostServerCert, 0644); err != nil { t.Fatalf("unexpected error: %v", err) } retries, count := 3, 0 - ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { count++ if count < retries { http.Error(w, "error", http.StatusTooManyRequests) @@ -253,6 +509,8 @@ func TestRemote_NewRepository_Retry(t *testing.T) { http.Error(w, "error encoding", http.StatusBadRequest) } })) + ts.TLS = loadTestingTLSConfig() + ts.StartTLS() defer ts.Close() opts := struct { Remote