Skip to content

Commit 8d32d51

Browse files
committed
chain: add implementation of ChainID
The specification defines an algorithm to calculate a `ChainID`, which can be used to identify the result of subsequent applications of layers. Because this algorithm is subtle and only needs to implemented in a single place, we provide a reference implementation. For convenience, we provide functions that calculate all the chain ids and just the top-level one. It is is integrated with the distribution/digest type for safety and convenience. Tests are formulated based on pre-calculation of chain identifiers to ensure correctness. Signed-off-by: Stephen J Day <[email protected]>
1 parent 656fb2f commit 8d32d51

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed

specs-go/chain/chainid.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2016 The Linux Foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package chain provides implementations of the ChainID calculation used in
16+
// identifying the result of subsequent layer applications.
17+
package chain
18+
19+
import "github.com/docker/distribution/digest"
20+
21+
// ChainID takes a slice of digests and returns the ChainID corresponding to
22+
// the last entry. Typically, these are a list of layer DiffIDs, with the
23+
// result providing the ChainID identifying the result of sequential
24+
// application of the preceeding layers.
25+
func ChainID(dgsts []digest.Digest) digest.Digest {
26+
chainIDs := make([]digest.Digest, len(dgsts))
27+
copy(chainIDs, dgsts)
28+
ChainIDs(chainIDs)
29+
30+
if len(chainIDs) == 0 {
31+
return ""
32+
}
33+
return chainIDs[len(chainIDs)-1]
34+
}
35+
36+
// ChainIDs calculates the recursively applied chain id for each identifier in
37+
// the slice. The result is written direcly back into the slice such that the
38+
// ChainID for each item will be in the respective position.
39+
//
40+
// By definition of ChainID, the zeroth element will always be the same before
41+
// and after the call.
42+
//
43+
// As an exmaple, given the chain of ids `[A, B, C]`, the result `[A,
44+
// ChainID(A|B), ChainID(A|B|C)]` will be written back to the slice.
45+
//
46+
// The input is provided as a return value for convenience.
47+
//
48+
// Typically, these are a list of layer DiffIDs, with the
49+
// result providing the ChainID for each the result of each layer application
50+
// sequentially.
51+
func ChainIDs(dgsts []digest.Digest) []digest.Digest {
52+
if len(dgsts) < 2 {
53+
return dgsts
54+
}
55+
56+
parent := digest.FromBytes([]byte(dgsts[0] + " " + dgsts[1]))
57+
next := dgsts[1:]
58+
next[0] = parent
59+
ChainIDs(next)
60+
61+
return dgsts
62+
}

specs-go/chain/chainid_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright 2016 The Linux Foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package chain
16+
17+
import (
18+
_ "crypto/sha256" // required to install sha256 digest support
19+
"reflect"
20+
"testing"
21+
22+
"github.com/docker/distribution/digest"
23+
)
24+
25+
func TestChainID(t *testing.T) {
26+
// To provide a good testing base, we define the individual links in a
27+
// chain recursively, illustrating the calculations for each chain.
28+
//
29+
// Note that we use invalid digests for the unmodified identifiers here to
30+
// make the computation more readable.
31+
chainDigestAB := digest.FromString("sha256:a" + " " + "sha256:b") // chain for A|B
32+
chainDigestABC := digest.FromString(chainDigestAB.String() + " " + "sha256:c") // chain for A|B|C
33+
34+
for _, testcase := range []struct {
35+
Name string
36+
Digests []digest.Digest
37+
Expected []digest.Digest
38+
}{
39+
{
40+
Name: "nil",
41+
},
42+
{
43+
Name: "empty",
44+
Digests: []digest.Digest{},
45+
Expected: []digest.Digest{},
46+
},
47+
{
48+
Name: "identity",
49+
Digests: []digest.Digest{"sha256:a"},
50+
Expected: []digest.Digest{"sha256:a"},
51+
},
52+
{
53+
Name: "two",
54+
Digests: []digest.Digest{"sha256:a", "sha256:b"},
55+
Expected: []digest.Digest{"sha256:a", chainDigestAB},
56+
},
57+
{
58+
Name: "three",
59+
Digests: []digest.Digest{"sha256:a", "sha256:b", "sha256:c"},
60+
Expected: []digest.Digest{"sha256:a", chainDigestAB, chainDigestABC},
61+
},
62+
} {
63+
t.Run(testcase.Name, func(t *testing.T) {
64+
t.Log("before", testcase.Digests)
65+
66+
var ids []digest.Digest
67+
68+
if testcase.Digests != nil {
69+
ids = make([]digest.Digest, len(testcase.Digests))
70+
copy(ids, testcase.Digests)
71+
}
72+
73+
ids = ChainIDs(ids)
74+
t.Log("after", ids)
75+
if !reflect.DeepEqual(ids, testcase.Expected) {
76+
t.Errorf("unexpected chain: %v != %v", ids, testcase.Expected)
77+
}
78+
79+
if len(testcase.Digests) == 0 {
80+
return
81+
}
82+
83+
// Make sure parent stays stable
84+
if ids[0] != testcase.Digests[0] {
85+
t.Errorf("parent changed: %v != %v", ids[0], testcase.Digests[0])
86+
}
87+
88+
// make sure that the ChainID function takes the last element
89+
id := ChainID(testcase.Digests)
90+
if id != ids[len(ids)-1] {
91+
t.Errorf("incorrect chain id returned from ChainID: %v != %v", id, ids[len(ids)-1])
92+
}
93+
})
94+
}
95+
}

0 commit comments

Comments
 (0)