mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 08:23:15 +01:00
62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
package helpers
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"path"
|
|
"strings"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/anchore/syft/syft/sbom"
|
|
"github.com/anchore/syft/syft/source"
|
|
)
|
|
|
|
const (
|
|
InputImage = "image"
|
|
InputDirectory = "dir"
|
|
InputFile = "file"
|
|
)
|
|
|
|
func DocumentNameAndNamespace(src source.Description, desc sbom.Descriptor) (string, string) {
|
|
name := DocumentName(src)
|
|
return name, DocumentNamespace(name, src, desc)
|
|
}
|
|
|
|
func DocumentNamespace(name string, src source.Description, desc sbom.Descriptor) string {
|
|
name = cleanName(name)
|
|
input := "unknown-source-type"
|
|
switch src.Metadata.(type) {
|
|
case source.ImageMetadata:
|
|
input = InputImage
|
|
case source.DirectoryMetadata:
|
|
input = InputDirectory
|
|
case source.FileMetadata:
|
|
input = InputFile
|
|
}
|
|
|
|
uniqueID := uuid.Must(uuid.NewRandom())
|
|
identifier := path.Join(input, uniqueID.String())
|
|
if name != "." {
|
|
identifier = path.Join(input, fmt.Sprintf("%s-%s", name, uniqueID.String()))
|
|
}
|
|
|
|
u := url.URL{
|
|
Scheme: "https",
|
|
Host: "anchore.com",
|
|
Path: path.Join(desc.Name, identifier),
|
|
}
|
|
|
|
return u.String()
|
|
}
|
|
|
|
// see: https://spdx.github.io/spdx-spec/v2.3/document-creation-information/#65-spdx-document-namespace-field
|
|
func cleanName(name string) string {
|
|
// remove # according to specification
|
|
name = strings.ReplaceAll(name, "#", "-")
|
|
// remove : for url construction
|
|
name = strings.ReplaceAll(name, ":", "-")
|
|
// clean relative pathing
|
|
return path.Clean(name)
|
|
}
|