-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fae5ff3
commit e52bb08
Showing
8 changed files
with
343 additions
and
288 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2020 Security Scorecard Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package cmd implements Scorecard commandline. | ||
package cmd | ||
|
||
var ( | ||
flagRepo string | ||
flagLocal string | ||
flagChecksToRun []string | ||
flagMetadata []string | ||
flagLogLevel string | ||
flagFormat string | ||
flagNPM string | ||
flagPyPI string | ||
flagRubyGems string | ||
flagShowDetails bool | ||
flagPolicyFile string | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
// Copyright 2020 Security Scorecard Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package cmd implements Scorecard commandline. | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
sce "github.com/ossf/scorecard/v4/errors" | ||
) | ||
|
||
type packageMangerResponse struct { | ||
associatedRepo string | ||
exists bool | ||
} | ||
|
||
// TODO(#1568): Add unit tests for this. | ||
func fetchGitRepositoryFromPackageManagers(npm, pypi, rubygems string) (packageMangerResponse, error) { | ||
if npm != "" { | ||
gitRepo, err := fetchGitRepositoryFromNPM(npm) | ||
return packageMangerResponse{ | ||
exists: true, | ||
associatedRepo: gitRepo, | ||
}, err | ||
} | ||
if pypi != "" { | ||
gitRepo, err := fetchGitRepositoryFromPYPI(pypi) | ||
return packageMangerResponse{ | ||
exists: true, | ||
associatedRepo: gitRepo, | ||
}, err | ||
} | ||
if rubygems != "" { | ||
gitRepo, err := fetchGitRepositoryFromRubyGems(rubygems) | ||
return packageMangerResponse{ | ||
exists: true, | ||
associatedRepo: gitRepo, | ||
}, err | ||
} | ||
|
||
return packageMangerResponse{}, nil | ||
} | ||
|
||
type npmSearchResults struct { | ||
Objects []struct { | ||
Package struct { | ||
Links struct { | ||
Repository string `json:"repository"` | ||
} `json:"links"` | ||
} `json:"package"` | ||
} `json:"objects"` | ||
} | ||
|
||
type pypiSearchResults struct { | ||
Info struct { | ||
ProjectUrls struct { | ||
Source string `json:"Source"` | ||
} `json:"project_urls"` | ||
} `json:"info"` | ||
} | ||
|
||
type rubyGemsSearchResults struct { | ||
SourceCodeURI string `json:"source_code_uri"` | ||
} | ||
|
||
// Gets the GitHub repository URL for the npm package. | ||
// nolint: noctx | ||
func fetchGitRepositoryFromNPM(packageName string) (string, error) { | ||
npmSearchURL := "https://registry.npmjs.org/-/v1/search?text=%s&size=1" | ||
const timeout = 10 | ||
client := &http.Client{ | ||
Timeout: timeout * time.Second, | ||
} | ||
resp, err := client.Get(fmt.Sprintf(npmSearchURL, packageName)) | ||
if err != nil { | ||
return "", sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("failed to get npm package json: %v", err)) | ||
} | ||
|
||
defer resp.Body.Close() | ||
v := &npmSearchResults{} | ||
err = json.NewDecoder(resp.Body).Decode(v) | ||
if err != nil { | ||
return "", sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("failed to parse npm package json: %v", err)) | ||
} | ||
if len(v.Objects) == 0 { | ||
return "", sce.WithMessage(sce.ErrScorecardInternal, | ||
fmt.Sprintf("could not find source repo for npm package: %s", packageName)) | ||
} | ||
return v.Objects[0].Package.Links.Repository, nil | ||
} | ||
|
||
// Gets the GitHub repository URL for the pypi package. | ||
// nolint: noctx | ||
func fetchGitRepositoryFromPYPI(packageName string) (string, error) { | ||
pypiSearchURL := "https://pypi.org/pypi/%s/json" | ||
const timeout = 10 | ||
client := &http.Client{ | ||
Timeout: timeout * time.Second, | ||
} | ||
resp, err := client.Get(fmt.Sprintf(pypiSearchURL, packageName)) | ||
if err != nil { | ||
return "", sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("failed to get pypi package json: %v", err)) | ||
} | ||
|
||
defer resp.Body.Close() | ||
v := &pypiSearchResults{} | ||
err = json.NewDecoder(resp.Body).Decode(v) | ||
if err != nil { | ||
return "", sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("failed to parse pypi package json: %v", err)) | ||
} | ||
if v.Info.ProjectUrls.Source == "" { | ||
return "", sce.WithMessage(sce.ErrScorecardInternal, | ||
fmt.Sprintf("could not find source repo for pypi package: %s", packageName)) | ||
} | ||
return v.Info.ProjectUrls.Source, nil | ||
} | ||
|
||
// Gets the GitHub repository URL for the rubygems package. | ||
// nolint: noctx | ||
func fetchGitRepositoryFromRubyGems(packageName string) (string, error) { | ||
rubyGemsSearchURL := "https://rubygems.org/api/v1/gems/%s.json" | ||
const timeout = 10 | ||
client := &http.Client{ | ||
Timeout: timeout * time.Second, | ||
} | ||
resp, err := client.Get(fmt.Sprintf(rubyGemsSearchURL, packageName)) | ||
if err != nil { | ||
return "", sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("failed to get ruby gem json: %v", err)) | ||
} | ||
|
||
defer resp.Body.Close() | ||
v := &rubyGemsSearchResults{} | ||
err = json.NewDecoder(resp.Body).Decode(v) | ||
if err != nil { | ||
return "", sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("failed to parse ruby gem json: %v", err)) | ||
} | ||
if v.SourceCodeURI == "" { | ||
return "", sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("could not find source repo for ruby gem: %v", err)) | ||
} | ||
return v.SourceCodeURI, nil | ||
} |
Oops, something went wrong.