Skip to content

Commit eb5c414

Browse files
dlorenctekton-robot
authored andcommitted
Fix staleness issue in entrypoint resolver.
There was a bug in the entrypoint resolution logic that allowed for stale values of the entrypoint to be returned. If an image is supplied by tag, and that tag changes to point to an image with a new entrypoint, we returned the old one. This change uses the image digest itself as our cache key, instead of the image name.
1 parent 33ea1a0 commit eb5c414

File tree

2 files changed

+106
-13
lines changed

2 files changed

+106
-13
lines changed

pkg/reconciler/v1alpha1/taskrun/entrypoint/entrypoint.go

+25-2
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,34 @@ func getWaitFile(stepNum int) string {
187187
// GetRemoteEntrypoint accepts a cache of digest lookups, as well as the digest
188188
// to look for. If the cache does not contain the digest, it will lookup the
189189
// metadata from the images registry, and then commit that to the cache
190-
func GetRemoteEntrypoint(cache *Cache, digest string, kubeclient kubernetes.Interface, taskRun *v1alpha1.TaskRun) ([]string, error) {
190+
func GetRemoteEntrypoint(cache *Cache, image string, kubeclient kubernetes.Interface, taskRun *v1alpha1.TaskRun) ([]string, error) {
191+
ref, err := name.ParseReference(image, name.WeakValidation)
192+
if err != nil {
193+
return nil, xerrors.Errorf("Failed to parse image %s: %w", image, err)
194+
}
195+
196+
var digest string
197+
// If the image is specified as a digest, we can just take the digest from the name and use that in our cache.
198+
// Otherwise we first have to resolve the tag to a digest.
199+
if d, ok := ref.(name.Digest); ok {
200+
digest = d.String()
201+
} else {
202+
img, err := getRemoteImage(image, kubeclient, taskRun)
203+
if err != nil {
204+
return nil, xerrors.Errorf("Failed to fetch remote image %s: %w", digest, err)
205+
}
206+
d, err := img.Digest()
207+
if err != nil {
208+
return nil, xerrors.Errorf("Failed to get digest for image %s: %w", image, err)
209+
}
210+
digest = d.String()
211+
}
212+
191213
if ep, ok := cache.get(digest); ok {
192214
return ep, nil
193215
}
194-
img, err := getRemoteImage(digest, kubeclient, taskRun)
216+
217+
img, err := getRemoteImage(image, kubeclient, taskRun)
195218
if err != nil {
196219
return nil, xerrors.Errorf("Failed to fetch remote image %s: %w", digest, err)
197220
}

pkg/reconciler/v1alpha1/taskrun/entrypoint/entrypoint_test.go

+81-11
Original file line numberDiff line numberDiff line change
@@ -213,19 +213,14 @@ func getDigestAsString(image v1.Image) string {
213213
return digestHash.String()
214214
}
215215

216-
func TestGetRemoteEntrypoint(t *testing.T) {
217-
expectedEntrypoint := []string{"/bin/expected", "entrypoint"}
218-
img := getImage(t, &v1.ConfigFile{
219-
Config: v1.Config{
220-
Entrypoint: expectedEntrypoint,
221-
},
222-
})
216+
func getServer(t *testing.T, img v1.Image) *httptest.Server {
223217
expectedRepo := "image"
224-
digetsSha := getDigestAsString(img)
218+
225219
configPath := fmt.Sprintf("/v2/%s/blobs/%s", expectedRepo, mustConfigName(t, img))
226-
manifestPath := fmt.Sprintf("/v2/%s/manifests/%s", expectedRepo, digetsSha)
220+
manifestPath := fmt.Sprintf("/v2/%s/manifests/%s", expectedRepo, getDigestAsString(img))
221+
latestPath := fmt.Sprintf("/v2/%s/manifests/latest", expectedRepo)
227222

228-
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
223+
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
229224
switch r.URL.Path {
230225
case "/v2/":
231226
w.WriteHeader(http.StatusOK)
@@ -236,7 +231,7 @@ func TestGetRemoteEntrypoint(t *testing.T) {
236231
if _, err := w.Write(mustRawConfigFile(t, img)); err != nil {
237232
t.Fatal(err)
238233
}
239-
case manifestPath:
234+
case manifestPath, latestPath:
240235
if r.Method != http.MethodGet {
241236
t.Errorf("Method; got %v, want %v", r.Method, http.MethodGet)
242237
}
@@ -247,6 +242,19 @@ func TestGetRemoteEntrypoint(t *testing.T) {
247242
t.Fatalf("Unexpected path: %v", r.URL.Path)
248243
}
249244
}))
245+
}
246+
247+
func TestGetRemoteEntrypoint(t *testing.T) {
248+
expectedEntrypoint := []string{"/bin/expected", "entrypoint"}
249+
img := getImage(t, &v1.ConfigFile{
250+
Config: v1.Config{
251+
Entrypoint: expectedEntrypoint,
252+
},
253+
})
254+
expectedRepo := "image"
255+
digetsSha := getDigestAsString(img)
256+
257+
server := getServer(t, img)
250258
defer server.Close()
251259
image := path.Join(strings.TrimPrefix(server.URL, "http://"), expectedRepo)
252260
finalDigest := image + "@" + digetsSha
@@ -286,6 +294,68 @@ func TestGetRemoteEntrypoint(t *testing.T) {
286294
}
287295
}
288296

297+
func TestGetRemoteEntrypointStale(t *testing.T) {
298+
initialEntrypoint := []string{"/bin/expected", "entrypoint"}
299+
img := getImage(t, &v1.ConfigFile{
300+
Config: v1.Config{
301+
Entrypoint: initialEntrypoint,
302+
},
303+
})
304+
305+
server := getServer(t, img)
306+
defer server.Close()
307+
expectedRepo := "image"
308+
image := path.Join(strings.TrimPrefix(server.URL, "http://"), expectedRepo) + ":latest"
309+
310+
entrypointCache, err := NewCache()
311+
if err != nil {
312+
t.Fatalf("couldn't create new entrypoint cache: %v", err)
313+
}
314+
taskRun := &v1alpha1.TaskRun{
315+
ObjectMeta: metav1.ObjectMeta{
316+
Namespace: "foo",
317+
Name: "taskRun",
318+
},
319+
Spec: v1alpha1.TaskRunSpec{
320+
ServiceAccount: "default",
321+
},
322+
}
323+
c := fakekubeclientset.NewSimpleClientset(&corev1.ServiceAccount{
324+
ObjectMeta: metav1.ObjectMeta{
325+
Name: "default",
326+
Namespace: "foo",
327+
},
328+
})
329+
ep1, err := GetRemoteEntrypoint(entrypointCache, image, c, taskRun)
330+
if err != nil {
331+
t.Errorf("couldn't get entrypoint remote: %v", err)
332+
}
333+
server.Close()
334+
335+
// Now change the image
336+
secondEntrypoint := []string{"/bin/expected", "entrypoint2"}
337+
img = getImage(t, &v1.ConfigFile{
338+
Config: v1.Config{
339+
Entrypoint: secondEntrypoint,
340+
},
341+
})
342+
server2 := getServer(t, img)
343+
image = path.Join(strings.TrimPrefix(server2.URL, "http://"), expectedRepo) + ":latest"
344+
defer server2.Close()
345+
ep2, err := GetRemoteEntrypoint(entrypointCache, image, c, taskRun)
346+
if err != nil {
347+
t.Fatalf("couldn't get entrypoint remote: %v", err)
348+
}
349+
350+
if !reflect.DeepEqual(ep1, initialEntrypoint) {
351+
t.Errorf("entrypoints do not match: %s should be %s", ep1, initialEntrypoint)
352+
}
353+
354+
if !reflect.DeepEqual(ep2, secondEntrypoint) {
355+
t.Errorf("entrypoints do not match: %s should be %s", ep2, secondEntrypoint)
356+
}
357+
}
358+
289359
func TestGetRemoteEntrypointWithNonDefaultSA(t *testing.T) {
290360
expectedEntrypoint := []string{"/bin/expected", "entrypoint"}
291361
img := getImage(t, &v1.ConfigFile{

0 commit comments

Comments
 (0)