internal/helm: validate loaded chart metadata obj

Signed-off-by: Hidde Beydals <hello@hidde.co>
This commit is contained in:
Hidde Beydals 2021-11-23 10:00:45 +01:00
parent 750b10e57b
commit fb0d7f24c8
2 changed files with 24 additions and 9 deletions

View File

@ -79,6 +79,9 @@ func (b *localChartBuilder) Build(ctx context.Context, ref Reference, p string,
if err != nil {
return nil, &BuildError{Reason: ErrChartPull, Err: err}
}
if err = curMeta.Validate(); err != nil {
return nil, &BuildError{Reason: ErrChartPull, Err: err}
}
result := &Build{}
result.Name = curMeta.Name
@ -104,10 +107,14 @@ func (b *localChartBuilder) Build(ctx context.Context, ref Reference, p string,
// - BuildOptions.Force is False
if opts.CachedChart != "" && !opts.Force {
if curMeta, err = LoadChartMetadataFromArchive(opts.CachedChart); err == nil {
if result.Name == curMeta.Name && result.Version == curMeta.Version {
result.Path = opts.CachedChart
result.ValuesFiles = opts.ValuesFiles
return result, 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 {
result.Path = opts.CachedChart
result.ValuesFiles = opts.ValuesFiles
return result, nil
}
}
}
}

View File

@ -108,10 +108,14 @@ func (b *remoteChartBuilder) Build(_ context.Context, ref Reference, p string, o
// - BuildOptions.Force is False
if opts.CachedChart != "" && !opts.Force {
if curMeta, err := LoadChartMetadataFromArchive(opts.CachedChart); err == nil {
if result.Name == curMeta.Name && result.Version == curMeta.Version {
result.Path = opts.CachedChart
result.ValuesFiles = opts.GetValuesFiles()
return result, 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 {
result.Path = opts.CachedChart
result.ValuesFiles = opts.GetValuesFiles()
return result, nil
}
}
}
}
@ -207,9 +211,13 @@ func validatePackageAndWriteToPath(reader io.Reader, out string) error {
if err = tmpFile.Close(); err != nil {
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)
}
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 {
return fmt.Errorf("failed to write chart to file: %w", err)
}