Files
syft/syft/formats/common/spdxhelpers/document_namespace.go
T
Alex Goodman 4da3be864f Refactor source API (#1846)
* refactor source API and syft json source block

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* update source detection and format test utils

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* generate list of all source metadata types

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* extract base and root normalization into helper functions

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* preserve syftjson model package name import ref

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* alias should not be a pointer

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

---------

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
2023-06-30 14:19:16 +00:00

62 lines
1.4 KiB
Go

package spdxhelpers
import (
"fmt"
"net/url"
"path"
"strings"
"github.com/google/uuid"
"github.com/anchore/syft/internal"
"github.com/anchore/syft/syft/source"
)
const (
inputImage = "image"
inputDirectory = "dir"
inputFile = "file"
)
func DocumentNameAndNamespace(src source.Description) (string, string) {
name := DocumentName(src)
return name, DocumentNamespace(name, src)
}
func DocumentNamespace(name string, src source.Description) string {
name = cleanName(name)
input := "unknown-source-type"
switch src.Metadata.(type) {
case source.StereoscopeImageSourceMetadata:
input = inputImage
case source.DirectorySourceMetadata:
input = inputDirectory
case source.FileSourceMetadata:
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(internal.ApplicationName, 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)
}