Skip to content
Merged
3 changes: 1 addition & 2 deletions cmd/base64.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ var base64Cmd = &cobra.Command{
if err != nil {
log.Fatal(err)
}

base64String, err := base64.Process(inputData, formatType)
if err != nil {
log.Fatal(err)
Expand All @@ -49,7 +48,6 @@ var base64Cmd = &cobra.Command{
// init - cobra init function
func init() {
rootCmd.AddCommand(base64Cmd)

requiredFlags := map[string]bool{
"in": true,
}
Expand All @@ -58,4 +56,5 @@ func init() {
base64Cmd.PersistentFlags().String(base64.FormatFlagName, base64.TextFormat, base64.FormatFlagDescription)
base64Cmd.PersistentFlags().String(base64.OutputFlagName, "", base64.OutputFlagDescription)
common.SetCustomHelpTemplate(base64Cmd, requiredFlags)
common.SetCustomErrorTemplate(base64Cmd)
}
1 change: 1 addition & 0 deletions cmd/base64Tgz.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ func init() {
base64TgzCmd.PersistentFlags().String(base64Tgz.CertFlagName, "", base64Tgz.CertPathDescription)
base64TgzCmd.PersistentFlags().String(base64Tgz.OutputFlagName, "", base64Tgz.OutputPathDescription)
common.SetCustomHelpTemplate(base64TgzCmd, requiredFlags)
common.SetCustomErrorTemplate(base64TgzCmd)
}
3 changes: 2 additions & 1 deletion cmd/decryptAttestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ func init() {
"in": true,
"priv": true,
}
decryptAttestationCmd.PersistentFlags().String(decryptAttestation.InputFlagName, decryptAttestation.DecryptAttestFileInDefaultPath, decryptAttestation.DecryptAttestFileInDescription)
decryptAttestationCmd.PersistentFlags().String(decryptAttestation.InputFlagName, "", decryptAttestation.DecryptAttestFileInDescription)
decryptAttestationCmd.PersistentFlags().String(decryptAttestation.PrivateKeyFlagName, "", decryptAttestation.PrivateKeyFlagDescription)
decryptAttestationCmd.PersistentFlags().String(decryptAttestation.OutputFlagName, "", decryptAttestation.DecryptAttestFlagDescription)
common.SetCustomHelpTemplate(decryptAttestationCmd, requiredFlags)
common.SetCustomErrorTemplate(decryptAttestationCmd)
}
3 changes: 2 additions & 1 deletion cmd/downloadCertificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var (
Short: downloadCertificate.ParameterShortDescription,
Long: downloadCertificate.ParameterLongDescription,
Run: func(cmd *cobra.Command, args []string) {
formatType, certificatePath, err := downloadCertificate.ValidateInput(cmd)
formatType, certificatePath, err := downloadCertificate.ValidateInput(cmd, versions)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -62,4 +62,5 @@ func init() {
downloadCertificatesCmd.PersistentFlags().String(downloadCertificate.FormatFlag, downloadCertificate.JsonFormat, downloadCertificate.DataFormatFlag)
downloadCertificatesCmd.PersistentFlags().String(downloadCertificate.OutputFlagName, "", downloadCertificate.OutputPathDescription)
common.SetCustomHelpTemplate(downloadCertificatesCmd, requiredFlags)
common.SetCustomErrorTemplate(downloadCertificatesCmd)
}
1 change: 1 addition & 0 deletions cmd/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ func init() {
encryptCmd.PersistentFlags().String(encrypt.CsrFlag, "", encrypt.CsrFlagDescription)
encryptCmd.PersistentFlags().Int(encrypt.ExpiryDaysFlag, 0, encrypt.ExpiryDaysFlagDescription)
common.SetCustomHelpTemplate(encryptCmd, requiredFlags)
common.SetCustomErrorTemplate(encryptCmd)
}
1 change: 1 addition & 0 deletions cmd/encryptString.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ func init() {
encryptStringCmd.PersistentFlags().String(encryptString.CertFlagName, "", encryptString.CertFlagDescription)
encryptStringCmd.PersistentFlags().String(encryptString.OutputFlagName, "", encryptString.OutputFlagDescription)
common.SetCustomHelpTemplate(encryptStringCmd, requiredFlags)
common.SetCustomErrorTemplate(encryptStringCmd)
}
1 change: 1 addition & 0 deletions cmd/getCertificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ func init() {
getCertificateCmd.PersistentFlags().String(getCertificate.VersionFlagName, "", getCertificate.VersionFlagDescription)
getCertificateCmd.PersistentFlags().String(getCertificate.OutputFlagName, "", getCertificate.FileOutFlagDescription)
common.SetCustomHelpTemplate(getCertificateCmd, requiredFlags)
common.SetCustomErrorTemplate(getCertificateCmd)
}
1 change: 1 addition & 0 deletions cmd/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ func init() {
imageCmd.PersistentFlags().String(image.FormatFlag, image.JsonFormat, image.DataFormatFlagDescription)
imageCmd.PersistentFlags().String(image.OutputFlagName, "", image.OutputFlagDescription)
common.SetCustomHelpTemplate(imageCmd, requiredFlags)
common.SetCustomErrorTemplate(imageCmd)
}
1 change: 1 addition & 0 deletions cmd/validateContract.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ func init() {
validateContractCmd.PersistentFlags().String(validateContract.InputFlagName, "", validateContract.InputFlagDescription)
validateContractCmd.PersistentFlags().String(validateContract.OsVersionFlagName, "", validateContract.OsVersionFlagDescription)
common.SetCustomHelpTemplate(validateContractCmd, requiredFlags)
common.SetCustomErrorTemplate(validateContractCmd)
}
1 change: 1 addition & 0 deletions cmd/validateEncryptionCertificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ func init() {
}
validateEncryptionCertificateCmd.PersistentFlags().String(validateEncryptionCertificate.InputFlagName, "", validateEncryptionCertificate.CertVersionFlagDescription)
common.SetCustomHelpTemplate(validateEncryptionCertificateCmd, requiredFlags)
common.SetCustomErrorTemplate(validateEncryptionCertificateCmd)
}
1 change: 1 addition & 0 deletions cmd/validateNetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,5 @@ func init() {
}
validateNetworkConfigCmd.PersistentFlags().String(validateNetwork.InputFlagName, "", validateNetwork.InputFlagDescription)
common.SetCustomHelpTemplate(validateNetworkConfigCmd, requiredFlags)
common.SetCustomErrorTemplate(validateNetworkConfigCmd)
}
20 changes: 20 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,23 @@ func SetCustomHelpTemplate(cmd *cobra.Command, requiredFlags map[string]bool) {
printFlags(false)
})
}

// SetCustomErrorTemplate function customizes Cobra's default flag error handling to
// print a spaced error message followed by command usage.
func SetCustomErrorTemplate(cmd *cobra.Command) {
cmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
SetMandatoryFlagError(cmd, err)
return nil
})

cmd.SilenceUsage = true
}

// SetMandatoryFlagError function print error if required flag is missing
func SetMandatoryFlagError(cmd *cobra.Command, err error) {
out := cmd.ErrOrStderr()
fmt.Fprintln(out, err)
fmt.Fprintln(out)
cmd.Usage()
os.Exit(1)
}
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ github.com/Masterminds/semver/v3 v3.4.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lpr
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ibm-hyper-protect/contract-go/v2 v2.5.0 h1:4A/sm/dHSNUci37mImOCMWRWAEIoiuu/PUslYAr6MNQ=
github.com/ibm-hyper-protect/contract-go/v2 v2.5.0/go.mod h1:i0Bb/Ko6N2RlCQiCsOqQw6bElpfLipYjXDYi3HZGejs=
github.com/ibm-hyper-protect/contract-go/v2 v2.5.1 h1:rClqsX+fYcko0ZxsTMM723o4cx7v+UwMqlbIGvGmXwo=
github.com/ibm-hyper-protect/contract-go/v2 v2.5.1/go.mod h1:i0Bb/Ko6N2RlCQiCsOqQw6bElpfLipYjXDYi3HZGejs=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
Expand Down
9 changes: 5 additions & 4 deletions lib/base64/base64.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ func ValidateInput(cmd *cobra.Command) (string, string, string, error) {
return "", "", "", err
}

if inputData == "" {
err := fmt.Errorf("Error: required flag '--in' is missing")
common.SetMandatoryFlagError(cmd, err)
}

formatType, err := cmd.Flags().GetString(FormatFlagName)
if err != nil {
return "", "", "", err
Expand All @@ -72,10 +77,6 @@ func Process(inputData, formatType string) (string, error) {
var base64String string
var err error

if inputData == "" {
return "", fmt.Errorf(inputMissingMessageBase64)
}

if formatType == TextFormat {
base64String, _, _, err = contract.HpcrText(inputData)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions lib/base64Tgz/base64Tgz.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ func ValidateInput(cmd *cobra.Command) (string, string, string, string, string,
return "", "", "", "", "", err
}

if inputData == "" {
err := fmt.Errorf("Error: required flag '--in' is missing")
common.SetMandatoryFlagError(cmd, err)
}

outputFormat, err := cmd.Flags().GetString(OutputFormatFlag)
if err != nil {
return "", "", "", "", "", err
Expand Down
33 changes: 30 additions & 3 deletions lib/decryptAttestation/decryptAttestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package decryptAttestation
import (
"fmt"
"log"
"strings"

"github.com/ibm-hyper-protect/contract-cli/common"
"github.com/ibm-hyper-protect/contract-go/v2/attestation"
Expand All @@ -32,7 +33,6 @@ const (
Attestation records are typically found at /var/hyperprotect/se-checksums.txt.enc
and contain cryptographic hashes for verifying workload integrity.`

DecryptAttestFileInDefaultPath = "build/se-checksums.txt.enc"
DecryptAttestFileInDescription = "Path to encrypted attestation file (se-checksums.txt.enc)"
DecryptAttestFlagDescription = "Path to save decrypted attestation records"
successMessageDecryptAttestation = "Successfully decrypted attestation records"
Expand All @@ -59,13 +59,40 @@ func ValidateInput(cmd *cobra.Command) (string, string, string, error) {
return "", "", "", err
}

requiredFlags := map[string]string{
"--in": encAttestPath,
"--priv": privateKeyPath,
}

var missing []string
for flag, val := range requiredFlags {
if val == "" {
missing = append(missing, flag)
}
}

if len(missing) > 0 {
if len(missing) == 1 {
err := fmt.Errorf("Error: required flag %s is missing.",
strings.Join(missing, ", "))
common.SetMandatoryFlagError(cmd, err)
} else {
err := fmt.Errorf("Error: required flag %s are missing.",
strings.Join(missing, ", "))
common.SetMandatoryFlagError(cmd, err)
}
}

return encAttestPath, privateKeyPath, decryptedAttestPath, nil
}

// DecryptAttestationRecords - function to decrypt attestation records
func DecryptAttestationRecords(encryptedAttestationRecordsPath, privateKeyPath string) (string, error) {
if !common.CheckFileFolderExists(encryptedAttestationRecordsPath) || !common.CheckFileFolderExists(privateKeyPath) {
log.Fatal("The path to encrypted attestation records file or private key doesn't exists")
if !common.CheckFileFolderExists(encryptedAttestationRecordsPath) {
log.Fatal("The path to encrypted attestation records file doesn't exists")
}
if !common.CheckFileFolderExists(privateKeyPath) {
log.Fatal("The path to private key doesn't exists")
}

encryptedChecksum, err := common.ReadDataFromFile(encryptedAttestationRecordsPath)
Expand Down
6 changes: 5 additions & 1 deletion lib/downloadCertificate/downloadCertificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ for contract encryption and workload deployment.`
)

// ValidateInput - function to validate download-certificate inputs
func ValidateInput(cmd *cobra.Command) (string, string, error) {
func ValidateInput(cmd *cobra.Command, versions []string) (string, string, error) {
formatType, err := cmd.Flags().GetString(FormatFlag)
if err != nil {
return "", "", err
Expand All @@ -50,6 +50,10 @@ func ValidateInput(cmd *cobra.Command) (string, string, error) {
if err != nil {
return "", "", err
}
if len(versions) == 0 {
err := fmt.Errorf("Error: required flag '--version' is missing")
common.SetMandatoryFlagError(cmd, err)
}

return formatType, certificatePath, nil
}
Expand Down
5 changes: 5 additions & 0 deletions lib/encrypt/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ func ValidateInput(cmd *cobra.Command) (string, string, string, string, string,
return "", "", "", "", "", err
}

if inputData == "" {
err := fmt.Errorf("Error: required flag '--in' is missing")
common.SetMandatoryFlagError(cmd, err)
}

osVersion, err := cmd.Flags().GetString(OsVersionFlagName)
if err != nil {
return "", "", "", "", "", err
Expand Down
4 changes: 4 additions & 0 deletions lib/encryptString/encryptString.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func ValidateInput(cmd *cobra.Command) (string, string, string, string, string,
if err != nil {
return "", "", "", "", "", err
}
if inputData == "" {
err := fmt.Errorf("Error: required flag '--in' is missing")
common.SetMandatoryFlagError(cmd, err)
}

inputFormat, err := cmd.Flags().GetString(FormatFlag)
if err != nil {
Expand Down
25 changes: 25 additions & 0 deletions lib/getCertificate/getCertificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package getCertificate

import (
"fmt"
"strings"

"github.com/ibm-hyper-protect/contract-cli/common"
"github.com/ibm-hyper-protect/contract-go/v2/certificate"
Expand Down Expand Up @@ -55,6 +56,30 @@ func ValidateInput(cmd *cobra.Command) (string, string, string, error) {
return "", "", "", err
}

requiredFlags := map[string]string{
"--in": encryptionCertsPath,
"--version": version,
}

var missing []string
for flag, val := range requiredFlags {
if val == "" {
missing = append(missing, flag)
}
}

if len(missing) > 0 {
if len(missing) == 1 {
err := fmt.Errorf("Error: required flag %s is missing.",
strings.Join(missing, ", "))
common.SetMandatoryFlagError(cmd, err)
} else {
err := fmt.Errorf("Error: required flag %s are missing.",
strings.Join(missing, ", "))
common.SetMandatoryFlagError(cmd, err)
}
}

return encryptionCertsPath, version, encryptionCertificatePath, nil
}

Expand Down
5 changes: 5 additions & 0 deletions lib/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func ValidateInput(cmd *cobra.Command) (string, string, string, string, error) {
if err != nil {
return "", "", "", "", err
}
if imageListJsonPath == "" {
err := fmt.Errorf("Error: required flag '--in' is missing")
common.SetMandatoryFlagError(cmd, err)
}

versionName, err := cmd.Flags().GetString(VersionFlagName)
if err != nil {
return "", "", "", "", err
Expand Down
7 changes: 7 additions & 0 deletions lib/validateContract/validateContract.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
package validateContract

import (
"fmt"

"github.com/ibm-hyper-protect/contract-cli/common"
"github.com/spf13/cobra"
)

Expand All @@ -38,6 +41,10 @@ func ValidateInput(cmd *cobra.Command) (string, string, error) {
if err != nil {
return "", "", err
}
if contract == "" {
err := fmt.Errorf("Error: required flag '--in' is missing")
common.SetMandatoryFlagError(cmd, err)
}

version, err := cmd.Flags().GetString(OsVersionFlagName)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func ValidateInput(cmd *cobra.Command) (string, error) {
if err != nil {
return "", err
}
if encryptionCertsPath == "" {
err := fmt.Errorf("Error: required flag '--in' is missing")
common.SetMandatoryFlagError(cmd, err)
}

return encryptionCertsPath, nil
}
Expand Down
7 changes: 7 additions & 0 deletions lib/validateNetwork/validateNetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
package validateNetwork

import (
"fmt"

"github.com/ibm-hyper-protect/contract-cli/common"
"github.com/spf13/cobra"
)

Expand All @@ -36,6 +39,10 @@ func ValidateInput(cmd *cobra.Command) (string, error) {
if err != nil {
return "", err
}
if networkConfig == "" {
err := fmt.Errorf("Error: required flag '--in' is missing")
common.SetMandatoryFlagError(cmd, err)
}

return networkConfig, nil
}
Loading