Skip to content

Commit

Permalink
Bugfix: import-le command was broken
Browse files Browse the repository at this point in the history
This was caused by the refactoring to the storage module which changed
ImportKey to take a crypto.PrivateKey. But since this is an interface{},
it in practice accepts anything, so type checking didn't catch that
the import-le call to ImportKey needed to be updated. This caused
acmetool to panic when import-le was used.

The import-le command has also been enhanced so that it sets the
default provider URL if one is not set and there is only one directory
URL used in the imported state directory.

Fixes #86
  • Loading branch information
hlandau committed Jan 25, 2016
1 parent 065f55c commit 7de5c8e
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions cmd/acmetool/le-import.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ func cmdImportLE() {
// - import the certificates

// Import account keys.
durls := map[string]struct{}{}

for _, accountName := range accountNames {
err := importLEAccount(s, lePath, accountName)
acct, err := importLEAccount(s, lePath, accountName)
log.Fatale(err, "import account")

durls[acct.DirectoryURL] = struct{}{}
}

keyFiles, err := filepath.Glob(filepath.Join(lePath, "keys", "*.pem"))
Expand All @@ -52,46 +56,61 @@ func cmdImportLE() {
err := importCert(s, certFile)
log.Fatale(err, "import certificate")
}

// If there is no default provider set, and we have only one directory URL
// imported, set it as the default provider.
if len(durls) == 1 && s.DefaultTarget().Request.Provider == "" {
for p := range durls {
s.DefaultTarget().Request.Provider = p
err := s.SaveTarget(s.DefaultTarget())
log.Fatale(err, "couldn't set default provider")
break
}
}
}

var knownProviderURLs = map[string]struct{}{}

func importLEAccount(s storage.Store, lePath, accountName string) error {
func importLEAccount(s storage.Store, lePath, accountName string) (*storage.Account, error) {
providerURL, err := getProviderURLFromAccountName(accountName)
if err != nil {
return err
return nil, err
}

knownProviderURLs[providerURL] = struct{}{}

pkPath := filepath.Join(lePath, "accounts", accountName, "private_key.json")
b, err := ioutil.ReadFile(pkPath)
if err != nil {
return err
return nil, err
}

k := jose.JsonWebKey{}
err = k.UnmarshalJSON(b)
if err != nil {
return err
return nil, err
}

_, err = s.ImportAccount(providerURL, k.Key)
acct, err := s.ImportAccount(providerURL, k.Key)
if err != nil {
return err
return nil, err
}

return nil
return acct, nil
}

func importKey(s storage.Store, filename string) error {
f, err := os.Open(filename)
b, err := ioutil.ReadFile(filename)
if err != nil {
return err
}

pk, err := acmeutils.LoadPrivateKey(b)
if err != nil {
return err
}
defer f.Close()

_, err = s.ImportKey(f)
_, err = s.ImportKey(pk)
return err
}

Expand Down

0 comments on commit 7de5c8e

Please sign in to comment.