-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: WaitForDeployment works with deployment
- Loading branch information
Showing
3 changed files
with
162 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package framework | ||
|
||
import ( | ||
"fmt" | ||
"github.com/sigstore/cosign/v2/cmd/cosign/cli" | ||
"os" | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
// cleanupKeys removes all keypair files from the testing directory | ||
func cleanupKeys(t testing.TB) { | ||
|
||
t.Logf("cleaning up keypair files") | ||
files, err := os.ReadDir(".") | ||
if err != nil { | ||
t.Fatalf("failed reading directory: %v", err) | ||
} | ||
for _, f := range files { | ||
if f.IsDir() { | ||
continue | ||
} | ||
reKey := regexp.MustCompile(".*.key") | ||
rePub := regexp.MustCompile(".*.pub") | ||
if reKey.MatchString(f.Name()) || rePub.MatchString(f.Name()) { | ||
err = os.Remove(f.Name()) | ||
if err != nil { | ||
t.Fatalf("failed removing file %s: %v", f.Name(), err) | ||
} | ||
} | ||
} | ||
t.Logf("cleaned up keypair files") | ||
} | ||
|
||
// CreateKeys creates a signing keypair for cosing with the provided name | ||
func (f *Framework) CreateKeys(t testing.TB, name string) (string, string) { | ||
args := []string{fmt.Sprintf("--output-key-prefix=%s", name)} | ||
err := os.Setenv("COSIGN_PASSWORD", "") | ||
if err != nil { | ||
t.Fatalf("failed setting COSIGN_PASSWORD: %v", err) | ||
} | ||
cmd := cli.GenerateKeyPair() | ||
cmd.SetArgs(args) | ||
err = cmd.Execute() | ||
if err != nil { | ||
f.Cleanup(t, err) | ||
} | ||
|
||
// read private key and public key from the current directory | ||
privateKey, err := os.ReadFile(fmt.Sprintf("%s.key", name)) | ||
if err != nil { | ||
f.Cleanup(t, err) | ||
} | ||
pubKey, err := os.ReadFile(fmt.Sprintf("%s.pub", name)) | ||
if err != nil { | ||
f.Cleanup(t, err) | ||
} | ||
|
||
return string(privateKey), string(pubKey) | ||
} | ||
|
||
// SignContainer signs the container with the provided private key | ||
func (f *Framework) SignContainer(t *testing.T, priv, img string) { | ||
// TODO: find a way to simplify this function - maybe use cosing CLI directly? | ||
// get SHA of the container image | ||
t.Setenv("COSIGN_PASSWORD", "") | ||
args := []string{ | ||
"sign", | ||
img, | ||
} | ||
t.Setenv("COSIGN_PASSWORD", "") | ||
cmd := cli.New() | ||
_ = cmd.Flags().Set("timeout", "30s") | ||
cmd.SetArgs(args) | ||
|
||
// find the sign subcommand in the commands slice | ||
for _, c := range cmd.Commands() { | ||
if c.Name() == "sign" { | ||
cmd = c | ||
break | ||
} | ||
} | ||
_ = cmd.Flags().Set("key", fmt.Sprintf("%s.key", priv)) | ||
_ = cmd.Flags().Set("tlog-upload", "false") | ||
_ = cmd.Flags().Set("yes", "true") | ||
_ = cmd.Flags().Set("allow-http-registry", "true") | ||
err := cmd.Execute() | ||
if err != nil { | ||
f.Cleanup(t, err) | ||
} | ||
} |
Oops, something went wrong.