Skip to content
This repository has been archived by the owner on Jul 19, 2023. It is now read-only.

Feature/get token #13

Merged
merged 10 commits into from
Sep 1, 2022
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
5 changes: 5 additions & 0 deletions cmd/fetch/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions cmd/fetch/main.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -9,6 +10,7 @@ import (
type FetchCommandDependencies struct {
ConfigurationManager *configurationManager.ConfigurationManager
PosthogClient *posthog.PosthogClient
AlleroBackendClient *alleroBackendClient.AlleroBackendClient
}

var fetchCmd = &cobra.Command{
Expand Down
15 changes: 12 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down
2 changes: 2 additions & 0 deletions cmd/validate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down
52 changes: 52 additions & 0 deletions pkg/alleroBackendClient/alleroBackendClient.go
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 8 additions & 0 deletions pkg/configurationManager/configurationManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
3 changes: 2 additions & 1 deletion pkg/configurationManager/configurations.type.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package configurationManager

type UserConfig struct {
MachineId string `json:"machine_id"`
MachineId string `json:"machine_id"`
AlleroToken string `json:"allero_token"`
}
32 changes: 32 additions & 0 deletions pkg/httpClient/httpClient.go
Original file line number Diff line number Diff line change
@@ -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)
}