Skip to content

Commit 0ff14aa

Browse files
authored
Merge pull request #486 from stevvooe/chainid-implementation
identity: add implementation of ChainID
2 parents eabda42 + 05bcdc7 commit 0ff14aa

File tree

13 files changed

+1242
-8
lines changed

13 files changed

+1242
-8
lines changed

glide.lock

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package: github.com/opencontainers/image-spec
22
import:
33
- package: github.com/pkg/errors
4-
version: ">=0.7.0"
4+
version: '>=0.7.0'
55
- package: github.com/xeipuuv/gojsonschema
66
version: d5336c75940ef31c9ceeb0ae64cf92944bccb4ee
77
- package: github.com/russross/blackfriday
88
version: ~v1.4
99
- package: github.com/shurcooL/sanitized_anchor_name
1010
version: 10ef21a441db47d8b13ebcc5fd2310f636973c77
11+
- package: github.com/opencontainers/go-digest

identity/chainid.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 identity provides implementations of subtle calculations pertaining
16+
// to image and layer identity. The primary item present here is the ChainID
17+
// calculation used in identifying the result of subsequent layer applications.
18+
//
19+
// Helpers are also provided here to ease transition to the
20+
// github.com/opencontainers/go-digest package, but that package may be used
21+
// directly.
22+
package identity
23+
24+
import "github.com/opencontainers/go-digest"
25+
26+
// ChainID takes a slice of digests and returns the ChainID corresponding to
27+
// the last entry. Typically, these are a list of layer DiffIDs, with the
28+
// result providing the ChainID identifying the result of sequential
29+
// application of the preceding layers.
30+
func ChainID(dgsts []digest.Digest) digest.Digest {
31+
chainIDs := make([]digest.Digest, len(dgsts))
32+
copy(chainIDs, dgsts)
33+
ChainIDs(chainIDs)
34+
35+
if len(chainIDs) == 0 {
36+
return ""
37+
}
38+
return chainIDs[len(chainIDs)-1]
39+
}
40+
41+
// ChainIDs calculates the recursively applied chain id for each identifier in
42+
// the slice. The result is written direcly back into the slice such that the
43+
// ChainID for each item will be in the respective position.
44+
//
45+
// By definition of ChainID, the zeroth element will always be the same before
46+
// and after the call.
47+
//
48+
// As an example, given the chain of ids `[A, B, C]`, the result `[A,
49+
// ChainID(A|B), ChainID(A|B|C)]` will be written back to the slice.
50+
//
51+
// The input is provided as a return value for convenience.
52+
//
53+
// Typically, these are a list of layer DiffIDs, with the
54+
// result providing the ChainID for each the result of each layer application
55+
// sequentially.
56+
func ChainIDs(dgsts []digest.Digest) []digest.Digest {
57+
if len(dgsts) < 2 {
58+
return dgsts
59+
}
60+
61+
parent := digest.FromBytes([]byte(dgsts[0] + " " + dgsts[1]))
62+
next := dgsts[1:]
63+
next[0] = parent
64+
ChainIDs(next)
65+
66+
return dgsts
67+
}

identity/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 identity
16+
17+
import (
18+
_ "crypto/sha256" // required to install sha256 digest support
19+
"reflect"
20+
"testing"
21+
22+
"github.com/opencontainers/go-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+
}

identity/helpers.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 identity
16+
17+
import (
18+
_ "crypto/sha256" // side-effect to install impls, sha256
19+
_ "crypto/sha512" // side-effect to install impls, sha384/sh512
20+
21+
"io"
22+
23+
digest "github.com/opencontainers/go-digest"
24+
)
25+
26+
// FromReader consumes the content of rd until io.EOF, returning canonical
27+
// digest.
28+
func FromReader(rd io.Reader) (digest.Digest, error) {
29+
return digest.Canonical.FromReader(rd)
30+
}
31+
32+
// FromBytes digests the input and returns a Digest.
33+
func FromBytes(p []byte) digest.Digest {
34+
return digest.Canonical.FromBytes(p)
35+
}
36+
37+
// FromString digests the input and returns a Digest.
38+
func FromString(s string) digest.Digest {
39+
return digest.Canonical.FromString(s)
40+
}

0 commit comments

Comments
 (0)