sign

package
v2.0.0-...-6831f71 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SignCmd = &cobra.Command{
	Use:   "sign",
	Short: "Sign the yaml",
	Long:  `Sign the yaml. Takes yaml file, public key and private key as args`,
	Run: func(cmd *cobra.Command, args []string) {

		if publickey == "" {
			log.Error(errors.New("public key not provided"), "public key not provided")
			os.Exit(1)
		}

		if privatekey == "" {
			log.Error(errors.New("private key not provided"), "private key not provided")
			os.Exit(1)
		}

		var file io.ReadCloser
		var err error
		if signer.IsInputFromPipe() {
			file = os.Stdin
		} else {
			file, err = signer.OpenInputFile(f)
			if err != nil {
				log.Error(err, "Could not open input file")
				os.Exit(1)
			}
		}

		uobjs, err := signer.Decode(file)
		file.Close()
		if err != nil {
			log.Error(err, "Could not Decode input yaml contents")
			os.Exit(1)
		}

		pubKey, err := os.ReadFile(publickey)
		if err != nil {
			log.Error(err, "Could not read public key file")
			os.Exit(1)
		}

		password := ""
		if privatekeypassword != "" {
			password = privatekeypassword
		}

		privKey, err := signer.PrivateKeyFromPemFile(privatekey, password)
		if err != nil {
			log.Error(err, "Could not get private key from pem file")
			os.Exit(1)
		}

		for i, uobj := range uobjs {

			bytes, err := signer.UnstructuredToGVKSpecBytes(uobj)
			if err != nil {
				log.Error(err, "could not MarshalJSON")
				os.Exit(1)
			}

			hash := sha256.Sum256(bytes)

			signature, err := rsa.SignPSS(rand.Reader, privKey, crypto.SHA256, hash[:], nil)
			if err != nil {
				log.Error(err, "could not sign")
				os.Exit(1)
			}

			annotations := make(map[string]string)

			annotations["marketplace.redhat.com/signature"] = fmt.Sprintf("%x", signature)
			annotations["marketplace.redhat.com/publickey"] = fmt.Sprintf("%s", pubKey)

			uobjs[i].SetAnnotations(annotations)
		}

		uList := unstructured.UnstructuredList{}
		uList.SetGroupVersionKind(schema.GroupVersionKind{
			Group:   "",
			Version: "v1",
			Kind:    "List",
		})
		uList.Items = uobjs

		uListBytes, err := uList.MarshalJSON()
		if err != nil {
			log.Error(err, "could not MarshalJSON")
			os.Exit(1)
		}

		yamlout, err := sigsyaml.JSONToYAML(uListBytes)
		if err != nil {
			log.Error(err, "could not JSONToYAML")
			os.Exit(1)
		}

		fmt.Printf("%s", yamlout)

		os.Exit(0)
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL