Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compare index.yaml for integrity of git pages. #150

Merged
merged 7 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/blang/semver v3.5.1+incompatible
github.com/go-git/go-billy/v5 v5.3.1
github.com/go-git/go-git/v5 v5.4.2
github.com/google/go-cmp v0.6.0
github.com/google/go-github/v41 v41.0.0
github.com/hashicorp/go-version v1.6.0
github.com/sirupsen/logrus v1.8.1
Expand Down Expand Up @@ -72,7 +73,6 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,8 @@ github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github/v41 v41.0.0 h1:HseJrM2JFf2vfiZJ8anY2hqBjdfY1Vlj/K27ueww4gg=
github.com/google/go-github/v41 v41.0.0/go.mod h1:XgmCA5H323A9rtgExdTcnDkcqp6S30AVACCBDOonIxg=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
Expand Down
35 changes: 34 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,6 @@ func main() {
Usage: `Usage:
./bin/charts-build-scripts <command> --branch="release-v2.y"
BRANCH="release-v2.y" make <command>

Available branches for release: (release-v2.8; release-v2.9; release-v2.10...)
`,
Required: true,
Expand Down Expand Up @@ -374,6 +373,25 @@ func main() {
},
},
},
{
Name: "compare-index-files",
Usage: `Compare the index.yaml between github repository and charts.rancher.io.
`,
Action: compareIndexFiles,
Flags: []cli.Flag{
cli.StringFlag{
Name: "branch,b",
Usage: `Usage:
./bin/charts-build-scripts <command> --branch="release-v2.y"
BRANCH="release-v2.y" make <command>
Available branches for release: (release-v2.8; release-v2.9; release-v2.10...)
`,
Required: true,
EnvVar: defaultBranchEnvironmentVariable,
Destination: &Branch,
},
},
},
}

if err := app.Run(os.Args); err != nil {
Expand Down Expand Up @@ -803,3 +821,18 @@ func validateRelease(c *cli.Context) {
os.Exit(1)
}
}

func compareIndexFiles(c *cli.Context) {
if Branch == "" {
fmt.Println("BRANCH environment variable must be set to run validate-release-charts")
os.Exit(1)
}

rootFs := filesystem.GetFilesystem(getRepoRoot())

if err := auto.CompareIndexFiles(rootFs); err != nil {
fmt.Printf("failed to compare index files: %v \n", err)
os.Exit(1)
}
fmt.Println("index.yaml files are the same at git repository and charts.rancher.io")
}
59 changes: 59 additions & 0 deletions pkg/auto/validate.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
package auto

import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"strconv"

"github.com/Masterminds/semver"
"github.com/go-git/go-billy/v5"
"github.com/google/go-cmp/cmp"
"github.com/google/go-github/v41/github"
"github.com/rancher/charts-build-scripts/pkg/helm"
"github.com/rancher/charts-build-scripts/pkg/lifecycle"
"github.com/rancher/charts-build-scripts/pkg/options"
"github.com/rancher/charts-build-scripts/pkg/path"
"golang.org/x/oauth2"

helmRepo "helm.sh/helm/v3/pkg/repo"
)

// Referred to: https://github.com/rancher/charts
Expand Down Expand Up @@ -170,3 +179,53 @@ func (v *validation) checkNeverModifyReleasedChart(assetFilePaths map[string]str

return nil
}

// CompareIndexFiles will load the current index.yaml file from the root filesystem and compare it with the index.yaml file from charts.rancher.io
func CompareIndexFiles(rootFs billy.Filesystem) error {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function returns an error in some cases and also exits in other cases. There should only be 1 entry and exit point for a program, generally. Can we update this to return an error consistently and have the caller made the decision to exit or otherwise?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

// verify, search & open current index.yaml file
localIndexYaml, err := helm.OpenIndexYaml(rootFs)
if err != nil {
return err
}

// download index.yaml from charts.rancher.io
resp, err := http.Get("https://charts.rancher.io/index.yaml")
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("status code %d", resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}

// save it to a temporary file
tempIndex, err := os.CreateTemp("", "temp-index.yaml")
if err != nil {
return err
}
defer tempIndex.Close()

_, err = io.Copy(tempIndex, bytes.NewReader(body))
if err != nil {
return err
}

tempIndexYaml, err := helmRepo.LoadIndexFile(tempIndex.Name())
if err != nil {
return err
}
defer os.Remove(tempIndex.Name())

// compare both index.yaml files
if diff := cmp.Diff(localIndexYaml, tempIndexYaml); diff != "" {
fmt.Println(diff)
return errors.New("index.yaml files are different at git repository and charts.rancher.io")
}
return nil
}
16 changes: 16 additions & 0 deletions pkg/helm/helm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package helm

import (
"errors"
"fmt"
"os"

Expand Down Expand Up @@ -107,3 +108,18 @@ func UpdateIndex(original, new *helmRepo.IndexFile) (*helmRepo.IndexFile, bool)
updatedIndex.SortEntries()
return updatedIndex, upToDate
}

// OpenIndexYaml will check and open the index.yaml file in the local repository at the default file path
func OpenIndexYaml(rootFs billy.Filesystem) (*helmRepo.IndexFile, error) {
helmIndexFilePath := filesystem.GetAbsPath(rootFs, path.RepositoryHelmIndexFile)

exists, err := filesystem.PathExists(rootFs, path.RepositoryHelmIndexFile)
if err != nil {
return nil, err
}
if !exists {
return nil, errors.New("index.yaml file does not exist in the local repository")
}

return helmRepo.LoadIndexFile(helmIndexFilePath)
}
File renamed without changes.
Loading