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

root command to list unverified upstream sources #148

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions cmd/list_unverified_sources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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 return prints the upstreamSrc
to stdout.`,
Args: cobra.ExactArgs(0),
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 (REQUIRED)")
listUnverifiedSourcesCmd.MarkFlagRequired("package")
rootCmd.AddCommand(listUnverifiedSourcesCmd)
}
58 changes: 58 additions & 0 deletions impl/list_unverified_sources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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 getUpstreamSrcsWithSkipCheck(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
manishk-arista marked this conversation as resolved.
Show resolved Hide resolved
// which do not have valid signature check.
func ListUnverifiedSources(repo string, pkg string) error {
repoManifest, loadManifestErr := manifest.LoadManifest(repo)
if loadManifestErr != nil {
return loadManifestErr
}

upstreamSources := []manifest.UpstreamSrc{}
pkgFound := false
for _, pkgSpec := range repoManifest.Package {
pkgName := pkgSpec.Name
if pkgName != pkg {
continue
}
pkgFound = true
upstreamSources = getUpstreamSrcsWithSkipCheck(pkgSpec.UpstreamSrc)
break
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

add a break ?


if !pkgFound {
return fmt.Errorf("impl.ListUnVerifiedSources: '%s' package is not part of this repo", pkg)
}

if len(upstreamSources) != 0 {
yamlUpstreamSources, err := yaml.Marshal(upstreamSources)
if err != nil {
return fmt.Errorf("impl.ListUnVerifiedSources: '%s' unmarshaling yaml", err)
}
fmt.Println(string(yamlUpstreamSources))
}
return nil
}
70 changes: 70 additions & 0 deletions impl/list_unverified_sources_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) 2023 Arista Networks, Inc. All rights reserved.
// Arista Networks, Inc. Confidential and Proprietary.

package impl

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

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

func TestListUnverifiedSources(t *testing.T) {
manishk-arista marked this conversation as resolved.
Show resolved Hide resolved
cwd, _ := os.Getwd()
repo := filepath.Join(cwd, "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")
}

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

err := ListUnverifiedSources(repo, "foo3")
require.NotEqual(t, nil, err)

t.Log("TestListUnverifiedSourcesFail 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
Loading