Skip to content

Commit

Permalink
integrate checksum in kpm workflow
Browse files Browse the repository at this point in the history
Signed-off-by: Nishant Bansal <[email protected]>
  • Loading branch information
NishantBansal2003 committed Nov 25, 2024
1 parent 63bea3a commit b9f3bda
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 10 deletions.
85 changes: 78 additions & 7 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"oras.land/oras-go/v2"
remoteauth "oras.land/oras-go/v2/registry/remote/auth"

"kcl-lang.io/kpm/pkg/checker"
"kcl-lang.io/kpm/pkg/constants"
"kcl-lang.io/kpm/pkg/downloader"
"kcl-lang.io/kpm/pkg/env"
Expand Down Expand Up @@ -47,6 +48,8 @@ type KpmClient struct {
homePath string
// The settings of kpm loaded from the global configuration file.
settings settings.Settings
// The checker to validate dependencies
DepChecker *checker.DepChecker
// The flag of whether to check the checksum of the package and update kcl.mod.lock.
noSumCheck bool
// The flag of whether to skip the verification of TLS.
Expand All @@ -66,10 +69,16 @@ func NewKpmClient() (*KpmClient, error) {
return nil, err
}

depChecker := checker.NewDepChecker(
checker.WithCheckers(checker.NewIdentChecker(), checker.NewVersionChecker(), checker.NewSumChecker(
checker.WithSettings(*settings))),
)

return &KpmClient{
logWriter: os.Stdout,
settings: *settings,
homePath: homePath,
DepChecker: depChecker,
DepDownloader: &downloader.DepDownloader{},
}, nil
}
Expand Down Expand Up @@ -897,16 +906,11 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (*
}

dep.FromKclPkg(dpkg)
dep.Sum, err = c.AcquireDepSum(*dep)

dep.Sum, err = utils.HashDir(localPath)
if err != nil {
return nil, err
}
if dep.Sum == "" {
dep.Sum, err = utils.HashDir(localPath)
if err != nil {
return nil, err
}
}

if dep.LocalFullPath == "" {
dep.LocalFullPath = localPath
Expand All @@ -926,6 +930,9 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (*
// if err != nil {
// return nil, err
// }
if err := c.ValidateDependency(dep); err != nil {
return nil, err
}
}

if dep.Source.Local != nil {
Expand All @@ -939,6 +946,24 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (*
return dep, nil
}

func (c *KpmClient) ValidateDependency(dep *pkg.Dependency) error {
tmpKclPkg := pkg.KclPkg{
HomePath: dep.LocalFullPath,
Dependencies: pkg.Dependencies{Deps: func() *orderedmap.OrderedMap[string, pkg.Dependency] {
m := orderedmap.NewOrderedMap[string, pkg.Dependency]()
m.Set(dep.Name, *dep)
return m
}()},
NoSumCheck: c.GetNoSumCheck(),
}

if err := c.DepChecker.Check(tmpKclPkg); err != nil {
return reporter.NewErrorEvent(reporter.InvalidKclPkg, err, fmt.Sprintf("%s package does not match the original kcl package", dep.FullName))
}

return nil
}

// DownloadFromGit will download the dependency from the git repository.
func (c *KpmClient) DownloadFromGit(dep *downloader.Git, localPath string) (string, error) {
var msg string
Expand Down Expand Up @@ -1148,13 +1173,57 @@ func (c *KpmClient) PullFromOci(localPath, source, tag string) error {
)
}

if err := c.ValidatePkgPullFromOci(ociOpts, storagePath); err != nil {
return reporter.NewErrorEvent(
reporter.InvalidKclPkg,
err,
fmt.Sprintf("failed to validate kclPkg at %s", storagePath),
)
}

reporter.ReportMsgTo(
fmt.Sprintf("pulled '%s' in '%s' successfully", source, storagePath),
c.logWriter,
)
return nil
}

func (c *KpmClient) ValidatePkgPullFromOci(ociOpts *opt.OciOptions, storagePath string) error {
kclPkg, err := c.LoadPkgFromPath(storagePath)
if err != nil {
return reporter.NewErrorEvent(
reporter.FailedGetPkg,
err,
fmt.Sprintf("failed to load kclPkg at %v", storagePath),
)
}

dep := &pkg.Dependency{
Name: kclPkg.GetPkgName(),
Source: downloader.Source{
Oci: &downloader.Oci{
Reg: ociOpts.Reg,
Repo: ociOpts.Repo,
Tag: ociOpts.Tag,
},
},
}

dep.FromKclPkg(kclPkg)
dep.Sum, err = utils.HashDir(storagePath)
if err != nil {
return reporter.NewErrorEvent(
reporter.FailedHashPkg,
err,
fmt.Sprintf("failed to hash kclPkg - %s", dep.Name),
)
}
if err := c.ValidateDependency(dep); err != nil {
return err
}
return nil
}

// PushToOci will push a kcl package to oci registry.
func (c *KpmClient) PushToOci(localPath string, ociOpts *opt.OciOptions) error {
repoPath := utils.JoinPath(ociOpts.Reg, ociOpts.Repo)
Expand Down Expand Up @@ -1525,6 +1594,8 @@ func (c *KpmClient) pullTarFromOci(localPath string, ociOpts *opt.OciOptions) er
tagSelected = ociOpts.Tag
}

ociOpts.Tag = tagSelected

full_repo := utils.JoinPath(ociOpts.Reg, ociOpts.Repo)
reporter.ReportMsgTo(
fmt.Sprintf("pulling '%s:%s' from '%s'", ociOpts.Repo, tagSelected, full_repo),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
start to pull 'oci://localhost:5001/test/k8s'
the latest version '1.31.2' will be pulled
pulling '/test/k8s:1.31.2' from 'localhost:5001/test/k8s'
pulled 'oci://localhost:5001/test/k8s' in '<workspace>/localhost:5001/test/k8s' successfully
pulled 'oci://localhost:5001/test/k8s' in '<workspace>/localhost:5001/test/k8s/1.31.2' successfully
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
start to pull 'k8s'
the latest version '1.31.2' will be pulled
pulling 'test/k8s:1.31.2' from 'localhost:5001/test/k8s'
pulled 'k8s' in '<workspace>/localhost:5001/test/k8s' successfully
pulled 'k8s' in '<workspace>/localhost:5001/test/k8s/1.31.2' successfully
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
start to pull 'oci://localhost:5001/test/helloworld'
the latest version '0.1.2' will be pulled
pulling '/test/helloworld:0.1.2' from 'localhost:5001/test/helloworld'
pulled 'oci://localhost:5001/test/helloworld' in '<workspace>/localhost:5001/test/helloworld' successfully
pulled 'oci://localhost:5001/test/helloworld' in '<workspace>/localhost:5001/test/helloworld/0.1.2' successfully

0 comments on commit b9f3bda

Please sign in to comment.