-
Notifications
You must be signed in to change notification settings - Fork 26
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
add identity search workflow #491
Open
linus-sun
wants to merge
1
commit into
sigstore:main
Choose a base branch
from
linus-sun:linussun/yaml-configuration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 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,37 @@ | ||
// | ||
// Copyright 2024 The Sigstore 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 main | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/sigstore/rekor-monitor/pkg/identity" | ||
"github.com/sigstore/rekor-monitor/pkg/notifications" | ||
) | ||
|
||
type IdentityMonitorConfiguration struct { | ||
StartIndex *int `yaml:"startIndex"` | ||
EndIndex *int `yaml:"endIndex"` | ||
MonitoredValues identity.MonitoredValues `yaml:"monitoredValues"` | ||
ServerURL string `yaml:"serverURL"` | ||
OutputIdentitiesFile string `yaml:"outputIdentities"` | ||
LogInfoFile string `yaml:"logInfoFile"` | ||
GitHubIssue notifications.GitHubIssueInput `yaml:"githubIssue"` | ||
EmailNotificationSMTP notifications.EmailNotificationInput `yaml:"emailNotificationSMTP"` | ||
EmailNotificationMailgun notifications.MailgunNotificationInput `yaml:"emailNotificationMailgun"` | ||
EmailNotificationSendGrid notifications.SendGridNotificationInput `yaml:"emailNotificationSendGrid"` | ||
Interval *time.Duration `yaml:"interval"` | ||
} |
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,143 @@ | ||
// | ||
// Copyright 2021 The Sigstore 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 main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
"runtime" | ||
"strings" | ||
"time" | ||
|
||
"github.com/sigstore/rekor-monitor/pkg/rekor" | ||
"github.com/sigstore/rekor-monitor/pkg/util/file" | ||
"github.com/sigstore/rekor/pkg/client" | ||
"github.com/sigstore/rekor/pkg/util" | ||
"gopkg.in/yaml.v2" | ||
"sigs.k8s.io/release-utils/version" | ||
) | ||
|
||
// Default values for monitoring job parameters | ||
const ( | ||
publicRekorServerURL = "https://rekor.sigstore.dev" | ||
logInfoFileName = "logInfo.txt" | ||
outputIdentitiesFileName = "identities.txt" | ||
) | ||
|
||
// This main function performs a periodic identity search. | ||
// Upon starting, any existing latest snapshot data is loaded and the function runs | ||
// indefinitely to perform identity search for every time interval that was specified. | ||
func main() { | ||
// Command-line flags that are parameters to the verifier job | ||
configFilePath := flag.String("config-file", "", "path to yaml configuration file containing identity monitor settings") | ||
once := flag.Bool("once", true, "whether to run the monitor on a repeated interval or once") | ||
flag.Parse() | ||
|
||
if configFilePath == nil { | ||
log.Fatalf("empty configuration file path") | ||
} | ||
|
||
readConfig, err := os.ReadFile(*configFilePath) | ||
if err != nil { | ||
log.Fatalf("error reading from identity monitor configuration file: %v", err) | ||
} | ||
|
||
configString := string(readConfig) | ||
var config IdentityMonitorConfiguration | ||
if err := yaml.Unmarshal([]byte(configString), &config); err != nil { | ||
log.Fatalf("error parsing identities: %v", err) | ||
} | ||
|
||
rekorClient, err := client.GetRekorClient(config.ServerURL, client.WithUserAgent(strings.TrimSpace(fmt.Sprintf("rekor-monitor/%s (%s; %s)", version.GetVersionInfo().GitVersion, runtime.GOOS, runtime.GOARCH)))) | ||
if err != nil { | ||
log.Fatalf("getting Rekor client: %v", err) | ||
} | ||
|
||
if config.StartIndex == nil || config.EndIndex == nil { | ||
logInfo, err := rekor.GetLogInfo(context.Background(), rekorClient) | ||
if err != nil { | ||
log.Fatalf("error getting log info: %v", err) | ||
} | ||
|
||
checkpoint, err := rekor.ReadLatestCheckpoint(logInfo) | ||
if err != nil { | ||
log.Fatalf("error reading checkpoint: %v", err) | ||
} | ||
|
||
var prevCheckpoint *util.SignedCheckpoint | ||
prevCheckpoint, err = file.ReadLatestCheckpoint(config.LogInfoFile) | ||
if err != nil { | ||
log.Fatalf("reading checkpoint log: %v", err) | ||
} | ||
|
||
checkpointStartIndex, checkpointEndIndex := rekor.GetCheckpointIndices(logInfo, prevCheckpoint, checkpoint) | ||
if config.StartIndex == nil { | ||
config.StartIndex = &checkpointStartIndex | ||
} | ||
if config.EndIndex == nil { | ||
config.EndIndex = &checkpointEndIndex | ||
} | ||
} | ||
|
||
if config.ServerURL == "" { | ||
config.ServerURL = publicRekorServerURL | ||
} | ||
if config.LogInfoFile == "" { | ||
config.LogInfoFile = logInfoFileName | ||
} | ||
if config.OutputIdentitiesFile == "" { | ||
config.OutputIdentitiesFile = outputIdentitiesFileName | ||
} | ||
if config.Interval == nil { | ||
defaultInterval := time.Hour | ||
config.Interval = &defaultInterval | ||
} | ||
|
||
ticker := time.NewTicker(*config.Interval) | ||
defer ticker.Stop() | ||
|
||
// To get an immediate first tick | ||
for ; ; <-ticker.C { | ||
err = rekor.IdentitySearch(*config.StartIndex, *config.EndIndex, rekorClient, config.MonitoredValues, config.OutputIdentitiesFile) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "failed to successfully complete identity search: %v", err) | ||
return | ||
} | ||
|
||
if *once { | ||
return | ||
} | ||
|
||
prevCheckpoint, checkpoint, err := rekor.GetPrevCurrentCheckpoints(rekorClient, config.LogInfoFile) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "failed to fetch previous and current checkpoints: %v", err) | ||
return | ||
} | ||
|
||
logInfo, err := rekor.GetLogInfo(context.Background(), rekorClient) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "failed to fetch log info: %v", err) | ||
return | ||
} | ||
|
||
checkpointStartIndex, checkpointEndIndex := rekor.GetCheckpointIndices(logInfo, prevCheckpoint, checkpoint) | ||
config.StartIndex = &checkpointStartIndex | ||
config.EndIndex = &checkpointEndIndex | ||
} | ||
} |
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also include
once
so this either runs indefinitely or only for one iteration?If so, this would need to be restructured a bit, so that we continually pull in the start index from the last checkpoint that was recorded.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
related, I think if you specify start/end, that should effectively be the same as
once
, as a one-off search across indices (which could be used if you are running the search for the first time, for example, or catching up)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was considering how to include once but I think that makes sense for constantly pulling in the last checkpoint, will do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also decide between the indefinite loop being in the cmd package or in the inner API. Right now, the functionality is embedded in the API, and I wouldn’t expect API users to need to loop indefinitely. Also it’s not possible to test an endless loop, so we can maintain good coverage in the API where the cmd package isn’t expected to have coverage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looping in the cmd package definitely makes more sense- will implement that way in this PR, and refactor consistency check as such in #492 (edit: #492 seems a bit too big, will refactor this in a future PR)