|
| 1 | +package kmip |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/ovh/kmip-go" |
| 8 | + "github.com/ovh/kmip-go/kmipclient" |
| 9 | + "github.com/ovh/okms-cli/common/flagsmgmt" |
| 10 | + "github.com/ovh/okms-cli/common/flagsmgmt/kmipflags" |
| 11 | + "github.com/ovh/okms-cli/common/output" |
| 12 | + "github.com/ovh/okms-cli/common/utils/exit" |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +func createCommand() *cobra.Command { |
| 17 | + cmd := &cobra.Command{ |
| 18 | + Use: "create", |
| 19 | + Short: "Create kmip keys", |
| 20 | + } |
| 21 | + cmd.AddCommand( |
| 22 | + createSymmetricKey(), |
| 23 | + createKeyPair(), |
| 24 | + ) |
| 25 | + return cmd |
| 26 | +} |
| 27 | + |
| 28 | +func createSymmetricKey() *cobra.Command { |
| 29 | + cmd := &cobra.Command{ |
| 30 | + Use: "symmetric", |
| 31 | + Aliases: []string{"sym"}, |
| 32 | + Short: "Create KMIP symmetric key", |
| 33 | + Args: cobra.NoArgs, |
| 34 | + } |
| 35 | + |
| 36 | + var alg kmipflags.SymmetricAlg |
| 37 | + usage := kmipflags.KeyUsageList{kmipflags.ENCRYPT, kmipflags.DECRYPT} |
| 38 | + |
| 39 | + cmd.Flags().Var(&alg, "alg", "Key algorithm") |
| 40 | + size := cmd.Flags().Int("size", 0, "Key bit length") |
| 41 | + cmd.Flags().Var(&usage, "usage", "Cryptographic usage") |
| 42 | + name := cmd.Flags().String("name", "", "Optional key name") |
| 43 | + |
| 44 | + sensitive := cmd.Flags().Bool("sensitive", false, "Set sensitive attribute") |
| 45 | + extractable := cmd.Flags().Bool("extractable", true, "Set the extractable attribute") |
| 46 | + description := cmd.Flags().String("description", "", "Set the description attribute") |
| 47 | + comment := cmd.Flags().String("comment", "", "Set the comment attribute") |
| 48 | + |
| 49 | + _ = cmd.MarkFlagRequired("alg") |
| 50 | + _ = cmd.MarkFlagRequired("size") |
| 51 | + |
| 52 | + cmd.Run = func(cmd *cobra.Command, args []string) { |
| 53 | + req := kmipClient.Create(). |
| 54 | + SymmetricKey(kmip.CryptographicAlgorithm(alg), *size, usage.ToCryptographicUsageMask()). |
| 55 | + WithAttribute(kmip.AttributeNameExtractable, *extractable). |
| 56 | + WithAttribute(kmip.AttributeNameSensitive, *sensitive) |
| 57 | + if *name != "" { |
| 58 | + req = req.WithName(*name) |
| 59 | + } |
| 60 | + if *description != "" { |
| 61 | + req = req.WithAttribute(kmip.AttributeNameDescription, *description) |
| 62 | + } |
| 63 | + if *comment != "" { |
| 64 | + req = req.WithAttribute(kmip.AttributeNameComment, *comment) |
| 65 | + } |
| 66 | + |
| 67 | + resp := exit.OnErr2(req.ExecContext(cmd.Context())) |
| 68 | + |
| 69 | + if cmd.Flag("output").Value.String() == string(flagsmgmt.JSON_OUTPUT_FORMAT) { |
| 70 | + output.JsonPrint(resp) |
| 71 | + } else { |
| 72 | + fmt.Println("Key created with ID", resp.UniqueIdentifier) |
| 73 | + // Print returned attributes if any |
| 74 | + if resp.Attributes != nil && len(resp.Attributes.Attribute) > 0 { |
| 75 | + printAttributeTable(resp.Attributes.Attribute) |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return cmd |
| 81 | +} |
| 82 | + |
| 83 | +func createKeyPair() *cobra.Command { |
| 84 | + cmd := &cobra.Command{ |
| 85 | + Use: "key-pair", |
| 86 | + Short: "Create an asymmetric key-pair", |
| 87 | + Args: cobra.NoArgs, |
| 88 | + } |
| 89 | + |
| 90 | + var alg kmipflags.AsymmetricAlg |
| 91 | + cmd.Flags().Var(&alg, "alg", "Key-pair algorithm") |
| 92 | + size := cmd.Flags().Int("size", 0, "Modulus bit length of the RSA key-pair to generate") |
| 93 | + var curve kmipflags.EcCurve |
| 94 | + cmd.Flags().Var(&curve, "curve", "Elliptic curve for EC keys") |
| 95 | + privateUsage := kmipflags.KeyUsageList{kmipflags.SIGN} |
| 96 | + publicUsage := kmipflags.KeyUsageList{kmipflags.VERIFY} |
| 97 | + cmd.Flags().Var(&privateUsage, "private-usage", "Private key allowed usage") |
| 98 | + cmd.Flags().Var(&publicUsage, "public-usage", "Public key allowed usage") |
| 99 | + privateName := cmd.Flags().String("private-name", "", "Optional private key name") |
| 100 | + publicName := cmd.Flags().String("public-name", "", "Optional public key name") |
| 101 | + |
| 102 | + privateSensitive := cmd.Flags().Bool("private-sensitive", false, "Set sensitive attribute on the private key") |
| 103 | + privateExtractable := cmd.Flags().Bool("private-extractable", true, "Set the extractable attribute on the private key") |
| 104 | + description := cmd.Flags().String("description", "", "Set the description attribute on both keys") |
| 105 | + comment := cmd.Flags().String("comment", "", "Set the comment attribute on both keys") |
| 106 | + |
| 107 | + _ = cmd.MarkFlagRequired("alg") |
| 108 | + cmd.MarkFlagsMutuallyExclusive("curve", "size") |
| 109 | + |
| 110 | + cmd.Run = func(cmd *cobra.Command, args []string) { |
| 111 | + var req kmipclient.ExecCreateKeyPairAttr |
| 112 | + switch alg { |
| 113 | + case kmipflags.RSA: |
| 114 | + if *size == 0 { |
| 115 | + exit.OnErr(errors.New("Missing --size flag")) |
| 116 | + } |
| 117 | + req = kmipClient.CreateKeyPair().RSA(*size, privateUsage.ToCryptographicUsageMask(), publicUsage.ToCryptographicUsageMask()) |
| 118 | + case kmipflags.ECDSA: |
| 119 | + if curve == 0 { |
| 120 | + exit.OnErr(errors.New("Missing --curve flag")) |
| 121 | + } |
| 122 | + req = kmipClient.CreateKeyPair().ECDSA(kmip.RecommendedCurve(curve), privateUsage.ToCryptographicUsageMask(), publicUsage.ToCryptographicUsageMask()) |
| 123 | + } |
| 124 | + if *privateName != "" { |
| 125 | + req = req.PrivateKey().WithName(*privateName) |
| 126 | + } |
| 127 | + if *publicName != "" { |
| 128 | + req = req.PublicKey().WithName(*publicName) |
| 129 | + } |
| 130 | + req = req.PrivateKey(). |
| 131 | + WithAttribute(kmip.AttributeNameExtractable, *privateExtractable). |
| 132 | + WithAttribute(kmip.AttributeNameSensitive, *privateSensitive) |
| 133 | + if *description != "" { |
| 134 | + req = req.Common().WithAttribute(kmip.AttributeNameDescription, *description) |
| 135 | + } |
| 136 | + if *comment != "" { |
| 137 | + req = req.Common().WithAttribute(kmip.AttributeNameComment, *comment) |
| 138 | + } |
| 139 | + resp := exit.OnErr2(req.ExecContext(cmd.Context())) |
| 140 | + |
| 141 | + if cmd.Flag("output").Value.String() == string(flagsmgmt.JSON_OUTPUT_FORMAT) { |
| 142 | + output.JsonPrint(resp) |
| 143 | + } else { |
| 144 | + fmt.Println("Pubic Key ID:", resp.PublicKeyUniqueIdentifier) |
| 145 | + fmt.Println("Private Key ID:", resp.PrivateKeyUniqueIdentifier) |
| 146 | + // Print returned attributes if any |
| 147 | + if attrs := resp.PublicKeyTemplateAttribute; attrs != nil && len(attrs.Attribute) > 0 { |
| 148 | + fmt.Println("Public Key Attributes:") |
| 149 | + printAttributeTable(attrs.Attribute) |
| 150 | + } |
| 151 | + if attrs := resp.PrivateKeyTemplateAttribute; attrs != nil && len(attrs.Attribute) > 0 { |
| 152 | + fmt.Println("Private Key Attributes:") |
| 153 | + printAttributeTable(attrs.Attribute) |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return cmd |
| 159 | +} |
0 commit comments