gitea

Development moved to Codeberg

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  129. 129
  130. 130
  131. 131
// Copyright 2022 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package helm

import (
	"archive/tar"
	"compress/gzip"
	"errors"
	"io"
	"strings"

	"code.gitea.io/gitea/modules/validation"

	"github.com/hashicorp/go-version"
	"gopkg.in/yaml.v2"
)

var (
	// ErrMissingChartFile indicates a missing Chart.yaml file
	ErrMissingChartFile = errors.New("Chart.yaml file is missing")
	// ErrInvalidName indicates an invalid package name
	ErrInvalidName = errors.New("package name is invalid")
	// ErrInvalidVersion indicates an invalid package version
	ErrInvalidVersion = errors.New("package version is invalid")
	// ErrInvalidChart indicates an invalid chart
	ErrInvalidChart = errors.New("chart is invalid")
)

// Metadata for a Chart file. This models the structure of a Chart.yaml file.
type Metadata struct {
	APIVersion   string            `json:"api_version" yaml:"apiVersion"`
	Type         string            `json:"type,omitempty" yaml:"type,omitempty"`
	Name         string            `json:"name" yaml:"name"`
	Version      string            `json:"version" yaml:"version"`
	AppVersion   string            `json:"app_version,omitempty" yaml:"appVersion,omitempty"`
	Home         string            `json:"home,omitempty" yaml:"home,omitempty"`
	Sources      []string          `json:"sources,omitempty" yaml:"sources,omitempty"`
	Description  string            `json:"description,omitempty" yaml:"description,omitempty"`
	Keywords     []string          `json:"keywords,omitempty" yaml:"keywords,omitempty"`
	Maintainers  []*Maintainer     `json:"maintainers,omitempty" yaml:"maintainers,omitempty"`
	Icon         string            `json:"icon,omitempty" yaml:"icon,omitempty"`
	Condition    string            `json:"condition,omitempty" yaml:"condition,omitempty"`
	Tags         string            `json:"tags,omitempty" yaml:"tags,omitempty"`
	Deprecated   bool              `json:"deprecated,omitempty" yaml:"deprecated,omitempty"`
	Annotations  map[string]string `json:"annotations,omitempty" yaml:"annotations,omitempty"`
	KubeVersion  string            `json:"kube_version,omitempty" yaml:"kubeVersion,omitempty"`
	Dependencies []*Dependency     `json:"dependencies,omitempty" yaml:"dependencies,omitempty"`
}

type Maintainer struct {
	Name  string `json:"name,omitempty" yaml:"name,omitempty"`
	Email string `json:"email,omitempty" yaml:"email,omitempty"`
	URL   string `json:"url,omitempty" yaml:"url,omitempty"`
}

type Dependency struct {
	Name         string        `json:"name" yaml:"name"`
	Version      string        `json:"version,omitempty" yaml:"version,omitempty"`
	Repository   string        `json:"repository" yaml:"repository"`
	Condition    string        `json:"condition,omitempty" yaml:"condition,omitempty"`
	Tags         []string      `json:"tags,omitempty" yaml:"tags,omitempty"`
	Enabled      bool          `json:"enabled,omitempty" yaml:"enabled,omitempty"`
	ImportValues []interface{} `json:"import_values,omitempty" yaml:"import-values,omitempty"`
	Alias        string        `json:"alias,omitempty" yaml:"alias,omitempty"`
}

// ParseChartArchive parses the metadata of a Helm archive
func ParseChartArchive(r io.Reader) (*Metadata, error) {
	gzr, err := gzip.NewReader(r)
	if err != nil {
		return nil, err
	}
	defer gzr.Close()

	tr := tar.NewReader(gzr)
	for {
		hd, err := tr.Next()
		if err == io.EOF {
			break
		}
		if err != nil {
			return nil, err
		}

		if hd.Typeflag != tar.TypeReg {
			continue
		}

		if hd.FileInfo().Name() == "Chart.yaml" {
			if strings.Count(hd.Name, "/") != 1 {
				continue
			}

			return ParseChartFile(tr)
		}
	}

	return nil, ErrMissingChartFile
}

// ParseChartFile parses a Chart.yaml file to retrieve the metadata of a Helm chart
func ParseChartFile(r io.Reader) (*Metadata, error) {
	var metadata *Metadata
	if err := yaml.NewDecoder(r).Decode(&metadata); err != nil {
		return nil, err
	}

	if metadata.APIVersion == "" {
		return nil, ErrInvalidChart
	}

	if metadata.Type != "" && metadata.Type != "application" && metadata.Type != "library" {
		return nil, ErrInvalidChart
	}

	if metadata.Name == "" {
		return nil, ErrInvalidName
	}

	if _, err := version.NewSemver(metadata.Version); err != nil {
		return nil, ErrInvalidVersion
	}

	if !validation.IsValidURL(metadata.Home) {
		metadata.Home = ""
	}

	return metadata, nil
}