Skip to content

Commit

Permalink
Merge pull request #31 from interlynk-io/fix/tool_github_method
Browse files Browse the repository at this point in the history
fix tools
  • Loading branch information
viveksahu26 authored Feb 10, 2025
2 parents 7b73b24 + 9279d00 commit a707c90
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
33 changes: 16 additions & 17 deletions pkg/source/github/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"io"
"os"
"path/filepath"
"time"

"github.com/interlynk-io/sbommv/pkg/iterator"
"github.com/interlynk-io/sbommv/pkg/logger"
Expand Down Expand Up @@ -124,39 +123,39 @@ func (it *GitHubIterator) fetchSBOMFromReleases(ctx *tcontext.TransferMetadata)
return nil
}

// Fetch SBOM by running a tool (Syft)
func (it *GitHubIterator) fetchSBOMFromTool(ctx *tcontext.TransferMetadata) error {
logger.LogDebug(ctx.Context, "Generating SBOM using tool", "repository", it.client.RepoURL)
logger.LogDebug(ctx.Context, "Generating SBOM using Tool", "repository", it.client.RepoURL)

// Clone the repository
repoDir := filepath.Join(os.TempDir(), fmt.Sprintf("sbommv_%d", time.Now().UnixNano()))
repoDir := filepath.Join(os.TempDir(), fmt.Sprintf("%s-%s", it.client.Repo, it.client.Version))
defer os.RemoveAll(repoDir)

if err := CloneRepoWithGit(ctx, it.client.RepoURL, repoDir); err != nil {
return fmt.Errorf("failed to clone repository: %w", err)
return fmt.Errorf("failed to clone the repository: %w", err)
}

// Generate SBOM
sbomFile, err := GenerateSBOM(ctx, repoDir, it.binaryPath)
// Generate SBOM and save in memory
sbomData, err := GenerateSBOM(ctx, repoDir, it.binaryPath)
if err != nil {
return fmt.Errorf("failed to generate SBOM: %w", err)
}

// Ensure the "sboms" directory exists
sbomDir := "sboms"
if err := os.MkdirAll(sbomDir, 0o755); err != nil {
return fmt.Errorf("failed to create SBOM output directory: %w", err)
sbomBytes, err := os.ReadFile(sbomData)
if err != nil {
return fmt.Errorf("failed to read SBOM: %w", err)
}

// Move SBOM to final location
sbomFilePath := fmt.Sprintf("%s/github_tool_sbom_%s.json", sbomDir, sanitizeRepoName(it.client.RepoURL))
if err := os.Rename(sbomFile, sbomFilePath); err != nil {
return fmt.Errorf("failed to move SBOM file: %w", err)
if len(sbomBytes) == 0 {
return fmt.Errorf("generate SBOM with zero file data: %w", err)
}

// store data
it.sboms = append(it.sboms, &iterator.SBOM{
Path: sbomFilePath,
Data: nil, // SBOM stored in file, no need for in-memory data
Path: "",
Data: sbomBytes,
Repo: fmt.Sprintf("%s/%s", it.client.Owner, it.client.Repo),
Version: it.client.Version,
})
logger.LogDebug(ctx.Context, "SBOM successfully stored in memory", "repository", it.client.RepoURL)
return nil
}
2 changes: 1 addition & 1 deletion pkg/source/github/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var SupportedTools = map[string]string{
}

func GenerateSBOM(ctx *tcontext.TransferMetadata, repoDir, binaryPath string) (string, error) {
logger.LogDebug(ctx.Context, "Initializing SBOM generation with Syft")
logger.LogDebug(ctx.Context, "Generating SBOM using Syft", "repo_dir", repoDir)

// Ensure Syft binary is executable
if err := os.Chmod(binaryPath, 0o755); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/target/interlynk/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (i *InterlynkAdapter) ParseAndValidateParams(cmd *cobra.Command) error {

token := viper.GetString("INTERLYNK_SECURITY_TOKEN")
if token == "" {
return fmt.Errorf("INTERLYNK_SECURITY_TOKEN environment variable is required")
return fmt.Errorf("missing INTERLYNK_SECURITY_TOKEN: authentication required")
}

if url == "" {
Expand All @@ -103,7 +103,7 @@ func (i *InterlynkAdapter) ParseAndValidateParams(cmd *cobra.Command) error {
return fmt.Errorf("Interlynk validation failed: %w", err)
}

logger.LogDebug(cmd.Context(), "Interlynk system is up and running.")
logger.LogDebug(cmd.Context(), "Interlynk system is up and running.")

return nil
}
Expand Down

0 comments on commit a707c90

Please sign in to comment.