@@ -92,7 +92,7 @@ func (options *ScanReleasedOptions) run(cmd *cobra.Command, _ []string) error {
9292 return err
9393 }
9494 }
95- _ , err := ScanReleased (options .Scan .OutputDirectory , options , log , verbose )
95+ _ , err := ScanReleased (& ctx , options .Scan .OutputDirectory , options , log , verbose )
9696 return err
9797}
9898
@@ -212,7 +212,7 @@ func (options *ScanAndCompareOptions) Run(cmd *cobra.Command, _ []string) error
212212 }
213213
214214 releasedScanOutDir := path .Join (outputDirectory , "released" )
215- releasedScanResults , err := ScanReleased (releasedScanOutDir , & options .Scan , log , verbose )
215+ releasedScanResults , err := ScanReleased (& ctx , releasedScanOutDir , & options .Scan , log , verbose )
216216 if err != nil {
217217 return err
218218 }
@@ -326,7 +326,7 @@ func ScanZarfYamlImages(zarfYamlScanOutDir string, options *CommonScanOptions, l
326326 return scanImagesResult , nil
327327}
328328
329- func ScanReleased (outDirectory string , options * ScanReleasedOptions , log * slog.Logger , verbose bool ) (map [string ]map [string ]string , error ) {
329+ func ScanReleased (ctx * context. Context , outDirectory string , options * ScanReleasedOptions , log * slog.Logger , verbose bool ) (map [string ]map [string ]string , error ) {
330330 log .Debug ("Scan command invoked" , slog .String ("zarfLocation" , options .Scan .ZarfYamlLocation ))
331331 pkg , err1 := parseZarfYaml (& options .Scan )
332332 sbomScanResults := make (map [string ]map [string ]string )
@@ -347,17 +347,16 @@ func ScanReleased(outDirectory string, options *ScanReleasedOptions, log *slog.L
347347 }
348348 encodedPrivateUrl := url .PathEscape (privateRepoUrl )
349349
350- ctx := context .Background ()
351- client := NewGithubClient (& ctx )
350+ client := NewGithubClient (ctx )
352351
353352 var packageUrls []string
354- if exists , err := checkPackageExistenceInRepo (client , & ctx , options .Fetch .RepoOwner , encodedPublicUrl , log ); err != nil {
353+ if exists , err := checkPackageExistenceInRepo (client , ctx , options .Fetch .RepoOwner , encodedPublicUrl , log ); err != nil {
355354 return sbomScanResults , fmt .Errorf ("failed to check package existence for URL: %s, %w" , encodedPublicUrl , err )
356355 } else if exists {
357356 log .Debug ("Package exists in public repo, adding it to fetch" , slog .String ("packageUrl" , publicRepoUrl ))
358357 packageUrls = append (packageUrls , publicRepoUrl )
359358 }
360- if exists , err := checkPackageExistenceInRepo (client , & ctx , options .Fetch .RepoOwner , encodedPrivateUrl , log ); err != nil {
359+ if exists , err := checkPackageExistenceInRepo (client , ctx , options .Fetch .RepoOwner , encodedPrivateUrl , log ); err != nil {
361360 return sbomScanResults , fmt .Errorf ("failed to check package existence for URL: %s, %w" , encodedPrivateUrl , err )
362361 } else if exists {
363362 log .Debug ("Package exists in private repo, adding it to fetch" , slog .String ("packageUrl" , privateRepoUrl ))
@@ -378,7 +377,7 @@ func ScanReleased(outDirectory string, options *ScanReleasedOptions, log *slog.L
378377 flavors := determineFlavors (& pkg )
379378 log .Debug ("Flavors" , slog .Any ("flavors" , flavors ))
380379
381- flavorToSboms , err := fetchSbomsForFlavors (& ctx , client , packageUrls , flavors , options .Fetch .RepoOwner , tempDir , log )
380+ flavorToSboms , err := fetchSbomsForFlavors (ctx , client , packageUrls , flavors , options .Fetch .RepoOwner , tempDir , log )
382381 if err != nil {
383382 return sbomScanResults , err
384383 }
@@ -425,9 +424,25 @@ func ScanReleased(outDirectory string, options *ScanReleasedOptions, log *slog.L
425424var NewGithubClient = createGithubClient
426425var FetchSboms = utils .FetchSboms
427426
427+ func getAuthToken () string {
428+ githubToken := os .Getenv ("GITHUB_TOKEN" )
429+ if githubToken != "" {
430+ return githubToken
431+ }
432+ return os .Getenv ("GITLAB_RELEASE_TOKEN" )
433+ }
434+
428435func createGithubClient (ctx * context.Context ) * github.Client {
436+ log := Logger (ctx )
437+ // GitHub REST API requires raw token, not base64-encoded
438+ token := getAuthToken ()
439+ if token == "" {
440+ log .Warn ("No GitHub token found in environment (GITHUB_TOKEN or GITLAB_RELEASE_TOKEN)" )
441+ } else {
442+ log .Debug ("GitHub token found for REST API" , slog .Int ("length" , len (token )))
443+ }
429444 ts := oauth2 .StaticTokenSource (
430- & oauth2.Token {AccessToken : utils . GetAuthToken () },
445+ & oauth2.Token {AccessToken : token },
431446 )
432447 tc := oauth2 .NewClient (* ctx , ts )
433448 return github .NewClient (tc )
@@ -515,7 +530,7 @@ func fetchSboms(tempDir string, tag string, repoOwner string, packageUrl string,
515530}
516531
517532func checkPackageExistenceInRepo (client * github.Client , ctx * context.Context , owner string , pkgUrl string , log * slog.Logger ) (bool , error ) {
518- log .Debug ("Checking if package %s exists in " , pkgUrl , owner )
533+ log .Debug ("Checking if package exists" , slog . String ( "url" , pkgUrl ), slog . String ( " owner" , owner ) )
519534 apiPath := fmt .Sprintf ("/orgs/%s/packages/container/%s" , owner , pkgUrl )
520535 req , err := client .NewRequest ("GET" , apiPath , nil )
521536 if err != nil {
0 commit comments