Skip to content

Commit eb6488a

Browse files
committed
test: update simulator
Signed-off-by: Marvin Drees <[email protected]>
1 parent b84e48a commit eb6488a

File tree

4 files changed

+115
-50
lines changed

4 files changed

+115
-50
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2024 The Update Framework Authors
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+
// SPDX-License-Identifier: Apache-2.0
16+
//
17+
18+
package simulator
19+
20+
import "github.com/theupdateframework/go-tuf/v2/metadata"
21+
22+
type SimulatorOption interface {
23+
apply(*RepositorySimulator)
24+
}
25+
26+
type DelegatesOption map[string]metadata.Metadata[metadata.TargetsType]
27+
28+
func (o DelegatesOption) apply(s *RepositorySimulator) {
29+
s.MDDelegates = o
30+
}
31+
32+
func WithDelegates(delegates map[string]metadata.Metadata[metadata.TargetsType]) SimulatorOption {
33+
return DelegatesOption(delegates)
34+
}

internal/testutils/simulator/repository_simulator.go

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,12 @@ package simulator
5151

5252
import (
5353
"bytes"
54-
"crypto"
55-
"crypto/ed25519"
5654
"crypto/sha256"
5755
"fmt"
5856
"log/slog"
5957
"net/url"
6058
"os"
6159
"path/filepath"
62-
"regexp"
6360
"strconv"
6461
"strings"
6562
"time"
@@ -161,7 +158,7 @@ func (rs *RepositorySimulator) setupMinimalValidRepository() {
161158
rs.MDRoot = metadata.Root(rs.SafeExpiry)
162159

163160
for _, role := range metadata.TOP_LEVEL_ROLE_NAMES {
164-
publicKey, _, signer := CreateKey()
161+
publicKey, _, signer := createKey()
165162

166163
mtdkey, err := metadata.KeyFromPublicKey(*publicKey)
167164
if err != nil {
@@ -212,20 +209,6 @@ func (rs *RepositorySimulator) AllTargets() <-chan metadata.TargetsType {
212209
return ch
213210
}
214211

215-
func CreateKey() (*ed25519.PublicKey, *ed25519.PrivateKey, *signature.Signer) {
216-
public, private, err := ed25519.GenerateKey(nil)
217-
if err != nil {
218-
slog.Error("Failed to generate key", "err", err)
219-
}
220-
221-
signer, err := signature.LoadSigner(private, crypto.Hash(0))
222-
if err != nil {
223-
slog.Error("failed to load signer", "err", err)
224-
}
225-
226-
return &public, &private, &signer
227-
}
228-
229212
func (rs *RepositorySimulator) AddSigner(role string, keyID string, signer signature.Signer) {
230213
if _, ok := rs.Signers[role]; !ok {
231214
rs.Signers[role] = make(map[string]*signature.Signer)
@@ -241,7 +224,7 @@ func (rs *RepositorySimulator) RotateKeys(role string) {
241224
}
242225

243226
for i := 0; i < rs.MDRoot.Signed.Roles[role].Threshold; i++ {
244-
publicKey, _, signer := CreateKey()
227+
publicKey, _, signer := createKey()
245228
mtdkey, err := metadata.KeyFromPublicKey(*publicKey)
246229
if err != nil {
247230
slog.Error("Repository simulator: key conversion failed while rotating keys", "err", err)
@@ -316,34 +299,6 @@ func (rs *RepositorySimulator) DownloadFile(urlPath string, maxLength int64, _ t
316299
return data, err
317300
}
318301

319-
func IsWindowsPath(path string) bool {
320-
match, _ := regexp.MatchString(`^[a-zA-Z]:\\`, path)
321-
return match
322-
}
323-
324-
func trimPrefix(path string, prefix string) (string, error) {
325-
var toTrim string
326-
if IsWindowsPath(path) {
327-
toTrim = path
328-
} else {
329-
parsedURL, e := url.Parse(path)
330-
if e != nil {
331-
return "", e
332-
}
333-
toTrim = parsedURL.Path
334-
}
335-
336-
return strings.TrimPrefix(toTrim, prefix), nil
337-
}
338-
339-
func hasPrefix(path, prefix string) bool {
340-
return strings.HasPrefix(filepath.ToSlash(path), prefix)
341-
}
342-
343-
func hasSuffix(path, prefix string) bool {
344-
return strings.HasSuffix(filepath.ToSlash(path), prefix)
345-
}
346-
347302
func (rs *RepositorySimulator) fetch(urlPath string) ([]byte, error) {
348303
path, err := trimPrefix(urlPath, rs.LocalDir)
349304
if err != nil {
@@ -552,7 +507,7 @@ func (rs *RepositorySimulator) AddDelegation(delegatorName string, role metadata
552507
delegator.Delegations.Roles = append(delegator.Delegations.Roles, role)
553508

554509
// By default add one new key for the role
555-
publicKey, _, signer := CreateKey()
510+
publicKey, _, signer := createKey()
556511
mdkey, err := metadata.KeyFromPublicKey(*publicKey)
557512
if err != nil {
558513
slog.Error("Repository simulator: key conversion failed while adding delegation", "err", err)
@@ -580,7 +535,7 @@ func (rs *RepositorySimulator) AddSuccinctRoles(delegatorName string, bitLength
580535
slog.Error("Can't add a SuccinctRoles when delegated roles are used")
581536
os.Exit(1)
582537
}
583-
publicKey, _, signer := CreateKey()
538+
publicKey, _, signer := createKey()
584539
mdkey, err := metadata.KeyFromPublicKey(*publicKey)
585540
if err != nil {
586541
slog.Error("Repository simulator: key conversion failed while adding succinct roles", "err", err)

internal/testutils/simulator/repository_simulator_setup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func InitLocalEnv() error {
6464
func InitMetadataDir() (*RepositorySimulator, string, string, error) {
6565
if err := InitLocalEnv(); err != nil {
6666
slog.Error("Failed to initialize environment", "err", err)
67-
os.Exit(1)
67+
return nil, "", "", err
6868
}
6969

7070
metadataDir := filepath.Join(LocalDir, metadataPath)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2024 The Update Framework Authors
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+
// SPDX-License-Identifier: Apache-2.0
16+
//
17+
18+
package simulator
19+
20+
import (
21+
"crypto"
22+
"crypto/ed25519"
23+
"log/slog"
24+
"net/url"
25+
"path/filepath"
26+
"regexp"
27+
"strings"
28+
29+
"github.com/sigstore/sigstore/pkg/signature"
30+
)
31+
32+
// createKey generates a new ed25519 public-private key pair and a signer using the private key.
33+
// It returns pointers to the public key, private key, and the signer.
34+
// If there is an error during the key generation or signer loading, it logs the error and continues.
35+
func createKey() (*ed25519.PublicKey, *ed25519.PrivateKey, *signature.Signer) {
36+
public, private, err := ed25519.GenerateKey(nil)
37+
if err != nil {
38+
slog.Error("Failed to generate key", "err", err)
39+
}
40+
41+
signer, err := signature.LoadSigner(private, crypto.Hash(0))
42+
if err != nil {
43+
slog.Error("Failed to load signer", "err", err)
44+
}
45+
46+
return &public, &private, &signer
47+
}
48+
49+
// trimPrefix is a function that takes a path and a prefix as input.
50+
// It checks if the path starts with a drive letter (e.g., "C:\").
51+
// If it does, it trims the prefix from the path.
52+
// If it doesn't, it parses the path as a URL and trims the prefix from the URL's path.
53+
// The function returns the trimmed path or an error if the path cannot be parsed as a URL.
54+
func trimPrefix(path string, prefix string) (string, error) {
55+
var toTrim string
56+
if match, _ := regexp.MatchString(`^[a-zA-Z]:\\`, path); match {
57+
toTrim = path
58+
} else {
59+
parsedURL, err := url.Parse(path)
60+
if err != nil {
61+
return "", err
62+
}
63+
64+
toTrim = parsedURL.Path
65+
}
66+
67+
return strings.TrimPrefix(toTrim, prefix), nil
68+
}
69+
70+
func hasPrefix(path, prefix string) bool {
71+
return strings.HasPrefix(filepath.ToSlash(path), prefix)
72+
}
73+
74+
func hasSuffix(path, prefix string) bool {
75+
return strings.HasSuffix(filepath.ToSlash(path), prefix)
76+
}

0 commit comments

Comments
 (0)