Skip to content
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

optionally load default configs from semgrep #72

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var dumpCmd = &cobra.Command{
Use: "dump",
Short: "Dump current config",
Run: func(cmd *cobra.Command, args []string) {
config, err := pkg.LoadConfig(configFiles)
config, err := pkg.LoadConfig(configFiles, deploymentId)
if err != nil {
log.Panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var relayCmd = &cobra.Command{
}()

// load config(s)
config, err := pkg.LoadConfig(configFiles)
config, err := pkg.LoadConfig(configFiles, 0)
if err != nil {
log.Panic(err)
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

var configFiles []string
var jsonLog bool
var deploymentId int

var rootCmd = &cobra.Command{
Use: "semgrep-network-broker",
Expand All @@ -38,7 +39,7 @@ var rootCmd = &cobra.Command{
}()

// load config(s)
config, err := pkg.LoadConfig(configFiles)
config, err := pkg.LoadConfig(configFiles, deploymentId)
if err != nil {
log.Panic(err)
}
Expand Down Expand Up @@ -93,4 +94,5 @@ func Execute() {
func init() {
rootCmd.PersistentFlags().StringArrayVarP(&configFiles, "config", "c", nil, "config file(s)")
rootCmd.PersistentFlags().BoolVarP(&jsonLog, "json-log", "j", false, "JSON log output")
rootCmd.PersistentFlags().IntVarP(&deploymentId, "deployment-id", "d", 0, "Semgrep deployment ID")
}
44 changes: 43 additions & 1 deletion pkg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"reflect"
"strings"

Expand Down Expand Up @@ -251,8 +254,47 @@ type Config struct {
Outbound OutboundProxyConfig `mapstructure:"outbound" json:"outbound"`
}

func LoadConfig(configFiles []string) (*Config, error) {
func LoadConfig(configFiles []string, deploymentId int) (*Config, error) {
config := new(Config)

if deploymentId > 0 {
hostname := os.Getenv("SEMGREP_HOSTNAME")
if hostname == "" {
hostname = "semgrep.dev"
}
url := url.URL{
Scheme: "https",
Host: hostname,
Path: fmt.Sprintf("/api/broker/%d/default-config", deploymentId),
}

resp, err := http.Get(url.String())
if err != nil {
return nil, fmt.Errorf("failed to request default broker config from %v: %v", hostname, err)
}

if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to request default config from %s: HTTP %v", url.String(), resp.StatusCode)
}

f, err := os.CreateTemp("", "default-config*.json")
if err != nil {
return nil, fmt.Errorf("failed to create temp file to store default config: %v", err)
}
defer func() {
f.Close()
os.Remove(f.Name())
}()

io.Copy(f, resp.Body)
defer resp.Body.Close()

viper.SetConfigFile(f.Name())
if err := viper.MergeInConfig(); err != nil {
return nil, fmt.Errorf("failed to merge config file '%s': %v", f.Name(), err)
}
}

for i := range configFiles {
viper.SetConfigFile(configFiles[i])
if err := viper.MergeInConfig(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestEmptyConfigs(t *testing.T) {
config, err := LoadConfig(nil)
config, err := LoadConfig(nil, 0)
if err != nil {
t.Error(err)
}
Expand Down
Loading