internal/helm: validate loaded chart metadata obj
Signed-off-by: Hidde Beydals <hello@hidde.co>
This commit is contained in:
		
							parent
							
								
									750b10e57b
								
							
						
					
					
						commit
						fb0d7f24c8
					
				| 
						 | 
					@ -79,6 +79,9 @@ func (b *localChartBuilder) Build(ctx context.Context, ref Reference, p string,
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, &BuildError{Reason: ErrChartPull, Err: err}
 | 
							return nil, &BuildError{Reason: ErrChartPull, Err: err}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						if err = curMeta.Validate(); err != nil {
 | 
				
			||||||
 | 
							return nil, &BuildError{Reason: ErrChartPull, Err: err}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	result := &Build{}
 | 
						result := &Build{}
 | 
				
			||||||
	result.Name = curMeta.Name
 | 
						result.Name = curMeta.Name
 | 
				
			||||||
| 
						 | 
					@ -104,6 +107,9 @@ func (b *localChartBuilder) Build(ctx context.Context, ref Reference, p string,
 | 
				
			||||||
	// - BuildOptions.Force is False
 | 
						// - BuildOptions.Force is False
 | 
				
			||||||
	if opts.CachedChart != "" && !opts.Force {
 | 
						if opts.CachedChart != "" && !opts.Force {
 | 
				
			||||||
		if curMeta, err = LoadChartMetadataFromArchive(opts.CachedChart); err == nil {
 | 
							if curMeta, err = LoadChartMetadataFromArchive(opts.CachedChart); err == nil {
 | 
				
			||||||
 | 
								// If the cached metadata is corrupt, we ignore its existence
 | 
				
			||||||
 | 
								// and continue the build
 | 
				
			||||||
 | 
								if err = curMeta.Validate(); err == nil {
 | 
				
			||||||
				if result.Name == curMeta.Name && result.Version == curMeta.Version {
 | 
									if result.Name == curMeta.Name && result.Version == curMeta.Version {
 | 
				
			||||||
					result.Path = opts.CachedChart
 | 
										result.Path = opts.CachedChart
 | 
				
			||||||
					result.ValuesFiles = opts.ValuesFiles
 | 
										result.ValuesFiles = opts.ValuesFiles
 | 
				
			||||||
| 
						 | 
					@ -111,6 +117,7 @@ func (b *localChartBuilder) Build(ctx context.Context, ref Reference, p string,
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// If the chart at the path is already packaged and no custom values files
 | 
						// If the chart at the path is already packaged and no custom values files
 | 
				
			||||||
	// options are set, we can copy the chart without making modifications
 | 
						// options are set, we can copy the chart without making modifications
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -108,6 +108,9 @@ func (b *remoteChartBuilder) Build(_ context.Context, ref Reference, p string, o
 | 
				
			||||||
	// - BuildOptions.Force is False
 | 
						// - BuildOptions.Force is False
 | 
				
			||||||
	if opts.CachedChart != "" && !opts.Force {
 | 
						if opts.CachedChart != "" && !opts.Force {
 | 
				
			||||||
		if curMeta, err := LoadChartMetadataFromArchive(opts.CachedChart); err == nil {
 | 
							if curMeta, err := LoadChartMetadataFromArchive(opts.CachedChart); err == nil {
 | 
				
			||||||
 | 
								// If the cached metadata is corrupt, we ignore its existence
 | 
				
			||||||
 | 
								// and continue the build
 | 
				
			||||||
 | 
								if err = curMeta.Validate(); err == nil {
 | 
				
			||||||
				if result.Name == curMeta.Name && result.Version == curMeta.Version {
 | 
									if result.Name == curMeta.Name && result.Version == curMeta.Version {
 | 
				
			||||||
					result.Path = opts.CachedChart
 | 
										result.Path = opts.CachedChart
 | 
				
			||||||
					result.ValuesFiles = opts.GetValuesFiles()
 | 
										result.ValuesFiles = opts.GetValuesFiles()
 | 
				
			||||||
| 
						 | 
					@ -115,6 +118,7 @@ func (b *remoteChartBuilder) Build(_ context.Context, ref Reference, p string, o
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Download the package for the resolved version
 | 
						// Download the package for the resolved version
 | 
				
			||||||
	res, err := b.remote.DownloadChart(cv)
 | 
						res, err := b.remote.DownloadChart(cv)
 | 
				
			||||||
| 
						 | 
					@ -207,9 +211,13 @@ func validatePackageAndWriteToPath(reader io.Reader, out string) error {
 | 
				
			||||||
	if err = tmpFile.Close(); err != nil {
 | 
						if err = tmpFile.Close(); err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if _, err = LoadChartMetadataFromArchive(tmpFile.Name()); err != nil {
 | 
						meta, err := LoadChartMetadataFromArchive(tmpFile.Name())
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
		return fmt.Errorf("failed to load chart metadata from written chart: %w", err)
 | 
							return fmt.Errorf("failed to load chart metadata from written chart: %w", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						if err = meta.Validate(); err != nil {
 | 
				
			||||||
 | 
							return fmt.Errorf("failed to validate metadata of written chart: %w", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	if err = fs.RenameWithFallback(tmpFile.Name(), out); err != nil {
 | 
						if err = fs.RenameWithFallback(tmpFile.Name(), out); err != nil {
 | 
				
			||||||
		return fmt.Errorf("failed to write chart to file: %w", err)
 | 
							return fmt.Errorf("failed to write chart to file: %w", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue