初始提交: Gitea 项目代码
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package container
|
||||
|
||||
const (
|
||||
ContentTypeDockerDistributionManifestV2 = "application/vnd.docker.distribution.manifest.v2+json"
|
||||
|
||||
ManifestFilename = "manifest.json"
|
||||
UploadVersion = "_upload"
|
||||
)
|
||||
@@ -0,0 +1,55 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package helm
|
||||
|
||||
// https://github.com/helm/helm/blob/main/pkg/chart/
|
||||
|
||||
const ConfigMediaType = "application/vnd.cncf.helm.config.v1+json"
|
||||
|
||||
// Maintainer describes a Chart maintainer.
|
||||
type Maintainer struct {
|
||||
// Name is a user name or organization name
|
||||
Name string `json:"name,omitempty"`
|
||||
// Email is an optional email address to contact the named maintainer
|
||||
Email string `json:"email,omitempty"`
|
||||
// URL is an optional URL to an address for the named maintainer
|
||||
URL string `json:"url,omitempty"`
|
||||
}
|
||||
|
||||
// Metadata for a Chart file. This models the structure of a Chart.yaml file.
|
||||
type Metadata struct {
|
||||
// The name of the chart. Required.
|
||||
Name string `json:"name,omitempty"`
|
||||
// The URL to a relevant project page, git repo, or contact person
|
||||
Home string `json:"home,omitempty"`
|
||||
// Source is the URL to the source code of this chart
|
||||
Sources []string `json:"sources,omitempty"`
|
||||
// A SemVer 2 conformant version string of the chart. Required.
|
||||
Version string `json:"version,omitempty"`
|
||||
// A one-sentence description of the chart
|
||||
Description string `json:"description,omitempty"`
|
||||
// A list of string keywords
|
||||
Keywords []string `json:"keywords,omitempty"`
|
||||
// A list of name and URL/email address combinations for the maintainer(s)
|
||||
Maintainers []*Maintainer `json:"maintainers,omitempty"`
|
||||
// The URL to an icon file.
|
||||
Icon string `json:"icon,omitempty"`
|
||||
// The API Version of this chart. Required.
|
||||
APIVersion string `json:"apiVersion,omitempty"`
|
||||
// The condition to check to enable chart
|
||||
Condition string `json:"condition,omitempty"`
|
||||
// The tags to check to enable chart
|
||||
Tags string `json:"tags,omitempty"`
|
||||
// The version of the application enclosed inside of this chart.
|
||||
AppVersion string `json:"appVersion,omitempty"`
|
||||
// Whether or not this chart is deprecated
|
||||
Deprecated bool `json:"deprecated,omitempty"`
|
||||
// Annotations are additional mappings uninterpreted by Helm,
|
||||
// made available for inspection by other applications.
|
||||
Annotations map[string]string `json:"annotations,omitempty"`
|
||||
// KubeVersion is a SemVer constraint specifying the version of Kubernetes required.
|
||||
KubeVersion string `json:"kubeVersion,omitempty"`
|
||||
// Specifies the chart type: application or library
|
||||
Type string `json:"type,omitempty"`
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package container
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"gitea.dev/modules/json"
|
||||
"gitea.dev/modules/packages/container/helm"
|
||||
"gitea.dev/modules/validation"
|
||||
|
||||
oci "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
)
|
||||
|
||||
const (
|
||||
PropertyRepository = "container.repository"
|
||||
PropertyDigest = "container.digest"
|
||||
PropertyMediaType = "container.mediatype"
|
||||
PropertyManifestTagged = "container.manifest.tagged"
|
||||
PropertyManifestReference = "container.manifest.reference"
|
||||
|
||||
DefaultPlatform = "linux/amd64"
|
||||
|
||||
labelLicenses = "org.opencontainers.image.licenses"
|
||||
labelURL = "org.opencontainers.image.url"
|
||||
labelSource = "org.opencontainers.image.source"
|
||||
labelDocumentation = "org.opencontainers.image.documentation"
|
||||
labelDescription = "org.opencontainers.image.description"
|
||||
labelAuthors = "org.opencontainers.image.authors"
|
||||
)
|
||||
|
||||
type ImageType string
|
||||
|
||||
const (
|
||||
TypeOCI ImageType = "oci"
|
||||
TypeHelm ImageType = "helm"
|
||||
)
|
||||
|
||||
// Name gets the name of the image type
|
||||
func (it ImageType) Name() string {
|
||||
switch it {
|
||||
case TypeHelm:
|
||||
return "Helm Chart"
|
||||
default:
|
||||
return "OCI / Docker"
|
||||
}
|
||||
}
|
||||
|
||||
// Metadata represents the metadata of a Container package
|
||||
type Metadata struct {
|
||||
Type ImageType `json:"type"`
|
||||
IsTagged bool `json:"is_tagged"`
|
||||
Platform string `json:"platform,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Authors []string `json:"authors,omitempty"`
|
||||
Licenses string `json:"license,omitempty"`
|
||||
ProjectURL string `json:"project_url,omitempty"`
|
||||
RepositoryURL string `json:"repository_url,omitempty"`
|
||||
DocumentationURL string `json:"documentation_url,omitempty"`
|
||||
Labels map[string]string `json:"labels,omitempty"`
|
||||
ImageLayers []string `json:"layer_creation,omitempty"`
|
||||
Manifests []*Manifest `json:"manifests,omitempty"`
|
||||
}
|
||||
|
||||
type Manifest struct {
|
||||
Platform string `json:"platform"`
|
||||
Digest string `json:"digest"`
|
||||
Size int64 `json:"size"`
|
||||
}
|
||||
|
||||
func IsMediaTypeValid(mt string) bool {
|
||||
return strings.HasPrefix(mt, "application/vnd.docker.") || strings.HasPrefix(mt, "application/vnd.oci.")
|
||||
}
|
||||
|
||||
func IsMediaTypeImageManifest(mt string) bool {
|
||||
return strings.EqualFold(mt, oci.MediaTypeImageManifest) || strings.EqualFold(mt, "application/vnd.docker.distribution.manifest.v2+json")
|
||||
}
|
||||
|
||||
func IsMediaTypeImageIndex(mt string) bool {
|
||||
return strings.EqualFold(mt, oci.MediaTypeImageIndex) || strings.EqualFold(mt, "application/vnd.docker.distribution.manifest.list.v2+json")
|
||||
}
|
||||
|
||||
// ParseImageConfig parses the metadata of an image config
|
||||
func ParseImageConfig(mediaType string, r io.Reader) (*Metadata, error) {
|
||||
if strings.EqualFold(mediaType, helm.ConfigMediaType) {
|
||||
return parseHelmConfig(r)
|
||||
}
|
||||
|
||||
// fallback to OCI Image Config
|
||||
// FIXME: this fallback is not right, we should strictly check the media type in the future
|
||||
metadata, err := parseOCIImageConfig(r)
|
||||
if err != nil {
|
||||
if !IsMediaTypeImageManifest(mediaType) {
|
||||
return &Metadata{Platform: "unknown/unknown"}, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return metadata, nil
|
||||
}
|
||||
|
||||
func parseOCIImageConfig(r io.Reader) (*Metadata, error) {
|
||||
var image oci.Image
|
||||
// FIXME: JSON-KEY-CASE: here seems a abuse of the case-insensitive decoding feature, spec is case-sensitive
|
||||
// https://github.com/opencontainers/image-spec/blob/main/schema/config-schema.json
|
||||
if err := json.NewDecoderCaseInsensitive(r).Decode(&image); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
platform := DefaultPlatform
|
||||
if image.OS != "" && image.Architecture != "" {
|
||||
platform = fmt.Sprintf("%s/%s", image.OS, image.Architecture)
|
||||
if image.Variant != "" {
|
||||
platform = fmt.Sprintf("%s/%s", platform, image.Variant)
|
||||
}
|
||||
}
|
||||
|
||||
imageLayers := make([]string, 0, len(image.History))
|
||||
for _, history := range image.History {
|
||||
cmd := history.CreatedBy
|
||||
if i := strings.Index(cmd, "#(nop) "); i != -1 {
|
||||
cmd = strings.TrimSpace(cmd[i+7:])
|
||||
}
|
||||
if cmd != "" {
|
||||
imageLayers = append(imageLayers, cmd)
|
||||
}
|
||||
}
|
||||
|
||||
metadata := &Metadata{
|
||||
Type: TypeOCI,
|
||||
Platform: platform,
|
||||
Licenses: image.Config.Labels[labelLicenses],
|
||||
ProjectURL: image.Config.Labels[labelURL],
|
||||
RepositoryURL: image.Config.Labels[labelSource],
|
||||
DocumentationURL: image.Config.Labels[labelDocumentation],
|
||||
Description: image.Config.Labels[labelDescription],
|
||||
Labels: image.Config.Labels,
|
||||
ImageLayers: imageLayers,
|
||||
}
|
||||
|
||||
if authors, ok := image.Config.Labels[labelAuthors]; ok {
|
||||
metadata.Authors = []string{authors}
|
||||
}
|
||||
|
||||
if !validation.IsValidURL(metadata.ProjectURL) {
|
||||
metadata.ProjectURL = ""
|
||||
}
|
||||
if !validation.IsValidURL(metadata.RepositoryURL) {
|
||||
metadata.RepositoryURL = ""
|
||||
}
|
||||
if !validation.IsValidURL(metadata.DocumentationURL) {
|
||||
metadata.DocumentationURL = ""
|
||||
}
|
||||
|
||||
return metadata, nil
|
||||
}
|
||||
|
||||
func parseHelmConfig(r io.Reader) (*Metadata, error) {
|
||||
var config helm.Metadata
|
||||
if err := json.NewDecoder(r).Decode(&config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
metadata := &Metadata{
|
||||
Type: TypeHelm,
|
||||
Description: config.Description,
|
||||
ProjectURL: config.Home,
|
||||
}
|
||||
|
||||
if len(config.Maintainers) > 0 {
|
||||
authors := make([]string, 0, len(config.Maintainers))
|
||||
for _, maintainer := range config.Maintainers {
|
||||
authors = append(authors, maintainer.Name)
|
||||
}
|
||||
metadata.Authors = authors
|
||||
}
|
||||
|
||||
if len(config.Sources) > 0 && validation.IsValidURL(config.Sources[0]) {
|
||||
metadata.RepositoryURL = config.Sources[0]
|
||||
}
|
||||
if !validation.IsValidURL(metadata.ProjectURL) {
|
||||
metadata.ProjectURL = ""
|
||||
}
|
||||
|
||||
return metadata, nil
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package container
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.dev/modules/packages/container/helm"
|
||||
|
||||
oci "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestParseImageConfig(t *testing.T) {
|
||||
description := "Image Description"
|
||||
author := "Gitea"
|
||||
license := "MIT"
|
||||
projectURL := "https://gitea.com"
|
||||
repositoryURL := "https://gitea.com/gitea"
|
||||
documentationURL := "https://docs.gitea.com"
|
||||
|
||||
// FIXME: JSON-KEY-CASE: the test case is not right, the config fields are capitalized in the spec
|
||||
// https://github.com/opencontainers/image-spec/blob/main/schema/config-schema.json
|
||||
configOCI := `{"config": {"labels": {"` + labelAuthors + `": "` + author + `", "` + labelLicenses + `": "` + license + `", "` + labelURL + `": "` + projectURL + `", "` + labelSource + `": "` + repositoryURL + `", "` + labelDocumentation + `": "` + documentationURL + `", "` + labelDescription + `": "` + description + `"}}, "history": [{"created_by": "do it 1"}, {"created_by": "dummy #(nop) do it 2"}]}`
|
||||
|
||||
metadata, err := ParseImageConfig(oci.MediaTypeImageManifest, strings.NewReader(configOCI))
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, TypeOCI, metadata.Type)
|
||||
assert.Equal(t, description, metadata.Description)
|
||||
assert.ElementsMatch(t, []string{author}, metadata.Authors)
|
||||
assert.Equal(t, license, metadata.Licenses)
|
||||
assert.Equal(t, projectURL, metadata.ProjectURL)
|
||||
assert.Equal(t, repositoryURL, metadata.RepositoryURL)
|
||||
assert.Equal(t, documentationURL, metadata.DocumentationURL)
|
||||
assert.ElementsMatch(t, []string{"do it 1", "do it 2"}, metadata.ImageLayers)
|
||||
assert.Equal(
|
||||
t,
|
||||
map[string]string{
|
||||
labelAuthors: author,
|
||||
labelLicenses: license,
|
||||
labelURL: projectURL,
|
||||
labelSource: repositoryURL,
|
||||
labelDocumentation: documentationURL,
|
||||
labelDescription: description,
|
||||
},
|
||||
metadata.Labels,
|
||||
)
|
||||
assert.Empty(t, metadata.Manifests)
|
||||
|
||||
configHelm := `{"description":"` + description + `", "home": "` + projectURL + `", "sources": ["` + repositoryURL + `"], "maintainers":[{"name":"` + author + `"}]}`
|
||||
|
||||
metadata, err = ParseImageConfig(helm.ConfigMediaType, strings.NewReader(configHelm))
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, TypeHelm, metadata.Type)
|
||||
assert.Equal(t, description, metadata.Description)
|
||||
assert.ElementsMatch(t, []string{author}, metadata.Authors)
|
||||
assert.Equal(t, projectURL, metadata.ProjectURL)
|
||||
assert.Equal(t, repositoryURL, metadata.RepositoryURL)
|
||||
|
||||
metadata, err = ParseImageConfig("anything-unknown", strings.NewReader(""))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, &Metadata{Platform: "unknown/unknown"}, metadata)
|
||||
}
|
||||
Reference in New Issue
Block a user