1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
// Copyright (c) 2019-2023, Sylabs Inc. All rights reserved.
// Copyright (c) 2017, SingularityWare, LLC. All rights reserved.
// Copyright (c) 2017, Yannick Cote <yhcote@gmail.com> All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package siftool
import (
"crypto"
"encoding/hex"
"errors"
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/sylabs/sif/v2/pkg/sif"
)
var (
dataType *int
partType *int32
partFS *int32
partArch *int32
signHash *int32
signEntity *string
sbomFormat *string
groupID *uint32
linkID *uint32
alignment *int
name *string
)
// getAddExamples returns add command examples based on rootCmd.
func getAddExamples(rootPath string) string {
examples := []string{
rootPath +
" add image.sif recipe.def --datatype 1",
rootPath +
" add image.sif rootfs.squashfs --datatype 4 --parttype 1 --partfs 1 --partarch 2",
rootPath +
" add image.sif signature.bin --datatype 5 --signentity 433FE984155206BD962725E20E8713472A879943 --signhash 1",
}
return strings.Join(examples, "\n")
}
// addFlags declares the command line flags for the add command.
func addFlags(fs *pflag.FlagSet) {
dataType = fs.Int("datatype", 0, `the type of data to add
[NEEDED, no default]:
1-Deffile, 2-EnvVar, 3-Labels,
4-Partition, 5-Signature, 6-GenericJSON,
7-Generic, 8-CryptoMessage, 9-SBOM,
10-OCI.RootIndex, 11-OCI.Blob`)
partType = fs.Int32("parttype", 0, `the type of partition (with --datatype 4-Partition)
[NEEDED, no default]:
1-System, 2-PrimSys, 3-Data,
4-Overlay`)
partFS = fs.Int32("partfs", 0, `the filesystem used (with --datatype 4-Partition)
[NEEDED, no default]:
1-Squash, 2-Ext3, 3-ImmuObj,
4-Raw`)
partArch = fs.Int32("partarch", 0, `the main architecture used (with --datatype 4-Partition)
[NEEDED, no default]:
1-386, 2-amd64, 3-arm,
4-arm64, 5-ppc64, 6-ppc64le,
7-mips, 8-mipsle, 9-mips64,
10-mips64le, 11-s390x, 12-riscv64`)
signHash = fs.Int32("signhash", 0, `the signature hash used (with --datatype 5-Signature)
[NEEDED, no default]:
1-SHA256, 2-SHA384, 3-SHA512,
4-BLAKE2s_256, 5-BLAKE2b_256`)
signEntity = fs.String("signentity", "", `the entity that signs (with --datatype 5-Signature)
[NEEDED, no default]:
example: 433FE984155206BD962725E20E8713472A879943`)
sbomFormat = fs.String("sbomformat", "", `the SBOM format (with --datatype 9-sbom):
cyclonedx-json, cyclonedx-xml, github-json,
spdx-json, spdx-rdf, spdx-tag-value,
spdx-yaml, syft-json`)
groupID = fs.Uint32("groupid", 0, "set groupid [default: 0]")
linkID = fs.Uint32("link", 0, "set link pointer [default: 0]")
alignment = fs.Int("alignment", 0, "set alignment [default: 4096 with --datatype 4-Partition, 0 otherwise]")
name = fs.String("filename", "", "set logical filename/handle [default: input filename]")
}
var errDataTypeRequired = errors.New("-datatype flag is required with a valid range")
// getDataType returns the data type corresponding to input.
func getDataType() (sif.DataType, error) {
switch *dataType {
case 1:
return sif.DataDeffile, nil
case 2:
return sif.DataEnvVar, nil
case 3:
return sif.DataLabels, nil
case 4:
return sif.DataPartition, nil
case 5:
return sif.DataSignature, nil
case 6:
return sif.DataGenericJSON, nil
case 7:
return sif.DataGeneric, nil
case 8:
return sif.DataCryptoMessage, nil
case 9:
return sif.DataSBOM, nil
case 10:
return sif.DataOCIRootIndex, nil
case 11:
return sif.DataOCIBlob, nil
default:
return 0, errDataTypeRequired
}
}
func getArch() string {
switch *partArch {
case 1:
return "386"
case 2:
return "amd64"
case 3:
return "arm"
case 4:
return "arm64"
case 5:
return "ppc64"
case 6:
return "ppc64le"
case 7:
return "mips"
case 8:
return "mipsle"
case 9:
return "mips64"
case 10:
return "mips64le"
case 11:
return "s390x"
case 12:
return "riscv64"
default:
return "unknown"
}
}
var errInvalidHashType = errors.New("invalid hash type")
func getHashType() (crypto.Hash, error) {
switch *signHash {
case 1:
return crypto.SHA256, nil
case 2:
return crypto.SHA384, nil
case 3:
return crypto.SHA512, nil
case 4:
return crypto.BLAKE2s_256, nil
case 5:
return crypto.BLAKE2b_256, nil
default:
return 0, fmt.Errorf("%w: %v", errInvalidHashType, *signHash)
}
}
var errInvalidSBOMFormat = errors.New("invalid SBOM format")
func getSBOMFormat() (sif.SBOMFormat, error) {
switch *sbomFormat {
case "cyclonedx-json":
return sif.SBOMFormatCycloneDXJSON, nil
case "cyclonedx-xml":
return sif.SBOMFormatCycloneDXXML, nil
case "github", "github-json":
return sif.SBOMFormatGitHubJSON, nil
case "spdx-json":
return sif.SBOMFormatSPDXJSON, nil
case "spdx-rdf":
return sif.SBOMFormatSPDXRDF, nil
case "spdx-tag-value":
return sif.SBOMFormatSPDXTagValue, nil
case "spdx-yaml":
return sif.SBOMFormatSPDXYAML, nil
case "syft-json":
return sif.SBOMFormatSyftJSON, nil
default:
return 0, fmt.Errorf("%w: %v", errInvalidSBOMFormat, *sbomFormat)
}
}
var (
errPartitionArgs = errors.New("with partition datatype, --partfs, --parttype and --partarch must be passed")
errInvalidFingerprintLength = errors.New("invalid signing entity fingerprint length")
errSBOMArgs = errors.New("with SBOM datatype, --sbomformat must be passed")
)
func getOptions(dt sif.DataType, fs *pflag.FlagSet) ([]sif.DescriptorInputOpt, error) {
var opts []sif.DescriptorInputOpt
if *groupID == 0 {
opts = append(opts, sif.OptNoGroup())
} else {
opts = append(opts, sif.OptGroupID(*groupID))
}
if fs.Changed("link") {
opts = append(opts, sif.OptLinkedID(*linkID))
}
if fs.Changed("alignment") {
opts = append(opts, sif.OptObjectAlignment(*alignment))
}
if fs.Changed("filename") {
opts = append(opts, sif.OptObjectName(*name))
}
switch dt {
case sif.DataPartition:
if *partType == 0 || *partFS == 0 || *partArch == 0 {
return nil, errPartitionArgs
}
opts = append(opts,
sif.OptPartitionMetadata(sif.FSType(*partFS), sif.PartType(*partType), getArch()),
)
case sif.DataSignature:
b, err := hex.DecodeString(*signEntity)
if err != nil {
return nil, fmt.Errorf("failed to decode signing entity fingerprint: %w", err)
}
ht, err := getHashType()
if err != nil {
return nil, err
}
fp := make([]byte, 20)
if got, want := len(b), len(fp); got != want {
return nil, fmt.Errorf("%w: got %v, want %v", errInvalidFingerprintLength, got, want)
}
copy(fp, b)
opts = append(opts, sif.OptSignatureMetadata(ht, fp))
case sif.DataSBOM:
if *sbomFormat == "" {
return nil, errSBOMArgs
}
f, err := getSBOMFormat()
if err != nil {
return nil, err
}
opts = append(opts, sif.OptSBOMMetadata(f))
}
return opts, nil
}
// getAdd returns a command that adds a data object to a SIF.
func (c *command) getAdd() *cobra.Command {
cmd := &cobra.Command{
Use: "add <sif_path> <object_path>",
Short: "Add data object",
Long: "Add a data object to a SIF image.",
Example: getAddExamples(c.opts.rootPath),
Args: cobra.ExactArgs(2),
}
addFlags(cmd.Flags())
cmd.PreRunE = c.initApp
cmd.RunE = func(cmd *cobra.Command, args []string) error {
dt, err := getDataType()
if err != nil {
return err
}
f, err := os.Open(args[1])
if err != nil {
return err
}
defer f.Close()
opts, err := getOptions(dt, cmd.Flags())
if err != nil {
return err
}
return c.app.Add(args[0], dt, f, opts...)
}
return cmd
}
|