Skip to content

Commit

Permalink
import key tests from distributed option
Browse files Browse the repository at this point in the history
  • Loading branch information
sugh01 committed Aug 5, 2024
1 parent df42cd5 commit c33443f
Show file tree
Hide file tree
Showing 7 changed files with 368 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ mocks/
cluster/

.charon/
node*/

keystore*
53 changes: 29 additions & 24 deletions cli/actions/importKeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,23 @@ func (s *sedgeActions) ImportValidatorKeys(options ImportValidatorKeysOptions) e
}
options.GenerationPath = absGenerationPath

if options.Distributed {
cwd, err := os.Getwd()
if err != nil {
fmt.Println("Error:", err)
return err
if !isDefaultKeysPath(options.GenerationPath, options.From) {
if !options.Distributed {
defaultKeystorePath := filepath.Join(options.GenerationPath, "keystore")
log.Warnf("The keys path is not the default one, copying the keys to the default path %s", defaultKeystorePath)
copy.Copy(options.From, defaultKeystorePath)
}
}

if options.Distributed {
cwd, _ := os.Getwd()
charonPath := filepath.Join(cwd, ".charon")

if !isDefaultKeysPath(options.GenerationPath, options.From) {
charonPath = options.From
log.Infof("Copying the keys from %s", charonPath)
options.From = filepath.Join(options.GenerationPath, "keystore")
}
charonValidatorKeysPath := filepath.Join(charonPath, "validator_keys")
defaultKeystorePath := filepath.Join(configs.DefaultAbsSedgeDataPath, "keystore")
log.Infof("Copying the keys to the default path %s", defaultKeystorePath)
Expand Down Expand Up @@ -142,18 +152,6 @@ func (s *sedgeActions) ImportValidatorKeys(options ImportValidatorKeysOptions) e
return err
}
}
// Copy import scripts
if options.ValidatorClient == "lodestar" {
importScriptsPath := filepath.Join(cwd, "/scripts/charon/import_lodestar_keys.sh")
importScriptsPathDest := filepath.Join(defaultKeystorePath, "import_lodestar_keys.sh")
if err := copy.Copy(importScriptsPath, importScriptsPathDest); err != nil {
return err
}
// modify permissions
if err := os.Chmod(importScriptsPathDest, 0755); err != nil {
return err
}
}
if options.ValidatorClient == "prysm" {
keystorePasswordPath := filepath.Join(defaultKeystorePath, "keystore_password.txt")
f, err := os.Create(keystorePasswordPath)
Expand All @@ -165,12 +163,6 @@ func (s *sedgeActions) ImportValidatorKeys(options ImportValidatorKeysOptions) e
}
}

if !isDefaultKeysPath(options.GenerationPath, options.From) {
defaultKeystorePath := filepath.Join(options.GenerationPath, "keystore")
log.Warnf("The keys path is not the default one, copying the keys to the default path %s", defaultKeystorePath)
copy.Copy(options.From, defaultKeystorePath)
}

var ctID string
switch options.ValidatorClient {
case "prysm":
Expand Down Expand Up @@ -335,7 +327,20 @@ func setupLodestarValidatorImport(dockerClient client.APIClient, serviceManager
containerConfig = &container.Config{
Image: validatorImage,
Entrypoint: []string{
"/keystore/import_lodestar_keys.sh",
"sh", "-c", `
#!/bin/sh
set -e
for f in /keystore/validator_keys/keystore-*.json; do
echo "Importing key ${f}"
pwdfile="/keystore/$(basename "$f" .json).txt"
echo "Using password file ${pwdfile}"
# Import keystore with password.
node /usr/app/packages/cli/bin/lodestar validator import \
--dataDir="/data" \
--importKeystores="$f" \
--importKeystoresPassword="${pwdfile}"
done
`,
},
}
}
Expand Down
76 changes: 72 additions & 4 deletions cli/actions/importKeys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestImportKeys_ValidatorRunning(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

dockerClient := importKeysGoldenPath(t, ctrl, false)
dockerClient := importKeysGoldenPath(t, ctrl, false, false)
serviceManager := services.NewServiceManager(dockerClient)
cmdRunner := test.SimpleCMDRunner{}
s := actions.NewSedgeActions(actions.SedgeActionsOptions{
Expand Down Expand Up @@ -113,7 +113,7 @@ func TestImportKeysCustom_ValidatorRunning(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

dockerClient := importKeysGoldenPath(t, ctrl, true)
dockerClient := importKeysGoldenPath(t, ctrl, true, false)
serviceManager := services.NewServiceManager(dockerClient)
cmdRunner := test.SimpleCMDRunner{}
s := actions.NewSedgeActions(actions.SedgeActionsOptions{
Expand Down Expand Up @@ -197,7 +197,7 @@ func TestImportKeys_CustomOptions(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

dockerClient := importKeysGoldenPath(t, ctrl, tt.customImage)
dockerClient := importKeysGoldenPath(t, ctrl, tt.customImage, false)
serviceManager := services.NewServiceManager(dockerClient)
cmdRunner := test.SimpleCMDRunner{}
s := actions.NewSedgeActions(actions.SedgeActionsOptions{
Expand Down Expand Up @@ -280,6 +280,36 @@ func TestImportKeys_UnexpectedExitCode(t *testing.T) {
assert.ErrorIs(t, err, actions.ErrValidatorImportCtBadExitCode)
}

func TestImportKeys_DistributedMode(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

dockerClient := importKeysGoldenPath(t, ctrl, false, true)
serviceManager := services.NewServiceManager(dockerClient)
cmdRunner := test.SimpleCMDRunner{}
s := actions.NewSedgeActions(actions.SedgeActionsOptions{
DockerClient: dockerClient,
ServiceManager: serviceManager,
CommandRunner: &cmdRunner,
})

from, err := setupCharonKeystoreDir(t)
if err != nil {
t.Fatal(err)
}

generationPath := t.TempDir()

err = s.ImportValidatorKeys(actions.ImportValidatorKeysOptions{
ValidatorClient: "prysm",
Network: "holesky",
GenerationPath: generationPath,
Distributed: true,
From: from,
})
assert.NoError(t, err)
}

//go:embed testdata/keystore
var keystoreTestData embed.FS

Expand Down Expand Up @@ -316,9 +346,45 @@ func setupKeystoreDir(t *testing.T) (string, error) {
return tempKeystore, nil
}

//go:embed testdata/charon
var charonKeystoreTestData embed.FS

func setupCharonKeystoreDir(t *testing.T) (string, error) {
t.Helper()
tempKeystore := t.TempDir()

baseTestDir := "testdata/charon"
dirs := []string{""}
for len(dirs) > 0 {
currentDir := dirs[0]
dirEntries, err := charonKeystoreTestData.ReadDir(path.Join(baseTestDir, currentDir))
if err != nil {
return "", err
}
for _, entry := range dirEntries {
if entry.IsDir() {
dirs = append(dirs, filepath.Join(currentDir, entry.Name()))
} else {
entryData, err := charonKeystoreTestData.ReadFile(path.Join(baseTestDir, currentDir, entry.Name()))
if err != nil {
return "", err
}
if err := os.MkdirAll(filepath.Join(tempKeystore, currentDir), 0o755); err != nil {
return "", err
}
if err := ioutil.WriteFile(filepath.Join(tempKeystore, currentDir, entry.Name()), entryData, 0o755); err != nil {
return "", err
}
}
}
dirs = dirs[1:]
}
return tempKeystore, nil
}

// importKeysGoldenPath returns a mocked docker client interface with all the
// required responses for a correct validator import keys container execution.
func importKeysGoldenPath(t *testing.T, ctrl *gomock.Controller, withCustomImage bool) client.APIClient {
func importKeysGoldenPath(t *testing.T, ctrl *gomock.Controller, withCustomImage bool, withDistributedOption bool) client.APIClient {
t.Helper()
dockerClient := sedge_mocks.NewMockAPIClient(ctrl)

Expand Down Expand Up @@ -350,6 +416,8 @@ func importKeysGoldenPath(t *testing.T, ctrl *gomock.Controller, withCustomImage
}, nil)
if withCustomImage {
inspectCall.Times(2)
} else if withDistributedOption {
inspectCall.MinTimes(2)
} else {
inspectCall.Times(3)
}
Expand Down
1 change: 1 addition & 0 deletions cli/actions/testdata/charon/charon-enr-private-key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
09504dd3fea5458aafe25e52b60d2c8db170e58ffe12d0976f5e50d405a7ec42
Loading

0 comments on commit c33443f

Please sign in to comment.