minimal implementation for pmg

This commit is contained in:
Sahilb315
2025-04-09 23:27:46 +05:30
parent 4805f443f5
commit ed8f9b6d53
13 changed files with 188 additions and 68 deletions
+5 -15
View File
@@ -1,13 +1,12 @@
package common
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/safedep/dry/crypto"
"github.com/safedep/pmg/pkg/common/utils"
)
// ExtractorOptions holds configuration for running an extractor script
@@ -21,10 +20,9 @@ type ExtractorOptions struct {
// RunExtractor extracts an embedded script to a temp file and executes it
func RunPkgExtractor(opts ExtractorOptions) (string, error) {
interpreterPath, err := exec.LookPath(opts.Interpreter)
interpreterPath, err := utils.GetInterpreterPath(opts.Interpreter)
if err != nil {
return "", fmt.Errorf("interpreter '%s' not found in PATH: %s",
opts.Interpreter, err.Error())
return "", err
}
// Create a temporary file for the embedded script
@@ -52,16 +50,8 @@ func RunPkgExtractor(opts ExtractorOptions) (string, error) {
// Build the command with all arguments
cmdArgs := append([]string{scriptFile.Name(), opts.PackageName, outputFile}, opts.Args...)
cmd := exec.Command(interpreterPath, cmdArgs...)
// Capture both stdout and stderr
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("error running extractor: %s\nStderr: %s",
err.Error(), stderr.String())
if err = utils.ExecCmd(interpreterPath, cmdArgs); err != nil {
return "", err
}
return outputFile, nil
+3 -3
View File
@@ -3,15 +3,15 @@ package common
import (
"fmt"
"net/http"
"os"
drygrpc "github.com/safedep/dry/adapters/grpc"
"github.com/safedep/pmg/pkg/common/utils"
"google.golang.org/grpc"
)
func NewCloudClientConnection() (*grpc.ClientConn, error) {
tok := os.Getenv("SAFEDEP_API_KEY")
tenantId := os.Getenv("SAFEDEP_TENANT_ID")
tok := utils.ApiKey()
tenantId := utils.TenantDomain()
if tok == "" || tenantId == "" {
return nil, fmt.Errorf("SAFEDEP_API_KEY and SAFEDEP_TENANT_ID must be set")
}
+21
View File
@@ -0,0 +1,21 @@
package utils
import (
"bytes"
"fmt"
"os/exec"
)
func ExecCmd(name string, args []string) error {
cmd := exec.Command(name, args...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("error running cmd %s: %s\nStderr: %s", name,
err.Error(), stderr.String())
}
return nil
}
+11
View File
@@ -0,0 +1,11 @@
package utils
import "os"
func ApiKey() string {
return os.Getenv("SAFEDEP_API_KEY")
}
func TenantDomain() string {
return os.Getenv("SAFEDEP_TENANT_ID")
}
+14
View File
@@ -0,0 +1,14 @@
package utils
import (
"fmt"
"os/exec"
)
func GetInterpreterPath(name string) (string, error) {
path, err := exec.LookPath(name)
if err != nil {
return "", fmt.Errorf("interpreter '%s' not found in PATH: %s", name, err.Error())
}
return path, nil
}
+30
View File
@@ -0,0 +1,30 @@
package utils
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/safedep/dry/log"
)
func ConfirmInstallation(maliciousPkgs map[string]string) bool {
fmt.Printf("\nWARNING: %d potentially malicious packages detected!\n", len(maliciousPkgs))
fmt.Println("The following packages have been flagged:")
for name, reason := range maliciousPkgs {
fmt.Printf("- %s: %s\n", name, reason)
}
fmt.Print("\nDo you want to continue with installation? (y/N): ")
reader := bufio.NewReader(os.Stdin)
response, err := reader.ReadString('\n')
if err != nil {
log.Errorf("Failed to read user input: %v", err)
return false
}
response = strings.ToLower(strings.TrimSpace(response))
return response == "y" || response == "yes"
}