diff --git a/cmd/fetch/github.go b/cmd/fetch/github.go index eac65c5..07168ea 100644 --- a/cmd/fetch/github.go +++ b/cmd/fetch/github.go @@ -18,6 +18,10 @@ func NewGithubCommand(deps *FetchCommandDependencies) *cobra.Command { Long: "Fetch data of GitHub repositories and entire organizations", Args: cobra.MinimumNArgs(1), PreRun: func(cmd *cobra.Command, args []string) { + argsHead := []string{ + "github", + } + args = append(argsHead, args...) deps.PosthogClient.PublishCmdUse("data fetched", args) }, RunE: func(cmd *cobra.Command, args []string) error { @@ -36,6 +40,7 @@ func NewGithubCommand(deps *FetchCommandDependencies) *cobra.Command { } func execute(deps *FetchGithubDependencies, args []string) error { + githubConnectorDeps := &githubConnector.GithubConnectorDependencies{Client: deps.GithubClient} githubConnector := githubConnector.New(githubConnectorDeps) diff --git a/cmd/fetch/main.go b/cmd/fetch/main.go index 67b421e..e2ec617 100644 --- a/cmd/fetch/main.go +++ b/cmd/fetch/main.go @@ -1,6 +1,7 @@ package fetch import ( + "github.com/allero-io/allero/pkg/alleroBackendClient" "github.com/allero-io/allero/pkg/configurationManager" "github.com/allero-io/allero/pkg/posthog" "github.com/spf13/cobra" @@ -9,6 +10,7 @@ import ( type FetchCommandDependencies struct { ConfigurationManager *configurationManager.ConfigurationManager PosthogClient *posthog.PosthogClient + AlleroBackendClient *alleroBackendClient.AlleroBackendClient } var fetchCmd = &cobra.Command{ diff --git a/cmd/root.go b/cmd/root.go index a141c17..36cc9c8 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -6,7 +6,9 @@ import ( "github.com/allero-io/allero/cmd/fetch" "github.com/allero-io/allero/cmd/validate" "github.com/allero-io/allero/cmd/version" + "github.com/allero-io/allero/pkg/alleroBackendClient" "github.com/allero-io/allero/pkg/configurationManager" + "github.com/allero-io/allero/pkg/httpClient" "github.com/allero-io/allero/pkg/posthog" "github.com/allero-io/allero/pkg/rulesConfig" "github.com/fatih/color" @@ -37,26 +39,33 @@ var CliVersion string func init() { configurationManager := configurationManager.New() + httpClient, _ := httpClient.New() posthogClient, _ := posthog.New(&posthog.PosthogClientDependencies{ ConfigurationManager: configurationManager, CliVersion: CliVersion, }) - rulesConfigDeps := &rulesConfig.RulesConfigDependencies{ + alleroBackendClient, _ := alleroBackendClient.New(&alleroBackendClient.AlleroBackendClientDeps{ ConfigurationManager: configurationManager, - } - rulesConfig := rulesConfig.New(rulesConfigDeps) + HttpClient: httpClient, + }) + + rulesConfig := rulesConfig.New(&rulesConfig.RulesConfigDependencies{ + ConfigurationManager: configurationManager, + }) rootCmd.AddCommand(fetch.New(&fetch.FetchCommandDependencies{ ConfigurationManager: configurationManager, PosthogClient: posthogClient, + AlleroBackendClient: alleroBackendClient, })) rootCmd.AddCommand(validate.New(&validate.ValidateCommandDependencies{ ConfigurationManager: configurationManager, PosthogClient: posthogClient, RulesConfig: rulesConfig, + AlleroBackendClient: alleroBackendClient, })) rootCmd.AddCommand(version.New(&version.VersionCommandDependencies{ diff --git a/cmd/validate/main.go b/cmd/validate/main.go index 2f12fa2..90125fe 100644 --- a/cmd/validate/main.go +++ b/cmd/validate/main.go @@ -3,6 +3,7 @@ package validate import ( "errors" + "github.com/allero-io/allero/pkg/alleroBackendClient" "github.com/allero-io/allero/pkg/configurationManager" "github.com/allero-io/allero/pkg/posthog" "github.com/allero-io/allero/pkg/resultsPrinter" @@ -16,6 +17,7 @@ type ValidateCommandDependencies struct { RulesConfig *rulesConfig.RulesConfig ConfigurationManager *configurationManager.ConfigurationManager PosthogClient *posthog.PosthogClient + AlleroBackendClient *alleroBackendClient.AlleroBackendClient } func New(deps *ValidateCommandDependencies) *cobra.Command { diff --git a/pkg/alleroBackendClient/alleroBackendClient.go b/pkg/alleroBackendClient/alleroBackendClient.go new file mode 100644 index 0000000..9f7d838 --- /dev/null +++ b/pkg/alleroBackendClient/alleroBackendClient.go @@ -0,0 +1,52 @@ +package alleroBackendClient + +import ( + "github.com/allero-io/allero/pkg/configurationManager" + "github.com/allero-io/allero/pkg/httpClient" +) + +type AlleroBackendClientDeps struct { + HttpClient *httpClient.HttpClient + ConfigurationManager *configurationManager.ConfigurationManager +} + +type AlleroBackendClient struct { + httpClient *httpClient.HttpClient + configurationManager *configurationManager.ConfigurationManager + AlleroToken string +} + +func New(deps *AlleroBackendClientDeps) (*AlleroBackendClient, error) { + alleroToken, err := getAlleroToken(deps.HttpClient, deps.ConfigurationManager) + if err != nil { + return nil, err + } + return &AlleroBackendClient{ + httpClient: deps.HttpClient, + configurationManager: deps.ConfigurationManager, + AlleroToken: alleroToken, + }, nil +} + +func getAlleroToken(hc *httpClient.HttpClient, cm *configurationManager.ConfigurationManager) (string, error) { + userConfig, _, err := cm.GetUserConfig() + if err != nil { + return "", err + } + + if userConfig.AlleroToken == "" { + // TODO OY replace + // respBody, err := c.HttpClient.Get("token") + respBody, err := hc.Get("") + if err != nil { + return "", err + } + userConfig.AlleroToken = string(respBody) + err = cm.UpdateUserConfig(userConfig) + + if err != nil { + return "", err + } + } + return userConfig.AlleroToken, nil +} diff --git a/pkg/configurationManager/configurationManager.go b/pkg/configurationManager/configurationManager.go index b66e091..ec70e54 100644 --- a/pkg/configurationManager/configurationManager.go +++ b/pkg/configurationManager/configurationManager.go @@ -30,16 +30,24 @@ func (cm *ConfigurationManager) GetUserConfig() (*UserConfig, bool, error) { // A new user userId := uuid.New() jsonContent.MachineId = userId.String() + jsonContent.AlleroToken = "" jsonContentBytes, _ := json.MarshalIndent(jsonContent, "", " ") err = fileManager.WriteToFile(alleroUserConfig, jsonContentBytes) if err != nil { return nil, false, err } + return jsonContent, true, err } return nil, false, err } +func (cm *ConfigurationManager) UpdateUserConfig(userConfig *UserConfig) error { + alleroUserConfig := fmt.Sprintf("%s/config.json", fileManager.GetAlleroHomedir()) + jsonContentBytes, _ := json.MarshalIndent(userConfig, "", " ") + return fileManager.WriteToFile(alleroUserConfig, jsonContentBytes) +} + func (cm *ConfigurationManager) GetGithubToken() string { githubToken := os.Getenv("ALLERO_GITHUB_TOKEN") if githubToken == "" { diff --git a/pkg/configurationManager/configurations.type.go b/pkg/configurationManager/configurations.type.go index 833ff6f..34c926a 100644 --- a/pkg/configurationManager/configurations.type.go +++ b/pkg/configurationManager/configurations.type.go @@ -1,5 +1,6 @@ package configurationManager type UserConfig struct { - MachineId string `json:"machine_id"` + MachineId string `json:"machine_id"` + AlleroToken string `json:"allero_token"` } diff --git a/pkg/httpClient/httpClient.go b/pkg/httpClient/httpClient.go new file mode 100644 index 0000000..5ba28e7 --- /dev/null +++ b/pkg/httpClient/httpClient.go @@ -0,0 +1,32 @@ +package httpClient + +import ( + "io/ioutil" + "net/http" +) + +type HttpClient struct { + baseUrl string +} + +func New() (*HttpClient, error) { + return &HttpClient{ + // TODO OY replace + baseUrl: "https://api-service-prod-goxe6bbhaa-uc.a.run.app", + }, nil +} + +func (c *HttpClient) Get(relativeUrl string) ([]byte, error) { + var url string + if relativeUrl == "" { + url = c.baseUrl + } else { + url = c.baseUrl + "/" + relativeUrl + } + resp, err := http.Get(url) + if err != nil { + return nil, err + } + defer resp.Body.Close() + return ioutil.ReadAll(resp.Body) +}