feat: support https URLs for digest-file (#2811)

This feature allows one to specify an https URL for any of the
digest-file options, resulting in an HTTP PUT to the provided
URL. This could for example be a (pre-signed) URL to S3 or GCS.

Currently the final digest is only written to the local filesystem,
which disappears and is not accessible when Kaniko is run in a
managed container service like AWS ECS.

By supporting https a single implementation supports all storage
services, without the need for special code for S3, GCS, etc..
This commit is contained in:
Lio李歐
2023-11-10 11:05:36 -08:00
committed by GitHub
parent 5133ad83b1
commit e5395c7a21
2 changed files with 42 additions and 0 deletions
+30
View File
@@ -19,8 +19,10 @@ package executor
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
@@ -475,4 +477,32 @@ func TestWriteDigestFile(t *testing.T) {
t.Errorf("expected file to be written successfully, but got error: %v", err)
}
})
t.Run("https PUT OK", func(t *testing.T) {
var uploadedContent []byte
// Start a test server that checks the PUT request.
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPut {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
uploadedContent, _ = io.ReadAll(r.Body)
w.WriteHeader(http.StatusNoContent)
}))
defer server.Close()
// Temporarily replace the default client with the test server client to avoid TLS verification errors.
oldClient := http.DefaultClient
defer func() { http.DefaultClient = oldClient }()
http.DefaultClient = server.Client()
err := writeDigestFile(server.URL+"/df?sig=1234", []byte("test"))
if err != nil {
t.Fatalf("expected file to be written successfully, but got error: %v", err)
}
if string(uploadedContent) != "test" {
t.Errorf("expected uploaded content to be 'test', but got '%s'", uploadedContent)
}
})
}