Commit aac243f7 authored by Sietse Ringers's avatar Sietse Ringers
Browse files

irma subcommands now use default scheme path at /var/www/irma if applicable

parent f3c64f10
package cmd
import (
"fmt"
"net/url"
"path/filepath"
"strings"
......@@ -12,12 +13,35 @@ import (
)
var downloadCmd = &cobra.Command{
Use: "download path url...",
Short: "[Experimental] Download a scheme manager",
Long: `The download command downloads and saves a scheme manager given its URL, saving it in path (i.e., an irma_configuration folder).`,
Args: cobra.ExactArgs(2),
Use: "download [path] [url...]",
Short: "Download scheme(s)",
Long: downloadHelp(),
Run: func(cmd *cobra.Command, args []string) {
if err := downloadSchemeManager(args[0], args[1:]); err != nil {
var path string
var urls []string
defaultIrmaconf := defaultIrmaconfPath()
if len(args) == 0 {
path = defaultIrmaconf
} else {
if err := fs.AssertPathExists(args[0]); err == nil {
path = args[0]
urls = args[1:]
} else {
path = defaultIrmaconf
urls = args
}
}
if path == defaultIrmaconf {
if defaultIrmaconf == "" {
die("Failed to determine default irma_configuration path", nil)
}
if err := fs.EnsureDirectoryExists(defaultIrmaconf); err != nil {
die("Failed to create irma_configuration directory", err)
}
fmt.Println("No irma_configuration path specified, using " + defaultIrmaconf)
}
if err := downloadSchemeManager(path, urls); err != nil {
die("Downloading scheme failed", err)
}
},
......@@ -54,19 +78,36 @@ func downloadSchemeManager(dest string, urls []string) error {
}
conf, err := irma.NewConfiguration(dest)
for _, u := range normalizedUrls {
urlparts := strings.Split(u, "/")
managerName := urlparts[len(urlparts)-1]
manager := irma.NewSchemeManager(managerName)
manager.URL = u
if err := conf.InstallSchemeManager(manager, nil); err != nil {
return err
if len(urls) == 0 {
if err := conf.DownloadDefaultSchemes(); err != nil {
return errors.WrapPrefix(err, "failed to download default schemes", 0)
}
} else {
for _, u := range normalizedUrls {
urlparts := strings.Split(u, "/")
managerName := urlparts[len(urlparts)-1]
manager := irma.NewSchemeManager(managerName)
manager.URL = u
if err := conf.InstallSchemeManager(manager, nil); err != nil {
return err
}
}
}
return nil
}
func downloadHelp() string {
defaultIrmaconf := defaultIrmaconfPath()
str := "The download command downloads and saves scheme managers given their URLs, saving it in path (i.e., an irma_configuration folder).\n\n"
if defaultIrmaconf != "" {
str += "If path is not given, the default path " + defaultIrmaconf + " is used.\n"
}
str += "If no urls are given, the default IRMA schemes are downloaded."
return str
}
func init() {
schemeCmd.AddCommand(downloadCmd)
}
......@@ -16,14 +16,21 @@ import (
// metaCmd represents the meta command
var metaCmd = &cobra.Command{
Use: "meta irma_configuration attribute",
Use: "meta attribute",
Short: "Parse an IRMA metadata attribute and print its contents",
Args: cobra.ExactArgs(2),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
confpath := args[0]
confpath, err := cmd.Flags().GetString("irmaconf")
if err != nil {
die("Failed to get irma_configuration flag", err)
}
metaint := new(big.Int)
_, ok := metaint.SetString(args[1], 10)
_, ok := metaint.SetString(args[0], 10)
if !ok {
// Not a base-10 integer, try to parse as base64. This is safe:
// Since the first byte of a metadata attribute is its version, currently 0x03,
// the first letter of any baase64'd metadata attribute will be 'A'. So it can never happen
// that a base64'd metadata attribute consists of only digits.
bts, err := base64.StdEncoding.DecodeString(args[0])
if err != nil {
return errors.WrapPrefix(err, "Could not parse argument as decimal or base64 integer", 0)
......@@ -90,4 +97,6 @@ func prettyprint(ob interface{}) string {
func init() {
RootCmd.AddCommand(metaCmd)
metaCmd.Flags().StringP("irmaconf", "i", defaultIrmaconfPath(), "path to irma_configuration")
}
package cmd
import (
"path/filepath"
"github.com/privacybydesign/irmago/server"
"github.com/spf13/cobra"
)
// schemeCmd represents the scheme command
var schemeCmd = &cobra.Command{
Use: "scheme",
Short: "IRMA scheme manager tool",
Short: "Manage IRMA schemes",
}
func init() {
RootCmd.AddCommand(schemeCmd)
}
func defaultIrmaconfPath() string {
cachepath, err := server.CachePath()
if err != nil {
return ""
}
return filepath.Join(cachepath, "irma_configuration")
}
......@@ -25,7 +25,7 @@ import (
// signCmd represents the sign command
var signCmd = &cobra.Command{
Use: "sign [privatekey] [path]",
Short: "Sign a scheme manager directory",
Short: "Sign a scheme directory",
Long: `Sign a scheme manager directory, using the specified ECDSA key. Both arguments are optional; "sk.pem" and the working directory are the defaults. Outputs an index file, signature over the index file, and the public key in the specified directory.
Careful: this command could fail and invalidate or destroy your scheme manager directory! Use this only if you can restore it from git or backups.`,
......
package cmd
import (
"io/ioutil"
"path/filepath"
"github.com/go-errors/errors"
......@@ -10,20 +11,34 @@ import (
)
var updateCmd = &cobra.Command{
Use: "update path...",
Short: "[Experimental] Update a scheme manager",
Long: `The update command updates a scheme manager within an irma_configuration folder by comparing its index with the online version, and downloading any new and changed files.
Careful: this command could fail and invalidate or destroy your scheme manager folder! Use this only if you can restore it from git or backups.`,
Use: "update [path...]",
Short: "Update a scheme",
Long: updateHelp(),
Run: func(cmd *cobra.Command, args []string) {
if err := updateSchemeManager(args); err != nil {
die("", err)
var paths []string
irmaconf := defaultIrmaconfPath()
if len(args) != 0 {
paths = args
} else {
if irmaconf == "" {
die("Failed to read default irma_configuration path", nil)
}
files, err := ioutil.ReadDir(irmaconf)
if err != nil {
die("Failed to read default irma_configuration path", err)
}
paths = make([]string, 0, len(files))
for _, file := range files {
if file.IsDir() {
paths = append(paths, filepath.Join(irmaconf, file.Name()))
}
}
}
},
}
func init() {
schemeCmd.AddCommand(updateCmd)
if err := updateSchemeManager(paths); err != nil {
die("Updating schemes failed", err)
}
},
}
func updateSchemeManager(paths []string) error {
......@@ -38,13 +53,11 @@ func updateSchemeManager(paths []string) error {
path, err := filepath.Abs(path)
irmaconf, manager := filepath.Dir(path), filepath.Base(path)
// TODO: this parses all managers within the irma_configuration folder, not just the one specified
// Should make a Configuration constructor that parses just one manager
conf, err := irma.NewConfiguration(irmaconf)
if err != nil {
return err
}
if err := conf.ParseFolder(); err != nil {
if err := conf.ParseSchemeManagerFolder(path, irma.NewSchemeManager(manager)); err != nil {
return err
}
......@@ -55,3 +68,17 @@ func updateSchemeManager(paths []string) error {
return nil
}
func updateHelp() string {
defaultIrmaconf := defaultIrmaconfPath()
str := "The update command updates an IRMA scheme within an irma_configuration folder by comparing its index with the online version, and downloading any new and changed files.\n\n"
if defaultIrmaconf != "" {
str += "If no paths are given, the default schemes at " + defaultIrmaconf + " are updated.\n\n"
}
str += "Careful: this command could fail and invalidate or destroy your scheme manager folder! Use this only if you can restore it from git or backups."
return str
}
func init() {
schemeCmd.AddCommand(updateCmd)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment