-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add config parsing and launching agent in CLI
- Loading branch information
Showing
7 changed files
with
137 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package main | ||
|
||
type config struct { | ||
RekorServerURL string | ||
RekorServerURL string `yaml:"rekorServerURL"` | ||
} |
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 @@ | ||
rekorServerURL: https://rekor.sigstore.dev |
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 |
---|---|---|
@@ -1,36 +1,104 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/oklog/run" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
// ErrNotImplimented signals incomplete code path | ||
ErrNotImplimented = errors.New(`rekor-sidekick: not implemented`) | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func newCLI() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "rekor-sidekick", | ||
Short: "Transparency log monitoring and alerting", | ||
Long: "Daemon that monitors a Rekor instance and forwards entries of interest to configurable destinations", | ||
Run: runCLI, | ||
} | ||
|
||
cmd.Flags().String("config", "/etc/rekor-sidekick/config.yaml", "Path to configuration file") | ||
return cmd | ||
} | ||
|
||
func runCLI(cmd *cobra.Command, args []string) { | ||
fmt.Println(ErrNotImplimented) | ||
os.Exit(1) | ||
if err := viper.BindPFlags(cmd.Flags()); err != nil { | ||
fmt.Println("Failed to bind command line flags to viper:", err) | ||
os.Exit(1) | ||
} | ||
|
||
// Environment variable setup | ||
viper.SetEnvPrefix(`REKOR_SIDEKICK`) | ||
viper.AutomaticEnv() | ||
|
||
// Load config file | ||
{ | ||
f, err := os.Open(viper.GetString("config")) | ||
if err != nil { | ||
fmt.Println("Failed to open config file:", err) | ||
os.Exit(1) | ||
} | ||
defer f.Close() | ||
|
||
viper.SetConfigType("yaml") | ||
if err := viper.ReadConfig(f); err != nil { | ||
fmt.Println("Failed to parse config:", err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
var c config | ||
if err := viper.Unmarshal(&c); err != nil { | ||
fmt.Println("Failed to load configuration:", err) | ||
os.Exit(1) | ||
} | ||
|
||
a, err := newAgent(c) | ||
if err != nil { | ||
fmt.Println("Failed to initialize agent:", err) | ||
os.Exit(1) | ||
} | ||
|
||
// This run group manages all the concurrent processes | ||
// this command runs. That is the agent and a signal handler | ||
// at the moment. | ||
var g run.Group | ||
|
||
// Agent process | ||
g.Add(a.run, func(error) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
defer cancel() | ||
a.shutdown(ctx) | ||
}) | ||
|
||
// Signal handler process | ||
{ | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
g.Add(func() error { | ||
c := make(chan os.Signal, 1) | ||
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) | ||
select { | ||
case sig := <-c: | ||
return fmt.Errorf("received signal %s", sig) | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
} | ||
}, func(error) { | ||
cancel() | ||
}) | ||
} | ||
|
||
// Launch! | ||
if err = g.Run(); err != nil { | ||
log.Println(err) | ||
} | ||
|
||
} | ||
|
||
func main() { | ||
cmd := newCLI() | ||
|
||
cmd.Execute() | ||
} |
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