Skip to content

Commit

Permalink
add root command to list unverified upstream sources
Browse files Browse the repository at this point in the history
This new root command is part of the `stest` and is
designed to list all upstream sources with the `skip-check`
flag set to `true`.
- If `-p <package>` is specified, it lists unverified
sources for the specified package.
- Otherwise, it lists all unverified upstream sources
in the repository.

The output is written to:
`/dest/code.arista.io/eos/eext/{rep}/{package}.unverifiedSources.json`.

This file will be included in the Barney snapshot build,
enabling better tracking of unverified sources.
  • Loading branch information
manishk-arista committed Dec 19, 2024
1 parent 2f083e0 commit 59cdbba
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 0 deletions.
30 changes: 30 additions & 0 deletions cmd/list_unverified_sources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2022 Arista Networks, Inc. All rights reserved.
// Arista Networks, Inc. Confidential and Proprietary.

package cmd

import (
"code.arista.io/eos/tools/eext/impl"
"github.com/spf13/cobra"
)

// listUnverifiedSourcescmd represents the list-unverified-sources command
var listUnverifiedSourcescmd = &cobra.Command{
Use: "list-unverified-sources",
Short: "list unverified upstream sources",
Long: `Checks for the upstream sources within package which don't
have a valid signature check i.e, skip-check flag is true
and generates content hash for the upstream sources.`,
RunE: func(cmd *cobra.Command, args []string) error {
repo, _ := cmd.Flags().GetString("repo")
pkg, _ := cmd.Flags().GetString("package")
err := impl.ListUnverifiedSources(repo, pkg)
return err
},
}

func init() {
listUnverifiedSourcescmd.Flags().StringP("repo", "r", "", "Repository name (OPTIONAL)")
listUnverifiedSourcescmd.Flags().StringP("package", "p", "", "specify package name (OPTIONAL)")
rootCmd.AddCommand(listUnverifiedSourcescmd)
}
55 changes: 55 additions & 0 deletions impl/list_unverified_sources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) 2022 Arista Networks, Inc. All rights reserved.
// Arista Networks, Inc. Confidential and Proprietary.

package impl

import (
"fmt"

"code.arista.io/eos/tools/eext/manifest"
"gopkg.in/yaml.v3"
)

// fetch upstream sources from manifest
func fetchUpstreamSrcsWithSkipCheck(upstreamSrcManifest []manifest.UpstreamSrc) []manifest.UpstreamSrc {
upstreamSrcs := []manifest.UpstreamSrc{}

for _, upstreamSrcFromManifest := range upstreamSrcManifest {
if upstreamSrcFromManifest.Signature.SkipCheck {
upstreamSrcs = append(upstreamSrcs, upstreamSrcFromManifest)
}
}

return upstreamSrcs
}

// ListUnverifiedSources lists all the upstream sources within a package
// which do not have valid signature check. For The upstream sources with
// `skip-check` flag as true content hash is generated
func ListUnverifiedSources(repo string, pkg string) error {
repoManifest, loadManifestErr := manifest.LoadManifest(repo)
if loadManifestErr != nil {
return loadManifestErr
}

checkAllPackages := pkg == "" // if pkg name has not been specified, check all
upstreamSources := []manifest.UpstreamSrc{}

for _, pkgSpec := range repoManifest.Package {
pkgName := pkgSpec.Name
if !checkAllPackages && pkgName != pkg {
continue
}
upstreamSources = append(upstreamSources, fetchUpstreamSrcsWithSkipCheck(pkgSpec.UpstreamSrc)...)
}

if len(upstreamSources) != 0 {
yamlUpstreamSources, err := yaml.Marshal(upstreamSources)
if err != nil {
return err
}
fmt.Println(string(yamlUpstreamSources))
}

return nil
}
62 changes: 62 additions & 0 deletions impl/list_unverified_sources_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) 2023 Arista Networks, Inc. All rights reserved.
// Arista Networks, Inc. Confidential and Proprietary.

//go:build containerized

package impl

import (
"bytes"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

func TestListUnverifiedSources(t *testing.T) {
curPath, _ := os.Getwd()
repo := filepath.Join(curPath, "testData/unverified-src")

testpkgs := map[string]string{
"foo1": "",
"foo2": `- source-bundle:
name: srpm
override:
version: 1.7.7-1.fc40
src-suffix: ""
sig-suffix: ""
full-url: ""
git:
url: ""
revision: ""
signature:
skip-check: true
detached-sig:
full-url: ""
public-key: ""
on-uncompressed: false
`,
}

var r, w, rescueStdout *(os.File)
var buffer bytes.Buffer

for pkg, outputExpected := range testpkgs {
rescueStdout = os.Stdout
r, w, _ = os.Pipe()
os.Stdout = w

ListUnverifiedSources(repo, pkg)

w.Close()
buffer.ReadFrom(r)
outputGot := buffer.String()
os.Stdout = rescueStdout

require.Equal(t, outputExpected, outputGot)
}

t.Log("TestListUnverifiedSources test Passed")
}
25 changes: 25 additions & 0 deletions impl/testData/unverified-src/eext.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
package:
- name: foo1
upstream-sources:
- source-bundle:
name: srpm
override:
version: 1.7.7-1.fc40
type: srpm
build:
repo-bundle:
- name: el9

- name: foo2
upstream-sources:
- source-bundle:
name: srpm
override:
version: 1.7.7-1.fc40
signature:
skip-check: true
type: srpm
build:
repo-bundle:
- name: el9

0 comments on commit 59cdbba

Please sign in to comment.