github.com/hpcng/singularity@v3.1.1+incompatible/cmd/internal/cli/key_push.go (about) 1 // Copyright (c) 2017-2019, Sylabs Inc. All rights reserved. 2 // This software is licensed under a 3-clause BSD license. Please consult the 3 // LICENSE.md file distributed with the sources of this project regarding your 4 // rights to use or distribute this software. 5 6 package cli 7 8 import ( 9 "fmt" 10 "os" 11 "strconv" 12 13 "github.com/spf13/cobra" 14 "github.com/sylabs/singularity/docs" 15 "github.com/sylabs/singularity/internal/pkg/sylog" 16 "github.com/sylabs/singularity/pkg/sypgp" 17 ) 18 19 func init() { 20 KeyPushCmd.Flags().SetInterspersed(false) 21 22 KeyPushCmd.Flags().StringVarP(&keyServerURL, "url", "u", defaultKeyServer, "specify the key server URL") 23 KeyPushCmd.Flags().SetAnnotation("url", "envkey", []string{"URL"}) 24 } 25 26 // KeyPushCmd is `singularity key list' and lists local store OpenPGP keys 27 var KeyPushCmd = &cobra.Command{ 28 Args: cobra.ExactArgs(1), 29 DisableFlagsInUseLine: true, 30 PreRun: sylabsToken, 31 Run: func(cmd *cobra.Command, args []string) { 32 if err := doKeyPushCmd(args[0], keyServerURL); err != nil { 33 sylog.Errorf("push failed: %s", err) 34 os.Exit(2) 35 } 36 }, 37 38 Use: docs.KeyPushUse, 39 Short: docs.KeyPushShort, 40 Long: docs.KeyPushLong, 41 Example: docs.KeyPushExample, 42 } 43 44 func doKeyPushCmd(fingerprint string, url string) error { 45 el, err := sypgp.LoadPubKeyring() 46 if err != nil { 47 return err 48 } 49 if el == nil { 50 return fmt.Errorf("no public keys in local store to choose from") 51 } 52 53 if len(fingerprint) != 16 && len(fingerprint) != 40 { 54 return fmt.Errorf("please provide a keyid(16 chars) or a full fingerprint(40 chars)") 55 } 56 57 keyID, err := strconv.ParseUint(fingerprint[len(fingerprint)-16:], 16, 64) 58 if err != nil { 59 return fmt.Errorf("please provide a keyid(16 chars) or a full fingerprint(40 chars): %s", err) 60 } 61 62 keys := el.KeysById(keyID) 63 if len(keys) != 1 { 64 return fmt.Errorf("could not find the requested key") 65 } 66 entity := keys[0].Entity 67 68 if err = sypgp.PushPubkey(entity, url, authToken); err != nil { 69 return err 70 } 71 72 fmt.Printf("public key `%v' pushed to server successfully\n", fingerprint) 73 74 return nil 75 }