Skip to content

Commit

Permalink
typo(cmd/mirror): add warning if key already exists (#1053)
Browse files Browse the repository at this point in the history
* typo(cmd/mirrow): add warning if key already exists

* feat(repository): return public key path if save

* feat(cmd/mirror): print public key path if --save

feat(cmd/mirror): use log.Warn to print warning
  • Loading branch information
9547 authored Jan 14, 2021
1 parent fdc32c5 commit bc366b8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
12 changes: 7 additions & 5 deletions cmd/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/pingcap/tiup/pkg/cluster/spec"
"github.com/pingcap/tiup/pkg/environment"
"github.com/pingcap/tiup/pkg/localdata"
"github.com/pingcap/tiup/pkg/logger/log"
"github.com/pingcap/tiup/pkg/repository"
"github.com/pingcap/tiup/pkg/repository/model"
ru "github.com/pingcap/tiup/pkg/repository/utils"
Expand Down Expand Up @@ -153,7 +154,7 @@ func newMirrorSetCmd() *cobra.Command {
addr := args[0]
profile := environment.GlobalEnv().Profile()
if err := profile.ResetMirror(addr, root); err != nil {
fmt.Printf("Failed to set mirror: %s\n", err.Error())
log.Errorf("Failed to set mirror: %s\n", err.Error())
return err
}
fmt.Printf("Set mirror to %s success\n", addr)
Expand Down Expand Up @@ -548,7 +549,7 @@ func newMirrorGenkeyCmd() *cobra.Command {
fmt.Printf("KeyID: %s\nKeyContent: \n%s\n", id, string(content))
} else {
if utils.IsExist(privPath) {
fmt.Println("Key already exists, skipped")
log.Warnf("Warning: private key already exists(%s), skip", privPath)
return nil
}

Expand All @@ -572,18 +573,19 @@ func newMirrorGenkeyCmd() *cobra.Command {
return err
}

fmt.Printf("private key have been write to %s\n", privPath)
fmt.Printf("Private key has been writeen to %s\n", privPath)
}

if saveKey {
pubKey, err := ki.Public()
if err != nil {
return err
}
if err = v1manifest.SaveKeyInfo(pubKey, "public", ""); err != nil {
pubPath, err := v1manifest.SaveKeyInfo(pubKey, "public", "")
if err != nil {
return err
}
fmt.Printf("public key have been write to current working dir\n")
fmt.Printf("Public key has been written to current working dir: %s\n", pubPath)
}
return nil
},
Expand Down
6 changes: 3 additions & 3 deletions pkg/repository/clone_mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func CloneMirror(repo *V1Repository,
return errors.Trace(err)
}
// save owner key
if err := v1manifest.SaveKeyInfo(ownerkeyInfo, "pingcap", keyDir); err != nil {
if _, err := v1manifest.SaveKeyInfo(ownerkeyInfo, "pingcap", keyDir); err != nil {
return errors.Trace(err)
}

Expand Down Expand Up @@ -304,8 +304,8 @@ func cloneComponents(repo *V1Repository,
continue
}
}
if _, err := repo.FetchComponentManifest(name, false); err != nil || versionItem.Yanked {
// The component or the version is yanked, skip download binary
if _, err := repo.FetchComponentManifest(name, false); err != nil || versionItem.Yanked {
// The component or the version is yanked, skip download binary
continue
}
if err := download(targetDir, tmpDir, repo, &versionItem); err != nil {
Expand Down
19 changes: 10 additions & 9 deletions pkg/repository/v1manifest/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,38 +107,39 @@ func Init(dst, keyDir string, initTime time.Time) (err error) {
}

// SaveKeyInfo saves a KeyInfo object to a JSON file
func SaveKeyInfo(key *KeyInfo, ty, dir string) error {
func SaveKeyInfo(key *KeyInfo, ty, dir string) (string, error) {
id, err := key.ID()
if err != nil {
return err
return "", err
}

if dir == "" {
dir, err = os.Getwd()
if err != nil {
return err
return "", err
}
}
if utils.IsNotExist(dir) {
if err := os.MkdirAll(dir, 0755); err != nil {
return errors.Annotate(err, "create key directory")
return "", errors.Annotate(err, "create key directory")
}
}

f, err := os.Create(path.Join(dir, fmt.Sprintf("%s-%s.json", id[:ShortKeyIDLength], ty)))
pubPath := path.Join(dir, fmt.Sprintf("%s-%s.json", id[:ShortKeyIDLength], ty))
f, err := os.Create(pubPath)
if err != nil {
return err
return pubPath, err
}
defer f.Close()

if _, found := key.Value["private"]; found {
err = f.Chmod(0600)
if err != nil {
return err
return pubPath, err
}
}

return json.NewEncoder(f).Encode(key)
return pubPath, json.NewEncoder(f).Encode(key)
}

// GenAndSaveKeys generate private keys to keys param and save key file to dir
Expand All @@ -150,7 +151,7 @@ func GenAndSaveKeys(keys map[string][]*KeyInfo, ty string, num int, dir string)
}
keys[ty] = append(keys[ty], k)

if err := SaveKeyInfo(k, ty, dir); err != nil {
if _, err := SaveKeyInfo(k, ty, dir); err != nil {
return err
}
}
Expand Down

0 comments on commit bc366b8

Please sign in to comment.