Skip to content

Commit

Permalink
feat: support a package type "cargo"
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed May 18, 2023
1 parent 4f1de57 commit 3c4c1d4
Show file tree
Hide file tree
Showing 20 changed files with 164 additions and 34 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/wc-integration-test.yaml
Expand Up @@ -58,6 +58,9 @@ jobs:
- run: github-compare -v
- run: terrafmt version

- name: Test the package type "cargo"
run: sk --version

- name: test cosign
run: aqua i
working-directory: tests/cosign
Expand Down
1 change: 1 addition & 0 deletions pkg/config/aqua/config.go
Expand Up @@ -62,6 +62,7 @@ const (
PkgInfoTypeGitHubArchive = "github_archive"
PkgInfoTypeHTTP = "http"
PkgInfoTypeGoInstall = "go_install"
PkgInfoTypeCargo = "cargo"
)

func (registries *Registries) UnmarshalYAML(unmarshal func(interface{}) error) error {
Expand Down
7 changes: 7 additions & 0 deletions pkg/config/package.go
Expand Up @@ -143,6 +143,9 @@ func (cpkg *Package) GetFileSrc(file *registry.File, rt *runtime.Runtime) (strin

func (cpkg *Package) getFileSrc(file *registry.File, rt *runtime.Runtime) (string, error) {
pkgInfo := cpkg.PackageInfo
if pkgInfo.Type == "cargo" {
return filepath.Join("bin", file.Name), nil
}
assetName, err := cpkg.RenderAsset(rt)
if err != nil {
return "", fmt.Errorf("render the asset name: %w", err)
Expand All @@ -166,6 +169,7 @@ const (
PkgInfoTypeGitHubArchive = "github_archive"
PkgInfoTypeHTTP = "http"
PkgInfoTypeGoInstall = "go_install"
PkgInfoTypeCargo = "cargo"
)

type Param struct {
Expand Down Expand Up @@ -344,6 +348,9 @@ func (cpkg *Package) GetPkgPath(rootDir string, rt *runtime.Runtime) (string, er
return "", fmt.Errorf("render Go Module Path: %w", err)
}
return filepath.Join(rootDir, "pkgs", pkgInfo.GetType(), p, pkg.Version, "bin"), nil
case PkgInfoTypeCargo:
registry := "crates.io"
return filepath.Join(rootDir, "pkgs", pkgInfo.GetType(), registry, *pkgInfo.Crate, pkg.Version), nil
case PkgInfoTypeGitHubContent, PkgInfoTypeGitHubRelease:
if pkgInfo.RepoOwner == "aquaproj" && (pkgInfo.RepoName == "aqua" || pkgInfo.RepoName == "aqua-proxy") {
return filepath.Join(rootDir, "internal", "pkgs", pkgInfo.GetType(), "github.com", pkgInfo.RepoOwner, pkgInfo.RepoName, pkg.Version, assetName), nil
Expand Down
1 change: 1 addition & 0 deletions pkg/config/registry/error.go
Expand Up @@ -7,6 +7,7 @@ var (
errRepoRequired = errors.New("repo_owner and repo_name are required")
errGitHubContentRequirePath = errors.New("github_content package requires path")
errGoInstallRequirePath = errors.New("go_install package requires path")
errCargoRequireCrate = errors.New("cargo package requires crate")
errAssetRequired = errors.New("github_release package requires asset")
errURLRequired = errors.New("http package requires url")
errInvalidPackageType = errors.New("package type is invalid")
Expand Down
30 changes: 30 additions & 0 deletions pkg/config/registry/package_info.go
Expand Up @@ -16,6 +16,7 @@ const (
PkgInfoTypeGitHubArchive = "github_archive"
PkgInfoTypeHTTP = "http"
PkgInfoTypeGoInstall = "go_install"
PkgInfoTypeCargo = "cargo"
)

type PackageInfo struct {
Expand All @@ -28,6 +29,7 @@ type PackageInfo struct {
Description string `json:"description,omitempty" yaml:",omitempty"`
Link string `json:"link,omitempty" yaml:",omitempty"`
Asset *string `json:"asset,omitempty" yaml:",omitempty"`
Crate *string `json:"crate,omitempty" yaml:",omitempty"`
URL *string `json:"url,omitempty" yaml:",omitempty"`
Path *string `json:"path,omitempty" yaml:",omitempty"`
Format string `json:"format,omitempty" jsonschema:"example=tar.gz,example=raw,example=zip,example=dmg" yaml:",omitempty"`
Expand Down Expand Up @@ -59,6 +61,7 @@ func (pkgInfo *PackageInfo) Copy() *PackageInfo {
RepoOwner: pkgInfo.RepoOwner,
RepoName: pkgInfo.RepoName,
Asset: pkgInfo.Asset,
Crate: pkgInfo.Crate,
Path: pkgInfo.Path,
Format: pkgInfo.Format,
Files: pkgInfo.Files,
Expand Down Expand Up @@ -93,20 +96,34 @@ func (pkgInfo *PackageInfo) resetByPkgType(typ string) {
case PkgInfoTypeGitHubRelease:
pkgInfo.URL = nil
pkgInfo.Path = nil
pkgInfo.Crate = nil
case PkgInfoTypeGitHubContent:
pkgInfo.URL = nil
pkgInfo.Asset = nil
pkgInfo.Crate = nil
case PkgInfoTypeGitHubArchive:
pkgInfo.URL = nil
pkgInfo.Path = nil
pkgInfo.Asset = nil
pkgInfo.Crate = nil
pkgInfo.Format = ""
case PkgInfoTypeHTTP:
pkgInfo.Path = nil
pkgInfo.Asset = nil
case PkgInfoTypeGoInstall:
pkgInfo.URL = nil
pkgInfo.Asset = nil
pkgInfo.Crate = nil
pkgInfo.WindowsExt = ""
pkgInfo.CompleteWindowsExt = nil
pkgInfo.Cosign = nil
pkgInfo.SLSAProvenance = nil
pkgInfo.Format = ""
pkgInfo.Rosetta2 = nil
case PkgInfoTypeCargo:
pkgInfo.URL = nil
pkgInfo.Asset = nil
pkgInfo.Path = nil
pkgInfo.WindowsExt = ""
pkgInfo.CompleteWindowsExt = nil
pkgInfo.Cosign = nil
Expand All @@ -131,6 +148,9 @@ func (pkgInfo *PackageInfo) overrideVersion(child *VersionOverride) *PackageInfo
if child.Asset != nil {
pkg.Asset = child.Asset
}
if child.Crate != nil {
pkg.Crate = child.Crate
}
if child.Path != nil {
pkg.Path = child.Path
}
Expand Down Expand Up @@ -230,6 +250,10 @@ func (pkgInfo *PackageInfo) OverrideByRuntime(rt *runtime.Runtime) { //nolint:cy
pkgInfo.Asset = ov.Asset
}

if ov.Crate != nil {
pkgInfo.Crate = ov.Crate
}

if ov.Files != nil {
pkgInfo.Files = ov.Files
}
Expand Down Expand Up @@ -262,6 +286,7 @@ type VersionOverride struct {
RepoOwner string `yaml:"repo_owner,omitempty" json:"repo_owner,omitempty"`
RepoName string `yaml:"repo_name,omitempty" json:"repo_name,omitempty"`
Asset *string `yaml:",omitempty" json:"asset,omitempty"`
Crate *string `json:"crate,omitempty" yaml:",omitempty"`
Path *string `yaml:",omitempty" json:"path,omitempty"`
URL *string `yaml:",omitempty" json:"url,omitempty"`
Files []*File `yaml:",omitempty" json:"files,omitempty"`
Expand Down Expand Up @@ -443,6 +468,11 @@ func (pkgInfo *PackageInfo) Validate() error { //nolint:cyclop
return errGoInstallRequirePath
}
return nil
case PkgInfoTypeCargo:
if pkgInfo.Crate == nil {
return errCargoRequireCrate
}
return nil
case PkgInfoTypeGitHubContent:
if !pkgInfo.HasRepo() {
return errRepoRequired
Expand Down
1 change: 1 addition & 0 deletions pkg/config/registry/replacements.go
Expand Up @@ -10,6 +10,7 @@ type Override struct {
Type string `json:"type,omitempty" jsonschema:"enum=github_release,enum=github_content,enum=github_archive,enum=http,enum=go,enum=go_install"`
Format string `yaml:",omitempty" json:"format,omitempty" jsonschema:"example=tar.gz,example=raw,example=zip"`
Asset *string `yaml:",omitempty" json:"asset,omitempty"`
Crate *string `json:"crate,omitempty" yaml:",omitempty"`
Files []*File `yaml:",omitempty" json:"files,omitempty"`
URL *string `yaml:",omitempty" json:"url,omitempty"`
CompleteWindowsExt *bool `json:"complete_windows_ext,omitempty" yaml:"complete_windows_ext,omitempty"`
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/exec/exec_test.go
Expand Up @@ -152,7 +152,7 @@ packages:
whichCtrl := which.New(d.param, finder.NewConfigFinder(fs), reader.New(fs, d.param), registry.New(d.param, ghDownloader, fs, d.rt, &cosign.MockVerifier{}, &slsa.MockVerifier{}), d.rt, osEnv, fs, linker)
downloader := download.NewDownloader(nil, download.NewHTTPDownloader(http.DefaultClient))
executor := &exec.Mock{}
pkgInstaller := installpackage.New(d.param, downloader, d.rt, fs, linker, nil, &checksum.Calculator{}, unarchive.New(executor, fs), &policy.Checker{}, &cosign.MockVerifier{}, &slsa.MockVerifier{}, &installpackage.MockGoInstallInstaller{})
pkgInstaller := installpackage.New(d.param, downloader, d.rt, fs, linker, nil, &checksum.Calculator{}, unarchive.New(executor, fs), &policy.Checker{}, &cosign.MockVerifier{}, &slsa.MockVerifier{}, &installpackage.MockGoInstallInstaller{}, &installpackage.MockCargoPackageInstaller{})
policyFinder := policy.NewConfigFinder(fs)
ctrl := execCtrl.New(d.param, pkgInstaller, whichCtrl, executor, osEnv, fs, policy.NewReader(fs, policy.NewValidator(d.param, fs), policyFinder, policy.NewConfigReader(fs)), policyFinder)
if err := ctrl.Exec(ctx, logE, d.param, d.exeName, d.args...); err != nil {
Expand Down Expand Up @@ -248,7 +248,7 @@ packages:
whichCtrl := which.New(d.param, finder.NewConfigFinder(fs), reader.New(fs, d.param), registry.New(d.param, ghDownloader, afero.NewOsFs(), d.rt, &cosign.MockVerifier{}, &slsa.MockVerifier{}), d.rt, osEnv, fs, linker)
downloader := download.NewDownloader(nil, download.NewHTTPDownloader(http.DefaultClient))
executor := &exec.Mock{}
pkgInstaller := installpackage.New(d.param, downloader, d.rt, fs, linker, nil, &checksum.Calculator{}, unarchive.New(executor, fs), &policy.Checker{}, &cosign.MockVerifier{}, &slsa.MockVerifier{}, &installpackage.MockGoInstallInstaller{})
pkgInstaller := installpackage.New(d.param, downloader, d.rt, fs, linker, nil, &checksum.Calculator{}, unarchive.New(executor, fs), &policy.Checker{}, &cosign.MockVerifier{}, &slsa.MockVerifier{}, &installpackage.MockGoInstallInstaller{}, &installpackage.MockCargoPackageInstaller{})
ctrl := execCtrl.New(d.param, pkgInstaller, whichCtrl, executor, osEnv, fs, &policy.MockReader{}, policy.NewConfigFinder(fs))
b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/install/install_test.go
Expand Up @@ -103,7 +103,7 @@ packages:
}
downloader := download.NewDownloader(nil, download.NewHTTPDownloader(http.DefaultClient))
executor := &exec.Mock{}
pkgInstaller := installpackage.New(d.param, downloader, d.rt, fs, linker, nil, &checksum.Calculator{}, unarchive.New(executor, fs), &policy.Checker{}, &cosign.MockVerifier{}, &slsa.MockVerifier{}, &installpackage.MockGoInstallInstaller{})
pkgInstaller := installpackage.New(d.param, downloader, d.rt, fs, linker, nil, &checksum.Calculator{}, unarchive.New(executor, fs), &policy.Checker{}, &cosign.MockVerifier{}, &slsa.MockVerifier{}, &installpackage.MockGoInstallInstaller{}, &installpackage.MockCargoPackageInstaller{})
policyFinder := policy.NewConfigFinder(fs)
policyReader := policy.NewReader(fs, &policy.MockValidator{}, policyFinder, policy.NewConfigReader(fs))
ctrl := install.New(d.param, finder.NewConfigFinder(fs), reader.New(fs, d.param), registry.New(d.param, registryDownloader, fs, d.rt, &cosign.MockVerifier{}, &slsa.MockVerifier{}), pkgInstaller, fs, d.rt, policyReader, policyFinder)
Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/updatechecksum/update.go
Expand Up @@ -181,6 +181,10 @@ func (ctrl *Controller) getChecksums(ctx context.Context, logE *logrus.Entry, ch
logE.Debug("skip updating go_install package's checksum")
return nil
}
if pkg.PackageInfo.Type == "cargo" {
logE.Debug("skip updating cargo package's checksum")
return nil
}
logE.Info("updating a package checksum")
rts, err := checksum.GetRuntimesFromSupportedEnvs(supportedEnvs, pkg.PackageInfo.SupportedEnvs)
if err != nil {
Expand Down
16 changes: 16 additions & 0 deletions pkg/controller/wire.go
Expand Up @@ -276,6 +276,10 @@ func InitializeInstallCommandController(ctx context.Context, param *config.Param
installpackage.NewGoInstallInstallerImpl,
wire.Bind(new(installpackage.GoInstallInstaller), new(*installpackage.GoInstallInstallerImpl)),
),
wire.NewSet(
installpackage.NewCargoPackageInstallerImpl,
wire.Bind(new(installpackage.CargoPackageInstaller), new(*installpackage.CargoPackageInstallerImpl)),
),
)
return &install.Controller{}
}
Expand Down Expand Up @@ -432,6 +436,10 @@ func InitializeExecCommandController(ctx context.Context, param *config.Param, h
installpackage.NewGoInstallInstallerImpl,
wire.Bind(new(installpackage.GoInstallInstaller), new(*installpackage.GoInstallInstallerImpl)),
),
wire.NewSet(
installpackage.NewCargoPackageInstallerImpl,
wire.Bind(new(installpackage.CargoPackageInstaller), new(*installpackage.CargoPackageInstallerImpl)),
),
)
return &cexec.Controller{}
}
Expand Down Expand Up @@ -493,6 +501,10 @@ func InitializeUpdateAquaCommandController(ctx context.Context, param *config.Pa
installpackage.NewGoInstallInstallerImpl,
wire.Bind(new(installpackage.GoInstallInstaller), new(*installpackage.GoInstallInstallerImpl)),
),
wire.NewSet(
installpackage.NewCargoPackageInstallerImpl,
wire.Bind(new(installpackage.CargoPackageInstaller), new(*installpackage.CargoPackageInstallerImpl)),
),
policy.NewChecker,
)
return &updateaqua.Controller{}
Expand Down Expand Up @@ -600,6 +612,10 @@ func InitializeCopyCommandController(ctx context.Context, param *config.Param, h
installpackage.NewGoInstallInstallerImpl,
wire.Bind(new(installpackage.GoInstallInstaller), new(*installpackage.GoInstallInstallerImpl)),
),
wire.NewSet(
installpackage.NewCargoPackageInstallerImpl,
wire.Bind(new(installpackage.CargoPackageInstaller), new(*installpackage.CargoPackageInstallerImpl)),
),
)
return &cp.Controller{}
}
Expand Down
12 changes: 8 additions & 4 deletions pkg/controller/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/installpackage/aqua_test.go
Expand Up @@ -69,7 +69,7 @@ e922723678f493216c2398f3f23fb027c9a98808b49f6fce401ef82ee2c22b03 aqua_linux_arm
}
ctrl := installpackage.New(d.param, &download.Mock{
RC: io.NopCloser(strings.NewReader("xxx")),
}, d.rt, fs, domain.NewMockLinker(fs), d.checksumDownloader, d.checksumCalculator, &unarchive.MockUnarchiver{}, &policy.Checker{}, &cosign.MockVerifier{}, &slsa.MockVerifier{}, &installpackage.MockGoInstallInstaller{})
}, d.rt, fs, domain.NewMockLinker(fs), d.checksumDownloader, d.checksumCalculator, &unarchive.MockUnarchiver{}, &policy.Checker{}, &cosign.MockVerifier{}, &slsa.MockVerifier{}, &installpackage.MockGoInstallInstaller{}, &installpackage.MockCargoPackageInstaller{})
if err := ctrl.InstallAqua(ctx, logE, d.version); err != nil {
if d.isErr {
return
Expand Down
36 changes: 36 additions & 0 deletions pkg/installpackage/cargo.go
@@ -0,0 +1,36 @@
package installpackage

import (
"context"
"fmt"
)

type CargoPackageInstaller interface {
Install(ctx context.Context, crate, version, root string) error
}

type MockCargoPackageInstaller struct {
Err error
}

func (mock *MockCargoPackageInstaller) Install(ctx context.Context, crate, version, root string) error {
return mock.Err
}

type CargoPackageInstallerImpl struct {
exec Executor
}

func NewCargoPackageInstallerImpl(exec Executor) *CargoPackageInstallerImpl {
return &CargoPackageInstallerImpl{
exec: exec,
}
}

func (inst *CargoPackageInstallerImpl) Install(ctx context.Context, crate, version, root string) error {
_, err := inst.exec.Exec(ctx, "cargo", "install", "--version", version, "--root", root, crate)
if err != nil {
return fmt.Errorf("install a crate: %w", err)
}
return nil
}
14 changes: 14 additions & 0 deletions pkg/installpackage/download.go
Expand Up @@ -63,6 +63,10 @@ func (inst *InstallerImpl) download(ctx context.Context, logE *logrus.Entry, par
return inst.downloadGoInstall(ctx, ppkg, param.Dest, logE)
}

if pkgInfo.Type == "cargo" {
return inst.downloadCargo(ctx, logE, ppkg, param.Dest)
}

logE.Info("download and unarchive the package")

file, err := download.ConvertPackageToFile(ppkg, param.Asset, inst.runtime)
Expand Down Expand Up @@ -192,3 +196,13 @@ func (inst *InstallerImpl) downloadGoInstall(ctx context.Context, pkg *config.Pa
}
return nil
}

func (inst *InstallerImpl) downloadCargo(ctx context.Context, logE *logrus.Entry, pkg *config.Package, root string) error {
logE.Info("Installing a crate")
crate := *pkg.PackageInfo.Crate
version := pkg.Package.Version
if err := inst.cargoPackageInstaller.Install(ctx, crate, version, root); err != nil {
return fmt.Errorf("cargo install: %w", err)
}
return nil
}

0 comments on commit 3c4c1d4

Please sign in to comment.