2026-06-01 15:32:22 +05:30
|
|
|
package analyzer
|
|
|
|
|
|
|
|
|
|
import (
|
2026-06-10 13:42:09 +05:30
|
|
|
"github.com/safedep/dry/cloud"
|
2026-06-01 15:32:22 +05:30
|
|
|
"github.com/safedep/dry/log"
|
|
|
|
|
"github.com/safedep/pmg/internal/cloudauth"
|
|
|
|
|
)
|
|
|
|
|
|
2026-06-10 13:42:09 +05:30
|
|
|
type credentialsResolver func() (*cloud.Credentials, func() error, error)
|
|
|
|
|
|
2026-06-01 15:32:22 +05:30
|
|
|
// NewMalysisAnalyzer creates the malysis query analyzer best suited for the
|
|
|
|
|
// current environment. When SafeDep Cloud credentials are available (via
|
|
|
|
|
// keychain or environment), it returns an authenticated analyzer that queries
|
2026-06-10 13:42:09 +05:30
|
|
|
// api.safedep.io and honors tenant-specific package exclusions, degrading to
|
|
|
|
|
// the unauthenticated community analyzer if the API rejects the credentials.
|
|
|
|
|
// When no credentials are available, it returns the community analyzer.
|
2026-06-01 15:32:22 +05:30
|
|
|
func NewMalysisAnalyzer(config MalysisQueryAnalyzerConfig) (PackageVersionAnalyzer, error) {
|
2026-06-10 13:42:09 +05:30
|
|
|
return newMalysisAnalyzer(config, cloudauth.ResolveCredentials)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newMalysisAnalyzer(config MalysisQueryAnalyzerConfig,
|
|
|
|
|
resolveCredentials credentialsResolver) (PackageVersionAnalyzer, error) {
|
|
|
|
|
community, err := NewMalysisQueryAnalyzer(config)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
creds, closeResolver, err := resolveCredentials()
|
2026-06-01 15:32:22 +05:30
|
|
|
if err != nil {
|
|
|
|
|
log.Debugf("SafeDep Cloud credentials unavailable, using community malysis analyzer: %v", err)
|
2026-06-22 10:12:02 +05:30
|
|
|
return withCache(community, config), nil
|
2026-06-01 15:32:22 +05:30
|
|
|
}
|
|
|
|
|
defer func() {
|
|
|
|
|
if closeErr := closeResolver(); closeErr != nil {
|
|
|
|
|
log.Warnf("failed to close credential resolver: %v", closeErr)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2026-06-10 13:42:09 +05:30
|
|
|
authenticated, err := NewMalysisAuthenticatedQueryAnalyzer(config, creds)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Debugf("SafeDep Cloud credentials found, using authenticated malysis analyzer with community fallback")
|
2026-06-22 10:12:02 +05:30
|
|
|
return withCache(newMalysisFallbackAnalyzer(authenticated, community), config), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// withCache wraps a in a read-through cache decorator when one is configured.
|
|
|
|
|
func withCache(a PackageVersionAnalyzer, config MalysisQueryAnalyzerConfig) PackageVersionAnalyzer {
|
|
|
|
|
if config.Cache == nil {
|
|
|
|
|
return a
|
|
|
|
|
}
|
|
|
|
|
return newMalysisCachingAnalyzer(a, config.Cache)
|
2026-06-01 15:32:22 +05:30
|
|
|
}
|