Skip to content

Commit 8edcf6b

Browse files
authored
Merge pull request #506 from diofeher/fix-install-unstable-tofu
fix: cosign check for unstable tofu versions
2 parents 6f56c2b + 9284961 commit 8edcf6b

File tree

2 files changed

+79
-11
lines changed

2 files changed

+79
-11
lines changed

versionmanager/retriever/tofu/tofuretriever.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package tofuretriever
2121
import (
2222
"context"
2323
"errors"
24+
"fmt"
2425
"net/url"
2526
"runtime"
2627
"strings"
@@ -52,9 +53,9 @@ const (
5253

5354
defaultTofuURLTemplate = "https://github.com/opentofu/opentofu/releases/download/v{{ .Version }}/{{ .Artifact }}"
5455

55-
baseIdentity = "https://github.com/opentofu/opentofu/.github/workflows/release.yml@refs/heads/v"
56-
issuer = "https://token.actions.githubusercontent.com"
57-
unstableIdentity = "https://github.com/opentofu/opentofu/.github/workflows/release.yml@refs/heads/main"
56+
baseIdentity = "https://github.com/opentofu/opentofu/.github/workflows/release.yml@refs/heads/v"
57+
issuer = "https://token.actions.githubusercontent.com"
58+
mainIdentity = "https://github.com/opentofu/opentofu/.github/workflows/release.yml@refs/heads/main"
5859

5960
baseFileName = "tofu_"
6061
)
@@ -211,7 +212,7 @@ func (r TofuRetriever) checkSumAndSig(ctx context.Context, version *version.Vers
211212
return err
212213
}
213214

214-
identity := buildIdentity(version, stable)
215+
identity := buildIdentity(version)
215216
err = cosigncheck.Check(ctx, dataSums, dataSumsSig, dataSumsCert, identity, issuer, r.conf.Displayer)
216217
if err == nil || !errors.Is(err, cosigncheck.ErrNotInstalled) {
217218
return err
@@ -257,15 +258,19 @@ func buildAssetNames(version string, arch string, stable bool) []string {
257258
return []string{nameBuilder.String(), sumsAssetName, sumsAssetName + ".pem", sumsAssetName + ".sig"}
258259
}
259260

260-
func buildIdentity(v *version.Version, stable bool) string {
261-
if !stable {
262-
return unstableIdentity
261+
func buildIdentity(v *version.Version) string {
262+
segments := v.Segments()
263+
if len(segments) < 3 {
264+
return baseIdentity + v.String()
265+
}
266+
267+
// According to https://opentofu.org/docs/intro/install/standalone/,
268+
// alpha and beta versions have a specific identity.
269+
if strings.Contains(v.Prerelease(), "alpha") || strings.Contains(v.Prerelease(), "beta") {
270+
return mainIdentity
263271
}
264272

265-
cleanedVersion := v.String()
266-
indexDot := strings.LastIndexByte(cleanedVersion, '.')
267-
// cleaned, so indexDot can not be -1
268-
shortVersion := cleanedVersion[:indexDot]
273+
shortVersion := fmt.Sprintf("%d.%d", segments[0], segments[1])
269274

270275
return baseIdentity + shortVersion
271276
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
*
3+
* Copyright 2024 tofuutils authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package tofuretriever
20+
21+
import (
22+
"testing"
23+
24+
"github.com/hashicorp/go-version"
25+
)
26+
27+
func TestBuildIdentity(t *testing.T) {
28+
tests := map[string]struct {
29+
name string
30+
version *version.Version
31+
expected string
32+
}{
33+
"stable version": {
34+
name: "1.11.0",
35+
version: version.Must(version.NewVersion("1.11.0")),
36+
expected: "https://github.com/opentofu/opentofu/.github/workflows/release.yml@refs/heads/v1.11",
37+
},
38+
"unstable alpha version": {
39+
name: "1.7.0-alpha1",
40+
version: version.Must(version.NewVersion("1.7.0-alpha1")),
41+
expected: "https://github.com/opentofu/opentofu/.github/workflows/release.yml@refs/heads/main",
42+
},
43+
"unstable beta version": {
44+
name: "1.7.0-beta1",
45+
version: version.Must(version.NewVersion("1.7.0-beta1")),
46+
expected: "https://github.com/opentofu/opentofu/.github/workflows/release.yml@refs/heads/main",
47+
},
48+
"unstable rc version": {
49+
name: "1.7.0-rc1",
50+
version: version.Must(version.NewVersion("1.7.0-rc1")),
51+
expected: "https://github.com/opentofu/opentofu/.github/workflows/release.yml@refs/heads/v1.7",
52+
},
53+
}
54+
55+
for _, test := range tests {
56+
t.Run(test.name, func(t *testing.T) {
57+
actual := buildIdentity(test.version)
58+
if actual != test.expected {
59+
t.Errorf("expected %s, got %s", test.expected, actual)
60+
}
61+
})
62+
}
63+
}

0 commit comments

Comments
 (0)