-
Notifications
You must be signed in to change notification settings - Fork 142
New policies #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
New policies #19
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ba4dcaf
Add SECURITY.md policy.
jeffmendoza f5bf5e0
Outside Collaborator policy.
jeffmendoza f861259
Add Binary Artifacts policy.
jeffmendoza 2f4a932
Update Binary Artifacts policy to use new scorecard check interface.
jeffmendoza a561281
Fix imports after merging policies into single branch.
jeffmendoza 4642ff3
Move from reflect to github.com/google/go-cmp where appropriate.
jeffmendoza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,212 @@ | ||
| // Copyright 2021 Allstar 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 binary implements the Binary Artifacts security policy check from | ||
| // scorecard. | ||
| package binary | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "path" | ||
|
|
||
| "github.com/ossf/allstar/pkg/config" | ||
| "github.com/ossf/allstar/pkg/config/operator" | ||
| "github.com/ossf/allstar/pkg/policydef" | ||
|
|
||
| "github.com/google/go-github/v32/github" | ||
| "github.com/ossf/scorecard/checker" | ||
| "github.com/ossf/scorecard/checks" | ||
| "github.com/ossf/scorecard/clients/githubrepo" | ||
| "github.com/rs/zerolog/log" | ||
| ) | ||
|
|
||
| const configFile = "binary_artifacts.yaml" | ||
| const polName = "Binary Artifacts" | ||
|
|
||
| // OrgConfig is the org-level config definition for this policy. | ||
| type OrgConfig struct { | ||
| // OptConfig is the standard org-level opt in/out config, RepoOverride applies to all | ||
| // config. | ||
| OptConfig config.OrgOptConfig `yaml:"optConfig"` | ||
|
|
||
| // Action defines which action to take, default log, other: issue... | ||
| Action string `yaml:"action"` | ||
| } | ||
|
|
||
| // RepoConfig is the repo-level config for this policy. | ||
| type RepoConfig struct { | ||
| // OptConfig is the standard repo-level opt in/out config. | ||
| OptConfig config.RepoOptConfig `yaml:"optConfig"` | ||
|
|
||
| // Action overrides the same setting in org-level, only if present. | ||
| Action *string `yaml:"action"` | ||
| } | ||
|
|
||
| type mergedConfig struct { | ||
| Action string | ||
| } | ||
|
|
||
| type details struct { | ||
| Messages []checker.CheckDetail | ||
| } | ||
|
|
||
| var configFetchConfig func(context.Context, *github.Client, string, string, string, interface{}) error | ||
|
|
||
| func init() { | ||
| configFetchConfig = config.FetchConfig | ||
| } | ||
|
|
||
| // Binary is the Binary Artifacts policy object, implements policydef.Policy. | ||
| type Binary bool | ||
|
|
||
| // NewBinary returns a new Binary Artifacts policy. | ||
| func NewBinary() policydef.Policy { | ||
| var b Binary | ||
| return b | ||
| } | ||
|
|
||
| // Name returns the name of this policy, implementing policydef.Policy.Name() | ||
| func (b Binary) Name() string { | ||
| return polName | ||
| } | ||
|
|
||
| type logger struct { | ||
| Messages2 []checker.CheckDetail | ||
| } | ||
|
|
||
| func (l *logger) Info(desc string, args ...interface{}) { | ||
| cd := checker.CheckDetail{Type: checker.DetailInfo, Msg: fmt.Sprintf(desc, args...)} | ||
| l.Messages2 = append(l.Messages2, cd) | ||
| } | ||
|
|
||
| func (l *logger) Warn(desc string, args ...interface{}) { | ||
| cd := checker.CheckDetail{Type: checker.DetailWarn, Msg: fmt.Sprintf(desc, args...)} | ||
| l.Messages2 = append(l.Messages2, cd) | ||
| } | ||
|
|
||
| func (l *logger) Debug(desc string, args ...interface{}) { | ||
| cd := checker.CheckDetail{Type: checker.DetailDebug, Msg: fmt.Sprintf(desc, args...)} | ||
| l.Messages2 = append(l.Messages2, cd) | ||
| } | ||
|
|
||
| // Check performs the polcy check for this policy based on the | ||
| // configuration stored in the org/repo, implementing policydef.Policy.Check() | ||
| func (b Binary) Check(ctx context.Context, c *github.Client, owner, | ||
| repo string) (*policydef.Result, error) { | ||
| oc, rc := getConfig(ctx, c, owner, repo) | ||
| enabled := config.IsEnabled(oc.OptConfig, rc.OptConfig, repo) | ||
| log.Info(). | ||
| Str("org", owner). | ||
| Str("repo", repo). | ||
| Str("area", polName). | ||
| Bool("enabled", enabled). | ||
| Msg("Check repo enabled") | ||
|
|
||
| repoClient := githubrepo.CreateGithubRepoClient(ctx, c) | ||
jeffmendoza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err := repoClient.InitRepo(owner, repo); err != nil { | ||
| return nil, err | ||
| } | ||
| l := logger{} | ||
| cr := &checker.CheckRequest{ | ||
| Ctx: ctx, | ||
| Client: c, | ||
| RepoClient: repoClient, | ||
| HTTPClient: nil, | ||
| Owner: owner, | ||
| Repo: repo, | ||
| GraphClient: nil, | ||
| Dlogger: &l, | ||
| } | ||
| // TODO, likely this should be a "scorecard" policy that runs multiple checks | ||
| // here, and uses config to enable/disable checks. | ||
| res := checks.BinaryArtifacts(cr) | ||
| if res.Error2 != nil { | ||
| return nil, res.Error2 | ||
| } | ||
|
|
||
| var notify string | ||
| if res.Score < checker.MaxResultScore { | ||
jeffmendoza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| notify = fmt.Sprintf("Scorecard Check Binary Artifacts failed: %v\n"+ | ||
| "Please run scorecard directly for details: https://github.com/ossf/scorecard\n", | ||
| res.Reason) | ||
| } | ||
|
|
||
| return &policydef.Result{ | ||
| Enabled: enabled, | ||
| Pass: res.Score >= checker.MaxResultScore, | ||
| NotifyText: notify, | ||
| Details: details{ | ||
| Messages: l.Messages2, | ||
| }, | ||
| }, nil | ||
| } | ||
|
|
||
| // Fix implementing policydef.Policy.Fix(). Scorecard checks will not have a Fix option. | ||
| func (b Binary) Fix(ctx context.Context, c *github.Client, owner, repo string) error { | ||
| log.Warn(). | ||
| Str("org", owner). | ||
| Str("repo", repo). | ||
| Str("area", polName). | ||
| Msg("Action fix is configured, but not implemented.") | ||
| return nil | ||
| } | ||
|
|
||
| // GetAction returns the configured action from this policy's configuration | ||
| // stored in the org-level repo, default log. Implementing | ||
| // policydef.Policy.GetAction() | ||
| func (b Binary) GetAction(ctx context.Context, c *github.Client, owner, repo string) string { | ||
| oc, rc := getConfig(ctx, c, owner, repo) | ||
| mc := mergeConfig(oc, rc, repo) | ||
| return mc.Action | ||
| } | ||
|
|
||
| func getConfig(ctx context.Context, c *github.Client, owner, repo string) (*OrgConfig, *RepoConfig) { | ||
| oc := &OrgConfig{ // Fill out non-zero defaults | ||
| Action: "log", | ||
| } | ||
| if err := configFetchConfig(ctx, c, owner, operator.OrgConfigRepo, configFile, oc); err != nil { | ||
| log.Error(). | ||
| Str("org", owner). | ||
| Str("repo", operator.OrgConfigRepo). | ||
| Str("area", polName). | ||
| Str("file", configFile). | ||
| Err(err). | ||
| Msg("Unexpected config error, using defaults.") | ||
| } | ||
| rc := &RepoConfig{} | ||
| if err := configFetchConfig(ctx, c, owner, repo, path.Join(operator.RepoConfigDir, configFile), rc); err != nil { | ||
| log.Error(). | ||
| Str("org", owner). | ||
| Str("repo", repo). | ||
| Str("area", polName). | ||
| Str("file", path.Join(operator.RepoConfigDir, configFile)). | ||
| Err(err). | ||
| Msg("Unexpected config error, using defaults.") | ||
| } | ||
| return oc, rc | ||
| } | ||
|
|
||
| func mergeConfig(oc *OrgConfig, rc *RepoConfig, repo string) *mergedConfig { | ||
| mc := &mergedConfig{ | ||
| Action: oc.Action, | ||
| } | ||
|
|
||
| if !oc.OptConfig.DisableRepoOverride { | ||
| if rc.Action != nil { | ||
| mc.Action = *rc.Action | ||
| } | ||
| } | ||
| return mc | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.